var Page = Class.create({
    clientName: false,
    initialize: function() {
    }, 
    search: function() {
        if (! this.checkCriteria()) {
            return;
        }
        this.openResultWindow(this.getCriteria());
    },
    setClientName: function(name) {
        this.clientName = name;
    },
    openResultWindow: function (criteria) {
        var option = (getQueryVariable("debug") == "1") ? "yes" : "no";
        var parameters = {
            top: ((screen.height - 400)/ 2),
            left: ((screen.width - 674)/2),
            width: 674,
            height: 400,
            toolbar: option,
            location: option,
            addressbar: option,
            status: option,
            menu: option,
            resizable: "yes",
            scrollbars: "yes"
        };
/*        var winParams = this.setClientWindowParams(getQueryVariable("client"));
        
        if (winParams != false) {
            parameters.width  = winParams.width; 
            parameters.height = winParams.height;
        }*/
        
        var clientName = !this.clientName 
                         ? getQueryVariable("client")
                         : this.clientName;
                         
        var url = $A(window.location.href.split('?')).first()
                  + '?action=search&client=' + clientName;

        url = url + "&" + criteria.toQueryString();
        
        var trackerUrl = $H(window.location
                                  .search
                                  .substring(1)
                                  .parseQuery()).get('click');
                                  
        url = trackerUrl ? trackerUrl + url : url;
        
        var options = $H(parameters).toQueryString().replace(new RegExp("&", "g"), ", ");
        var target = "gtm_" + Math.random().toString().replace(".", "");
        var popupWindow = window.open(url, target, options);
        popupWindow.focus();
    },
    checkCriteria: function() {
        if (document.getElementById("departureMonth") && !form.checkDate()) {
            return false;
        }
        if (! checkIATA($("departureAirport"))) {
            alert("Please type departure airport");
            $("departureAirport").focus();
            $("departureAirport").select();
            return false;
        }
        if (! checkIATA($("destinationAirport"))) {
            alert("Please type destination airport");
            $("destinationAirport").focus();
            $("destinationAirport").select();
            return false;
        }
        return true;
    },
    getCriteria: function() {
        var form = document.forms['flight_search'];
        var criteria = {
            passengers: parseInt(form.elements["numAdults"].value),
            departureDate: this.getDate(form.elements["departureDay"].value, form.elements["departureMonth"].value),
            returnDate: this.getDate(form.elements["returnDay"].value, form.elements["returnMonth"].value),
            departure: this.getIATA($("departureAirport")),
            destination: this.getIATA($("destinationAirport")),
            cabin: $("cabin") ? $("cabin").value : "e",
            trip: $("tripTypeR").checked ? 2 : 1,
            popup: true
        };

        if (form.elements['deptime'] && form.elements['arrtime']) {
            criteria['departureTime'] = form.elements['deptime'].value; 
            criteria['returnTime']    = form.elements['arrtime'].value;
        }
        return $H(criteria);
    },
    getDate: function(day, yearMonth) {
        var day = parseInt(day);
        if (day < 10) {
            day = "0" + day;
        }
        return day + yearMonth.substr(4, 2) + yearMonth.substr(0, 4);
    },
    getIATA: function(element) {
        switch (element.nodeName.toLowerCase()) {
            case "select":
                return element.options[element.selectedIndex].value;
            break;
            case "input":
                return element.value.substr(element.value.indexOf("[") + 1, 3);
            break;
        }
    
    },
    setClientWindowParams: function(client) {
        switch (client) {
            case "perthComAu":
                return {width: 800, height: 600};
            break;
        }
        
        return false;
    }
});