 
/* ****************************************************************************************** */
//Function to display 'Privacy Preferences'
function openPrivacy(urlAddress, width, height) {
	url = urlAddress  + "?RGSTRAPP";
	var lsEmail = document.frmInfo.email.value;
	lsEmail=lsEmail.replace(/^\s*|\s*$/g,"");
	
	if (validEmail(lsEmail))  {
		url = url + "&" + lsEmail;
	        windowName  = "buttons";
	        params      = "toolbar=0,";
	        params     += "location=0,";
	        params     += "directories=0,";
	        params     += "status=0,";
	        params     += "menubar=0,";
	        params     += "scrollbars=1,";
	        params     += "resizable=0,";
	        params += "width="+width+",";
	        params += "height="+height;
	        win = window.open(url, windowName, params);
	        win.opener.name = "opener";
	} else {
		showFocus("Please enter a valid e-mail.", document.frmInfo.email);
	}
}  

// VALIDATION FUNCTIONS

/* ****************************************************************************************** */
// Master validation function
function validateOwnerForm(form) {
 //variables to shorten dropdown item references
	var selectedState = form.st.options[form.st.selectedIndex].value;
	var emailTrim=form.email.value;
	emailTrim=emailTrim.replace(/^\s*|\s*$/g,"");  //Removing the leading and trailing whitespace characters
	
	if ((form.first_name) && (form.first_name.value == "")) { showFocus("Please enter your first name.", form.first_name,true); return false; }
	if ((form.last_name) && (form.last_name.value == "")) { showFocus("Please enter your last name.", form.last_name,true); return false; }
	if ((form.email) && (!validEmail(emailTrim))) { showFocus("Please enter a valid E-Mail.", form.email,true); return false; }
	if ((form.street) && (form.street.value == "")) { showFocus("Please enter your address.", form.street,true); return false; }
	if ((form.city) && (form.city.value == "")) { showFocus("Please enter your city.", form.city,true); return false; }
	if ((form.st) && (selectedState == "")) { showFocus("Please select your state.", form.st,false); return false; }
	if (form.zip) {	
		if (form.zip.value == "") { showFocus("Please enter your zip code.", form.zip,true); return false; }
		if (!validZip(form.zip.value)) { showFocus("Please enter a valid zip code.", form.zip,true); return false; }
	}	
	if ((form.phone1) || (form.phone2) || (form.phone3)) {
		if ((form.phone1.value + form.phone2.value + form.phone3.value) == "") { showFocus("Please enter your phone number.", form.phone1,true); return false; }
		if (form.phone1.value == "") { showFocus("Please enter your area code.", form.phone1, true); return false; }
		if (!validPhone(form.phone1.value, form.phone2.value, form.phone3.value)) { showFocus("Please enter a valid phone number.", form.phone1,true); return false; }
	}
}
function validateApplForm(form,edit) {

	var selectedBrand = form.brand.options[form.brand.selectedIndex].value;
	var selectedAppliance = form.appliance.options[form.appliance.selectedIndex].value;
	var selectedApplianceType = form.appliancetype.options[form.appliancetype.selectedIndex].value;
	var selectedInstMonth = form.instMonth.options[form.instMonth.selectedIndex].value;
	var selectedInstDay = form.instDay.options[form.instDay.selectedIndex].value;
	var selectedInstYear = form.instYear.options[form.instYear.selectedIndex].value;

	
	if ((form.brand) || (form.appliance) || (form.appliancetype)) {
		if (selectedBrand == "000") { showFocus("Please select a brand.", form.brand,false); return false; }
		if (selectedAppliance == "000") { showFocus("Please select an appliance.", form.appliance,false); return false; }
		if ((selectedApplianceType == "") || (selectedApplianceType == "000") || (selectedApplianceType == ".")) { showFocus("Please select an appliance type.", form.appliancetype,false); return false; }
	}
	if (form.model) {
		if (form.model.value == "") { showFocus("Please enter a model number.", form.model,true); return false; }
		if (!validModelNumber(form.model.value)) { showFocus("Please enter a valid model number.", form.model,true); return false; }
	}
	if (form.serial) {
		if (form.serial.value == "") { showFocus("Please enter a serial number.", form.serial,true); return false; }
		if (!validSerialNumber(form.serial.value, selectedBrand)) { showFocus("Please enter a valid serial number.", form.serial,true); return false; }
	}
	if ((form.instMonth) || (form.instDay) || (form.instYear)) {
		if ((selectedInstMonth + selectedInstDay + selectedInstYear) == "") { showFocus("Please select an installation date.", form.instMonth,false); return false; }
		if (selectedInstMonth == "") { showFocus("Please select an installation month.", form.instMonth,false); return false; }
		if (selectedInstDay == "") { showFocus("Please select an installation day.", form.instDay,false); return false; }
		if (selectedInstYear == "") { showFocus("Please select an installation year.", form.instYear,false); return false; }
	}
	if (edit == false)
	{
		var selectedExpMonth = form.expMonth.options[form.expMonth.selectedIndex].value;
		var selectedExpDay = form.expDay.options[form.expDay.selectedIndex].value;
		var selectedExpYear = form.expYear.options[form.expYear.selectedIndex].value;
		if (form.Service) {
			//modified by saravanan 
			//if ((form.Service[1].checked) && ((form.expMonth.value == "") || (form.expDay.value == "") || (form.expYear.value == ""))) { showFocus("Please select a warranty expiration date.", form.expMonth,false); return false; }
			if ((form.Service[1].checked) && ((selectedExpMonth + selectedExpDay + selectedExpYear) == "")) { showFocus("Please select a warranty expiration date.", form.expMonth,false); return false;}
		}
		if(form.Service[1].checked)
		{
			if ((form.expMonth) || (form.expDay) || (form.expYear)) {
				if ((selectedExpMonth != "") || (selectedExpDay != "") || (selectedExpYear != "")) {
					if (selectedExpMonth == "") { showFocus("You have not selected a valid expiration month.  Please re-enter.", form.expMonth,false); return false; }
					if (selectedExpDay == "") { showFocus("You have not selected a valid expiration day.  Please re-enter.", form.expDay,false); return false; }
					if (selectedExpYear == "") { showFocus("You have not selected a valid expiration Year.  Please re-enter.", form.expYear,false); return false; }
				}
			}	
		}
		else
		{
			if ((form.expMonth) || (form.expDay) || (form.expYear)) {
				if ((selectedExpMonth != "") || (selectedExpDay != "") || (selectedExpYear != "")) {
					form.expMonth.value = "";
					form.expDay.value = "";
					form.expYear.value = "";
				}
			}
		}
		if (form.another && !(form.another[0].checked) && !(form.another[1].checked)) { window.alert("Would you like to Register Another Appliance? Please select yes or no."); return false; }
	}
	
}

/* ****************************************************************************************** */
//Alphanumeric character validator
function validSerialNumber(testString, selectedBrand) {
	testString = testString.replace(/[\s\-]+/, "");
	if (selectedBrand && selectedBrand !== "000" && selectedBrand !== "UNK") {
		if (!/^[ADFGHLMRSTVZ][ADFGHLMRSTVZ]/i.test(testString)) {
			return false;
		}		
	}
	return (/^[A-Za-z0-9][A-Za-z0-9]*$/).test(testString);
}

/* ****************************************************************************************** */
//Alphanumeric character validator
function validModelNumber(testString) {
	
	testString=testString.replace(/^\s*|\s*$/g,""); //Removing the leading and trailing whitespace characters
	var validNums = "0123456789";
	var inValidNums = false;
	var validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var inValidChars = false;
	for (i=0; i<testString.length; i++) { 
   		testChar = testString.charAt(i);
		if (validNums.indexOf(testChar,0) != -1) {inValidNums = true;}
		if (validChars.indexOf(testChar,0) != -1) {inValidChars = true;}
	}
	return (inValidNums && inValidChars);
}


/******************************************************************************************* */
//Phone Number Validator
function validPhone(areacode,prefix,base) {
	var validChars = "0123456789";
	var fullNumber = areacode + prefix + base;
	if (fullNumber.length != 10) {			// Phone number MUST be 10 digits
		return false;
	}
	for (i=0; i<fullNumber.length; i++) {   	// check all the characters in the phone number
   		testChar = fullNumber.charAt(i);
		if (validChars.indexOf(testChar,0) == -1) {
			return false;					// break loop and return false if a phone number character is not in the valid character list
		}
	}
	return true;
}

/* ****************************************************************************************** */
//Zip Code Validator
function validZip(zipcode) {

	var validChars = "0123456789";
	for (i=0; i<zipcode.length; i++) {   	// check all the characters in Zip Code
   		testChar = zipcode.charAt(i);
		if (validChars.indexOf(testChar,0) == -1) {
			return false;					// break loop and return false if a zip code character is not in the valid character list
		}
	}
	return true;
}

/* ****************************************************************************************** */
// Function to check if the email address is valid.
function validEmail(email) {
	
	
	email=email.replace(/^\s*|\s*$/g,"");    //Removing the leading and trailing whitespace characters
	
	var invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;" ;
	
	if (email == "") {                       	// cannot be empty
		return false;
	}
	for (i=0; i<invalidChars.length; i++) {   	// does it contain any inalid characters
   		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	lengthOfEmail = email.length;
	if( (email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
		return false;
	}
	Pos = email.indexOf("@",1);
	if(email.charAt(Pos + 1) == ".") {
		return false;
	}
	while((Pos < lengthOfEmail) && ( Pos != -1)) {
		Pos = email.indexOf(".",Pos);
		if(email.charAt(Pos + 1) == ".") {
        	return false;
		}
		if(Pos != -1) {
        	Pos++;  
		}
	}
	atPos = email.indexOf("@",1);            // there must be one "@" symbol
	if (atPos == -1) {
		return false;
	}
	if (email.indexOf("@",atPos+1) != -1) { // and only one "@" symbol 
		return false;
	}
	periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) {                   // and at least one "." after the "@"
		return false;
	}
	if (periodPos+3 > email.length) {        // must be at least 2 characters after the "."
		return false;
	}
	return true;
} 
/* ****************************************************************************************** */
// Function to set focus on email field if erroneous e-mail is entered. 
function showFocus(msg,field,canBeSelected) {
	alert(msg);
	field.focus();
	if (canBeSelected) {
		field.select();
	}
}  

// END VALIDATION FUNCTIONS
