
// ---------------------------------------------------------------------------------
// HTML_Validate
// ---------------------------------------------------------------------------------
//
// Modifed by PureBlend to highlight errant prompt fields
//
// Copyright  2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.
//
// Eric's old notes:
// 		Log error to Console^2: throw new Error("[debug] " + theStarIMG.alt);


// ---------------------------------------------------------------------------------
// Cliff's Utilities
// ---------------------------------------------------------------------------------

function GetDataStr(anID) {

	var	theStr		= "";
	var theField	= document.getElementById(anID);

	if (theField) {
		theStr 		= theField.value;
	}

	return theStr;
}

function returnError(anError,anID) {

	if (anError != "") {
		anError		= anError + "<br>";
		thePrompt	= document.getElementById(anID + "_prompt");

		if (thePrompt) {
			thePrompt.className = "inputform_problem";
		}
	}

	return anError;
}

function GetFieldLink(anID) {

	var theLink	= '<a href="#' + anID + '_prompt">' + anID + '</a>';

	return theLink;

}

// ---------------------------------------------------------------------------------
// email
// ---------------------------------------------------------------------------------

function checkEmailField(anID) {

	var error		= "";
	var	theStr		= GetDataStr(anID);

	if (theStr == "") {
	   error = "You didn't enter an " + GetFieldLink(anID) + " address.";
	}

	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(theStr))) {
	   error = "Please enter a valid " + GetFieldLink(anID) + " address.";
	}
	else {
		//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\s\;\:\\\"\[\]]/
		if (theStr.match(illegalChars)) {
			error = "The " + GetFieldLink(anID) + " address contains illegal characters.";
		}
	}

	return returnError(error,anID);
}

// ---------------------------------------------------------------------------------
// phone number - strip out delimiters and check for 10 digits
// ---------------------------------------------------------------------------------

function checkPhone(anID) {

	var error		= "";
	var	theStr		= GetDataStr(anID);

	if (theStr == "") {
	   error = "You didn't enter a phone number.";
	}

	var stripped = theStr.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) {
		   error = "The phone number contains illegal characters.";

		}
		if (!(stripped.length == 10)) {
		error = "The phone number is the wrong length. Make sure you included an area code.";
		}

	return returnError(error,anID);
}

// ---------------------------------------------------------------------------------
// password - between 6-8 chars, uppercase, lowercase, and numeral
// ---------------------------------------------------------------------------------

function checkPassword(anID) {

	var error		= "";
	var	theStr		= GetDataStr(anID);

	if (theStr == "") {
	   error = "You didn't enter a " + GetFieldLink(anID) + ".";
	}

		var illegalChars = /[\W_]/; // allow only letters and numbers

		if ((theStr.length < 6) || (theStr.length > 10)) {
		   error = "The " + GetFieldLink(anID) + " is the wrong length; it must be between 6 and 10 characters.";
		}
		else if (illegalChars.test(theStr)) {
		  error = "The " + GetFieldLink(anID) + " contains illegal characters.";
		}
/*		else if (!((theStr.search(/(a-z)+/)) && (theStr.search(/(A-Z)+/)) && (theStr.search(/(0-9)+/)))) {
		   error = "The " + GetFieldLink(anID) + " must contain at least one uppercase letter, one lowercase letter, and one numeral.";
		}
*/
	return returnError(error,anID);
}

// ---------------------------------------------------------------------------------
// username - 4-10 chars, uc, lc, and underscore only.
// ---------------------------------------------------------------------------------

function checkUsername(anID) {

	var error		= "";
	var	theStr		= GetDataStr(anID);

	if (theStr == "") {
	   error = "You didn't enter a username.";
	}

	var illegalChars = /\W/; // allow letters, numbers, and underscores
	if ((theStr.length < 4) || (theStr.length > 10)) {
	   error = "The username is the wrong length.";
	}
	else if (illegalChars.test(theStr)) {
		error = "The username contains illegal characters.";
	}

	return returnError(error,anID);
}

// ---------------------------------------------------------------------------------
// non-empty textbox
// ---------------------------------------------------------------------------------

function isEmptyField(anID) {

	var error		= "";
	var	theStr		= GetDataStr(anID);

	  if (theStr.length < 1) {
		 error = "The required field " + GetFieldLink(anID) + " is empty."
	  }

	return returnError(error,anID);
}

// ---------------------------------------------------------------------------------
// was textbox altered
// ---------------------------------------------------------------------------------

function isDifferent(anID) {

	var error		= "";
	var	theStr		= GetDataStr(anID);

	  if (theStr != "Can\'t touch this!") {
		 error = "You altered the inviolate text area: " +  GetFieldLink(anID) + ".";
	  }

	return returnError(error,anID);
}

// ---------------------------------------------------------------------------------
// valid selector from dropdown list
// ---------------------------------------------------------------------------------

function checkDropdownField(anID) {

	var error		= "";
	var theChoice	= document.getElementById(anID).selectedIndex;

		if (theChoice == 0) {
			error = "Please choose an option from the drop-down list: " + GetFieldLink(anID) + ".";
		}

	return returnError(error,anID);
}

// ---------------------------------------------------------------------------------
// exactly one radio button is chosen
// ---------------------------------------------------------------------------------

function checkRadio(myFormElement,anID) {

	var error		= "";
	var myOption	= -1;

	for (i=myFormElement.length-1; i > -1; i--) {
		if (myFormElement[i].checked) {
			myOption = i;
		}
	}

	if (myOption == -1) {
		error = "Please click a " + GetFieldLink(anID) + " radio button.";
	}
	
	return returnError(error,anID);
   }

