﻿var valErrorDesc = "";
var isError = 0;
var advancedSearch = false;
var smilesSearch = false;
var whiteLabel = true;
var airlineDedicatedPageFrom = false;
var publicPath = "http://www.e-vacances.fr/publicvac/";

function initAll()
{
	initBehavioursAndPaths ();
	initOrigins ();
	initErrorManagement();
}
function initOrigins ()
{	
	//Test if the client comes from another site (ex: FREE)
	if (!this.frameOrigin)
	{
		frameOrigin="NOFRAME";
	}    
	//if origin is not defined, we try to define it another way, or set it to "".
	if (!this.Session_Origin)
    	{
		if (document.LinkForm.origin.value && document.LinkForm.origin.value != "")
		{
			this.Session_Origin = "?origin=" + document.LinkForm.origin.value;
			document.submitVols.origin.value = document.LinkForm.origin.value;
		}
		else
		{
	  		this.Session_Origin="";
   		}
	}
}
function initBehavioursAndPaths ()
{
	//check if we come from a jsp
	/*var splitedURL = document.URL.split("/");
	if (splitedURL[splitedURL.length-2]=="jsp")
	{
		publicPath = "../";
	}
	//white label behaviour
	if(whiteLabel)
	{
		publicPath = "../../../../";
	}*/
}
function initErrorManagement()
{
	//we clean error message from previous operations
	valErrorDesc = "";
	isError1 = 0;
}

function monthAsInt(monthName)
{
	var m = 0;
	switch(monthName)
	{
		case 'JAN':m=0;break;
		case 'FEV':m=1;break;
		case 'MAR':m=2;break;
		case 'AVR':m=3;break;
		case 'MAI':m=4;break;
		case 'JUN':m=5;break;
		case 'JUL':m=6;break;
		case 'AOU':m=7;break;
		case 'SEP':m=8;break;
		case 'OCT':m=9;break;
		case 'NOV':m=10;break;
		case 'DEC':m=11;break;
		default:break;
	}
	return m;
}

function createDate(inDay, inMonthYear)
{
	
	var a = inDay*1;
	var b = inMonthYear*1;
	result = new Date(new Date().getFullYear(), b, a);
	if (result.getTime()<=(new Date()).getTime()) {
	  result.setFullYear(result.getFullYear()+1);
	}
	return result;
	/*var depMonthArray = inMonthYear.split(" ");
	var depMonth = depMonthArray[0];
	var depYear = depMonthArray[1];
	
	var a = inDay*1;
	var b = monthAsInt(depMonth)*1;
	var c = depYear*1;
	
	result = new Date(c,b,a);
	
	if(result.getMonth()==b)
	{
		return result;
	}
	else
	{
		return new Date(100000, 1, 1);
	}*/

}

function pagerechercheVols(){
  //advancedSearch=true;
  initAll();
  createRequestFlight();
  submitRecherche();  
}

function rechercheVols() {
  initAll();
  createRequestFlight();
  submitEffective();
}

function addError(msg) {
	isError=1;
	if (valErrorDesc != "") {valErrorDesc = valErrorDesc + "\n";}
	valErrorDesc = valErrorDesc + msg;
}

function dateOk(df) {
	if (df == null || df.length < 7) {
		return false;
  }
	else
	{
		var day = parseInt(df.substring(6)*1);
		var month = parseInt(df.substring(4,6)*1) - 1;
		var year = parseInt(df.substring(0,4));
		return !(isNaN(day) || isNaN(month) || isNaN(year) || day != (new Date(year, month, day)).getDate());
	}
}
function dateOkUsingDate(y, m, d) 
{
    with (new Date(y, m, d)) 
    {
    	d = d.toString();
    	if(d.indexOf("0")==0 && d.length==2)
	     d = d.charAt(1); 	            	   
        return ((getFullYear().toString() == y.toString()) && (getMonth().toString() == m.toString())&& (getDate().toString() == d.toString()));      
    }
}

function createFlightDates ()		       	
{
	with(document.submitVols)
	{
		
		valDepDateSelected = createDate(ctrVolDepartJour.value, ctrVolDepartMois.value - 1);
		valArrDateSelected = createDate(ctrVolRetourJour.value, ctrVolRetourMois.value - 1);
		
		
		//check A/R
		if (valDepDateSelected == valArrDateSelected)
			SEARCH_BY.value = 1;
		else
			SEARCH_BY.value = 2;
		D_Day.value = ctrVolDepartJour.value;
		R_Day.value = ctrVolRetourJour.value;
		D_Month.value = valDepDateSelected.getFullYear()*100+valDepDateSelected.getMonth()+1;
		R_Month.value = valArrDateSelected.getFullYear()*100+valArrDateSelected.getMonth()+1;
		
		
		//controls if flexible dates selected then, return dat - departure date must not be inferiror or equal to 3
		if (!dateOk(D_Month.value + D_Day.value)) {
			addError("La date de départ que vous avez choisie est incorrecte.");
	 	}
		if (!dateOk(R_Month.value + R_Day.value)) {
			addError("La date de retour que vous avez choisie est incorrecte.");		
		}
		if (document.getElementById('trip_type2').checked ==true) {
			TRIP_TYPE.value = "O";
			dateTest="true";
      TripType.value = "1";
		}
		else {
			TRIP_TYPE.value = "R";
			if (valDepDateSelected > valArrDateSelected)
				addError("La date de retour précède la date de départ.");
                        /*if(MINUS_PLUS_DATE.checked && Math.round((valArrDateSelected - valDepDateSelected) / 86400000)<=3)
				addError("Les dates sélectionnées sont trop proches pour utiliser l'option 'Dates de voyage flexibles'.\n");	*/
		}
	}
}

function createRequestFlight()
{
	with(document.submitVols)
	{
		//we retrieve simple data
		valDe = D_City.value;
		valVers = A_City.value;
		var num_adults = 1*ctrVolNbAdultes.value;
	        var num_enfants = 1*ctrVolNbEnfants.value;
	        var num_bebes = 1*ctrVolNbBebes.value;				
		numAdu.value = 1*ctrVolNbAdultes.value;
		numEnf.value = 1*ctrVolNbEnfants.value;	
		//if departure or arrival city is badly defined
		if ( ((valDe == '') || (valVers == '')) && !advancedSearch)
		{
			addError("Veuillez renseigner les champs : " + "\n\n");
			if (valDe == '') addError("  - Lieu de départ" + "\n"); 
			if (valVers == '') addError("  - Lieu d'arrivée" + "\n");
		}
		//checking pax
		for (i=1 ;i<=num_adults;i++)
	        	eval("PaxType"+i+".value='ADT'");
        	for (i=(num_adults+1) ;i<=(num_adults+num_enfants);i++)
           		eval("PaxType"+i+".value='CHD'");
		for (i=1 ;i<=num_bebes;i++)
           		eval("PaxType"+i+"_Infant.value='Y'");		 
		createFlightDates (); 
		if (valErrorDesc != "") window.alert(valErrorDesc);	 
		if (advancedSearch)
			FCT.value = "AABLNKENTRY";
		else
		{
			FCT.value=("AAGETAVAIL");
			CHANNEL.value="VOL";
		}		

		//Smiles flight
		if(smilesSearch)
		{
			TRIP_TYPE.value = "O";
						
			if(advancedSearch)
			{
				action="./jsp/smiles/flight/advancedSearch.jsp"+Session_Origin;
			}
			else
			{
				SEARCH_BY.value = 1;
				FCT.value = 'AAGETAVAIL';
				var ele = NumFields("formSearch","Destination#1_DIRECT_NON_STOP");
				el("formSearch",ele).value = 'Y';
				var ele2 = NumFields("formSearch","FORWARDING_ACTION");
				el("formSearch",ele2).value = '/smiles/flight/advancedSearch.jsp';
				var ele3 = NumFields("formSearch","Destination#1_Airline#1");
				el("formSearch",ele3).value = 'AF';			
				var ele3 = NumFields("formSearch","FCT");
				el("formSearch",ele3).value = 'AAGETAVAIL';			
				value2.value = "smiles/flight/advancedSearch.jsp";
				action=publicPath+"jsp/smiles/flight/wait.jsp" + Session_Origin;
			}
		}

		//Master Pricer
		else
		{
			//TRIP_TYPE.value = "R";	
			B_LOCATION_IN.value = D_City.value;
			E_LOCATION_IN.value = A_City.value;
			D_DATE.value = D_Month.value +""+D_Day.value;
			R_DATE.value = R_Month.value +""+R_Day.value;
		        //D_ANYTIME.value = "MORNING";
		        //document.submitVols.D_ANYTIME.value = document.getElementById('D_ANYTIME').value;
		        //alert("D_ANYTIME="+D_ANYTIME.value);
		        //alert("D_ANYTIME="+(document.getElementById('D_ANYTIME').value));
		        if (whiteLabel)
		          	SITE.value = document.LinkForm.SITE.value;			
    			for (i=1 ;i<=num_adults;i++)
        			eval("TRAVELLER_TYPE_"+i+".value='ADT'");
   			for (i=(num_adults+1) ;i<=(num_adults+num_enfants);i++)
   		        	eval("TRAVELLER_TYPE_"+i+".value='CHD'");
			for (i=1 ;i<=num_bebes;i++)
           		    	eval("HAS_INFANT_"+i+".value='true'");	        	
	          	if(advancedSearch)
      	  			ADVANCED_SEARCH.value="true";
	          	if (frameOrigin=="FREE")
			{
				action="./frameFree.jsp";
      	  			origin.value="FRP-39-13-VOLS";
      	  		}
      	  		else
      	  		{
      	  			var servletName=(advancedSearch) ? "AirSimpleSearchServlet" : "AirAvailabilityServlet";
				servletName=(airlineDedicatedPageFrom) ? "AirAvailabilityAirlineDedicatedServlet" : servletName;
				value2.value = "planItGoMP/"+servletName; //redirect wait.jsp towards results page
      	  			//if on a pig path, and not in a white label, we don't modify the path
      	  			if (document.location.toString().indexOf("jsp/planItGoMP/")!=-1 && document.location.toString().indexOf("whiteLabel")==-1)
      	  			{
      	  				action= publicPath+servletName + Session_Origin;
      	  			}
      	  			//else we add "jsp/planItGoMP/" to the path
	      	  		else
      	  			{
      	  				action= publicPath+"jsp/planItGoMP/"+ servletName + Session_Origin;
      	  			}
      	  			
      	  			/*
      	  			if (document.location.toString().indexOf("jsp/planItGoMP/")==-1)
      	  				action= publicPath+"jsp/planItGoMP/"+ servletName + Session_Origin;
      	  			else
      	  				action= publicPath+servletName + Session_Origin;*/
      	  			
      	  		}
		}
	}   
}

function submitEffective() {
	with(document.submitVols)
	{	
		if (valErrorDesc == "")	
		{
			
				//TEST GDS
			         if (frameOrigin!="FREE")
			         {  
					if (window.document.submitVols.BV_EngineID && window.document.submitVols.BV_SessionID)//test existence of these vars
					{
						BV_EngineID.value = document.LinkForm.BV_EngineID.value;
						BV_SessionID.value = document.LinkForm.BV_SessionID.value;
					}
			         }
				
				////TEST GDS
				//var ll = document.submitVols.length;
		       		//tmp = "";
		       		//for (i=1;i<ll;i++)
		       		//	tmp = tmp + document.submitVols[i].name +'=>'+ document.submitVols[i].value+'\n';
		    		//alert(tmp);
		    		submit();
		}
	 }
}

function submitRecherche(){
	with(document.submitVols)
	{	
		if (valErrorDesc == "")	
		{
			
				//TEST GDS
			         if (frameOrigin!="FREE")
			         {  
					if (window.document.submitVols.BV_EngineID && window.document.submitVols.BV_SessionID)//test existence of these vars
					{
						BV_EngineID.value = document.LinkForm.BV_EngineID.value;
						BV_SessionID.value = document.LinkForm.BV_SessionID.value;
					}
			         }
				
				////TEST GDS
				//var ll = document.submitVols.length;
		       		//tmp = "";
		       		//for (i=1;i<ll;i++)
		       		//	tmp = tmp + document.submitVols[i].name +'=>'+ document.submitVols[i].value+'\n';
		    		//alert(tmp);
		    		//redirection automatique sur les criteres de recherche
		    		action="http://www.e-vacances.fr/publicvac/jsp/planItGoMP/AirSimpleSearchServlet";		    		
		    		submit();
		}
	 }
}
