//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}";

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].textQCSReEmailId.value)== "")
	{
		alert("Please complete the required fields.");
			document.forms[0].textQCSReEmailId.focus();
		return false;	
	}
	if(trim(document.forms[0].textQCSAddress.value)== "")
	{
		alert("Please complete the required fields.");
			document.forms[0].textQCSAddress.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__CountryData.value)!="")
	{		
		if(trim(document.forms[0].Country__CountryData.value)== COUNTRY_USA_GUID)
		{
			if(trim(document.forms[0].USState__CountryData.value)== "")
			{
				alert("Please complete the required fields.");
				document.forms[0].USState__CountryData.focus();
				return false;
			}
		}
		if(trim(document.forms[0].Country__CountryData.value) == COUNTRY_CANADA_GUID )
		{
			if(trim(document.forms[0].CANProvince__CountryData.value)== "")
				{
					alert("Please complete the required fields.");
					document.forms[0].CANProvince__CountryData.focus();
					return false;
				}		
		}		
	}
	else
	{	
		alert("Please complete the required fields.");
		document.forms[0].Country__CountryData.focus();
		return false;	
	}
	if(trim(document.forms[0].textQCSZipCode.value)== "")
	{
		alert("Please complete the required fields.");
			document.forms[0].textQCSZipCode.focus();
		return false;
	}
	if(trim(document.forms[0].drpCaseCategories.value)== "0")
	{
		alert("Please complete the required fields.");
			document.forms[0].drpCaseCategories.focus();
		return false;	
	}
	if(trimSpaceAndReturn(document.forms[0].textQCSMessage.value)== "")
	{
		alert("Please complete the required fields.");
			document.forms[0].textQCSMessage.focus();
		return false;	
	}	

	//Validate the given EMail address
	var strEmail = trim(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;
	}
		
	if(trim(document.forms[0].textQCSEmailId.value) != trim(document.forms[0].textQCSReEmailId.value))
	{	
		alert("The email address you entered does not match. Please re-enter your email address.");
		document.forms[0].textQCSReEmailId.select()
		return false;
	}
		
	if(!IsOtherCountrySelected() && !IsOtherStateSelected() && !IsOtherProvinceSelected())
		return checkSpecialCharacters(); ////Function to check for Special Characters
	
	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;
}

/*
To check the maximum length allowed for a field onKeyPress event fired.
*/
function checkMaximumLengthOnKeyPress(casenote_field) 
{
	
	var maxLength = 1023;
	var existingText = casenote_field.value;
	if (existingText.length > maxLength)
	{ 
		casenote_field.value = existingText.substring(0,maxLength);
		return false; 
	} 
	else
	{ 
		//newline character is considered as two digit so it should be allowed till the
		//value length reaches maxLength-2 after that it should not be allowed
		if((existingText.length == maxLength-1) &&
			(event.keyCode == 13))
		{
			return false;
		}
		return true; 
	}
} 		
/*
To check the maximum length allowed for a field onPaste event fired.
*/
function checkMaximumLengthOnPaste(casenote_field)
{

	var maxLength = 1024;
	//existing value in textarea
	var existingText = casenote_field.value;
	
	//if entered value length is < maxLength then allow pasting
	if (existingText.length < maxLength)
	{
		//get Copied string in clipBoardData
		var clipBoardText = window.clipboardData.getData("Text");
		totalLength = existingText.length + clipBoardText.length;
	
		//if total length is > maxlength then need to truncate the string
		//more than maximum length. Also don't allow the user to paste the copied string
		//because it has more than maximum length, so just directly append to the form field value.
		if(totalLength > maxLength)
		{	
			//get strings that can be added at the end of the existing string
			var noOfCharactersToAppend = clipBoardText.length - (totalLength - maxLength);
			
			var strAppendToExistingText = clipBoardText.substr(0,noOfCharactersToAppend);
						
			//append with the existing value
			casenote_field.value = existingText + strAppendToExistingText;
			
			
			//if the end(ex:- maxLength =254 then 254th) character is a newline character then the javascript consider it as a two character
			//so the length of the field value will become one characer more than maximum length. In such case we need to truncate the newline character.
			if(casenote_field.value.length>maxLength)
				casenote_field.value = casenote_field.value.substr(0,maxLength-1);
			
			return false;
		}
		return true;
	}
	else
		return false;
}		


function onBodyLoad()
{
	document.forms[0].textQCSFirstName.focus();
	loadStateProvince();
}		
