/* --------------------------------------------------------------------------------------------
Author: Brent McClintock
Date: 	11/28/2001  

Royalty checks in amount of $10 should be paid to above for each use.  :-)

Purpose:
	When using Cold Fusion's date validation on CFINPUT's, you lose the onKeyPress(), onKeyUp(), and onKeyDown()
	functions normally available on FORM elements.  If needed, use validateDate() in the onSubmit.

Example:
	<FORM name="theform" ... onSubmit="javascript: if( validateDate('theform', 'sampleDate', 0)  .... ){ return true; } else { return false; }">
		<input type="text" name="sampleDate" onKeyPress="javascript: addSlash(this);">
	</FORM>
	( also see /cfdocs/legis/CT/add_citat.cfm )
	
Includes:
	1) addSlash(object)
	2) validateDate(formname, fieldname, nullable)
	
	...MODIFIED COLD FUSION VALIDATION FUNCTIONS...
	3) checkdate(date)
	4) checkday(checkYear, checkMonth, checkDay)
	5) checkinteger(object_value)	
	6) numberrange(object_value, min_value, max_value)	
	7) checknumber(object_value)	
	8) checkrange(object_value, min_value, max_value)	
	9) cnvtDate(date) - added 6/03 J. Rondeau
	10) validateTime (formname,fieldname, nullable) - added 8/03 SRB 
	11) validateTimeById(fieldID, nullable) - added 9/10/03 BAM
	12) checktime(time) - added 9/10/03 BAM

CHANGE CONTROL:
	12152003	- error in checktime() didn't allow any time w/ ZERO in the minutes portion
--------------------------------------------------------------------------------------------- */

	// used to add slashes in date fields at the 2nd and 5th positions (MM/DD/YY)
	// call addSlash() in the onKeyPress() or onKeyUp() form attribute
	function addSlash(obj)
	{
		var myObj = document.getElementById(obj.id);
		var field_len = myObj.value.length;
		
		if( field_len == 2 || field_len == 5 )
		{  myObj.value += '/';  }	//add a slash
	}
//*****************************************************************************
	// Validates the date in the given document.forms.FORMNAME.FIELDNAME
	// formname and fieldname must be enclosed in aphostrophies
	// NULLABLE = 0 if field is to be required
	// NULLABLE = 1 if field is NOT required
	// Returns true if in MM/DD/YYYY or MM/DD/YY format,
	// Else returns false and focus to the field in question
	function validateDate(formname,fieldname,nullable)
	{	
		// alert("formname: "+formname+"\nfieldname: "+fieldname+"\nnullable: "+nullable);
		var myVal = eval("document.forms."+formname+"."+fieldname+".value");
		
		if( myVal.length == 0 )
		{ 
			if( nullable == 0 )		//cannot be null, a required date
			{
				alert("You must enter a date in the format MM/DD/YYYY .");
				eval("document.forms."+formname+"."+fieldname+".focus()");			
				return false;
			}
			else
			{ return true; }
		}
		else
		{
			if( checkdate(myVal) == false )
			{ 
				alert("Dates must be in the format MM/DD/YYYY .");
				eval("document.forms."+formname+"."+fieldname+".focus()");
				eval("document.forms."+formname+"."+fieldname+".select()");
				return false;
			}
			else
			{ return true; }
		}
	}
//*****************************************************************************
	// added on 12172002 for LDPinfo
	//similar to above, but takes ID instead of field NAME
	// NULLABLE = 0 if field is to be required
	// NULLABLE = 1 if field is NOT required
	// Returns true if in MM/DD/YYYY or MM/DD/YY format,
	// Else returns false and focus to the field in question 
	function validateDateById(fieldID,nullable)
	{	
		var obj = document.getElementById(fieldID);
		var myVal = obj.value;
		
		if( myVal.length == 0 )
		{ 
			if( nullable == 0 )		//cannot be null, a required date
			{
				alert("You must enter a date in the format MM/DD/YYYY .");
				obj.focus();
				return false;
			}
			else
			{ return true; }
		}
		else
		{
			if( checkdate(myVal) == false )
			{ 
				alert("Dates must be in the format MM/DD/YYYY .");
				obj.focus();
				obj.select();
				return false;
			}
			else
			{ return true; }
		}
	}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
	// BELOW ARE MODIFIED COLD FUSION SCRIPTs FOR VALIDATING DATES

	//Returns true if value is a date format
	//otherwise returns false	
	function checkdate(object_value)
	{
	 // alert("value =  " + object_value);

	    if (object_value.length == 0) // NULL is not allowed
	        return false;
	
	    //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;
	
	    sMonth = object_value.substring(0, isplit);
	
		if (sMonth.length == 0)
	        return false;
	
		isplit = object_value.indexOf('/', isplit + 1);
	
		if (isplit == -1 || (isplit + 1 ) == object_value.length)
			return false;
	
	    sDay = object_value.substring((sMonth.length + 1), isplit);
	
		if (sDay.length == 0)
	        return false;
	
		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 (!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;
	}
//*****************************************************************************************
	// validates that the day is proper for the given month
	// called in checkdate()
	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
	}
//******************************************************************************************
	// validates that the object being considered is an integer
	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;
	}
//******************************************************************************************
	//validates that a number is within a certain range
	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 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)	
					trailing_blank = true;
	        // ignore leading blanks
	
			}
		        else if (trailing_blank)
				return false;
			else
				digits = true;
		}	
	    //All tests passed, so...
	    return true
	}
//*****************************************************************************************
	//validates that a value lies within a given range
	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;
	}
//******************************************************************************************* 
// this takes a date in format mm/dd/yy  (# of digits in mo, day can be 1 or 2 - yr can be 2 or 4)
//  and converts it to a string -  month abbreviated (sometimes) mmm. dd, yyyy   (no leading zero on days)  
// done mostly for history apps
//  validates date prior to conversion   
// author J.R.  date: 6/03
function cnvtDate(mdyDate) {

if (checkdate(mdyDate) == true) {
  var Months = ['Jan.','Feb.','March',
      'April','May','June','July','Aug.','Sept.',
      'Oct.','Nov.','Dec.'];
   var isplit = mdyDate.indexOf('/');
   var month = mdyDate.substring(0, isplit);
   isplit = mdyDate.indexOf('/', isplit + 1);
   var day1 = mdyDate.substring((month.length + 1), isplit);
    if(day1.substr(0,1) == '0') {
	  var day = day1.substr(1,1);
	  } // end if day starts w/ 0
	 else { 
	  var day = day1;
	 } // end else day is 2 digits
   var yy = mdyDate.substring(isplit + 1);
   //alert ("year is " + yy); 
    month =   month - 1;
   var year = (yy < 1000) ? yy + 2000 : yy;
   var str = Months[month] + " "  + day +", " + year ;
    return  str;
  }  // end if checkDate = true
else{ 
 alert("check Dte routine returns NG");
 return false;
 } // end else
} // end function


//*****************************************************************************
// added on 08132003 by SRB
// NULLABLE = 0 if field is to be required
// NULLABLE = 1 if field is NOT required
// Returns true if date valid,
// Else returns false and focus to the field in question 	

	function validateTime (formname,fieldname,nullable) {

 	var temp = eval("document.forms."+formname+"."+fieldname+".value.toUpperCase()");

	if( temp.length == 0 )
		{ 
			if( nullable == 0 )		//cannot be null, a required date
			{
				alert("Time is Required.");
				eval("document.forms."+formname+"."+fieldname+".focus()");			
				return false;
			}
			else
			{ return true; }
		}
	
	if (temp.indexOf(":") >0) {
		temp = temp.substring(0, temp.indexOf(":")) + temp.substring((temp.indexOf(":") +1), temp.length)
	}
	
	if (temp.indexOf("A") > 0)	{
		var AMPos = temp.indexOf("A")
		var justTime = temp.substring(0, AMPos)
		while (justTime.charAt(justTime.length - 1) == " ") {
			justTime = justTime.substr(0, justTime.length - 1)
		}
		numchk = parseInt(justTime, 10)
		if (isNaN(numchk)) {
			alert("You have input illegal Time data!")
			fieldname.focus()
			return false
		}
		if (justTime.length < 3) {
			justTime = justTime + "00"
		}
		if (justTime.length > 4 || eval(justTime) >1259) {
			alert("You have input illegal Time data!")
			fieldname.focus()
			return false
		}else {		
			var x = justTime.substring(0, justTime.length-2) + ":" + justTime.substr(justTime.length-2) + "AM";
			eval("document.forms."+formname+"."+fieldname+".value='"+x+"'");
		}
	}else if (temp.indexOf("P") > 0) {
		var PMPos = temp.indexOf("P")
		justTime = temp.substring(0, PMPos)
		while (justTime.charAt(justTime.length - 1) == " ") {
			justTime = justTime.substr(0, justTime.length - 1)
		}
		numchk = parseInt(justTime, 10)
		if (isNaN(numchk)) {
			alert("You have input illegal Time data!")
			fieldname.focus();			
			return false;
		}
		if (justTime.length < 3) {
			justTime = justTime + "00"
		}
		if (justTime.length > 4 || eval(justTime) > 1259) {
			alert("You have input illegal Time data!")
			fieldname.focus()
			return false
		}else {
			var x = justTime.substring(0, justTime.length-2) + ":" + justTime.substr(justTime.length-2) + "PM"
			eval("document.forms."+formname+"."+fieldname+".value='"+x+"'");
		}
	}else {
		numchk = parseInt(temp, 10)
		if (isNaN(numchk)) {
			alert("You have input illegal Time data!")
			fieldname.focus()
			return false
		}
		if (temp.length < 3) {
			temp = temp + "00"
		}
		justTime = temp
		if (justTime.length > 4 || eval(justTime) > 1259) {
			alert("You have input illegal Time data!")
			fieldname.focus()
			return false
		}else {
			if (eval(temp) >= 700 && eval(temp) < 1200) {
				merid = "AM"
			}else {
				merid = "PM"
			}
			var x = temp.substring(0, temp.length-2) + ":" + temp.substring(temp.length-2, temp.length) + merid
			eval("document.forms."+formname+"."+fieldname+".value='"+x+"'");
		}
	}
	
}
//*****************************************************************************
// added on 09102003 by BAM
// NULLABLE = 0 if field is to be required
// NULLABLE = 1 if field is NOT required
// Returns true if time is in format HH:MM TT,
// Else returns false and focus to the field in question 	
//-------- A VERY STRICT VALIDATION, not as friendly as 'validateTime()'
	function validateTimeById(fieldID,nullable)
	{	
		var obj = document.getElementById(fieldID);
		var myVal = obj.value;
		
		if( myVal.length == 0 )
		{ 
			if( nullable == 0 )		//cannot be null, a required date
			{
				alert("You must enter a time in the format HH:MM TT .");
				obj.focus();
				return false;
			}
			else
			{ return true; }
		}
		else
		{
			if( checktime(myVal) == false )
			{ 
				alert("Times must be in the format HH:MM TT .");
				obj.focus();
				obj.select();
				return false;
			}
			else
			{ return true; }
		}
	}		
//*****************************************************************************
	//used in conjuction w/ validateTimeById()	
	// a strict validation of the format 'HH:MM TT'
	function checktime(object_value)
	{
	    if (object_value.length == 0) // NULL is not allowed
	        return false;
	
	    //Returns true if value is a time in the HH:MM TT format
		//------------------------------------------
		//get hours and validate
		isplit = object_value.indexOf(':');
	
		if (isplit == -1 || isplit == object_value.length || isplit == 0)
			return false;
	
	    hrs = object_value.substring(0, isplit);
	
		if( hrs.length == 0 )
	    { return false; }
		//------------------------------------------
		//get mins and validate
		isplit = object_value.indexOf(' ', isplit + 1);
	
		if( isplit == -1 )
		{ return false; }
	
	    mins = object_value.substring((hrs.length + 1), isplit);
	
		if( mins.length == 0 )
	    { return false; }
		//------------------------------------------
		ampm = Trim(object_value.substring(isplit + 1));
		//------------------------------------------
		if (!checkinteger(hrs)) //check hours
			return false;
		else
		if (!checkrange(hrs, 1, 12)) //check hours
			return false;
		else
		if (!checkinteger(mins)) //check mins
			return false;
		else
		if (!checkrange(mins, 0, 59)) //check mins	(12152003)
			return false;
		else
		if (ampm.toUpperCase() != "AM" && ampm.toUpperCase() != "PM") //check TT
			return false;
		else
			return true;
	}
//*****************************************************************************


