
function checkNum(formID) {
  thisVal = eval("document.calcform." + formID + ".value");
  if (isNaN(thisVal)) {
	alert(" This isn't a number! \n Please enter a valid number in box " + formID.substring(3, 4) + ". ");
//	eval("document.calcform." + formID + ".value = ''");
//	eval("document.calcform." + formID + ".focus()");
	elementNum = eval(formID.substring(3, 4)); if (elementNum == 1) elementNum -= 1;
	document.calcform.elements[elementNum].value = "";
	document.calcform.elements[elementNum].focus();
  }
}

function checkChange() {
  alert(" You don't need to enter anything here; \n values are calculated automatically. ");
  document.calcform.box3.value = "";
  if (document.calcform.box1.value != "" && document.calcform.box2.value != "") calculate();
}

function calculate() {
  if (document.calcform.box1.value != "" && document.calcform.box2.value != "") {
	calcVal = 0; newVal1 = eval(document.calcform.box1.value);
	newVal2 = eval(document.calcform.box2.value);
	if (document.calcform.Operator.selectedIndex == 0) { calcVal = newVal1 + newVal2; }
	if (document.calcform.Operator.selectedIndex == 1) { calcVal = newVal1 - newVal2; }
	if (document.calcform.Operator.selectedIndex == 2) { calcVal = newVal1 * newVal2; }
	if (document.calcform.Operator.selectedIndex == 3) { calcVal = newVal1 / newVal2; }
	document.calcform.box3.value = "The answer is: " + calcVal;
  }
  else { alert(" Please enter a number in both boxes. "); }
}


function ccNumCheck() {
  proceed = 1; errMessage = "";
  ccnum = document.validation.ccNum.value;
  type = document.validation.ccType.options[document.validation.ccType.selectedIndex].value;

  if (ccnum == "") {
	errMessage = "Please enter something in the credit card field! ";
	alert(errMessage); document.validation.ccNum.focus();
  }
//  if (isNaN(ccnum)) { errMessage = "This is not a number! "; }
  else {

// Remove all spaces from ccnum
  if (ccnum.indexOf(" ") != -1) {
	var tstring = "";
	ccnum = '' + ccnum;
	splitstring = ccnum.split(" ");
	for(i = 0; i < splitstring.length; i++) tstring += splitstring[i];
	ccnum = tstring;
  }
//  alert(type + ": " + ccnum);

  if (type == "Visa") {
	// Visa: length 16, prefix 4, dashes or spaces optional.
	var re = /^4\d{3}( |-)?\d{4}( |-)?\d{4}( |-)?\d{4}$/;
  }
  else if (type == "MasterCard") {
	// Mastercard: length 16, prefix 51-55, dashes or spaces optional.
	var re = /^5[1-5]\d{2}( |-)?\d{4}( |-)?\d{4}( |-)?\d{4}$/;
  }
  else if (type == "AMEX") {
	// American Express: length 15, prefix 34 or 37.
	var re = /^3[4,7]\d{13}$/;
  }

  if (!re.test(ccnum)) {
	proceed = 0;
	alert("The " + type + " number you have entered is invalid!  \nPlease try again.");
	document.validation.ccNum.focus();
  }
  else {
	// Checksum ("Mod 10")
	// Add even digits in even length strings or odd digits in odd length strings.
	var checksum = 0;
	for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
		checksum += parseInt(ccnum.charAt(i-1));
	}

	// Analyze odd digits in even length strings or even digits in odd length strings.
	for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
		var digit = parseInt(ccnum.charAt(i-1)) * 2;
		if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
	}

	if ((checksum % 10) != 0) {
		proceed = 0;
		alert("The " + type + " number you have entered is invalid! \nPlease try again.");
		document.validation.ccNum.focus();
	}
  }

  if (proceed == 1) { alert("This is a valid credit card number! "); }

  }
}


function checkDate() {
  var monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); valid = 1; errMessage = "";

  thisMonth = document.validation.Month.selectedIndex;
  thisDay = document.validation.Day.selectedIndex;
  thisYear = document.validation.Year.options[document.validation.Year.selectedIndex].value;

  if (thisMonth == 0 || thisDay == 0) {
	errMessage = "Please select a";
	if (thisMonth == 0) { errMessage += " month"; }
	if (thisDay == 0) {
		if (thisMonth == 0) { errMessage += " and"; }
		errMessage += " day";
	}
	errMessage += " to proceed! "; alert(errMessage);
  }

  else {
	thisDate = monthNames[thisMonth-1] + " " + thisDay + ", " + thisYear;

	if (thisMonth == 2) {
		if (thisDay >= 30) { valid = 0; }
		if (thisDay == 29 && thisYear/4 != Math.round(thisYear/4)) { valid = 0; }
	}
	else if (thisMonth == 4 || thisMonth == 6 || thisMonth == 9 || thisMonth == 11) {
		if (thisDay == 31) { valid = 0; }
	}

	if (valid == 0) {
		errMessage = " is NOT a valid date! \n";
		errMessage += monthNames[thisMonth-1] + " doesn't have " + thisDay + " days";
		if (thisMonth == 2 && thisDay == 29) { errMessage += " this year (it's not a leap year). \nPlease try again."; }
		else { errMessage += "; please try again. "; }
	}
	else {
		errMessage = " is a valid date! ";
		if (thisMonth == 2 && thisDay == 29) { errMessage += " (It's a leap year) "; }
	}

	alert(thisDate + errMessage);
  }
}


function reqCheck() {
  userName = document.validation.Name.value;
  if (userName == "") {
	alert("Please enter your name! ");
	document.validation.Name.focus();
  }
  else { alert(userName + ", thanks for entering your name! "); }
}

function clearReq() {
//Not Currently Used
  document.validation.Name.value = "";
  document.validation.elements['E-mail'].value = "";
  document.validation.Company.value = "";
//  document.validation.Name.focus();
}


function changeOptions(sex) {
  var nameArray = new Array(); nextItem = 0; newMessage = "";
  menStatus = "removed"; womenStatus = "removed";
//  if (document.optionsform.showwomen.checked == true) { nameArray.push('Amy', 'Jen', 'Michelle'); }
//  if (document.optionsform.showmen.checked == true) { nameArray.push('Rob', 'Tom', 'Victor'); }
  if (document.optionsform.showwomen.checked == true) {
	nameArray = new Array('Amy', 'Jen', 'Michelle'); nextItem = 3;
	womenStatus = "added";
  }
  if (document.optionsform.showmen.checked == true) {
	nameArray[nextItem] = "Rob"; nameArray[nextItem+1] = "Tom"; nameArray[nextItem+2] = "Victor";
	menStatus = "added";
  }
//  alert(nameArray.length);

  while (document.optionsform.names.options.length > 0) { document.optionsform.names.options[0] = null; }
  document.optionsform.names.options[0] = new Option('[Click here]');
  document.optionsform.names.options[0].value = "";

  for (arrayLoop=0; arrayLoop<nameArray.length; arrayLoop++) {
	document.optionsform.names.options[arrayLoop+1] = new Option(nameArray[arrayLoop]);
	document.optionsform.names.options[arrayLoop+1].value = nameArray[arrayLoop];
  }
  document.optionsform.names.selectedIndex = 0;

  newMessage = "The " + sex + " have been ";
  if (sex == "women") { newMessage += womenStatus; }
  if (sex == "men") { newMessage += menStatus; }
  newMessage += "! "; document.optionsform.infobox.value = newMessage;
}


browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);
browser = "ie";
if (browserName == "Microsoft Internet Explorer" && browserVer < 4) { browser = "nonie"; }
if (browserName != "Microsoft Internet Explorer") { browser = "nonie"; }

function toggle(idName) {
  current = (document.all[idName].style.display == "none") ? "block" : "none";
  document.all[idName].style.display = current;
  arrowVal = "arrow" + idName.substring(idName.length-1, idName.length);
  if (current == "block") { document[arrowVal].src = arrowon.src; }
  else { document[arrowVal].src = arrowoff.src; }
}


if (browserName == "Netscape" && browserVer >= 3) version = "n3";
else version = "n2";
if (version == "n2" && browserName == "Microsoft Internet Explorer" && browserVer >= 4) version = "n3";
if (version == "n3") {
  arrowon = new Image(7, 7);
  arrowon.src = "../function/downarrow1.gif";
  rolloveron = new Image(120, 20);
  rolloveron.src = "../function/rollover_on.gif";

  arrowoff = new Image(7, 7);
  arrowoff.src = "../function/smallarrow1.gif";
  rolloveroff = new Image(120, 20);
  rolloveroff.src = "../function/rollover_off.gif";

  simpsons1on = new Image(32, 32);
  simpsons1on.src = "../function/homer.gif";
  simpsons2on = new Image(32, 32);
  simpsons2on.src = "../function/marge.gif";
  simpsons3on = new Image(32, 32);
  simpsons3on.src = "../function/bart.gif";
  simpsons4on = new Image(32, 32);
  simpsons4on.src = "../function/lisa.gif";
  simpsons5on = new Image(32, 32);
  simpsons5on.src = "../function/maggie.gif";
}

function img_act(imgName) {
  if (version == "n3") {
	imgOn = eval(imgName + "on.src");
	document[imgName].src = imgOn;
  }
}

function img_inact(imgName) {
  if (version == "n3") {
	imgOff = eval(imgName + "off.src");
	document[imgName].src = imgOff;
  }
}


function randomImage() {
  rannum = Math.round(Math.random()*4) + 1;
  newSrc = eval("simpsons" + rannum + "on.src");
//  alert(newSrc);
  document.Simpsons.src = newSrc;
}

function specificImage(newSelection) {
  if (eval(newSelection) > 0) {
	newSrc = eval("simpsons" + newSelection + "on.src");
//	alert(newSrc);
	document.Simpsons.src = newSrc;
  }
}

function showImage(newSelection) {
//Not Currently Used
  menuNum = 0;
  if (newSelection == "random") { menuNum = Math.round(Math.random()*4) + 1; }
  else if (newSelection == "specific") { menuNum = document.randomimages.names.selectedIndex; }

  if (menuNum > 0) {
	newSrc = eval("simpsons" + menuNum + "on.src");
//	alert(newSrc);
	document.Simpsons.src = newSrc;
  }
}


function getCookie(cookieHeader) {
  var search = cookieHeader + "=", cookieString = "";
  if (document.cookie.length > 0) {
	findNameIndex = document.cookie.indexOf(search);
	if (findNameIndex != -1) {
		findNameIndex += search.length;
		endIndex = document.cookie.indexOf(";", findNameIndex);
		if (endIndex == -1) endIndex = document.cookie.length;
		cookieString = document.cookie.substring(findNameIndex, endIndex);
	}
  }
  return (cookieString);
}

function checkCookies() {
  newValue = 0;
//  alert("Cookies: " + document.cookie);
  document.cookie = "TestValue=1";
  newValue = getCookie("TestValue");
  if (newValue != 1) { alert("Cookies have been cancelled or disabled. "); }
  else {
	document.cookie = "TestValue=" + "";
	alert("Your browser is cookie-enabled! ");
  }
}

function setCookie(newPage) {
  if (document.cookieform.Name.value == "" || document.cookieform.Color.selectedIndex == 0 || document.cookieform.Animal.selectedIndex == 0) { alert("Please answer all three questions to proceed. "); }

  else {
	newValue = ""; height = 350;
	newCookie = document.cookieform.Name.value + "+" + document.cookieform.Color.selectedIndex + "+" + document.cookieform.Animal.selectedIndex;
//	alert(newCookie + " " + newPage);
	document.cookie = "Democookie=" + newCookie;
	newValue = getCookie("Democookie");
	if (newValue == "") { alert("Cookies appear to have been cancelled or disabled on your browser. \nPlease try again."); }
	else {
		if (newPage == "preferences") { height = 580; }
		window.open(newPage + ".shtml", newPage, "toolbar=no,width=400,height=" + height + ",directories=no,location=no,status=no,scrollbars=yes,resizable=yes,menubar=no");
	}
  }
}
