// SET ALL PRICE VALUES
var del_cost = 0,count,season, fcost, curc, foo, paid;
var discount;
var discount1 = 0.90;
var discount2 = 0.90;// HS

var pricesbygroup = new Array();
//PRICELIST | CAR | 1WEEK (LS) | 2WEEKS (LS) | 1WEEK (HS) | 2WEEKS (HS) | 1WEEK (WS) | 2WEEKS (WS) | MY QUOTED PRICE | 1WEEK (ES) | 2WEEKS (ES)
pricesbygroup[0]= new Array('A','169','309', '215' , '399', '135','260',parseInt(0),'189','359');
pricesbygroup[1]= new Array('B','189','349', '239' , '439', '155','290',parseInt(0),'215','389');
pricesbygroup[2]= new Array('C','215','395', '259' , '469', '175','320',parseInt(0),'235','415');
pricesbygroup[3]= new Array('D','249','439', '315' , '519', '190','360',parseInt(0),'259','440');
pricesbygroup[4] =new Array('D1','299','549', '349' , '599', '229','400',parseInt(0),'315','580');
pricesbygroup[5]= new Array('E','330','590', '399' , '750', '280','520',parseInt(0),'369','710');
pricesbygroup[6]= new Array('F','289','499', '339' , '649', '240','445',parseInt(0),'289','499');
pricesbygroup[7]= new Array('F1','380','680', '450' , '890', '315','580',parseInt(0),'440','780');
pricesbygroup[8]= new Array('G','450','790', '550' , '1030', '399','699',parseInt(0),'450','790');
pricesbygroup[9]= new Array('H','349','690', '450' , '890', '310','590',parseInt(0),'349','690');
pricesbygroup[10]= new Array('I','385','790', '560' , '1080', '340','660',parseInt(0),'385','790');
pricesbygroup[11]= new Array('K','285','499', '330' , '620', '220','400',parseInt(0),'299','559');
pricesbygroup[12]= new Array('K1','285','499', '330' , '620', '220','400',parseInt(0),'299','559');
pricesbygroup[13]= new Array('L','375','740', '490' , '900', '350','680',parseInt(0),'420','790');
pricesbygroup[14]= new Array('L1','419','819', '559' , '1079', '419','819',parseInt(0),'450','850');
pricesbygroup[15]= new Array('L2','550','1099', '599' , '1149', '449','849',parseInt(0),'630','1199');
pricesbygroup[16]= new Array('J','1800','3500', '2100' , '5200', '1800','3500',parseInt(0),'1800','3500');


function fix_dates(){	
	if (!fixBridge()){
		fix_dfields();
	}
}
function fix_dfields(){
	today = new Date();
	today.setDate(today.getDate()+1);
	document.onlinequote.pickupdate.value = today.toString().substr(0,3)+" "+today.getDate()+" "+today.toString().substr(4,3)+" "+today.getFullYear();
	today.setDate(today.getDate()+7);
	document.onlinequote.returndate.value = today.toString().substr(0,3)+" "+today.getDate()+" "+today.toString().substr(4,3)+" "+today.getFullYear();	
}
function calc(){
	var ttemp = document.onlinequote.pickupdate.value;
	dateP = new Date(ttemp);
	dateP.setHours(document.onlinequote.Phour.value);
	ttemp = document.onlinequote.returndate.value;
	dateR = new Date(ttemp);
	dateR.setHours(document.onlinequote.Rhour.value);
	
	//calc duration
	count = (dateR - dateP) / 1000 / 3600 / 24;
	count = Math.ceil(count);
	if (dateP < new Date()){
		alert("La date de départ doit être au plus tard aujourd'hui!!");
		}
	else if (dateR < dateP){
		alert('La date de départ doit être postérieure à la date de retour!');
		}
	else if (count < 4){
		alert('contactez-nous pour la location jours de moins que 4!\n\ndonne maintenant +30 2810 240120\nCourrier électronique: info@location-voiture-crete.fr\nEn direct de notre page de contact');
		}
	else
		{
		if ((count <7 )&&(count >3))
			{//count=7
			}
		else if ((count == 13))
			{count=14}
			document.getElementById('fill-box').style.display = 'none';
			document.getElementById('conf-box').style.display = 'block';
			document.getElementById('compact').checked = true;
			document.getElementById('large').checked = true;
			document.getElementById('4WD').checked = true;
			document.getElementById('vans').checked = true;
			document.getElementById('cabriolet').checked = true;
		showcars();
		}
		
}
function showcars(){
	getSeason();
	document.getElementById('conf1').innerHTML = document.onlinequote.pickupdate.value+", "+document.onlinequote.Phour.value+":00 hrs<br>"+document.onlinequote.pickup.value.split("_").join(" ");
	document.getElementById('conf2').innerHTML = document.onlinequote.returndate.value+", "+document.onlinequote.Rhour.value+":00 hrs<br>"+document.onlinequote.retur.value.split("_").join(" ");
			
	document.getElementById('conf3').innerHTML = "<img src='images/check_m.gif' border='0'/> Tout Inclus<br>";
	if (document.onlinequote.pay1.checked)
		document.getElementById('conf3').innerHTML += "<img src='images/check_m.gif' border='0'/> Payer &aacute; l'arriv&eacute;e"
		else
		document.getElementById('conf3').innerHTML += "<img src='images/check_m.gif' border='0'/> Pr&eacute;paiement"
	document.getElementById('conf3').innerHTML += "<br><img src='images/check_m.gif' border='0'/> Journ&eacute;es: " + count;

	if (document.onlinequote.booster.checked) document.getElementById('conf4').innerHTML += "1 x Les si&egrave;ge b&eacute;b&eacute;<br>";
	if (document.onlinequote.babyseat.checked) document.getElementById('conf4').innerHTML += "1 x Les si&egrave;ge enfants<br>";
	if (document.onlinequote.gps.checked) document.getElementById('conf4').innerHTML += "1 x GPS";
	
	//document.getElementById('banners').style.display = 'none';
	if(getcarprice(count))	show_listings()
}
function getSeason(){
	season = 0;
	cur_y = dateP.getFullYear();	
	if (dateP > new Date (cur_y, 3, 25 ) && dateP <= new Date (cur_y, 8, 30 )){
		season = 'LS';
		if (dateP > new Date (cur_y, 6, 1 ) && dateP <= new Date (cur_y, 7, 30 ))
		season = 'HS';
	}
	else 
		season = 'WS';	
	//EXTRA SEASON	
	if (dateP > new Date (cur_y, 7, 24 ) && dateP <= new Date (cur_y, 8, 15 ))
		season = 'ES';

}

function getcarprice(duration){
		// ADD DELIVERY COST
		check_del();
		// PRICE CALCULATION
		var ss=0;
		if (season=='WS')
			if (3 < duration && duration < 14) ss = 5
			else if (duration > 13)	ss = 6
			else ss=10 //ERROR	
		else if (season=='LS')
			if (3 < duration && duration < 14) ss = 1
			else if (duration > 13)	ss = 2
			else ss=10 //ERROR	
		else if (season=='HS')
			if (3 < duration && duration < 14) ss = 3
			else if (duration > 13)	ss = 4
			else ss=10 //ERROR
		else 
			if (3 < duration && duration < 14) ss = 8
			else if (duration > 13)	ss = 9
			else ss=10 //ERROR
		if (ss!=10){
			for (var i=0; i<17; i++)
			{
				if (duration>13) pricesbygroup[i][7] = parseInt((pricesbygroup[i][ss] / 14) * duration) + del_cost;
				else if (duration < 7 )pricesbygroup[i][7] = parseInt((pricesbygroup[i][ss] / 7) * 7) + del_cost - 1;
				else pricesbygroup[i][7] = parseInt((pricesbygroup[i][ss] / 7) * duration) + del_cost;
					
					var tmp2 = 0;
					if (count <8 )tmp2 = 7
					
					if (document.onlinequote.pay2.checked){
						if((season=='HS')||(season=='ES')){
							paid = discount2;
						}else{
							paid = discount1;
							}
					}else paid = 1
					
					//alert (paid);
					document.getElementById('price'+i).innerHTML = "&euro; " + (pricesbygroup[i][7]/count).toFixed(2);
					document.getElementById('tprice'+i).innerHTML = "&euro; " + (pricesbygroup[i][7]).toFixed(0);
			};
			return true;
		}
}
function show_listings(){
	if (document.onlinequote.pay2.checked)
		doToggle(0)
	else doToggle(1)
		
	for (var k=0; k<17; k++)
		document.getElementById('group'+k).style.display = "block";	
}
function hide_listings(){
	if (document.onlinequote.pay2.checked)
		doToggle(0)
	else doToggle(1)
		
	for (var l=0; l<17; l++)
		document.getElementById('group'+l).style.display = "none";
}
function mygroup(sel_group){
	switch (sel_group) {
 		case 0:	group = "Groupe A. Mini";tmp_price = document.getElementById('tprice0').innerHTML;break;
	    case 1:	group = "Groupe B. Economique";tmp_price = document.getElementById('tprice1').innerHTML;break;
	    case 2:	group = "Groupe C. Compacte";tmp_price = document.getElementById('tprice2').innerHTML;break;
	    case 3:	group = "Groupe D. Standard";tmp_price = document.getElementById('tprice3').innerHTML;break;
	    case 4:	group = "Groupe D1. Familiales";tmp_price = document.getElementById('tprice4').innerHTML;break;
	    case 5:	group = "Groupe E. Intermediaire";tmp_price = document.getElementById('tprice5').innerHTML;break;
	    case 6:	group = "Groupe F. Jeep-Mini";tmp_price = document.getElementById('tprice6').innerHTML;break;
	    case 7:	group = "Groupe F1. Jeep-Full";tmp_price = document.getElementById('tprice7').innerHTML;break;
	    case 8:	group = "Groupe G. SUV";tmp_price = document.getElementById('tprice8').innerHTML;break;
	    case 9:	group = "Groupe H. Mini-bus";tmp_price = document.getElementById('tprice9').innerHTML;break;
	    case 10:	group = "Groupe I. Bus";tmp_price = document.getElementById('tprice10').innerHTML;break;
	    case 11:	group = "Groupe K. Automatique";tmp_price = document.getElementById('tprice11').innerHTML;break;
	    case 12:	group = "Groupe K1. Automatique";tmp_price = document.getElementById('tprice12').innerHTML;break;
	    case 13:	group = "Groupe L. Cabiolet";tmp_price = document.getElementById('tprice13').innerHTML;break;
	    case 14:	group = "Groupe L1. Cabiolet";tmp_price = document.getElementById('tprice14').innerHTML;break;
	    case 15:	group = "Groupe L2. Cabiolet";tmp_price = document.getElementById('tprice15').innerHTML;break;
	    case 16:	group = "Groupe J. Luxe";tmp_price = document.getElementById('tprice16').innerHTML;break;
	    default: group = "-";break;       
	    } 
		
		if (document.onlinequote.pay2.checked){
			document.onlinequote.fullpay.value = "PREPAID";
						if((season=='HS')||(season=='ES')){
							discount = discount2;
						}else{
							discount = discount1;
							}
			document.onlinequote.price.value = (tmp_price.substr(2)*1) + Math.floor((tmp_price.substr(2) / discount)-(tmp_price.substr(2) * 1.00));
			document.onlinequote.discountedprice.value = ((tmp_price.substr(2) * 1)).toFixed(0);
		}
		else{
			document.onlinequote.fullpay.value = "PAY ON ARRIVAL";
						if(season=='HS'){
							discount = discount2;
						}else{
							discount = discount1;
							}
			document.onlinequote.price.value = ((tmp_price.substr(2) * 1)).toFixed(0);
			document.onlinequote.discountedprice.value = (discount * (tmp_price.substr(2) * 1)).toFixed(0);
		}
		document.onlinequote.group.value = group;
		document.onlinequote.days.value = count;
				
		document.onlinequote.fullbos.value = 'No';
		document.onlinequote.fullbas.value = 'No';
		document.onlinequote.fullgps.value = 'No';
	if (document.onlinequote.booster.checked) document.onlinequote.fullbos.value = 'Yes';
	if (document.onlinequote.babyseat.checked) document.onlinequote.fullbas.value = 'Yes';
	if (document.onlinequote.gps.checked) document.onlinequote.fullgps.value = 'Yes';
		
		document.onlinequote.submit();
		
		
}
function restart_quote(){
	hide_listings();
	document.getElementById('fill-box').style.display = 'block';
	document.getElementById('conf-box').style.display = 'none';
	//document.getElementById('banners').style.display = 'block';
	document.getElementById('conf4').innerHTML = "";
}

function check_del(){	
	var area = document.onlinequote.pickup.options[document.onlinequote.pickup.selectedIndex].parentNode.label;
	if (area == "Heraklion Area")	{del_cost = 0;}
	else if (area == "Chania Area")	{del_cost = 25;}
	else if (area == "Rethymno Area")	{del_cost = 19;}
	else	{del_cost = 15;}
	del_cost = del_cost + check_oneway();
}
function check_oneway(){
	var oneway = 0;//idc = document.onlinequote.pickup.selectedIndex;
	tmp1 = document.onlinequote.pickup.options[document.onlinequote.pickup.selectedIndex].parentNode.label;
	tmp2 = document.onlinequote.pickup.options[document.onlinequote.retur.selectedIndex].parentNode.label;
	if ((tmp1!=tmp2)&&(document.onlinequote.retur.value!="identique")){
		oneway = 15;
	}
	return oneway;
	
}

function bookmarksite(title, url){
if (document.all)
window.external.AddFavorite(url, title);
else if (window.sidebar)
window.sidebar.addPanel(title, url, "")
}
function addZero(num)
{
	(String(num).length < 2) ? num = String("0" + num) :  num = String(num);
	return num;		
}
function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
} 
function fixBridge(){
	//alert(getQuerystring('PUP').length);
	var flg = false;
	if(getQuerystring('PUP').length > 0) {document.onlinequote.pickup.value=getQuerystring('PUP'); flg=true;}
	if(getQuerystring('REP').length > 0) {document.onlinequote.retur.value=getQuerystring('REP'); flg=true;}
	if(getQuerystring('PUD').length > 0) {document.onlinequote.pickupdate.value=getQuerystring('PUD').split("_").join(" "); flg=true;}
	else{fix_dfields()}
	if(getQuerystring('RED').length > 0) {document.onlinequote.returndate.value=getQuerystring('RED').split("_").join(" "); flg=true;}
	if(getQuerystring('PUH').length > 0) {document.onlinequote.Phour.value=getQuerystring('PUH'); flg=true;}
	if(getQuerystring('REH').length > 0) {document.onlinequote.Rhour.value=getQuerystring('REH'); flg=true;}
	//document.onlinequote.pickup.value=getQuerystring('PUP');
	//document.onlinequote.pickup.value=getQuerystring('PUP');
	//document.onlinequote.pickup.value=getQuerystring('PUP');
	//alert(getQuerystring('PUP'));
	return true;
}

function appear(arr, dom1){
	if(dom1.checked){
	for (var i=0;i<arr.length;i++){	
			document.getElementById('group'+arr[i]).style.display = "block";
		}
	}
	else{	
	for (var i=0;i<arr.length;i++){	
			document.getElementById('group'+arr[i]).style.display = "none";
		}
	}
}

function doToggle(btns){
		var jz1=document.getElementsByName("prepay_img");
		var jz2=document.getElementsByName("poa_img");
		for(i = 0; i < jz1.length; i++){		
			if (btns==0){
				jz1[i].src ='images/prepay_active.gif';
				jz2[i].src ='images/poa_inactive.gif';
			}
			else{
				jz1[i].src ='images/prepay_inactive.gif';
				jz2[i].src ='images/poa_active.gif';
			}
	    }	
		
		if (btns==0){
			discounted('minus');
			//document.onlinequote.pay1.checked = 'false';
			document.onlinequote.pay2.checked = 'true';
			//alert(document.onlinequote.pay2.checked);
		}
		else{
			discounted('plus');
			document.onlinequote.pay1.checked = 'true';
			//alert(document.onlinequote.pay2.checked);
			//document.onlinequote.pay2.checked = 'false';
		}
				
}
function discounted(what){
	var coloring;
	if (what=='minus'){
						if((season=='HS')||(season=='ES')){
							paid = discount2;
						}else{
							paid = discount1;
							}
			coloring = 'green';
	}
	else{
			paid = 1;
			coloring = 'maroon';
	}
	for (i=0;i<17;i++){
		document.getElementById('price'+i).innerHTML = "&euro; " + ((paid * pricesbygroup[i][7])/count).toFixed(2);
		document.getElementById('tprice'+i).innerHTML = "&euro; " + ((paid * pricesbygroup[i][7])).toFixed(0);
		
	}
	
	for(i = 0; i < document.getElementsByName("pricing").length; i++){		
		document.getElementsByName("pricing")[i].style.color = coloring;
	}	
	
	var x = document.getElementsByTagName('div');

	for(i=0;i<x.length;i++) { 
		if(x[i].getAttribute('name')=='pricing'){
		x[i].style.color = coloring;
		}
	}
	
}
