function kk_updateType() {
var str = location.href;
var upperTypeParameter = str.match("TYPE=Oneway");
if (upperTypeParameter=="TYPE=Oneway") kk_updateSearchForm(document.searchForm.type, 'Oneway');
}

function kk_flight_toggleOneTwoWay(){
var returnDate=document.getElementById('returnDateDiv');
var returnTimeDiv=document.getElementById('returnTimeDiv');
var returning=document.getElementById('returning');
if(document.searchForm.type[0].checked){returnDate.style.display="block";returnTimeDiv.style.display="block";returning.style.display="block";}
else if(document.searchForm.type[1].checked){returnDate.style.display="none";returnTimeDiv.style.display="none";returning.style.display="none";}
}

function kk_flightToMultistop(uri){
        var url = uri + "?forceVisible=true";
        document.location.href=uri;
}

function kk_flightToTripPlanner(uri){
        if ( !document.searchForm.allSubCatId[0].checked ){
            for (i=1; i < 4; i++) {
                if (document.searchForm.allSubCatId[i].checked) {
                        var allSubCatId = document.searchForm.allSubCatId[i].value;
                }
            }
            //retrieves parameters
            var departure = document.searchForm.departure.value;
            var arrival = document.searchForm.arrival.value;
	    var departureDate = document.searchForm.departureDate.value;
	    var returnDate = document.searchForm.returnDate.value;
            var nbadults = document.searchForm.nbadults.value;

            var url = uri + '?allSubCatId=' + allSubCatId + '&townDeparture=' + departure + '&destination=' + arrival + '&departureDate=' + departureDate + '&returnDate=' + returnDate  + '&nbAdults=' + nbadults;
            document.location.href = url;
    }
}

function checkForm(form) {
return kk_flight_ctrlForm(form);
}


function kk_flight_caculateNumberOfDays(maxMonth){
today=new Date();
var currentMonth = today.getMonth();
var lastMonth = (maxMonth + currentMonth + 1)%12; //caculate the current month + maxMonth
var daysInMonth = kk_flight_DaysArray(12);
var lastDate = daysInMonth[lastMonth];
var lastDay
if (lastMonth!=0)
{
 lastDay=new Date(today.getFullYear()+1, lastMonth-1,lastDate);
}
else { lastDay=new Date(today.getFullYear(), lastMonth-1, lastDate);}
var one_day=1000*60*60*24;
var numberOfDays=(Math.ceil((lastDay.getTime()-today.getTime())/(one_day)));
return numberOfDays;

}

function kk_flight_IsFourDigitsYear(year){
 return (year.search(/\d{4}/) != -1);
}

function kk_flight_ConvertToFourDigitsYear(year){ 
var yearToConvert = "20" + year;
return yearToConvert;
}

function kk_flight_isNewCalendar(){
return (document.getElementById("yfcMiniCalDiv")!=null);
}

var dtCh= "/";
function kk_flight_IsInterger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function kk_flight_stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function kk_flight_daysInFebruary(year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function kk_flight_DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
   } 
   return this;
}


function kk_flight_isDate(dtStr){

  //var daysInMonth=[0,31,29,31,30,31,30,31,31,30,31,30,31];
	var daysInMonth = kk_flight_DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) {strDay=strDay.substring(1);}
	if (strMonth.charAt(0)=="0" && strMonth.length>1) {strMonth=strMonth.substring(1);}
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) {strYr=strYr.substring(1);}
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert(alert1);
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert(alert1);
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>kk_flight_daysInFebruary(year)) || day > daysInMonth[month]){
		alert(alert1);
		return false;
	}
	if ((strYear.length != 4 && strYear.length != 2) || year==0 ){
		alert(alert1);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || kk_flight_IsInterger(kk_flight_stripCharsInBag(dtStr, dtCh))==false){
		alert(alert1);
		return false;
	}
return true;
}

function kk_flight_openDepartureDateCalendar() {
if (cal==null) {
cal=new kk_CalendarPopup();
cal.setReturnFunction("kk_flight_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);
}
month=document.searchForm.dmonth.options[document.searchForm.dmonth.selectedIndex].value;
year=document.searchForm.dyear.options[document.searchForm.dyear.selectedIndex].value;
cal.showCalendar('depdate',month,year); 
return false;
}

function kk_flight_openReturnDateCalendar() {
if (cal2==null) {
cal2=new kk_CalendarPopup();
cal2.setReturnFunction("kk_flight_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);
}
month=document.searchForm.rmonth.options[document.searchForm.rmonth.selectedIndex].value;
year=document.searchForm.ryear.options[document.searchForm.ryear.selectedIndex].value;
cal2.showCalendar('retdate',month,year); 
return false;
}

function kk_flight_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 kk_flight_currentYear() {
var now=new Date(); 
var year=now.getYear(); 
if(year<999) year+=1900;
return year;
}

function kk_flight_setDepDate(y,m,d) {
var year=kk_flight_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;
kk_flight_setMonth();
kk_flight_setYear();
if (typeof(document.searchForm.wdday)!="undefined") kk_flight_getDepWeekDay(d,m,y);
}

function kk_flight_setRetDate(y,m,d) {
var year=kk_flight_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 (typeof(document.searchForm.wrday)!="undefined") kk_flight_getRetWeekDay(d,m,y);
}

function kk_flight_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];

var isNewCalendar = kk_flight_isNewCalendar();

if (isNewCalendar)
{
  var departureDate=form.departureDate.value;
  var returnDate=form.returnDate.value;
  if (!kk_flight_isDate(departureDate) || !kk_flight_isDate(returnDate)) {return false;}
  var splitDepDate=departureDate.split("/");
  var splitRetDate=returnDate.split("/");
  selectedDepDay=splitDepDate[0];
  selectedDepMonth=splitDepDate[1];
  selectedDepYear=splitDepDate[2];
  selectedRetDay=splitRetDate[0];
  selectedRetMonth=splitRetDate[1];
  selectedRetYear=splitRetDate[2];
 
  if (!kk_flight_IsFourDigitsYear(selectedDepYear)) {
	selectedDepYear=kk_flight_ConvertToFourDigitsYear(selectedDepYear);
  }
  if (!kk_flight_IsFourDigitsYear(selectedRetYear)) {
	selectedRetYear=kk_flight_ConvertToFourDigitsYear(selectedRetYear);
  }	
}
else
{
  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";}
if ((selectedDepartureText==null||selectedDepartureText=="")&&(selectedDepartureSelect != null&&selectedDepartureSelect != "I")) { form.departure.value=selectedDepartureSelect;}
else {
if (form.arrivalTEXT.type!="hidden") {form.departure.value=selectedDepartureText;}
}
if (typeof(form.arrivalTEXT)!="undefined") {selectedArrivalText=form.arrivalTEXT.value;}
else {selectedArrivalText=form.arrival.value;}
if (typeof(form.arrivalSEL)!="undefined") {selectedArrivalSelect=form.arrivalSEL.options[form.arrivalSEL.selectedIndex].value;}
else {selectedArrivalSelect="I";}
if ((selectedArrivalText==null||selectedArrivalText=="")&&(selectedArrivalSelect!=null&&selectedArrivalSelect!="I")) { form.arrival.value=selectedArrivalSelect;}
else {
if (form.arrivalTEXT.type!="hidden") { form.arrival.value=selectedArrivalText; }
}
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;
if (today.getTime()>selectedDepTime||((today.getTime()>selectedRetTime )&&(form.type.type=="hidden"||!form.type[1].checked))) {
alert (alert2);
return false;
}
if (form.type.type=="hidden"||form.type[0].checked) {
if (selectedRetTime<selectedDepTime) {
alert (alert3);
return false;
}
}
if (form.departureTEXT.type=="hidden"&&form.arrivalTEXT.type=="hidden") {
form.departureTEXT.value=form.departure.value ;
form.arrivalTEXT.value=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 (isNewCalendar)
{	if ((yfcMiniCal.maxMonth != null)&&(yfcMiniCal.maxMonth.length != 0)) 						
		{kk_flight_dayRangeValidDate=kk_flight_caculateNumberOfDays(yfcMiniCal.maxMonth)+1;}
 	else {kk_flight_dayRangeValidDate=kk_flight_caculateNumberOfDays(11)+1;}
		validDepDate=today.getTime()+24*3600*1000*kk_flight_dayRangeValidDate;
	if (validDepDate<=selectedDepTime) {
		if (typeof(alert7)=="undefined") {alert("The departure date you can choose is up to 11 months from today");}
                else{alert(alert7);}
	return false;
	}
}
else  //old calendar
{	if (typeof(kk_flight_dayRangeValidDate)=="undefined") kk_flight_dayRangeValidDate=350;
	validDepDate=today.getTime()+24*3600*1000*kk_flight_dayRangeValidDate;
	if (validDepDate<=selectedDepTime) {
	alert(alert5);
	return false;
	}
}

if (form.from.value=="kelbest") {form.from.value="shopbot";}

return true;

}

function kk_flight_setMonth() {
document.searchForm.rmonth.selectedIndex=document.searchForm.dmonth.selectedIndex  ;
if (typeof(document.searchForm.wdday)!="undefined") kk_flight_updateWeekDay();
}
   
function kk_flight_setYear() {
document.searchForm.ryear.selectedIndex=document.searchForm.dyear.selectedIndex  ;
if (typeof(document.searchForm.wdday)!="undefined") kk_flight_updateWeekDay();
}

function kk_flight_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;
}
kk_flight_getDepWeekDay(dd,dm,dy);
kk_flight_getRetWeekDay(rd,rm,ry);
}

function kk_flight_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();
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 kk_flight_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();
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 {document.searchForm.wdday.value="";}    
}  
}

function kk_flight_pV(airPort) {
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 kk_flight_checkEmptyAirport() {
if (typeof(document.forms.formulaire.departure)!="undefined") {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=kk_flight_stringTrim(departure);
arrival=kk_flight_stringTrim(arrival);		
if (arrival.length==0||departure.length==0) {
alert(ALERT_AIRPORT_EMPTY); 
return false;
}
else {return true;}
}

function kk_flight_stringTrim(strToTrim) {
if (strToTrim!=null) {strToTrim= strToTrim.replace(/^\s+|\s+$/g, '');}
return strToTrim;
}

function  kk_flight_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  kk_flight_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  kk_flight_updateDepartureText() {
document.forms.formulaire.departureTEXT.value=document.forms.formulaire.departure.value;
return true;
}

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

function  kk_flight_majDate() {
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;
}
}

