//------------------------------------------------------------------------
//	CHECK IF VALUE ENETERED IS A VALID STRING
//------------------------------------------------------------------------
function IfValidString(TextBox)
{
	var TextBoxValue = TextBox.value;
	
	try{
		
		if(TextBoxValue.length == 0) return false;
		if(TextBoxValue =='') return false;
		if(TextBoxValue.indexOf(" ") == 0) return false;
	}catch (e){return false ;}
	return true;
}
//------------------------------------------------------------------------
//	CHECK IF VALUE ENETERED IS A VALID STRING
//------------------------------------------------------------------------
function IfValidSelect(Droddown)
{
	try{
		if(Droddown.selectedIndex == 0) return false;
	}catch (e){return false ;}
	return true;
}
//------------------------------------------------------------------------
//	CHECK IF VALUE ENETERED IS A VALID STRING
//------------------------------------------------------------------------
function ValidString2(Field, bRet)
{
	var Ret1 = true;
	
	try{
		if(Field.value.length == 0) Ret1 = false;
		if(Field.value =='') Ret1 = false;
		if(Field.value.indexOf(" ") == 0) Ret1 = false;
	}catch (e){Ret1 = false;}
	
	if(bRet == true && Ret1 == false){
		return false;
	}else if(bRet == false && Ret1 == true){
		return false;
	}else{
		return Ret1;
	}
}
	
function GetCurrentDate()
{
	var D = new Date();
	var MM = D.getMonth();
	var Mon, Days, cDate;
	MM = MM + 1;
	Mon = MM;
	if(MM < 10) Mon = "0" + MM;
	if(D.getDate() < 10){
		Days = "0" + D.getDate();
	}else{
		Days = "" + D.getDate();}
	cDate = Mon + "/" + Days + "/" + D.getFullYear();
	return cDate;
}
// CHECK FOR VALID DATE INPUT
function ValidateDate(Val1)
{
	if(Val1.indexOf(" ") != -1 || Val1.indexOf("/") == -1 || Val1.indexOf("/") == Val1.lastIndexOf("/"))
	{
		return false;
	}
	if(Val1.length != 10) return false;
	var mMonth, mDay, mYear;
	mMonth = Val1.substring(0,2);
	mDay = Val1.substring(3,5);
	if(Val1.length == 8)
		mYear = Val1.substring(6,8);
	else if(Val1.length == 10)
		mYear = Val1.substring(6,10);
	var IsLeapYear;
	var IsValid = true;
	if(mYear%4 == 0)
		IsLeapYear = true;
	else
		IsLeapYear = false;

	if((mMonth <= 0 || mMonth > 12) || (mDay <= 0 || mDay > 31)) IsValid = false;
	if ((mMonth == '04' | mMonth == '06' || mMonth == '09' || mMonth == '11') && (mDay > 30)) IsValid = false
	if(!IsLeapYear)
	{
		if(mMonth == '02' && mDay > 28)IsValid = false;
	}
	else
	{
		if(mMonth == '02' && mDay > 29) IsValid = false;
	}
	if(mMonth.indexOf("/") != -1 || mDay.indexOf("/") != -1 || mYear.indexOf("/") != -1) IsValid = false;
	return IsValid;
}
	//**************************************************************************
	// RETURN DATE DIFFERENCE IN DAYS
	//**************************************************************************
	function DateDifference(Date1, Date2)
	{
		var Return = -1;
		var D1 = new Date(Date1)
		var D2 = new Date(Date2);
		var OneDayMS =1000*60*60*24; // MILLISECONDS IN A DAY
		Return = Math.ceil((D2.getTime()-D1.getTime())/(OneDayMS)) - 1;
		return Return;
	}
	//**************************************************************************
	// PERFORM DAYS DIFFERENCE BETWEEN CURRENT DATE AND SENT BusinessDate
	//**************************************************************************
	function CheckIfBookingAvailable(BusinessDate)
	{
		var Ret = true;
		var CurrentDate = GetCurrentDate();
		var DateDiff = DateDifference(CurrentDate, BusinessDate);
		//alert(DateDiff);
		if(DateDiff > 120) Ret= false;
		return Ret;
	}
	
function curMon(DropOff){
	var Field;
	
	if(DropOff == undefined){
		Field = document.forms[0].ddlPickupMonth;
	}else{
		Field = document.forms[0].ddlDropoffMonth;
	}
	if(Field==undefined)
	{
		if(DropOff == undefined){
			Field = document.forms[0].Bar1_ddlPickupMonth;
		}else{
			Field = document.forms[0].Bar1_ddlDropoffMonth;
		}
	}
	
	var mon = Field.options[Field.options.selectedIndex].value;
	if (mon!='NaN'){
		return (Math.abs(mon) -1);
	}
	else{
		return '';
	}
}
function curYr(DropOff){
	var Field;
	
	if(DropOff == undefined){
		Field = document.forms[0].ddlPickupYear;
	}else{
		Field = document.forms[0].ddlDropoffYear;
	}
	if(Field==undefined)
	{
		if(DropOff == undefined){
			Field = document.forms[0].Bar1_ddlPickupYear;
		}else{
			Field = document.forms[0].Bar1_ddlDropoffYear;
		}
	}
	var yr = Field.options[Field.selectedIndex].value;
	if (yr!='NaN'){
		return yr;
	}
	else{
		return '';
	}
}


function AddDays(mDate, Days){
	return (new Date(mDate.getTime() + (Days * 24 * 60 * 60 *1000)));
}

function FormatDate(D)
{
	var D2 = new Date(D);
	var Mon;
	if(Math.abs(D2.getMonth()+1) < 10)
		Mon = "0" + Math.abs(D2.getMonth()+1);
	else
		Mon = Math.abs(D2.getMonth()+1);
	return (Mon + "/" + D2.getDate() + "/" + D2.getFullYear());
}

function SetDates(Fld, mDate)
{
	if(document.getElementById("ddlPickupMonth") != undefined){
		SetDates1(Fld, mDate)
	}else{
		SetDates2(Fld, mDate)
	}
	
}
function SetDates1(Fld, mDate)
{
	var Day1, Mon1, Year1;
	/*var Pos1, Pos2;
	Day1 = GetDay(mDate)
	Pos1 = mDate.indexOf("/");
	Mon1 = mDate.substring(0, Pos1);
	Pos2 = mDate.indexOf("/", Pos1+1)
	Day1 = mDate.substring(Pos1+1, Pos2);
	Year1 = mDate.substring(Pos2+1);*/
	
	Day1 = GetDay(mDate);
	Mon1 = GetMonth(mDate);
	Year1 = GetYear(mDate);
	
	if(Fld.indexOf('Pickup') != -1)
	{
		document.forms[0].ddlPickupMonth.options[Math.abs(Mon1)-1].selected = true;
		document.forms[0].ddlPickupDay.options[Math.abs(Day1)-1].selected = true;
		for(var i=0; i < document.forms[0].ddlPickupYear.length; i++)
		{
			if(document.forms[0].ddlPickupYear.options[i].value == Year1)
			{
				document.forms[0].ddlPickupYear.options[i].selected = true;
				break;
			}
		}
		
		mDate = FormatDate(AddDays(new Date(mDate),1));// add one more
		
		Day1 = GetDay(mDate);
		Mon1 = GetMonth(mDate);
		Year1 = GetYear(mDate);
		
		document.forms[0].ddlDropoffMonth.options[Math.abs(Mon1)-1].selected = true;
		document.forms[0].ddlDropoffDay.options[Math.abs(Day1)-1].selected = true;
		for(var i=0; i < document.forms[0].ddlDropoffYear.length; i++)
		{
			if(document.forms[0].ddlDropoffYear.options[i].value == Year1)
			{
				document.forms[0].ddlDropoffYear.options[i].selected = true;
				break;
			}
		}
	}else{
		document.forms[0].ddlDropoffMonth.options[Math.abs(Mon1)-1].selected = true;
		document.forms[0].ddlDropoffDay.options[Math.abs(Day1)-1].selected = true;
		for(var i=0; i < document.forms[0].ddlDropoffYear.length; i++)
		{
			if(document.forms[0].ddlDropoffYear.options[i].value == Year1)
			{
				document.forms[0].ddlDropoffYear.options[i].selected = true;
				break;
			}
		}
	}
	
}




/***************/
function SetDates2(Fld, mDate)
{
	var Day1, Mon1, Year1;
	/*var Pos1, Pos2;
	Day1 = GetDay(mDate)
	Pos1 = mDate.indexOf("/");
	Mon1 = mDate.substring(0, Pos1);
	Pos2 = mDate.indexOf("/", Pos1+1)
	Day1 = mDate.substring(Pos1+1, Pos2);
	Year1 = mDate.substring(Pos2+1);*/
	
	Day1 = GetDay(mDate);
	Mon1 = GetMonth(mDate);
	Year1 = GetYear(mDate);
	
	if(Fld.indexOf('Pickup') != -1)
	{
		document.forms[0].Bar1_ddlPickupMonth.options[Math.abs(Mon1)-1].selected = true;
		document.forms[0].Bar1_ddlPickupDay.options[Math.abs(Day1)-1].selected = true;
		for(var i=0; i < document.forms[0].Bar1_ddlPickupYear.length; i++)
		{
			if(document.forms[0].Bar1_ddlPickupYear.options[i].value == Year1)
			{
				document.forms[0].Bar1_ddlPickupYear.options[i].selected = true;
				break;
			}
		}
		
		mDate = FormatDate(AddDays(new Date(mDate),1));// add one more
		
		Day1 = GetDay(mDate);
		Mon1 = GetMonth(mDate);
		Year1 = GetYear(mDate);
		
		document.forms[0].Bar1_ddlDropoffMonth.options[Math.abs(Mon1)-1].selected = true;
		document.forms[0].Bar1_ddlDropoffDay.options[Math.abs(Day1)-1].selected = true;
		for(var i=0; i < document.forms[0].Bar1_ddlDropoffYear.length; i++)
		{
			if(document.forms[0].Bar1_ddlDropoffYear.options[i].value == Year1)
			{
				document.forms[0].Bar1_ddlDropoffYear.options[i].selected = true;
				break;
			}
		}
	}else{
		document.forms[0].Bar1_ddlDropoffMonth.options[Math.abs(Mon1)-1].selected = true;
		document.forms[0].Bar1_ddlDropoffDay.options[Math.abs(Day1)-1].selected = true;
		for(var i=0; i < document.forms[0].Bar1_ddlDropoffYear.length; i++)
		{
			if(document.forms[0].Bar1_ddlDropoffYear.options[i].value == Year1)
			{
				document.forms[0].Bar1_ddlDropoffYear.options[i].selected = true;
				break;
			}
		}
	}
	
}
/**************/

function GetYear(_Date)
{
	var Pos1 = _Date.lastIndexOf('/');
	return(_Date.substring(Pos1+1));
	
}
function GetMonth(_Date)
{
	var Pos1;
	Pos1 = _Date.indexOf('/');
	return(_Date.substring(0, Pos1));
}

function GetDay(_Date)
{
	var Pos1, Pos2;
	
	Pos1 = _Date.indexOf('/');
	Pos2 = _Date.indexOf('/', Pos1+1)
	return(_Date.substring(Pos1+1, Pos2));
}
function RefreshDDL(_Task)
{
	if(_Task.toUpperCase ()== 'USA'){
		if(document.forms[0].linkUSLocations.src.indexOf('USLocationsOn') != -1)
		{
			return;
		}else{
			document.forms[0].linkUSLocations.src = _Images[0].src;
			document.forms[0].linkIntLocations.src = _Images[3].src;
			SwapDDLS(_Task);
			document.getElementById("Table2_2").rows[1].style.visibility = 'visible';
		}
	}else if(_Task.toUpperCase() == 'INT'){
		if(document.forms[0].linkIntLocations.src.indexOf('IntLocationsOn') != -1){
			return;
		}else{
			document.forms[0].linkUSLocations.src = _Images[1].src;
			document.forms[0].linkIntLocations.src = _Images[2].src;
			SwapDDLS(_Task);
			document.getElementById("Table2_2").rows[1].style.visibility = 'hidden';
		}
	}
	ChangeColor();
}

function SwapDDLS(mTask)
{
	var Opt, _Val, _Text;
	document.forms[0].ddlPickup.options.length = 0;// = null;
	document.forms[0].ddlDropoff.options.length = 0;// = null;
	if(mTask.toUpperCase ()== 'USA'){
		// REFRESH USA
		for (var I = 0; I < document.forms[0].ddlHoldLocal.options.length; I++)
		{
			Opt = document.forms[0].ddlHoldLocal.options[I];
			_Val = Opt.value;
			_Text = Opt.text;
			document.forms[0].ddlPickup.options[I] = new Option(_Text, _Val);	
			if(_Text.indexOf("choose a") > -1) _Text = "same as pickup";
			document.forms[0].ddlDropoff.options[I]= 	new Option(_Text, _Val);
			Opt = null;
		}
		document.forms[0].txtInternational.value = '';
	}else if(mTask.toUpperCase() == 'INT'){
		// REFRESH INTERNATIONAL
		for (var I = 0; I < document.forms[0].ddlHoldInt.options.length; I++)
		{
			Opt = document.forms[0].ddlHoldInt.options[I];
			_Val = Opt.value;
			_Text = Opt.text;
			document.forms[0].ddlPickup.options[I] = new Option(_Text, _Val);	
			if(_Text.indexOf("choose a") > -1) _Text = "same as pickup";
			document.forms[0].ddlDropoff.options[I]= 	new Option(_Text, _Val);
			Opt = null;
		}
		document.forms[0].txtInternational.value = 'YES';
	}
}
function SetLocations()
{
	if(document.forms[0].ddlPickup.options.length == 0) return;
	
	document.forms[0].txtRentalLocations.value = '';
	var TSD1, TSD2, Pass1, Pass2, Code1, Code2, Name1, Name2, Pos;
	var CityName1, CityName2;
	var Val = '';
	
	// set Rental Location TSD Numbers AND Location ID - Pickup
	Val = document.forms[0].ddlPickup.options[document.forms[0].ddlPickup.selectedIndex].value;

	if(Val != '' && Val.indexOf(":") > 0) {
		Pos = Val.indexOf(":");
		Code1 = Val.substring(0, Pos);
		Pos2 = Val.indexOf(":", Pos + 1);
		TSD1 = Val.substring(Pos + 1, Pos2);
		Pos = Pos2+1;
		Pos2 = Val.indexOf(":", Pos);
		Pass1 = Val.substring(Pos, Pos2);
		Pos =  Val.lastIndexOf(":");
		CityName1 = Val.substring(Pos+1, Val.length);
	}else{
		TSD1 = '-1'; Code1 = '-1', Pass1 = '-1'; CityName1='-1';
	}
	
	// set Rental Location TSD Numbers AND Location ID - Dropoff
	Val = document.forms[0].ddlDropoff.options[document.forms[0].ddlDropoff.selectedIndex].value;
	
	if(Val != '' && Val.indexOf(":") > 0){
		Pos = Val.indexOf(":");
		Code2 = Val.substring(0, Pos);
		Pos2 = Val.indexOf(":", Pos + 1);
		TSD2 = Val.substring(Pos + 1, Pos2);
		Pos = Pos2+1;
		Pos2 = Val.indexOf(":", Pos);
		Pass2 = Val.substring(Pos, Pos2);
		Pos =  Val.lastIndexOf(":");
		CityName2 = Val.substring(Pos+1, Val.length);
	}else{
		TSD2 = '-1'; Pass2 = '-1';Code2 = '-1'; CityName2='-1';
	}
	
	// set rental name location - pickup 
	Name1 = document.forms[0].ddlPickup.options[document.forms[0].ddlPickup.selectedIndex].text;
	
	// set rental name location - dropoff 
	Val = document.forms[0].ddlDropoff.options[document.forms[0].ddlDropoff.selectedIndex].text;
	if(Val == 'same as pickup'){
		Name2 = Name1; TSD2 = TSD1; Pass2 = Pass1; Code2 = Code1; CityName2 = CityName1;
	}else{
		Name2 = document.forms[0].ddlDropoff.options[document.forms[0].ddlDropoff.selectedIndex].text;
	}
	
	document.forms[0].txtRentalLocations.value = 
		TSD1 + '#' + Pass1 + '#' + Code1 + '#' + Name1 + "#" + CityName1 +
			"|" + TSD2 + '#' + Pass2 + '#' + Code2 + '#' + Name2 + "#" + CityName2;
	//alert(document.forms[0].txtRentalLocations.value);
}
function SubmitRateForm_VehicleDetails(SelectedVehicle)
{
	document.forms[0].txtRateID.value = SelectedVehicle + ":VehicleDetails";
	//alert(document.forms[0].txtRateID.value);
	//alert(SelectedVehicle);
	document.forms[0].submit();
	
}
function SubmitRateForm(SelectedVehicle)
{
	//alert(SelectedVehicle);
	document.forms[0].txtRateID.value = SelectedVehicle;
	//alert("txtRateID: " + document.forms[0].txtRateID.value);// + "\nSelected Vehicle: " + SelectedVehicle);
	document.forms[0].submit();
}
function infoWindow(info) {
	vehicleswin = window.open(info, "infoWindow", "toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=300,height=210");
	vehicleswin.focus();
}
function ChangeColor()
{
	for(var i=0; i< document.forms[0].ddlPickup.options.length; i++)
	{
		if(document.forms[0].ddlPickup.options[i].value == '' && 
			document.forms[0].ddlPickup.options[i].text.indexOf('--') == 0)
			{
				document.forms[0].ddlPickup.options[i].style.backgroundColor= '#808080';
				document.forms[0].ddlDropoff.options[i].style.backgroundColor= '#808080';
			}
	}
}


/*DATE FUNCTIONS*/
//---------------------------------------------------------------------
//	CALLING ADD DAYS FUNCTION
//---------------------------------------------------------------------
function IncrementDate(DATE, NumberOfDays)
{
	var D = new Date(DATE);
	D = AddDays(D, NumberOfDays);
	return FormatDate(D.getMonth()+1 + "/" + D.getDate() + "/" + D.getFullYear());
}
//---------------------------------------------------------------------
//	RETURN FALSE IF PROVIDED VALUE IS AN INVALID DATE
//---------------------------------------------------------------------
function IsDate(Val1)
{
	if(Val1.indexOf(" ") != -1 || Val1.indexOf("/") == -1 || Val1.indexOf("/") == Val1.lastIndexOf("/")) return false;
	if(Val1.length != 10) return false;
	
	var mMonth, mDay, mYear;
	mMonth = Val1.substring(0,2);
	mDay = Val1.substring(3,5);
	mYear = Val1.substring(6,10);
	
	var IsLeapYear;
	var IsValid = true;
	if(mYear%4 == 0)
		IsLeapYear = true;
	else
		IsLeapYear = false;
		
	if((mMonth <= 0 || mMonth > 12) || (mDay <= 0 || mDay > 31)) IsValid = false;
	if ((mMonth == '04' | mMonth == '06' || mMonth == '09' || mMonth == '11') && (mDay > 30)) IsValid = false
	
	if(!IsLeapYear){
		if(mMonth == '02' && mDay > 28)IsValid = false;
	}else{
		if(mMonth == '02' && mDay > 29) IsValid = false;
	}
	
	if(mMonth.indexOf("/") != -1 || mDay.indexOf("/") != -1 || mYear.indexOf("/") != -1) IsValid = false;
	return IsValid;
}
//---------------------------------------------------------------------
//	RETURN PICKUP DATE
//---------------------------------------------------------------------
function GetPickupDate()
{
	var frm = document.forms[0];
	return frm.ddlPickupMonth.options[frm.ddlPickupMonth.selectedIndex].value + "/" +
		frm.ddlPickupDay.options[frm.ddlPickupDay.selectedIndex].value + "/" +
		frm.ddlPickupYear.options[frm.ddlPickupYear.selectedIndex].value;
}

//---------------------------------------------------------------------
//	RETURN DROPOFF DATE
//---------------------------------------------------------------------
function GetReturnDate()
{
	var frm = document.forms[0];
	return frm.ddlDropoffMonth.options[frm.ddlDropoffMonth.selectedIndex].value + "/" +
		frm.ddlDropoffDay.options[frm.ddlDropoffDay.selectedIndex].value + "/" +
		frm.ddlDropoffYear.options[frm.ddlDropoffYear.selectedIndex].value; 
}
//---------------------------------------------------------------------
//	INCREMENT RENTAL RETURN DATE DROPDOWNS
//---------------------------------------------------------------------
function ModifyRteurnDate()
{
	var PickupDate = GetPickupDate();
	var ReturnDate = GetReturnDate();
	var frm = document.forms[0];
	
	if(IsDate(PickupDate) == false || IsDate(ReturnDate) == false) return;
	
	var NewDate = IncrementDate(PickupDate, 2);
	
	var Pos = NewDate.indexOf("/",0);
	var Mon = NewDate.substring(0, Pos);
	Pos +=1;
	var newPos = NewDate.indexOf("/", Pos);
	var mDay = NewDate.substring(Pos, newPos);
	Pos = newPos +1
	var mYear = NewDate.substring(Pos, NewDate.length);
	if(mDay.length != 2) mDay = "0" + mDay;
	if(Mon.length != 2) Mon = "0" + Mon;
	
	frm.ddlDropoffMonth.options[Math.abs(Mon)-1].selected = true;
	frm.ddlDropoffDay.options[Math.abs(mDay)-1].selected = true;
	
	for (var i = 0; i < frm.ddlDropoffYear.options.length; i++){
		if(frm.ddlDropoffYear.options[i].value == mYear){
			frm.ddlDropoffYear.options[i].selected = true;
			break;
		}
	}
}