/*
		validate.js
		Developer:  Nathan Jones
		File Name:  validate.js
		Date Originally Created:  12/18/2002
		Date Modified:  06/02/2003 (ongoing)
		Modified By:  Karl Snyder 
		Modification:  KS - 09/21/2003: Fixed _hasValue for SELECT-ONE.  Added _hasSelected.
		               EH - 06/02/2003: fixing _checkdate ... very strange... the function still breaks if a yyyy value
		               is between 100 - 1752
		Purpose:  
		Total number of functions: 17
*/

/*         File Contents:

		_isDate(object_value)
		_iExist(formElement)
		_isNumeric(object_value)
		_isDecimalNumber(object_value)
		_checkdate(object_value, date_delimiter)
		_checkday(checkYear, checkMonth, checkDay)
		_checkinteger(object_value)
		_checknumber(object_value)
		_checkrange(object_value, min_value, max_value)
		_checktime(object_value)
		_numberrange(object_value, min_value, max_value)
		_onError(form_object, input_object, object_value, error_message)
		_hasValue(obj)
		_validate_phone(val, formObj)
		_transform_phone(thestring)
		_swap_delimiters(val, formObj, delim_from, delim_to)
		_confirm_Password(password,confirmpassword)
		_valid_Email(formObj)
*/

function _isDate(object_value) {
	// Matches m/d/yyyy
	//var regex = new RegExp(/^(?:(?:(?:0?[13578]|1[02])(\/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(\/|-)(?:29|30)))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:0?[1-9]|1[0-2])(\/|-)(?:0?[1-9]|1\d|2[0-8]))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(0?2(\/|-)29)(\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?(?:0[48]|[2468][048]|[13579][26]))$/);
	
	var regex = /^(0?[13578]|1[02])\/([12][0-9]|3[01]|0?[1-9])\/(19|20)(\d\d)|(0?[469]|11)\/([12][0-9]|30|0?[1-9])\/(19|20)(\d\d)|(0?2)\/(1[0-9]|2[0-9]|0?[1-9])\/(19|20)(\d\d)$/;

	if (object_value.match(regex)) { return true; }
	else { return false; }
}

//Checks to see if the form element to be validated exists
function _iExist(formElement) {

   if (formElement == null){      return false;   }
   else {      return true;   }
}

//Numeric value check funtion.
function _isNumeric(object_value) {			
	for (var i=0, value_length=(object_value.length), valid='0123456789'; i<value_length; i++) {
	    if (valid.indexOf(object_value.substring(i,i+1)) == -1) {
			return false;
            //alert('invalid data');
        }
    }
	return true;
}

//Same as _isNumeric but allows decimals (more than one, so trap that later)
function _isDecimalNumber(object_value) {
	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

	//check for numeric characters
	return objRegExp.test(object_value);

	//if (object_value)

	//for (var i=0, value_length=(object_value.length), valid='0123456789.'; i<value_length; i++) {
	//    if (valid.indexOf(object_value.substring(i,i+1)) == -1) {
	//		return false;
    //        }
    //    }
	//return true;
}

//Valid date check function. 
//Also includes calls to other functions, and will return an object expected error if they are not included in this file. 
//Date object_value, date_delimiter values required in function call.
function _checkdate(object_value, date_delimiter) {
  // Returns true if value is a date format or is NULL
  // otherwise returns false

  if (object_value.length == 0)
    return true;

  // Returns true if value is a date in the format is mm/dd/yyyy, where "/" is the date delimiter
  isplit = object_value.indexOf(date_delimiter);
  lisplit = object_value.lastIndexOf(date_delimiter);

  // the last bit of logic in the if statement below (isplit == 2 && object_value.charAt(3)) is included so we can
  // validate mm/yyy with these functions, too, simply by preappending eg., "11/", to a "mm/yyyy" date
  
  if (isplit == -1 || isplit == object_value.length || lisplit == 0 || isplit == 0 || (isplit == 2 && object_value.charAt(3) == date_delimiter))
    return false;
    
  sMonth = object_value.substring(0, isplit);
  isplit = object_value.indexOf(date_delimiter, isplit + 1);

  if (isplit == -1 || (isplit + 1 ) == object_value.length)
    return false;
  sDay = object_value.substring((sMonth.length + 1), isplit);
  sYear = object_value.substring(isplit + 1);

  if (!_checkinteger(sMonth)) // check month
    return false;
  else if (!_checkrange(sMonth, 1, 12)) // check month
    return false;
	
  else if ((sYear>99)&&(sYear<1753)) // fixing a strange bug
    return false;
	
  else if (!_checkinteger(sYear)) // check year
    return false;
  else if (!_checkrange(sYear, 0, 9999)) // check year
    return false;
  else if (!_checkinteger(sDay)) // check day
    return false;
  else if (!_checkday(sYear, sMonth, sDay)) // check day
    return false;
  else
    return true;
}


//Valid day function.  Used in _checkdate function.
//CheckYear, checkMonth, checkDay values required in function call.
function _checkday(checkYear, checkMonth, checkDay) {
  maxDay = 31;
  if (checkMonth == 4 || checkMonth == 6 || checkMonth == 9 || checkMonth == 11)
    maxDay = 30;
  else if (checkMonth == 2) {
    if (checkYear % 4 > 0)
      maxDay =28;
    else if (checkYear % 100 == 0 && checkYear % 400 > 0)
      maxDay = 28;
    else
      maxDay = 29;
  }
  return _checkrange(checkDay, 1, maxDay); // check day
}


//Valid integer function.  Used in _checkdate function.
function _checkinteger(object_value) {
  // Returns true if value is a number or is NULL
  // otherwise returns false  
  if (object_value.length == 0)
    return true;

  // Returns true if value is an integer defined as
  // having an optional leading + or -.
  // otherwise containing only the characters 0-9.
  var decimal_format = ".";
  var check_char;

  // The first character can be + -  blank or a digit.
  check_char = object_value.indexOf(decimal_format)

  // Was it a decimal?
  if (check_char < 1)
    return _checknumber(object_value);
  else
    return false;
}


//Valid number function.  Used in _checkdate function.
function _checknumber(object_value) {
  // Returns true if value is a number or is NULL
  // otherwise returns false  
  if (object_value.length == 0)
    return true;

  // Returns true if value is a number defined as
  // having an optional leading + or -.
  // having at most 1 decimal point.
  // otherwise containing only the characters 0-9.
  var start_format = " .+-0123456789";
  var number_format = " .0123456789";
  var check_char;
  var decimal = false;
  var trailing_blank = false;
  var digits = false;

  // The first character can be + - .  blank or a digit.
  check_char = start_format.indexOf(object_value.charAt(0))

  // Was it a decimal?
  if (check_char == 1) decimal = true;
  else if (check_char < 1)
    return false;
        
  // Remaining characters can be only . or a digit, but only one decimal.
  for (var i = 1; i < object_value.length; i++) {
    check_char = number_format.indexOf(object_value.charAt(i))
    if (check_char < 0)
      return false;
    else if (check_char == 1) {
      if (decimal) // Second decimal.
        return false;
      else
        decimal = true;
    }
    else if (check_char == 0) {
      if (decimal || digits) // ignore leading blanks
        trailing_blank = true;
    }
    else if (trailing_blank)
      return false;
    else
      digits = true;
  }

  // All tests passed, so...
  return true;
}


//Valid value range funtion.  Used in _checkdate function.
//Object_value, min_value, max_value required in function call.
function _checkrange(object_value, min_value, max_value) {
  // if value is in range then return true else return false
  if (object_value.length == 0)
    return true;

  if (!_checknumber(object_value))
    return false;
  else
    return (_numberrange((eval(object_value)), min_value, max_value));
  
  // All tests passed, so...
  return true;
}


//Valid time value function. 
function _checktime(object_value) {
  // Returns true if value is in time format or is NULL
  // otherwise returns false
  if (object_value.length == 0)
    return true;

  // Returns true if value is a date in the mm/dd/yyyy format
  isplit = object_value.indexOf(':');

  if (isplit == -1 || isplit == object_value.length)
    return false;

  sHour = object_value.substring(0, isplit);
  iminute = object_value.indexOf(':', isplit + 1);

  if (iminute == -1 || iminute == object_value.length)
    sMin = object_value.substring((sHour.length + 1));
  else
    sMin = object_value.substring((sHour.length + 1), iminute);

  if (!_checkinteger(sHour)) // check hour
    return false;
  else if (!_checkrange(sHour, 0, 23)) // check hour
    return false;
  if (!_checkinteger(sMin)) // check minutes
    return false;
  else if (!_checkrange(sMin, 0, 59)) // check minutes
    return false;

  // did they specify seconds
  if (iminute != -1) {
    sSec = object_value.substring(iminute + 1);
    if (!_checkinteger(sSec)) // check seconds
      return false;
    else if (!_checkrange(sSec, 0, 59)) // check seconds
      return false;
  }
  
  // All tests passed, so...
  return true;
}


//Valid number range function.  
//Object_value, min_value, max_value required in function call.
function _numberrange(object_value, min_value, max_value) {
  // check minimum
  if (min_value != null) {
    if (object_value < min_value)
      return false;
  }
  // check maximum
  if (max_value != null) {
    if (object_value > max_value)
      return false;
  }
  
  // All tests passed, so...
  return true;
}


//Function used to print out error message alert boxes.
function _onError(form_object, input_object, object_value, error_message) {
  alert(error_message);
  return false;
}

//Function used to determine if a form control has value.
function _hasValue(obj) {
     //alert("Running hasValue1.\n" + "type=" + obj.type + "\tvalue=" + obj.value + "\tlength=" + obj.value.length);
     //alert("Running hasValue from validat.\n" + "objValue=" + obj.value + "\ttype=" + obj.type + "\tlength=" + obj.value.length);
  var type;

  if (obj.type != undefined) { type = obj.type.toUpperCase(); }
  else { type = "RADIO"; }

  if (type == "TEXT" || type == "PASSWORD" || type == "HIDDEN" || type == "TEXTAREA") {	
	if (obj.value.length == 0)
      return false;
    else
      return true;
  }
  else if (type == "SELECT-ONE") {
    for (i=0; i < obj.length; i++) {
      if (obj.options[i].selected)
	  	if (obj.options[i].value.length != 0)
        	return true;
    }
    return false;  
  }  
  else if (type == "SELECT") {
    for (i=0; i < obj.length; i++) {
      if (obj.options[i].selected)
        return true;
    }
    return false;  
  }
  else if (type == "SINGLE_VALUE_RADIO" || type == "SINGLE_VALUE_CHECKBOX") {
    if (obj.checked)
      return true;
    else
      return false;
  }
  else if (type == "RADIO" || type == "CHECKBOX") {
	if (obj.length == undefined) { 
      if (obj.value.length != 0)
        return true;
	} 
	else {
      for (i=0; i < obj.length; i++) {
        if (obj[i].value.length != 0)
          return true;
	  }
    }
    return false;  
  }
}

//Function used to determine if a form control has a selected value.
function _hasSelected(obj) {
     //alert("Running hasValue1.\n" + "type=" + obj.type + "\tvalue=" + obj.value + "\tlength=" + obj.value.length);
     //alert("Running hasValue from validat.\n" + "objValue=" + obj.value + "\ttype=" + obj.type + "\tlength=" + obj.value.length);

  if (obj.type != undefined) { type = obj.type.toUpperCase(); }
  else { type = "RADIO"; }

  if (type == "SELECT-ONE") {
    for (i=0; i < obj.length; i++) {
      if (obj.options[i].selected)
        return true;
    }
    return false;  
  }  
  else if (type == "SELECT") {
    for (i=0; i < obj.length; i++) {
      if (obj.options[i].selected)
        return true;
    }
    return false;  
  }
  else if (type == "SELECT-MULTIPLE") {
    for (i=0; i < obj.length; i++) {
      if (obj.options[i].selected)
        return true;
    }
    return false;  
  }
  else if (type == "SINGLE_VALUE_RADIO" || type == "SINGLE_VALUE_CHECKBOX") {
    if (obj.checked)
      return true;
    else
      return false;
  }
  else if (type == "RADIO" || type == "CHECKBOX") {

	if (obj.length == undefined) { 
      if (obj.checked)
        return true;
	}
	else {
      for (i=0; i < obj.length; i++) {
        if (obj[i].checked)
          return true;
	  }
    }
    return false;  
  }
}

function _validate_phone(val, formObj){

//This function is passed a value and a form object (input field the original case).
//It removes any non integer characters and creates a new, integer-only string.
//It passes 10-digit, integer-only, strings on to the _transform_phone function.
//It returns true, but does not transform further, any international phone number (that is, an integer 
// with more than 10 digits).
//It fails any string that, after having its non numbers removed, is less than 10 digits  

if(val.length < 10){
    return false;
}

else {
   newString = val;
   var cnt = 0;
   for(var i=1, value_length = (val.length), valid='0123456789';i<=value_length;i++){
      cnt = cnt+1;
      if (valid.indexOf(newString.substr(cnt-1,1))==-1){
	tempA = newString.substr(0,cnt-1);
	tempB = newString.substr(cnt,newString.length);
	newString = tempA.concat(tempB);
	cnt = cnt-1;
        if(newString.length<10) {
          return false;
        }
      }
    }
    if(newString.length==10) {newString = _transform_phone(newString);}else{}
    if (newString.length>10) {}else{}
    formObj.value = newString;
} //end else
  return true;
}



function _transform_phone(thestring){

//Used with validatePhone, this function assumes we have a 10 digit number passed to us
//creates (xxx)xxx-xxxx

var a = "(";
var b = ")";
var c = " ";
var d = "-";

var thefront = thestring.substr(0,3);
var themiddle = thestring.substr(3,3);
var theend = thestring.substr(6,4);
var newstring = a.concat(thefront,b,c,themiddle,d,theend);
return newstring;
}


function _swap_delimiters(val, formObj, delim_from, delim_to) {

//All we're really doing is replacing the first two occurances of "delim_from" e.g., "-" with "delim_to" e.g., "/"
//For example, if we want to reformat mm-dd-yyyy to mm/dd/yyyy.
//Validation takes place outside the context of this function.

newDate=""
if (val.length == 0) {return;}

else {

  split1 = val.indexOf(delim_from);
  split2 = val.indexOf(delim_from, (split1+1));

  if ((split1==-1)||(split2==-1))return;

  sMonth = val.slice(0, (split1));
  sDay = val.slice((split1 + 1), (split2));
  sYear = val.slice(split2+1);

  newDate = sMonth.concat(delim_to,sDay,delim_to,sYear);
  formObj.value = newDate;
}
return;
}

//this function checks to see if the user enters the same text string into two form fields.

function _confirm_Password(password,confirmpassword) {
	if (password.value != confirmpassword.value) { return false; }
	else { return true; }}


//this function checks to see if the user entered an email that passes the minimum reality checks
//i.e., @ sign is present and not the first character; period is present and not the first character
function _valid_Email(formObj){
if (((!(formObj.value == ""))) &&
	(((formObj.value).indexOf("@")<1)||
	((formObj.value).indexOf(".")<1))){
	alert("Please enter a valid email address.");
	return false;
	}
	else { return true;  }}













