﻿var HistoryFlag = false;
var SilverlightReady = null;
var SearchMode = "normal";  //"normal" for an init search, "location" is search for location only, "context" is search for context only

function AddHistory(title, paras) {
    HistoryFlag = false;
    document.getElementById("HistoryFrame").src = "Blank.aspx?title=" + title + "&paras=" + paras;
}

function BackToHistory(paras) {
    var oSilverLight = document.getElementById("SLClient");
    oSilverLight.Content.Page.HistoryCallback(paras);
}

function CopyToClipboard(paras) {
    window.clipboardData.setData('text', paras);
}

var HttpUitility = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    },

    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}

function DoResize() {
    try {
        autoScrollResize();
        var control = document.getElementById("SLClient");
        var h = Math.max(600, document.documentElement.clientHeight, document.documentElement.offsetHeight);
        var w = Math.max(900, document.documentElement.clientWidth);
        control.style.width = w + "px";
        control.style.height = (h - 10) + "px";
        var container = document.getElementById("silverlightControlHost");
        container.style.width = control.style.width;
        container.style.height = control.style.height;

        var dummy = document.getElementById("dummyLink");

        if (dummy) {
            dummy.style.display = "none";
        }
        SplashScreen_Resize();
    }
    catch (ex) { }
}

function disableScrollbar() {
    document.getElementsByTagName('body')[0].style.overflow = 'hidden';
    document.getElementsByTagName('html')[0].style.overflow = 'hidden';
}

function autoScrollResize() {
    var ch = document.body.clientHeight;
    var cw = document.body.clientWidth;
    var bh = document.documentElement.clientHeight;
    var bw = document.documentElement.clientWidth;
    if (ch < bh && cw < bw) {
        document.getElementsByTagName('body')[0].style.overflow = 'hidden';
        document.getElementsByTagName('html')[0].style.overflow = 'hidden';
    } else {
    document.getElementsByTagName('html')[0].style.overflow = 'auto';
    }
}

if (typeof XMLHttpRequest == "undefined")
    XMLHttpRequest = function() {
        return new ActiveXObject(
            navigator.userAgent.indexOf("MSIE 5") >= 0 ?
            "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
        );
    };

function GetAjaxValue(url) {
    var xml = new XMLHttpRequest();
    xml.open("POST", url, true);   //True to asyn mode
    xml.send(null);
    return xml.responseText;
}

function ShowBottomMessage(msg) {
    var o = document.getElementById("BottomMessage");
    if (o) o.innerHTML = msg;
}

function AddLog(entryType, query, where, description) {
    try {
        var sScreen = "";
        try {
            sScreen = window.screen.width + "x" + window.screen.height;
        }
        catch (ex) {
            sScreen = "0x0";
        }
        var LoggingLink = "/logging.aspx?EntryType=" + entryType +
        "&query=" + HttpUitility.encode("" + query) +
        "&where=" + HttpUitility.encode("" + where) +
        "&desc=" + HttpUitility.encode("" + description) +
        "&From=" + HttpUitility.encode("" + window.location) +
        "&Referrer=" + HttpUitility.encode("" + document.referrer) +
        "&screen=" + sScreen;
        GetAjaxValue(LoggingLink);
    }
    catch (ex) { }
}

function DoLoadPage() {
    LogSilverlightVersion();
    if (SilverlightReady == null) DetectSilverlight();
    if (SilverlightReady == null || SilverlightReady == false) {
        window.location = "/SilverlightInstall.htm";
    }
    DoResize();
}

var minSilverlightVersion = "3.0.40624.0"; //SL3 RTW version

function DetectSilverlight() {
    SilverlightReady = CheckSilverlight(minSilverlightVersion);
}

function LogSilverlightVersion() {
    if (CheckSilverlight("4.0")) {
        AddLog("SLVersion", "4.0", "", "");
    }
    else if (CheckSilverlight("3.0")) {
        AddLog("SLVersion", "3.0", "", "");
    }
    else if (CheckSilverlight("2.0")) {
        AddLog("SLVersion", "2.0", "", "");
    }
    else if (CheckSilverlight("1.0")) {
        AddLog("SLVersion", "1.0", "", "");
    }
    else {
        AddLog("SLVersion", "0", "", "");
    }
}

function CheckSilverlight(version) {
    var isVersionSupported = false;
    var container = null;

    try {
        var control = null;

        try {
            //silverlight plugin check for IE
            control = new ActiveXObject('AgControl.AgControl');
            if (version == null) {
                isVersionSupported = true;
            }
            else if (control.IsVersionSupported(version)) {
                isVersionSupported = true;
            }
            control = null;
        }
        catch (e) {
            //silverlight plugin check for non IE
            var plugin = navigator.plugins["Silverlight Plug-In"];
            if (plugin) {
                if (version === null) {
                    isVersionSupported = true;
                }
                else {
                    var actualVer = plugin.description;
                    if (actualVer === "1.0.30226.2")
                        actualVer = "2.0.30226.2";
                    var actualVerArray = actualVer.split(".");
                    while (actualVerArray.length > 3) {
                        actualVerArray.pop();
                    }
                    while (actualVerArray.length < 4) {
                        actualVerArray.push(0);
                    }
                    var reqVerArray = version.split(".");
                    while (reqVerArray.length > 4) {
                        reqVerArray.pop();
                    }

                    var requiredVersionPart;
                    var actualVersionPart
                    var index = 0;


                    do {
                        requiredVersionPart = parseInt(reqVerArray[index]);
                        actualVersionPart = parseInt(actualVerArray[index]);
                        index++;
                    }
                    while (index < reqVerArray.length && requiredVersionPart === actualVersionPart);

                    if (requiredVersionPart <= actualVersionPart && !isNaN(requiredVersionPart)) {
                        isVersionSupported = true;
                    }
                }
            }
        }
    }
    catch (e) {
        isVersionSupported = false;
    }
    if (container) {
        document.body.removeChild(container);
    }

    return isVersionSupported;
}

