/// <reference path="jquery-vsdoc.js" /> 
/// <reference path="trinet.js" /> 

jQuery(function()
{
	var showCompany = function()
	{
		var row = jQuery("form[name$=searchForm] tr[id$=companyRow]");
		var list = jQuery("form[name$=searchForm] span[id$=lookIn] input:checkbox:checked").filter(function()
		{
			return "notes" === this.value || "requests" === this.value || "contacts" === this.value;
		});
		
		if (0 === list.length)
		{
			jQuery("select", row).attr("disabled", true);
			// row.hide();
		}
		else
		{
			jQuery("select", row).attr("disabled", false);
			// row.show();
		}
	};
	
	var showContact = function()
	{
		var row = jQuery("form[name$=searchForm] tr[id$=contactRow]");
		var list = jQuery("form[name$=searchForm] span[id$=lookIn] input:checkbox:checked").filter(function()
		{
			return "requests" === this.value;
		});
		
		if (0 === list.length)
		{
			jQuery("select", row).attr("disabled", true);
			// row.hide();
		}
		else
		{
			jQuery("select", row).attr("disabled", false);
			// row.show();
		}
	};
	
	var showDate = function()
	{
		var row = jQuery("form[name$=searchForm] tr[id$=dateRangeRow1], form[name$=searchForm] tr[id$=dateRangeRow2]");
		var list = jQuery("form[name$=searchForm] span[id$=lookIn] input:checkbox:checked[value=requests]");
		if (0 === list.length)
		{
			jQuery("input:text", row).attr("disabled", true);
			// row.hide();
		}
		else
		{
			jQuery("input:text", row).attr("disabled", false);
			// row.show();
		}
	};
	
	var showInternet = function()
	{
		var list = jQuery("form[name$=searchForm] span[id$=lookIn] input:checkbox:checked[value=internet]");
		if (0 === list.length)
		{
			jQuery("#multiSearchFormContainer").hide();
		}
		else
		{
			jQuery("#multiSearchFormContainer").show();
		}
	};
	
	jQuery("form[name$=searchForm] span[id$=lookIn] input:checkbox, form[name$=searchForm] div[id=lookInCommands] a").click(function()
	{
		showCompany();
		showContact();
		showDate();
		showInternet();
	});
	
	jQuery("form[name$=searchForm]").submit(function()
	{
		var txt = jQuery("form[name$=searchForm] input[name=q]");
		if (0 === txt.val().length)
		{
			var list = jQuery("form[name$=searchForm] span[id$=lookIn] input:checkbox:checked[value=requests]");
			if (0 === list.length)
			{
				alert("Please enter something to search for!");
				txt.setFocus();
				return false;
			}
			
			txt.each(function() { this.value = ""; });
		}
	});
	
	jQuery("form[name$=searchForm] select[id$=companyID]").change(function()
	{
		if (-1 == this.selectedIndex || 0 == this.selectedIndex)
		{
			jQuery("form[name$=searchForm] select[id$=contactID]").clearList(1);
		}
		else
		{
			var companyId = parseInt(this.options[this.selectedIndex].value, 10);
			Trinet.Web.Site.Services.Contacts.ContactService.ListContacts(companyId, null, function(result)
			{
				jQuery("form[name$=searchForm] select[id$=contactID]").loadList(result, 
				{
					staticItemCount: 1, 
					dataValueField: "ContactId",
					dataTextField: "ContactName",
					dataGroupField: "SiteName"
				})
				.setFocus();
			});
		}
	});
	
	showCompany();
	showContact();
	showDate();
	showInternet();
	
	var list = jQuery("form[name$=searchForm] span[id$=lookIn] input:checkbox:checked");
	if (1 === list.length && "internet" === list.attr("value"))
	{
		if (0 !== jQuery("form[name$=searchForm] input[name=q]").val().length)
		{
			setTimeout(function() { jQuery("form[name=multiSearchForm]").trigger("submit"); }, 1000);
		}
	}
});