<!--

function isBlank(theString){
	if ((theString == null) || (theString == "") || (theString == " ")){
		return true;
	}else{
		for(var i = 0; i < theString.length; i++){
			var c = theString.charAt(i);
			if ((c != '\n') && (c != '\t')){
				return false;
			}
		}
		return true;
	}
}

function GetRadioValue(radioField){
  alert("radioField.length is: "+radioField.length)
  for (var i = 0; i < radioField.length; i++){
    alert("radioField["+i+"].checked is: "+radioField[i].checked)
    if (radioField[i].checked) { return radioField[i].value }
  }
  return ""
}

function GetObjValue(objField){
  switch (objField.type){
    case "text":
      return objField.value
    case "hidden":
      return objField.value
    case "select-one":
      return objField.value //selectedIndex.toString()
    case "textarea":
      return objField.value
    case "radio":
      return objField.value //GetRadioValue(objField)
  }
}

function SetObjValue(objField, vData){
  switch (objField.type){
    case "text":
    case "hidden":
      objField.value = vData
      break
    case "select-one":
      objField.selectedIndex = vData
      break
  }
}

function allDigits(str){
	return inValidCharSet(str, "0123456789")
}

function inValidCharSet(str, charset){
	var result = true
	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i = 0; i < str.length; i++){
		if (charset.indexOf(str.substr(i,1)) < 0){
			result = false
			break
		}
	}
	return result
}

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function emailFilter(objField){
  //Grab the controls name and value, or selectedIndex in the case of a drop down
  var strObjName = objField.name //.toLowerCase()
  var strObjValue = GetObjValue(objField) //selectedIndex for drop downs
  if (!isEmail(strObjValue)){
    return "-- "+strObjName+" entered is invalid; email address format is: 'user@domain.com'\n"
  }else{
    return ""
  }
  /*
  //See if there's an @ sign in the string
  var index = strObjValue.indexOf("@")
  if (index > 0){
    //Make sure there's a period somewhere after the @ sign
    var pindex = strObjValue.indexOf(".", index)
    if ((pindex > index+1) && (strObjValue.length > pindex+1)){
	    return ""
	  }
  }
  return "-- "+strObjName+" entered is invalid; email address format is: 'user@domain.com'\n"
  */
}

function numFilter(objField, strFormat) {
  //Grab the controls name and value, or selectedIndex in the case of a drop down
  var strObjName = objField.name //.toLowerCase()
  var strObjValue = GetObjValue(objField) //selectedIndex for drop downs
	if (!isBlank(strObjValue)) { //Do not perform if empty strObjValue
  	var chr = "";
		var numbers = ""; //Store all the numbers here
		//Process to remove non-numbers and spaces
		for(var i = 0; i < strObjValue.length; i++) {
			chr = strObjValue.charAt(i);
			if(!(isNaN(chr) || chr == " ")) numbers += chr;
		}
		//Remove country code, if any
		//if(numbers.substring(0, 2) == "47") numbers = numbers.substring(2, numbers.length);
		var output = ""; //Assign numbers here
		//assign numbers to chosen strFormat
		var n = 0, i = 0;
		while(i < strFormat.length && n < numbers.length) {
			chr = strFormat.charAt(i);
			if(chr == "#") {
				output += numbers.charAt(n++)
			} else {
				output += chr;
			}
			i++;
		}
		//If it's a phone number field, it must be 10 digits long.
		if (numbers.length != 10 && strObjName.toLowerCase().indexOf("phone") > -1) {
			return "-- The "+strObjName+" must be 10 digits long in the format '###-###-####'\n"
		}else{
		  objField.value = output; //output to objField
		  return ""
		}
	}
}

function dateFilter(objField){
  //Grab the controls name and value, or selectedIndex in the case of a drop down
  var strObjName = objField.name //.toLowerCase()
  var strObjValue = GetObjValue(objField) //selectedIndex for drop downs
  //Make sure it in slash format, not dash format
  strObjValue = strObjValue.replace(/-/g, "/")
  //If it used dashes, change the field's value
  objField.value = strObjValue
  //Get the individual data parts
  var strDateParts = strObjValue.split("/")
  //Now make sure it's a valid date
  if (strDateParts.length == 3){
		var month = parseInt(strDateParts[0],10)
		var day = parseInt(strDateParts[1],10)
		var year = parseInt(strDateParts[2],10)
		var result = allDigits(strDateParts[0]) && (month > 0) && (month < 13) &&
    				     allDigits(strDateParts[1]) && (day > 0) && (day < 32) &&
    				     allDigits(strDateParts[2]) && ((strDateParts[2].length == 2) || (strDateParts[2].length == 4))
    if (result){
      return ""
    }else{
      return "-- "+strObjName+" entered is invalid; use 'mm/dd/yyyy' format\n"
    }
  }else{
    return "-- "+strObjName+" entered is invalid; use 'mm/dd/yyyy' format\n"
  }
}

function exceedsMaxLen(strToCheck, strExcType, intMax){
  //If intMax is -1, we're not supposed to check the length
  if (intMax == -1) { return false }
  //If the length is <= intMax, we're okay
  if (strToCheck.length <= intMax) { return false }
  //If there's no exception, it's too long at this point
  if (isBlank(strExcType)) { return true }
  
  //Otherwise we need to see if this is one of the 14 exceptions
  var i = 0
  var bTooLong = true
  var strExceptions
  var strExceptions1 = new Array("")
  var strExceptions2 = new Array("")
  
  //Get the correct exception string by which var we're checking
  switch (strExcType){
    case "1":
      strExceptions = strExceptions1
      break
    case "2":
      strExceptions = strExceptions2
      break
  }
  
  //Now see if the string to check is too long or not
  for (i = 0; i < strExceptions.length; i++){
    if (strToCheck == strExceptions[i]) { bTooLong = false }
  }
  
  return bTooLong
}

function validateField(objField, intMaxLen, strExcType, strRequired){
  //Grab the controls name and value, or selectedIndex in the case of a drop down
  var bRequired = (!isBlank(strRequired))
  var strObjName = objField.name
  var strObjNameLower = strObjName.toLowerCase()
  var strObjValue = GetObjValue(objField) //selectedIndex for drop downs
  
  //If it's a drop down, and it's required, but it's on the first selection
  if (objField.type.toLowerCase() == "select-one" && bRequired && objField.selectedIndex <= 0){
    return "-- Make a selection for "+strObjName+"\n"
  //If it's a radio field, and it's required but nothing is selected
  } else if (objField.type.toLowerCase() == "radio" && bRequired && isBlank(strObjValue)){
    return "-- Make a selection for "+strObjName+"\n"
  //If it's empty and it's required (textboxes)
  } else if (isBlank(strObjValue) && bRequired){
    return "-- "+strObjName+" field is empty\n"
  }
  //If it's not required, but has data, check some other stuff
  if (!isBlank(strObjValue)){
    //Make sure the data entered doesn't exceed the allowable length.
    if (exceedsMaxLen(strObjValue, strExcType, intMaxLen)){
      return "-- "+strObjName+" entered is too long; limit is "+intMaxLen+"\n"
    //If it's a PHONE number, make sure it's in the correct format.
    }else if (strObjNameLower.indexOf("phone") > -1){
      return numFilter(objField, "###-###-####")
    //If it's a DATE field, make sure it's in the correct format.
    }else if (strObjNameLower.indexOf("date") > -1){
      return dateFilter(objField)
    //If it's an EMAIL field, do a rough check of the format.
    }else if (strObjNameLower.indexOf("email") > -1){
      return emailFilter(objField)
    }
  }
  
  //If we make it past the two checks above, return an empty string
  return ""
  
}
//-->
