﻿// Functions for job searching
// June 2007 : Javascript
// Tim Surtell @ Clario

var objDDTimeout;

//************************************************************************************************************
function SetupPage()
	{
	// Setup expanding table
	SetupExpandingTable('job_view_ajax.aspx?JobID=', false, true, true)
	
	// Set specialisms and areas
	if (document.getElementById("lstSpecialisms_lstSpecialisms"))
		{
		SpecialismChanged(document.getElementById("lstSpecialisms_lstSpecialisms"));
		AreaChanged(document.getElementById("lstAreas_lstAreas"));
		}
		
	// Send Site to a Friend
	AutoSendSiteToAFriend()
	}
//************************************************************************************************************
function HoursClicked(Hours)
	{
	if (document.getElementById("chkFulltime").checked == false && document.getElementById("chkParttime").checked == false)
		{
		if (Hours.id == "chkFulltime")
			{
				document.getElementById("chkParttime").checked = true;
			}
			else
			{
				document.getElementById("chkFulltime").checked = true;
			}
		}
	}
//************************************************************************************************************
function TypeClicked(Type)
	{
	if (document.getElementById("chkPermanent").checked == false && document.getElementById("chkTemporaryContract").checked == false)
		{
		if (Type.id == "chkPermanent")
			{
				document.getElementById("chkTemporaryContract").checked = true;
			}
			else
			{
				document.getElementById("chkPermanent").checked = true;
			}
		}
	}
//************************************************************************************************************
function SpecialismsClicked()
	{

	CloseJobsByEmail();

	if (document.getElementById("tblSpecialisms").style.display == "block")
		{
		document.getElementById("tblSpecialisms").style.display = "none";
		document.getElementById("aChangeSpecialisms").innerHTML = "change";
		}
	else
		{
		document.getElementById("tblSpecialisms").style.display = "block";
		document.getElementById("aChangeSpecialisms").innerHTML = "hide";
		}

	document.getElementById("tblAreas").style.display = "none";
	document.getElementById("aChangeAreas").innerHTML = "change";
	}
//************************************************************************************************************
function AreasClicked()
	{
	
	CloseJobsByEmail();

	if (document.getElementById("tblAreas").style.display == "block")
		{
		document.getElementById("tblAreas").style.display = "none";
		document.getElementById("aChangeAreas").innerHTML = "change";
		}
	else
		{
		document.getElementById("tblAreas").style.display = "block";
		document.getElementById("aChangeAreas").innerHTML = "hide";
		}

	document.getElementById("tblSpecialisms").style.display = "none";
	document.getElementById("aChangeSpecialisms").innerHTML = "change";
	}
//************************************************************************************************************
function SpecialismChanged(Specialism)
	{
	
	if (!document.getElementById("aSpecialisms")) { return; }
	
	// Don't select separators
	for (var LoopCounter = 0; LoopCounter < Specialism.length; LoopCounter++)
		{
		if (Specialism.options[LoopCounter].value == "")
			{
			Specialism.options[LoopCounter].selected = false;
			}
		}

	// Update specialisms text
	document.getElementById("aSpecialisms").innerHTML = "";
	for (var LoopCounter = 0; LoopCounter < Specialism.options.length; LoopCounter++)
		{
		if (Specialism.options[LoopCounter].selected)
			{
			document.getElementById("aSpecialisms").innerHTML = document.getElementById("aSpecialisms").innerHTML + Specialism.options[LoopCounter].text + ", ";
			}
		}
	
	// If nothing is selected, select all
	if (document.getElementById("aSpecialisms").innerHTML == "")
		{
		Specialism.options[0].selected = true;
		document.getElementById("aSpecialisms").innerHTML = Specialism.options[0].text + ", ";
		}
	
	// Remove leading spaces
	document.getElementById("aSpecialisms").innerHTML = document.getElementById("aSpecialisms").innerHTML.replace(/&nbsp;&nbsp;&nbsp;&nbsp;/g, "");

	// Remove raquos
	document.getElementById("aSpecialisms").innerHTML = document.getElementById("aSpecialisms").innerHTML.replace(/» /g, "");
	
	// Remove trailing comma
	document.getElementById("aSpecialisms").innerHTML = document.getElementById("aSpecialisms").innerHTML.substr(0, document.getElementById("aSpecialisms").innerHTML.length - 2);

	// Restrict wrapping to two lines
	if (document.getElementById("aSpecialisms").innerHTML.length > 55)
		{
		document.getElementById("aSpecialisms").innerHTML = document.getElementById("aSpecialisms").innerHTML.substr(0, 55) + "...";
		}
	}
//************************************************************************************************************
function AreaChanged(Area)
	{

	if (!document.getElementById("aAreas")) { return; }

	// Update areas text
	document.getElementById("aAreas").innerHTML = "";
	for (var LoopCounter = 0; LoopCounter < Area.options.length; LoopCounter++)
		{
		if (Area.options[LoopCounter].selected)
			{
			document.getElementById("aAreas").innerHTML = document.getElementById("aAreas").innerHTML + Area.options[LoopCounter].text + ", ";
			}
		}
	
	// If nothing is selected, select all
	if (document.getElementById("aAreas").innerHTML == "")
		{
		Area.options[0].selected = true;
		document.getElementById("aAreas").innerHTML = Area.options[0].text + ", ";
		}
	
	// Remove leading spaces
	document.getElementById("aAreas").innerHTML = document.getElementById("aAreas").innerHTML.replace(/&nbsp;&nbsp;&nbsp;&nbsp;/g, "");

	// Remove raquos
	document.getElementById("aAreas").innerHTML = document.getElementById("aAreas").innerHTML.replace(/» /g, "");
	
	// Remove trailing comma
	document.getElementById("aAreas").innerHTML = document.getElementById("aAreas").innerHTML.substr(0, document.getElementById("aAreas").innerHTML.length - 2);

	// Restrict wrapping to two lines
	if (document.getElementById("aAreas").innerHTML.length > 55)
		{
		document.getElementById("aAreas").innerHTML = document.getElementById("aAreas").innerHTML.substr(0, 55) + "...";
		}
	}
//************************************************************************************************************
	function JobsByEmailKeydown()
		{
		OpenJobsByEmail();
		clearTimeout(objDDTimeout);
		objDDTimeout = setTimeout("CloseJobsByEmail()", 10000);
		}
//************************************************************************************************************
	function JobsByEmailKeypress(objEvent)
		{
		if (window.event)
				{
				if (objEvent.keyCode == 13)
					{
					JobsByEmail();
					return false;
					}
				}
			else
				{
				if (objEvent.which == 13)
					{
					JobsByEmail();
					return false;
					}
				}
		}
//************************************************************************************************************
	function OpenJobsByEmail()
		{
		document.getElementById("imgJobsByEmailDDTopRight").src = "Images/Tabs/dd_top_right_over.gif";
		document.getElementById("tblJobsByEmail").style.left = (parseInt(GetElementX(document.getElementById("tdJobsByEmailMarker"))) - 29) + "px";
		document.getElementById("tblJobsByEmail").style.top = (parseInt(GetElementY(document.getElementById("tdJobsByEmailMarker"))) + 26) + "px";
		document.getElementById("tblJobsByEmail").style.display = "block";
		clearTimeout(objDDTimeout);
		objDDTimeout = setTimeout("CloseJobsByEmail()", 10000);
		}
//************************************************************************************************************
	function CloseJobsByEmail()
		{
		if (document.getElementById("tdJobsByEmail"))
			{
			document.getElementById("imgJobsByEmailDDTopRight").src = "Images/Tabs/dd_top_right.gif";
			document.getElementById("tblJobsByEmail").style.display = "none";
			}
		}
//************************************************************************************************************
	function JobsByEmail()
		{
    var SpecialismIDs;
    var AreaIDs;
    var Permanent;
    var TemporaryContract;
    var Fulltime;
    var Parttime;
    var EmailAddress;
    
		CloseJobsByEmail();
    
    // Get the SpecialismIDs
    SpecialismIDs = "";
    for (var LoopCounter = 0; LoopCounter < document.getElementById("lstSpecialisms_lstSpecialisms").options.length; LoopCounter++)
		{
		if (document.getElementById("lstSpecialisms_lstSpecialisms").options[LoopCounter].selected)
			{
			SpecialismIDs = SpecialismIDs + document.getElementById("lstSpecialisms_lstSpecialisms").options[LoopCounter].value + ",";
			}
		}

		// Remove trailing comma
		SpecialismIDs = SpecialismIDs.substr(0, SpecialismIDs.length - 1);

    // Get the AreaIDs
    AreaIDs = "";
    for (var LoopCounter = 0; LoopCounter < document.getElementById("lstAreas_lstAreas").options.length; LoopCounter++)
		{
		if (document.getElementById("lstAreas_lstAreas").options[LoopCounter].selected)
			{
			AreaIDs = AreaIDs + document.getElementById("lstAreas_lstAreas").options[LoopCounter].value + ",";
			}
		}

		// Remove trailing comma
		AreaIDs = AreaIDs.substr(0, AreaIDs.length - 1);

		// Get the other criteria
		Permanent = document.getElementById("chkPermanent").checked;
		TemporaryContract = document.getElementById("chkTemporaryContract").checked;
		Fulltime = document.getElementById("chkFulltime").checked;
		Parttime = document.getElementById("chkParttime").checked;

    // Retrieve fields
    EmailAddress = document.getElementById("txtJBEEmailAddress").value;
    
    // Validate fields
    if (IsValidEmail(EmailAddress) == false)
			{
			alert("Please enter a valid Email Address");
			document.getElementById("txtJBEEmailAddress").focus();
			return false;
			}
			
		var xmlHttp;
			
		// Instantiate a XMLHttpRequest object
		try
			{
			// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();
			}
		catch (e)
			{
			// Internet Explorer
			try
				{
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
			catch (e)
				{
				try
					{
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
				catch (e)
					{
					alert("Your browser does not support AJAX!");
					}
				}
			}
			
		// Create a function to respond to XMLHttpRequest state changes
		xmlHttp.onreadystatechange = function()
			{
			if(xmlHttp.readyState == 4)
				{
				// Get return value
				var ReturnValue = xmlHttp.responseText;

				if (ReturnValue == "True")
					{
					alert("Thank you for signing up to Jobs by Email.\n\nWe have sent an email to " + EmailAddress + " introducing you to the service and including\nyour password so that you can log into your greatcare.co.uk account in the future.\n\nDidn't get our email?\n\nPlease check your spam folder and ensure that emails from greatcare.co.uk are not\ntreated as spam. Please check also that you have entered your email address correctly.\nIf it is incorrect, you can enter it again.");
					}
				else
					{
					alert("Please enter a valid email address.");
					}
				}
			}
			
			// Send request using XMLHttpRequest
			xmlHttp.open("GET", ajaxPath + "candidate_register.aspx?AJAX=True&SpecialismIDs=" + SpecialismIDs + "&AreaIDs=" + AreaIDs + "&Permanent=" + Permanent + "&TemporaryContract=" + TemporaryContract + "&Fulltime=" + Fulltime + "&Parttime=" + Parttime + "&EmailAddress=" + EmailAddress, true);
			xmlHttp.send(null);
			
		return false;
		}

