// THIS IS A UTWK SYSTEM FILE - DO NOT EDIT 
// based on http://www.wesgrant.com/wes/validation.asp
var emailexp = /^[a-z][a-z_0-9\-\.]+@[a-z_0-9\.\-]+\.[a-z]{2,4}$/i
var phoneexp =  /^\d{10}$/

function CRLF () {
	return String.fromCharCode(10) + String.fromCharCode(13);
}

function TAB(howMany) {
	var tempStr
	for (count = 0; count < howMany; count++) {
		tempStr = tempStr & String.fromCharCode(9);
	}
}

function validateEmail(str) {	
	return emailexp.test(str)
}
// Credit Card Validation Javascript
// copyright 12th May 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validateCreditCard(cardNumber) {
  var isValid = false;
  var ccCheckRegExp = /[^\d ]/;
  isValid = !ccCheckRegExp.test(cardNumber);
  if (isValid)
  {
    var cardNumbersOnly = cardNumber.replace(/ /g,"");
    var cardNumberLength = cardNumbersOnly.length;
    var numberProduct;
    var numberProductDigitIndex;
    var checkSumTotal = 0;

    for (digitCounter = cardNumberLength - 1; 
      digitCounter >= 0; 
      digitCounter--)
    {
      checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
      digitCounter--;
      numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
      for (var productDigitCounter = 0;
        productDigitCounter < numberProduct.length; 
        productDigitCounter++)
      {
        checkSumTotal += 
          parseInt(numberProduct.charAt(productDigitCounter));
      }
    }

    isValid = (checkSumTotal % 10 == 0);
  }
  return isValid;
}

function validatePhone(str) {
	return phoneexp.test(str)
}

function StripChars(ItemsToStrip, str) {
	returnString = "";
	for (i = 0; i < str.length; i++) {  
		var c = str.charAt(i);
        	if (ItemsToStrip.indexOf(c) == -1) returnString += c;       	 	 
	}
	return returnString;
}

function AllSpace(str) {   //Makes String Blank if noting but spaces
	for (i=0; i < str.length; i++) {
		if (str.charAt(i) != " ") {
			return str;
		}
	}
	return "";
}

function SetDec(str, places) { //chops decimal places to max number of places	
	if (isNaN(str)) {
		return str;
	}
	if (str.indexOf(".") != -1) {
	    if (places > 0) {
		str = str.substring(0, eval(str.indexOf(".")) + eval(places) + eval(1));
	    } else {
		str = str.substring(0, str.indexOf("."));
	    }
	}
	return str;
}

function DateFormat(dateVal) {	
	DayVal = dateVal.getDate();
	MonthVal = dateVal.getMonth();
	YearVal = dateVal.getYear();	
	if (YearVal<1800) {
		if(YearVal>10){
			YearVal = eval(YearVal) + 1900;
		}else{
			YearVal = eval(YearVal) + 2000;				
		}
	}
	tempStr = YearVal + "-" + eval(MonthVal + 1) + "-" + DayVal;	
	return tempStr;
}

function stripNonDigits(str) {
	return str.replace(/[^0-9]/g,"")
}

function checkform(form, errColor, startColor, showAlert, showErrors, fontStyle) {
    Error = false;	
    alertStr = "";
    for (x=0; x < form.elements.length; x++ ) {	
		e = form.elements[x];
		e1= form.elements[(x+1)];
		fieldError = false;   
		if (e.type == "text" || e.type == "select-one" || e.type == "password"  || e.type == "textarea"  || e.type == "checkbox") {
	    if (showErrors == true) {
		document.all[e.name + 'Error'].innerHTML = "";
	    }		
	    if (e.type != "select-one"){ e.value = AllSpace(e.value);}
	    if (x+1 < form.length && e1.name.charAt(0) == "@") {
		paramStr = e1.name.substring(1, e1.name.length);
		params = null;
		params = paramStr.split("#");
			
		if (params[7] != null) {
			backColor = params[7];
		} else {
			backColor = startColor;
		} 
		
		if (params[6] != null && AllSpace(params[6]) != "" ) {
			defaultValue = params[6];
		} else {
			defaultValue = "";
		}
		if (params[0] == "select") {
			if(e.selectedIndex==0){
				alertStr = alertStr + "The " + params[2] + " field must be selected." + CRLF();
				if (showErrors == true) {
					document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field is not checked.</font>";
				}
				Error = true;
				fieldError = true;
			}

		} else if (params[1] == "NoBlank" && e.value == "" && defaultValue == "") {
			alertStr = alertStr + "The " + params[2] + " field must not be blank." + CRLF();
			if (showErrors == true) {
				document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field must not be blank.</font>";
			}
			Error = true;
			fieldError = true;	
		
		} else if (params[1] == "NoBlank" && e.value == "" && defaultValue != "") {
			e.value = defaultValue;

		} else if (params[0] == "checkbox") {
			if(!e.checked){
				alertStr = alertStr + "The " + params[2] + " field must be checked." + CRLF();
				if (showErrors == true) {
					document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field is not checked.</font>";
				}
				Error = true;
				fieldError = true;
			}
		} else if (params[0] == "email") {
			if (!validateEmail(e.value) && e.value != "") {				
				alertStr = alertStr + "The " + params[2] + " field does not contain a valid email address." + CRLF();
				if (showErrors == true) {
					document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field does not contain a valid email address.</font>";
				}
				Error = true;
				fieldError = true;
			} 
		} else if (params[0] == "ccnumber" && e.value != "") {
			if (!validateCreditCard(e.value)) {
			 	alertStr = alertStr + "Then " + params[2] + " field does not contain a valid card number." + CRLF();
				if (showErrors == true) {
					document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field does not contain a valid card number.</font>";
				}
				Error = true;
				fieldError = true;
			}
		} else if (params[0] == "number" && e.value != "") {
			e.value =  StripChars("$,%", e.value);
			if (params[3] != null) {
				e.value = SetDec(e.value, params[3]);	
			}
			if (isNaN(e.value)) {
			 	alertStr = alertStr + "Then " + params[2] + " field does not contain a valid numeric value." + CRLF();
				if (showErrors == true) {
					document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field does not contain a valid numeric value.</font>";
				}
				Error = true;
				fieldError = true;
			} else {
				if (params[4] != null) {
					if (eval(e.value) < eval(params[4])) {
						alertStr = alertStr + "Then " + params[2] + " field must be greater than " + params[4] + CRLF();
						if (showErrors == true) {
							document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field must be greater than " + params[4] + "</font>";
						}
						Error = true;
						fieldError = true;
					}
				}
				if (params[5] != null) {
					if (eval(e.value) > eval(params[5])) {
						alertStr = alertStr + "Then " + params[2] + " field must be less than " + params[5] + CRLF();
						if (showErrors == true) {
							document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field must be less than " + params[5] + "</font>";
						}
						Error = true;
						fieldError = true;
					}
				}
			}
			
		} else if (params[0] == "age" && e.value != "") {
			e.value = SetDec(e.value,0);
			if (eval(e.value) < 0 || eval(e.value) > 120) {
				alertStr = alertStr + "The " + params[2] + " field doesn't appear to be a valid age." + CRLF();
				if (showErrors == true) {
					document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field doesn't appear to be a valid age.</font>";
				}
				Error = true;
				fieldError = true;
			}			
		} else if (params[0] == "date" && e.value != "") {
			dateYear = new String();
			curDate = new Date();
			if (e.value.substring(2,3) == "-"){
				tempDate = new Date(e.value.substring(3,5)+"-"+e.value.substring(0,2)+"-"+e.value.substring(6,10));
			}else{
				tempDate = new Date(e.value.substring(5,7)+"-"+e.value.substring(8,10)+"-"+e.value.substring(0,4));
			}
			dateYear = dateYear + tempDate.getYear();
			if (e.value != "") {
			    if (tempDate == "NaN") {
				alertStr = alertStr + "The " + params[2] + " field is not a valid date." + CRLF();
				if (showErrors == true) {
					document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field is not a valid date.</font>";
				}
				Error = true;
				fieldError = true;			
			    } else if (params[4] != null && dateYear < eval(curDate.getYear()) - eval(params[4])) {
				alertStr = alertStr + "The " + params[2] + " field is too low." + CRLF();
				if (showErrors == true) {
					document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field is too low.</font>";
				}
				Error = true;
				fieldError = true;			
	
			    } else if (params[5] != null && dateYear > eval(curDate.getYear()) + eval(params[5])) {
				alertStr = alertStr + "The " + params[2] + " field is too high." + CRLF();
				if (showErrors == true) {
					document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field is too high.</font>";
				}
				Error = true;
				fieldError = true;			
	
			    } else {
				e.value = DateFormat(tempDate);
			    }
			}
		} else if (params[0] == "gender" && e.value != "") {
			if (e.value.substring(0, 1)=="M" || e.value.substring(0, 1)=="m"){e.value = "M";}
			if (e.value.substring(0, 1)=="F" || e.value.substring(0, 1)=="f"){e.value = "F";}
			if (e.value!="F" && e.value!="M" )
			{
			    	alertStr = alertStr + "The " + params[2] + " field is invalid.  Please Specify M or F." + CRLF();
				if (showErrors == true) {
					document.all[e.name + 'Error'].innerHTML = "<font  style='" + fontStyle + "'>The " + params[2] + " field is invalid.  Please specify M or F.</font>";
				}
				Error = true;
				fieldError = true;	
			}
		}
		if (fieldError == true) {
			e.style.background = errColor;
	    	} else {
			e.style.background = backColor;
	   	}			
	    } 
	}				
    }
    
    if (Error == true) {
	if (showAlert == true) {
		alert (alertStr);
	}
	return false;
    }
    
}