// JavaScript Document

function compareDates (value1, value2) 
{
	var date1, date2;
	var month1, month2;
	var year1, year2;
	
	date1  = value1.substring (value1.indexOf ("/")+1, value1.lastIndexOf ("/"));
	month1 = value1.substring (0, value1.indexOf ("/"));
	year1  = value1.substring (value1.lastIndexOf ("/")+1, value1.length);

	date2  = value2.substring (value2.indexOf ("/")+1, value2.lastIndexOf ("/"));
	month2 = value2.substring (0, value2.indexOf ("/"));
	year2  = value2.substring (value2.lastIndexOf ("/")+1, value2.length);
   
	if (date1.length<2)
	{
		date1 = "0"+date1;
	}
	if (date2.length<2)
	{
		date2 = "0"+date2;
	}
	if (month1.length<2)
	{
		month1 = "0"+month1;
	}
	if (month2.length<2)
	{
		month2 = "0"+month2;
	}

   if (year1 > year2) return 1;
   else if (year1 < year2) return -1;
   else if (month1 > month2 && year1 >= year2) return 1;
   else if (month1 < month2 && year1 <= year2) return -1;
   else if (date1 > date2 && month1 >= month2 && year1 >= year2) return 1;
   else if (date1 < date2 && month1 <= month2 && year1 <= year2) return -1;
   else return 0;
}


function validateMainSearch()
{
	if(document.form1.sltPick.value == "")
	{
		alert("Please select a city");
		document.form1.sltPick.focus();
		return false;
	}
	if(document.form1.from.value == "")
	{
		alert("Please select the date you would like to travel from");
		document.form1.from.focus();
		return false;
	}
	if(document.form1.sltDriving.value == "")
	{
		alert("Please select a Driving Option");
		document.form1.sltDriving.focus();
		return false;
	}
	if(document.form1.sltDriving.value == "S")
	{
		alert('For "Self Drive" booking, please contact respective branch!');
		document.form1.sltDriving.focus();
		return false;
	}
	if(document.form1.sltService.value == "")
	{
		alert("Please select a service option");
		document.form1.sltService.focus();
		return false;
	}
	
	var d = new Date();
	
	var month2 = d.getMonth() + 1;
	if ((month2.toString()).length < 2)
	{
		month2 = "0" + month2.toString();
	}
	var date2 = d.getDate();
	if (date2.toString().length < 2)
	{
		date2 = "0" + date2.toString();
	}
	var year2 = d.getFullYear();
	
	var todayDte = month2 + "/" + date2 + "/" + year2;
	
	if(compareDates (document.form1.from.value, todayDte) < 1)
	{
		alert("Please make sure the dates you enter are greater than " + todayDte);
		return false;
	}
	if (document.form1.to.value != "")
	{
		if(compareDates (document.form1.to.value, document.form1.from.value) < 0)
		{
			alert("Please make sure the To date is not earlier than the From date");
			return false;
		}
	}
	
	if(document.form1.to.value == "")
	{
		if(document.form1.txtToTime.value != "0000")
		{
			if(document.form1.txtToTime.value < document.form1.txtFromTime.value)
			{
				alert("Please make sure the To time is not earlier than the From time");
				return false;
			}
		}
	}
	else
	{
		if(compareDates (document.form1.to.value, document.form1.from.value) < 1)
		{
			if(document.form1.txtToTime.value < document.form1.txtFromTime.value)
			{
				alert("Please make sure the To time is not earlier than the From time");
				return false;
			}
		}
	}
	if(compareDates (document.form1.to.value, document.form1.from.value) ==0 && (document.form1.txtFromTime.value==document.form1.txtToTime.value))
	{
		alert("Please make sure the From Time and To time are not same");
		return false;
	}
	if(compareDates (document.form1.to.value, document.form1.from.value) ==0)
	{
		if ((parseInt(document.form1.txtToTime.value) - parseInt(document.form1.txtFromTime.value)) < 100)
		{
			alert("Please make sure the To time should be one hour after than the From time");
			return false;
		}
	}
	
	dtFrom = document.form1.from.value
	dateFrom = dtFrom.substring (0, dtFrom.indexOf ("/"));
	monthFrom= dtFrom.substring (dtFrom.indexOf ("/")+1, dtFrom.lastIndexOf ("/"));
	yearFrom = dtFrom.substring (dtFrom.lastIndexOf ("/")+1, dtFrom.length);
	dtFrom = monthFrom + "/" + dateFrom + "/" + yearFrom;
	
	dtTo = document.form1.to.value
	dateTo = dtTo.substring (0, dtTo.indexOf ("/"));
	monthTo= dtTo.substring (dtTo.indexOf ("/")+1, dtTo.lastIndexOf ("/"));
	yearTo = dtTo.substring (dtTo.lastIndexOf ("/")+1, dtTo.length);
	dtTo = monthTo + "/" + dateTo + "/" + yearTo;
	
	//document.form1.action="../booking_engine.asp";
	document.form1.action="booking_engine.asp";
	document.form1.submit();	
}

function validateMainSearch1()
{
	if(document.form1.sltPickUpCity.value == "")
	{
		alert("Please select a city");
		document.form1.sltPickUpCity.focus();
		return false;
	}
	
	if(document.form1.sltService1.value == "")
	{
		alert("Please select service.");
		document.form1.sltService1.focus();
		return false;
	}
	
	if(document.form1.sltDriving1.value == "")
	{
		alert("Please select a Car Model.");
		document.form1.sltDriving1.focus();
		return false;
	}
	
	if(document.form1.from1.value == "")
	{
		alert("Please select the date you would like to travel from");
		document.form1.from1.focus();
		return false;
	}
			
	var d = new Date();

	var month2 = d.getMonth() + 1;
	if ((month2.toString()).length < 2)
	{
		month2 = "0" + month2.toString();
	}
	var date2 = d.getDate();
	if (date2.toString().length < 2)
	{
		date2 = "0" + date2.toString();
	}
	var year2 = d.getFullYear();
	var hour2 = d.getHours()+'00';
	var todayDte = month2 + "/" + date2 + "/" + year2;
	
	if(compareDates (document.form1.from1.value, todayDte) < 0)
	{
		alert("Please make sure the dates you enter are greater or equal to " + todayDte);
		return false;
	}
	
	if (compareDates (document.form1.from1.value, todayDte) == 0)
	{
		if ((parseInt(document.form1.txtFromTime1.value) - parseInt(hour2)) < 400)
		{
			alert("The Booking can be made after four hours to Current Time ");
			return false;
		}			
	}
	
//	if (document.form1.to1.value != "")
//	{
//		if(compareDates (document.form1.to1.value, document.form1.from1.value) < 0)
//		{
//			alert("Please make sure the To date is not earlier than the From date");
//			return false;
//		}
//	}

//	if(document.form1.to1.value == "")
//	{
//		if(document.form1.txtToTime1.value != "0000")
//		{
//			if(document.form1.txtToTime1.value < document.form1.txtFromTime1.value)
//			{
//				alert("Please make sure the To time is not earlier than the From time");
//				return false;
//			}
//		}
//	}
//	else
//	{
//		if(compareDates (document.form1.to1.value, document.form1.from1.value) < 1)
//		{
//			if(document.form1.txtToTime1.value < document.form1.txtFromTime1.value)
//			{
//				alert("Please make sure the To time is not earlier than the From time");
//				return false;
//			}
//		}
//	}

//	if(compareDates (document.form1.to1.value, document.form1.from1.value) ==0 && (document.form1.txtFromTime1.value==document.form1.txtToTime1.value))
//	{
//		alert("Please make sure the From Time and To time are not same");
//		return false;
//	}

//	if(compareDates (document.form1.to1.value, document.form1.from1.value) ==0)
//	{
//		if ((parseInt(document.form1.txtToTime1.value) - parseInt(document.form1.txtFromTime1.value)) < 100)
//		{
//			alert("Please make sure the To time should be one hour after than the From time");
//			return false;
//		}
//	}

	dtFrom = document.form1.from1.value
	dateFrom = dtFrom.substring (0, dtFrom.indexOf ("/"));
	monthFrom= dtFrom.substring (dtFrom.indexOf ("/")+1, dtFrom.lastIndexOf ("/"));
	yearFrom = dtFrom.substring (dtFrom.lastIndexOf ("/")+1, dtFrom.length);
	dtFrom = monthFrom + "/" + dateFrom + "/" + yearFrom;

//	dtTo = document.form1.to1.value
//	dateTo = dtTo.substring (0, dtTo.indexOf ("/"));
//	monthTo= dtTo.substring (dtTo.indexOf ("/")+1, dtTo.lastIndexOf ("/"));
//	yearTo = dtTo.substring (dtTo.lastIndexOf ("/")+1, dtTo.length);
//	dtTo = monthTo + "/" + dateTo + "/" + yearTo;


	if (document.form1.txtlat.value == "" || document.form1.txtlat.value == "No Record Exist")
	{
		alert("Please Select Pickup Location");
		document.form1.txtlat.focus();
		return false;
	}

	
	if (document.form1.txtlon.value == "" || document.form1.txtlon.value == "No Record Exist")
	{
		alert("Please Select Drop Off Location");
		document.form1.txtlon.focus();
		return false;
	}	
	
	if(document.form1.sltService1.value=="To Airport" || document.form1.sltService1.value =="From Airport")
	{
		if (document.form1.txtfgtno.value == "")
		{
			alert("Please Enter Airline / Flight No.");
			document.form1.txtfgtno.focus();
			return false;
		}
	}
	
	
	if (document.form1.txtTravelers.value == "")
	{
		alert("Please Enter No. of Travelers.");
		document.form1.txtTravelers.focus();
		return false;
	}
	
	if (isNaN(document.form1.txtTravelers.value))
	{
		alert("Please Enter Numeric data only.");
		document.form1.txtTravelers.focus();
		return false;
	}	

	if (document.form1.txtTravelers.value > 3)
	{
		alert("Book another car later if travelers are more than 3");
		document.form1.txtTravelers.focus();
		return false;
	}

	document.form1.action="booking_engineLimo.asp";
	//document.form1.action="../booking_engineLimo.asp";
	document.form1.submit();	
	//document.form1.action = "BookingMail.asp?service="+document.form1.sltService1.value+"&FromDate="+document.form1.from1.value +"&FromTime="+document.form1.txtFromTime1.value+"&ToDate="+document.form1.to1.value+"&ToTime="+document.form1.txtToTime1.value +"&City="+document.form1.sltPickUpCity.value;
	//document.form1.submit();	
}

function tabNew1()
{
	$("#content2").hide();
	$("#content1").show();
	$("#t1").addClass("tab1-active");
	$("#t2").removeClass("tab2-active");
	$("#t2").addClass("tab2");
}
function tabNew2()
{
	$("#content1").hide();
	$("#content2").show();
	$("#t2").addClass("tab2-active");
	$("#t1").removeClass("tab1-active");
	$("#t1").addClass("tab1");
}

function fill()
{
	var e = window.event;
	document.form1.txtlat.value = document.form1.ddllat.value;
	document.getElementById("myDiv").style.display="none";
	document.form1.txtlon.focus();	
}

function fill2()
{
	var e = window.event;
	if(e.keyCode == 13 || e.keyCode == 32)
	{
		document.form1.txtlat.value = document.form1.ddllat.value;
		document.getElementById("myDiv").style.display="none";
		document.form1.txtlon.focus();	
	}
}

function fill3()
{
	var e = window.event;
	document.form1.txtlon.value = document.form1.ddllon.value;
	document.getElementById("myDiv1").style.display="none";	
	document.form1.txtfgtno.focus();
}

function fill4()
{
	var e = window.event;
	if(e.keyCode == 13 || e.keyCode == 32)
	{
		document.form1.txtlon.value = document.form1.ddllon.value;
		document.getElementById("myDiv1").style.display="none";
		document.form1.txtfgtno.focus();
	}
}


function myLatLon()
{
	var Lon = document.form1.txtlat.value;
	if(Lon!="")
	{
		//alert(window.event.keyCode);
		GetLat();
		document.getElementById("myDiv").style.display="block";
		var e = window.event;
		if(e.keyCode==40)
		{
			document.form1.ddllat.focus();	
		}
	}
	else
	{
		document.getElementById("myDiv").style.display="none";	
	}
}

function myLatLon1()
{
	var Lon = document.form1.txtlon.value;
	if(Lon!="")
	{
		//alert(window.event.keyCode);

		GetLon();
		document.getElementById("myDiv1").style.display="block";

		var e = window.event;
		if(e.keyCode==40)
		{
			document.form1.ddllon.focus();	
		}
	}
	else
	{
		document.getElementById("myDiv1").style.display="none";	
	}
}

function GetLat()
{
	
	var city = document.form1.sltPickUpCity.value;
	var status = 1;
	if (document.form1.sltService1.value =="From Airport")
	{
		var Lon = "airport";
		calctest(Lon, city, status);
	}
	else
	{
		var Lon = document.form1.txtlat.value;
		
		calcLatLon(Lon, city, status);
	}
}

function GetLon()
{

	var city = document.form1.sltPickUpCity.value;
	var status = 2;	
	
	if (document.form1.sltService1.value =="To Airport")
	{
		var Lon = "airport";
		calctest(Lon, city, status);
	}
	else
	{
		var Lon = document.form1.txtlon.value;

		calcLatLon(Lon, city, status);
	}
}

function calcLatLon(Lon, city, status)
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url = "get_LatLon.asp";
	url = url + "?Lon=" + Lon;
	url = url + "&city=" + city;
	url = url + "&sid=" + Math.random();
	//alert (url);
	//document.write(url)
	if(status == 1)
	{
		xmlHttp.onreadystatechange = FillLat;
	}
	else
	{
		xmlHttp.onreadystatechange = FillLon;
	}

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function FillLat()
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		//document.form1.txtlat.innerHTML=xmlHttp.responseText
	
		var xmlResponse = xmlHttp.responseText;
		//optionval = xmlResponse.split("^");
		optionval = xmlResponse.split("^");
		//arr1 = optionval[1].split("#");

		if (optionval.length =="1")
		{
			document.form1.ddllat.options[0] = new Option("No Record Exist");
		}
		else
		{
			//document.form1.ddllon.options[0] = new Option("--Select--");
		}
		document.form1.ddllat.options[0].value = "";
		for(var i=1 ; i < optionval.length ; i++)
		{
			arr = optionval[i].split("#");
			//alert(arr[0]);
			document.form1.ddllat.options[i] = new Option(arr[1]);
			document.form1.ddllat.options[i].value = arr[0];
		}
		document.form1.ddllat.selectedIndex = 0;
	} 
}

function FillLon()
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		var xmlResponse = xmlHttp.responseText;
		//alert(xmlResponse);
		//optionval = xmlResponse.split(",");
		optionval = xmlResponse.split("^");
		//arr1 = optionval[1].split("#");
		//alert(optionval.length)
		
		//document.getElementById("txtlon").innerHTML = '[' + xmlResponse + ']';
		//document.getElementById("txtlon").value.innerHTML = 'Loaded: ' + optionval.length + ' tags: <br />' + xmlResponse;
		//alert(document.getElementById("txtlon").innerHTML);
		

		//document.getElementById("txtlon").innerHTML=xmlHttp.responseText;
		if (optionval.length =="1")
		{
			document.form1.ddllon.options[0] = new Option("No Record Exist");
		}
		else
		{
			//document.form1.ddllon.options[0] = new Option("--Select--");
		}
		//document.form1.txtlon.options[0].value = "";
		for(var i=1 ; i < optionval.length ; i++)
		{
			arr = optionval[i].split("#");
			//alert(arr[0]);
			document.form1.ddllon.options[i] = new Option(arr[1]);
			document.form1.ddllon.options[i].value = arr[0];
			//document.getElementById("txtlon").value.innerHTML=optionval[i];
			//document.form1.txtlat1.value = arr[0];
			//document.form1.txtlon.value.innerHTML = arr[0];
		}
		//document.form1.txtlon.value = document.form1.txtlon.value.innerHTML;
		document.form1.ddllon.selectedIndex = 0;
	} 
}

function clickHandler()
{
	var CID = null
	clearTimeout(CID)
	CID = setTimeout(getSuggestions, 333);
}
	
function getSuggestions()
{
	var input = escape(document.form1.txtlat.value);
	ASPAjax.liveASP('suggestCountry("'+input+'")' , 'SUGGESTIONS' )
}

function calctest(Lon, city, status)
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url = "get_LatLonNew.asp";
	url = url + "?Lon=" + Lon;
	url = url + "&city=" + city;
	url = url + "&sid=" + Math.random();
	//alert (url);
	//document.write(url)
	if(status == 1)
	{
		xmlHttp.onreadystatechange = FillLat;
	}
	else
	{
		xmlHttp.onreadystatechange = FillLon;
	}

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function OpenCorporateSite()
{
	window.open("http://corporate.carzonrent.com/Corweb/CorporateSite/Corporatelogin.asp");
}
