function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
   
function emptyCompanySizeText(event) {
	var txtObj;
	txtObj = document.getElementById("txtFTE");
	if (txtObj.value == "# of employees"){ 
			txtObj.value = "";
			txtObj.className = "objInitialInput txtKeyword";
			return;
	}
	
	if (txtObj.value == ""){ 
			txtObj.value = "# of employees";
			txtObj.className = "objInitialInput txtKeywordEmpty";
			return;
	}			
}

function txtKeyword_OnKeyDown(event){
	var key;
	var txtObj;
	
	if (event.keyCode){
		key = event.keyCode;
	}
	else if(event.which){
		key = event.which;
	}
	else{
		return(true);
	}
	
	txtObj = document.getElementById("txtKeyword");
	if(txtObj.className == "objInitialInput txtKeywordEmpty")
	{
		txtObj.value = "";
		txtObj.className = "objInitialInput txtKeyword";
	}
	
}


function txtKeyword_OnFocus(event){
	var txtObj = document.getElementById("txtKeyword");
	if(txtObj.value == txtObj.getAttribute("defaultValue")){
		txtObj.className = "objInitialInput txtKeyword";
		txtObj.value = "";
	}
}

function txtKeyword_OnBlur(event){
	var txtObj = document.getElementById("txtKeyword");
	if(txtObj.value == ""){
		txtObj.className = "objInitialInput txtKeywordEmpty";
		txtObj.value = txtObj.getAttribute("defaultValue");
	}
}


function selectSearchByField(event){
	var source, obj;
	
	if(event.srcElement){
		source = event.srcElement;
	}
	else if(event.target){
		source = event.target;
	}
	else{
		return(true);
	}
	
	obj = document.getElementById("txtKeyword");
	if(!obj){
		obj = document.getElementById("cmbJobCategory");
	}
	
	if(source.value == 1){
		if(obj.tagName == "INPUT"){
			var parentObj = obj.offsetParent;
			parentObj.removeChild(obj);
			
			var newObj = document.createElement("SELECT");
			newObj.id = "cmbJobCategory";
			newObj.setAttribute("defaultValue", obj.getAttribute("defaultValue"));
			newObj.setAttribute("userValue", obj.value);
			newObj.className = "objInitialInput";
			
			newObj.options[newObj.options.length] = new Option("Chose a Job Category ----------------->","");
			for(var i = 0; i < aNarrowCode.length ; i ++){
				newObj.options[newObj.options.length] = new Option(aNarrowDesc[i],aNarrowCode[i]);
			}
			parentObj.appendChild(newObj);
		}
	}
	else if(source.value == 0){

		if(obj.tagName == "SELECT"){
			
			var parentObj = obj.offsetParent;
			parentObj.removeChild(obj);
			
			var newObj = document.createElement("INPUT");
			newObj.id = "txtKeyword";
			newObj.setAttribute("defaultValue", obj.getAttribute("defaultValue"));
			
			if( obj.getAttribute("userValue") != obj.getAttribute("defaultValue")){
			
				newObj.value = obj.getAttribute("userValue");
				newObj.className = "objInitialInput txtKeyword";
			}
			else{
			
				newObj.value = obj.getAttribute("defaultValue");
				newObj.className = "objInitialInput txtKeywordEmpty";
			}
			
			//newObj.attachEvent("onkeydown",txtKeyword_OnKeyDown);
			try{
				newObj.addEventListener("focus", txtKeyword_OnFocus, false);
				newObj.addEventListener("blur", txtKeyword_OnBlur, false);
			}
			catch(e){
				newObj.onfocus = txtKeyword_OnFocus;
				newObj.onblur = txtKeyword_OnBlur;
			}
			parentObj.appendChild(newObj);
		}
	}
}


function selectLocationField(event){
	var source, obj;
	
	if(event.srcElement){
		source = event.srcElement;
	}
	else if(event.target){
		source = event.target;
	}
	else{
		return(true);
	}
	
	obj = document.getElementById("txtZipCode");
	if(!obj){
		obj = document.getElementById("cmbStateMetro");
	}
	
	if(source.value == 1){
		if(obj.tagName == "INPUT"){
			var parentObj = obj.offsetParent;
			parentObj.removeChild(obj);
			
			var newObj = document.createElement("SELECT");
			newObj.id = "cmbStateMetro";
			newObj.userValue = obj.value;
			newObj.className = "objInitialInput";
			
			newObj.options[newObj.options.length] = new Option("Chose a State/Metro ----------------->","");
			for(var i = 0; i < aMetroCode.length ; i ++){
				newObj.options[newObj.options.length] = new Option(aMetroDesc[i],aMetroCode[i]);
			}
						
			parentObj.appendChild(newObj);
		}
	}
	else if(source.value == 0){

		if(obj.tagName == "SELECT"){
			
			var parentObj = obj.offsetParent;
			parentObj.removeChild(obj);
			
			var newObj = document.createElement("INPUT");
			newObj.id = "txtZipCode";
			newObj.className = "objInitialInput";
			newObj.maxLength = 5;
			if(obj.userValue != "")
				newObj.value = obj.userValue;
			
			parentObj.appendChild(newObj);

		}
	}
}

function postForm(){
	var objKeyword = document.getElementById("txtKeyword");
	var objJobCategory = document.getElementById("cmbJobCategory");
	var objZipCode = document.getElementById("txtZipCode");
	var objStateMetro = document.getElementById("cmbStateMetro");
	var objSearchByOption_0 = document.getElementById("rdbSearchByOption_0");
	var objLocationOption_0 = document.getElementById("rdbLocationOption_0");
	var objSearchByOption_1 = document.getElementById("rdbSearchByOption_1");
	var objLocationOption_1 = document.getElementById("rdbLocationOption_1");
	var objFTE = document.getElementById("txtFTE");
	if (objFTE.value=="# of employees")
		objFTE.value="";
		
	var objHiddenKeyword = document.getElementById("hdKeyword");
	var objHiddenJobCategory = document.getElementById("hdJobCategory");
	var objHiddenZipCode = document.getElementById("hdZipCode");
	var objHiddenStateMetro = document.getElementById("hdStateMetro");
	var objHiddenSearchByOption = document.getElementById("hdSearchByOption");
	var objHiddenLocationOption = document.getElementById("hdLocationOption");
	
	if (objSearchByOption_0.checked)
		objHiddenSearchByOption.value = "0";
	else
		objHiddenSearchByOption.value = "1";


	if (objLocationOption_0.checked)
		objHiddenLocationOption.value = "0";
	else
		objHiddenLocationOption.value = "1";

	if(objFTE.value!=""){
		if (IsNumeric(objFTE.value) == false) {
			alert("Please enter a valid # of employees");
			objFTE.focus;
			return(false);
		}
	}

	if(objKeyword){
		if(objKeyword.value == ""){
			alert("Please enter a valid keyword");
			return(false);
		}
		else{
			objHiddenKeyword.value = objKeyword.value;
		}
	}
	
	if(objJobCategory){
		if(objJobCategory.value == ""){
			alert("Please enter a valid job category");
			return(false);
		}
		else{
			objHiddenJobCategory.value = objJobCategory.value;
		}
	}
	//Uncomment the block below if zip code become required
	if(objZipCode){
		if(objZipCode.value!="")
		{
			if((isNaN(objZipCode.value)) || (!validateZipCode(objZipCode.value))){
				alert("Please enter a valid zip code");
				return(false);
			}
			else{
				objHiddenZipCode.value = objZipCode.value;
			}
		}
	}
	
	if(objZipCode){
		objHiddenZipCode.value = objZipCode.value;
	}
	
	//Uncomment the block below if State/Metro code become required
	/*if(objStateMetro){
		if(objStateMetro.value == ""){
			alert("Please enter a valid state/metro");
			return(false);
		}
		else{
			objHiddenStateMetro.value = objStateMetro.value;
		}
	}*/
	
	if(objStateMetro){
		objHiddenStateMetro.value = objStateMetro.value;
	}

	var strBrowser = new String(navigator.userAgent);
	if (strBrowser.toUpperCase().indexOf("MSIE") >= 0)
	{
		objSearchByOption_0.style.visibility = "hidden";
		objLocationOption_0.style.visibility = "hidden";
		objSearchByOption_1.style.visibility = "hidden";
		objLocationOption_1.style.visibility = "hidden";
		objSearchByOption_0.checked = true;
		objLocationOption_0.checked = true;
	}
	
	document.getElementById("mainform").submit();
}

function pageInit(){
	
	var objSearchByOption_0 = document.getElementById("rdbSearchByOption_0");
	var objSearchByOption_1 = document.getElementById("rdbSearchByOption_1");
	var objLocationOption_0 = document.getElementById("rdbLocationOption_0");
	var objLocationOption_1 = document.getElementById("rdbLocationOption_1");
	
	objSearchByOption_0.style.visibility = "visible";
	objSearchByOption_1.style.visibility = "visible";
	objLocationOption_0.style.visibility = "visible";
	objLocationOption_1.style.visibility = "visible";
}




function openCommonWindow(strUrl,strWidth,strHeight){		
		
		 var subWin = window.open(strUrl,"SalWizWindow",
								"toolbar=yes,scrollbars=yes," + 
									"resizable=yes,width=" + strWidth + "," +
										"height=" + strHeight + ",left=0,top=0");		
	
		if(subWin!=null) subWin.focus();
    	
}

function validateZipCode(strZipCode)
{
	var boolResponse;
	var ajaxRequest;
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
		
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser doesn't support ajax!");
				return false;
			}
		}
	}
	/*
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var strResponse = new String(ajaxRequest.responseText)
			boolResponse = ( strResponse.indexOf("TRUE")>=0 );
		}
	}
	*/
	
	ajaxRequest.open("GET", "/salarywizard/layoutscripts/swzl_zipcodevalidation.asp?zipcode=" + strZipCode, false);
	ajaxRequest.send(null); 
	
	var strResponse = new String(ajaxRequest.responseText)
	boolResponse = ( strResponse.indexOf("TRUE")>=0 );
	
	return(boolResponse);
}

function selectJobCategory(strCode){
	var cmb = document.getElementById("cmbJobCategory");
	for(var i = 0 ; i < cmb.options.length ; i ++){
		if(cmb.options[i].value == strCode){
			cmb.selectedIndex = i;
			return;
		}
	}
}

function selectStateMetro(strCode){
	var cmb = document.getElementById("cmbStateMetro");
	for(var i = 0 ; i < cmb.options.length ; i ++){
		if(cmb.options[i].value == strCode){
			cmb.selectedIndex = i;
			return;
		}
	}
}

