﻿function EmailsIndexer()
{
	$("#emailsTable tr").each(function(index)
	{
		$(this).find("select[name$=.Type]").attr("name", "Emails[" + index + "].Type");
		$(this).find("input[name$=.Address]").attr("name", "Emails[" + index + "].Address");
	});
}

function PhonesIndexer()
{
	$("#phonesTable tr").each(function(index)
	{
		$(this).find("select[name$=.Type]").attr("name", "Phones[" + index + "].Type");
		$(this).find("input[name$=.Number]").attr("name", "Phones[" + index + "].Number");
	});
}

function AddressIndexer()
{
	$("#addressesTable tr").each(function(index)
	{
		$(this).find("select[name$=.Type]").attr("name", "Addresses[" + (index - 1) + "].Type");
		$(this).find("input[name$=.PostalCode]").attr("name", "Addresses[" + (index - 1) + "].PostalCode");
		$(this).find("select[name$=.Country]").attr("name", "Addresses[" + (index - 1) + "].Country");
		$(this).find("input[name$=.Region]").attr("name", "Addresses[" + (index - 1) + "].Region");
		$(this).find("input[name$=.City]").attr("name", "Addresses[" + (index - 1) + "].City");
		$(this).find("input[name$=.Street]").attr("name", "Addresses[" + (index - 1) + "].Street");
	});
	
	$("#addressesTableHeader").css("display", ($("#addressesTable tr").length > 1) ? "" : "none");
}

function WebsitesIndexer()
{
	$("#websitesTable tr").each(function(index)
	{
		$(this).find("input[name$=.Type]").attr("name", "Websites[" + index + "].Type");
		$(this).find("input[name$=.Url]").attr("name", "Websites[" + index + "].Url");
	});
}

$(document).ready(function()
{
	$("#addEmail").click(
		function()
		{
			$("#emailsTable").append($("#newEmail tbody").html());
			EmailsIndexer();
			
			$("#emailsTable a").click(function()
			{
				$(this).parent().parent().remove();
				EmailsIndexer();
				return false;
			});

			return false;
		});

	$("#emailsTable a").click(
		function()
		{
			$(this).parent().parent().remove();
			EmailsIndexer();
			return false;
		});

	$("#addPhone").click(
		function()
		{
			$("#phonesTable").append($("#newPhone tbody").html());
			PhonesIndexer();

			$("#phonesTable a").click(function()
			{
				$(this).parent().parent().remove();
				PhonesIndexer();
				return false;
			});

			return false;
		});

	$("#phonesTable a").click(
		function()
		{
			$(this).parent().parent().remove();
			PhonesIndexer();
			return false;
		});

	$("#addAddress").click(
		function()
		{
			$("#addressesTable").append($("#newAddress").html());
			AddressIndexer();

			$("#addressesTable a").click(function()
			{
				$(this).parent().parent().remove();
				AddressIndexer();
				return false;
			});

			return false;
		});

	$("#addressesTable a").click(
		function()
		{
			$(this).parent().parent().remove();
			AddressIndexer();
			return false;
		});

	$("#addressesTableHeader").css("display", ($("#addressesTable tr").length > 1) ? "" : "none");

	$("#addWebsite").click(
		function()
		{
			$("#websitesTable").append($("#newWebsite tbody").html());
			WebsitesIndexer();
			
			$("#websitesTable a").click(function()
			{
				$(this).parent().parent().remove();
				WebsitesIndexer();
				return false;
			});

			return false;
		});

	$("#websitesTable a").click(
		function()
		{
			$(this).parent().parent().remove();
			WebsitesIndexer();
			return false;
		});

	$("#tagsGroup a.checkoxDiv").click(
		function()
		{
			if ($(this).attr("class") == "checkoxDiv")
			{
				$(this).attr("class", "checkoxDiv checkoxDivClick")
				$(this).find("input").attr("value", "true");
			}
			else
			{
				$(this).attr("class", "checkoxDiv");
				$(this).find("input").attr("value", "false");
			}

			return false;
		});

	$("#tagsGroup input[value=true]").each(
		function(index)
		{
			$(this).parent().attr("class", "checkoxDiv checkoxDivClick")
		});

	$("#DeleteContacts").click(
		function()
		{
			$("#ListForm").attr("action", "/Contact/DeleteContacts");
			$("#ListForm").submit();
			return false;
		});

	$("#RestoreContacts").click(
		function()
		{
			$("#ListForm").attr("action", "/Contact/RestoreContacts");
			$("#ListForm").submit();
			return false;
		});

	$("#AddGroups").click(
		function()
		{
			$("#tagsGroup input[value=true]").each(
				function(index)
				{
					$("#ListForm").append($(this));
				});

			$("#ListForm").attr("action", "/Contact/AddContacts");
			$("#ListForm").submit();
			return false;
		});

	$("#RemoveGroups").click(
		function()
		{
			$("#ListForm").attr("action", "/Contact/RemoveContacts" + window.location.search);
			$("#ListForm").submit();
			return false;
		});

	$("#SelectAllContacts").click(
		function()
		{
			$("#ListForm input").attr("checked", "checked");
			return false;
		});

	$("#UnselectAllContacts").click(
		function()
		{
			$("#ListForm input").removeAttr("checked");
			return false;
		});

	$(".tree li").hover(
		function ()
		{
			$(this).find("a.redSmall").show();
		}, 
		function ()
		{
			$(this).find("a.redSmall").hide();
		});
});
