﻿if (typeof (mrO) == 'undefined') {
    mrO = {};
}
//if (typeof (mrO.searchInput) == 'undefined') {
//    mrO.searchInput = {};
//}

mrO.searchInput = function(Id, args) {
    this.Id = Id;
    this.args = args;

    this.elementInputs = new Object();
    this.cityInputs = new Object();
    this.dateInputs = new Object();

    // incoming arguments...
    this.oP = this.args ? this.args : {};
    var k, def = {
        //WebService:WebService.proc()  MUST BE DEFINED
        //XSLFileName:"bla.xsl"         MUST BE DEFINED
        //delay: 650,
        //processStyle: "textbox"         //textbox || popup
    };
    for (k in def) {
        if (typeof (this.oP[k]) != typeof (def[k])) {
            this.oP[k] = def[k];
        }
    }
};

mrO.searchInput.prototype.init = function() {
    //nothing to do yet... :=
};

mrO.searchInput.prototype.sing = function() {
    var singString = "";
    singString = singString + this.getCity1IATA() + "\n";
    singString = singString + this.getCity2IATA() + "\n";
    singString = singString + this.getFromDate() + "\n";
    singString = singString + this.getToDate() + "\n";
    
    return singString;
};

/* ARBITRARY OBJECTS..? */
mrO.searchInput.elementObject = function(significance, id, template) {
    this.significance = significance;
    this.id = id;
    this.template = template;

    this.element = null;
};

mrO.searchInput.elementObject.prototype.ensureElements = function() {
    if (this.element == null && this.id != null) {
        this.element = document.getElementById(this.id);
    }
};

mrO.searchInput.elementObject.prototype.sing = function() {
    alert("significance: " + this.significance + "\nid:" + this.id + "\ntemplate:" + this.template);
};

/*DATE INPUT OBJECT*/
mrO.searchInput.dateInputObject = function(significance, textboxId, dateDropdownId, monthDropdownId) {
    this.significance = significance;
    this.textboxId = textboxId;
    this.dateDropdownId = dateDropdownId;
    this.monthDropdownId = monthDropdownId;

    this.textboxElement = null;
    this.dateDropdownElement = null;
    this.monthDropdownElement = null;
};
mrO.searchInput.dateInputObject.prototype.ensureElements = function() {
    if (this.textboxElement == null && this.textboxId != null) {
        this.textboxElement = document.getElementById(this.textboxId);
    }
    if (this.dateDropdownElement == null && this.dateDropdownId != null) {
        this.dateDropdownElement = document.getElementById(this.dateDropdownId);
    }
    if (this.monthDropdownElement == null && this.monthDropdownId != null) {
        this.monthDropdownElement = document.getElementById(this.monthDropdownId);
    }
};
mrO.searchInput.dateInputObject.prototype.sing = function() {
    alert("significance: " + this.significance + "\ntextboxId:" + this.textboxId + "\ndateDropdownId:" + this.dateDropdownId + "\nmonthDropdownId:" + this.monthDropdownId);
};
mrO.searchInput.dateInputObject.prototype.getSelectedDate = function() {
    var stringDate;
    //    var date = new Date(0, 0, 0, 0, 0, 0);
    this.ensureElements();
    if (this.textboxElement != null) {
        stringDate = this.textboxElement.value;
    } else if (this.dateDropdownElement != null && this.monthDropdownElement != null) {
        var day = this.dateDropdownElement[this.dateDropdownElement.selectedIndex].value;
        var monthAndYear = this.monthDropdownElement[this.monthDropdownElement.selectedIndex].value;
        stringDate = day + "/" + monthAndYear

    } else {
        stringDate = null;
    }
    //why oh why would you wanna use this??
    //    var arrDate = stringDate.split('/');
    //    date.setFullYear(arrDate[2], (arrDate[1] - 1), arrDate[0]);

    return stringDate;
};

/*CITY INPUT OBJECT*/
mrO.searchInput.cityInputObject = function(significance) {
    this.significance = significance;
    
    // to be bound later
    this.textboxId = null;
    this.cityDropdownId = null;
    this.hiddenSwitchId = null;
    this.hiddenACId = null;
    this.countryDropdownId = null;
    this.areaDropdownId = null;

    // to be looked up once needed
    this.textboxElement = null;
    this.cityDropdownElement = null;
    this.hiddenSwitchElement = null;
    this.hiddenACElement = null;
    this.countryDropdownElement = null;
    this.areaDropdownElement = null;
};

mrO.searchInput.cityInputObject.prototype.ensureElements = function() {
    if (this.textboxElement == null && this.textboxId != null) {
        this.textboxElement = document.getElementById(this.textboxId);
    }
    if (this.cityDropdownElement == null && this.cityDropdownId != null) {
        this.cityDropdownElement = document.getElementById(this.cityDropdownId);
    }
    if (this.hiddenSwitchElement == null && this.hiddenSwitchId != null) {
        this.hiddenSwitchElement = document.getElementById(this.hiddenSwitchId);
    }
    if (this.hiddenACId == null && this.textboxId != null) { // generate if not supplied
        this.hiddenACId = "AC_IATA_" + this.textboxId;
    } else if (this.hiddenACId != null && this.hiddenACId == '') { // if empty set to null
        this.hiddenACId = null;
    }
    if (this.hiddenACElement == null && this.hiddenACId != null) {
        this.hiddenACElement = document.getElementById(this.hiddenACId);
    }
    if (this.countryDropdownElement == null && this.countryDropdownId != null) {
        this.countryDropdownElement = document.getElementById(this.countryDropdownId);
    }
    if (this.areaDropdownElement == null && this.areaDropdownId != null) {
        this.areaDropdownElement = document.getElementById(this.areaDropdownId);
    }
};

mrO.searchInput.cityInputObject.prototype.addInput = function(template, input) {
    if (template == "textbox") {
        this.textboxId = input["textboxId"];
    }
    else if (template == "cityDropdown") {
        this.cityDropdownId = input["cityDropdownId"];
    }
    else if (template == "hiddenSwitch") {
        this.hiddenSwitchId = input["hiddenSwitchId"];
    }
    else if (template == "hiddenAC") {
        this.hiddenACId = input["hiddenACId"];
    }
    else if (template == "cascadingDropdown") {
        this.countryDropdownId = input["countryDropdownId"];
        this.areaDropdownId = input["areaDropdownId"];
    }
};

mrO.searchInput.cityInputObject.prototype.getSelectedIATA = function() {
    var iata = null;
    this.ensureElements();

    if (this.countryDropdownElement != null && this.areaDropdownElement != null) {
        if (this.areaDropdownElement.selectedIndex >= 0) {
            iata = this.areaDropdownElement[this.areaDropdownElement.selectedIndex].value;
        }
    }
    else if (this.hiddenSwitchElement != null) {
        if (this.hiddenSwitchElement.value == "list") {
            iata = this.cityDropdownElement[this.cityDropdownElement.selectedIndex].value;
        } else if (this.hiddenSwitchElement.value == "text") {
            if (this.hiddenACElement != null && this.hiddenACElement.value != "") {
                iata = this.hiddenACElement.value;
            } else {
                iata = this.textboxElement.value;
            }
        }
    }
    else {
        if (this.hiddenACElement != null && this.hiddenACElement.value != "") {
            iata = this.hiddenACElement.value;
        } else {
            iata = this.textboxElement.value;
        }
    }
    return iata;
};

/*
* REGISTRATORS
*/
// arbitrary elements..
mrO.searchInput.prototype.registerElement = function(significance, id, template) {
    id = (id == '') ? null : id;
    template = (template == '') ? null : template;
    this.elementInputs[significance] = new mrO.searchInput.elementObject(significance, id, template);
};

// accepts a call on the form:
//    registerCityInput('city1', {"textbox":{"textboxId":"id_to_textbox"},"cascadingDropDown":{"countryDropdown":"id_to_countryDropdown", "areaDropdown": "id_to_areaDropdown"}}
mrO.searchInput.prototype.registerCityInput = function(significance, inputs) {
    if (typeof (this.cityInputs[significance]) == 'undefined' || this.cityInputs[significance] == null) {
        this.cityInputs[significance] = new mrO.searchInput.cityInputObject(significance);
    }
    if (typeof (significance) == 'undefined' || significance == null) {
        return;
    }
    if (typeof (inputs) == 'undefined' || inputs == null) {
        return;
    }
    
    for (var input in inputs) {
        this.cityInputs[significance].addInput(input, inputs[input]);
    }
};

// cityinput, textbox, autocomplete or dropdown
//mrO.searchInput.prototype.registerCityInput = function(significance, textboxId, cityDropdownId, hiddenSwitchId, hiddenACId) {
//    textboxId = (textboxId == '') ? null : textboxId;
//    cityDropdownId = (cityDropdownId == '') ? null : cityDropdownId;
//    hiddenSwitchId = (hiddenSwitchId == '') ? null : hiddenSwitchId;
//    if (hiddenACId == null) { // generate if not supplied
//        hiddenACId = "AC_IATA_" + textboxId;
//    } else if (hiddenACId == '') { // if empty set to null
//        hiddenACId = null;
//    }

//    this.cityInputs[significance] = new mrO.searchInput.cityInputObject(significance, textboxId, cityDropdownId, hiddenSwitchId, hiddenACId);
//};

// dateinputs, 2xdropdown or textbox..
mrO.searchInput.prototype.registerDateInput = function(significance, textboxId, dateDropdownId, monthDropdownId) {
    textboxId = (textboxId == '') ? null : textboxId;
    dateDropdownId = (dateDropdownId == '') ? null : dateDropdownId;
    monthDropdownId = (monthDropdownId == '') ? null : monthDropdownId;

    this.dateInputs[significance] = new mrO.searchInput.dateInputObject(significance, textboxId, dateDropdownId, monthDropdownId);
};


/*
* FUNCTIONS
*/
mrO.searchInput.prototype.getCityIATA = function(significance) {
    if (this.cityInputs[significance] != null) {
        return this.cityInputs[significance].getSelectedIATA();
    }
};

mrO.searchInput.prototype.getCity1IATA = function() {
    return this.getCityIATA("city1");
};
mrO.searchInput.prototype.getCity2IATA = function() {
    return this.getCityIATA("city2");
};
mrO.searchInput.prototype.getCity3IATA = function() {
    return this.getCityIATA("city3");
};
mrO.searchInput.prototype.getCity4IATA = function() {
    return this.getCityIATA("city4");
};

mrO.searchInput.prototype.getDate = function(significance) {
    return this.dateInputs[significance].getSelectedDate();
};
mrO.searchInput.prototype.getFromDate = function() {
    return this.getDate("fromdate");
};
mrO.searchInput.prototype.getToDate = function() {
    return this.getDate("todate");
};

//mrOairSearchInput = new mrOsearchInput("airSearchInput", {});

//testSearchInput.registerCityInput("city1", "ctl01_ctl02_ctl00_city1TextBox", "ctl01_ctl02_ctl00_city1DropDown", "ctl01_ctl02_ctl00_cities1ViewState", "AC_IATA_ctl01_ctl02_ctl00_city1TextBox");
//testSearchInput.registerCityInput("city2", "ctl01_ctl02_ctl00_city2TextBox", null, "ctl01_ctl02_ctl00_cities2ViewState", "AC_IATA_ctl01_ctl02_ctl00_city2TextBox");
//setTimeout(function() { alert(" 6 sec: " + testSearchInput.getCity1IATA() + " -> " + testSearchInput.getCity2IATA()); }, 6000);
//setTimeout(function() { alert("12 sec: " + testSearchInput.getCity1IATA() + " -> " + testSearchInput.getCity2IATA()); }, 12000);
//setTimeout(function() { alert("18 sec: " + testSearchInput.getCity1IATA() + " -> " + testSearchInput.getCity2IATA()); }, 18000);

//testSearchInput.registerDateInput("fromdate", "textboxid", "ctl01_ctl02_ctl00_day1DropDown", "ctl01_ctl02_ctl00_monthAndYear1DropDown");
//testSearchInput.registerDateInput("todate", "textboxid", "ctl01_ctl02_ctl00_day2DropDown", "ctl01_ctl02_ctl00_monthAndYear2DropDown");
//setTimeout(function() { alert(" 6 sec: " + testSearchInput.getFromDate() + " -> " + testSearchInput.getToDate()); }, 6000);
//setTimeout(function() { alert("12 sec: " + testSearchInput.getFromDate() + " -> " + testSearchInput.getToDate()); }, 12000);
//setTimeout(function() { alert("18 sec: " + testSearchInput.getFromDate() + " -> " + testSearchInput.getToDate()); }, 18000);
