
// =================isFormatDate======================================
// This is a trimmed down version for format date only. 
// Full version, which can do tons more for date, time, comparing dates etc
// can be found at WWW: http://www.mattkruse.com/ or in J: toolbox (I hope)
//
// ===================================================================
// Field        | Full Form          | Short Form
// -------------+--------------------+-----------------------
// Year         | yyyy (4 digits)    | yy (2 digits), y (2 or 4 digits)
// Month        | mm (2 digits)      | m (1 or 2 digits)
// Day			| dd (2 digits)      | d (1 or 2 digits)
// ------------------------------------------------------------------

//	Format can be any combo above with any of the following separators:
//		1.	'-' (ie m-d-y) or 
//		2.	'/' (ie mm/d/yyyy) or 
//		3.	none (ie mmddyyyy) or
//		4.	whatever you want as the separator (ie mmEddEyyyy) ;)
//		It all depends what is passed to isFormatDate as the 'format' variable

// ------------------------------------------------------------------
// * isFormatDate ( date_string, format_string. form )
//	call function example: isFormatDate(form.name,'format',form)
//		-where form.name (theForm.dob) is the field to check
//		and format (mm-dd-yyyy) is the format to check against
// Returns true if date string matches format of format string and
// is a valid date. Else returns false.
// It is recommended that you trim whitespace around the value before
// passing it to this function, as whitespace is NOT ignored!
// ALSO, cannot have the current or future year. thisYear var must be defined on html page.
// ------------------------------------------------------------------
function isFormatDate(field,format,form) {
	var val= trimVal(field.value); //trimVal function very bottom of this page
	var date=getDateFromFormat(val,format);
		document.getElementById('month').className = '';
		document.getElementById('day').className = '';
		document.getElementById('year').className = '';
		
	if (date==0) { 
		//if(val== '--'| val==''){
		//	val='A <em>blank date</em>';
		//}
		//commented code removes the birthdate message if left entirely blank. Else statement is included in this.
		//if(val =='A <em>blank date</em>'){
			//return false;
		//} //end if blank
		//else{
		msg = msg + '<li>Please enter a valid Date of Birth.</li>';
		//alert(form.day.classname);
		document.getElementById('month').className = 'RequiredSelectText';
		document.getElementById('day').className = 'RequiredSelectText';
		document.getElementById('year').className = 'RequiredSelectText';
		field.value = ''; //setting to blank will allow other 'required' functions to flag date field as invalid
		return false;
		//}//end else
	}
	return true;
	}

// ------------------------------------------------------------------
// Utility functions for parsing in getDateFromFormat()
// ------------------------------------------------------------------
function _isInteger(val) {
	var digits="1234567890";
	for (var i=0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i))==-1) { return false; }
		}
	return true;
	}
function _getInt(str,i,minlength,maxlength) {
	for (var x=maxlength; x>=minlength; x--) {
		var token=str.substring(i,i+x);
		if (token.length < minlength) { return null; }
		if (_isInteger(token)) { return token; }
		}
	return null;
	}
	
// ------------------------------------------------------------------
// * getDateFromFormat( date_string , format_string )
//
// This function takes a date string and a format string. It matches
// If the date string matches the format string, it returns the 
// getTime() of the date. If it does not match, it returns 0.
// ------------------------------------------------------------------
function getDateFromFormat(val,format) {
	val=val+"";
	format=format+"";
	var i_val=0;
	var i_format=0;
	var c="";
	var token="";
	var token2="";
	var x,y;
	var date=1;
	var ampm="";

	while (i_format < format.length) {
		// Get next token from format string
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		// Extract contents of value based on format token
		if (token=="yyyy" || token=="yy" || token=="y") {
			if (token=="yyyy") { x=4;y=4; }
			if (token=="yy")   { x=2;y=2; }
			if (token=="y")    { x=2;y=4; }
			year=_getInt(val,i_val,x,y);
			
			//thisYear is defined on the xsl page as the current year
			if (year==null || year >= thisYear) { return 0; }
			i_val += year.length;
			if (year.length==2) {
				if (year > 70) { year=1900+(year-0); }
				else { year=2000+(year-0); }
				}
			}
		else if (token=="mm"||token=="m") {
			month=_getInt(val,i_val,token.length,2);
			if(month==null||(month<1)||(month>12)){return 0;}
			i_val+=month.length;}
		else if (token=="dd"||token=="d") {
			date=_getInt(val,i_val,token.length,2);
			if(date==null||(date<1)||(date>31)){return 0;}
			i_val+=date.length;}
		else {
			if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
			else {i_val+=token.length;}
			}
		}
	// If there are any trailing characters left in the value, it doesn't match
	if (i_val != val.length) { return 0; }
	// Is date valid for month?
	if (month==2) {
		// Check for leap year
		if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
			if (date > 29){ return 0; }
			}
		else { if (date > 28) { return 0; } }
		}
	if ((month==4)||(month==6)||(month==9)||(month==11)) {
		if (date > 30) { return 0; }
		}
	return;
	}

// ------------------------------------------------------------------
// trimVal strips spaces outa variable
// ------------------------------------------------------------------

function trimVal( strValue ) {
 var objRegExp = /^(\s*)$/;
    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}
