﻿
function GetWebVacsOnLoad()
{
    SetImgState(1, 1);
	Public_VacanciesHome.GetWebVacsOnLoad(GetWebVacsOnLoad_CallBack);
}
function GetWebVacsOnLoad_CallBack(response)
{
	if (response.error == null)
	{
        SetImgState(1, 0);
		var ResponseValue = response.value;
		document.getElementById("dvVacancies").innerHTML = ResponseValue;	
	}
	else
	{
        SetImgState(1, 0);
		window.alert("Our vacancy database is currently unavailable. Please try again later. We apologise for any inconvenience that may have occurred");
		return;
	}
}




function GetWebVacs()
{
    SetImgState(1, 1);
	Public_VacanciesHome.GetWebVacs(document.getElementById("dpJobTitle").value, document.getElementById("dpLocation").value, GetWebVacs_CallBack);
}
function GetWebVacs_CallBack(response)
{
	if (response.error == null)
	{
        SetImgState(1, 0);
		var ResponseValue = response.value;
		document.getElementById("dvVacancies").innerHTML = ResponseValue;	
	}
	else
	{
	    SetImgState(1, 0);
	    window.alert("Our vacancy database is currently unavailable. Please try again later. We apologise for any inconvenience that may have occurred");
		return;
	}
}




function SetImgState(Img, State)
{
    if (Img == 1)//recruitment loader
    {
        if (State == 1)
            document.getElementById("imgLoader").src="../Common/Images/cargando.gif";
        else
            document.getElementById("imgLoader").src="../Common/Images/note.gif";
    }
    else    //EF mail loader
    {
        if (State == 1)
            document.getElementById("imgEFLoader").src="../Common/Images/cargando.gif";
        else
            document.getElementById("imgEFLoader").src="../Common/Images/note.gif";
    }
}



function Reset()
{
    ResetDroplist("dpJobTitle");
    ResetDroplist("dpLocation");
    GetWebVacsOnLoad();
}




function ResetDroplist(id)
{
	for (var i= document.getElementById(id).options.length-1; i>-1; i--) 
	{	
		if (document.getElementById(id).options[i].value == "-1")
			document.getElementById(id).options[i].selected = true;
	}	
}




function ToggleRow(vacID)
{
	switch(document.getElementById(vacID + "_1").style.display)
	{
		case "":
		    document.getElementById(vacID + "_1").style.display = "none";
			document.getElementById(vacID + "_1").style.visibility="hidden";
		break;
			
		case "none":
			document.getElementById(vacID + "_1").style.display= "";
			document.getElementById(vacID + "_1").style.visibility="visible";		
		break;
	}
}




/*
description:	toggles index divs in correct position relative to their anchors
parameters:		anchor(object that anchors div), div (object)
notes:			
*/
var offsetY = 30;											
var iconW = 20;

function ToggleDiv(anchor, div, align)
{
	switch (div.style.visibility)
	{		
		case "hidden":
			var coords = getObjCoords(anchor); 
			div.style.top = (coords.y) + offsetY;
			
			if (align== "l")
				div.style.left = (coords.x) - parseInt(div.style.width);
			else
				div.style.left = coords.x;
				
			div.style.display= "";
			div.style.visibility= "visible";
		break;
		
		
		case "visible":
			div.style.display= "none";
			div.style.visibility="hidden";
		break;
	}
}




/*
description:	gets specified objects co-ordinates
parameters:		inputElement(object)
notes:			eg. getObjCoords(someElement).y;
*/
function getObjCoords(inputElement) 
{
    var coords =  new Object();
    coords.x = 0;
    coords.y = 0;

    try 
    {
        targetElement = inputElement;
        if(targetElement.x && targetElement.y)
        { 
            coords.x = targetElement.x;
            coords.y = targetElement.y;
        } 
		else
		{ 
			if(targetElement.offsetParent)
			{ 
				coords.x += targetElement.offsetLeft;
				coords.y += targetElement.offsetTop;

				while(targetElement = targetElement.offsetParent) 
  				{
					coords.x += targetElement.offsetLeft;
					coords.y += targetElement.offsetTop;
				}
			} 
			else 
			{
				//alert(\"Could not find any reference for coordinate positioning.\");
			}
		}
		return coords;
		//alert(inputElement.id +  " x: " + coords.x + "; y: " + coords.y);		
    } 
    catch(error) 
    {
        //alert(error.msg);
        return coords;
    }
}			




function getEF(vacID)
{
	document.getElementById("hdnEFVacID").value = vacID;
	document.getElementById("dvEF").style.display= "none";
	document.getElementById("dvEF").style.visibility="hidden";
	ToggleDiv(document.getElementById("email_" + vacID), document.getElementById("dvEF"), 'r');
}




function SendEF()
{
	if ( EmailValid(document.getElementById("txtEmail")) )
	{
	    SetImgState(2, 1);
	    Public_VacanciesHome.SendEFMail(document.getElementById("hdnEFVacID").value, document.getElementById("txtTo").value, document.getElementById("txtEmail").value, document.getElementById("txtMsg").value, document.getElementById("txtFrom").value, SendEF_CallBack);
	}
	else
	    alert("Please enter a valid email address");
}
function SendEF_CallBack(response)
{
	if (response.error == null)
	{
	    SetImgState(2, 0);
	    var ResponseValue = response.value;
		alert("The " + ResponseValue + " vacancy has been sent to " + document.getElementById("txtTo").value);
		closeEF();
	}
	else
	{
	    SetImgState(2, 0);
		window.alert("Our vacancy email functionality is currently unavailable. Please try again later. We apologise for any inconvenience that may have occurred");
		closeEF();		
		return;
	}
}




function closeEF()
{
    RefreshEF();
	document.getElementById("dvEF").style.display= "none";
	document.getElementById("dvEF").style.visibility="hidden";
}




function RefreshEF()
{
    document.getElementById("txtTo").value = "";
	document.getElementById("txtEmail").value = "";
    document.getElementById("txtMsg").value = "";
    document.getElementById("txtFrom").value = "";
}




/*
description:	validates email address
parameters:		Obj (email address box)
notes:			
				c1== -1	@ must be present
				c2== -1	. must be present
				c1== 0	@ cannot come as first character
				c2== 0	. cannot come as first character
				c1==c2-1	@. back-to-back not allowed
				c1==c2+1	.@ back-to-back not allowed
				c3!=1	@ can occur only once
				(c4 != -1 && c4 < c1)		if hyphen present & comes before @ not allowed 
				(c4 != -1 && c4 == emlchar.length-1) if hyphen present & comes as a last character not allowed

*/

	function EmailValid(Obj)
	{
		var c1;
		var c2;
		var c3;

		emlchar	=Obj.value;
		emlchar = emlchar.toLowerCase() ;
		if(trimstr(emlchar,Obj)==-1) 
		{
			return false;
		}

		c1 = emlchar.indexOf("@");
		c2 = emlchar.indexOf(".");  
		c3 = occurs("@", Obj) ;
		c4 = emlchar.indexOf("-");	
		c5 = occurs(" ", Obj) ;



		if (c1==-1 || c2==-1 || c1== 0 || c2==0 || c1==c2-1 || c1==c2+1 || c3!=1 || (c4 != -1 && c4 == emlchar.length-1) || c5 >= 0) 
		{
			Obj.focus();
			return false;
		}    

		if (emlchar.length < 5 || c1==emlchar.length - 1 || c2==emlchar.length - 1 )
		{
			Obj.focus();
			return false;
		}

		tmpStr = "0123456789_-abcdefghijklmnopqrstuvwxyz" ;
		cnt = 0
		i = emlchar.indexOf(".", cnt);
		
		while (true) {
			ch1 = emlchar.charAt(i-1) ;
			ch2 = emlchar.charAt(i+1) ;
			if (tmpStr.indexOf(ch1) == -1 || tmpStr.indexOf(ch2) == -1)
				return false;

			cnt = cnt + 1 ;	
			i = emlchar.indexOf(".", cnt);
			if (i == -1)
				break;
		}

		return true;
	}
	function occurs(ch, fieldname) 
	{
		cnt 	= 0
		flag	= 0 

		for (i=0; i < fieldname.value.length; ++i) 
		{
			if (fieldname.value.substring(i,i+1) == ch) 
			{
				cnt	= cnt + 1 ;
				flag= 1;
			}
		}

		if (flag == 1) 
			return (cnt) ;
		else
			return (-1) ;
	}
	function trimstr(strin,obj)
	{
		var i,j;

		//This loop checks whether a field is containing white spaces only & gives error appropriately
		for(i=0; ((i < strin.length) && (strin.charAt(i) == ' ')); i++);
		if (i == strin.length) 
		{
			obj.focus();
			return -1;
		}

		//	This loop truncates the input string if it contains in-between 
		//	white spaces. e.g. if string 'strin' contains "amod deshpande"
		//  then output will be "amod"

		/*
			strin1 = "";
			for(j=0;j<strin.length && strin.charAt(i) != ' ' && strin.charAt(i) != "";j++,i++)
			{
				strin1 += strin.charAt(i);
			}
			obj.value = strin1;
			return 0;
		*/
	}
