﻿
$(document).ready(function() {

    //allocate functions to buttons
    $("#btnQA_Cancel").attr("href", "javascript:hideQAAddress()");
});

function selectQAAddress(onSelectedLine) {
    var sel = $("#selQA_Address");
    var lineid = sel.find(":selected").val();
    if (lineid != null) {
        SalesAndMarketingSite.Services.Lookups.GetQuickAddressLine(lineid, onCompletedGetQuickAddressLine, onFailed, onSelectedLine);
    }
    if (lineid != "") { QuickAddress.hide(); }
}

function hideQAAddress() {
    QuickAddress.hide();
}

QuickAddress =
{
    show: function(obj, postcode, line1, street, town, onSelectedLine) {

        $("#selQA_Address").unbind("change");
        $("#btnQA_Search").unbind("click");
        $("#selQA_Address").bind("change", function() { selectQAAddress(onSelectedLine) });
        $("#btnQA_Search").bind("click", function() { searchQAAddress(onSelectedLine); return false; });

        obj = $(obj);
        $('#txtQA_Postcode').val(postcode);
        $('#txtQA_HouseNo').val(line1);
        $('#txtQA_Street').val(street);
        $('#txtQA_Town').val(town);

        //var sel = $get('selQA_Address');
        //sel.options.length = 0;
        var sel = $('#selQA_Address');        
        //sel.length = 0;
        hide(sel);

        $(document).showMask();

        var qa = $('#divQA_QuickAddress');
        qa.show();
        //qa.css("top", obj.offset().top - obj.offsetParent().offset().top - 173);
        //qa.css("left", obj.offset().left - obj.offsetParent().offset().left - 62);
        qa.formcenter();

        if ($('#txtQA_Postcode').val() != "" || !($('#txtQA_HouseNo').val() == ""))
            searchQAAddress(onSelectedLine);
    },

    hide: function() {
        var qa = $('#divQA_QuickAddress');
        qa.hide();
        $(document).hideMask();
    }
}

function searchQAAddress(onSelectedLine) {
    var houseNo = $('#txtQA_HouseNo').val();
    var street = $('#txtQA_Street').val();
    var town = $('#txtQA_Town').val();
    var postcode = $('#txtQA_Postcode').val();

    //var sel = $get('selQA_Address');
    var sel = $('#selQA_Address');
    hide(sel);

    // ?? check rules
    var buff = new StringBuffer();
    if (postcode == "" && houseNo == "" && (street == "" || town == ""))
        buff.append("Missing postcode or house number and town or street\r\n");

    if (buff.length() > 0)
        alert(String.format('INVALID DETAILS!\nYour request contains the following error(s):\n\n{0}\n\rPlease correct your details and try again.\r\nThank You.', buff.toString()));
    else {
        //var divWait = $get('divQA_Wait');
        var divWait = $('#divQA_Wait');
        showWaiting(divWait);

        SalesAndMarketingSite.Services.Lookups.GetQuickAddressList(houseNo, street, town, postcode, onCompletedGetQuickAddressList, onFailed, onSelectedLine);
    }
}

function onFailedQA(a, b, c) {
    //var divWait = $get('divQA_Wait');
    var divWait = $('#divQA_Wait');
    hide(divWait);

    onFailed(a, b, c);
}

function onCompletedGetQuickAddressList(results, onSelectedLine) {
    //var sel = $get('selQA_Address');
    //sel.options.length = 0;
    var sel = $('#selQA_Address');
    deleteOptions(sel)
    //sel.length = 0;

    //var divWait = $get('divQA_Wait');
    var divWait = $('#divQA_Wait');
    hide(divWait);

    /*
    if (results.length == 1) {
        SalesAndMarketingSite.Services.Lookups.GetQuickAddressLine(results[0].value, onCompletedGetQuickAddressLine, onFailedQA, onSelectedLine);
        hideQAAddress()
    }
    else if (results.length == 0) {
    */
    if (results.length == 0) {
        addOption(sel, '-- Incomplete search. Please provide more info --', '');
    }
    else {
        for (i = 0; i < results.length; i++) {
            if (i == 0)
                addOption(sel, '-- Please select (from ' + results.length + ') --', '');
            addOption(sel, results[i].key, results[i].value);
        }
    }

    show(sel);
}

function onCompletedGetQuickAddressLine(results, onSelectedLine) {
    var output = { line1: results[0],
        line2: results[1],
        line3: results[2],
        town: results[3],
        county: results[4],
        postcode: results[5]
    }

    onSelectedLine(output);
    //QuickAddress.hide();
}
