var ZIP_DEFAULT_VALUE = "Enter Zip Code of Job Location";
var ZIP_DEFAULT_VALUE2 = "Enter Zip Code";
var TITLE_DEFAULT_VALUE = "Enter Job Title";
var KEY_DEFAULT_VALUE2 = "Enter Position Title";
var ZIP_SEND_TIMEOUT = 1000;


function switchDefaultUniqueValue(element) { 
	if(!is_ie()){
		var elementName = element.id;
		if (element.value == -1)
			element.className = "defaultValue";
		else
			element.className = "uniqueValue";
	}
}

function checkForOther(element) {
	if (element.value == "0") {
		gebi("jobTitleText").style.display = "block";
	} else {
 		gebi("jobTitleTextInput").value = "";
 		outFromTitle(gebi("jobTitleTextInput"));
 		gebi("jobTitleText").style.display = "none";
 		enableGetStartedButton(false);
 	}
}

function visitZip(element) {
	if (element.value == ZIP_DEFAULT_VALUE) {
		element.value = "";
		element.className = "uniqueValue";
	}
}

function visitZip2(element) {
	if (element.value == ZIP_DEFAULT_VALUE2) {
		element.value = "";
		element.className = "uniqueValue";
	}
}

function visitKey(element) {
	if (element.value == KEY_DEFAULT_VALUE2) {
		element.value = "";
		element.className = "uniqueValue";
	}
}

function outFromKey(element) {
	if (trim(element.value) == "") {
		element.value = KEY_DEFAULT_VALUE2;
		element.className = "defaultValue";
	}
}

function visitTitleText(element) {
	if (element.value == TITLE_DEFAULT_VALUE) {
		element.value = "";
		element.className = "uniqueValue";
	}
}

function outFromZip(element) {
	if (trim(element.value) == "") {
		element.value = ZIP_DEFAULT_VALUE;
		element.className = "defaultValue";
	}
}

function outFromZip2(element) {
	if (trim(element.value) == "") {
		element.value = ZIP_DEFAULT_VALUE2;
		element.className = "defaultValue";
	}
}

function outFromTitle(element) {
	if (trim(element.value) == "") {
		element.value = TITLE_DEFAULT_VALUE;
		element.className = "defaultValue";
	}
}

function activateElementOnClick(element) {
	if(!is_ie()){
		element.className = "uniqueValue";
	}
}

var zipInputTimer;
function enableDisableButtonsKeyUp() {
	clearTimeout(zipInputTimer);
	zipInputTimer = setTimeout(function eb() {enableDisableButtons();} ,100);
}

var zipInputTimer2;
function enableDisableButtonsKeyUp2() {
	clearTimeout(zipInputTimer2);
	zipInputTimer2 = setTimeout(function eb() {enableDisableButtons2();} ,100);
}

function enableDisableButtons() {
	if (isEmptyZip()) {
		enableGetStartedButton(false);
		return;
	}
	checkForCustomTemplate();
}

function enableDisableButtons2() {
	if (isEmptyZip()) {
		//enableGetStartedButton(false);
		return;
	}
	checkForCustomTemplate2();
}

function checkForCustomTemplate() {
	var company = gebi("customCompanies").value;
	var jobTitleValue = gebi("jobTitle").value;
	if ((company > 0) && (jobTitleValue == 0)) {
		if ((gebi("jobTitleTextInput").value!="") && (gebi("jobTitleTextInput").value!=TITLE_DEFAULT_VALUE)) {
			enableGetStartedButton(true);
			gebi("getStartedButton").onclick = function() { goToCreateSearch(); };
		} else {
			enableGetStartedButton(false);
		}	
		return;
	}
	
	if ((company <= 0) || (jobTitleValue <= 0)) {
		enableGetStartedButton(false);
		return;
	}
	sendJsonRequest("map.json", "customTitleId=" + jobTitleValue + "&companyId=" + company, cbEnableDisableButtons, "getTemplateName", "MiniDataPublicHandler");
}

function checkForCustomTemplate2() {
	//enableGetStartedButton(true);
	gebi("getStartedButton").onclick = function() { goToCreateSearch2(); };
}

function enableGetStartedButton(toEnable) {
	if (toEnable) {
		gebi("getStartedButtonInactive").style.display = "none";
		gebi("getStartedButtonActive").style.display = "block";
	} else {
		gebi("getStartedButtonInactive").style.display = "block";
		gebi("getStartedButtonActive").style.display = "none";
	}
}

function checkFieldsStatus(){
   
   var name = gebi("customCompanies");
   var job = gebi("jobTitle");
   var zip = gebi("zipInput");

   if(name.value == '-1' && job.value =='-1' && zip.value == 'Enter Zip Code of Job Location' ){
     return true;
   }else{
     return false;
   }

}

function changeButtonstatus(enableFlag){
   if(checkFieldsStatus()){
	  enableGetStartedButton(enableFlag);
   }
}

function redirectNewSearch(){
   if(checkFieldsStatus()){
	  document.location="../gsearch?page=new";
   }
}

function cbEnableDisableButtons(ds) {
	enableGetStartedButton(true);
	var templateName = ds.getData()[0].json;
	if (templateName!="" && gebi('zipInput').value != "") {
		gebi("getStartedButton").onclick = function() { showAutoConfirm(templateName); };
	} else {
		gebi("getStartedButton").onclick = function() { goToCreateSearch(); };
	}
}

function showAutoConfirm(templateName) {
	validateZip(function() {doShowAutoConfirm(templateName)} );
}

function goToCreateSearch() {
	validateZip(function(){ goToCreateSearchCb() });
}

function goToCreateSearch2() {

	var keywords = gebi("keyword").value;

	if(KEY_DEFAULT_VALUE2 == keywords || keywords == '' ){
		alert('Please enter a Title');
		return;
	}

	validateZip(function(){ goToCreateSearchCb2() });
}

function customize(templateName, zip) {
	//validateZip(function(){ goToCreateSearchCb() });
	validateZip(function(){
		customize_(templateName, zip)
	});
}

function validateZip(cb) {
	var zip = gebi("zipInput").value;
	if (zip == ZIP_DEFAULT_VALUE)
		zip = "";
	if (zip == "") {
		return;
	}
	sendJsonRequest("map.json", "zip=" + zip, cbValidateZip, "validateZip", "MiniDataPublicHandler", cb);
}

function cbValidateZip(ds, cb) {
	var isValid = ds.getData()[0].json;
	if (isValid)
		cb();
	else
		alert("Please enter a valid 5-digit US zip code");
}

function goToCreateSearchCb(){
	var params = getParamsForCreateSearch();
	window.location = "../gsearch?page=new&" + params;
}

function goToCreateSearchCb2(){
	var params = getParamsForCreateSearch2();
	//window.location = "../gsearch?page=new&" + params;
	saveSearch2();
}

function saveSearch2(){
	var customTitle;



	if (gebi("jobLevel").value == -1 ) {
		alert("Please select a Level");
		return;
	}
	
	if (gebi("keyword").value == "") {
		alert("Please enter a title");
		return;
	}else{
	    if(gebi("keyword").value.length >= 2) {
			 addToCriteria2(gebi("keyword").value,1,false);
			clearTimeout(ikTimer);
		}
	}

	var jobLevel = gebi("jobLevel").value;
	var searchName = gebi("searchName").value;
	
	var param2 = "searchName=" + searchName + "&filterJobLevel=" + jobLevel +"&criteriasJsonStr2=" + criteriasJsonString2()+ "&page=create";

		
	var zip = gebi("zipInput").value;
	if (zip != "") {
		param2 += "&zip=" + zip;
	}
	
	sendJsonRequest("map.json", param2, startSearch2, "saveSearch", "LogicHandler");

}

function startSearch2(ds) {
	var result = ds.getData()[0].json;
	if (!result) {
		alert(ALERT_SEARCH_NAME_EXIST);
	} else {
		document.location="/gsearch";
	}
}

function setSearchName(){
   var d = new Date();
   var date = d.getMonth()+'-'+d.getDate()+'-'+d.getYear()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
   gebi("searchName").value=gebi("keyword").value+" "+date;
}

function getParamsForCreateSearch() {
	var result = "";
	var company = gebi("customCompanies").value;
	if (company > 0)
		result += "companyId=" + company + "&";
	var jobTitle = gebi("jobTitle").value;
	if (jobTitle >=0)
		result += "customTitleId=" + jobTitle + "&";
	if (jobTitle == 0) {
		var jobOther = gebi("jobTitleTextInput").value;
		if (jobOther == TITLE_DEFAULT_VALUE)
			jobOther = "";
		if (jobOther != "") {
			result += "jobOther=" + jobOther + "&";
		}
	}	
	var zip = gebi("zipInput").value;
	if (zip == ZIP_DEFAULT_VALUE)
		zip = "";
	if (zip != "")
		result += "zip=" + zip + "&";

	result += "name=auto"
		
	//if (result.length > 0)
		//result = result.substr(0, result.length-1);
		
	return result;
}

function getParamsForCreateSearch2() {
	var result = "";
	var keywords = gebi("keyword").value;

	if (keywords != '')
		result += "positionTitle=" + keywords + "&";
	
	var zip = gebi("zipInput").value;
	if (zip == ZIP_DEFAULT_VALUE)
		zip = "";
	if (zip != "")
		result += "zip=" + zip + "&";

	result += "name=auto";	
		
	return result;
}


function doShowAutoConfirm(templateName){
	var zipParam = "";
	var zip = gebi("zipInput").value;
	if (zip == ZIP_DEFAULT_VALUE)
		zip = "";
	if (zip != "")
		zipParam += "&zip=" + zip;
		
	sendJsonRequest("doc", "templateName=" + templateName + zipParam, cbShowAutoConfirmBox, "getAutoConfirmBox", "HtmlHandler");
}

function cbShowAutoConfirmBox(ds) {
	var str = ds.getDocument();
	gebi("autoConfirmBox").innerHTML = str;
	gebi("autoConfirmBox").style.display = "block";
}

function submitConfirmation(templateName, zip) {
	var zipParam = "";
	if (zip != '')
		zipParam = "&zip=" + zip;
	closeConfirm();
	var callback = function(){sendJsonRequest("map.json", "templateName=" + templateName + zipParam, cbSubmitConfirmation, "autoConfirm", "MiniDataHandler")};	
	new Interactive.Popups.Confirm(500, 150, 'The job will be posted on the Job Board and sent to internal and external candidates identified by the system. Please confirm by clicking the Continue button below.', [callback, null]);	
}

function customize_(templateName, zip) {
	var zipParam = "";
	if (zip != '')
		zipParam = "&zip=" + zip;
		
	sendJsonRequest("map.json", "templateName=" + templateName + zipParam, cbCustomize, "autoCustomize", "MiniDataHandler");		
}

function cbCustomize(ds) {
	var jobid = ds.getData()[0].json;
	window.location = "/gsearch?searchId=" + jobid + "&page=position";
}

function cbSubmitConfirmation(ds) {
	var position = ds.getData()[0].json;
	alert("Your search for " + position + " has been submitted.  You will be notified by email when candidates apply.  Click on menu My Account->My Searches to check on the status of your searches.");
	window.location = "/myAsn";
}

function closeHelp() {
	gebi("customSearchHelpTextFrame").style.display = "none";
	gebi("customSearchHelpText").style.display = "none";
}

function showHelp() {
	gebi("customSearchHelpText").style.display = "block";
	gebi("customSearchHelpTextFrame").style.display = "block";	
}

function closeConfirm(url) {
	gebi("autoConfirmBox").innerHTML = "";
	gebi("autoConfirmBox").style.display = "none";
	if (url != null) {
		window.location = url;
	}
}

function isEmptyZip() {
	return ((gebi("zipInput").value=="") || (gebi("zipInput").value==ZIP_DEFAULT_VALUE));
}

function isDefaultZip() {
	return (gebi("zipInput").value==ZIP_DEFAULT_VALUE)
}

