function validateEmail(theAddress) {
	var retValue = true;
	var AtSym       = theAddress.indexOf('@');
	var Period      = theAddress.lastIndexOf('.');
	var Space       = theAddress.indexOf(' ');
	var Length      = theAddress.length - 1;  // Array is from 0 to length-1

	// '@' cannot be in first position, Must be at least one valid char btwn '@' and '.'
	// Must be at least one valid char after '.', No empty spaces permitted
	if((AtSym < 1) || (Period <= AtSym+1) || (Period == Length ) || (Space  != -1))
		retValue = false;
	return retValue;
}

var remote;
function launchWin(helpURL, size) {
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remote = window.open(helpURL, "AIC", size+",scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	remote.focus();
}

function launchPersWin(helpURL) {
	var w = 750;
	var h = 420;
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remote = window.open(helpURL, "AIC", "width=" + w + ",height=" + h + ",scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	remote.focus();
	return false;
}

function launchNoScrollWin(helpURL, size) {
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	var theWin = window.open(helpURL, "AIC", size+",scrollbars=0,resizable=1,left="+yPos+",top="+xPos);
	theWin.focus();
	return false;
}

var remotePrint;
function launchPrintWin(helpURL, size) {
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remotePrint = window.open(helpURL, "AIC", size+",toolbar=1,scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	remotePrint.focus();
}

function ltrim(s) {
	return s.replace(/^\s*/, "");
}

function rtrim(s) {
	return s.replace(/\s*$/, "");
}

// Combine the rtrim() and ltrim() functions to make the trim() function
function trimString(s){
	return rtrim(ltrim(s));
}

function validateEmailSignup(theForm) {
	var errMsg = "";
	var retVal = true;
	
	if(theForm.email.value == "" || theForm.email.value == "Your e-mail address") {
		errMsg += "Please enter your email address.\n";
		retVal = false;
	}
	else if(!validateEmail(theForm.email.value)) {
		errMsg += "Please enter a valid email address\n";
		retVal = false;
	}
	
	if(!retVal)
		alert(errMsg);
	return retVal;
}

var bFirst = true;
function clearDefault(theField) {
	if(bFirst) {
		theField.value = "";
		bFirst = false;
	}
}

function validateSku(theForm){
	var errMsg = "The product number you entered is not valid.\nPlease try again.";
	theForm.sku.value = trimString(theForm.sku.value);
	if(theForm.sku.value.length < 4 || theForm.sku.value == "Product #" || theForm.sku.value.length > 10 || containsMalCode(theForm.sku.value)) {
		alert(errMsg);
		return false;
	}
	return true;
}

function validateSearch(theForm) {
	if (theForm.searchFor.value.length < 2) 	{
		alert("Please enter at least 2 characters.");
		return false;
	}
	else if(containsMalCode(theForm.searchFor.value)) {
		alert("Please enter a valid search term.");
		return false;
	}
	return true;
}

/* Shopping tote functions */
function validateUpdate(theForm) {
	var retVal = true;
	var errMsg, qty, isPersonalized;
	
	// make sure there is a min of 4 for personalized cards
	if(theForm.basketItemID.length) {
		for(i=0; i<theForm.basketItemID.length; i++) {
			isPersonalized = eval("theForm.isPersonalized" + theForm.basketItemID[i].value + ".value");
			qty = parseInt(eval("theForm.qty" + theForm.basketItemID[i].value + ".value"));
			
			if(isPersonalized == "True" && qty<5 && qty>0){
				retVal = false;
				errMsg = "There is a minimum of five boxes for each image on personalized cards.";
			}
		}
	}
	else {	// there's only one item in the basket, can't use .length
		isPersonalized = eval("theForm.isPersonalized" + theForm.basketItemID.value + ".value");
		qty = parseInt(eval("theForm.qty" + theForm.basketItemID.value + ".value"));
		
		if(isPersonalized == "True" && qty<5 && qty>0) {
			retVal = false;
			errMsg = "There is a minimum of five boxes for each image on personalized cards.";
		}
	}
	
	for(i=0; i<theForm.elements.length; i++)
		if(theForm.elements[i].name.indexOf("qty") != -1 && isNaN(theForm.elements[i].value)) {
			retVal = false;
			errMsg = "Please enter a valid number for quantity field(s)";
			break;
		}
	
	if(!retVal)
		alert(errMsg);
	return retVal;
}

function reassignRedirect(redirectURL, theForm) {
	theForm.redirectURL.value = redirectURL;
	if(validateUpdate(theForm))
		theForm.submit();
}

// Malicious code mitigation
function checkForMalCode(theForm) {
	var retBool = true;
	for(i=0; i<theForm.elements.length; i++)
		if(theForm.elements[i].type == "textarea" || theForm.elements[i].type == "text" || theForm.elements[i].type == "password")
			if(containsMalCode(theForm.elements[i].value)) {
				retBool = false;
				break;
			}
	return retBool;
}
function containsMalCode(inputStr) {
	var scriptTag 	= new RegExp("<scr"+"ipt", "i");
	var embedTag 	= new RegExp("<embed", "i");
	var objectTag 	= new RegExp("<object", "i");
	var appletTag 	= new RegExp("<applet", "i");
	var formTag 	= new RegExp("<form", "i");
	var anchorTag 	= new RegExp("<a ", "i");
	
	if(inputStr.search(scriptTag) != -1)
		return true;
	else if(inputStr.search(embedTag) != -1)
		return true;
	else if(inputStr.search(objectTag) != -1)
		return true;
	else if(inputStr.search(appletTag) != -1)
		return true;
	else if(inputStr.search(formTag) != -1)
		return true;
	else if(inputStr.search(anchorTag) != -1)
		return true;
	else
		return false;
}
