﻿// Functions for job details/application
// June 2007 : Javascript
// Tim Surtell @ Clario

var objDDTimeout;

//************************************************************************************************************
function SetupPage()
	{
	// Setup expanding table
	SetupExpandingTable('job_view_ajax.aspx?JobID=', false, true, true)
	
	// Send Site to a Friend
	AutoSendSiteToAFriend()

	if (document.getElementById("chkShowProfile"))
		{
		// Show Profile
		ShowProfileClicked();
		}

	if (document.getElementById("txtName"))
		{
		// Override OnClick event for finish button
		document.getElementById("btnApply").onclick = ChangeStatus;

		// Initialize Available
		if (document.getElementById("lstAvailable").selectedIndex == 0)
			{
			// Immediately
			document.getElementById("tdAvailable").style.display = "none";
			document.getElementById("tdCalendarSpacer").style.display = "none";
			document.getElementById("tdCalendar").style.display = "none";
			document.getElementById("tdDays").style.display = "none";
			}
		if (document.getElementById("lstAvailable").selectedIndex == 1)
			{
			// AvailableFrom
			try
				{
				document.getElementById("tdAvailable").style.display = "table-cell";
				document.getElementById("tdCalendarSpacer").style.display = "table-cell";
				document.getElementById("tdCalendar").style.display = "table-cell";
				}
			catch(err)
				{
			document.getElementById("tdAvailable").style.display = "block";
				document.getElementById("tdCalendarSpacer").style.display = "block";
				document.getElementById("tdCalendar").style.display = "block";
				}
			document.getElementById("tdDays").style.display = "none";
			document.getElementById("txtAvailable").style.width = "70px";
			}
		if (document.getElementById("lstAvailable").selectedIndex == 2)
			{
			// NoticePeriod
			try
				{
				document.getElementById("tdAvailable").style.display = "table-cell";
				document.getElementById("tdDays").style.display = "table-cell";
				}
			catch(err)
				{
			document.getElementById("tdAvailable").style.display = "block";
			document.getElementById("tdDays").style.display = "block";
				}
			document.getElementById("tdCalendarSpacer").style.display = "none";
			document.getElementById("tdCalendar").style.display = "none";
			document.getElementById("txtAvailable").style.width = "25px";
			}
		}
	}
//************************************************************************************************************
	function JobsByEmailKeydown()
		{
		OpenJobsByEmail();
		clearTimeout(objDDTimeout);
		objDDTimeout = setTimeout("CloseJobsByEmail();CloseSendJobToAFriend()", 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();CloseSendJobToAFriend()", 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 SectorIDs;
    var AreaIDs;
    var EmailAddress;
    
		CloseJobsByEmail();
		CloseSendJobToAFriend();
    
    // Get the SectorIDs and AreaIDs
    SectorIDs = document.getElementById("hdnSectorIDs").value;
    AreaIDs = document.getElementById("hdnAreaIDs").value;

    // 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&SectorIDs=" + SectorIDs + "&AreaIDs=" + AreaIDs + "&EmailAddress=" + EmailAddress, true);
			xmlHttp.send(null);
			
		return false;
		}
//************************************************************************************************************
	function SendJobToAFriendKeydown()
		{
		OpenSendJobToAFriend();
		clearTimeout(objDDTimeout);
		objDDTimeout = setTimeout("CloseJobsByEmail();CloseSendJobToAFriend()", 10000);
		}
//************************************************************************************************************
	function SendJobToAFriendKeypress(objEvent)
		{
		if (window.event)
				{
				if (objEvent.keyCode == 13)
					{
					SendJobToAFriend();
					return false;
					}
				}
			else
				{
				if (objEvent.which == 13)
					{
					SendJobToAFriend();
					return false;
					}
				}
		}
//************************************************************************************************************
	function OpenSendJobToAFriend()
		{
		document.getElementById("imgSendToAFriendDDTopRight").src = "Images/Tabs/dd_top_right_over.gif";
		document.getElementById("tblSendJobToAFriend").style.left = (parseInt(GetElementX(document.getElementById("tdSendToAFriendMarker"))) - 29) + "px";
		document.getElementById("tblSendJobToAFriend").style.top = (parseInt(GetElementY(document.getElementById("tdSendToAFriendMarker"))) + 26) + "px";
		document.getElementById("tblSendJobToAFriend").style.display = "block";
		clearTimeout(objDDTimeout);
		objDDTimeout = setTimeout("CloseJobsByEmail();CloseSendJobToAFriend()", 10000);
		}
//************************************************************************************************************
	function CloseSendJobToAFriend()
		{
		document.getElementById("imgSendToAFriendDDTopRight").src = "Images/Tabs/dd_top_right.gif";
		document.getElementById("tblSendJobToAFriend").style.display = "none";
		}
//************************************************************************************************************
	function SendJobToAFriend() 
    {
    var JobID;
    var ToEmailAddress;
    var Message;
    var FromEmailAddress;
    
		CloseJobsByEmail();
		CloseSendJobToAFriend();
    
    // Get the JobID
    JobID = document.getElementById("hdnJobID").value;
    
    // Retrieve fields
    ToEmailAddress = document.getElementById("txtToEmailAddress").value;
    Message = document.getElementById("txtMessage").value;
    FromEmailAddress = document.getElementById("txtFromEmailAddress").value;
    
    // Validate fields
    if (IsValidEmail(ToEmailAddress) == false)
			{
			alert("Please enter a valid 'To' Email Address");
			OpenSendJobToAFriend();
			document.getElementById("txtToEmailAddress").focus();
			return false;
			}
    if (IsValidEmail(FromEmailAddress) == false)
			{
			alert("Please enter a valid 'From' Email Address");
			OpenSendJobToAFriend();
			document.getElementById("txtFromEmailAddress").focus();
			return false;
			}

    if (Message == "Your message")
			{
			Message = "";
			}

		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("The job has been successfully sent to " + ToEmailAddress + ".");
					}
				else
					{
					alert("There was a problem sending the job - please check your email addresses and try again.");
					}
				}
			}
			
			// Send request using XMLHttpRequest
			xmlHttp.open("GET", ajaxPath + "send_to_a_friend_ajax.aspx?JobID=" + JobID + "&FromEmailAddress=" + FromEmailAddress + "&ToEmailAddress=" + ToEmailAddress + "&Message=" + Message, true);
			xmlHttp.send(null);
			
			return false;
    }
//************************************************************************************************************
	function LoginKeypress(objEvent)
		{
		if (window.event)
				{
				if (objEvent.keyCode == 13)
					{
					Login();
					return false;
					}
				}
			else
				{
				if (objEvent.which == 13)
					{
					Login();
					return false;
					}
				}
		}
//************************************************************************************************************
	function Login()
		{
    var LoginEmailAddress;
    var LoginPassword;
            
    // Retrieve fields
    LoginEmailAddress = document.getElementById("txtLoginEmailAddress").value;
    LoginPassword = document.getElementById("txtLoginPassword").value;
    
    // Validate fields
    if (IsValidEmail(LoginEmailAddress) == false)
			{
			alert("Please enter a valid Email Address");
			document.getElementById("txtLoginEmailAddress").focus();
			return false;
			}
    if (LoginPassword.length == 0)
			{
			alert("Please enter your password");
			document.getElementById("txtLoginPassword").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")
					{
					location.href = location.href + "#apply";
					location.reload(true);
					}
				else
					{
					alert("Your credentials have not been accepted.\n\nPlease check that your Email Address and Password are correct and then try again.");
					}
				}
			}
			
			// Send request using XMLHttpRequest
			xmlHttp.open("GET", ajaxPath + "candidate_login.aspx?AJAX=True&EmailAddress=" + LoginEmailAddress + "&Password=" + LoginPassword, true);
			xmlHttp.send(null);
			
		return false;
		}
//************************************************************************************************************
function ChangeStatus()
	{
	// Show uploading status icon
	document.getElementById("imgFinishStatus").src = "Images/Icons/icon_status_uploading.gif";
	
	// Add scroll into view code to application details section
	document.getElementById('frmForm').action = document.getElementById('frmForm').action + '#apply';

	// Do usual ASP.Net validation
	if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();
	
	// If the page is invalid, hide uploading status icon and scroll up to application details section
	if (Page_IsValid == false)
		{
		document.getElementById("imgFinishStatus").src = "Images/trans_pixel.gif";
		document.getElementById("aApplyBookmark").scrollIntoView();
		}
	}
//************************************************************************************************************
function ExpandLogin()
	{
	document.getElementById("trGotAnAccount").style.display = "none";
	document.getElementById("trLogin").style.display = "block";
	document.getElementById("txtLoginEmailAddress").focus();
	}
//************************************************************************************************************
function ExpandCollapseJobDetails()
	{
	if (document.getElementById("trJobDetails").style.display == "none")
		{
		document.getElementById("trJobDetails").style.display = "block";
		document.getElementById("imgExpandCollapseJobDetailsIcon").src = "Images/Icons/icon_collapse.gif";
		document.getElementById("imgExpandCollapseJobDetailsIcon").alt = "Collapse";
		document.getElementById("imgExpandCollapseJobDetailsIcon").title = "Collapse";
		}
	else
		{
		document.getElementById("trJobDetails").style.display = "none";
		document.getElementById("imgExpandCollapseJobDetailsIcon").src = "Images/Icons/icon_expand.gif";
		document.getElementById("imgExpandCollapseJobDetailsIcon").alt = "Expand";
		document.getElementById("imgExpandCollapseJobDetailsIcon").title = "Expand";
		}
	}
//************************************************************************************************************
function AvailableChanged()
	{
	var DateToday = new Date()

	if (document.getElementById("lstAvailable").selectedIndex == 0)
		{
		// Immediately
		document.getElementById("tdAvailable").style.display = "none";
		document.getElementById("tdCalendarSpacer").style.display = "none";
		document.getElementById("tdCalendar").style.display = "none";
		document.getElementById("tdDays").style.display = "none";
		}
	if (document.getElementById("lstAvailable").selectedIndex == 1)
		{
		// AvailableFrom
		try
			{
			document.getElementById("tdAvailable").style.display = "table-cell";
			document.getElementById("tdCalendarSpacer").style.display = "table-cell";
			document.getElementById("tdCalendar").style.display = "table-cell";
			}
		catch(err)
			{
		document.getElementById("tdAvailable").style.display = "block";
			document.getElementById("tdCalendarSpacer").style.display = "block";
			document.getElementById("tdCalendar").style.display = "block";
			}
		document.getElementById("tdDays").style.display = "none";
		document.getElementById("txtAvailable").value = Pad(DateToday.getDate(), 2) + "/" + Pad((DateToday.getMonth() + 1), 2) + "/" + Pad(DateToday.getFullYear(), 2);
		document.getElementById("txtAvailable").style.width = "70px";
		document.getElementById("txtAvailable").focus();	
		document.getElementById("txtAvailable").select();	
		}
	if (document.getElementById("lstAvailable").selectedIndex == 2)
		{
		// NoticePeriod
		try
			{
			document.getElementById("tdAvailable").style.display = "table-cell";
			document.getElementById("tdDays").style.display = "table-cell";
			}
		catch(err)
			{
		document.getElementById("tdAvailable").style.display = "block";
		document.getElementById("tdDays").style.display = "block";
			}
		document.getElementById("tdCalendarSpacer").style.display = "none";
		document.getElementById("tdCalendar").style.display = "none";
		document.getElementById("txtAvailable").style.width = "25px";
		document.getElementById("txtAvailable").value = "30";
		document.getElementById("txtAvailable").focus();	
		document.getElementById("txtAvailable").select();	
		}
	}
//************************************************************************************************************
function ShowProfileClicked()
	{
	if (document.getElementById("chkShowProfile").checked == true)
		{
		document.getElementById("chkShowProfileToAgenciesOnly").disabled = false;
		document.getElementById("tblProfile").style.display = "";
		}
	else
		{
		document.getElementById("chkShowProfileToAgenciesOnly").disabled = true;
		document.getElementById("chkShowProfileToAgenciesOnly").checked = false;
		document.getElementById("tblProfile").style.display = "none";
		}
	}