﻿function flashLoadCo(theCo) {
    flashProxy.call('loadCo', theCo)
    var top = document.getElementById("chart_builder_top");
    if (top) {
        top.style.display = "none";
    }
}

function flashLoadAndReplaceCo(theCo) {
    flashProxy.call('loadAndReplaceCo', theCo)
    var top = document.getElementById("chart_builder_top");
    if (top) {
        top.style.display = "none";
    }
}

function checkEnterChartCode(e) {
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode;
    } else if (e) {
        keycode = e.which;
    } else {
        return true;
    }
    if (keycode != 13) {
        return true;
    } else {
        submitChartCode("first");
    }
}

function submitChartCode(type) {
    if (type == "first") {
        var code = document.getElementById("chart_code");
        if (code.value == 'Enter code or name' || code.value.replace(/^\s+|\s+$/, '') == '') {
            alert("Please enter a code or name.");
            code.value = "";
            code.focus();
            return false;
        }
        doFlashLookup();
    } else {
        var codeAu = document.getElementById("indices_au");
        var codeOs = document.getElementById("indices_os");
        var sectAu = document.getElementById("sectors_au");

        if (codeAu.value != "-") {
            flashLoadCo(codeAu.value);
            codeAu.selectedIndex = 0;
        }
        if (codeOs.value != "-") {
            flashLoadCo(codeOs.value);
            codeOs.selectedIndex = 0;
        }
        if (sectAu.value != "-") {
            flashLoadCo(sectAu.value);
            sectAu.selectedIndex = 0;
        }
    }
    return false;
}

var strFlashLookupCode;

function doFlashLookup() {
    var ef = document.getElementById("chart_code");
    var btn = document.getElementById("chart_submit");
    var strCtry = "AX";

    strFlashLookupCode = ef.value.toUpperCase();
    strFlashLookupCode = strFlashLookupCode == 'AUDUSD=X' ? 'AUD=X' : strFlashLookupCode;
    ef.value = strFlashLookupCode;

    // If bypass or known code go straight to page
    if (strCtry == "AX" && knownASX[strFlashLookupCode]) {
        flashLoadCo(strFlashLookupCode + "." + strCtry);
        return false;
    }

    var posEF = calcAbsolutePos(ef);
    var posBTN = calcAbsolutePos(btn);

    x1 = posEF.x - 1;
    y1 = posEF.y - 1;
    x2 = posEF.x + posEF.width + 2;
    y2 = posEF.y + posEF.height + 1;

    if (!divLookupBox) {
        divLookupBox = document.createElement("div");
        document.body.appendChild(divLookupBox);
    }

    divLookupBox.className = "loadingBox";
    divLookupBox.innerHTML = "Searching for: " + strFlashLookupCode;
    setElementPos(divLookupBox, x1, y1, x2 - x1, y2 - y1);
    btn.disabled = true;

    function handleFlash(str) {
        if (!str) {
            alert("Problem retrieving data, please retry.");
            hideLookup();
            btn.disabled = false;
            return;
        }
        if (str.indexOf("__good__") != -1) {
            // Code found
            hideLookup();
            flashLoadCo(strFlashLookupCode + "." + strCtry);
            btn.disabled = false;
        } else {
            var flashboxheight = fMozilla ? 140 : 119;
            var iHeight = str.indexOf("Sorry") == -1 ? flashboxheight : 60;

            setElementPos(divLookupBox, posEF.x + 1, posEF.y + posEF.height + 1, 350, iHeight);
            divLookupBox.className = "lookupBox";
            divLookupBox.innerHTML = str;
            btn.disabled = false;
        }
    }

    var strURL = formatURL("http://{domain}/lookup/default.aspx?feedid=14&code={code}&ctry={ctry}", strFlashLookupCode, strCtry);
    loadDoc(strURL, handleFlash);
}

function doFlashLookupCode(strCode) {
    document.getElementById("chart_code").value = strCode;
    hideLookup();
}

function handleKeyDown(e) {
    if (!e)
        e = window.event;

    if (e.keyCode != 13)
        return;

    var code = document.getElementById("chart_code");
    if (code.value == 'Enter code or name' || code.value.replace(/^\s+|\s+$/, '') == '') {
        alert("Please enter a code or name.");
        code.value = "";
        code.focus();
        return false;
    }
    doFlashLookup();
    return false;
}

document.getElementById("chartbuilder").onkeydown = handleKeyDown;
