var xmlHttp
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

//=====================================================================================================
//===================calculates the price in the booking form===========================================
var url_price = "include/get_price.php?pickup="; 
function calculate_price() 
{ 
	//alert("ok");
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var pickup = document.getElementById("pickup").value; 

	var dropoff = document.getElementById("dropoff").value;

	var pickup_time = document.getElementById("pickup_time").value; 

	var dropoff_time = document.getElementById("dropoff_time").value;

	var car_id = document.getElementById("car_id").value;

	var location_from = document.getElementById("location_from").value;

	var one_day_rental_mess = document.getElementById("one_day_rental_mess").value;

	var company_id = document.getElementById("cid").value;

	var car_airport = document.getElementById("car_airport").value;

	var grace_hours = document.getElementById("grace_hours").value;
	

	pickup_time_arr = pickup_time.split(':');
	dropoff_time_arr = dropoff_time.split(':');

	pickup_time_arr[0] = parseFloat(pickup_time_arr[0]);
	dropoff_time_arr[0] = parseFloat(dropoff_time_arr[0]);
	
	valoare = pickup_time_arr[0] - dropoff_time_arr[0];


	if(dropoff_time_arr[0] - pickup_time_arr[0] > grace_hours || ((dropoff_time_arr[0] - pickup_time_arr[0] == grace_hours) && (pickup_time_arr[1] == "00") && (dropoff_time_arr[1] == "30")))
	{
		alert(one_day_rental_mess);
	}

	//alert("lala:"+url_price + pickup + "&dropoff=" + dropoff + "&pickup_time=" + pickup_time + "&dropoff_time=" + dropoff_time + "&car_id=" + car_id + "&location_from=" + location_from);

	xmlHttp.open("GET", url_price + pickup + "&dropoff=" + dropoff + "&pickup_time=" + pickup_time + "&dropoff_time=" + dropoff_time + "&car_id=" + car_id + "&location_from=" + location_from + "&company_id=" + company_id + "&car_airport=" + car_airport, true); 
	xmlHttp.onreadystatechange = handleHttpResponse_price; 
	xmlHttp.send(null);
}

function handleHttpResponse_price() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		//xmlDoc=xmlHttp.responseXML;
		//alert(xmlDoc.getElementsByTagName("nr_days2")[0].childNodes[0].nodeValue);
		if(document.onlinequote.sess_language_id.value == 1)
		{
			document.onlinequote.current_currency.value = 'euro';
			document.onlinequote.currency.value = 'euro';
		}
		xmlDoc=xmlHttp.responseXML;
		//xmlDoc.getElementsByTagName("drop")[0].childNodes[0].nodeValue+" ======== "+
		//alert(xmlDoc.getElementsByTagName("cars_groups_not_available")[0].childNodes[0].nodeValue);
		
		var mylist = xmlDoc.getElementsByTagName("cars_groups_not_available")[0].childNodes[0].nodeValue;
		document.onlinequote.nrdays.value = xmlDoc.getElementsByTagName("nr_days")[0].childNodes[0].nodeValue;
		document.onlinequote.car_name.value = xmlDoc.getElementsByTagName("car_name")[0].childNodes[0].nodeValue;
		document.onlinequote.total_price.value = xmlDoc.getElementsByTagName("total_price")[0].childNodes[0].nodeValue;
		document.onlinequote.total_price_gc.value = xmlDoc.getElementsByTagName("total_price_gc")[0].childNodes[0].nodeValue;
		document.onlinequote.total_price_gc2.value = xmlDoc.getElementsByTagName("total_price_gc2")[0].childNodes[0].nodeValue;

		var pickup_location_name = xmlDoc.getElementsByTagName("pickup_location_name")[0].childNodes[0].nodeValue;

		document.onlinequote.totalprice_euro.value = document.onlinequote.total_price.value;

		if(xmlDoc.getElementsByTagName("not_hire_car")[0].childNodes[0].nodeValue == 1) 
		{
			//alert(document.onlinequote.car_not_available.value);
			var car_not_available = document.onlinequote.car_not_available.value;
			var car_not_available_2 = document.onlinequote.car_not_available_2.value;
			var car_not_available_3 = document.onlinequote.car_not_available_3.value;
			var car_not_available_4 = document.onlinequote.car_not_available_4.value;

			//====adauagat de Irina pe 2 feb 2010 pt stop sale system
			if(mylist == 0)
			{
				document.getElementById("show_message").innerHTML = "<img src='images/no_booking.jpg'>"+car_not_available;
			}
			else
			{
				//=adaugam lista de grupuri care nu sunt disponibile pt perioada respectiva
				document.getElementById("show_message").innerHTML = "<img src='images/no_booking.jpg'>"+car_not_available_2+mylist+car_not_available_3+pickup_location_name+car_not_available_4;	
			}
			
			document.getElementById("show_message").style.display = "block";
		}
		else
		{
			document.getElementById("show_message").innerHTML = "";
			document.getElementById("show_message").style.display = "none";
			var final_price = document.getElementById("final_price").value;

			if(final_price == 'yes')
			{
				document.onlinequote.submit();
			}
		}
		//document.getElementById("calculated_price").value = "yes";
		//alert("pick = "+xmlDoc.getElementsByTagName("pick")[0].childNodes[0].nodeValue);
		//alert("drop = "+xmlDoc.getElementsByTagName("drop")[0].childNodes[0].nodeValue);
		//alert("days = "+xmlDoc.getElementsByTagName("not_hire_car")[0].childNodes[0].nodeValue);
		//alert(""+xmlDoc.getElementsByTagName("nr_days2")[0].childNodes[0].nodeValue);
		//document.getElementById("price").innerHTML=
		//xmlDoc.getElementsByTagName("price")[0].childNodes[0].nodeValue;

	} 
	
} 

//============================================================================================
//====================calculates the price in rentacar_book_personal.php================================
var url_price_extra = "include/get_price_extra.php?baby_seat="; 
function calculate_price_extra() 
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	
	var baby_seat = document.getElementById("baby_seat").value;
	var booster_seat = document.getElementById("booster_seat").value;
	var child_seat = document.getElementById("child_seat").value;
	var old_total_price = document.getElementById("old_total_price").value;
	var old_extra = document.getElementById("old_extra").value;
	var additional_drivers = document.getElementById("additional_drivers").value;
	var nr_days = document.getElementById("nr_days").innerHTML;
	

	
    //document.book_clubs.calculate_total_price.value = "Please wait...";


	//alert("lala:"+url_price + pickup + "&dropoff=" + dropoff + "&pickup_time=" + pickup_time + "&dropoff_time=" + dropoff_time + "&car_id=" + car_id);

	xmlHttp.open("GET", url_price_extra + baby_seat + "&booster_seat=" + booster_seat + "&child_seat=" + child_seat + "&nr_days=" + nr_days + "&old_total_price=" + old_total_price 
		+ "&old_extra=" + old_extra + "&additional_drivers=" + additional_drivers, true); 
	xmlHttp.onreadystatechange = handleHttpResponse_price_extra; 
	xmlHttp.send(null);
}

function handleHttpResponse_price_extra() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		xmlDoc=xmlHttp.responseXML;
		var pound = document.getElementById("pound").value;
		document.getElementById("span_total_price").innerHTML = xmlDoc.getElementsByTagName("total_price")[0].childNodes[0].nodeValue;
		document.getElementById("span_total_price_pound").innerHTML = Math.round((xmlDoc.getElementsByTagName("total_price")[0].childNodes[0].nodeValue / pound) * 100) / 100;

		document.getElementById("span_extras").innerHTML = parseFloat(xmlDoc.getElementsByTagName("old_extra")[0].childNodes[0].nodeValue) + parseFloat(xmlDoc.getElementsByTagName("extra_drivers")[0].childNodes[0].nodeValue);
		document.getElementById("span_extras_pound").innerHTML = Math.round(((parseFloat(xmlDoc.getElementsByTagName("old_extra")[0].childNodes[0].nodeValue) + parseFloat(xmlDoc.getElementsByTagName("extra_drivers")[0].childNodes[0].nodeValue)) / pound) * 100) / 100;

		document.onlinequote.total_price.value = xmlDoc.getElementsByTagName("total_price")[0].childNodes[0].nodeValue;
		document.onlinequote.extras.value = document.getElementById("span_extras").innerHTML
		document.onlinequote.old_extra.value = xmlDoc.getElementsByTagName("old_extra")[0].childNodes[0].nodeValue;


		document.onlinequote.baby_seat_price.value = xmlDoc.getElementsByTagName("baby_seat_price")[0].childNodes[0].nodeValue;
		document.onlinequote.child_seat_price.value = xmlDoc.getElementsByTagName("child_seat_price")[0].childNodes[0].nodeValue;
		document.onlinequote.booster_cushion_price.value = xmlDoc.getElementsByTagName("booster_cushion_price")[0].childNodes[0].nodeValue;
		document.onlinequote.add_driver_price.value = xmlDoc.getElementsByTagName("extra_drivers")[0].childNodes[0].nodeValue;

		//alert("baby = "+document.onlinequote.baby_seat_price.value+"child= "+document.onlinequote.child_seat_price.value+"booster = "+document.onlinequote.booster_cushion_price.value+"driver = "+document.onlinequote.add_driver_price.value)
	} 
} 

//=======================================================================================
//===============changes the car image in the booking form================================
var url = "include/get_image.php?car_id="; 

function getimage() 
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var my_car_id = document.getElementById("car_id").value; 

	xmlHttp.open("GET", url + my_car_id, true); 
	xmlHttp.onreadystatechange = handleHttpResponse; 
	xmlHttp.send(null);
}

function handleHttpResponse() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		xmlDoc=xmlHttp.responseXML;
		document.getElementById("image_car").src = xmlDoc.getElementsByTagName("image")[0].childNodes[0].nodeValue;
	} 
}

//================================================================================================
//============START function added to show cars depending on the chosen location=====================
//============added by IRINA on 21 april 2009===================================================
function get_cars()
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var url_img = "include/get_cars.php?location_from="; 
	var location_from = document.getElementById("location_from").value; 

	var car_group = document.getElementById("car_group").value;

	curent_select_cars = document.getElementById("car_id");
	curent_select_cars.options[0] = new Option("Loading ...", "0|0");
	curent_select_cars.selectedIndex = 0;
	

	xmlHttp.open("GET", url_img + location_from + "&car_group=" + car_group, true); 
	xmlHttp.onreadystatechange = handleHttpResponse_cars; 
	xmlHttp.send(null);
}

//===================================================================================================================
function handleHttpResponse_cars()
{
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		
		xmlDoc=xmlHttp.responseXML;

		curent_select_cars = document.getElementById("car_id");

		var select_a_car = document.getElementById("select_a_car").value;

		curent_select_cars.options[0] = new Option(select_a_car+" ...", "0|0");
		curent_select_cars.options.length  = 1;

		

		var respNode = xmlDoc.getElementsByTagName("response")[0];
		var cars = respNode.getElementsByTagName("car");


		if(cars.length > 0)
		{
	
				for (var i=0; i<cars.length; i++)
				{
					var car_name = cars[i].getElementsByTagName("car_name")[0].firstChild.nodeValue;
					var car_value = cars[i].getElementsByTagName("car_value")[0].firstChild.nodeValue;

					var objOption=document.createElement("option");

					objOption.innerHTML = car_name;
					objOption.value = car_value;
					
					curent_select_cars.options[i+1] = new Option(car_name, car_value);
					if(isNaN(car_value))
					{
						curent_select_cars.options[i+1].className="blue_bg";
						//curent_select_cars.options[car_value].parentNode.label;
					}

				}
		}
		curent_select_cars.selectedIndex = 0;

	} 

}
//================================================================================================
//============END function added to show cars depending on the chosen location=====================
//============added by IRINA on 21 april 2009===================================================


//=================================================================================================================
//==========================START for the admin section============================================================
//=======================created by IRINA on 17 March 2009=========================================================
//=================================================================================================================
var admin_url_price = "../include/get_price.php?pickup="; 
function admin_calculate_price() 
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var pickup = document.getElementById("pickup").value; 

	var dropoff = document.getElementById("dropoff").value;

	var pickup_time = document.getElementById("pickup_time").value; 

	var dropoff_time = document.getElementById("dropoff_time").value;

	var car_id = document.getElementById("car_id").value;

	var location_from = document.getElementById("location_from").value;

	var one_day_rental_mess = document.getElementById("one_day_rental_mess").value;

	var grace_hours = document.getElementById("grace_hours").value;

	pickup_time_arr = pickup_time.split(':');
	dropoff_time_arr = dropoff_time.split(':');

	pickup_time_arr[0] = parseFloat(pickup_time_arr[0]);
	dropoff_time_arr[0] = parseFloat(dropoff_time_arr[0]);
	
	valoare = pickup_time_arr[0] - dropoff_time_arr[0];


	if(dropoff_time_arr[0] - pickup_time_arr[0] > grace_hours || ((dropoff_time_arr[0] - pickup_time_arr[0] == grace_hours) && (pickup_time_arr[1] == "00") && (dropoff_time_arr[1] == "30")))
	{
		alert(one_day_rental_mess);
	}

	//alert("lala:"+admin_url_price + pickup + "&dropoff=" + dropoff + "&pickup_time=" + pickup_time + "&dropoff_time=" + dropoff_time + "&car_id=" + car_id + "&location_from=" + location_from, true); 
	

	xmlHttp.open("GET", admin_url_price + pickup + "&dropoff=" + dropoff + "&pickup_time=" + pickup_time + "&dropoff_time=" + dropoff_time + "&car_id=" + car_id + "&location_from=" + location_from, true); 
	xmlHttp.onreadystatechange = admin_handleHttpResponse_price; 
	xmlHttp.send(null);
}

function admin_handleHttpResponse_price() 
{ 
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		xmlDoc=xmlHttp.responseXML;
	
		//alert(xmlDoc.getElementsByTagName("nr_days")[0].childNodes[0].nodeValue);
		document.onlinequote.nr_days.value = xmlDoc.getElementsByTagName("nr_days")[0].childNodes[0].nodeValue;
		//alert("nr_days = "+document.onlinequote.nr_days.value);
		//noul pret al inchirierii masinii, obtinut prin ajax
		document.onlinequote.car_hire_price.value = xmlDoc.getElementsByTagName("total_price")[0].childNodes[0].nodeValue;
		//noul pret total = pretul inchirierii masinii + extra options
		//alert(document.onlinequote.total_price.value);
		document.onlinequote.total_price.value = parseFloat(document.onlinequote.car_hire_price.value) + parseFloat(document.onlinequote.booking_extras.value);
		//alert(document.onlinequote.total_price.value);

		//==added 1 July 2010 - goldcar price====
		document.onlinequote.total_price_gc.value = xmlDoc.getElementsByTagName("total_price_gc")[0].childNodes[0].nodeValue;
		document.onlinequote.total_price_gc2.value = xmlDoc.getElementsByTagName("total_price_gc2")[0].childNodes[0].nodeValue;

		document.getElementById("div_nr_days").innerHTML = document.onlinequote.nr_days.value + ' days';
		document.onlinequote.old_total_price.value = document.onlinequote.car_hire_price.value;
		document.onlinequote.booking_extras.focus();

		if(xmlDoc.getElementsByTagName("not_hire_car")[0].childNodes[0].nodeValue == 1) 
		{
			alert(document.onlinequote.car_not_available.value);
		}
		else
		{
			if(document.onlinequote.final_price.value == 'yes')
			{
				document.onlinequote.submit();
			}
		}
		

	} 
	
} 



var admin_url_price_extra = "../include/get_price_extra.php?baby_seat="; 
function admin_calculate_price_extra() 
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	
	var baby_seat = document.getElementById("baby_seat").value;
	var booster_seat = document.getElementById("booster_seat").value;
	var child_seat = document.getElementById("child_seat").value;
	var old_total_price = document.getElementById("old_total_price").value;
	var old_extra = document.getElementById("old_extra").value;
	var additional_drivers = document.getElementById("additional_drivers").value;
	var nr_days = document.getElementById("nr_days").value;

	//alert("lala:"+admin_url_price_extra + baby_seat + "&booster_seat=" + booster_seat + "&child_seat=" + child_seat + "&nr_days=" + nr_days + "&old_total_price=" + old_total_price 
	//	+ "&old_extra=" + old_extra + "&additional_drivers=" + additional_drivers);

	xmlHttp.open("GET", admin_url_price_extra + baby_seat + "&booster_seat=" + booster_seat + "&child_seat=" + child_seat + "&nr_days=" + nr_days + "&old_total_price=" + old_total_price 
		+ "&old_extra=" + old_extra + "&additional_drivers=" + additional_drivers, true); 
	xmlHttp.onreadystatechange = admin_handleHttpResponse_price_extra; 
	xmlHttp.send(null);
}

function admin_handleHttpResponse_price_extra() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		xmlDoc=xmlHttp.responseXML;
		//alert(xmlDoc.getElementsByTagName("total_price")[0].childNodes[0].nodeValue);
		
		document.onlinequote.total_price.value = xmlDoc.getElementsByTagName("total_price")[0].childNodes[0].nodeValue;
		document.onlinequote.booking_extras.value = parseFloat(xmlDoc.getElementsByTagName("old_extra")[0].childNodes[0].nodeValue) + parseFloat(xmlDoc.getElementsByTagName("extra_drivers")[0].childNodes[0].nodeValue);
		document.onlinequote.old_extra.value = xmlDoc.getElementsByTagName("old_extra")[0].childNodes[0].nodeValue;
		document.onlinequote.booking_extras.blur();

		document.onlinequote.baby_seat_price.value = xmlDoc.getElementsByTagName("baby_seat_price")[0].childNodes[0].nodeValue;
		document.onlinequote.child_seat_price.value = xmlDoc.getElementsByTagName("child_seat_price")[0].childNodes[0].nodeValue;
		document.onlinequote.booster_cushion_price.value = xmlDoc.getElementsByTagName("booster_cushion_price")[0].childNodes[0].nodeValue;
		document.onlinequote.add_driver_price.value = xmlDoc.getElementsByTagName("extra_drivers")[0].childNodes[0].nodeValue;

		//alert("baby = "+document.onlinequote.baby_seat_price.value+"child= "+document.onlinequote.child_seat_price.value+"booster = "+document.onlinequote.booster_cushion_price.value+"driver = "+document.onlinequote.add_driver_price.value)
	} 
} 

//=================================================================================================================
//==========================END for the admin section==============================================================
//=======================created by IRINA on 17 March 2009=========================================================
//=================================================================================================================

//=======for admin - to calculate ACH.es prices having the GoldCar prices===
//=======added by Irina on 19 May 2010======================================
var admin_achs_url_price = "../include/get_admin_achs_price.php?price="; 
function admin_calculate_ach_es_price(price, price_type, achsprice_id, block_price_id)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	//car_id = document.getElementById("cid").value;
	if(document.getElementById(block_price_id).checked === true)
	{
		block_price_id_value = 1;
	}
	else
	{
		block_price_id_value = 0;
	}

	xmlHttp.open("GET", admin_achs_url_price + price + "&price_type=" + price_type + "&achsprice_id=" + achsprice_id + "&block_price_id=" + block_price_id_value, true); 
	xmlHttp.onreadystatechange = admin_handleHttpResponse_calculate_ach_es_price; 
	xmlHttp.send(null);
}

function admin_handleHttpResponse_calculate_ach_es_price()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		xmlDoc=xmlHttp.responseXML;
		price_type = xmlDoc.getElementsByTagName("price_type")[0].childNodes[0].nodeValue;
		achs_price_id = xmlDoc.getElementsByTagName("achs_price_id")[0].childNodes[0].nodeValue;
		new_price_value = xmlDoc.getElementsByTagName(price_type)[0].childNodes[0].nodeValue;
		goldcarprice = xmlDoc.getElementsByTagName("goldcarprice")[0].childNodes[0].nodeValue;

		if((new_price_value != 0) || ((new_price_value == 0) && (goldcarprice == 0)))
		{
			document.getElementById(achs_price_id).value = new_price_value;
			document.getElementById(achs_price_id).style.color = "#008000";
		}
		//alert(xmlDoc.getElementsByTagName(price_type)[0].childNodes[0].nodeValue);
	} 

}

function block_price(check_box_name, achs_price_id)
{
	f = document.search;
	if(eval("f."+check_box_name+".checked") === true)
	{
		document.getElementById(achs_price_id).style.color = "#004080";
	}

	if(eval("f."+check_box_name+".checked") === false)
	{
		f.check_block_price_all.checked = false;
	}
	
}

function block_all_price()
{
	f = document.search;
	prices_arr = new Array();
	prices_arr[0] = "low";
	prices_arr[1] = "mid";
	prices_arr[2] = "high";
	prices_arr[3] = "extra";
	prices_arr[4] = "mid_canary";
	prices_arr[5] = "high_canary";
	if(f.check_block_price_all.checked === true)
	{
		for(i = 0; i < prices_arr.length; i++)
		{
			//alert("val = check_block_price_"+prices_arr[i]);
			document.getElementById("check_block_price_"+prices_arr[i]).checked = true;
			document.getElementById("aches_"+prices_arr[i]+"price").style.color = "#004080";
		}
	}
	else
	{
		for(i = 0; i < prices_arr.length; i++)
		{
			document.getElementById("check_block_price_"+prices_arr[i]).checked = false;
			document.getElementById("aches_"+prices_arr[i]+"price").style.color = "#008000";
		}
	}
}

function block_all_price_promo(car_groups_number)
{
	f = document.search;
	//alert(df);
	if(f.check_block_price_all.checked === true)
	{
		for(i = 1; i <= car_groups_number; i++)
		{
			document.getElementById("check_block_price_"+i).checked = true;
			document.getElementById("price_"+i).style.color = "#004080";
		}
	}
	else
	{
		for(i = 1; i <= car_groups_number; i++)
		{
			document.getElementById("check_block_price_"+i).checked = false;
			document.getElementById("price_"+i).style.color = "#008000";
		}
	}
}


function activate_all_prices(car_groups_number)
{
	if(document.getElementById("active_all_prices").checked === true)
	{
		for(i = 1; i <= car_groups_number; i++)
		{
			document.getElementById("active_"+i).checked = true;
		}
	}
	else
	{
		for(i = 1; i <= car_groups_number; i++)
		{
			document.getElementById("active_"+i).checked = false;
		}
	}
}

function deactivate_all_prices(i)
{
	if((document.getElementById("active_"+i).checked === false) && (document.getElementById("active_all_prices").checked === true))
	{
		document.getElementById("active_all_prices").checked = false;
	}
}