//Please change the following parameter when move to another machine
var domainName = ".webhire.com";

function cookieVal(cookieName) {
    thisCookie = document.cookie.split("; ")
    for (i = 0; i < thisCookie.length; i++) {
        if (cookieName == thisCookie[i].split("=")[0]) {
		if(thisCookie[i].indexOf("=")>0)
	            return thisCookie[i].split("=")[1];
		else
			return 0;
        }
    }
    return 0;
}

function setCookie(name, value){
    document.cookie = name + "=" + value + "; domain=" + domainName + "; path=/";
}

function sort(sortField){
    var sortOrder = "ASCEND";
    if(document.GetCurrentPage.SortField.value==sortField){
        if(document.GetCurrentPage.SortOrder.value=="ASCEND")
           sortOrder = "DESCEND";
    }else{
        document.GetCurrentPage.SortField.value = sortField;
    }
	document.GetCurrentPage.ResultStart.value = "1";
    document.GetCurrentPage.SortOrder.value = sortOrder;
    document.GetCurrentPage.submit();
}

function avSearch(){
	//fix wild card
	for(i=0; i<document.search.elements.length; i++){
		elem = document.search.elements[i];
		if(elem.type=="text" && elem.value!="")
		{
			var elValue = "";
			var eWords = elem.value.split(' ');
			for(j=0; j<eWords.length; j++){
				var eWord = eWords[j];
				var index = eWord.indexOf("*");

				if(index>=0 && index<3){
					window.alert("You have to type in a least three letters before using wildcard *");
					elem.focus();
					return false;
				}
/*
				else{
					if(index>=3 && index!=eWord.indexOf("**")) //change one * to two *
					{
						eWord = eWord.substring(0, index) + "*" + eWord.substring(index);
					}

					if(elValue=="")
					{
						elValue = eWord;
					}else
						elValue = elValue + " " + eWord;
				}
*/
			}
/*
			if(elValue.indexOf(",")>0){
				eWords = elValue.split(',');
				elValue = eWords[0];
				for(j=1; j<eWords.length; j++){
					elValue += " AND " + eWords[j]; 
				}				
			}
			elem.value = elValue;		
*/	
		}
	}
}

var today = new Date();

//we need to parse Date from mm-dd-yy format 
function stringToDate(dateString){
	var month = dateString.substring(0,2);
      var day = dateString.substring(3,5);
	var year = dateString.substring(6,8);
      if(year>50)
		year = 19 + year;
	else
		year = 20 + year;
      var d = new Date();
      d.setMonth(month - 1);
      d.setDate(day);
      d.setFullYear(year);
	return d;
}

//Convert date from mm-dd-yy format to mm/dd/yyyy
function ConvertDate(dateString){
	var d = new Date(dateString);
	var month = d.getMonth() + 1;
      if(month<10)
		month = "0" + month;
	var day = d.getDate();
      if(day<10)
          day = "0" + day;
	var year = d.getYear();
      if(year<10)
          year = "200" + year;
      else if(year<50)
          year = "20" + year;
      else if(year<100)
          year = "19" + year;

	document.write(month + "/" + day + "/" + year);
}

var password = "webhire";
var redirectURL = "/Errors/NotAuthorized.html";

function setAuth(accountID){
    document.cookie = "auth_password=" + password + accountID + "; path=/";
}

function checkAuth(accountID){
	if(document.cookie.indexOf("auth_password=" + password + accountID)<0)    
		window.location = redirectURL;
}

var logoutLink = "<b>[ <A href=\"/profiler/logout.pfl\">Log out</A> ]</b>";
function LogoutLink(destUrl){
	ldapID = cookieVal("ProfileId");
	if(ldapID>0){
		document.write(logoutLink);
	}
}

var logoutLinkIntra = "<b>[ <A href=\"/profiler/logout.pfl?SITE=A\">Log out</A> ]</b>";
function LogoutLinkIntra(destUrl){
	ldapID = cookieVal("ProfileId");
	if(ldapID>0){
		document.write(logoutLinkIntra);
	}
}

function LogOut(destUrl)
{
	var expiration = new Date();
	//Clear the profile id to complete logout
	document.cookie = "ProfileId=; expires=" + expiration.toGMTString() + "; path=/";
	// goto destination (i.e. Job Search page)
	document.URL = destUrl;
}

function SetERPCode()
{
	var erpCode = "";
	argstr = top.location.href;
	idx = argstr.indexOf("ERP_CODE=");
	if(idx>0){
		erpCode = argstr.substring(idx + 9, argstr.length);	
	    document.cookie = "ERP_CODE=" + erpCode + "; path=/";    		
	}
}

//	Displays red star when strDate is less than intDays days in the past.
//	To automatically display star, pass -1 to intDays.
//	If intType is 1, star has white background.
//	If intType is 2, star has gray background.
function displayStar(strDate, intDays, intType)
{
	var Today = new Date();
	var dateIn = stringToDate(strDate);
	var strOut = "";
	if ((intDays==-1) || (Today.getTime() - dateIn.getTime() < intDays * 86400000))
		strOut = "<img src=\"" + location.protocol + "//" + location.hostname + "/images/hotjobs" + intType + ".gif\" border=\"0\">";
	document.writeln(strOut);
}

var upArrow = "/images/sort_ascend.gif";
var downArrow = "/images/sort_descend.gif";
function SortImage(field){
	var sortField = document.GetCurrentPage.SortField.value; 
	var sortOrder = document.GetCurrentPage.SortOrder.value; 
	
	if(sortField!=null && sortOrder!=null && sortField == field){
		if(sortOrder == 'ASCEND'){
			document.write("<img src=\"" + serverName + upArrow + "\" border=0 alt=\"Sort Descending\">");
		}else if(sortOrder = 'DESCEND'){
			document.write("<img src=\"" + serverName + downArrow + "\" border=0 alt=\"Sort Ascending\">");
		}
	}
}
