// JavaScript Document
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var fulldate = new Date();
var minYear=fulldate.getFullYear(); //get current year
var maxYear=2100;
var msg = "";

var popUpWin=0;

function popUpWindow(URLStr, left, top, width, height)

{

  if(popUpWin)

  {

    if(!popUpWin.closed) popUpWin.close();

  }

  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,scrollbars=yes,status=no,menubar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

}

function validate_form()
{
	var f = document.forms[0];
	
	if (f.name.value == "") 
	{
		msg += "You must enter your name!\n";
	}
	
	if (f.name.value.length < 2 && f.name.value != "") 
	{
		msg += "Your name must be at least 2 chars.\n";
	}
	
	if (f.date.value == "") 
	{
		msg += "You must enter a pickup date!\n";
	}

	if (f.date.value != "")
	{
		isDate(f.date.value);
	}


	if (f.phone.value == "") 
	{
		msg += "You must enter your phone number!\n";
	}
	
	if (f.phone.value != "")
	{
  		var checkOK = "0123456789-";
  		var checkStr = f.phone.value;
  		var allValid = true;
		var length = 0;
		
		
  		for (var i = 0;  i < checkStr.length;  i++)
  		{
    		var ch = checkStr.charAt(i);
			
			if (!isNaN(ch)) length++;
			
			if (checkOK.indexOf(ch) != -1) continue;
				
			allValid = false;
			break;
  		}
	
  		if (!allValid)
  		{
    		msg += "Enter only digit characters in the phone number field\n";
  		} else {
			if (length < 10)
			{
				msg += "Phone number must be at least 10 digits!\n";
			}
			if (length > 10)
			{
				msg += "Phone number has too many digits!\n";
			}
		}
	}
	
	if ((f.hour.value < 8 || f.hour.value == 12) && f.meridian.value == "AM" || (f.hour.value > 6 && f.hour.value < 12) && f.meridian.value == "PM" || f.hour.value == 6 && f.minute.value > 0 && f.meridian.value == "PM") 
	{ 
		msg += "Pick-up time not within normal business hours!\n"
	}	

	
	if (msg != "")
	{
		alert("This following errors need to be fixed:\n\n" + msg);
		msg = "";
		return;
	}
	
	f.name.value = trim(f.name.value);	
	f.date.value = trim(f.date.value);	
	f.phone.value = trim(f.phone.value);	
	
	popUpWindow('','100','100','400','150');
	f.target = "popUpWin";
	f.submit();
	f.name.value="";
	f.date.value="";
	f.phone.value="";
	f.hour[0].selected = true;
	f.minute[0].selected = true;
	f.meridian[0].selected = true;
}

function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this;
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		msg += "The date format should be : mm/dd/yyyy\n";
	}
	if (strMonth.length<1 || month<1 || month>12){
		msg += "Please enter a valid month!\n";
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		msg += "Please enter a valid day!\n";
	}
//	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//		msg += "Please enter a valid 4 digit year between "+minYear+" and "+maxYear+"!\n";
//	}
	if (strMonth < fulldate.getMonth() || strMonth == fulldate.getMonth() && strDay < fulldate.getDay() || strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		msg += "The date you entered is invalid!\n";		
	}
}