var OUTBOUND_DEPARTURE = 2;
var OUTBOUND_ARRIVAL = 3;
var INBOUND_DEPARTURE = 4;
var INBOUND_ARRIVAL = 5;

function setFlightTypeAndAirports(flightType){

    switch (flightType){

		case "retour":
            $("#selInpSearchF2From").hide();
            $("#selInpSearchF2To").hide();
            $("#returnFlightDepartureText").hide();
            $("#returnFlightArrivalText").hide();
            $("#returnDatePicker").show();
            $("#returnDateText").show();
            setAirports("selInpSearchF1From",$("#selInpSearchF1From").val(),flightType);
            break;

		case "one_way":
            $("#selInpSearchF2From").hide();
            $("#selInpSearchF2To").hide();
            $("#returnFlightDepartureText").hide();
            $("#returnFlightArrivalText").hide();
            $("#returnDatePicker").hide();
            $("#returnDateText").hide();
            setAirports("selInpSearchF1From",$("#selInpSearchF1From").val(),flightType);
            break;

		case "open_jaw":
            $("#selInpSearchF2From").show();
            $("#selInpSearchF2To").show();
            $("#returnFlightDepartureText").show();
            $("#returnFlightArrivalText").show();
            $("#returnDatePicker").show();
            $("#returnDateText").show();
            break;

		default:
			break;
	}
}

function setAirports(objId,fieldValue,flightType){
    var f1To;
    var f1From;
    var f2To;
    var f2From;

    switch (objId) {

		case "selInpSearchF1From":
			switch (flightType){

				case "retour":
                    if	(fieldValue != "xxx"){
                        $("#selInpSearchF1From").val(fieldValue);
                        $("#selInpSearchF2To").val(fieldValue);
                        f1To=$("#selInpSearchF1To").val();
						f2From=$("#selInpSearchF2From").val();
                        updateDestinations($("#selInpSearchF1From").val(),"#selInpSearchF1To",OUTBOUND_ARRIVAL,f1To);
                        updateDestinations($("#selInpSearchF1From").val(),"#selInpSearchF2From",OUTBOUND_ARRIVAL,f2From);
                    }
					else{
                        $("#selInpSearchF1From").val(fieldValue);
                        $("#selInpSearchF2To").val(fieldValue);
					}
					break;

				case "one_way":
					if(fieldValue != "xxx"){
						$("#selInpSearchF1From").val(fieldValue);
                        f1To=$("#selInpSearchF1To").val();
                        updateDestinations($("#selInpSearchF1From").val(),"#selInpSearchF1To",OUTBOUND_ARRIVAL,f1To);
                    }
					else{
						$("#selInpSearchF1From").val(fieldValue);
                    }
					break;

				case "open_jaw":
					if(fieldValue != "xxx"){
						$("#selInpSearchF1From").val(fieldValue);
                        f1To=$("#selInpSearchF1To").val();
                        updateDestinations($("#selInpSearchF1From").val(),"#selInpSearchF1To",OUTBOUND_ARRIVAL,f1To);
                    }
					else{
						$("#selInpSearchF1From").val(fieldValue);
                    }
					break;

				default:
				break;
			}
			break;

		case "selInpSearchF1To":
			switch (flightType){
				case "retour":
					if(fieldValue != "xxx"){
						$("#selInpSearchF1To").val(fieldValue);
						$("#selInpSearchF2From").val(fieldValue);
                        f1From=$("#selInpSearchF1From").val();
                        updateDestinations($("#selInpSearchF1To").val(),"#selInpSearchF1From",OUTBOUND_DEPARTURE,f1From);
					}
					else{
						$("#selInpSearchF1To").val(fieldValue);
						$("#selInpSearchF2From").val(fieldValue);
					}
					break;

				case "one_way":
				    $("#selInpSearchF1To").val(fieldValue);
					break;

				case "open_jaw":
					$("#selInpSearchF1To").val(fieldValue);
					break;

				default:
				break;
			}

			break;

		case "selInpSearchF2From":
			switch (flightType){

				case "open_jaw":
					if	(fieldValue != "xxx"){
						$("#selInpSearchF2From").val(fieldValue);
                        f2To=$("#selInpSearchF2To").val();
                        updateDestinations($("#selInpSearchF2From").val(),"#selInpSearchF2To",INBOUND_ARRIVAL,f2To);
                    }
					else{
						$("#selInpSearchF2From").val(fieldValue);
					}
                break;

                default:
				break;
			}
			break;

		case "selInpSearchF2To":
			switch (flightType){

				case "open_jaw":
					if(fieldValue != "xxx"){
						$("#selInpSearchF2To").val(fieldValue);
                        f2From=$("#selInpSearchF2From").val();
                        updateDestinations($("#selInpSearchF2To").val(),"#selInpSearchF2From",INBOUND_DEPARTURE,f2From);
                    }
					else{
						$("#selInpSearchF2To").val(fieldValue);
					}
					break;

				default:
				break;
			}
			break;

		default:
			break;

	}

}

function init(flightType){
    if	(flightType == 'retour'){
        var f1From  = $("#selInpSearchF1From").val();
        var f1To	= $("#selInpSearchF1To").val();
		var f2From	= $("#selInpSearchF2From").val();
		var f2To	= $("#selInpSearchF2To").val();
        if(f1From!=f2To||f1To!=f2From){
            $("#selInpSearchF2From").val(f1To);
            $("#selInpSearchF2To").val(f1From);
		}
	}
    setFlightTypeAndAirports(flightType);
}

// server method call to retrieve destination airports where you can fly to from chosen departure airport
// selectedairportcode : code of the chosen departure airport
// selectboxid : id of the selectbox you wish to update
// identifier : to identify the server method we wish to call
function updateDestinations(selectedairportcode,selectboxid,identifier,value) {
    var location = 'LoadAirports.action?airportCode=' + selectedairportcode + '&identifier=' + identifier;
    $(selectboxid).emptySelect();
    $.getJSON(location,function(json){
        $(selectboxid).loadSelect(json);
        $(selectboxid).val(value);
    });
}

function updateDepartureYear() {
    var day = $("#departureDay").val();
    var month = $("#departureMonth").val();
    var year = $("#departureYear").val();
    
    var departureDate = new Date(year,month,day);
    var sysdate = new Date();
    if(departureDate < sysdate) {
    	$("#departureYear").val(parseInt(year)+1);
    }
}

function updateReturnYear() {
    var day = $("#returnDay").val();
    var month = $("#returnMonth").val();
    var year = $("#returnYear").val();
    
    var returnDate = new Date(year,month,day);
    var sysdate = new Date();
    if(returnDate < sysdate) {
    	$("#returnYear").val(parseInt(year)+1);
    }
}

// server method call to set returndate = departuredate + 7 days
function updateReturnDate(){
    var day = $("#departureDay").val();
    if (day.length < 2){
        day = "0" + day;
    }
    var month = $("#departureMonth").val();
    if (month.length < 2){
        month = "0" + month;
    }
    var departureDate = day + month + $("#departureYear").val();
    var location = 'UpdateReturnDate.action?departureDate=' + departureDate;
    $.getJSON(location,
        function(data){
            updateReturnLinked(data[0].returnDate);
            departureDate = day + "/" + month + "/" + $("#departureYear").val();
            $("#departureDate").val(departureDate);
            $("#returnDate").val(data[0].returnDate)
        });
}

function updateReturnDateField() {
    var departureDate = $("#returnDay").val() + "/" + $("#returnMonth").val() + "/" + $("#returnYear").val();
    $("#returnDate").val(departureDate);
}