// JavaScript Document

<!--
//Code for displaying input errors above the form

	var W3CDOM = (document.createElement && document.getElementsByTagName && document.createTextNode);   //Check browser's DOM support 
	var isFormValid;
	var errMsg;
	var errList;
	var arrErrLabels;

	//replace email node based on country
	function eNode(form)
	{
		var country = form.country_origin.value;
		// Americas
		if ( country == "US" || country == "CA" || country == "SV" || country == "GT" || country == "HN" || country == "MX" 
		|| country == "NI" || country == "AR" || country == "BO" || country == "BR" || country == "CL" || country == "CO" 
		|| country == "EC" || country == "GY" || country == "PY" || country == "PE" || country == "UY" || country == "VE" 
		|| country == "BS" || country == "DO" || country == "HT" || country == "TT" )
 		{
 			form.send_to.value = 1099;
  		}
		
		//EMEA
		if ( country == "BI" || country == "CM" || country == "CF" || country == "TD" || country == "CG" || country == "CD" 
		|| country == "GQ" || country == "RW" || country == "ER" || country == "ET" || country == "KE" || country == "SO" 
		|| country == "TZ" || country == "UG" || country == "BF" || country == "CI" || country == "GH" || country == "GN" 
		|| country == "GW" || country == "LR" || country == "MR" || country == "NE" || country == "NG" || country == "SN" 
		|| country == "SL" || country == "TG" || country == "CZ" || country == "RO" || country == "EE" || country == "LV" 
		|| country == "LT" || country == "AT" || country == "BE" || country == "FI" || country == "FR" || country == "DE" 
		|| country == "IE" || country == "IT" || country == "PT" || country == "ES" || country == "SE" || country == "CH" 
		|| country == "GB" || country == "AM" || country == "AZ" || country == "BY" || country == "GE" || country == "KZ" 
		|| country == "KG" || country == "MD" || country == "RU" || country == "TJ" || country == "TM" || country == "UA" 
		|| country == "UZ" || country == "AL" || country == "BA" || country == "BG" || country == "HR" || country == "CY" 
		|| country == "GR" || country == "MK" || country == "MT" || country == "SI" || country == "TR" || country == "BH" 
		|| country == "IL" || country == "JO" || country == "KW" || country == "LB" || country == "SA" || country == "AE" 
		|| country == "YE" || country == "DZ" || country == "EG" || country == "MA" || country == "TN" || country == "NL" 
		|| country == "AN" || country == "NO")
 		{
 			form.send_to.value = 1097;
  		}
		// South Africa
		if ( country == "AO" || country == "MW" || country == "MZ" || country == "NA" || country == "ZA" || country == "SZ"
		|| country == "ZM" || country == "ZW" )
 		{
 			form.send_to.value = 1101;
  		}

		//APJ-North  
		if ( country == "CN" || country == "JP" || country == "MN" || country == "KR" || country == "TW" || country == "BN" 
		|| country == "KH" || country == "AF" || country == "BD" || country == "BT" || country == "MV" || country == "NP" 
		|| country == "PK" || country == "LK" || country == "FJ" || country == "PG" || country == "SB"  )
 		{
 			form.send_to.value = 1100;

  		}
		
		//APJ-Southeast Asia  
		if ( country == "ID" || country == "MY" || country == "PH" || country == "SG"
		|| country == "TH" || country == "VN" || country == "IN" || country == "AU" || country == "NZ"  )
 		{
 			form.send_to.value = 1098;
		}

		
		//Poland
		if ( country == "PL" )
 		{
 			form.send_to.value = 1096;
  		}
		
		//Hungary
		if ( country == "HU" )
 		{
 			form.send_to.value = 1163;
  		}
		
		//Slovakia
		if ( country == "SK" )
 		{
 			form.send_to.value = 1095;
  		}
		
	}
	
	// Jump to top of page on error
	
	// Redirect on slecting 'Yes'
	function reDir(oInput)
	{
		switch (oInput)
		{
			case 1 :
			window.location = "redhat_reg_error.html";
			break;
		}
	}
	
	
	// Check whether a string is empty
	function IsEmpty(s)
	{
		return( (s == null) || (s.length == 0) )
	}

	// Check email address validity
	function IsEmailAddr(email)
	{
		var check = /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/;

		if ( check.test(email) )
			return true;
		else
			return false;
	}

	// Check invalid characters in user input
	function IsInvalidInput(badChars, userInput)
	{
		var re = new RegExp(badChars);

		if ( re.test(userInput) )
			return true;
		else
			return false;
	}
	

	// Validate the form values
	function FormValidator(theForm)
	{
		isFormValid = true;
		errMsg = "";

		HidePreviousErrors();
		
		// START WHITE SPACE CHECK - Added 20060206		
		var temp_first_name = theForm.first_name.value;
		var temp_last_name = theForm.last_name.value;
		temp_first_name = temp_first_name.replace(new RegExp(/^\s+/),"");
		temp_last_name = temp_last_name.replace(new RegExp(/^\s+/),"");
		// END WHITE SPACE CHECK
		
		//check for valid date
		var right_now = new Date();
		var the_year = right_now.getFullYear();
		var the_month = right_now.getMonth()+ 1;
		var the_day = right_now.getDate();
		
		
		if ( IsEmpty(theForm.OPT_purchase_direct.value) )
		{
			AddError(theForm.OPT_purchase_direct, "Did you purchase your Software directly from HP?");
		}
		//Month check
		if ( IsEmpty(theForm.OPT_month.value) )
		{
			AddError(theForm.OPT_month, "Month is required");
		}
		
		if (theForm.OPT_year.value == the_year && theForm.OPT_month.value > the_month )
		{
			AddError(theForm.OPT_month, "Purchase month is not valid.  Month must be current or previous month");
		}
		
		//Day check
		if ( IsEmpty(theForm.OPT_day.value) )
		{
			AddError(theForm.OPT_day, "Day is required");
		}
		
		if (theForm.OPT_month.value == the_month && theForm.OPT_day.value > the_day && theForm.OPT_year.value == the_year )
		{
			AddError(theForm.OPT_day, "Purchase date must be on or before today's date");
		}  
			
		
		//Year check
		if ( IsEmpty(theForm.OPT_year.value) )
		{
			AddError(theForm.OPT_year, "Year is required");
		}
		
		if (theForm.OPT_year.value > the_year )
		{
			AddError(theForm.OPT_year, "Purchase year is not valid. Year must be current");
		}  
		
		//correct date selection check 
		if ( theForm.OPT_month.value == 2 && theForm.OPT_day.value == 29 && theForm.OPT_year.value < 2008 )
		{
			AddError(theForm.OPT_year, "Purchase day is not valid for selected month in selected year");
		}  
		
		if (theForm.OPT_day.value > 29 && theForm.OPT_month.value == 2 )
		{
			AddError(theForm.OPT_year, "Purchase day is not valid for selected month");
		}  
		
		if ( theForm.OPT_day.value > 30 && theForm.OPT_month.value == 4 )
		{
			AddError(theForm.OPT_year, "Purchase day is not valid for selected month");
		}  
		
		if ( theForm.OPT_day.value > 30 && theForm.OPT_month.value == 6 )
		{
			AddError(theForm.OPT_year, "Purchase day is not valid for selected month");
		}  
		
		if ( theForm.OPT_day.value > 30 && theForm.OPT_month.value == 9 )
		{
			AddError(theForm.OPT_year, "Purchase day is not valid for selected month");
		}  
		
		if ( theForm.OPT_day.value > 30 && theForm.OPT_month.value == 11 )
		{
			AddError(theForm.OPT_year, "Purchase day is not valid for selected month");
		}  
		
	
		if (theForm.OPT_purchase_direct.value == "Yes" )
		{
			AddError(theForm.OPT_purchase_direct, "If you purchased directly from HP, you do not need to register for support, this will be done automatically for you.");
		}
			
	// Check for product quantity
	if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,()^-_+=?><{}\"':;]", theForm.OPT_578055_B21.value) )
		{
			 AddError(theForm.OPT_578055_B21, "Product quantity can only be a number");
		}

if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,()^-_+=?><{}\"':;]", theForm.OPT_578056_B21.value) )
		{
			 AddError(theForm.OPT_578056_B21, "Product quantity can only be a number");
		}

if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,()^-_+=?><{}\"':;]", theForm.OPT_578057_B21.value) )
		{
			 AddError(theForm.OPT_578057_B21, "Product quantity can only be a number");
		}

if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,()^-_+=?><{}\"':;]", theForm.OPT_578058_B21.value) )
		{
			 AddError(theForm.OPT_578058_B21, "Product quantity can only be a number");
		}

if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,()^-_+=?><{}\"':;]", theForm.OPT_578059_B21.value) )
		{
			 AddError(theForm.OPT_578059_B21, "Product quantity can only be a number");
		}

if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,()^-_+=?><{}\"':;]", theForm.OPT_578060_B21.value) )
		{
			 AddError(theForm.OPT_578060_B21, "Product quantity can only be a number");
		}

if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,()^-_+=?><{}\"':;]", theForm.OPT_578061_B21.value) )
		{
			 AddError(theForm.OPT_578061_B21, "Product quantity can only be a number");
		}

if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,()^-_+=?><{}\"':;]", theForm.OPT_578062_B21.value) )
		{
			 AddError(theForm.OPT_578062_B21, "Product quantity can only be a number");
		}



		if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,()^-_+=?><{}\"':;]", theForm.OPT_445209_B21.value) )
		{
			 AddError(theForm.OPT_445209_B21, "Product quantity can only be a number");
		}
		if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,(^-_+=?><{}\"':;]", theForm.OPT_445210_B21.value) )
		{
			 AddError(theForm.OPT_445210_B21, "Product quantity can only be a number");
		}
		if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,(^-_+=?><{}\"':;]", theForm.OPT_445211_B21.value) )
		{
			 AddError(theForm.OPT_445211_B21, "Product quantity can only be a number");
		}
		if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,(^-_+=?><{}\"':;]", theForm.OPT_445212_B21.value) )
		{
			 AddError(theForm.OPT_445212_B21, "Product quantity can only be a number");
		}
		if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,(^-_+=?><{}\"':;]", theForm.OPT_445213_B21.value) )
		{
			 AddError(theForm.OPT_445213_B21, "Product quantity can only be a number");
		}
		if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,(^-_+=?><{}\"':;]", theForm.OPT_445214_B21.value) )
		{
			 AddError(theForm.OPT_445214_B21, "Product quantity can only be a number");
		}
		if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,(^-_+=?><{}\"':;]", theForm.OPT_445215_B21.value) )
		{
			 AddError(theForm.OPT_445215_B21, "Product quantity can only be a number");
		}
		if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\.#!@$%&,(^-_+=?><{}\"':;]", theForm.OPT_445216_B21.value) )
		{
			 AddError(theForm.OPT_445216_B21, "Product quantity can only be a number");
		}
		
		
		if (theForm.OPT_578055_B21.value == "" && theForm.OPT_578056_B21.value == "" && theForm.OPT_578057_B21.value == "" && theForm.OPT_578058_B21.value == "" && theForm.OPT_578059_B21.value == "" && theForm.OPT_578060_B21.value == "" && theForm.OPT_578061_B21.value == "" && theForm.OPT_578062_B21.value == "" && theForm.OPT_445209_B21.value == "" && theForm.OPT_445210_B21.value == "" && theForm.OPT_445211_B21.value == "" && theForm.OPT_445212_B21.value == "" && theForm.OPT_445213_B21.value == "" && theForm.OPT_445214_B21.value == "" && theForm.OPT_445215_B21.value == "" && theForm.OPT_445216_B21.value == "" )
		{
			AddError(theForm.OPT_578055_B21, "Please complete the quantity field for at least one product");
		}
		
		
		if ( IsEmpty(temp_first_name) )
		{
			AddError(theForm.first_name, "First name is required");
		}
		else if ( IsInvalidInput("[~!#`@\"&\r\n\^\|<>%&\\\\]", temp_first_name) )
		{
			AddError(theForm.first_name, "First name is not valid");
		}

		if ( IsEmpty(temp_last_name) )
		{
			AddError(theForm.last_name, "Last name is required");
		}
		else if ( IsInvalidInput("[~!#`@\"&\r\n\^\|<>%&\\\\]", temp_last_name) )
		{
			AddError(theForm.last_name, "Last name is not valid");
		}

		// if phone exist check if is valid
		if ( IsEmpty(theForm.OPT_phone_number.value) )
		{
			AddError(theForm.OPT_phone_number, "Phone number is required");
		}
		else if ( IsInvalidInput("[abcdefghijklmnopqrstuvwxyz~`*|\]", theForm.OPT_phone_number.value) )
		{
			  AddError(theForm.OPT_phone_number, "Phone number contains invalid characters");
		}
		
		if ( IsEmpty(theForm.email_address.value) )
		{
			AddError(theForm.email_address, "Email address is required");
		}
		else if ( !IsEmailAddr(theForm.email_address.value) )
		{
			AddError(theForm.email_address, "Email address is not valid");
		}
		
		if ( IsEmpty(theForm.OPT_email_address_confirm.value) )
		{
			AddError(theForm.OPT_email_address_confirm, "Confirming the Email address is required");
		}
		else if ( !IsEmailAddr(theForm.OPT_email_address_confirm.value) )
		{
			AddError(theForm.OPT_email_address_confirm, "Confirmed Email address is not valid");
		}
		
		// Check for confirmed email address
		if (theForm.email_address.value != theForm.OPT_email_address_confirm.value)
		{
			AddError(theForm.OPT_email_address_confirm, "Email address entered does not match");
		}
				
		if ( IsEmpty(theForm.OPT_company.value) )
		{
			AddError(theForm.OPT_company, "Company Name is required");
		}
		
		if ( IsEmpty(theForm.OPT_address.value) )
		{
			AddError(theForm.OPT_address, "Equipment Location Street Address is required");
		}
		
		if ( IsEmpty(theForm.OPT_city.value) )
		{
			AddError(theForm.OPT_city, "Equipment Location City is required");
		}
		
		if ( IsEmpty(theForm.OPT_zip_code.value) )
		{
			AddError(theForm.OPT_zip_code, "Zip/Postal Code is required");
		}
		
		if ( IsEmpty(theForm.country_origin.value) )
		{
			AddError(theForm.country_origin, "Country or Region is required");
		}
		
		if (isFormValid)
		{
			return true;
		}
		else window.location = String(window.location).replace(/\#.*$/, "") + "#top";
		{
			if (!W3CDOM)
			{
				errMsg = "Please correct the following error(s): \n" + errMsg;
				alert( errMsg );   // Display javascript popup only if the browser does not support W3C DOM
			}
			else
			{
				var errDiv = document.getElementById('err_display');
				var errSpan = document.getElementById('err_fields');

				if ( errSpan.hasChildNodes() )
				{
					errSpan.replaceChild( errList, errSpan.firstChild );   // We have already displayed some errors so replace the old ones
				}
				else
				{
					errSpan.appendChild( errList);   // This is the first time form was submitted
					errSpan.appendChild( document.createElement("br") );   // Add some extra spacing
				}

				errDiv.style.visibility = 'visible';
			}
			return false;
		}
	}

	// DHTML code to add bulleted list of errors
	function AddError(theObj, message)
	{
		if (!W3CDOM)
		{
			errMsg += " - " + message + "\n";   // For non-supporting browsers
		}
		else
		{
			if (isFormValid)
			{
				errList = document.createElement("ul");   // Create a UL tag only once per form submit
				errList.className = 'bulletlist';
			}
			
			var item = document.createElement("li");
			item.appendChild( document.createTextNode(message) );
			errList.appendChild( item );

			var nodeTR = theObj.parentNode.parentNode;
			var nodeTD = nodeTR.getElementsByTagName("td")[1];   // Get the TD node that contains field label
			nodeTD.className = "error";

			arrErrLabels[arrErrLabels.length] = nodeTD;   // Store TD node in global array so that error class can be removed before the next pass. push() method of array object does not work in IE 5.
		}

		isFormValid = false;
	}

	// DHTML code to remove error class from field labels
	function HidePreviousErrors()
	{
		if (arrErrLabels)
		{
			var arrLen = arrErrLabels.length;
			for (var i=0; i<arrLen; i++)
			{
				arrErrLabels[i].className = "";
			}

			var errDiv = document.getElementById('err_display');
			errDiv.style.visibility = 'hidden';
		}

		arrErrLabels = null;
		arrErrLabels = new Array();
	}


//-->