var LoadingImg = $("<img>").attr("src", "/images/ajax_load.gif").attr("alt", "Loading....").attr("class", "imgWait");
$(document).ready(function() {
    $("#btnSearch").click(function() {

        if (($("#from_location").val() == "") || ($("#from_location").val() == "enter city or airport code")) {
            alert("PLEASE ENTER A VALID PICK-UP LOCATION\nCHECK YOUR SPELLING AND\nDO NOT ENTER A STATE OR COUNTRY, ONLY THE CITY");
            return false;
        }

        if (!validateDate($('#pickup_date').get(0))) return false;
        if (!validateDate($('#dropoff_date').get(0))) return false;
        if (!DataCompare($('#pickup_date').val(), $('#dropoff_date').val())) { alert("Pick-Up date must less than Drop-Off date."); return false; }


        var dataStr = $("#frmCarSearch").serialize();
        dataStr += "&process_id=10"
        $("#frmCarSearch").attr("action", "/result.php?" + dataStr);
        $("#frmCarSearch").trigger("submit");
    });
    $("#from_location").focus(function() { $("#from_location").select(); });
    var today = new Date();
    today.setDate(today.getDate() + 1);
    $("#pickup_date").datepicker({ minDate: today, onSelect: fnDateSelected, hideIfNoPrevNext: true, duration: 'fast' });
    $("#dropoff_date").datepicker({ minDate: today, hideIfNoPrevNext: true, duration: 'fast' });
    var dataStr = "process_id=230&businessobject=AIRPORT&mode=CITY&cityname=&statename=&countryname=&usepaging=N&pagenumber=-1&itemsperpage=-1";
    $("#from_location").autocomplete("bmtrvlib_proxy.php?" + dataStr, {
        minChars: 3,
        dataType: "text/xml",
        parse: parseXML,
        formatItem: formatItem,
        formatResult: formatResult
    }).result(function(event, item) { /* for result options */ });
});

function isArray(obj) {
    //returns true is it is an array
    if (obj.constructor.toString().indexOf("Array") == -1)
        return false;
    else
        return true;
}

function parseXML(xml) {
    var json = $.xml2json(xml);
    var results = [];
    if (json.d != null) {
        if (json.d.r != null) {

            if (!isArray(json.d.r)) {
                var airportcode = json.d.r.s1.toUpperCase(); ;
                var airportname = json.d.r.s2;
                var city = json.d.r.s5;
                var state = json.d.r.s4;
                var country = json.d.r.s3;
                if ((city != null) && (country != null)) {

                    Avalue = country;
                    if (state != "") { Avalue += ", " + state; }
                    Avalue += ", " + city;
                    Avalue += " - (" + airportcode + ") " + airportname;
                    results[results.length] = { "data": { text: Avalue, value: Avalue },
                        result: Avalue, value: Avalue
                    };
                }
            }
            else {

                for (var idx = 0; idx <= json.d.r.length - 1; idx++) {
                    var airportcode = json.d.r[idx].s1.toUpperCase(); ;
                    var airportname = json.d.r[idx].s2;
                    var city = json.d.r[idx].s5;
                    var state = json.d.r[idx].s4;
                    var country = json.d.r[idx].s3;
                    if ((city != null) && (country != null)) {

                        Avalue = country;
                        if (state != "") { Avalue += ", " + state; }
                        Avalue += ", " + city;
                        Avalue += " - (" + airportcode + ") " + airportname;
                        results[results.length] = { "data": { text: Avalue, value: Avalue },
                            result: Avalue, value: Avalue
                        };
                    }
                }
            }
        }
    }

    return results;
};

function formatItem(data) {
    return data.text;
};

function formatResult(data) {
    return data.text;
};
