var linkNum = 0;

preloadImg();

/* 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();
	}
}

// Define regular expressions
var charexp = /./;
var numexp = /[0-9]/;
var floatexp = /[(0-9)|.]/;
var phonexp = /^\d{10}$/;

// 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,path) {
	if (path.length > 0) {
		path = path + "/";
	}

	var src = path+name+"Over.gif";
	imgSwap(name,src);
}

/* This function handles mouseout image swaps */
function imgOut(name,path) {
	if (path.length > 0) {
		path = path + "/";
	}

	var src = path+name+".gif";
	imgSwap(name,src);
}

/* 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);
	}
}
