//The function validates that all the mandatory fields are filled as per specification.
var COUNTRY_USA_GUID = "{83F02198-6C32-11D3-811F-0000F80627E2}";
var COUNTRY_CANADA_GUID =  "{83F02022-6C32-11D3-811F-0000F80627E2}";
var isFormSubmitted = false;

function checkForMandatory()
{	
	var lblFNValue = document.getElementById("idFNMandatory").innerHTML;
	
	if(lblFNValue == "*")
	{
		if(trim(document.forms[0].textQCSFirstName.value)== "")
		{			
			alert("Please complete the required fields.");
			document.forms[0].textQCSFirstName.focus();
			return false;
		}
	}
	
	var lblLNValue = document.getElementById("idLNMandatory").innerHTML;
	
	if(lblLNValue == "*")
	{
		if(trim(document.forms[0].textQCSLastName.value)== "")
		{			
			alert("Please complete the required fields.");
			document.forms[0].textQCSLastName.focus();
			return false;
		}
	}
	
	
	if(trim(document.forms[0].textQCSEmailId.value)== "")
	{
		alert("Please complete the required fields.");
			document.forms[0].textQCSEmailId.focus();
		return false;	
	}	
	
	if(trim(document.forms[0].textQCSAddress1.value)== "")
	{
		alert("Please complete the required fields.");
			document.forms[0].textQCSAddress1.focus();
		return false;	
	}
	
	if(trim(document.forms[0].textQCSCity.value)== "")
	{
		alert("Please complete the required fields.");
			document.forms[0].textQCSCity.focus();
		return false;
	}	
	if(trim(document.forms[0].Country__BillingAddressCountryData.value)!="")
	{		
		if(trim(document.forms[0].Country__BillingAddressCountryData.value)== COUNTRY_USA_GUID)
		{
			if(trim(document.forms[0].USState__BillingAddressCountryData.value)== "")
			{
				alert("Please complete the required fields.");
				document.forms[0].USState__BillingAddressCountryData.focus();
				return false;
			}
		}
		if(trim(document.forms[0].Country__BillingAddressCountryData.value) == COUNTRY_CANADA_GUID )
		{
			if(trim(document.forms[0].CANProvince__BillingAddressCountryData.value)== "")
				{
					alert("Please complete the required fields.");
					document.forms[0].CANProvince__BillingAddressCountryData.focus();
					return false;
				}		
		}		
	}
	else
	{	
		alert("Please complete the required fields.");
		document.forms[0].Country__BillingAddressCountryData.focus();
		return false;	
	}
	if(trim(document.forms[0].textQCSZipCode.value)== "")
	{
		alert("Please complete the required fields.");
			document.forms[0].textQCSZipCode.focus();
		return false;
	}
	
	//Validate the given EMail address
	var strEmail = document.forms[0].textQCSEmailId.value;
	if(!CheckEmailFormat(strEmail))
	{
		alert("You have entered an invalid email address. Please re-enter your email address.");
		document.forms[0].textQCSEmailId.select();
		return false;
	}
	//Function to check for Special Characters
	if( !checkSpecialCharacters() )
		return false;
		
	if(!IsOtherCountrySelected() && !IsOtherStateSelected() && !IsOtherProvinceSelected())
		return true;
	
	return false;
}

function startsWith(value,search)
{
	for(start=0;start<value.length+1-search.length;start++)
		if (value.substr(start,search.length) == search) return true;
	return false;
}

function onBodyLoad()
{
	document.forms[0].textQCSFirstName.focus();
	loadStateProvince();
}

function checkForMandatoryCatalogName() {

	if (ControlToSubmit == "loginWidgetSubmit") {

		if (loginWidgetSetFocus())
			ControlToSubmit = "";

		return false;
	}

	if (isFormSubmitted) {
	    return false;
	}
	
	var catalogChecked = false;
	for( i = 0; i < CatalogListCount; i++)
	{
	    var temp = "" + i;
	    if(i < 10)
	       temp = "0" + i; 
	       
		var strSearchCtlName	=	"RequestCatalogDataList_ctl" + temp + "_chkSelectCatalog" ;
		var chkBox = document.forms[0].elements[strSearchCtlName];
		if( chkBox != null )
		{
			if( chkBox.checked ) 
			{
				catalogChecked = true;
				break;
			}	
		}
	}
	
	if( !catalogChecked )
	{
		document.forms[0].elements["RequestCatalogDataList_ctl00_chkSelectCatalog"].focus();
		alert("Please select any Catalog.");
		return false;
	}

	var retVal = checkForMandatory();
	if (retVal) {
	    isFormSubmitted = true;
	    return true;
	}
	else {
	    return false;
	}
}	
