var linkNum = 0;

preloadImg();

/* this function preloads all images for the site */
function preloadImg() {
	var imgPath = "../images/"
	var img = new Array();

	for (i=0; i<53; i++) {
		img[i] = new Image(1,1);
	}

	img[0].src = imgPath + "spacer.gif";
	img[1].src = imgPath + "shieldLogo.gif";
	img[2].src = imgPath + "banners/grapesBanner.jpg";
	img[3].src = imgPath + "banners/grapesBannerRight.jpg";
	img[4].src = imgPath + "banners/grapesTopNav.gif";
	img[5].src = imgPath + "buttons/bLogin.gif";
	img[6].src = imgPath + "1.jpg";
	img[7].src = imgPath + "2.jpg";
	img[8].src = imgPath + "3.jpg";
	img[9].src = imgPath + "4.jpg";
	img[10].src = imgPath + "5.jpg";
	img[11].src = imgPath + "environControl.gif";
	img[12].src = imgPath + "environControlOver.gif";
	img[13].src = imgPath + "inventoryMgmt.gif";
	img[14].src = imgPath + "inventoryMgmtOver.gif";
	img[15].src = imgPath + "reputation.gif";
	img[16].src = imgPath + "reputationOver.gif";
	img[17].src = imgPath + "security.gif";
	img[18].src = imgPath + "securityOver.gif";
	img[19].src = imgPath + "service.gif";
	img[20].src = imgPath + "serviceOver.gif";
	img[21].src = imgPath + "locationMap.gif";
	img[22].src = imgPath + "banners/barrelBanner.jpg";
	img[23].src = imgPath + "banners/barrelBannerRight.jpg";
	img[24].src = imgPath + "banners/forkliftBanner.jpg";
	img[25].src = imgPath + "banners/forkliftBannerRight.jpg";
	img[26].src = imgPath + "banners/glassBanner.jpg";
	img[27].src = imgPath + "banners/glassBannerRight.jpg";
	img[28].src = imgPath + "buttons/bCalculate.gif";
	img[29].src = imgPath + "buttons/bSignUp.gif";
	img[30].src = imgPath + "buttons/bCancel.gif";
	img[31].src = imgPath + "cases/case3333a.gif";
	img[32].src = imgPath + "cases/case3333b.gif";
	img[33].src = imgPath + "cases/case33a.gif";
	img[34].src = imgPath + "cases/case66a.gif";
	img[35].src = imgPath + "cases/case66b.gif";
	img[36].src = imgPath + "mugshots/moisheMana.jpg";
	img[37].src = imgPath + "mugshots/eugeneLemay.jpg";
	img[38].src = imgPath + "mugshots/elizabethVanDyk.jpg";
	img[39].src = imgPath + "mugshots/shaulShemesh.jpg";
	img[40].src = imgPath + "mugshots/danaOberfest.jpg";
	img[41].src = imgPath + "mugshots/kellyReizun.jpg";
	img[42].src = imgPath + "packaging/box1.gif";
	img[43].src = imgPath + "packaging/box2.gif";
	img[44].src = imgPath + "packaging/box3.gif";
	img[45].src = imgPath + "packaging/box4.gif";
	img[46].src = imgPath + "packaging/box5.gif";
	img[47].src = imgPath + "packaging/box6.gif";
	img[48].src = imgPath + "packaging/box7.gif";
	img[49].src = imgPath + "packaging/box8.gif";
	img[50].src = imgPath + "packaging/box9.gif";
	img[51].src = imgPath + "packaging/packingTape.gif";
	img[52].src = imgPath + "packaging/tapeDispenser.gif";
}

/* This function opens a window to a link */
function openLink(URL) {
	if (URL.length > 0) {
		var newWindow;

		newWindow = window.open(URL,"link"+ linkNum,"dependant=no,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,width=400,height=400,status=yes,location=yes,directories=yes");
		linkNum++;
		newWindow.focus();
	}
}

// this function tells the user that sections are under construction
function underConstruction() {
	alert("This section is currently under construction.\nPlease try again later.");
	document.location.href = document.location;
}

/* show/hide extra info in online estimate */
function dispExtraInfo(check1, check2, check3) {
	var state = "hidden";

	if ((check2 == true) || (check3 == true)) {
		state = "hidden";
	} else {
		state = "visible";
	}

	if (isIE) {
		document.all.extraInfo.style.visibility = state;
	}
}

// Define regular expressions
var charexp = /./;
var numexp = /[0-9]/;
var floatexp = /[(0-9)|.]/;
var phonexp = /^\d{10}$/;

/* this function handles calculations for the online estimate */
function updateCost() {
	var pos, rowName, cost;

	for (i=0; i<document.formEstimate.elements.length; i++) {
		pos = 0;
		rowName = "";
		cost = "";

		pos = document.formEstimate.elements[i].name.indexOf("Cost");

		if (pos > 0) {
			rowName = document.formEstimate.elements[i].name.substr(0,pos);

			eval("document.formEstimate."+ rowName +"Qty.value = stripChar(document.formEstimate."+ rowName +"Qty.value,/[^((0-9)|\.)]+/)");
			eval("cost = parseFloat(document.formEstimate."+ rowName +"Qty.value) * parseFloat(document.formEstimate."+ rowName +"Price.value)");

			cost *= 100;
			cost = Math.round(cost)/100;

			document.formEstimate.elements[i].value = stripChar(cost.toString(),/[^((0-9)|\.)]+/);
		}
	}
}

/* this function calculates the online estimate */
function calculateEstimate() {
	var cost;
	var a,b,c,d,e,f;

	a = document.formEstimate.aCaseCost.value.toString();
	if (a <= 0) {
		a = 0;
	}
	b = document.formEstimate.bCaseCost.value.toString();
	if (b <= 0) {
		b = 0;
	}
	c = document.formEstimate.cCaseCost.value.toString();
	if (c <= 0) {
		c = 0;
	}
	d = document.formEstimate.dCaseCost.value.toString();
	if (d <= 0) {
		d = 0;
	}
	e = document.formEstimate.eCaseCost.value.toString();
	if (e <= 0) {
		e = 0;
	}
	f = document.formEstimate.fCaseCost.value.toString();
	if (f <= 0) {
		f = 0;
	}

	cost = parseFloat(a) + parseFloat(b) + parseFloat(c) + parseFloat(d) + parseFloat(e) + parseFloat(f);
	cost *= 100;
	cost = Math.round(cost)/100;

	document.formEstimate.totalMonth.value = cost;

	// update extra info
	dispExtraInfo(document.formEstimate.lockKey.checked, document.formEstimate.invReport.checked, document.formEstimate.pickupDelivery.checked);
}


// Remove all characters from string that are not digits
function stripChar(str,exp) {
	return str.replace(exp,"");
}

// Test string for characters
function hasChar(str) {
	return charexp.test(str);
}

// This function opens a popup window
function openPopup(URL) {
	var newWindow;

	newWindow = window.open(URL,"popup","dependant=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes,width=388,height=417,status=no,location=no,directories=no");
	newWindow.focus();
}

/* This function performs image swaps.
	The path to the images folder is assumed to be ../images.
	The image is assumed to be a GIF file */
function imgSwap(name,src) {
	eval("document."+name+".src = \"../images/"+src+"\"");
}

/* This function handles mouseover image swaps */
function imgOver(name) {
	var src = name+"Over.gif";
	imgSwap(name,src);
}

/* This function handles mouseout image swaps */
function imgOut(name) {
	var src = name+".gif";
	imgSwap(name,src);
}

/* this function checks and submits the sign up form */
function signUp() {
	// check if user has read the terms of service
	if (document.formSignUp.iAgree.checked) {
		// check for required fields
		document.formSignUp.submit();
	} else {
		// display error window
		alert("You have not read and agreed to\nthe Terms of Services Agreement");
		document.formSignUp.iAgree.focus();
	}
}

/* this function checks and submits the questionaire form */
function submitQuestionaire() {
	// check for required fields
	document.formQuestionaire.submit();
}

/* this function checks and submits the signup form */
function submitPackaging() {
	// check for required fields
	document.formPackaging.submit();
}

/* this function displays a login error message */
function badLogin() {
	var msg = "Incorrect Login Information!";
	alert(msg);
}

// This function validates a form and submits it if the required fields are complete
// formName is the name of the form within the document
// fields is a comma seperated list of form fields that are required
// check is the type of validation to perform:
/*
	1= Value is present (all characters)
	2= Value is alphabetical (no numbers)
	3= Value is a valid date (MM/DD/YYYY)
	4= Value is a valid email (has an @ symbol)
	5= Value is not the default in a select box
*/
function submitForm(formName,fields,check) {
	var valid = true;
	var pos = 0;
	var alertText = "Please complete the following fields:\n";
	var fieldName = new Array();
	var fieldValue = new Array();
	var checkType = new Array();

	var i = 0;
	pos = fields.indexOf(",");
	while (pos != -1) {
		fieldName[i] = fields.substring(0,pos);
		eval ("fieldValue["+i+"] = document." + formName + "." + fieldName[i] + ".value");
		if (fieldValue[i] == null) {
			fieldValue[i] = " ";
		}
		fields = fields.substring(pos + 1, fields.length);
//		fieldName[i] = fieldName[i].substring(3,fieldName[i].length);
		i++;
		pos = fields.indexOf(",");
	}

	if (fields.length > 0) {
		fieldName[i] = fields;
		eval ("fieldValue["+i+"] = document." + formName + "." + fieldName[i] + ".value");
		if (fieldValue[i] == null) {
			fieldValue[i] = " ";
		}
//		fieldName[i] = fieldName[i].substring(3,fieldName[i].length);
	}

	var i = 0;
	pos = check.indexOf(",");
	while (pos != -1) {
		checkType[i] = check.substring(0,pos);
		check = check.substring(pos + 1, check.length);
		i++;
		pos = check.indexOf(",");
	}

	if (check.length > 0) {
		checkType[i] = check;
	}


	for (i=0; i<fieldValue.length; i++) {
		pos = fieldValue[i].indexOf(" ");
		while (pos == 0) {
			fieldValue[i] = fieldValue[i].substring(1, fieldValue[i].length);
			pos = fieldValue[i].indexOf(" ");
		}
		// Is the value present?
		if ((fieldValue[i].length==0) && (checkType[i]!=5)) {
			valid=false;
			alertText = alertText + " - " + fieldName[i] + "    (empty)\n";

		} else if ((fieldValue[i].length>0) || (checkType[i]==5)) {
			if (checkType[i]==2) {
			// Is the value alphabetical?
				var j=0;
				while (j<fieldValue[i].length) {
					var oneMore=j+1;
					var theLetter=fieldValue[i].substring(j, oneMore)
					if ((!alph.test(theLetter)) && (theLetter!="-") && (theLetter!=" ") && (theLetter!="[") && (theLetter!="]") && (theLetter!=".")) {
						valid=false;
						alertText = alertText + " - " + fieldName[i] + "    (invalid)\n";
						j=fieldValue[i].length;
					}
					j++;
				}
			} else if (checkType[i]==3) {
			// Is the value a valid date?
				if ( (fieldValue[i].length > 10) || (fieldValue[i].substring(2,3)!="/") || (fieldValue[i].substring(5,6)!="/") || (!num.test(fieldValue[i].substring(0,1))) || (!num.test(fieldValue[i].substring(1,2))) || (!num.test(fieldValue[i].substring(3,4))) || (!num.test(fieldValue[i].substring(4,5))) || (!num.test(fieldValue[i].substring(0,1))) || (!num.test(fieldValue[i].substring(6,7))) || (!num.test(fieldValue[i].substring(7,8))) || (!num.test(fieldValue[i].substring(8,9))) || (!num.test(fieldValue[i].substring(9,10))) ) {
					valid=false;
					alertText = alertText + " - " + fieldName[i] + "    (invalid - must be MM/DD/YYYY)\n";
				}
			} else if (checkType[i]==4) {
			// Is the value a valid email address?
				pos = fieldValue[i].indexOf("@");

				if (pos==-1) {
					valid=false;
					alertText = alertText + " - " + fieldName[i] + "    (invalid)\n";
				}
			} else if (checkType[i]==5) {
			// Is the value other than the default in the select box?
		eval("selected=document."+formName+"."+fieldName[i]+".options[document."+formName+"."+fieldName[i]+".selectedIndex].value");
				if (selected==0) {
					valid=false;
					alertText = alertText + " - " + fieldName[i] + "    (empty)\n";
				}
			}
			//More Validation algorithms can be inserted here
		}
	}

	if (valid) {
		eval("document."+ formName +".submit()");
	} else {
		alert(alertText);
	}
}
