﻿function selectUrl(selectElement) {
    if (selectElement != null && selectElement.value != null && selectElement.value.length > 0) {
        window.location = selectElement.value;
    }
}

function adjustMenu() {
    var elem = document.getElementById("menuTable");
    if (elem != null) {
        var anchors = elem.getElementsByTagName("a");
        if (anchors != null) {
            for (var i = 0; i < anchors.length; ++i) {
                if (anchors[i].style.cursor === "text") {
                    anchors[i].style.width = "120px";
                    anchors[i].style.textDecorationNone = 'true';
                    anchors[i].style.color = "black";
                }
            }
            elem.style.display = "";            
        }
    }
}

function initCentreNumber() {
    var baseCentreNumber = $('.baseCentreNumber');
    if (baseCentreNumber != null && !baseCentreNumber.disabled) {
        baseCentreNumber.autocomplete('../AutoCompleteCentre.ashx', { cacheLength: 1, width: 450, minChars: 3,
            formatResult: function(row, i, n) { return jQuery.trim(String(row).substring(0, 6)); },
            max: 100
        }).result(function(event, data, formatted) { $('.goBtn').click(); });
    }
}

function initSiteDialogue() {
    var siteDialogue = $("#siteDialogue");
    if (siteDialogue != null) {
        siteDialogue.dialog({ autoOpen: false, width: 450, modal: false, show: 'fold', hide: 'scale', bgiframe: true, position: ['right', 'top'] });
    }
}

$(document).ready(function() {
initCentreNumber();
initSiteDialogue();    
});

function onSiteInfoClick() {

    var i;
    var ddlSiteInfo = document.getElementById('ddlSiteInfo');
    if (ddlSiteInfo.options.length == 0) {
        var select = $('.sites')[0];
        var options = select.options;
        for (i = 0; i < options.length; ++i) {
            ddlSiteInfo.add(new Option(options[i].text, options[i].value));
        }
        ddlSiteInfo.selectedIndex = select.selectedIndex;
        ddlSiteInfo.disabled = (options.length <= 1);
        onDdlSiteInfoChange(ddlSiteInfo);
    }
    $('#siteDialogue').dialog('open');
}

function setTd(tdname, text) {
    if (text == null) {
        text = '';
    }
    var element = document.getElementById(tdname);
    if (element != null) {
        element.innerHTML = text;
    }
}

function displaySite(siteInfo) {
    setTd('tdPostcode', siteInfo.PostCode);
    setTd('tdTown', siteInfo.Town);
    setTd('tdTelephone', siteInfo.Telephone);
    setTd('tdFax', siteInfo.FaxNumber);
    setTd('tdPrincipal', siteInfo.Principal);
    setTd('tdPrincipalEmail', siteInfo.PrincipalEmail);
    var address = '';
    if (siteInfo.Address1 != null) {
        address = address + siteInfo.Address1;
    }
    if (siteInfo.Address2 != null && siteInfo.Address2.length > 0) {
        if (address.length > 0) {
            address = address + "<br />";
        }
        address = address + siteInfo.Address2;
    }
    if (siteInfo.Address3 != null && siteInfo.Address3.length > 0) {
        if (address.length > 0) {
            address = address + "<br />";
        }
        address = address + siteInfo.Address3;
    }
    setTd('tdAddress', address);
}

function onDdlSiteInfoChange(ddlSiteInfo) {
    $.ajax({
        url: "../Shared/Services/SiteInfo.svc/GetSiteInfo",
        data: '"' + ddlSiteInfo.options[ddlSiteInfo.selectedIndex].value + '"',
        type: "POST",
        processData: false,
        dataType: "text",  // not "json" we'll parse
        contentType: "application/json",
        success: function(res) { displaySite(eval('(' + res + ')')); },
        error: function(xhr) { alert('error'); }
    });
}
