

// If this function changes, change the one in /partner/extranet/quotebox.php
function carChange(targ,selObj,restore){ 
	if (selObj.options[selObj.selectedIndex].text != "Please Select" && selObj.options[selObj.selectedIndex].text != "-------------------") {
		var carText = selObj.options[selObj.selectedIndex].text;
		// Find and replace any "-" with "_" in the car name
		carText = carText.replace("-", "_");
		var carSelected = "/quote-"+selObj.options[selObj.selectedIndex].value+"-"+carText+".php";
	  
		//Find and replace "spaces" with "_"'s.
		carSelected = carSelected.replace(/ /g, "_");

		document.quote.action = carSelected;	// Update the form action
		if (selObj.options[selObj.selectedIndex].text != "All Vehicles") {
			getLocationList(selObj); // Update the chain list
			//getLocationList2(selObj); // Update the chain list
		}
	}
}



function submitformQuote()
{
	if (document.quote.onsubmit()) {

// Check the start and end dates
		//startdate = document.quote.startyear.value + "/" + document.quote.startmonth.value + "/" + document.quote.startdate.value;
		//enddate = document.quote.endyear.value + "/" + document.quote.endmonth.value + "/" + document.quote.enddate.value;
		
//		startdate = document.quote.date1.value;
//		enddate = document.quote.date2.value;
		
/*		startdate1= Date.parse(startdate);
		enddate1= Date.parse(enddate);
		
		var year1 = document.quote.startyear.value;
		var month1 = document.quote.startmonth.value - 1;
		var day1 = document.quote.startdate.value;
		source_date1 = new Date(year1,month1,day1);	

		var year2 = document.quote.endyear.value;
		var month2 = document.quote.endmonth.value - 1;
		var day2 = document.quote.enddate.value;
		source_date2 = new Date(year2,month2,day2);	
		
		if (startdate1 > enddate1 || year1 != source_date1.getFullYear() || month1 != source_date1.getMonth() || day1 != source_date1.getDate() || year2 != source_date2.getFullYear() || month2 != source_date2.getMonth() || day2 != source_date2.getDate()) {
			alert("Please enter a valid Start and Finish hire time");
		} else {
*/

document.quote.submit();

//		}
	}
}

function hireFinishChange(selObj) {
	//make hire finish the same as hire start, provided something is there

	getCarList(selObj); // Update the chain list

	if (document.getElementById("hirestart").value != "") {
		document.getElementById("hirefinish").value = document.getElementById("hirestart").value;
	}
}

function endDateChange() {
	// Make the end date 2 days on from the start date
	startdate = new Date(document.quote.startyear.value + "/" + document.quote.startmonth.value + "/" + document.quote.startdate.value);
	alert(startdate);
	enddate = new Date();
	enddate.setTime(startdate.getTime());
	enddate.setHours(startdate.getHours()+(2*24));

	alert(enddate);
}


function adjustNumberOfDays() {
            
			var pudHrCtrl = document.quote.starthour.value;
			var pudMnCtrl = document.quote.startmin.value;

			var pudString = document.quote.date1a.value;
			var pudParts = pudString.split("-");
			var pudDay = parseInt(pudParts[0], 10);
       		var pudMonth = parseInt(pudParts[1], 10);
            var pudYear = parseInt(pudParts[2], 10);

			var pud = new Date();
//alert ("PUD1: " + pud);
			pud.setFullYear(pudYear, pudMonth - 1, pudDay);
//alert ("PUD2: " + pud);
//alert("Time: " + parseInt(pudHrCtrl, 10) + ":" + parseInt(pudMnCtrl, 10));
			pud.setHours(parseInt(pudHrCtrl, 10), parseInt(pudMnCtrl, 10), 0, 0);


			var dodHrCtrl = document.quote.endhour.value;
			var dodMnCtrl = document.quote.endmin.value;
			
			var dodString = document.quote.date2a.value;
			var dodParts = dodString.split("-");
            var dodDay = parseInt(dodParts[0], 10);  
			var dodMonth = parseInt(dodParts[1], 10);  
            var dodYear = parseInt(dodParts[2], 10);
			
            var dod = new Date();
            dod.setFullYear(dodYear, dodMonth - 1, dodDay);
            dod.setHours(parseInt(dodHrCtrl, 10), parseInt(dodMnCtrl, 10), 0, 0);

//alert ("PUD: " + pud);
//alert ("DOD: " + dod);

            //Set 1 day in milliseconds
            var one_day = 1000 * 60 * 60 * 24;

            // calculate time zone offset difference in milliseconds
            var offsetDiff = (dod.getTimezoneOffset() - pud.getTimezoneOffset()) * 60 * 1000;

            // now calculate difference corrected with offset considering DST
            var diff = dod.getTime() - pud.getTime() - offsetDiff;

            // calculate in days
            var duration = diff / one_day;

//alert(duration);

			// calculate minutes, hours, days to avoid rounding errors
            diff = diff / 1000 / 60;
            var minutes = Math.floor(diff % 60);
            diff = diff / 60;
            var hours = Math.floor(diff % 24);
            diff = diff / 24;
            var days = Math.floor(diff);
            
            // finally round up the value as we do not have hourly rental
            // considering the 59 minutes grace period as well
            var durationDays;
            if (hours >= 1) {
               durationDays = Math.ceil(duration);
            } else {
               durationDays = Math.floor(duration);
            }

			document.getElementById('divTotalDays').innerHTML = durationDays;


}

// --------------------------------------------------

function getLocationList(sel) {
	var carCode = sel.options[sel.selectedIndex].value;
	var locationCode = document.getElementById('hirestart').value;
	
	document.getElementById('hirestart').options.length = 0;	// Empty location select box
	
	if(carCode.length>0){
		var index = ajax.length;
		ajax[index] = new sack();		
		ajax[index].requestFile = '/Connections/getLocations2.php?carCode='+carCode+'&locationCode='+locationCode;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createLocations(index) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function createLocations(index) {
	var obj = document.getElementById('hirestart');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

function getLocationList2(sel) {
	var carCode = sel.options[sel.selectedIndex].value;
	var locationCode = document.getElementById('hirefinish').value;
	
	document.getElementById('hirefinish').options.length = 0;	// Empty location select box
	
	if(carCode.length>0){
		var index = ajax.length;
		ajax[index] = new sack();		
		ajax[index].requestFile = '/Connections/getLocations2.php?carCode='+carCode+'&locationCode='+locationCode;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createLocations2(index) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function createLocations2(index) {
	var obj = document.getElementById('hirefinish');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}


// -------------------------------------------------------------------------------------------------------

function getCarList(sel) {
	var locationCode = sel.options[sel.selectedIndex].value;
	var carCode = document.getElementById("Selected Vehicle").value;
	
	document.getElementById('Selected Vehicle').options.length = 0;	// Empty location select box
	if(locationCode.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		ajax[index].requestFile = '/Connections/getCars2.php?locationCode='+locationCode+'&carCode='+carCode;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createCars(index) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function createCars(index) {
	var obj = document.getElementById('Selected Vehicle');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

function resetQuoteBox() {
	document.getElementById('Selected Vehicle').options.length = 0;	// Empty car select box
		var index = ajax.length;
		ajax[index] = new sack();
		ajax[index].requestFile = '/Connections/getCars2.php?locationCode=0&carCode=0';	// Specifying which file to get
		ajax[index].onCompletion = function(){ createCars(index) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function

	document.getElementById('hirestart').options.length = 0;	// Empty location select box
		var index2 = ajax.length;
		ajax[index2] = new sack();
		ajax[index2].requestFile = '/Connections/getLocations2.php?locationCode=0&carCode=0&reset=1';	// Specifying which file to get
		ajax[index2].onCompletion = function(){ createLocations(index2) };	// Specify function that will be executed after file has been found
		ajax[index2].runAJAX();		// Execute AJAX function

	document.getElementById('hirefinish').options.length = 0;	// Empty location select box
		var index3 = ajax.length;
		ajax[index3] = new sack();
		ajax[index3].requestFile = '/Connections/getLocations2.php?locationCode=0&carCode=0&reset=1';	// Specifying which file to get
		ajax[index3].onCompletion = function(){ createLocations2(index3) };	// Specify function that will be executed after file has been found
		ajax[index3].runAJAX();		// Execute AJAX function

		var index4 = ajax.length;
		ajax[index4] = new sack();
		ajax[index4].requestFile = '/Connections/resetQuoteBox.php';	// Specifying which file to get
		ajax[index4].runAJAX();		// Execute AJAX function

		// Reset start and end dates
		// start date = tomorrow
		// end date = start date + 2
		var startDate = getObj('date1');	
		var startDate2 = getObj('date1a');
		
		var changeDate1 = new Date();
		changeDate1 =  parseInt(changeDate1.getDate() + 1, 10) + " " + monthName(changeDate1.getMonth()) + " " + changeDate1.getFullYear();
		startDate.value = changeDate1;
		var changeDate1 = new Date();
		changeDate1 =  parseInt(changeDate1.getDate() + 1, 10) + "-" + (changeDate1.getMonth()+1) + "-" + changeDate1.getFullYear();
		startDate2.value = changeDate1;

		var endDate = getObj('date2');	
		var endDate2 = getObj('date2a');

		var changeDate2 = new Date();
		changeDate2.setDate(changeDate2.getDate()+3);
		changeDate2 =  parseInt(changeDate2.getDate(), 10) + " " + monthName(changeDate2.getMonth()) + " " + changeDate2.getFullYear();
		endDate.value = changeDate2;
		var changeDate2 = new Date();
		changeDate2.setDate(changeDate2.getDate()+3);
		changeDate2 =  parseInt(changeDate2.getDate(), 10) + "-" + (changeDate2.getMonth()+1) + "-" + changeDate2.getFullYear();
		endDate2.value = changeDate2;

		// Reset start and end times
		// start and end times = 9:00 am
		
		var startHour = getObj('starthour');
		var startMin = getObj('startmin');
		//startHour.value = "09";
		
		var currentTime = new Date()
		var hours = currentTime.getHours()
		hours = hours + 1;
		if (hours > 23) {
			hours = 0;
		}
		if (hours < 10) {
			hours = "0" + hours;
		}
		startHour.value = hours;

		startMin.value = "00";

		var endHour = getObj('endhour');
		var endMin = getObj('endmin');		
		//endHour.value = "09";
		
		endHour.value = hours;
		endMin.value = "00";
		
		adjustNumberOfDays(); // Reset number of days
}

function monthName(monthNumber) {
	var monthName=new Array(12);
	monthName[0]="January";
	monthName[1]="February";
	monthName[2]="March";
	monthName[3]="April";
	monthName[4]="May";
	monthName[5]="June";
	monthName[6]="July";
	monthName[7]="August";
	monthName[8]="September";
	monthName[9]="October";
	monthName[10]="November";
	monthName[11]="December";

	return monthName[monthNumber];
}

