/*
=======================================================================
Copyright W3Now Web Design, Inc.  
W3WebForms.com    W3Now.com   W3SiteBuilder.com   W3WebTemplates.com
This information must remain in tact as presented here

Visit http://W3WebForms.com/  
to easily generate functional online forms for your web pages. 

The form information can be emailed to you or stored in a CSV file in your web site directory
Making an online form for your web page is easy...just log in, make a few entries, then copy the 
generated HTML code into your web page for a fully functional, spam-proof online form 

Forms generated by W3WebForms.com are spam-proof and fully functional.

=======================================================================
*/

function takeYear(theDate) {
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900; 
	//(Why 38? Because Epoch Time will end in 2038. You can also use (y > 69) ? 1900: 2000; because in the Epoch Time system no date can be before 1970). 
	return y;
}


function validDate(MYDATESTRING) { // mm/dd/yy or mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
	delimArray=new Array();
	delimIndex=0;
	if (MYDATESTRING.length > 10) {
		return false;
	} 
	delimIndex = 0;
	validChars = "0123456789/-.";
	validDelimiterChars="/-.";
	for (z=0; z<MYDATESTRING.length; z++) { 
		charOK=false;
		for (i=0; i<validChars.length; i++) {
				if (MYDATESTRING.charAt(z)== validChars.charAt(i) ) {
					charOK=true;
					break;
				} 
		}
		if (!charOK) { return false; }
		for (i=0; i<validDelimiterChars.length; i++) {
				if (MYDATESTRING.charAt(z)== validDelimiterChars.charAt(i) ) { 
					delimIndex++;
					delimArray[delimIndex]=MYDATESTRING.charAt(z);
					break;
				} 
		} 
	} 
	if ( delimIndex != 2 )  {  return false; }
	if (delimArray[1] != "/")  MYDATESTRING=MYDATESTRING.replace(delimArray[1], "/");
	if (delimArray[2] != "/")  MYDATESTRING=MYDATESTRING.replace(delimArray[2], "/");
	  
	monthfield=MYDATESTRING.split("/")[0];
	dayfield=MYDATESTRING.split("/")[1];
	yearfield=MYDATESTRING.split("/")[2]; 
	
	if (yearfield.length ==1 ) yearfield = "200" + yearfield;   
	if (yearfield.length ==2 ) yearfield = "20" + yearfield;   
	if (yearfield.length !=4) return false; 
	
	if (dayfield < 1 || dayfield > 31) return false;
	if (monthfield < 1 || monthfield > 12) return false;
	if (yearfield <= 1994 || yearfield > 2032) return false;
	
	maxdays=31;
	if (monthfield==9 || monthfield==4 || monthfield==6 || monthfield==11) {
		maxdays=30;
	}
	else if (monthfield==2) {
		 maxdays=28;
		 leapyears="1994,1996,2000,2004,2008,2012,2016,2020,2024,2028,2032";
		 leapArray = leapyears.split(',');
		 nbr=leapArray.length;
		 for (c=0; c<nbr; c++) {  
				if (yearfield==leapArray[c]){
					maxdays=29;
					break;
				}
		 }
	 }
	 if (dayfield > maxdays) return false;
	 
	/* this approach just does not work consistently
	 //alert ("monthfield:" + monthfield +  " dayfield:" + dayfield + " yearfield:" + yearfield);
	if (monthfield !=12) { // I have no idea why, but when it's 12 this does not work
			var dx = new Date(yearfield , monthfield, dayfield );   
			var doD = dx.getDate() * 1;// the 1 turns them into a number, not a date for comparison 
			var doM = dx.getMonth() * 1; // the 1 turns them into a number, not a date for comparison 
			//var doY = dx.getFullYear() * 1;// the 1 turns them into a number, not a date for comparison 
			var doY  = takeYear(dx); 
		   alert ("doM returned" + doM +  " doD returned" + doD + " doY returned" + doY);
			if ( (doM != monthfield) || (doD != dayfield) || (doY != yearfield) ) { // the 1 turns them into a number, not a date for comparison  
					// alert("not date ok"); 
					return false; 
			}  
	}
	*/
	return true;
}  
