// A utility function that returns true if a string contains only
// whitespace characters
function isblank(s)
{
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != '') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function open_Policy() {
	//Function to Bring up the Private Policy Window
	var winl = (screen.width - 650) / 2;
	var wint = (screen.height - 450) / 2;
	var props = 'scrollBars=yes,resizable=yes,toolbar=no,menubar=no,location=no,directories=no,width=600,height=500,'+'left='+winl+',top='+wint;
	helpWindow = window.open("http://www.stewleonardsgifts.com/privacy.htm", "Help_Scr", props);
}


function open_Shipping() {
	//Function to Bring up the Private Policy Window
	var winl = (screen.width - 500) / 2;
	var wint = (screen.height - 400) / 2;
	var props = 'scrollBars=yes,resizable=yes,toolbar=no,menubar=no,location=no,directories=no,width=600,height=500,'+'left='+winl+',top='+wint;
	shipWindow = window.open("http://www.stewleonardsgifts.com/shipping.htm", "Ship_Scr", props);
}

function open_Delivery() {
	//Function to Bring up the Private Policy Window
	var winl = (screen.width - 500) / 2;
	var wint = (screen.height - 400) / 2;
	var props = 'scrollBars=yes,resizable=yes,toolbar=no,menubar=no,location=no,directories=no,width=600,height=500,'+'left='+winl+',top='+wint;
	delvWindow = window.open("http://www.stewleonardsgifts.com/delivery_dates.htm", "Delv_Scr", props);
}


function open_Help() {
	//Function to Bring up the Private Policy Window
	var winl = (screen.width - 650) / 2;
	var wint = (screen.height - 450) / 2;
	var props = 'scrollBars=yes,resizable=yes,toolbar=no,menubar=no,location=no,directories=no,width=600,height=500,'+'left='+winl+',top='+wint;
	helpWindow = window.open("http://www.stewleonardsgifts.com/help.htm", "Help_Scr", props);
}


function verify_login(f)
/* ======================================================================
FUNCTION: 	verify_login	

INPUT:		f - Reference for the Html Form	
				
RETURNS:	True/False	

DESC:		Client Side Form Validation before we Submit the Form.
			Used when logging into the Gift Center Web Site					
====================================================================== */
{
	//First Check for a User Name
	if(f.username.value == '')
	{
		alert('Please Enter your User Name');
		f.username.focus();
		return false;
	}
	// Next Check for a P A S S 
	if(f.pass.value == '')
	{
		alert('Please Enter your Password');
		f.pass.focus();
		return false;
	}

}



function verify_add_2_cart(f)
/* ======================================================================
FUNCTION: 	verify_login	

INPUT:		f - Reference for the Html Form	
				
RETURNS:	True/False	

DESC:		Client Side Form Validation before we Submit the Form.
			Used when adding an item to a shopping cart					
====================================================================== */
{

	//If the user selected to add a name from the drop-down make
	//sure that they entered a name
	//alert(f.cmbSend.options[f.cmbSend.selectedIndex].value) Value of the Selected Option
	
	
	switch(f.cmbSend.options[f.cmbSend.selectedIndex].value)	{
		case "-1":
			//MySelf as the Recipent do nothing
			return true;	//Should be True
			break;
		case "0":
			//Other, Add Name
			if(f.add_name.value =='')
			{
				alert('Please enter a Recipent name.')
				f.add_name.focus();
				return false;
			}
			if(((f.message.value == null) || (f.message.value == "") || isblank(f.message.value))
				&& ((f.from.value == null) || (f.from.value == "") || isblank(f.from.value)))
			{
				alert('Please Enter a Gift Message and Gift From Name!')
				f.message.focus();
				return false;
			}
			break;
		default:
			//Make Sure that they Entered a Gift Message and From Name
			if(((f.message.value == null) || (f.message.value == "") || isblank(f.message.value))&&((f.from.value == null) || (f.from.value == "") || isblank(f.from.value)))
			{
				alert('Please Enter a Gift Message and Gift From Name!')
				f.message.focus();
				return false;
			}
			break;
	}

}

// function to verify that the user entered all of the required 
// account profile fields
function verify_account_profile(f)
{
	var msg;
	var empty_fields = "";
	var str_temp = "";
	var errors = "";
	var digits = "0123456789";
	var temp;
	var str_Card = "";
	var str_Number = "";
	var iCardLength = 0;
	
	// Loop thru all of the text elements of the form, looking for all
	// text elements that don't have an "optional" property.
	for(var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		if (((e.type == "text") || (e.type == "textarea") || (e.type == "password")) && !e.optional) {
			// first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
				str_temp = e.name;
				empty_fields += "\n          " + str_temp.replace("_"," ");
				continue;
			}
		}
	}
	
	//Check for Drop-Down Values for States
	if(f.Billing_State.selectedIndex==0) {
		str_temp = f.Billing_State.name;
		empty_fields += "\n          " + str_temp.replace("_"," ");
	}
	if(f.Shipping_State.selectedIndex==0) {
		str_temp = f.Shipping_State.name;
		empty_fields += "\n          " + str_temp.replace("_"," ");
	}	
	

	//Lastly Make sure that the Password ia at least four characters and that they match with Confirm
	str_temp = f.Password.value + "";
	if(str_temp.length < 4) {
		errors += " the Password must be at least 4 characters long!";
		errors += "\n";
	
	}
	
	if (f.Password.value != f.Confirm_Password.value) {
		errors += " the Password does not match the Confirm Password!";
		errors += "\n";
	}
	
	//Check if they entered in CC Number than get the Remaing CC Info
	if ((f.Card_Number.value != null) || (f.Card_Number.value != ""))
	{
		// If they enter a CC Number than they Must enter in all of the Other Info
		if ((f.Card_Month.selectedIndex==0) || (f.Card_Year.selectedIndex==0))
		{
			str_temp = "Credit Card Expiration Date";
			empty_fields += "\n          " + str_temp;
		}
		if ((f.Card_Type.selectedIndex==0) || (f.Card_Type.selectedIndex==0))
		{
			str_temp = "Credit Card Type";
			empty_fields += "\n          " + str_temp;
		}
		if ((f.Card_Name.value == null) || (f.Card_Name.value == "")|| isblank(f.Card_Name.value))
		{
			str_temp = "Credit Card Name";
			empty_fields += "\n          " + str_temp;
		}
		//Add the Code to validate the CC Number based on the type of Card
		//First check only for numbers
		for (var k=0;k<f.Card_Number.value.length;k++){
			temp=f.Card_Number.value.substring(k,k+1);
			if(digits.indexOf(temp)==-1){
				str_temp = "Invalid Credit Card Number!\n          Please enter in only Numbers.";
				empty_fields += "\n          " + str_temp;
				break;
			}
		}
		// Now use a switch case to determeine the proper lengths of the card
		// at this point we can assume that the card entered contains on numbers
		str_Card = f.Card_Type.options[f.Card_Type.selectedIndex].value;	
		str_Number = f.Card_Number.value;
		iCardLength = str_Number.length;
		switch(str_Card){
			case "Visa Card":
				if ( 	(iCardLength==16) &&
						(str_Number.substr(0,1)=="4") ) {
						//do nothing
						}
					else if (	(iCardLength==13) &&
								(str_Number.substr(0,1)=="4") ) {
						 //do nothing
						}
					else
					{	
					str_temp = "The Visa Card you entered is not valid!";
					empty_fields += "\n          " + str_temp;	
					}		
				break;
				
			case "Master Card":
				if((str_Number.length!=16)||(str_Number.substr(0,1)!="5")){
					str_temp = "The MasterCard you entered is not valid!";
					empty_fields += "\n          " + str_temp;
				}
				break;

			case "AmEx Card":
				//alert(iCardLength);
				//alert(str_Number.substr(0,2));
				if ( 	(iCardLength==15) &&
						(str_Number.substr(0,2)=="34") ) {
						//do nothing
						}
					else if (	(iCardLength==15) &&
								(str_Number.substr(0,2)=="37") ) {
						 //do nothing
						}
					else
					{	
					str_temp = "The American Express Card you entered is not valid!";
					empty_fields += "\n          " + str_temp;	
					}				
				break;
				
			case "Diners Club":
				alert(iCardLength);
				alert(str_Number.substr(0,2));
				if ( 	(iCardLength==14) &&
						(str_Number.substr(0,2)=="30") ) {
						//do nothing
						}
					else if (	(iCardLength==14) &&
								(str_Number.substr(0,2)=="36") ) {
						 //do nothing
						}
					else if (	(iCardLength==14) &&
								(str_Number.substr(0,2)=="38") ) {
						 //do nothing
						}	
					else
					{	
					str_temp = "The Diners Club Card you entered is not valid!";
					empty_fields += "\n          " + str_temp;	
					}
			
			/*
				if(((str_Number.substr(0,2)!="30")||(str_Number.substr(0,2)!="36")||(str_Number.substr(0,2)!="38"))&&(str_Number.length!=14)){
					str_temp = "The Diners Card you entered is not valid!";
					empty_fields += "\n          " + str_temp;
				}
				
				*/
				
				break;
				
			case "Discover Card":
				if((str_Number.length!=16)||(str_Number.substr(0,4)!="6011")){
					str_temp = "The Discover Card you entered is not valid!";
					empty_fields += "\n          " + str_temp;
				}
				break;
		}
		
		
	}
	
	// Now if their were any errors display them
	if(!empty_fields && !errors) return true;
	
	msg  = "______________________________________________\n\n";
	msg += "The form was not submitted because of the following error(s).\n";
	msg += "Please correct these error(s) and re-submit.\n";
	msg += "_______________________________________________\n\n";
	
	if (empty_fields) {
		msg += "- The following required field(s) are empty:\n";
		msg += empty_fields + "\n";
	}
	if (errors) {
		msg += "- Their was a problem with your Password:\n";
		msg += errors + "\n";
	}
	alert(msg);
	return false;
}

// Function to copy the Billing Address to the Shipping Address
function copy_billing_address()
{
	if(document.frmProfile.chkCopy_Addr.checked) {
		document.frmProfile.Shipping_Address.value = document.frmProfile.Billing_Address.value;
		document.frmProfile.Shipping_Address2.value = document.frmProfile.Billing_Address2.value;
		document.frmProfile.Shipping_City.value = document.frmProfile.Billing_City.value;
		document.frmProfile.Shipping_ZipCode.value = document.frmProfile.Billing_ZipCode.value;
		document.frmProfile.Shipping_Phone.value = document.frmProfile.Billing_Phone.value;
		document.frmProfile.Shipping_Company.value = document.frmProfile.Billing_Company.value;
		// Finnally copy over the States
		document.frmProfile.Shipping_State.selectedIndex = document.frmProfile.Billing_State.selectedIndex;
	}



}




function verify_address_profile(f)
// Function to verify the address data was entered
{
	var msg;
	var empty_fields = "";
	var str_temp = "";
	var errors = "";
	
	// Loop thru all of the text elements of the form, looking for all
	// text elements that don't have an "optional" property.
	for(var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		if (((e.type == "text") || (e.type == "textarea") || (e.type == "password")) && !e.optional) {
			// first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
				str_temp = e.name;
				empty_fields += "\n          " + str_temp.replace("_"," ");
				continue;
			}
		}
	}
	
	if(f.Shipping_State.selectedIndex==0) {
		str_temp = f.Shipping_State.name;
		empty_fields += "\n          " + str_temp.replace("_"," ");
	}
	
	// Now if their were any errors display them
	if(!empty_fields && !errors) return true;
	
	msg  = "______________________________________________\n\n";
	msg += "The form was not submitted because of the following error(s).\n";
	msg += "Please correct these error(s) and re-submit.\n";
	msg += "_______________________________________________\n\n";
	
	if (empty_fields) {
		msg += "- The following required field(s) are empty:\n";
		msg += empty_fields + "\n";
	}
	alert(msg);
	return false;
	
}






function validateAddress(incoming) {
//example: validateAddress(document.enter.email.value);
	var emailstring = incoming;
	var ampIndex = emailstring.indexOf("@");
	var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
		// find a dot in the portion of the string after the ampersand only
	var dotIndex = afterAmp.indexOf(".");
		// determine dot position in entire string (not just after amp portion)
	dotIndex = dotIndex + ampIndex + 1;
		// afterAmp will be portion of string from ampersand to dot
	afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
		// afterDot will be portion of string from dot to end of string
	var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
	var beforeAmp = emailstring.substring(0,(ampIndex));
	var email_regex = "/^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/";
		// index of -1 means "not found"
	if ((emailstring.indexOf("@") != "-1") &&
		(emailstring.length > 5) &&
		(afterAmp.length > 0) &&
		(beforeAmp.length > 1) &&
		(afterDot.length > 1) &&
		(email_regex.test(emailstring)) ) {
		  return true;
	} else {
			//alert("Please check your email address!");
			return false;
	}
}


function IsNumeric (str) {
	r = new RegExp("[^0-9]");
	return (!r.test(str));
}

function IsEmpty (str) {
	var strObj = new String(str);
	r = /(\s+)/g;
	strObj = strObj.replace(r, "");
	if (strObj.length == 0)
		return true;
	else
		return false;
}

function IsEmail (str) {
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported)
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}

// for US only (999)999-9999
function IsUSPhoneNumber (str) {
	if (str == "N/A")
		return true;
	var r = new RegExp("[0-9]{3}-[0-9]{3}-[0-9]{4}"),
	r2 = new RegExp("[0-9]{3} [0-9]{3} [0-9]{4}"),
	r3 = new RegExp("[0-9]{3} [0-9]{7}"),
	r4 = new RegExp("[0-9]{10}"),
	r5 = new RegExp("[0-9]{3}-[0-9]{7}");
	return r.test(str) || r2.test(str) || r3.test(str) || r4.test(str) || r5.test(str);
}

function verifyEmail(form) {
checkEmail = form.email.value

if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))) 
{alert("You have entered an invalid email address. Please try again.");
form.email.select();
return false;
} 

else {
form.method="get";
form.target="_self";
form.action="myscript.cgi";
form.submit();
}

}
