var xmlHttp;
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

function doRequestUsingGETCountry(ctrl,ctrlValue) {
	var objChildDest = document.getElementById("state");
	if ((ctrl.value == null)|| (ctrl.value == ""))
	{
		removeOptions(objChildDest);
		var objOption=new Option(controls_state_default,"");
		objChildDest[objChildDest.length]=objOption;
		return;
	}
    createXMLHttpRequest();
    queryString =   "countryState.do?method=load" ;
    queryString = queryString + "&selcountry="+ctrlValue;
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET", queryString, true);
    xmlHttp.send(null);
}

function handleStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
          updateStates();
        }
    }
}

/*
 * Update States corresponding to the country
 * described in XML document.
 */
function updateStates() {
	var stateXML = xmlHttp.responseXML;
	 var states = stateXML.getElementsByTagName("states")[0];
     var items = states.getElementsByTagName("state");
	 for (var i = 0 ; i < items.length ; i++) {
	     var item = items[i];
	     var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
	     var value = item.getElementsByTagName("value")[0].firstChild.nodeValue;
		 var idx = document.getElementById('state').options.length;
		 document.getElementById('state').options.length= items.length+1;
	     document.getElementById('state').options[i+1] = new Option(name, value);
	 }
}

/* This function is used to submit the contact us form and sending mail*/
function sendEmail()
{
	countryValue = getDropdownDesc(document.getElementById("country"));
	if(countryValue == '- Select your country -')
	{
		countryValue = "" ;
	}
	if(countryValue == "undefined" || countryValue == "" || countryValue == null)
	{
		countryValue = "" ;
	}
	document.getElementById("countryDesc").value = countryValue;
	
	stateValue = getDropdownDesc(document.getElementById("state"));
	if(stateValue == controls_state_default)
	{
		stateValue = "" ;
	}
	if(stateValue == "undefined" || stateValue == "" || stateValue == null)
	{
		stateValue = "" ;
	}
	document.getElementById("stateDesc").value = stateValue;
	document.getElementById("stateNew").value = stateValue;
	
	//document.getElementById("visitorCategory").value = getSelectedVisitorCatgory();
	var visitorCategoryObj = "";
	if((document.getElementById('Consumer').checked==false) && (document.getElementById('Professional').checked==false))
	{
		visitorCategoryObj = "visitorCategory";
	}
	
	var valid=validateMandatoryContact(visitorCategoryObj, "Category", "firstName", 
		"First Name", "lastName", "Last Name", "emailAddress", 
		"E-mail Address", "confirmEmailAddress", "Confirm E-mail Address","dobMonth", "Month of Birth","dobDay", "Day of Birth","dobYear", "Year of Birth", "message", "Message");//"age", "Age", "message", "Message");
	
	messageField = document.getElementById("message");
	if (messageField.value.length > 500) // if too long...trim it!
	messageField.value = messageField.value.substring(0, 500);
	
	if(valid)
	{
		if(validateFields())
		{
			document.contactUsForm.action = "loadContactUs.do?method=sendMail";
			document.contactUsForm.submit();
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}// End of sendEmail method

function getDropdownDesc(id)
{
	var obj;
	var selectedValue;
	var countSelectedOptions = 0;
	obj = id;
	for(var i=0; i<obj.options.length; i++)
	{
		if(obj.options[i].selected) 
		{
			countSelectedOptions++;
			selectedValue = obj.options[i].text;
		}
	}
	return selectedValue;
}

/* This function is used to reset the value of contact us form*/
function doReset(frm)
{
	var objChildDest="";
	if(confirm(contactus_confirm_reset_GLB_002))
	{
		objChildDest = document.getElementById("state");
		removeOptions(objChildDest);
		var objOption=new Option(controls_state_default,"");
		objChildDest[objChildDest.length]=objOption;
		frm.reset();
		document.getElementById("firstName").focus();
	}
	else
	{
		return false;
	}
	
}// End of doReset method

/* This function is used to retrieve the value of selected category*/
function getSelectedVisitorCatgory()
{
	var visitorCat = document.getElementsByName("visitor");
	var visitorCatgoryValue = "";
	for(var i=0;i<visitorCat.length;i++)
	{
		if(visitorCat[i].checked)
		{
			visitorCatgoryValue = visitorCat[i].value;
			//alert(visitorCatgoryValue);
		}
	}
	return visitorCatgoryValue;
}// End of getSelectedVisitorCatgory method

/* This function is used to validate the First Name field of contact us form*/
function isAlphaNumericValue(inputString,fieldName)
{
	var reChar=/^[a-zA-Z0-9\s][a-zA-Z0-9\s]*$/
    if(!reChar.test(inputString.value))
	{
		 alert(contactus_alertnumeric_MSS_040+" in the "+ fieldName+" field.");
		 inputString.focus();
		 return false;
	}
	return true;
}// End of isAlphaNumericValue method

/* This function is used to validate the Last Name field of contact us form*/
function isValidLastname(inputString,fieldName)
{
	var reCharSpecial=/^[a-zA-Z0-9-,\.\s][a-zA-Z0-9-,\.\s]*$/
    if(!reCharSpecial.test(inputString.value))
	{
		 alert(contactus_alert_LastName_MSS_031+" in the "+ fieldName+" field.");
		 inputString.focus();
		 return false;
	}
	return true;
	
}

/* This function is used to validate the city field of contact us form*/
function isValidCity(inputString,fieldName)
{
	var reCityChar=/^[a-zA-Z0-9\.\s][a-zA-Z0-9\.\s]*$/
    if(!reCityChar.test(inputString.value) && (inputString.value != ""))
	{
		 alert(contact_error_apphaCityChar_MSS_046+" in the "+ fieldName+" field.");
		 inputString.focus();
		 return false;
	}
	return true;
}

//validate phone fields
function validatePhoneOnly()
{
	var noofObjects = arguments.length;
	var error = "";
	var errorMsg ="";
	var flag = true;
	var charMsg = "";
	for(var i = 0; i < noofObjects; i=i+2)
    {
    	if(isInteger(document.getElementById(arguments[i]).value))
    	{
    	}
    	else
    	{
    		charMsg = contactus_alert_phone_MSS_032;
    		alert(charMsg);
    		document.getElementById(arguments[0]).focus();
		    return false;
    	}
    }
    return true;
}// End of validateNumberOnly method


/* This function is used to validate the input fields of contact us form*/
function validateFields()
{
	var firstName = document.getElementById("firstName");
	var lastName = document.getElementById("lastName");
	var email = document.getElementById("emailAddress"); 
	var email2 = document.getElementById("confirmEmailAddress"); 
	var phone = document.getElementById("phone");
	var phone1 = document.getElementById("phone1");
	var phone2 = document.getElementById("phone2");
	var zipCode = document.getElementById("zipCode");
	var zipExt = document.getElementById("zipExtension");
	var address1 = document.getElementById("address1");
	var address2 = document.getElementById("address2");
	var city = document.getElementById("city");
	var dobMonth = document.getElementById("dobMonth");
	var dobDay = document.getElementById("dobDay");
	var dobYear = document.getElementById("dobYear");
	
	var flag = false;
	var result = false;
	if(isAlphaNumericValue(firstName,"First Name"))
	{
		result = true;
		flag = true;
		if(isValidLastname(lastName,"Last Name"))
		{
			result = true;
			flag = true;
			if(checkEmail(email,email2))
			{
				result = true;
				flag = true;
				if(validatePhoneOnly("phone","Phone","phone1","Phone","phone2","Phone"))
					{
					result = true;
					flag = true;
						if(validatePhone())
						{
							result = true;
							flag = true;
							if(validateAddress("address1","Address 1","address2","Address 2"))
							{
								result = true;
								flag = true;
								if(isValidCity(city,"City"))
								{
									result = true;
									flag = true;
									if(validateZip(zipCode,zipExt))
									{
										result = true;
										flag = true;
										if(validateDOB(dobMonth, dobDay, dobYear)){
											result = true;
											flag = true;
										}else{
											flag = false;
										}
										/*if(validateAge())
										{
											result = true;
											flag = true;

										}
										else
										{
											flag = false;
										}*/
									}
									else
									{
										flag = false;
									}

								}//end of isValidCity()
								else
								{
									flag = false;
								}
							}//end of validateAddress()
							else
							{//alert('address'+flag);
								flag = false;
							}
						}//end of validatePhone()
						else
						{
							flag = false;
						}
					}//end of validateNumberOnly
					else
					{
						flag = false;
					}
			}//end of checkEmail
			else
			{
				flag = false;
			}
		}//end of varify last name
		else
		{
			flag = false;
		}
	}//end of varify first name
	else
	{
		flag = false;
	}
	
	//alert(flag);
	return flag;
}// End of validateFields method

/* This function is used to validate the phone field of contact us form*/
function validatePhone()
{
	var phone = document.getElementById("phone").value;
	var phone1 = document.getElementById("phone1").value;
	var phone2 = document.getElementById("phone2").value;
	if(phone != "" && phone1 == "" && phone2 == "")
	{
		alert(contactus_alert_phone_MSS_032);
		document.getElementById("phone").focus();
		return false;
	}
	else if(phone != "" && phone1 == "" && phone2 != "")
	{
		alert(contactus_alert_phone_MSS_032);
		document.getElementById("phone").focus();
		return false;
	}
	else if(phone != "" && phone1 != "" && phone2 == "")
	{
		alert(contactus_alert_phone_MSS_032);
		document.getElementById("phone").focus();
		return false;
	}
	else if(phone == "" && phone1 != "" && phone2 != "")
	{
		alert(contactus_alert_phone_MSS_032);
		document.getElementById("phone").focus();
		return false;
	}
	else if(phone == "" && phone1 == "" && phone2 != "")
	{
		alert(contactus_alert_phone_MSS_032);
		document.getElementById("phone").focus();
		return false;
	}
	else if(phone == "" && phone1 != "" && phone2 == "")
	{
		alert(contactus_alert_phone_MSS_032);
		document.getElementById("phone").focus();
		return false;
	}
	else if(phone != "" && phone1 != "" && phone2 != "")
	{
		if((isMinLength(document.getElementById("phone"),3)) || 
			(isMinLength(document.getElementById("phone1"),3)) ||
				(isMinLength(document.getElementById("phone2"),4)))
				{
					alert(contactus_alert_phone_MSS_032);
					document.getElementById("phone").focus();
					return false;
				}
				else
				{
					return true;
				}
	}
	else if(phone == "" && phone1 == "" && phone2 == "")
	{
		return true;
	}
}// End of validatePhone method

//validate zip fields
function validateZipFields()
{
	var noofObjects = arguments.length;
	var error = "";
	var errorMsg ="";
	var flag = true;
	var charMsg = "";
	for(var i = 0; i < noofObjects; i=i+2)
    {
    	if(isInteger(document.getElementById(arguments[i]).value))
    	{
    	}
    	else
    	{
    		charMsg = contactus_error_num_MSS_039+" in the "+ arguments[i+1]+" field.";
    		alert(charMsg);
    		document.getElementById(arguments[i]).focus();
		    return false;
    	}
    }
    return true;
}// End of validateZipFields method


/* This function is used to validate the zip code and zip code extension fields of contact us form*/
function validateZip(zipCode,zipExt)
{
	if(validateZipFields("zipCode","Postal Code","zipExtension","4 Digit Extension"))
	{
		if(isMinLength(zipCode,5) && zipCode.value != "")
		{
			alert(contactus_alert_invalidZipcode_MSS_044);
			zipCode.focus();
			return false;
		}
		else if(zipCode.value == "" && zipExt.value != "")
		{
			alert(contactus_alert_mandatory_Zipcode_MSS_033);
			zipCode.focus();
			return false;
		}
		else if(isMinLength(zipExt,4) && zipExt.value != "")
		{
			alert(contactus_min4Digit_MSS_045+" in the 4 Digit Extension field.");
			zipExt.focus();
			return false;
		}
		return true;
	}
	else
	{
		return false;	
	}
	
}//end of validateZipLength method

/* This function is used to validate the address fields of contact us form*/
function validateAddress()
{
	var noofObjects = arguments.length;
	var error = "";
	var errorMsg ="";
	var flag = true;
	var charMsg = "";
	for(var i = 0; i < noofObjects; i=i+2)
    {
    	if(!isAddressWithNumberAndSpecialChar(document.getElementById(arguments[i])))
    	{
    	}
    	else
    	{
    		charMsg =contactus_error_alphaNumSpecialChar_MSS_038 +" in the "+ arguments[i+1]+" field.";
    		alert(charMsg);
    		document.getElementById(arguments[i]).focus();
		    return false;
    	}
    }
    return true;
}// End of validateAddress method

/* This function is used to validate the length of message text area of contact us form*/
function msgLength(maxlimit)
{
field = document.getElementById("message");
if (field.value.length > maxlimit)
{ // if too long...trim it!
//alert("You cannot enter more than "+maxlimit+" characters in this field");
field.value = field.value.substring(0, maxlimit);
}
}

/* This function is used to block the enter */
function formKeyValidate(thisObj)
{ //alert(window.event.keyCode);
   	if ((window.event.keyCode == 13))
   	{ 
		
		return false;
	}
	else
	{
		return true;
	}
   
}
//validate zip fields
function validateAge()
{
	var noofObjects = arguments.length;
	var error = "";
	var errorMsg ="";
	var flag = true;
	var charMsg = "";
	var age=document.getElementById("age").value;
		if(isInteger(age))
    	{
		if(isMinLength(document.getElementById("age"),4)){
		alert("Please enter birth year i.e 1978");
			document.getElementById("age").focus();
		    	return false;
		}
    	}
    	else
    	{
    		charMsg = "Please enter a numeric value for Age.";
    		alert(charMsg);
    		document.getElementById("age").focus();
		    return false;
    
    }
    return true;
}// End of validateZipFields method

function validateDOB(dobMonth, dobDay, dobYear){
	var user_error_birthYear_MSS_051 ='Birth year should be between 1900 and current year.';
	var user_error_min2Digit_MSS_052= 'Please enter 2 digits';
	var user_error_validMonth_MSS_053= 'Please enter a valid month.';
	var user_error_validDay_MSS_054= 'Please enter a valid day.';
	var charMsg = "Please enter a numeric value for ";
	if(!isInteger(dobMonth.value)){
		alert(charMsg+ "the Month field.");
		flag = false;
		dobMonth.focus();
        return flag;
	}
	if(!isInteger(dobDay.value)){
		alert(charMsg+ "the Day field.");
		flag = false;
		dobDay.focus();
        return flag;
	}
	if(!isInteger(dobYear.value)){
		alert(charMsg+ "the Year field.");
		flag = false;
		dobYear.focus();
        return flag;
	}
	if(isMinLength(dobMonth,2)){
		alert(user_error_min2Digit_MSS_052+ " in the Month field.");
		flag = false;
		dobMonth.focus();
        return flag;
	}
	
	if(isMinLength(dobDay,2)){
		alert(user_error_min2Digit_MSS_052+ " in the Day field.");
		flag = false;
		dobMonth.focus();
        return flag;
	}
		var currentBirthDate = new Date();
		var currentBirthYear =  currentBirthDate.getYear();
		if(currentBirthYear < 200)
			currentBirthYear = 1900 + currentBirthDate.getYear();
		
		if(isMinLength(dobYear,4))
		{
			alert("Please enter 4 digits in the Year field.");
			//alert(" in the "+arguments[i+1]+ " field.");
			flag = false;
			dobYear.focus();
	        return flag;
		}
		if((dobYear.value<1900) ||dobYear.value > currentBirthYear ){
			alert(user_error_birthYear_MSS_051);
			flag = false;
			dobYear.focus();
			return flag;
		}
		var bMonth = dobMonth.value;
		var bDay = dobDay.value;
		var bYear = dobYear.value;	    				    			
		var daymonthflag = checkValidDate(bMonth,bDay,bYear);
		if(daymonthflag=="dayFlag")
		{
			alert(user_error_validDay_MSS_054);
			flag=false;
			dobDay.focus();
			return flag;
		
		}
		else if (daymonthflag=="monthFlag")
		{
			alert(user_error_validMonth_MSS_053);
			flag=false;
			dobMonth.focus();
			return flag;
		}
	
	 return true;
}


