/*
IMPORTANT:
When making changes to this file, always minify and save as library-validation-catalog-min.1.x.js.
The "x" should be incremented. The merchant class must also be adjusted to access the correct file.

Example:
cd /usr/local/php/mc6-scott/js
jsmin <library-validation-catalog.js >library-validation-catalog-min.1.1.js

*/
/* Start Library */
/* Function to deal with external links for XHTML Strict Compliance */
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;


// general onload function(s)
// not used
function jsInit() {

	if (address.same_as_billing.defaultChecked) {
		toggleContent(document.address, 'shipping');
	}

}


// Error Handling
var error = new Array();
var field = new Array();

function pushErr(err,fld) {
	error[error.length] = err;
	field[field.length] = fld;
	return;
}

function clearErr(form) {

	for (i=0;i<form.elements.length;i++) {
		form.elements[i].style.backgroundColor='#ffffff';
	}

}

function showErr() {

	if (arguments.length > 0) str = arguments[0] + "\n";
	else str = '';

	if (arguments.length > 1) color = arguments[1];
	else color = '#ffaabb';

	if (error.length === 0) return true;

	for(i=0;i<error.length;i++) {
		str += error[i] + "\n";
	}
	alert(str);

	for(i=0;i<field.length;i++) {
		field[i].style.backgroundColor=color;
	}

	field[0].focus();
	error = new Array();
	field = new Array();
	return false;
}

function setMsg(msg) {
	window.status = msg;
	return true;
}

// Opens Thawte Certificate
function OpenCertDetails() {
	thewindow = window.open('https://www.thawte.com/cgi/server/certdetails.exe?code=USNEWE41-1', 'anew', config='height=400,width=450,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=yes');
}

// function that opens window for help
function newLocation(i) {
	newWindow = window.open(i,'newWin','toolbar=no,location=no,scrollbars=no,resizable=yes,width=250,height=300');
	newWindow.document.close();
	newWindow.focus();
}


function newWindow(i) {
	leftPos=0;
	topPos=0;
	if (screen) {
		leftPos = screen.width/2-150;
		topPos = screen.height/2-100;
	}

	editWindow = window.open(i,'editWin4','toolbar=no,location=no,scrollbars=no,resizable=yes,width=300,height=300,left='+leftPos+',top='+topPos+'');
	editWindow.document.close();
	editWindow.focus();
}


function editModule(i) {
	leftPos=0;
	topPos=0;
	if (screen) {
		leftPos = screen.width/2-255;
		topPos = screen.height/2-250;
	}

	editWindow = window.open(i,'editWin5','toolbar=no,location=no,scrollbars=yes,resizable=yes,width=510,height=500,left='+leftPos+',top='+topPos+'');
	editWindow.document.close();
	editWindow.focus();
}


function isEmpty(inputVal) {
	if (inputVal === undefined || inputVal === "") {
		return true;
	}
	return false;
}


function isPosInteger(inputVal) {
	if (isEmpty(inputVal)) {
		return false;
	}
	inputStr = inputVal.toString();
	var i;
	for (i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") {
			return false;
		}
	}
	return true;
}

function isInteger(inputVal) {
	if (isEmpty(inputVal)) {
		return false;
	}
	inputStr = inputVal.toString();
	var i;
	for (i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") {
			if (i===0 && oneChar==='-') {
				continue;
			}
			return false;
		}
	}
	return true;
}


function isNumber(inputVal) {
	if (isEmpty(inputVal)) {
		return false;
	}
	oneDecimal = false;
	inputStr = inputVal.toString();
	var i;
	for (i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);
		if (i === 0 && oneChar === "-") {
			continue;
		}
		if (oneChar === "." && !oneDecimal) {
			oneDecimal = true;
			continue;
		}
		if (oneChar < "0" || oneChar > "9") {
			return false;
		}
	}
	return true;
}


function inRange(inputStr,i,j) {
	if (!isInteger(inputStr)) {
		return false;
	}
	num = parseInt(inputStr,10);
	if (num < i || num > j) {
		return false;
	}
	return true;
}


function isChecked(field) {
	var i,j;
	j = -1;
	for (i = 0; i < field.length; i++) {
		if (field[i].checked) {
			j = i;
		}
	}
	if (j === -1) {
		return false;
	}
	return true;
}

function validPassword(pass, confirm, min_pw_len){
  
	if(pass=='' && confirm==''){
		return true;
	}
	if(pass=='' || confirm==''){ 
		alert('Please fill in both password fields.'); 
		return false; 
	} 
	if(pass!=confirm){ 
		alert('Passwords do not match. Please try again.'); 
		return false; 
	}
	if(!(/\d/.test(pass) && /[a-zA-Z]/.test(pass))){
		alert('Passwords must contain at least one letter and one number.');
		return false;
	}
	if(pass.length<min_pw_len){
		alert('Passwords must be at least '+min_pw_len+' characters long.');
		return false;
	}
	return true;
}


function validEmail(email,require) {
	if (arguments.length < 2) {
		require = true;
	}
	invalidChars = " /:,;";
	if (require === true && email === "") {
		return false;
	} else if (require === false && email === "") {
		return true;
	}
	for (i=0; i<invalidChars.length;i++) {
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) > -1) {
			return false;
		}
		atPos = email.indexOf("@",1);
		if (atPos === -1) {
			return false;
		}
	}
	if (email.indexOf("@",atPos+1) > -1) {
		return false;
	}
	periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) {
		return false;
	}
	if (periodPos+3 > email.length) {
		return false;
	}
	return true;
}

function validFile(fileName) {
	var invalidChars = "!@#$%^&*()\\+={}[]\"\'<>?`\| /:,;";
	if (fileName === "") {
		return false;
	}
	var i;
	for (i=0; i<invalidChars.length;i++) {
		badChar = invalidChars.charAt(i);
		if (fileName.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	periodPos = fileName.indexOf(".",1);
	if (periodPos === -1) {
		return false;
	}
	if (periodPos+4 > fileName.length) {
		return false;
	}
	return true;
}

function minLen(str,i) {
	if (str.length < i) {
		return false;
	}
	return true;
}

function isHexColor(inputVal) {
	var i,c,j,inputStr;
	inputStr = inputVal.toString();
	if (inputStr.length !== 6 && inputStr.length !== 0) {
		return false;
	}
	j = -1;
	for (i = 0; i < inputStr.length; i++) {
		c = inputStr.charAt(i);
		if (!( ((c >= "a") && (c <= "f")) || ((c >= "A") && (c <= "F")) || ((c >= "0") && (c <= "9")) )) {
			j += 1;
			break;
		}
	}
	if (j > -1) {
		return false;
	}
	return true;
}

function makeArray(n) {
	var i, value;
	for (i = 1; i <= n; i++) {
		value[i] = 0;
	}
	return value;
}


// allows toggling of links
// <a href="index.html" onclick="toggle(document.getElementById('someID')); return false;">
function toggle(obj) {
	var el = document.getElementById(obj);
	if (el.style.display != 'none') {
		el.style.display = 'none';
	} else {
		el.style.display = '';
	}
}

// used in address.class.php to hide shipping form
function toggleContent(box, div) {
	if(box.checked === true){
		document.getElementById(div).style.display = "none";
	} else {
		document.getElementById(div).style.display = "";
	}
}

function toggleAllCheckbox(theElement, theName) {
	var theForm,z,s;
	theForm = theElement.form;
	z = 0;
	s = theName.length;
	for(z=0; z<theForm.length;z++){
		subName = theForm[z].name.substring(0,s);
		if(theForm[z].type == 'checkbox' && theName != theElement.name && subName == theName){
			theForm[z].checked = theElement.checked;
		}
	}
}

function hideLink(divID) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(divID).style.style.display = 'none';
	} else {
		if (document.layers) { // Netscape 4
			document.divID.style.display = 'none';
		} else {
			document.all.divID.style.display = 'none';
		}
	}
}
function showLink(divID) {
	if (document.getElementById) {
		document.getElementById(divID).style.display = "";
	} else {
		if (document.layers) {
			document.divID.style.display = "";
		} else {
			document.all.divID.style.display = "";
		}
	}
}



//	function custWindow(doc, w, h [,align, valign [, scroll] ] )
//
//	function that opens window with size and optional location and scroll input
//
//	arguments:
//	'doc'		is the document that will load in the new window
//	'w'			is the width of the window in pixels
//	'h'			is the height of the window in pixels
//	'align'		is the horizontal alignment (l, c, or r)
//	'valign'	is the vertical alignment (t, m, or b)
//	'scroll'	indicates whether or not to display scrollbars (yes or no)

function custWindow(doc,wi,hi,align,valign,scroll) {
	if (arguments.length < 6) {
		scroll = 'yes';
	}
	if (arguments.length < 4) {
		align = 'c';
		valign = 'm';
	}
	topPos=0;
	leftPos=0;
	if (screen) {
		if (align == 'c') {
			leftPos = screen.width/2 - wi/2;
		} else {
			if (align == 'r') {
				leftPos = screen.width - wi - 12;
			}
		}
		if (valign == 'm') {
			topPos = screen.height/2 - hi/2;
		} else {
			if (valign == 'b') {
				topPos = screen.height - hi - 45;
			}
		}
	}
	custWin = window.open(doc,'custWin','toolbar=no,location=no,scrollbars='+scroll+',resizable=yes,width='+wi+',height='+hi+',left='+leftPos+',top='+topPos+'');
	custWin.document.close();
	custWin.focus();
}

var stateArray = new Array("AA","AE","AL","AK","AP","AS","AZ","AR","CA","CO","CT","DE","DC","FM","FL","GA","GU","HI","ID","IL","IN","IA","KS","KY","LA","ME","MH","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","MP","OH","OK","OR","PW","PA","PR","RI","SC","SD","TN","TX","UT","VT","VI","VA","WA","WV","WI","WY");

function isState(inputVal) {
	if (isEmpty(inputVal) || inputVal.length > 2) {
		return false;
	}
	inputStr = inputVal.toString();
	inputStr = inputStr.toUpperCase();

	for (i=0; i<stateArray.length; i++) {
		if (inputStr == stateArray[i]) {
			return true;
		}
	}
	return false;
}

function stripCharsNotInBag(s, bag){
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (bag.indexOf(c) != -1) returnString += c;
	}
	return returnString;
}

function stripNonDigits (s){
	var i;
	var bag = '0123456789';
	var returnString = "";
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (bag.indexOf(c) != -1) returnString += c;
	}
	return returnString;
}

function isCreditCard(st) {
  // Strip non-digits
  st = stripNonDigits(st);

  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
	return (false);

  sum = 0; mul = 1; l = st.length;
	for (i = 0; i < l; i++) {
		digit = st.substring(l-i-1,l-i);
		tproduct = parseInt(digit,10)*mul;
		if (tproduct >= 10) {
			sum += (tproduct % 10) + 1;
		} else {
			sum += tproduct;
		}
		if (mul == 1) {
			mul++;
		} else {
			mul--;
		}
	}
	if ((sum % 10) === 0) {
		return (true);
	} else {
		return (false);
	}
}


function isVisa(cc) {
	// Strip non-digits
	cc = stripNonDigits(cc);
	if (((cc.length === 16) || (cc.length === 13)) && (cc.substring(0,1) === "4")) {
		return isCreditCard(cc);
	}
	return false;
}


function isMasterCard(cc) {
	// Strip non-digits
	cc = stripNonDigits(cc);
	firstdig = cc.substring(0,1);
	seconddig = cc.substring(1,2);
	if ((cc.length === 16) && (firstdig === "5") && ((seconddig >= "1") && (seconddig <= "5"))) {
		return isCreditCard(cc);
	}
	return false;
}


function isAmericanExpress(cc) {
	// Strip non-digits
	cc = stripNonDigits(cc);
	firstdig = cc.substring(0,1);
	seconddig = cc.substring(1,2);
	if ((cc.length === 15) && (firstdig === '3') && ((seconddig === '4') || (seconddig === '7'))) {
		return isCreditCard(cc);
	}
	return false;

}

function isDiscover(cc) {
	// Strip non-digits
	cc = stripNonDigits(cc);
	first4digs = cc.substring(0,4);
	if ((cc.length === 16) && (first4digs === "6011")) {
		return isCreditCard(cc);
	}
	return false;
}

function isJCB(cc) {
	// Strip non-digits
	cc = stripNonDigits(cc);
	firstdig = cc.substring(0,1);
	first4digs = cc.substring(0,4);
	if (((cc.length === 15) && ((first4digs === "2131") || (first4digs === "1800"))) || ((cc.length === 16) && (firstdig === '3'))) {
		return isCreditCard(cc);
	}
	return false;
}


function textareaGrow(obj) {
	var height = document.getElementById(obj).style.height;
	height = height.replace("px","");
	height = parseInt(height,10) * 1.33;
	if(height>650) {height = 650;}
	document.getElementById(obj).style.height = height+"px";
}

function textareaShrink(obj) {
	var height = document.getElementById(obj).style.height;
	height = height.replace("px","");
	height = parseInt(height,10) * 0.66;
	if(height<50) {height = 50;}
	document.getElementById(obj).style.height = height+"px";
}


/* how did you hear about us? */
function showOther(heard_about) {
	var chosen = heard_about[heard_about.selectedIndex].value.toLowerCase();
	if (chosen == 'other') {
		document.getElementById('heard_about_other').style.visibility = 'visible';
	} else {
		document.getElementById('heard_about_other').style.visibility = 'hidden';
	}
}


/* wish list select all */
function selectAll(formname) {
	var FORM = document.forms[formname];
	for (var z=0; z < FORM.length; z++) {
		if (FORM[z].type == 'checkbox' && FORM[z].name != 'wish_list_access') {
			FORM[z].checked = true;
		}
	}
}

function validateDecimalPlaces(inputObj, decimal_places){
	var decimal_offset = inputObj.value.indexOf(".");
	if (decimal_offset == -1){
		return true;
	}else if (decimal_offset + decimal_places + 1 < inputObj.value.length){
		return false;
	}
	return true;
}


/* End Library */
/* Start Validation */
/*
Error handling functions for form validation. Trés cool, man.
*/

function validLogin(form) {

	clearErr(form);

	if (isEmpty(form.first_name.value)) {
		pushErr("Please enter a first name.",form.first_name);
	}
	if (isEmpty(form.last_name.value)) {
		pushErr("Please enter a last name.",form.last_name);
	}
	if (isEmpty(form.address_1.value)) {
		pushErr("Please enter an address.",form.address_1);
	}
	if (isEmpty(form.city.value)) {
		pushErr("Please enter a city.",form.city);
	}
	if (isEmpty(form.state.value)) {
		pushErr("Please enter a state.",form.state);
	}
	if (isEmpty(form.postal_code.value)) {
		pushErr("Please enter a postal code.",form.postal_code);
	}
	if (isEmpty(form.country.value)) {
		pushErr("Please enter a country.",form.country);
	}
	if (isEmpty(form.phone_day.value) && isEmpty(form.phone_eve.value)) {
		pushErr("Please enter at least one phone number.",form.phone_day);
	}
	if (isEmpty(form.dob.value)) {
		pushErr("Please enter date of birth.",form.dob);
	}

	return showErr("The following must be addressed before submitting:");
}

function validCheckoutLogin(form) {

	clearErr(form);
	form.supportscript.value = true;
	if (form.email) {
		if (isEmpty(form.email.value)) {
			pushErr("Please enter your email address.",form.email);
		} else if(!validEmail(form.email.value)){
			pushErr("Please enter a valid email address.",form.email);
		}
	}
	if (form.password) {
		if (isEmpty(form.password.value)) {
			pushErr("Please enter your password.",form.password);
		}
	}
	return showErr("The following must be addressed before submitting:");
}

function validCheckoutBillingAddr(form,min_pw_len) {
	if (arguments.length < 2) {
		var require_email = true;
	}

	clearErr(form);

	// elements array
	var i;
	var x = form.elements;
	var bill_country = '';
	var ship_country = '';
	for (i=0; i<x.length; i++) {

		if (x[i].id == 'bill_first_name' && !x[i].value) {
			pushErr("Please enter your billing first name.",x[i]);
		}
		if (x[i].id == 'bill_last_name' && !x[i].value) {
			pushErr("Please enter your billing last name.",x[i]);
		}
		if (x[i].id == 'bill_address_1' && !x[i].value) {
			pushErr("Please enter your billing address.",x[i]);
		}
		if (x[i].id == 'bill_city' && !x[i].value) {
			pushErr("Please enter your billing city.",x[i]);
		}
		if (x[i].id == 'bill_state'){

			for (var j=0; j<x.length; j++) {
				if (x[j].id == 'bill_country') {
					bill_country = x[j].options[x[j].selectedIndex].value;
					break;
				}
			}
			if (bill_country == 'US' && !x[i].value) {
				pushErr("Please enter your two-letter billing state.",x[i]);
			} else if (bill_country == 'US') {
	 			if (!isState(x[i].value)) {
					pushErr("Please enter a valid two-letter billing state abbreviation.",x[i]);
	 			}
			}
 		}
 		// http://en.wikipedia.org/wiki/Postal_code
		if ((x[i].id == 'bill_postal_code' && !x[i].value) && (bill_country!='HK' && bill_country!='MO')) {
			pushErr("Please enter your billing postal/zip code.",x[i]);
		}
		if (x[i].id == 'bill_postal_code' && (x[i].value.length < 5) && bill_country == 'US') {
			pushErr("Please enter a minimum 5-digit billing postal/zip code.",x[i]);
		}
		if (x[i].id == 'bill_phone_day' && !x[i].value) {
			pushErr("Please enter your billing phone number.",x[i]);
		}
		if (x[i].id == 'bill_email') {
			if (!validEmail(x[i].value,true)) {
				pushErr("Please enter a valid email address.",x[i]);
			}
		}
		if (x[i].id == 'password') {
			var pw, verify;
			pw = x[i].value;
			verify = '';

			for (j=0; j<x.length; j++) {
				if (x[j].id == 'verify_password') {
					verify = x[j].value;
					break;
				}
			}
			
			if (pw.length < min_pw_len) {
				pushErr("Please create a password at least "+min_pw_len+" characters in length.",x[i]);
				field[field.length] = x[j];
			} else if (pw.length < min_pw_len) {
				pushErr("For your protection, password must be a minimum of "+min_pw_len+" characters in length.",x[i]);
				field[field.length] = x[j];
			} else if(!(/\d/.test(pw) && /[a-zA-Z]/.test(pw))){
				pushErr('Passwords must contain at least one letter and one number.',x[i]);
				field[field.length] = x[j];
			} else if (pw != verify) {
				pushErr("Your password and password verification do not match.",x[i]);
				field[field.length] = x[j];
			}
		}
		if (!form.same_as_billing.checked) {

 			if (x[i].id == 'ship_first_name' && !x[i].value){
 				pushErr("Please enter your shipping first name.",x[i]);
 			}
 			if (x[i].id == 'ship_last_name' && !x[i].value){
 				pushErr("Please enter your shipping last name.",x[i]);
 			}
 			if (x[i].id == 'ship_address_1' && !x[i].value){
 				pushErr("Please enter your shipping address.",x[i]);
 			}
 			if (x[i].id == 'ship_city' && !x[i].value){
 				pushErr("Please enter your shipping city.",x[i]);
 			}
 			if (x[i].id == 'ship_state'){

				for (j=0; j<x.length; j++) {
					if (x[j].id == 'ship_country') {
						ship_country = x[j].options[x[j].selectedIndex].value;
						break;
					}
				}
				if (ship_country == 'US' && !x[i].value) {
					pushErr("Please enter your two-letter shipping state.",x[i]);
				} else if (ship_country == 'US') {
					if (!isState(x[i].value)) {
						pushErr("Please enter a valid two-letter shipping state abbreviation.",x[i]);
					}
				}
 			}
 			if (x[i].id == 'ship_postal_code' && !x[i].value){
 				pushErr("Please enter your shipping postal/zip code.",x[i]);
 			}
			if (x[i].id == 'ship_postal_code' && (x[i].value.length < 5) && bill_country == 'US') {
				pushErr("Please enter a minimum 5-digit shipping postal/zip code.",x[i]);
			}
			if (x[i].id == 'ship_phone_day' && !x[i].value) {
				pushErr("Please enter your shipping phone number.",x[i]);
			}
 		}
	}

	return showErr("The following must be addressed before submitting:");
}

function validForgotPassword(form){
	clearErr(form);
	if (isEmpty(form.email_address.value)) {
		pushErr("Please enter your email address.",form.email_address);
	}
	return showErr("The following must be addressed before submitting:");
}

function validNewShipAddr(form) {
	clearErr(form);
	if (form.payment_plan.selectedIndex === 0) {
		pushErr("Please select a payment plan.",form.payment_plan);
	}

	return showErr("");

}

function validBillingCC(form,accepted) {
	clearErr(form);
	var x = form.elements;
	var cc_exp_mo;
	var cc_exp_mo_obj;
	var cc_exp_yr;
	var cc_exp_yr_obj;
	var validate_cvv;

	// Check to see if validate_cvv is set first
	for (var i=0; i<x.length; i++) {
		//alert(x[i].name + ' ' + x[i].value);
		if (x[i].id == 'validate_cvv'){
			validate_cvv = x[i].value;
		}
	}


	for (i=0; i<x.length; i++) {
		if (x[i].id == 'cc_num' && isEmpty(x[i].value)){
			pushErr ("Please enter your credit card number.", x[i]);
		} else if (x[i].id == 'cc_num' && !isCreditCard(x[i].value)){
			pushErr ("The credit card number you entered is not a valid card number.", x[i]);
		} else if (x[i].id == 'cc_num') {
			var cc_type;
			cc_type = false;
			for(var j=0; j<accepted.length; j++){
				if (accepted[j]=='visa' && isVisa(x[i].value)) {
					cc_type = 'Visa';
					break;
				} else if (accepted[j]=='mastercard' && isMasterCard(x[i].value)) {
					cc_type = 'MasterCard';
					break;
				} else if (accepted[j]=='amex' && isAmericanExpress(x[i].value)) {
					cc_type = 'American Express';
					break;
				} else if (accepted[j]=='discover' && isDiscover(x[i].value)) {
					cc_type = 'Discover';
					break;
				} else if (accepted[j]=='dinersclub' && isDinersClub(x[i].value)) {
					cc_type = 'Diners Club';
					break;
				} else if (accepted[j]=='jcb' && isJCB(x[i].value)) {
					cc_type = 'JCB';
					break;
				}
			}
			if (cc_type===false){
				pushErr ("The credit card number you entered is not accepted by the merchant.", x[i]);
			}
		} else if (x[i].id == 'cc_exp_mo'){
			// subtract 0 to cast it as an integer
			cc_exp_mo = x[i].options[x[i].selectedIndex].value - 0;
			cc_exp_mo_obj = x[i];
		} else if (x[i].id == 'cc_exp_yr'){
			cc_exp_yr = x[i].options[x[i].selectedIndex].value - 0;
			cc_exp_yr_obj = x[i];
		} else if (x[i].id == 'name' && isEmpty(x[i].value)){
			pushErr ("Please enter the name as it appears on the credit card.", x[i]);
		} else if (validate_cvv == '1') {
			if (x[i].id == 'cc_cvv' && isEmpty(x[i].value)){
				pushErr ("Please enter your credit card's security code.", x[i]);
			} else if (x[i].id == 'cc_cvv' && !isPosInteger(x[i].value)){
				pushErr ("Your credit card's security code should be a three- or four-digit number.", x[i]);
			} else if (x[i].id == 'cc_cvv' && (x[i].value.length != 3) && (x[i].value.length != 4)){
				pushErr ("Your credit card's security code should be a three- or four-digit number.", x[i]);
			}
		}
	}

	var today = new Date( );
	var year = today.getFullYear( ) - 2000;
	var month = today.getMonth( ) + 1;

	if ((year > cc_exp_yr) || (year == cc_exp_yr && month > cc_exp_mo)) {
		pushErr ("Please enter a valid expiration date.",cc_exp_mo_obj);
		field[field.length] = cc_exp_yr_obj;
	}

	return showErr("The following must be addressed before summitting:");
}


function validContactForm(form){
	clearErr(form);

	if (isEmpty(form.name.value)) {
		pushErr("Please enter your name.",form.name);
	}
	if (isEmpty(form.email.value)) {
		pushErr("Please enter your email address.",form.email);
	} else if (!validEmail(form.email.value)) {
		pushErr("Please enter a valid email address.",form.email);
	}
	if (isEmpty(form.subject.value)) {
		pushErr("Please enter a subject.",form.subject);
	}
	if (isEmpty(form.comments.value)) {
		pushErr("Please enter your comments.",form.comments);
	}
	return showErr("The following must be addressed before submitting:");
}

function validLoginForm(form){
	if (isEmpty(form.email.value)) {
		pushErr("Please enter your email address.",form.email);
	}
	if (isEmpty(form.password.value)) {
		pushErr("Please enter your password.",form.password);
	}
	return showErr("The following must be addressed before submitting:");
}

function validShipStatus(status,qtyobj,qty,shipped) {
	if (status.value == 'Pending' ||
		status.value == 'Back-ordered' ||
		status.value == 'Cancelled' ||
		status.value == 'Hold') {
		qtyobj.value = 0;
		return true;
	} else if (status.value == 'Complete') {
		qtyobj.value = qty - shipped;
		return true;
	}

	return false;
}

function validNewAccount(form,pass_min_len) {
	clearErr(form);

	if (isEmpty(form.first_name.value)) {
		pushErr("Please enter your first name.",form.first_name);
	}
	if (isEmpty(form.last_name.value)){
		pushErr("Please enter your last name.",form.last_name);
	}

	if(pass_min_len>0){
		if(!isEmpty(form.password.value) && isEmpty(form.confirm_password.value)){
			pushErr("Please enter the confirmation password.",form.confirm_password);
		}
		if(isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)){
			pushErr("Please enter your password.",form.password);
		}
		if((!isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)) && form.password.value != form.confirm_password.value){
			pushErr("Your passwords do not match.",form.password);
		}
		if((!isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)) && form.password.value.length<pass_min_len){
			pushErr("Your password must be at least "+pass_min_len+" characters long.",form.password);
		}
	}

	return showErr("The following must be addressed before submitting:");
}
function validNewWholesaleAccount(form,pass_min_len) {
	clearErr(form);
	if (isEmpty(form.company_name.value)) {
		pushErr("Please enter your company name.",form.company_name);
	}
	if (isEmpty(form.first_name.value)) {
		pushErr("Please enter your first name.",form.first_name);
	}
	if (isEmpty(form.last_name.value)){
		pushErr("Please enter your last name.",form.last_name);
	}
	if (isEmpty(form.address_1.value)) {
		pushErr("Please enter your street address.",form.address_1);
	}
	if (isEmpty(form.city.value)) {
		pushErr("Please enter your city.",form.city);
	}
	if (isEmpty(form.state.value)) {
		pushErr("Please enter your state.",form.state);
	}
	if (isEmpty(form.postal_code.value)) {
		pushErr("Please enter your postal code.",form.postal_code);
	}
	if (isEmpty(form.phone_day.value)) {
		pushErr("Please enter your phone number.",form.phone_day);
	}
	if(pass_min_len>0){
		if(!isEmpty(form.password.value) && isEmpty(form.confirm_password.value)){
			pushErr("Please enter the confirmation password.",form.confirm_password);
		}
		if(isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)){
			pushErr("Please enter your password.",form.password);
		}
		if((!isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)) && form.password.value != form.confirm_password.value){
			pushErr("Your passwords do not match.",form.password);
		}
		if((!isEmpty(form.password.value) && !isEmpty(form.confirm_password.value)) && form.password.value.length<pass_min_len){
			pushErr("Your password must be at least "+pass_min_len+" characters long.",form.password);
		}
	}

	return showErr("The following must be addressed before submitting:");
}

function validPostAuth(form,auth) {
	clearErr(form);

	if (isEmpty(form.amount.value) || form.amount.value===0) {
		pushErr("Please enter an amount.",form.amount);
	}
	if (form.amount.value>auth){
		form.amount.value = auth;
		pushErr("You may not postauth for more than "+auth+", which was the authorization amount.",form.amount);
	}

	return showErr("The following must be addressed before submitting:");
}


function validConfirm(form,agree) {
	clearErr(form);
	var x = form.elements;
	for (var i=0; i<x.length; i++) {
		if (x[i].id == 'cc_cvv' && isEmpty(x[i].value)){
			pushErr ("Please enter your credit card's security code.", x[i]);
		} else if (x[i].id == 'cc_cvv' && (!isPosInteger(x[i].value) || (x[i].value.length < 3))){
			pushErr ("Your credit card's security code should be a three- or four-digit number.", x[i]);
		}
		if (x[i].id == 'checkout_confirm_i_agree' && !x[i].checked){
			pushErr ("Please check the box labelled: \n\""+agree+"\"\nbefore proceeding.", x[i]);
		}

	}

	return showErr();
}
function validReviewPost(form) {

	clearErr(form);

	if (isEmpty(form.review_text.value)) {
		pushErr("Please enter your review text.",form.review_text);
	}
	if (form.rating_score.options[form.rating_score.selectedIndex].value < 0) {
		pushErr("Please select a rating score.",form.rating_score);
	}

	return showErr("The following must be addressed before submitting:");
}
/* End Validation */
/* Start Catalog */


function slideShow(direction) {
	if (document.images) {
		thisPicture = thisPicture + direction;
		if (thisPicture > imageCount) {
			thisPicture = 0;
		}
		if (thisPicture <  0) {
			thisPicture = imageCount;
		}

		document.getElementById('slideshow-image').innerHTML = imageArray[thisPicture];
		document.getElementById('slideshow-enlarge').innerHTML = detailArray[thisPicture];
		document.getElementById('slideshow-wrapper').style.width = document.getElementById('slideshow-current-image').style.width;
		document.getElementById('slideshow-caption').innerHTML = captionArray[thisPicture];
	}
}


function thumbnailReplace(thisPicture) {
	if (document.images) {
		document.getElementById('thumbnail-replace-main-image').innerHTML = mainimageArray[thisPicture];
		document.getElementById('thumbnail-replace-enlarge').innerHTML = detailArray[thisPicture];
		document.getElementById('thumbnail-replace-caption').innerHTML = captionArray[thisPicture];
		document.getElementById('thumbnail-replace-wrapper').style.width = widthArray[thisPicture];
		document.getElementById('thumbnail-replace-thumbnail-wrapper').innerHTML = thumbnailArray[thisPicture];
	}
}

function thumbnailGrid(thisPicture, Columns) {
	var ie = document.all;

	thumb_html = "<table class='thumbnail-replace-thumbnail'><tr>";
	c = 0; i = 0;
	while (i < thumbnailArray.length) {
		if (i == thisPicture) { i++; continue; }

		if (c >= Columns) {
			thumb_html = thumb_html + '<tr>';
			c = 0;
		}
		thumb_html = thumb_html + "<td>" + thumbnailArray[i] + "</td>";
		i++; c++;
		if (c >= Columns) {
			thumb_html = thumb_html + '</tr>';
		}
	}
	while (c < Columns) {
		thumb_html = thumb_html + "<td>" + blankImage + "</td>";
		c++;
	}
	thumb_html = thumb_html + "</table>";

	if (ie) {
		if (document.images) {
			document.all['thumbnail-replace-main-image'].innerHTML = mainimageArray[thisPicture];
			document.all['thumbnail-replace-enlarge'].innerHTML = detailArray[thisPicture];
			document.all['thumbnail-replace-caption'].innerHTML = captionArray[thisPicture];
			document.all['thumbnail-replace-thumbnail-wrapper'].innerHTML = thumb_html;
		}
	} else {
		if (document.images) {
			document.getElementById('thumbnail-replace-main-image').innerHTML = mainimageArray[thisPicture];
			document.getElementById('thumbnail-replace-enlarge').innerHTML = detailArray[thisPicture];
			document.getElementById('thumbnail-replace-caption').innerHTML = captionArray[thisPicture];
			document.getElementById('thumbnail-replace-wrapper').style.width = widthArray[thisPicture];
			document.getElementById('thumbnail-replace-thumbnail-wrapper').innerHTML = thumb_html;
		}
	}
}



function popUp(image,width,height,caption,closebutton) {

	popup = custWindow('',parseInt(width,10)+50,parseInt(height,10)+100,'c','m');
	var popup = custWin.document;
	var content = '';

	content += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n';
	content += '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n';
	content += '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n';
	content += '<head>\n';
	content += '<title>Preview</title>\n';
	content += '<style type="text/css">@import url("/mc_css/default.css");</style>\n';
	content += '</head>\n';
	content += '<body>\n';
	content += '<table cellpadding="5" cellspacing="0" border="0" width="100%" style="margin-top: 15px;">\n';
	content += '<tr><td align="center"><a href="javascript:self.close();"><img src="'+image+'"></a></td></tr>\n';
	content += '<tr><td align="center">'+caption+'</td></tr>\n';
	content += '<tr><td align="center"><a href="javascript:self.close();">'+closebutton+'</a></td><tr>\n';
	content += '</table>\n';
	content += '</body>\n';
	content += '</html>\n';

	popup.write(content);
	popup.close();
}



