// BasicSearchValidations.js

//This function Validates BasicSearchForm before sumbitting. 
//This is called by prepareSubmit(thisForm) funcion.
function validateForm(thisForm) {
    //if (thisForm.dunsNumber.value == null  || thisForm.dunsNumber.value == ""){	
		if (thisForm.busName.value == null || thisForm.busName.value == "" || thisForm.busName.value == "Business, Vendor, Client"){		
			alert("Please enter the name of the company that you are searching for.");
			thisForm.busName.focus();		
			return false;	
		}else if(thisForm.country.options[thisForm.country.selectedIndex].value=="US") {
			var us = document.getElementById("usState");
			if(us.options[us.selectedIndex].value == null || us.options[us.selectedIndex].value == ""){		
				alert("Please specify a state or you may search nationwide by choosing Nationwide option.");		
				us.focus();		
				return false;
			}else {
				thisForm.state.value=us.options[us.selectedIndex].value;
			}		
		}else if(thisForm.country.options[thisForm.country.selectedIndex].value=="CA") {
			var ca = document.getElementById("caState");
			if(ca.options[ca.selectedIndex].value == null || ca.options[ca.selectedIndex].value == ""){		
				alert("Please select a Province for Canada");		
				ca.focus();		
				return false;
			}else {
				thisForm.state.value=ca.options[ca.selectedIndex].value;
			}		
		}
	/*} else {
		var dunsNumber = thisForm.dunsNumber.value;
		dunsNumber = dunsNumber.replace("-", "");
		dunsNumber = dunsNumber.replace("-", "");
		thisForm.dunsNumber.value = dunsNumber;
		var errMessage = " is an invalid DUNS. Please check your number and try again.";
		if (dunsNumber == null || dunsNumber == ""){		
			alert("Please enter the D-U-N-S number of the company that you are searching for.");
			thisForm.dunsNumber.focus();		
			return false;
		} else if(dunsNumber.length != 9 || isNaN(dunsNumber)){
			alert(dunsNumber + errMessage);
			thisForm.dunsNumber.focus();		
			return false;
		}
	}*/	
	return true;		
}

//This function submits the page, if the user enters the valid data
//This funciton calls validateForm(thisForm) function

function prepareSubmit(thisForm) {		
	if(validateForm(thisForm))	{	
		thisForm.submit();
		//return false;
		submitRequest();
	} else {
	 	return false;				
	}		
}//Added code danny comments JIRA-7353
	var requestSubmitted = false; 
	 function submitRequest() { 
	   if (!requestSubmitted ) { 
	        requestSubmitted = true; 
	        return true; 
	    } 
	    return false; 
	 }	
		

//This function sets initial focus to Business name field

function setFocus() {
	document.BasicSearchForm.busName.focus();
}

function setSearchFocus() {
	document.BasicSearchForm1.busName.focus();
}

//This function disables if the selected country is non-US or non-Canada

function isStateRequired(thisForm, selectedValue) {
	if(selectedValue == "US" || selectedValue == "CA") {
		if (selectedValue == "US") {
        		var us = document.getElementById("usStates");
				var ca = document.getElementById("caStates");
				var no = document.getElementById("noStates");
				if(us.style.display == "none"){
					us.style.display = "block";
					ca.style.display = "none";
					no.style.display = "none";
				}
				else{
					us.style.display = "block";
				}
       	 } else {
	    	var us = document.getElementById("usStates");
			var ca = document.getElementById("caStates");
			var no = document.getElementById("noStates");			
			if(ca.style.display == "none"){
				ca.style.display = "block";
				us.style.display = "none";
				no.style.display = "none";
			}
			else{
				ca.style.display = "block";					
			}
		 }
	} else {
		var us = document.getElementById("usStates");
		var ca = document.getElementById("caStates");
		var no = document.getElementById("noStates");
		if(no.style.display == "none"){
				no.style.display = "block";
				us.style.display = "none";
				ca.style.display = "none";
		}
		else{
				no.style.display = "block";					
		}
	}
}

function submitWithEnter(thisForm){
	var netscape = "";
	var ver = navigator.appVersion;
	var len = ver.length;
	for(var i = 0; i < len; i++) if (ver.charAt(i) == "(") break;
	netscape = (ver.charAt(i+1).toUpperCase() != "C");
	
	// determines whether Netscape or Internet Explorer
	k = (netscape) ? DnEvents.which : window.event.keyCode;
	if (k == 13) { // enter key pressed
		prepareSubmit(thisForm);
		return false;
	} else {	
		if (netscape) document.captureEvents(Event.KEYDOWN|Event.KEYUP);
		return true;
	}
}

function searchFormSubmit(thisForm) {		
	if(validateBasicSearchForm(thisForm))	{	
		thisForm.submit();
		//return false;
		submitRequest();
	} else {
	 	return false;				
	}		
}

// EPLTFM-8371: New Function added for new international basic search page
function searchIntlFormSubmit(thisForm) {		
	if(validateIntlBasicSearchForm(thisForm))	{	
		thisForm.submit();
		submitRequest();
	} else {
	 	return false;				
	}		
}

// EPLTFM-8371: New Function added for new international basic search page validation
function validateIntlBasicSearchForm(thisForm) {

	var vCountry = document.getElementById("countryMod");
	
	if(document.getElementById("prod_adv_search")!=null && document.getElementById("prod_adv_search").style.display == "none"){
    
    	if (thisForm.busNameMod.value == null || thisForm.busNameMod.value == ""){		
			alert("Please enter the name of the company that you are searching for.");
			thisForm.busNameMod.focus();		
			return false;	
		}else if(vCountry.options[vCountry.selectedIndex].value=="-") {
				alert("Please select the country that you are searching for.");	
				vCountry.focus();	
				return false;
		}
	thisForm.busName.value = thisForm.busNameMod.value;	
	thisForm.country.value = vCountry.options[vCountry.selectedIndex].value;
	return true;
 }
}

function validateBasicSearchForm(thisForm) {
if(document.getElementById("prod_adv_search")!=null && document.getElementById("prod_adv_search").style.display == "none"){
    //if (thisForm.dunsNumber.value == null  || thisForm.dunsNumber.value == ""){	
		if (thisForm.busNameMod.value == null || thisForm.busNameMod.value == ""){		
			alert("Please enter the name of the company that you are searching for.");
			thisForm.busNameMod.focus();		
			return false;	
		}else {
			var us = document.getElementById("usStateMod");
			if(us.options[us.selectedIndex].value == null || us.options[us.selectedIndex].value == ""){		
				alert("Please specify a state or you may search nationwide by choosing Nationwide option.");		
				us.focus();		
				return false;
			}else {
				thisForm.state.value=us.options[us.selectedIndex].value;
			}		
		}
	thisForm.busName.value= thisForm.busNameMod.value;	
	return true;
}
}
//This function Validates BasicSearchForm before sumbitting. 
//This is called by prepareLandingSubmit(thisForm) funcion.
function validateLandingForm(thisForm) {
    //if (thisForm.dunsNumber.value == null  || thisForm.dunsNumber.value == ""){	
		if (thisForm.busName.value == null || thisForm.busName.value == ""){		
			alert("Please enter the name of the company that you are searching for.");
			thisForm.busName.focus();		
			return false;	
		}else{
			var us = document.getElementById("usState");
			if(us.options[us.selectedIndex].value == null || us.options[us.selectedIndex].value == ""){		
				alert("Please specify a state or you may search nationwide by choosing Nationwide option.");		
				us.focus();		
				return false;
			}else {
				thisForm.state.value=us.options[us.selectedIndex].value;
			}		
		}
	return true;		
}

//This function submits the page, if the user enters the valid data
//This funciton calls validateLandingForm(thisForm) function
function prepareLandingSubmit(thisForm) {		
	if(validateLandingForm(thisForm))	{	
		thisForm.submit();
		//return false;
		submitRequest();
	} else {
	 	return false;				
	}		
}

