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("Choose 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("Choose 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 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(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;
            document.getElementById("hdNarrowDesc").value = aNarrowDesc[document.getElementById("cmbJobCategory").selectedIndex-1];
		}
	}
	//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){
		if (objStateMetro.value != "")
		{
			var arrMetro = getMetroCodeDetails(objStateMetro.value);
			if(arrMetro)
			{
				if(arrMetro.length==8)
				{
					objHiddenZipCode.value = arrMetro[3];
				}
			}
		}
	}

	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;
		}
	}
}
function getMetroCodeDetails(strMetroCode)
{
	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.open("GET", "/salarywizard/layoutscripts/swzl_statemetrodata.asp?metrocode=" + strMetroCode, false);
	ajaxRequest.send(null); 
	
	var strResponse = new String(ajaxRequest.responseText);
	
	return(strResponse.split("|||"));
}
