// checkRequiredFields
	function checkRequiredFields(formObj) {  
		var alert_message = ""; 
		for (i = 0; i < formObj.requiredFields.length; i++) {
			if (formObj.requiredFields[i].value != "") {
				temp = formObj.requiredFields[i].value.split("|");
				fieldName 		= temp[0];
				isRequired		= temp[1].toLowerCase();
				validationType	= temp[2].toLowerCase();
				fieldTitle		= temp[3];				

				
				
				// Time (hh:mm:ss)
				 if (validationType == "time (hh:mm:ss)") {
					if (isRequired == "required" || !isEmpty(eval("formObj." + fieldName + ".value"))) {
						if (eval("!isValidTiming(formObj." + fieldName + ".value)")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						}
					}
				
				
				// Time (hh:mm am/pm)
				} else if (validationType == "time (hh:mm am/pm)") {
					if (isRequired == "required" || !isEmpty(eval("formObj." + fieldName + ".value"))) {
						if (eval("!isValidTime(formObj." + fieldName + ".value)")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						}
					}
				
				
				// Date
				} else if (validationType == "date") {
					
					thisDate = "";
					if (eval("formObj." + fieldName)) {
						thisDate = eval("formObj." + fieldName + ".value");
					
					} else if (eval("formObj." + fieldName + "Day") && eval("formObj." + fieldName + "Month") && eval("formObj." + fieldName + "Year")) {
						thisDate = eval("formObj." + fieldName + "Day.value") + "/" + eval("formObj." + fieldName + "Month.value") + "/" + eval("formObj." + fieldName + "Year.value");
					}

					if (isRequired == "required" || (!isEmpty(thisDate) && thisDate != "//")) {
						if (!isValidDate(thisDate)) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						}
					}
				
				
				// year
				} else if (validationType == "year") {
					if (isRequired == "required" || !isEmpty(eval("formObj." + fieldName + ".value"))) {
						if (eval("!isValidYear(formObj." + fieldName + ".value)")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						}
					}
				
				
				// select
				} else if (validationType == "select") {
					if (isRequired == "required") {
						if (eval("!isSelected(formObj." + fieldName + ".value)")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						}
					}
					
					
				// Radio
				} else if (validationType == "radio") {
					if (isRequired == "required") {
						if (eval("!isRadioSelected(formObj." + fieldName + ")")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						}
					}
					
					
				// Integer
				} else if (validationType == "integer") {
					if (isRequired == "required" || !isEmpty(eval("formObj." + fieldName + ".value"))) {
						if (eval("!isInteger(formObj." + fieldName + ".value)")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						
						} else {
						
							eval("thisFieldValue = parseInt(formObj." + fieldName + ".value)");
						
							// if have "Total Allowed" on integer field
							if (eval("formObj.totalAllowed_" + fieldName)) {
								eval("thisTotalAllowed = parseInt(formObj.totalAllowed_" + fieldName + ".value)");
							
								// check that not getting more than is available
								if (thisFieldValue > thisTotalAllowed) {
									alert_message = alert_message + "   " + fieldTitle + ": only " + thisTotalAllowed + " allowed\n";
								}
							}
							
							// if have "Allow Zero" on integer field
							if (eval("formObj.allowZero_" + fieldName)) {
								eval("thisAllowZero = formObj.allowZero_" + fieldName + ".value");
							
								// check that field is not zero
								if (thisAllowZero != 1 && thisFieldValue == 0) {
									if (isRequired == "required") {
										alert_message = alert_message + "   " + fieldTitle + ": zero not allowed\n";
									} else {
										alert_message = alert_message + "   " + fieldTitle + ": zero not allowed (leave blank if not wanted)\n";
									}
								}
							}
						}
					}
					
					
				// Decimal
				} else if (validationType == "decimal") {
					if ((isRequired == "required" || !isEmpty(eval("formObj." + fieldName + ".value"))) && eval("formObj." + fieldName + ".style.display != 'none'")) {
						
						if (eval("!isDecimalNumber(formObj." + fieldName + ".value)")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						} else {
						
							eval("thisFieldValue = parseFloat(formObj." + fieldName + ".value)");
						
							// if have "Allow Zero" on decimal field
							if (eval("formObj.allowZero_" + fieldName)) {
								eval("thisAllowZero = formObj.allowZero_" + fieldName + ".value");
							
								// check that field is not zero
								if (thisAllowZero != 1 && thisFieldValue == 0) {
									if (isRequired == "required") {
										alert_message = alert_message + "   " + fieldTitle + ": zero not allowed\n";
									} else {
										alert_message = alert_message + "   " + fieldTitle + ": zero not allowed (leave blank if not wanted)\n";
									}
								}
							}
							
							// if have "Allow Negative" on decimal field
							if (eval("formObj.allowNegative_" + fieldName)) {
								eval("thisAllowNegative = formObj.allowNegative_" + fieldName + ".value");
							
								// check that field is not zero
								if (thisAllowNegative != 1 && thisFieldValue < 0) {
									alert_message = alert_message + "   " + fieldTitle + ": negative numbers not allowed\n";
								}
							}
							
						}
					}
					
					
				// Email
				} else if (validationType == "email") {
					if (isRequired == "required" || !isEmpty(eval("formObj." + fieldName + ".value"))) {
						if (eval("!isValidEmail(formObj." + fieldName + ".value)")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						}
					}
					
					
				// URL	
				} else if (validationType == "url") {
					if (isRequired == "required" || !isEmpty(eval("formObj." + fieldName + ".value"))) {
						if (eval("!isValidURL(formObj." + fieldName + ".value)")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						}
					}
				
				// text
				} else if (validationType == "text") {
					if (isRequired == "required") {
						if (eval("isEmpty(formObj." + fieldName + ".value)")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						}
					}
				
				// image file
				} else if (validationType == "imagefile") {
					if (isRequired == "required" || !isEmpty(eval("formObj." + fieldName + ".value"))) {
						if (eval("!isValidImageType(formObj." + fieldName + ".value)")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						}
					}
					
				// jpg image file
				} else if (validationType == "jpgimagefile") {
					if (isRequired == "required" || !isEmpty(eval("formObj." + fieldName + ".value"))) {
						if (eval("!isValidJpgImageType(formObj." + fieldName + ".value)")) {
							alert_message = alert_message + "   " + fieldTitle + "\n";
						}
					}
				}
			}
		}
		return alert_message;
	}
	
	
// getUsrAge
	function getUsrAge(usrDOB, ageCutoffDate) {  
		var usrAge = "";
		if (isValidDate(usrDOB) && isValidDate(ageCutoffDate)) {
			
			tempArray 	= usrDOB.split("/");
			dobDay		= tempArray[0];
			dobMonth	= tempArray[1] - 1;
			dobYear		= tempArray[2];
			
			var thisDOB	= new Date(dobYear, dobMonth, dobDay);
			
			tempArray 	= ageCutoffDate.split("/");
			dobDay		= tempArray[0];
			dobMonth	= tempArray[1] - 1;
			dobYear		= tempArray[2];
			
			var thisAgeCutoffDate	= new Date(dobYear, dobMonth, dobDay);
			
			usrAge	= thisAgeCutoffDate.getFullYear() - thisDOB.getFullYear();
			
			if (thisDOB.getMonth() > thisAgeCutoffDate.getMonth()) {
				usrAge = usrAge - 1;
			
			} else if ((thisDOB.getMonth() == thisAgeCutoffDate.getMonth()) && (thisDOB.getDate() > thisAgeCutoffDate.getDate())) {
				usrAge = usrAge - 1;
			}
		}
		
		return usrAge;
	}	
	
	
// getRadioCheckboxValue
	function getRadioCheckboxValue (radioObject) {
		thisValue = "";
		
		eventOption = -1;
	
		if (radioObject[0]) {			
			for ( z=0; z < radioObject.length; z++) {
				if (radioObject[z].checked) {
					if (thisValue == "")
						thisValue = radioObject[z].value;
					else
						thisValue = thisValue + "|" + radioObject[z].value;
				}
			}
		} 
		else {
			if (radioObject.checked) { thisValue = radioObject.value }
		}
			
		return thisValue;
	}


// Returns "true" if "str" contains only alpha characters [a-z, A-Z] (no whitespace etc.).
	function isAlpha(strValue) {
		return (! strValue.search (/^([a-zA-Z]+)$/));
	}

// isDecimalNumber
	function isDecimalNumber (passedVal) {
		if(passedVal == "") {
			return false;
		}
			
		numberDecimals = 0;
		for (z=0; z<passedVal.length; z++) {
			if (passedVal.charAt(z) == ".") {
				numberDecimals = numberDecimals + 1;
			}
			else if (passedVal.charAt(z) == "-" && z == 0) {
			}
			else if (passedVal.charAt(z) < "0") {
				return false;
			}
			else if (passedVal.charAt(z) > "9") {
				return false;
			}
		}
		
		if (numberDecimals > 1)
			return false;
		
		
		return true;
	}

	
// isEmpty
	function isEmpty (strValue) {
		return (! strValue.replace (/^(\s*)/, "", strValue));
	}
	
	
// isInteger
	function isInteger (passedVal) {
		if(passedVal == "") {
			return false;
		}
			
		for (z=0; z<passedVal.length; z++) {
			if (passedVal.charAt(z) < "0") {
				return false;
			}
			else if (passedVal.charAt(z) > "9") {
				return false;
			}
		}
		return true;
	}
	
	
// isRadioSelected
	function isRadioSelected (radioObject) {
		eventOption = -1;
	
		if (radioObject[0]) {			
			for ( z=0; z < radioObject.length; z++) {
				if (radioObject[z].checked) {
					eventOption = z;
				}
			}
		} 
		else {
			if (radioObject.checked) { eventOption = 1; }
		}
			
		if(eventOption == -1) {
			return false;
		} else {
			return true;
		}
	}
	
	
// getRadioSelected
	function getRadioSelected (radioObject) {
		eventOption = -1;
	
		if (radioObject[0]) {			
			for ( z=0; z < radioObject.length; z++) {
				if (radioObject[z].checked) {
					eventOption = z;
				}
			}
		} 
		else {
			if (radioObject.checked) { eventOption = 1; }
		}
			
		return eventOption;
	}
	
	
// isSelected
	function isSelected (strValue) {
		if (strValue == "0" || strValue == "")
			return false;
		else
			return true;
	}
	
// Return "true" if "monthnum" and "yearnum" constitute a valid credit card date.
//
// Created (16 Oct 2000) Raymond Woo
// Modified (24 May 2001) Raymond Woo -- optimised code for checking : 2 digits only for month/year each
	function isValidCCDate(monthnum, yearnum) {
		var now = new Date();
		var futureexpiry = (now.getYear() + 10); // within x years
		// Must be 2 digits each
		if ((! /^\d{2}$/.test(monthnum)) || (! /^\d{2}$/.test(yearnum))) return false;
		// Cannot have expired and must be within x years.
		if (monthnum > 12 || monthnum < 1) return false;
		if (yearnum > (futureexpiry%100) || yearnum < (now.getYear()%100)) return false;
		if (yearnum == (now.getYear()%100) && monthnum <= now.getMonth()) return false; 
		return true;
	}
	
	
// Return "true" if "numbertocheck" is a valid credit card number, as defined by the Luhn algorithm.
//
// Created (16 Oct 2000) Raymond Woo
// Modified (24 May 2001) Raymond Woo -- optimised code for checking : 16-19 digits only
// Modified (20 Aug 2001) Raymond Woo -- doh, AMEX is 15 digits. have now dropped the lowerbound to 5, in case of proprietry cards
	function isValidCCNumber(numbertocheck) {
		var weight = 1;
		var checktotal = 0;
		//Characters can only be (5 to 19) digits.
		if (! /^\d{5,19}$/.test(numbertocheck)) return false;
		//LUHN check.
		if (numbertocheck.length % 2 == 0) {weight = 2}  
		else {weight = 1}
		for (i=0; i <= numbertocheck.length-1; i++) 
		{
			digit = numbertocheck.substring(i, i+1);
			val = digit * weight;
			if (val > 9) {val = val - 9}
			checktotal = checktotal + val;
			weight = (weight == 2) ? 1 : 2;
		}
		if (checktotal %10 == 0) {return true}  
		else {return false}
	}
	
	
// Returns an error message if there is an error with the credit card, returns null otherwise.
// This requires the isValidCCNumber and isValidCCDate javascript functions.
// Input:	numbertocheck = the credit card number - digits ONLY, no spaces
//			monthnum, yearnum = the expiry month and year (2-digit year)
//			cardtype = one of "VISA", "MAST","BANK", "DINE", "AMEX", "JCB", "PROP"
// 
// Created (24 May 2001) Raymond Woo
	function validateCCard (numbertocheck, monthnum, yearnum, cardtype) {
		var alert_message = ""; 
		
		if (!isValidCCDate(monthnum, yearnum)) {
			alert_message = alert_message + "   Expiry Date is not valid\n";
		}
		if (!isValidCCNumber(numbertocheck)) {
			alert_message = alert_message + "   Credit Card Number is not valid\n";
			return alert_message;
		}
		
		switch (cardtype) {
			case "VISA" :
				if (! /4\d+/.test(numbertocheck))
					alert_message = alert_message + "   Card number does not appear to be valid for a Visa\n";
				break;
			case "MAST" :
				if (! /5[12345]\d+/.test(numbertocheck))
					alert_message = alert_message + "   Card number does not appear to be valid for a Mastercard\n";
				break;
			case "BANK" :
				if ((! /56022[12345]\d+/.test(numbertocheck)) && (! /56105\d+/.test(numbertocheck)))
					alert_message = alert_message + "   Card number does not appear to be valid for a Bankcard\n";
				break;
			case "DINE" :
				if (! /3[0689]\d+/.test(numbertocheck))
					alert_message = alert_message + "   Card number does not appear to be valid for a Diners Club\n";
				break;
			case "AMEX" :
				if (! /3[47]\d+/.test(numbertocheck))
					alert_message = alert_message + "   Card number does not appear to be valid for an American Express\n";
				break;
			case "JCB" :
				if ((! /1800\d+/.test(numbertocheck)) && (! /2131\d*/.test(numbertocheck)) && (! /35(28|29|[345678])\d+/.test(numbertocheck)))
					alert_message = alert_message + "   Card number does not appear to be valid for a JCB\n";
				break;
			case "PROP" :
				if (! /5[078]\d+/.test(numbertocheck))
					alert_message = alert_message + "   Card number does not appear to be valid for a Proprietry Card\n";
				break;
			default : 
				alert_message = alert_message + "   Card Type is not valid\n";
		}

		return alert_message;
	}
	
	
// Checks the date input for a valid format.
// This code accepts the date formats: DD/MM/YYYY or DD-MM-YYYY
// To alter the format checked for, change the datePat (date pattern) -
//    d{2} requires 2 digits, d{1,2} requires either 1 or 2 digits etc.
//    \/|- requires / or - as a separator etc.
//
// Created (16 Oct 2000) Raymond Woo
	function isValidDate (strValue) {
		var datePat = /^(\d{2})(\/)(\d{2})\2(\d{4})$/; // Change this to check for a different format
		var matchArray = strValue.match(datePat); // is the format ok?
		if (matchArray == null) { return false;	}
		day = matchArray[1]; month = matchArray[3]; year = matchArray[4];
		if (month < 1 || month > 12) { return false; }
		if (day < 1 || day > 31) { return false; }
		if ((month==4 || month==6 || month==9 || month==11) && day==31) { return false; }
		if (month==2) {
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day>29 || (day==29 && !isleap)) { return false; }
		}
		return true;  // date is valid
	}
	
// isValidEmail
	function isValidEmail (emailStr) {
		
		var emailPat=/^(.+)@(.+)$/; 
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
		var validChars="\[^\\s" + specialChars + "\]"; 
		var quotedUser="(\"[^\"]*\")";
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom=validChars + '+'; 
		var word="(" + atom + "|" + quotedUser + ")";
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
		
		var matchArray=emailStr.match(emailPat)
		if (matchArray==null) {
			return false;
		}
				
		var user=matchArray[1];
		var domain=matchArray[2];
				
		if (user.match(userPat)==null) {
			return false;
		}
				
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) {
			for (var z=1;z<=4;z++) {
				if (IPArray[z]>255) {
					return false;
				}
			}
			return true;
		}
				
		var domainArray=domain.match(domainPat)
		if (domainArray==null) {
			return false;
		}
		
		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 || 
			domArr[domArr.length-1].length>3) {
			return false;
		}
			
		if (len<2) {
			return false;
		}
		
		return true;
	}
	
	
// isValidImageType
	function isValidImageType (strValue) {
		var imageTypesArray = new Array();
		
		imageTypesArray[0] = "gif";
		imageTypesArray[1] = "jpg";
		imageTypesArray[2] = "jpeg";
		
		
		var stringArray = strValue.split(".");
		fileType = stringArray[stringArray.length - 1];
		
		for (z = 0; z < imageTypesArray.length; z++) {
			if(imageTypesArray[z].toLowerCase() == fileType.toLowerCase()) {
				return true;
			}
		}
		return false;
	}	
	
	
// isValidJpgImageType
	function isValidJpgImageType (strValue) {
		var imageTypesArray = new Array();
		
		imageTypesArray[0] = "jpg";
		imageTypesArray[1] = "jpeg";
		
		
		var stringArray = strValue.split(".");
		fileType = stringArray[stringArray.length - 1];
		
		for (z = 0; z < imageTypesArray.length; z++) {
			if(imageTypesArray[z].toLowerCase() == fileType.toLowerCase()) {
				return true;
			}
		}
		return false;
	}
	
	
// Returns "true" if the "strValue" is in a valid 12 hour time format, eg. HH:MM or H:MM 
// P or PM or A or AM are optional at the end, and are case insensitive.
//
// Created (16 Oct 2000) Raymond Woo
// Modified (27 Nov 2000) Raymond Woo "AM" matching corrected. Doh.
	function isValidTime ( strValue ) {
		var objRegExp = /^(0{0,1}[1-9]|1[0-2]):[0-5]\d\s*([A]|[P])M*$/i;
		return objRegExp.test( strValue );
	}
	
	
// isValidTiming (h:mm:ss or m:ss or s.nnn)
	function isValidTiming( strValue ) {
		// var objRegExp = /^(0{0,1}[0-9]:|[0-9][0-9]:|)(0{0,1}[0-5]|)[0-9]:[0-5][0-9]*$/i;
		//                 hours  1/01/12 or blank   
		//											 minutes blank or 1-2 digits
		//																	    seconds 1-2 digits
		//																					 milli seconds - up to 3 digits
		var objRegExp = /^(0{0,1}[0-9]:|[0-9][0-9]:|)(0{0,1}[0-5][0-9]:|[0-9]:|)([0-5]|)[0-9](|\.[0-9]{1,3})*$/i;
		return objRegExp.test( strValue );
	}
	
	
// isValidURL
	function isValidURL (strValue) {
		
		// CHECK IF HAS VALID URL PREFIX
		var urlPrefixArray = new Array();
		urlPrefixArray[0] = "file";
		urlPrefixArray[1] = "ftp";
		urlPrefixArray[2] = "gopher";
		urlPrefixArray[3] = "http";
		urlPrefixArray[4] = "https";
		urlPrefixArray[5] = "telnet";
		urlPrefixArray[6] = "wais";
		urlPrefixArray[7] = "mailto";
		urlPrefixArray[8] = "news";
		
		
		// check URL is long enough
		if (strValue.length <= 7 ) {
			return false;
		}
		
		
		hasPrefix = false;
		stringAddress = "";
		var stringArray = strValue.split(":");
		stringPrefix = stringArray[0];
		
		
		if (stringArray.length >= 2) stringAddress = stringArray[1];
		
		
		var addressArray = stringAddress.split("//");
		if (addressArray.length >= 2) stringAddress = addressArray[1];
		
		var addressArray = stringAddress.split("/");
		stringAddress = addressArray[0];
		
		var addressArray = stringAddress.split("?");
		stringAddress = addressArray[0];
		
		for (z = 0; z < urlPrefixArray.length; z++) {
			if(urlPrefixArray[z] == stringPrefix) {
				hasPrefix = true;
			}
		}
		
		// IF NO VALID PREFIX - RETURN FALSE
		if (!hasPrefix) return false;
		
		
		// are regular expressions supported?
	    var supported = 0;
	    if (window.RegExp) {
	        var tempStr = "a";
	        var tempReg = new RegExp(tempStr);
	        if (tempReg.test(tempStr)) supported = 1;
	    }
		
		// check if right format
	    if (supported) {
			var r1 = new RegExp(/^[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.[a-z]{2,3})+$/);
			
			return (r1.test(stringAddress));
			
	    } else {
			return true;
		}
	}
	
	
// isValidYear
	function isValidYear ( strValue ) {
		var objRegExp = /^(\d{4})$/;
		var objRegExp2 = /^(\d{4})-(\d{4})$/;
		
		if (strValue.match( objRegExp )) {
			return true;
		} else if (strValue.match( objRegExp2 )) {
			return true;
		} else {
			return false;
		}
	}
	
	
	
// Eliminates everything apart from digits (0 through 9) from a string.
//
// Created (16 Oct 2000) Raymond Woo
	function reduceToDigits (strValue) {
		return (strValue.replace (/([^0-9])/g, "", strValue));
	}
	

	
//////////////////////////////////////////////////////////////////////////////////////
//////																			//////
//////							FORM CHECKING									//////
//////																			//////
//////////////////////////////////////////////////////////////////////////////////////


// formCheckComplete	
	function formCheckComplete(formObj) {  

		var alert_message = "";
	
		alert_message = checkRequiredFields(formObj);
		
		if (alert_message) { 
			alert ("Your form is incomplete.\n" +
				   "You must supply the following information:\n" + 
				   alert_message )
			return false;
		} else { 
			return true;
		}
	}
	
