// JavaScript Document
var popUpWin=0;

function popUpWindow(URLStr, width, height)

{

  if(popUpWin)

  {

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

  }
  
  if(width == "") width='400';
  if(height == "") height='250';

  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=100, top=100,screenX=100,screenY=100');
  popUpWin.document.close;
  popUpWin.focus();
}


function send_info()
{
	var msg = "";
	var f = document.contact;
	
	if (f.name.value == "" || f.name.value.length < 2) 
	{
		msg += "Please enter your name!\n";
	}
	
	if (f.email.value != "")
	{
		if (!check_email(f.email.value)) {
			msg += "Email address not in the correct format!\n";
		}
	}
	
	if (f.phone.value == "") 
	{
		msg += "Please enter your phone number!\n";
	}
	
	if (f.phone.value != "")
	{
		var checkOK = "0123456789-";
  		var checkStr = f.phone.value;
		var allValid = true;
		var length = 0;
		
		for (i = 0;  i < checkStr.length;  i++)
		{
   			ch = checkStr.charAt(i);
			
			if (!isNaN(ch)) length++;
			
   			for (j = 0; j < checkOK.length; j++)
   			{
				if (ch == checkOK.charAt(j)) break;
   				if (j == checkOK.length)
   				{
   					allValid = false;
   					break;
				}
	  		}
			if (!allValid) 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.message.value == "") 
	{
		msg += "Blank messages are not allowed!\n";
	}

	if (msg != "")
	{
		alert("The following errors need to be fixed:\n\n" + msg);
		return;
	}
	
	f.phone.value = trim(f.phone.value);	
   	f.name.value = trim(f.name.value);	
	f.message.value = trim(f.message.value);	
	f.email.value = trim(f.email.value);

	popUpWindow('','400','150');
	f.action = "sendinfo.php";
	f.target = "popUpWin";
	f.submit();
	f.reset();
	
} 


function reserve_it()
{
	var msg = "";
	var f = document.reserve;
	
	if (f.service.selectedIndex == 0) {
		msg += "Please select a service.\n";
	}
	
	if (f.reserve_date.value == "") 
	{
		msg += "Please enter a reservation date!\n";
	} else {
		msg += isDate(f.reserve_date.value);
	}
		
//	if ((f.hour.value < 8 || f.hour.value == 12) && f.meridian.value == "AM" || (f.hour.value > 8 && f.hour.value < 12) && f.meridian.value == "PM" || f.hour.value == 6 && f.minute.value > 0 && f.meridian.value == "PM") 
//	{ 
//		msg += "Schedule time not within normal business hours!\n"
//	}	

	if (f.transport_direction.selectedIndex == 0 && f.service.selectedIndex == 2) {
		msg += "Please select a direction for your transport.\n";
	}
	
	if (f.transport_direction.selectedIndex == 2 && (f.a_flight.value == "" || f.a_gate.value == "")) {
		msg += "Please enter your flight information.\n";
	}

	if (f.name_from.value == "" || f.name_from.value.length < 2) 
	{
		msg += "Please enter your name!\n";
	}
	
	if (f.address_from.value == "") 
	{
		msg += "Please enter a departure address!\n";
	}
	
	if (f.city_from.selectedIndex == 0)
	{
		msg += "Please choose a departure city!\n";
	} else {
		if (f.city_from.options[f.city_from.selectedIndex].value == "Other" && f.city_from_other.value == "") {
			msg += "Please enter a departure city in the other field!\n";
		} else {
			if (f.city_from.options[f.city_from.selectedIndex].value != "Other") {
				f.city_from_other.value = "";
			}	
		}
	}
	
	if (f.zip_from.value == "")
	{ 
		msg += "Please enter a departure zip code!\n";
	} else if (isNaN(f.zip_from.value))	{
		msg += "Enter only digit characters in the departure zip code field!\n";
	}
	

	if (f.email_from.value != "")
	{
		if (!check_email(f.email_from.value)) {
			msg += "Email address not in the correct format!\n";
		}
	}
	
	if (f.phone_from.value == "") 
	{
		msg += "Please enter your phone number!\n";
	}
	
	if (f.phone_from.value != "")
	{
		var checkOK = "0123456789-";
  		var checkStr = f.phone_from.value;
		var allValid = true;
		var length = 0;
		
		for (i = 0;  i < checkStr.length;  i++)
		{
   			ch = checkStr.charAt(i);
			
			if (!isNaN(ch)) length++;
			
   			if (checkOK.indexOf(ch) == -1)
			{
				allValid=false;
				break;
			}
		}
	
		if (!allValid)
		{
   			msg += "Enter only digits characters in the departure phone number field!\n";
		} else {
			if (length < 10)
			{
				msg += "Departure phone number must be at least 10 digits!\n";
			}
			if (length > 10)
			{
				msg += "Departure phone number has too many digits!\n";
			}
		}
	}
	
	
	if (document.getElementById('package_info').style.display == "block" && f.service.selectedIndex == 1) {
		if (f.name_to.value == "" || f.name_to.length < 2) 
		{
			msg += "Please enter a destination name!\n";
		}
	
		if (f.address_to.value == "") 
		{
			msg += "Please enter a destination address!\n";
		}
	
		if(f.city_to.selectedIndex == 0)
		{
			msg += "Please choose a destination city!\n";
		}
	
		if (f.zip_to.value == "")
		{ 
			msg += "Please enter a destination zip code!\n";
		} else if (isNaN(f.zip_to.value))	{
			msg += "Enter only digit characters in the destination zip code field!\n";
		}

		if (f.phone_to.value != "")
		{
			var checkOK = "0123456789-";
	  		var checkStr = f.phone_to.value;
			var allValid = true;
			var length = 0;
		
			for (i = 0;  i < checkStr.length;  i++)
			{
   				ch = checkStr.charAt(i);
			
				if (!isNaN(ch)) length++;
			
   				if (checkOK.indexOf(ch) == -1)
				{
					allValid=false;
					break;
				}
			}
	
			if (!allValid)
			{
   				msg += "Enter only digits characters in the destination phone number field!\n";
			} else {
				if (length < 10)
				{
					msg += "Destination phone number must be at least 10 digits!\n";
				}
				if (length > 10)
				{
					msg += "Destination phone number has too many digits!\n";
				}
			}
		}
		
		if (f.pkg_description.value == "") {
			msg += "Please enter a description of the package.\n";
		}
		
		
		if (f.pkg_weight.value != "") {
			var checkOK = "0123456789.";
	  		var checkStr = f.pkg_weight.value;
			var allValid = true;
			var length = 0;
		
			for (i = 0;  i < checkStr.length;  i++)
			{
   				ch = checkStr.charAt(i);
			
				if (!isNaN(ch)) length++;
			
   				if (checkOK.indexOf(ch) == -1)
				{
					allValid=false;
					break;
				}
			}
	
			if (!allValid) {
				msg += "Only digit chars and '.' are allowed in the weight field.\n";
			}
		}

		if (f.pkg_weight.value != "") {
			var checkOK = "0123456789.";
	  		var checkStr = f.pkg_weight.value;
			var allValid = true;
			var length = 0;
		
			for (i = 0;  i < checkStr.length;  i++)
			{
   				ch = checkStr.charAt(i);
			
				if (!isNaN(ch)) length++;
			
   				if (checkOK.indexOf(ch) == -1)
				{
					allValid=false;
					break;
				}
			}
	
			if (!allValid) {
				msg += "Only digit chars and '.' are allowed in the weight field.\n";
			}
		}
		
		if (f.pkg_length.value != "") {
			var checkOK = "0123456789.";
	  		var checkStr = f.pkg_length.value;
			var allValid = true;
			var length = 0;
		
			for (i = 0;  i < checkStr.length;  i++)
			{
   				ch = checkStr.charAt(i);
			
				if (!isNaN(ch)) length++;
			
   				if (checkOK.indexOf(ch) == -1)
				{
					allValid=false;
					break;
				}
			}
	
			if (!allValid) {
				msg += "Only digit chars and '.' are allowed in the length field.\n";
			}
		}
		
		if (f.pkg_width.value != "") {
			var checkOK = "0123456789.";
	  		var checkStr = f.pkg_width.value;
			var allValid = true;
			var length = 0;
		
			for (i = 0;  i < checkStr.length;  i++)
			{
   				ch = checkStr.charAt(i);
			
				if (!isNaN(ch)) length++;
			
   				if (checkOK.indexOf(ch) == -1)
				{
					allValid=false;
					break;
				}
			}
	
			if (!allValid) {
				msg += "Only digit chars and '.' are allowed in the width field.\n";
			}
		}

		if (f.pkg_height.value != "") {
			var checkOK = "0123456789.";
	  		var checkStr = f.pkg_height.value;
			var allValid = true;
			var length = 0;
		
			for (i = 0;  i < checkStr.length;  i++)
			{
   				ch = checkStr.charAt(i);
			
				if (!isNaN(ch)) length++;
			
   				if (checkOK.indexOf(ch) == -1)
				{
					allValid=false;
					break;
				}
			}
	
			if (!allValid) {
				msg += "Only digit chars and '.' are allowed in the height field.\n";
			}
		}

	}

	
	if (f.i_agree.checked == false)
	{
		msg += "You must agree with the Reservation Policy statement!\n";
	}

	if (msg != "")
	{
		alert("The following errors need to be fixed:\n\n" + msg);
		return;
	}
	
	
	popUpWindow('','400','450');
	f.action = "geocode.php";
	f.target = "popUpWin";
	f.submit();
}

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

function check_email(email)
{
		var valid_chars = "abcdefghijklmnopqrstuvwxyz0123456789@_-.";
		var len = email.length;
		
		for (var i=0; i < len; i++)
		{
			var letter = email.charAt(i).toLowerCase();
			if (valid_chars.indexOf(letter) != -1) 	continue;
			return false;
		}
		
		return true;
}

var dtCh= "/";
var fulldate = new Date();
var minYear=fulldate.getFullYear(); //get current year
var maxYear=2100;

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 msg = "";
	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";		
	}
	
	return msg;
}