/* act_validation.cfm ******************************************************************************************
*
*	DESCRIPTION:		Javascript Validation routines
*
*	ARGUMENTS:			none
*
*	INCLUDES:			none
*						
*	CUSTOM TAGS:		none
*						
*	AUTHOR:				Marc Amick
*	DATE:	  			3/07/2000 -  Created.
*****************************************************************************************************************/
var s_error_msg = '';
var firstCtrl = null;
//
// Displays any errors that have been collected for this form
//
function showErrors(){
	window.alert('There was at least one data entry error on the current form. Please correct the following fields and try again:\n\n' + s_error_msg);
	if (firstCtrl != null) {
		firstCtrl.focus();
	}
	s_error_msg = '';
	firstCtrl = null;
}

//
// Display any errors with alternative message heading
//
function showErrorsAlt(showDefaultMsg, altMsg){
	if (showDefaultMsg)
		showErrors();
	else {
		window.alert(altMsg + ':\n\n' + s_error_msg);
		s_error_msg = '';
		firstCtrl = null;
	}
}

//
// Appends a new error to the list of errors for this form
//
function appendError(s_err) {
	s_error_msg += '\n' + s_err;
}

//
// Flags the current field with error colors if there was an error
// otherwise sets it to its normal state
//
function setErrorColor(ctrl, b_err) {
	//
	// if user is running MS IE then set colors of the control in error
	//
	//
	if (b_err) {
		ctrl.className = "inputBad";
		if (firstCtrl == null)
			firstCtrl = ctrl;
	}
	else {
		ctrl.className = "input";
	}
}

function hasSpace(ctrl,name) {
  var ret = false;
  var beSpace = false;
  for (ix=0;ix < ctrl.value.length;ix++) {			
		if (ctrl.value.charAt(ix) == ' '){
			beSpace = true;
			break;		
		}
  }
  if (beSpace) {
	 appendError(name + ' can not have space(s).');
	 ret = true;
  }	
  setErrorColor(ctrl,ret);
  return ret;
}

//
// Returns true if the combo box passed to it has a value other than blanks selected
//
function isComboFilled(ctrl,name) {
	var ret = true;
	var ix = 0;
	var s_test = "";
	//
	// fail if there is not even a selected item
	//
	if (ctrl.selectedIndex==-1) {
  		ret = false;
	}
	else {
		s_test = ctrl.options[ctrl.selectedIndex].text;
		//
		// Count the spaces
		//
		for (ix=0;ix < s_test.length;ix++) {
			if (s_test.charAt(ix) != ' ') {
				break;
				}
		}
	}
	//
	// if its all spaces, or the length is 0 then its not filled
	//
	if ((ix == s_test.length) || (s_test.length==0)) {
		appendError(name + ' is a required field.');
   		ret = false;
	}
  	setErrorColor(ctrl,!ret);
	return ret;
}

//
// Returns true if the ctrl is filled with at least l_minlen chars and no more than l_maxlen chars
//
function isFilled(ctrl,name,l_minlen,l_maxlen,required) {
  var ret = true;
  
  if(isEmpty(ctrl)){
	 appendError(name + ' is a required field.');
   	 ret = false;
  }
  else {
  	s_val = ctrl.value;
	if (s_val.length < l_minlen) {
		if (required) 
		 	appendError(name + ' is a required field which must have at least ' + l_minlen + ' characters.');
		else
		 	appendError(name + ', if entered, must have at least ' + l_minlen + ' and no more than ' + l_maxlen + ' characters.');
		ret = false;
	}
	else {
		if (s_val.length > l_maxlen) {
	 		appendError(name + ' is a required field which must have no more than ' + l_maxlen + ' characters.');
			ret = false;
		}
	}
  }
  setErrorColor(ctrl,!ret);
  return ret;
}

function isYrsMths(ctl_yrs,ctl_mths,name_yrs,name_mths,name_both) {
	 
	var ret = true;
	//
	// check years and months seperately, then make sure the total time is
	// greater than 0
	//
	setErrorColor(ctl_mths,false);
	setErrorColor(ctl_yrs,false);
	if (isNumeric(ctl_yrs,name_yrs) == false) {
		ret = false;
	}
	if (isNumericMinMax(ctl_mths,name_mths,0,11) == false) {
		ret = false;
	}
	if (ret) {
		n_time = 0;
		if (isEmpty(ctl_yrs)==false) {
			n_time = n_time + (ctl_yrs.value * 12);
		}
		if (isEmpty(ctl_mths)==false) {
			n_time = n_time + ctl_mths.value;
		}
		if (n_time <= 0) {
			appendError(name_both + ' must be entered');
			setErrorColor(ctl_mths,true);
			setErrorColor(ctl_yrs,true);
			ret = false;
		}
	}
	return ret;
}


function isPhoneNumber(ctl1,ctl2,ctl3,name) {
	var ret = true;
	//
	// make sure phone # is numeric
	//
	ret = isNumeric(ctl1,name + ' (1)');
	if (ret) {
		ret = isNumeric(ctl2,name + ' (2)');
		if (ret) {
			ret = isNumeric(ctl3,name + ' (3)');
		}
	}
	if (ret) {
		//
		// make sure lengths are correct
		//
		if ((ctl1.value.length != 3) ||  (ctl2.value.length != 3) || (ctl3.value.length != 4)){
			ret = false;
			appendError(name + ' is not a properly formed phone number (999-999-9999)');
			setErrorColor (ctl1,!ret);
			setErrorColor (ctl2,!ret);
			setErrorColor (ctl3,!ret);
		}
	}

	return ret;
}
//
// Returns true the control contains a numeric (integer) value
//
function isNumericMinMax(ctrl,name,n_min,n_max) {
	var ret = isNumeric(ctrl,name)
	
	if (ret) {
		x = ctrl.value;

		if (x < n_min || x > n_max) {
			appendError(name + " " + 'Should be a number between ' + n_min + ' and ' + n_max);
			ret = false;
		}
	}
	
	setErrorColor (ctrl,!ret);
	return ret;
}

//
// Returns true the control contains a numeric (integer) value
//
function isNumeric(ctrl,name) {
  var nr1 = ctrl.value;
  var ret = true;
  var flg=0;
  var str="";
  var spc=""
  var arw="";
  var cmp="0123456789";
  
   // loop through each character in the string
  for (var i=0;i<nr1.length;i++){
   
   tst=nr1.substring(i,i+1)
   
	//
	// if the character is not in the list of allowable chars
	// then fail it
	//
   if (cmp.indexOf(tst)==0){
    flg++;
    str+=" "+tst;
    spc+=tst;
    arw+="^";
   }
   else{arw+="_";}
  }
  //
  // display an error message to the user if it failed
  //
  if (flg!=0){
   if (spc.indexOf(" ")>-1) {
    str+=" and a space";
    }
   appendError(name + " must be a number. I found " +flg+" unacceptable characters: "+str+".");
	ctrl.select();
	ctrl.focus(); 
	ret = false;
  }
  setErrorColor(ctrl,!ret);
  return ret;
}

//
// Returns true the control contains a numeric (floating point) value
//
function isFloatingPointMinMax(ctrl,name,n_min,n_max) {
	var ret = isFloatingPoint(ctrl,name);

	if (ret) {
		x = parseFloat(ctrl.value);

		if (! isNaN(x) ){
			if (x < n_min || x > n_max) {
				appendError(name + " " + 'Should be a number between ' + n_min + ' and ' + n_max);
				ret = false;
			}
		}
		else {
			appendError(name + " " + 'Should be a number between ' + n_min + ' and ' + n_max);
			ret = false;
		}
}
	
	if (ret == false){
		setErrorColor (ctrl,!ret);
	}
	return ret;
}

//
// Returns true the control contains a numeric (floating point) value with precision
//
function isFloatingPointMinMaxPrecision(ctrl,name,n_min,n_max, precision) {
	var ret = isFloatingPoint(ctrl,name);

	if (ret) {
		x = parseFloat(ctrl.value);
		
		if (! isNaN(x) ){
			
			//loop thru and count number of digits after decimal point
			numOfDecimal = 0;
			afterDecimal = -1;
			for (ix=0;ix < ctrl.value.length;ix++) {
				if (numOfDecimal == 1)
					afterDecimal = afterDecimal +1;
					
				if (ctrl.value.charAt(ix) == '.')
					numOfDecimal = numOfDecimal +1;
			}
			if (afterDecimal >= precision) {
				if (precision == 0)
					appendError(name + ' must be a whole number(no decimal positions).');
				else
					appendError(name + " " + 'can not exceed ' + precision + ' decimal positions.');
				ret = false;
			}
			
			if (x < n_min || x > n_max) {
				appendError(name + " " + 'should be a number between ' + n_min + ' and ' + n_max+ '.');
				ret = false;
			}
		}
		else {
			appendError(name + " " + 'should be a number between ' + n_min + ' and ' + n_max + '.');
			ret = false;
		}
}
	
	if (ret == false){
		setErrorColor (ctrl,!ret);
	}
	return ret;
}

//
// Returns true the control contains a numeric (floating point) value
//
function isFloatingPoint(ctrl,name) {
  var nr1 = ctrl.value;
  var ret = true;
  var flg=0;
  var str="";
  var spc=""
  var arw="";
  var dot_cnt=0;
  var cmp="0123456789.";
  
   // loop through each character in the string
  for (var i=0;i<nr1.length;i++){
   tst=nr1.substring(i,i+1)

	//
	// allow up to one dot
	//   
   if (tst == '.')
   	dot_cnt++;
	//
	// if the character is not in the list of allowable chars
	// or more than one exists fail it
	//
   if ( (cmp.indexOf(tst)==0) || (tst == '.' && dot_cnt > 1) ){
    flg++;
    str+=" "+tst;
    spc+=tst;
    arw+="^";
   }
   else{arw+="_";}
  }
  //
  // display an error message to the user if it failed
  //
  if (flg!=0){
   if (spc.indexOf(" ")>-1) {
    str+=" and a space";
    }
   appendError(name + " must be a number. I found "
   +flg+" unacceptable characters: "+str+".");
	ctrl.select();
	ctrl.focus(); 
	ret = false;
  }
  setErrorColor(ctrl,!ret);
  return ret;
}

//
// Return true if the control's value is empty
//
function isEmpty(ctrl) {
  var ret = false;
  if(ctrl.value == "" || ctrl.value == null){
   	 ret = true;
  }
  return ret;
}



//##########################date functions #########################
function fourDigitYear(year){
	if (year < 1000 )
		return 1900 + year;
	else
		return year;
}

/***********************************************************************************
 *
 * isDate - evaluates the args: month,day,year and if they represent a valid date, 
 *		    returns true.  Otherwise, adds a message to the error dlg and returns false.
 *
 * Arguments - month - number representing month.
 *			   day   - number representing day.
 *			   year  - number representing year.
 *			   name  - string describing the field in question (displayed in error dlg)
 *			   ctrl  - REQUIRED - ctrl to highlight on error.
 *			   ctrl2 - OPTIONAL - ctrl to highlight on error if date is entered in several flds.
 *			   ctrl3 - OPTIONAL - ctrl to highlight on error if date is entered in several flds.
 *
 * Assumptions - month is 1 based.
 *
 * History     - 4/25/2000   Mike Brune - Created
 ************************************************************************************/
 function isDate(month,day,year,name,ctrl,ctrl2,ctrl3){
 	var temp_date = new Date(year,month-1,day);
	var ret=false;
	
	if ((month-1 == temp_date.getMonth()) &&
	    (day == temp_date.getDate()) &&
		(year == fourDigitYear(temp_date.getYear()))){
			ret = true;
		}
	else{
		appendError(name + " Is invalid or not well formed (mm/dd/yyyy) "	);
		ret = false;
	}
	
	 setErrorColor(ctrl,!ret);
	 if (isDate.arguments.length >= 6)
	 	setErrorColor (ctrl2,!ret);
	 if (isDate.arguments.length == 7)
	 	setErrorColor (ctrl3,!ret);
		
	 return ret;
 }

 function isDateOlderThanNow(month,day,year,name,ctrl,ctrl2,ctrl3) {
 	var ret = false;
	
	ret = isDate(month,day,year,name,ctrl,ctrl2,ctrl3);
	if (ret) {
		//
		// make sure the date is older than now
		//
		var this_date = new Date(year,month-1,day);
		var now = new Date();
		
		if (now <= this_date) {
			appendError(name + " must be a less than today's date");
			ret = false;
		} 
	}
	
	setErrorColor(ctrl,!ret);
	if (isDateOlderThanNow.arguments.length >= 6)
	 	setErrorColor (ctrl2,!ret);
	if (isDateOlderThanNow.arguments.length == 7)
	 	setErrorColor (ctrl3,!ret);
	
	return ret;
 }

 /*******************************************************************************************
 *
 * isInvalid - If you have decided that a field is not valid, Call this routine to color the
 *			   the control, and add the message to the error message box.
 *
 * ctrl 	 - ctrl to be highlighted
 * name		 - name of the ctrl. (used for the error message)
 * message	 - describe why this ctrl is not valid.
 * valid	 - true/false.  Needs to be set and cleared by the user.
 *
 * 5/2/2000 - Created - Mike Brune
 ********************************************************************************************/
 function isInvalid(ctrl,name,message,valid)
 {
 	appendError(name + " " + message);
	setErrorColor (ctrl,valid);
 }
 

function isEmail(ctrl,name) {
 	var sVal = ctrl.value;
	var ret = false;
	
	if (sVal.indexOf("@")) {
		if (sVal.length > 2) {
			ret = true;
		}
	}

	if (!ret) {
		appendError(name + " is not a valid email address.");
	}
	
	setErrorColor (ctrl,!ret);
	return ret;
 }
