/*
	This is called from the orderform page
	It copies the billing information to the shipping information
	7.17.01
*/
var shipfirstname = "";
var shiplastname = "";
var shipaddress = "";
var shipaddress2 = "";
var shipcity = "";
var shipstate = "";
// var ShipStateIndex = 0;
var shipzip = "";
var shipphone = "";
function InitSaveVariables(form) {
	shipfirstname = form.shipfirstname.value;
	shiplastname = form.shiplastname.value;
	shipaddress = form.shipaddress.value;
	shipaddress2 = form.shipaddress2.value;
	shipcity = form.shipcity.value;
	shipstate = form.shipstate.value;
	shipzip = form.shipzip.value;
	shipphone = form.shipphone.value;
	}
function ShipToBillPerson(form) {
		InitSaveVariables(form);
		form.shipfirstname.value = form.requiredbillfirstname.value;
		form.shiplastname.value = form.requiredbilllastname.value;
		form.shipaddress.value = form.requiredbilladdress.value;
		form.shipaddress2.value = form.billaddress2.value;
		form.shipcity.value = form.requiredbillcity.value;
		form.shipstate.value = form.requiredbillstate.value;
		form.shipzip.value = form.requiredbillzip.value;
		form.shipphone.value = form.requiredbillphone.value;
	}
// End of the copy billing to shipping function
// Function to validate order form
// Called from orderform.cfm
// 7.18.01
	function checkrequired(which) {
	var pass=true;
	if (document.images) {
	for (i=0;i<which.length;i++) {
	var tempobj=which.elements[i];
	if (tempobj.name.substring(0,8)=="required") {
	if (((tempobj.type=="text"||tempobj.type=="textarea")&&
	tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
	tempobj.selectedIndex==0)) {
	pass=false;
	break;
	         }
	      }
	   }
	}
	if (!pass) {
	shortFieldName=tempobj.name.substring(8,30).toUpperCase();
	alert("Please make sure the "+shortFieldName+" field was properly completed.");
	return false;
	}
	else
	return true;
	}
// End of order form validation
// This function opens a new window for Verisign
// Called from cart.cfm, price.cfm, orderform.cfm 
//7.17.01
function popUp(url) 
{ 
	sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
	self.name = "mainWin";
}
// A function to validate zip code
// Called from cart.cfm
function validateZip(formname) {
		if (formname.zip.value == "" && formname.country.value=="US") {
			window.alert("Please enter your zip code.");
			return false;
		}
		else if (formname.country.value == "") {
			window.alert("Please enter your zip code or country.");
			return false;
		}
		/* else if (isNaN(zipform.zip.value) == true) {
            window.alert("Your zip code must be a number.");
			return false;
			} */
		return true;
		}
// A function for Warehouse Locator
// Called from index.cfm, warehouselocator.cfm 
function js4Process()
	{
	  var w=800;
	  var h=409;
	  var dx=(screen.availWidth-w)/2;
	  var dy=(screen.availHeight-h)/2;
	  var win=window.open("http://www.fastsite.com/cgi-bin/vic.pl?mode=1&csz="+escape(document.js4.csz.value)+"&user=radiator-warehouses&perpage=3&mpage=on","Results","width="+w+",height="+h+",screenX="+dx+",screenY="+dy+",left="+dx+",top="+dy+",resizable");
	  if (win.focus)
		{
			win.focus ();
		}
		  return false;
	}
// A generic popup window function.
// It's necessary to pass all of the available attributes; url, name of new window, window features
// 10.30.01
function newWindow(url, name, features)
	{
		popupWin = window.open(url, name, features);
		popupWin.focus();
		
		return false;
	}
	
	
// A popup window function for the delivery option.
// Called from guarantee.cfm, index.page
// 10.30.01
// Modified 04.29.02
function delWin(name, features, originwindow)
	{
		if (document.whlookup.zip.value == "")
		{
			alert("You must first enter a zip code.");
		}
		else 
		{
			var zip = document.whlookup.zip.value;
			var url = 'deliveryinfo.cfm?zip=' + zip;
		
			popupWin = window.open(url, name, features);
			if (popupWin.opener == null)
				popupWin.opener = window;
			popupWin.opener.name = originwindow;
			popupWin.focus();
		}
		return false;
	}
function changeFocus()
{
	window.blur();
}
// FORM VALIDATION FUNCTIONS
function validRequired(formField,fieldLabel) {
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a ' + fieldLabel);
		formField.focus();
		result = false;
	}
	
	return result;
}

function validPhone(formField, fieldLabel, required) {
	var result = true;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
	if (result) {
		var strng = formField.value;
		var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
		//strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped)) && stripped != '') {
		   alert(fieldLabel + ' contains illegal characters.');
		   formField.focus();
		   result = false;
		}
		// Then we count the length of the number. It should have exactly ten digits — any more or less, and we reject it.
		else if (!(stripped.length >= 10) && stripped != '') {
			alert(fieldLabel + ' is the wrong length. Make sure you included an area code.\n');
			formField.focus();
			result = false;
		}
	}
	return result;
}

function validEmail(formField, required, hidealert){
	if (required || formField.value.length > 0) {
		var testresults
		var str=formField.value
		var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(str))
		testresults=true
		else{
			if (!hidealert)
				alert("Please input your valid email address!")
				formField.focus();
		testresults=false
		}
		return (testresults)
	}
	else return true;
}

function validZip(formField, fieldLabel, required) {
	result = true;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
	var valid = "0123456789-";
	var hyphencount = 0;
	zip = formField.value;
	if (zip!='') {
		if (zip.length!=5 && zip.length!=10) {
			alert("Please enter your 5 digit or 5 digit+4 zip code.");
			formField.focus();
			return false;
		}
		for (var i=0; i < zip.length; i++) {
			temp = "" + zip.substring(i, i+1);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in your zip code.  Please try again.");
				formField.focus();
				return false;
			}
			if ((hyphencount > 1) || ((zip.length==10) && ""+zip.charAt(5)!="-")) {
				alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
				formField.focus();
				return false;
		   }
		}
	}
	return result;
}
// Validate State against javascript array of US States (pulled from db)
function validState(formField, fieldLabel, required) {
	var result = true;
	var strng = formField.value;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
	if (result && strng.length>0) {
		// Check against database of US States.  If it doesn't match, reject it.
		for (cnt=0; cnt < vstates.length; cnt++) {
			if (vstates[cnt].toUpperCase()==strng.toUpperCase()) {
				return true;
			}
		}
		// otherwise no match was found
		alert(fieldLabel + ' is not a valid US state.  Please enter the correct 2 digit abbreviation.\n');
		formField.focus();
		result = false;
	}
	return result;
}

function validCCExp(monthFormField, yearFormField) {
	var result = true;
	var ccmonth = monthFormField.value;
	var ccyear = yearFormField.value;
	if (ccyear < thisyear)
		result = false;
	else if (ccyear == thisyear && ccmonth < thismonth)
		result = false;
	if (result == false) {
		alert("Credit card expiration date has passed.  Please enter a valid expiration date.");
		monthFormField.focus();
	}
	return result;
}

// Javascript utility functions added by SS
/////////////////////////////

function zipinput(fe) 
{
	var code;
	var e = fe || window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code == 13)
	{
		gotochat(document.getElementById("chatzip").value);
	}
}	

function gotochat(zip)
{
	zip = trim(zip);
	
	if ((zip.length != 5) || (!is_numeric(zip)))
	{
		alert('Please enter a 5 digit zip code.');
	}
	else
	{
		document.getElementById("hiddenzip").value = zip;
		// alert(document.getElementById("hiddenzip").value);
		document.getElementById("hiddenzip").form.submit();
	}
}

function is_numeric(strString)
{
	//  check for valid numeric strings	
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
   return blnResult;
}

function trim(str)
{
	if ((str != null) && (str.length > 0))
	{
		return str.replace(/^\s*|\s*$/g,"");
	}

	return "";
}

