function isblank(s) {	for(var i = 0; i < s.length ; i++) {		var c = s.charAt(i);		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;	}	return true;}function validateNumber(val, min, max){	if ( isNaN( val ) ) return false;	if ( min && val < min ) return false;	if ( max && val > max ) return false;	return true;}function dateComponents(dateStr, format) {	var results = new Array();	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;	var matchArray = dateStr.match(datePat);		if (matchArray == null) { 		return null; 	}	//check for two digit (20th century) years and prepend 19.	matchArray[4] = (matchArray[4].length == 2) ? '19' + matchArray[4] : matchArray[4];	// parse date into variables	if (format.charAt(0)=="d"){ //what format does the server use for dates? 		results[0] = matchArray[1];		results[1] = matchArray[3];	} else { 		results[1] = matchArray[1];		results[0] = matchArray[3]; }	results[2] = matchArray[4];	return results;}function valiDate(obj, min, max, format){		dateBits = dateComponents(obj.value, format);	if (dateBits == null) return false;//Check it is a valid date first	day = dateBits[0];	month = dateBits[1];	year = dateBits[2];	if ((month < 1 || month > 12) || (day < 1 || day > 31)) { // check month range 		return false;	} 	if ((month==4 || month==6 || month==9 || month==11) && day==31) {		return false;	}	if (month == 2) {	// check for february 29th 		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 		if (day>29 || (day==29 && !isleap)) {			return false;		}	} //Now check whether a range is specified and if in bounds	var theDate = new Date(dateBits[2], parseInt(dateBits[1]) - 1, dateBits[0]);	if ( min ) {		minBits = dateComponents (min, format);		var minDate = new Date(minBits[2], parseInt(minBits[1]) - 1, minBits[0]);		if ( minDate.getTime() > theDate.getTime() ) return false;	} 	if ( max) {		maxBits = dateComponents (max, format);		var maxDate = new Date(maxBits[2], parseInt(maxBits[1]) - 1, maxBits[0]);		if ( theDate.getTime() > maxDate.getTime() ) return false;	}	return true;}function validateEmail( obj ) {	var emailStr = obj.value;	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid	if ( !reg1.test( emailStr ) && reg2.test( emailStr ) ) {		return true;	} else {	 	return false;	}}function validatePassword( obj ) {	var pwdStr = obj.value;	var reg1 = /[a-z]/ ;	var reg2 = /[A-Z]/ ;	var reg3 = /[0-9]/ ;	if ( reg1.test( pwdStr ) && reg2.test( pwdStr ) && reg3.test( pwdStr ) && pwdStr.length > 7) {		return true;	} else {	 	return false;	}}function validateMatch( obj1, obj2 ) {	var pwdStr1 = obj1.value;	var pwdStr2 = obj2.value;	if ( pwdStr1 == pwdStr2) {		return true;	} else {	 	return false;	}}function validateChoose( obj1 ) {	var inputStr1 = obj1.value;	if ( inputStr1 == 'Choose from list') {		return false;	} else {	 	return true;	}}function locateFileUpload( f ) { for(var i = 0; i < f.elements.length; i ++)  if( f.elements[i].type=='file' ){  return f.elements[i]; }}function validateFileType( obj, fTyp ) {dots = obj.value.split(".");fType = "." + dots[dots.length-1];if ( fTyp != null && fTyp.indexOf(fType) == -1 ) return false;return true;}function validateFileLimit( obj, cur, max ) {if ( cur >= max ) return false;return true;}function OnFailure( obj, lbl, msg ){	var msgs = new Array();	msgs["text"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease enter a value.";	msgs["textarea"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease enter a value.";	msgs["select-one"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease select an entry.";	msgs["select-multiple"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease select an entry.";	msgs["checkbox"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease select an entry.";	msgs["file"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease select a file.";	msgs["fileType"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease select a valid file type.";	msgs["fileLimit"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease reduce number of attachment(s) first.";	msgs["radio"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease select an entry.";	msgs["number"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease enter a valid number.";	msgs["date"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease enter a valid date.";	msgs["email"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease enter a valid e-mail address.";	msgs["choose"] = "\n\n All marked fields must be completed in order to submit this form. \n\nYou must choose a value from the list.";	msgs["passwordcheck"] = "\n\n All marked fields must be completed in order to submit this form. \n\nPlease enter a valid password.";	msgs["passwordmatch"] = "Passwords do not match!\n\n All marked fields must be completed in order to submit this form. \n\nPlease enter a valid password.";	if(msg[1]	|| msg[2]){ //upper/lower bound ranges have been specified		if(msg[1]	&& msg[2]){//range			term = ( msg[0] == "date" )? " ("+msg[3]+")" : "";			alert(lbl + msgs[msg[0]] + term + " between " + msg[1] + " and " + msg[2]);		} else if (msg[1]) {//lower bound			term = ( msg[0] == "number" ) ? " greater than " : " (" + msg[3] + ") after ";			alert(lbl + msgs[msg[0]] + term + msg[1]);		} else {//upper bound			term = ( msg[0] == "number" )? " less than " : " (" + msg[3] + ") before ";			alert(lbl + msgs[msg[0]] + term + msg[2]);		}	} else {//no range given		alert(lbl + msgs[msg[0]]);	}		obj.focus();	return false;}function isSomethingSelected( obj ){	for (var r=0; r < obj.length; r++){		if ( obj[r].checked ) return true;	}}function validateRequiredFields( f, a ){	for (var i=0; i < a.length; i++){		e = a[i][0];			//checks input types: "text","select-one","select-multiple","textarea","checkbox","radio","file","password"//alert(e.type)				switch (e.type) {					case "text":							if ((e.value == "" ) || (e.value == null) || isblank(e.value)) return OnFailure(e, a[i][1], ["text"]);							if ( a[i][2] ) {								switch ( a[i][2][0] ){									case "number":										if ( !validateNumber(e.value, a[i][2][1], a[i][2][2]) ) return OnFailure(e, a[i][1], ["number", a[i][2][1], a[i][2][2]]);										break									case "date":										if ( !valiDate(e, a[i][2][1], a[i][2][2], a[i][2][3]) ) return OnFailure(e, a[i][1], ["date", a[i][2][1], a[i][2][2], a[i][2][3]]);										break									case "email":										if ( !validateEmail(e) ) return OnFailure(e, a[i][1], ["email"]);										break									case "choose":										if ( !validateChoose(e) ) return OnFailure(e, a[i][1], ["choose"]);										break 									default:										break								}							}						break					case "file":					//make sure AT LEAST one file gets attached					if ( a[i][2][1] == 0 && ((e.value == "" ) || (e.value == null) || isblank(e.value)) ) return OnFailure(e, a[i][1], ["file"]);					if ( ((e.value == "" ) || (e.value == null) || isblank(e.value)) != "") {						//check type of file that is being uploaded						if ( a[i][2][0] != null && validateFileType( e, a[i][2][0] ) == false ) return OnFailure(e, a[i][1], ["fileType"]);						//check that file limit has not been reached						if ( a[i][2][2] != null && validateFileLimit( e, a[i][2][1], a[i][2][2] ) == false ) return OnFailure(e, a[i][1], ["fileLimit"]);					}						break					case "textarea":					if ((e.value == "" ) || (e.value == null) || isblank(e.value)) return OnFailure(e, a[i][1], ["textarea"]);						break					case "select-one":					if ( e.selectedIndex == 0 ) return OnFailure(e, a[i][1], ["select-one"]);						break					case "select-multiple":					if (e.selectedIndex == -1) return OnFailure(e, a[i][1], ["select-multiple"]);						break 					case "password":							if ( !validatePassword(e) ) return OnFailure(e, a[i][1], ["passwordcheck"]);							if ( !validateMatch(e, a[i][3]) ) return OnFailure(e, '', ["passwordmatch"]);	  			break					default:						//must be a checkbox or a radio group if none of above 						if ( !e[0]) {//handle single item group first							switch (e.type) {							case "checkbox":								if ( !e.checked )  return OnFailure(e, a[i][1], ["checkbox"]);								break							case "radio":								if ( !e.checked )  return OnFailure(e, a[i][1], ["radio"]);								break							default:									break							}						} else { //handle multi-item groups							switch (e[0].type) {							case "checkbox":								if ( !isSomethingSelected( e ) )  return OnFailure(e[0], a[i][1], ["checkbox"]);								break							case "radio":								if ( !isSomethingSelected( e ) )  return OnFailure(e[0], a[i][1], ["radio"]);								break							default:									break							}						}						break				}	}	return true;}
