
// ---------------------------------------------------------------------------------
// 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;
	} */
	
	try {
		theStr 		= theField.value;
	} catch (e) { alert(anID); theStr 		= theField.innerHTML; }

	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 ("+GetFieldLink(anID)+") 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 || theStr == ' ') {
		 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);
   }

// --------------------------------------------------------------------------------
//	length of anID.value must be at least min and less than max {type} in length
//	Fri Dec 22 2006 EHG
// ---------------------------------------------------------------------------------

function checkLength(anID,min,max,type) {
	var error			= "";
	var theElement		= document.getElementById(anID);
	var theValue		= theElement.value;

	if (theValue.length < min) {
		error += "" + GetFieldLink(anID) + " needs to be at least "+min+" "+type+" in length.";
	} 
	
	if (theValue.length > max) {
		error += "" + GetFieldLink(anID) + " can be at most "+max+" "+type+" in length.";
	} 
	
	return returnError(error,anID);
}

// --------------------------------------------------------------------------------
//	anID.value MUST EQUAL given required_value
//	Fri Dec 22 2006 EHG
// ---------------------------------------------------------------------------------

function checkExactValue(anID,required_value) {
	var error		= "";
	var theValue		= document.getElementById(anID).value;
	
	// Convert both to lowercase (relax)
	var theReqValue		= required_value.toLowerCase();
	theValue			= theValue.toLowerCase();
	
	if (theValue != theReqValue) {
		error += "" + GetFieldLink(anID) + " does not have an acceptable value.";
	} 
	return returnError(error,anID);
}

// --------------------------------------------------------------------------------
//	MUST conform to mm/dd/yyyy
//	Fri Dec 22 2006 EHG
// ---------------------------------------------------------------------------------
function checkDate(anID) {
	var error		= "";
	var theValue	= document.getElementById(anID).value

	return returnError(isDate(theValue,anID),anID);
}

// --------------------------------------------------------------------------------
//	value must be a real number and between min and max
//	Fri Dec 22 2006 EHG
// ---------------------------------------------------------------------------------
function checkIntegerValue(anID,min,max) {
	
	var error		= "";
	var theValue		= parseInt(document.getElementById(anID).value);
	
	if (isNaN(theValue)) {
		error += "" + GetFieldLink(anID) + " Please enter whole numbers only.";
	} else {
		if (theValue < min) {
			error += "" + GetFieldLink(anID) + " needs to be at least "+min+"";
		}
		if (theValue > max) {
			error += "" + GetFieldLink(anID) + " can be at most "+max+"";
		} 
	}
	return returnError(error,anID);
}

// --------------------------------------------------------------------------------
//	value must be a real number
//	Fri Dec 22 2006 EHG
// ---------------------------------------------------------------------------------

function isNumber(anID) {
	var error		= "";
	var theValue		= parseInt(document.getElementById(anID).value);
	
	if (isNaN(theValue)) {
		error += "" + GetFieldLink(anID) + " Please enter a number.";
	}
	return returnError(error,anID);
}











// EXTERNALLY SOURCED FXNs

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr,anID){
	var error = '';
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		error = ("The "+GetFieldLink(anID)+" format should be : mm/dd/yyyy")
	}
	if (strMonth.length<1 || month<1 || month>12){
		error = ("Please enter a valid month for "+GetFieldLink(anID))
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		error = ("Please enter a valid day for"+GetFieldLink(anID))
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		error = (""+GetFieldLink(anID)+" Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		error = ("Please enter a valid "+GetFieldLink(anID)+"")
	}
	return error;
}




function valueNotDefault(anID) {
	var error		= "";
	var ele			= document.getElementById(anID);
	var	theStr		= ele.value;//getAttribute('value');
	var theDefault	= ele.getAttribute('default');
	// 
	if (theStr == "" || theStr == theDefault) {
		error += "Please enter this important information in " + GetFieldLink(anID) + "";
		ele.setAttribute('value',theDefault);
	} 
	

	return returnError(error,anID);
	
}

