/* 
flightContol.js
Auhtor S Goyet
Last modified 26/4/2004
DESCRIPTION:controls for shopbot flight form
*/ 

//alert("LOADING  flightContol.js");

var searchFormValue=-1;

var cal = new CalendarPopup();
cal.setReturnFunction("setDepDate");
cal.setMonthNames(month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12);
cal.setDayHeaders(day1,day2,day3,day4,day5,day6,day7);
cal.setWeekStartDay(1);
cal.setTodayLabel(todayLabel);

var cal2 = new CalendarPopup();
cal2.setReturnFunction("setRetDate");
cal2.setMonthNames(month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12);
cal2.setDayHeaders(day1,day2,day3,day4,day5,day6,day7);
cal2.setWeekStartDay(1);
cal2.setTodayLabel(todayLabel);

function cleanTown(fromField)
{
     if (fromField=="departureSEL"){document.searchForm.departureTEXT.value="";}
     if (fromField=="departureTEXT")
     {
     	document.searchForm.departureSEL.selectedIndex=0;
     	document.searchForm.departure.value=document.searchForm.departureTEXT.value;
     }
     if (fromField=="arrivalSEL")
     {
     	document.searchForm.arrivalTEXT.value="";
     }
     if (fromField=="arrivalTEXT")
     {
     	document.searchForm.arrivalSEL.selectedIndex=0;
     	document.searchForm.arrival.value=document.searchForm.arrivalTEXT.value
     }
}


function currentYear()
{
  var now = new Date(); 
     var year = now.getYear(); 
     if(year < 999) year+=1900;
     return year;
}


function setDepDate(y,m,d)
{
     var year = currentYear();
    document.searchForm.dday.selectedIndex = d-1;
    document.searchForm.dmonth.selectedIndex = m-1;
    if (y == year)  document.searchForm.dyear.selectedIndex = 0;
    if (y == year+1)  document.searchForm.dyear.selectedIndex = 1;
    setMonth();
    setYear();
    if (dayCheck = "yes")
      {
	getDepWeekDay(d,m,y);
      }
}

function setRetDate(y,m,d)
{
    var year = currentYear();
    document.searchForm.rday.selectedIndex = d-1;
    document.searchForm.rmonth.selectedIndex = m-1;
    if (y == year)      document.searchForm.ryear.selectedIndex = 0;
    if (y == year+1)      document.searchForm.ryear.selectedIndex = 1;
    if (dayCheck = "yes")
      {
	getRetWeekDay(d,m,y);
      }
}

function ctrlForm(form)
{

if (typeof (form.airline)!="undefined"){
  if (form.airlineFormatted != null){
  var airlineName2 = form.airline.options[form.airline.selectedIndex].text;
  form.airlineFormatted.value=airlineName2;
  }
 }

 var daysinmonthR=[31,28,31,30,31,30,31,31,30,31,30,31];
 var daysinmonthD=[31,28,31,30,31,30,31,31,30,31,30,31];

  	selectedDepDay = form.dday.options[form.dday.selectedIndex].value;
  	selectedDepMonth = form.dmonth.options[form.dmonth.selectedIndex].value;
	selectedDepYear = form.dyear.options[form.dyear.selectedIndex].value;
  	selectedRetDay = form.rday.options[form.rday.selectedIndex].value;
  	selectedRetMonth = form.rmonth.options[form.rmonth.selectedIndex].value;
  	selectedRetYear = form.ryear.options[form.ryear.selectedIndex].value;
 
  	selectedDepDate = new Date(selectedDepYear, selectedDepMonth -1, selectedDepDay);
  	selectedRetDate = new Date(selectedRetYear, selectedRetMonth -1, selectedRetDay);

  	today = new Date();

  	if ( typeof (form.departureTEXT)!="undefined" )
  	{
    	selectedDepartureText = form.departureTEXT.value;
    }
	else
	{
    	selectedDepartureText = form.departure.value;
    }

  	if (typeof (form.departureSEL)!="undefined")
  	{
    	selectedDepartureSelect = form.departureSEL.options[form.departureSEL.selectedIndex].value;
    }
  	else
  	{
    	selectedDepartureSelect ="I";
    }

  	//check which departure has been used
  	if (	(selectedDepartureText == null || selectedDepartureText == "")
  			&& (selectedDepartureSelect != null && selectedDepartureSelect != "I") )
  	{
  		form.departure.value = selectedDepartureSelect;
  	}
	else 
	{ //  we use text field : BUG FIX ?Sabina? OPE
		if (form.arrivalTEXT.type != "hidden" )
		{ // case with select and not popup (like FR)
	 		form.departure.value = selectedDepartureText;
	 	}
	}

  	if (typeof (form.arrivalTEXT)!="undefined")
 	{
     	selectedArrivalText = form.arrivalTEXT.value;
    }
  	else
  	{
    	selectedArrivalText = form.arrival.value;
    }

	//alert( typeof (form.arrivalSEL) );
	if (typeof (form.arrivalSEL)!="undefined")
	{
    	selectedArrivalSelect = form.arrivalSEL.options[form.arrivalSEL.selectedIndex].value;
    }
  	else
  	{
    	selectedArrivalSelect ="I";
    }

  	//check which arrival has been used
  	// if => we use list
  	if (	(selectedArrivalText == null || selectedArrivalText == "")
  			&& (selectedArrivalSelect != null && selectedArrivalSelect != "I") )
  	{
  		form.arrival.value = selectedArrivalSelect;
  		//alert("use list and value is : " + form.arrival.value);
	}
	else 
	{ //  we use text field : BUG FIX ?Sabina? OPE
		if (form.arrivalTEXT.type != "hidden" )
		{ // case with select and not popup (like FR)
	 		form.arrival.value = selectedArrivalText;
	 	}
	}
	
  	//check if dates are correct (if not selected from calendar)
	// Date control
	if (selectedDepYear%4 != 0) daysinmonthD[1] = 29;
	if (selectedRetYear%4 != 0) daysinmonthR[1] = 29;

	if (	selectedDepDay>daysinmonthD[selectedDepMonth-1]
			|| (	(form.type.type=="hidden" || !form.type[1].checked) && selectedRetDay>daysinmonthR[selectedRetMonth-1]) )
  	{
    	alert (alert1);
    	return false;
	}

	selectedDepTime=selectedDepDate.getTime() + 24*3600*1000;	
	selectedRetTime=selectedRetDate.getTime() + 24*3600*1000;

  	//check if departure date is correct
	if (	today.getTime() > selectedDepTime
			||  ( (today.getTime() > selectedRetTime ) && (form.type.type=="hidden" || !form.type[1].checked) )  )
	{
    	alert (alert2);
	    return false;
	}

  	//check if departure date < arival date
	if (form.type.type=="hidden" || form.type[0].checked)
 	{
    	if ( selectedRetTime < selectedDepTime )
    	{
      		alert (alert3);
      		return false;
    	}
  	}

	// Arrival and departure control
	if (form.departureTEXT.type=="hidden" && form.arrivalTEXT.type=="hidden")
    {
    	form.departureTEXT.value=form.departure.value ;
		form.arrivalTEXT.value=form.arrival.value ;
	}

  	//check if "Leaving from" and "Going to" fields are informed
	//alert("departure=[" + form.departure.value + "] arrival=[" + form.arrival.value + "]");
  	if ( form.departure.value == "" || form.arrival.value == "" )
  	{
    	alert (alert4);
    	return false;
  	}

	if (form.type.type!="hidden" && form.type[1].checked)
	{
        form.rday.value = "";
        form.rmonth.value = "";
        form.ryear.value = "";
  	}
        if (form.from.value == "kelbest") { form.from.value = "shopbot"; } 
  	return true;
}

function setMonth()
{
        document.searchForm.rmonth.selectedIndex=document.searchForm.dmonth.selectedIndex  ;
    if (dayCheck = "yes")
      {
        updateWeekDay();
      }
}
                                                                                                                                                                                            
function setYear()
{
        document.searchForm.ryear.selectedIndex=document.searchForm.dyear.selectedIndex  ;
    if (dayCheck = "yes")
      {
        updateWeekDay();
      }
}



function updateWeekDay()
{

  var dd = document.searchForm.dday.value;
  var dm = document.searchForm.dmonth.value;
  var dy = document.searchForm.dyear.value;
  
  if (dy%4 == 0)
     var DMonthDays = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
  else
     var DMonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
                                                                                                                                               
                                                                                                                                               
  if (dd > DMonthDays[dm-1])
  {
        dd = DMonthDays[dm-1];
        document.searchForm.dday.value = dd;
  }


  var rd = document.searchForm.rday.value;
  var rm = document.searchForm.rmonth.value;
  var ry = document.searchForm.ryear.value;

  if (ry%4 == 0)
     var RMonthDays = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
  else
     var RMonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
                                                                                                                                               
  if (rd > RMonthDays[rm-1])
  {
        rd = RMonthDays[rm-1];
        document.searchForm.rday.value = rd;
  }


  getDepWeekDay(dd,dm,dy);
  getRetWeekDay(rd,rm,ry);
 }

function getDepWeekDay(d,m,y)
{
  var daysinmonth=[31,28,31,30,31,30,31,31,30,31,30,31];
  var day = document.searchForm.dday.options[document.searchForm.dday.selectedIndex].value-1;
  var month = document.searchForm.dmonth.options[document.searchForm.dmonth.selectedIndex].value-1;
  var year = document.searchForm.dyear.options[document.searchForm.dyear.selectedIndex].value;

  if (year%4 != 0) daysinmonth[1] = 29;

  if (typeof (document.searchForm.wdday)!="undefined"){
  if (daysinmonth[month]>=day)
    {
        var testdate = new Date(y,m-1,d);
        var weekday  =  testdate.getDay();
	// glenn      alert(d + "/" + "/" + m + "/" + y + " Date" + testdate + " " + weekday);
        if (weekday == 0)
                document.searchForm.wdday.value = Sun;
        if (weekday == 1)
                document.searchForm.wdday.value = Mon;
        if (weekday == 2)
                document.searchForm.wdday.value = Tue;
        if (weekday == 3)
                document.searchForm.wdday.value = Wed;
        if (weekday == 4)
                document.searchForm.wdday.value = Thu;
        if (weekday == 5)
                document.searchForm.wdday.value = Fri;
        if (weekday == 6)
                document.searchForm.wdday.value = Sat;
    }
  else
    {
      document.searchForm.wdday.value="";
    }
  }                                                                                                                           

}
function getRetWeekDay(d,m,y)
{
  var daysinmonth=[31,28,31,30,31,30,31,31,30,31,30,31];
  var day = document.searchForm.rday.options[document.searchForm.rday.selectedIndex].value-1;
  var month = document.searchForm.rmonth.options[document.searchForm.rmonth.selectedIndex].value-1;
  var year = document.searchForm.ryear.options[document.searchForm.ryear.selectedIndex].value;


  if (year%4 != 0) daysinmonth[1] = 29;

  if (typeof (document.searchForm.wrday)!="undefined"){
  if (daysinmonth[month]>=day)
    {
        var testdate = new Date(y,m-1,d);
        var weekday  =  testdate.getDay();
        //alert(d + "/" + "/" + m + "/" + y + " Date" + testdate + " " + weekday);
        if (weekday == 0)
                document.searchForm.wrday.value = Sun;
        if (weekday == 1)
                document.searchForm.wrday.value = Mon;
        if (weekday == 2)
                document.searchForm.wrday.value = Tue;
        if (weekday == 3)
                document.searchForm.wrday.value = Wed;
        if (weekday == 4)
                document.searchForm.wrday.value = Thu;
        if (weekday == 5)
                document.searchForm.wrday.value = Fri;
        if (weekday == 6)
                document.searchForm.wrday.value = Sat;
  }          
    else
    {flightControl.js
      document.searchForm.wdday.value="";
    }         
  }                                                                                                 
}


/*
 * Used in popup when a town is selected
 */
function pV(airPort)
{
	// alert("CALL pV");
	var selectedBox = parent.opener.window.document.formulaire.selectedBox.value;
	var selectedBoxText = selectedBox+"TEXT";
	parent.opener.window.document.formulaire.elements[selectedBox].value = airPort;	
	parent.opener.window.document.formulaire.elements[selectedBoxText].value = airPort;
	closePopup();
}


/*
 * function used in bForm_v2.jsp
 */
// bugfix 36140 OPE
/*
 * Check if the field are not empty
 * Display a error msg if arrival and departure are empty and stop
 */
function checkEmptyAirport()
{
//        alert("CALL checkEmptyAirport");

		//manage case where name come from popup
		// in this case (exclude FR) the value of arrivalTEXT and departureTEXT are not initied
	  	if ( typeof (document.forms.formulaire.departure)!="undefined" )
	  	{	// we have a textField => perhaps we come from popup
	  		document.forms.formulaire.departureTEXT.value = document.forms.formulaire.departure.value;
	  	}
	  	if ( typeof (document.forms.formulaire.arrival)!="undefined" )
	  	{
	  		document.forms.formulaire.arrivalTEXT.value = document.forms.formulaire.arrival.value;
		}

		var departure = document.forms.formulaire.departureTEXT.value;
		var arrival = document.forms.formulaire.arrivalTEXT.value;
		departure = stringTrim(departure);
		arrival = stringTrim(arrival);		

//        alert("departure=["+departure+"] size=" + departure.length);
//        alert("arrival=["+arrival+"] size=" + arrival.length);

		if ( arrival.length == 0 || departure.length == 0 )
		{
			alert(ALERT_AIRPORT_EMPTY); 
			return false;
		}
		else
		{
			return true;
		}
}

/*
 * Delete space at the begin and end of the param
 */
function stringTrim(strToTrim)
{
//	alert("CALL stringTrim on : [" + strToTrim + "]");
	if ( strToTrim != null )
	{
		strToTrim= strToTrim.replace(/^\s+|\s+$/g, '');
	}
	return strToTrim;
}

// these Functions was in bForm_v2
function updateDepartureTextFromSel()
{
	document.forms.formulaire.departureTEXT.value=document.forms.formulaire.departure.options[document.forms.formulaire.departure.selectedIndex].value;
	document.forms.formulaire.departureSelected.value=document.forms.formulaire.departure.selectedIndex;
	return true;
}

function updateArrivalTextFromSel()
{
	document.forms.formulaire.arrivalTEXT.value=document.forms.formulaire.arrival.options[document.forms.formulaire.arrival.selectedIndex].value;
	document.forms.formulaire.arrivalSelected.value=document.forms.formulaire.arrival.selectedIndex;
	return true;
}

function updateDepartureText()
{
	document.forms.formulaire.departureTEXT.value=document.forms.formulaire.departure.value;
	return true;
}

function updateArrivalText()
{
	document.forms.formulaire.arrivalTEXT.value=document.forms.formulaire.arrival.value;
	return true;
}


function maj_FlightDate() {

   if (document.searchForm.dday.value =="1" && document.searchForm.dmonth.value =="1" && document.searchForm.rday.value =="1" && document.searchForm.rmonth.value == "1")
    {
        var now = new Date();
        var tmp = now.getTime()+(86400000*7);
        now.setTime(tmp);
        var month = now.getMonth()+1;
        var year = now.getFullYear();
        var day = now.getDate();                                                                                                   
        document.searchForm.dday.options[day-1].selected=true;
        document.searchForm.dmonth.options[month-1].selected=true;
        document.searchForm.dyear.options[year-this_year].selected=true;

        var now2 = new Date();
        tmp = now2.getTime()+(86400000*14);
        now2.setTime(tmp);
        month = now2.getMonth()+1;
        year = now2.getFullYear();
        day = now2.getDate();
        document.searchForm.rday.options[day-1].selected=true;
        document.searchForm.rmonth.options[month-1].selected=true;
        document.searchForm.ryear.options[year-this_year].selected=true;
	}
}






