var httpEstimation = getHTTPObject();
var httpGraph = getHTTPObject();
var ordreTri=1;



function estimation_tri(ordre){
	ordreTri=ordre;
	valide_form_estimation(1);
}

function date_inferieure(date1,date2){
	var tabDate1=date1.split("\/");
	var tabDate2=date2.split("\/");
	if((tabDate1[1]<10) && (tabDate1[1].indexOf("0")==-1)){	plus1="0";	}else{	plus1="";	}
	if((tabDate1[0]<10) && (tabDate1[0].indexOf("0")==-1)){	plus2="0";	}else{	plus2="";	}
	if((tabDate2[1]<10) && (tabDate2[1].indexOf("0")==-1)){	plus3="0";	}else{	plus3="";	}
	if((tabDate2[0]<10) && (tabDate2[0].indexOf("0")==-1)){	plus4="0";	}else{	plus4="";	}
	var tmpDate1=tabDate1[2]+'-'+plus1+tabDate1[1]+'-'+plus2+tabDate1[0];
	var tmpDate2=tabDate2[2]+'-'+plus3+tabDate2[1]+'-'+plus4+tabDate2[0];
	if(tmpDate1<tmpDate2){	
		return true;	
	}else{	
		return false;	
	}
}


function valide_form_estimation(page){
	var val_depart_1=document.estimation_vol.estimation_depart_1.value;
	var val_depart_2=document.estimation_vol.estimation_depart_2.value;
	var val_retour_1=document.estimation_vol.estimation_retour_1.value;
	var val_retour_2=document.estimation_vol.estimation_retour_2.value;
	var val_indice=document.getElementById('estimation_indice').options[document.getElementById("estimation_indice").selectedIndex].value;
	var msg='';
	
	if(date_inferieure(val_depart_1,val_depart_2)){
	}else{
		msg=msg+'\nLa fecha de salida mínima debe ser inferior a la fecha de salida máxima.';
	}
	
	if(date_inferieure(val_retour_1,val_retour_2)){
	}else{
		msg=msg+'\nLa fecha de regreso mínima debe ser inferior a la fecha de regreso máxima.';
	}
	
	if(date_inferieure(val_depart_1,val_retour_1)){
	}else{
		msg=msg+'\nLa fecha de salida mínima debe ser inferior a la fecha de regreso mínima.';
	}
	
	if(msg!=""){
		alert('Las fechas mencionadas son incorrectas : \n--------------------------------------------------------------------------------------------'+msg);
	}else{
		//window.open("/vol_estimationPrix.php?depart1=" + val_depart_1 + "&depart2=" + val_depart_2 + "&retour1=" + val_retour_1 + "&retour2=" + val_retour_2 + "&indice=" + val_indice + "&iata_prov=" + iprov + "&iata_dest=" + idest + "&page=" + page + "&ordreTri=" + ordreTri);
		httpEstimation.open("GET","/vol_estimationPrix.php?depart1=" + val_depart_1 + "&depart2=" + val_depart_2 + "&retour1=" + val_retour_1 + "&retour2=" + val_retour_2 + "&indice=" + val_indice + "&iata_prov=" + iprov + "&iata_dest=" + idest + "&page=" + page + "&ordreTri=" + ordreTri, true);		
		httpEstimation.onreadystatechange = handleHttpResponseEstimation;		
		httpEstimation.send(null);
		
		//window.open("/vol_estimationGraph.php?depart1=" + val_depart_1 + "&depart2=" + val_depart_2 + "&retour1=" + val_retour_1 + "&retour2=" + val_retour_2 + "&indice=" + val_indice + "&iata_prov=" + iprov + "&iata_dest=" + idest);
		httpGraph.open("GET","/vol_estimationGraph.php?depart1=" + val_depart_1 + "&depart2=" + val_depart_2 + "&retour1=" + val_retour_1 + "&retour2=" + val_retour_2 + "&indice=" + val_indice + "&iata_prov=" + iprov + "&iata_dest=" + idest, true);		
		httpGraph.onreadystatechange = handleHttpResponseGraph;		
		httpGraph.send(null);		
	}
}


function handleHttpResponseEstimation(){
	if (httpEstimation.readyState == 4) {
				
		results = httpEstimation.responseText;
		tabResult = results.split("###");
		document.getElementById('div_estimation').innerHTML=tabResult[0];
		document.getElementById('div_estimation_page').innerHTML=tabResult[1];
	}
}

function handleHttpResponseGraph(){
	if (httpGraph.readyState == 4) {
				
		results = httpGraph.responseText;
		document.getElementById('estimationGraph').src=results;
	}
}