/**
 * $Id$
 *
 * argument list: [from, to], outbound, inbound, priceGroup
 *
 * <li>if <param>from</param> is specified <param>to</param> most also
 *     be specified and vice versa, both values most be a 3 letter airport code
 *     like CPH or BCN
 *
 * <li>outbound is the outbound departure date and is specified as ddMMyyyy
 *
 * <li>inbound is the inbound departure date and is specified as ddMMyyyy
 *
 * <li>priceGroup sets the default price group and is specified by the price
 *     group id
 *
 * <li>priceValuta sets the default valuta and is specified by a 3 letter
 *     currency code like DKK or EUR
 *
 *
 */
function validateForm() {
    if (pas.getNumPassengers()>0) return true;
    alert(err_no_passengers);
}

function getSearchObj() {
    var o = {};
    var a = document.location.search.split('&');
    if (a.length>0) {
        a[0] = a[0].substr(1);
    }
    for (var i=0;i<a.length;i++) {
        var a2 = a[i].split('=');
        o[a2[0]] = a2[1];
    }
    return o;
}

function submitForm() {
    if (validateForm()) {
        setDates();
        var path = location.pathname.substr(0,location.pathname.lastIndexOf('/')+1);
        var dExpire = new Date(new Date().getTime()+(3600000*24*1));     // Expires after 1 day
        var form = new FormSerializer('booking')
        Cookie.set(INSTALLATION_ID + '_booking1',form.serialize(), dExpire, path);	// Expires after 1 day

        document.forms.booking.submit();
    }
}


function setDates() {
    var date;
    var day;
    var month;
    var year;

    date = outboundCalendar.getDate();
    document.getElementById("outboundDay").value = date.getDate();
    document.getElementById("outboundMonth").value = date.getMonth();
    document.getElementById("outboundYear").value = date.getFullYear();

    date = inboundCalendar.getDate();
    document.getElementById("inboundDay").value = date.getDate();
    document.getElementById("inboundMonth").value = date.getMonth();
    document.getElementById("inboundYear").value = date.getFullYear();

    return true;
}


/** Sets the from and to airport selections to the
 *  specified values.
 *
 *  @param from a 3 letter airport code or an airport id number
 *  @param to   a 3 letter airport code or an airport id number
 */

function initRoutes(from, to) {
   if (typeof(from) == "number") {
        routeChooser.setSelectedId(from, to);
    } else {
        routeChooser.setSelectedCode(from, to);
    }
}


/** Sets the outbound and inbound date selection to the specified dates
 *
 *  @param outbound a string with the format of ddMMyyyy
 *  @param inbound  a string with the format of ddMMyyyy
 */

function initDates(outbound, inbound) {
    var currentDate;
    var maximumDate;
    var outboundDate;
    var inboundDate;

    currentDate = new Date();
    maximumDate = new Date(currentDate.getFullYear() + 1, currentDate.getMonth(), currentDate.getDate());
    maximumInboundDate = new Date(currentDate.getFullYear() + 1, currentDate.getMonth() + 1, currentDate.getDate());

    if (outbound == null) {
        outboundDate = currentDate;
    } else {
        outboundDate = outbound;
    }

    if (inbound == null) {
        inboundDate = outboundDate;
    } else {
        inboundDate = inbound;
    }

    outboundCalendar.setDate(outboundDate);
    outboundCalendar.setMaxDate(maximumDate);

    inboundCalendar.setDate(inboundDate);
    inboundCalendar.setMaxDate(maximumInboundDate);
}


/** Sets the ticket type selection
 *
 *  @param ticketType if the string equals 'oneway' oneway will be set
 *  as default otherwise return.
 */

function initTicketType(ticketType) {
    var ticketSelector;

    ticketSelector = document.getElementById("tickettype");
	if (ticketType == "oneway") {
		ticketTypeChooser.setSelected(ticketTypeChooser.getOneWayElement());
		routeChooser.onFromChanged();
	} else {
		ticketTypeChooser.setSelected(ticketTypeChooser.getRoundTripElement());
		routeChooser.onFromChanged();
	}
}


/** Sets the price group selection to the pricegroup with the specified name
 *
 *  @param priceGroupId an number specifying the pricegroup id
 */

function initPriceGroup(priceGroupId) {
    var priceGroupSelector;

    priceGroupSelector = document.getElementById("priceGroup");

    if (priceGroupSelector.options == null) {
        priceGroupSelector.value = priceGroupId;
    } else {
        for (var i = 0; i < priceGroupSelector.options.length; i++) {

            if (priceGroupSelector.options[i].value == priceGroupId) {
                priceGroupSelector.options[i].selected = true;
                return;
            }
        }
    }
}


/** Sets the valuta selection to the specified valuta
 *
 *  @param priceValutaId a 3 letter valuta code
 */

function initPriceValuta(priceValutaId) {
    var priceValutaSelector;

    priceValutaSelector = document.getElementById("priceValuta");

    for (var i = 0; i < priceValutaSelector.options.length; i++) {
        if (priceValutaSelector.options[i].value == priceValutaId) {
            priceValutaSelector.options[i].selected = true;
            return;
        }
    }
}

function initPassengers(cookieData) {
    var items;
    var selector;

    for (items in cookieData) {
        if (items.substring(0, 11) == "personType_") {
            selector = document.getElementById(items);
            selector.value = cookieData[items];
            selector.onchange();
        }
    }
}

function init() {

    var oArgs;
    var cookieData;

    oArgs = getSearchObj();

    eval('cookieData = ' + Cookie.get(INSTALLATION_ID + '_booking1'));
    if (oArgs.from) {
        initRoutes(oArgs.from, oArgs.to);

    } else if (cookieData != null && cookieData.fromAirport) {
        initRoutes(parseInt(cookieData.fromAirport, 10), parseInt(cookieData.toAirport, 10));
    }

    if (oArgs.outbound) {
        var outboundDate;
        var inboundDate;
        var year;
		var month;
		var date;

		year = parseInt(oArgs.outbound.substring(0,4),10);
		month = parseInt(oArgs.outbound.substring(4,6),10) -1;	
		date = parseInt(oArgs.outbound.substring(6,8),10)

		date += oArgs.outboundDayOffset != null ? parseInt(oArgs.outboundDayOffset) : 0;

        outboundDate = new Date(year, month , date);
        inboundDate = outboundDate;

        initDates(outboundDate, inboundDate);
    } else if (cookieData != null && cookieData.outboundDay) {
        var outboundDate;
        var inboundDate;

        outboundDate = new Date(cookieData.outboundYear, cookieData.outboundMonth, cookieData.outboundDay);
        inboundDate = new Date(cookieData.inboundYear, cookieData.inboundMonth, cookieData.inboundDay);

        initDates(outboundDate, inboundDate);
    } else {
        initDates(new Date(), new Date());
    }

    if(cookieData != null && cookieData.tickettype) {
        initTicketType(cookieData.tickettype);
    } else {
		ticketTypeChooser.setSelected(ticketTypeChooser.getRoundTripElement());
		routeChooser.onFromChanged();
    }


    if (oArgs.priceGroup) {
        initPriceGroup(oArgs.priceGroup);

    } else if (cookieData != null && cookieData.priceGroup){
        initPriceGroup(cookieData.priceGroup);
    }


    if (oArgs.priceValuta) {
        initPriceValuta(oArgs.priceValuta);
    } else if (cookieData != null && cookieData.priceValuta){
        initPriceValuta(cookieData.priceValuta);
    }

    if(cookieData != null && cookieData.personType_1) {
        initPassengers(cookieData);
    }
    else {
        // If nothing is stored in cookie, adults is set to 1 (personType_1 is always adult)
        var selector = document.getElementById("personType_1");
        selector.value = 1;
        selector.onchange();
    }
}

var form;



