﻿function dg(oid) { return document.getElementById(oid) }
function getScrollX() { return parseInt(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) }
function getScrollY() { return parseInt(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) }

var GlobalMouseX = 0, GlobalMouseY = 0;
function getGlobalMouseXY(e) {
    if (!e || e == null || e == "null") e = window.event;
    GlobalMouseX = e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) + "px";
    GlobalMouseY = e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px";
}
document.onmousemove = getGlobalMouseXY;

function ShowAjax(mode, Message) {
    if (!dg("dvAjaxWait")) {
        var dv = document.createElement("div"); dv.id = 'dvAjaxWait';dv.style.position = "absolute";dv.style.verticalAlign = "middle"; dv.style.visibility = "visible"; dv.style.display = ""; dv.style.border = "#0033ff 3px double";
        dv.style.background = "#ffffff";dv.style.fontWeight = "bold"; document.body.appendChild(dv);
    } else {var dv = dg("dvAjaxWait"); dv.style.visibility = "visible"; dv.style.display = "";}

    dv.innerHTML = Message
    if (mode == 'fullscreen') {//setOpacity(70, dv);
        dv.style.top = "0px"; dv.style.left = "0px"; dv.style.width = screen.width + "px"; dv.style.height = screen.height + "px";
    } else {//setOpacity(90, dv);
        dv.style.width = "200px"; dv.style.height = "32px";
        dv.style.left = (parseInt(GlobalMouseX) + 10) + "px"; dv.style.top = GlobalMouseY;
    }
}
function HideAjax() { if (dg("dvAjaxWait")) { dg("dvAjaxWait").style.visibility = "hidden"; dg("dvAjaxWait").style.display = "none"; } }

function modalBox(boxW, boxH) {
    if (!dg("modalBox")) {
        var bg = document.createElement("div"); bg.id = "modalBG";bg.style.position = "absolute";bg.style.padding = "3px"; bg.style.top = "0px";bg.style.left = "0px";bg.style.width = document.body.scrollWidth;bg.style.height = document.body.scrollHeight;
        bg.style.background = "#ccccdd"; bg.style.opacity = "0.9"
        var dv = document.createElement("div"); dv.id = 'modalBox'; dv.style.position = "absolute"; dv.style.overflow = "auto"; dv.style.top = (parseInt(window.pageYOffse?twindow.pageYOffset:document.body.scrollTop) + 20) + "px"; dv.style.left = ((parseInt(document.body.clientWidth) - boxW) / 2) + "px"; dv.style.width = boxW + "px"; dv.style.height = boxH + "px";
        dv.style.verticalAlign = "middle"; dv.style.visibility = "visible"; dv.style.display = ""; dv.style.border = "#3333ff 3px double"; dv.style.background = "#ffffff"; dv.style.fontWeight = "bold";dv.style.boxShadow = "4px 4px 12px 6px #888888"; dv.style.opacity = "1.0"
        var bClose = document.createElement("span"); bClose.style.position = "absolute"; bClose.style.top = dv.style.top; bClose.style.left = dv.style.left;bClose.style.padding = "3px"; bClose.style.cursor="pointer";bClose.style.border = "solid 1px #9999dd"; bClose.innerHTML = "X" //bClose.style.width = ; bg.style.height =
        addEvent(bClose, function() { removeByID("modalBG"); }, "click");
        bg.appendChild(dv);bg.appendChild(bClose); document.body.appendChild(bg);//document.body.scrollIntoView(true);
    }
}

function addEvent(Obj, eHandler, type) {
    if (Obj.addEventListener) {//W3C, FF: event , handler and True/False=Capture/Bubble
        if (type == "click") type = "click";
        Obj.addEventListener(type, eHandler, false); //Obj.addEventListener('click',eHandler2, false); // more
    } else if (Obj.attachEvent) {// IE
        if (type == "click") type = "onclick";
        Obj.attachEvent(type, eHandler); //Obj.attachEvent('onclick', eHandler2); // more
    }
}
function setNormal(Obj, oriText, classText) {if (Obj.value == oriText) {Obj.value = ''; Obj.className = classText;} }
function setBlur(Obj, oriText, classText) {if (Obj.value == '') {Obj.value = oriText; Obj.className = classText;} }

function gradDivsOpacity(fromColor, toColor, stepNum, stepHeight, widthVal) {
    if (fromColor) { } else {fromColor = "#000000"}
    if (toColor) { } else {toColor = "#FFFFFF"}
    if (widthVal) { } else {widthVal = "100%"}
    if (stepNum > 100) {stepNum = 100}
    var oStep = parseInt(100 / stepNum); var oStepUp=0;
    var chunkDiv = document.createElement("div"); chunkDiv.style.position = "relative"; chunkDiv.style.left = "0px"; chunkDiv.style.top = "0px";

    for (i = 0; i <= stepNum; i++) {
        var tpDiv = document.createElement("div"); tpDiv.style.backgroundColor = "rgb(30,30,30)"; tpDiv.style.height = stepHeight + "px"; tpDiv.style.position = "";
        oStepUp += parseInt(oStep); setOpacity(parseInt(100-oStepUp), tpDiv);
        chunkDiv.appendChild(tpDiv);
    }
    return chunkDiv;
}

function showTooltip(obj) {
    if (dg("tooltipBox")) { dg("tootipBox".innerHTML) } else {
        var tooltipBox = document.createElement("div"); tooltipBox.id = "tooltipBox"; tooltipBox.style.position = "absolute"; tooltipBox.style.left = "0px"; tooltipBox.style.top = "0px"; tooltipBox.style.overflow = "auto";
        tooltipBox.style.backgroundColor = "#ffff88"; tooltipBox.style.color = "#222222"; tooltipBox.innerHTML = obj.firstChild.innerHTML;
        tooltipBox.style.width = "300px"; //tooltipBox.style.height = "32px";
        var posX = (parseInt(GlobalMouseX) + 10)
        if ((parseInt(tooltipBox.style.width) + parseInt(GlobalMouseX) + 10) > parseInt(document.body.clientWidth)) { posX = parseInt(GlobalMouseX) - parseInt(tooltipBox.style.width) }
        tooltipBox.style.left = posX + "px"; tooltipBox.style.top = GlobalMouseY;
        tooltipBox.style.visibility = "visible"; tooltipBox.style.display = "";
        obj.appendChild(tooltipBox);
    }
}

function hideID(ObjID) {if (dg(ObjID)) { dg(ObjID).style.visibility = "hidden"; dg(ObjID).style.display = "none"; }}
function showID(ObjID, xOffset, yOffset) {
    if (dg(ObjID)) {
        var Obj = dg(ObjID);
        //xOffset = parseInt(xOffset); yOffset = parseInt(yOffset);alert(xOffset+ "|" + yOffset);
        if (xOffset == "center") {
            Obj.style.left = (parseInt(document.body.clientWidth) - parseInt(Obj.style.width)) / 2
            if (parseInt(Obj.style.left) < 0) Obj.style.left = "0px";
        } else { if (!isNaN(xOffset) && Obj.style.left) { Obj.style.left = (parseInt(Obj.style.left) + parseInt(xOffset)) + "px"; } }

        if (yOffset == "center") {
            Obj.style.top = (parseInt(document.body.clientHeight) - parseInt(Obj.style.height)) / 2
            if (parseInt(Obj.style.top) < 0) Obj.style.top = "0px";
        } else { if (!isNaN(yOffset) && Obj.style.top) { Obj.style.top = (parseInt(Obj.style.top) + parseInt(yOffset)) + "px"; } }
        if (Obj.style.visibility == "hidden") { Obj.style.visibility = "visible"; Obj.style.display = ""; } else { Obj.style.visibility = "hidden"; Obj.style.display = "none"; }
    }
}

function suggestAjax(thisObj, e, callFrom, xOffset, tickTo) { //to attach to any textbox, call this function from its onkeyup event (onkeypress gives weird delay), and name the div containter by using 'con' + the textbox's id, the combo is automatically added with 'cb'
    if (checkKeyPress('Down', e) && dg('cb' + thisObj.id)) {
        dg('cb' + thisObj.id).focus(); dg('cb' + thisObj.id).options[0].selected = true;index0Selected = true;
    } else {
        if (thisObj.value.length > 0) {
            if (checkKeyPress('Esc', e)) { dg('con' + thisObj.id).innerHTML = '' } else { ajaxRequest((callFrom == 'wx' ? '../forum/' : '') + 'wbajax.aspx', '?action=listusers&prefix=' + thisObj.value + '&targetid=' + thisObj.id + '&x=' + xOffset + "&tickTo=" + tickTo, 'con' + thisObj.id, e, false); thisObj.focus(); }
        } else { dg('con' + thisObj.id).innerHTML = ''; }
    }
}
var index0Selected = false;
function suggKeyup(Obj, TargetID, e, tickTo) {
    if (checkKeyPress('Enter', e)) { dg(TargetID).value = Obj.value; dg(TargetID).focus(); Obj.parentNode.innerHTML = ''; suggClick(Obj,tickTo, e); }; //set value to textbox and tick store
    if (checkKeyPress('Esc', e)) { Obj.parentNode.innerHTML = ''; dg(TargetID).focus(); };
    if (checkKeyPress('Down', e)) {index0Selected = false; }
    if (checkKeyPress('Up', e)) {
        if (Obj.selectedIndex == 0 && index0Selected == false) { index0Selected = true; }
        else if (Obj.selectedIndex == 0 && index0Selected == true) { dg(TargetID).focus(); index0Selected = false; }
        else{ }
    }
}
function suggClick(Obj, TargetID, e) {
    var ObjID = Obj.value;
    dg(TargetID).value = ObjID; dg(TargetID).focus();
    if (dg("store" + TargetID)) {
        var spUser = document.createElement("span"); spUser.id = ObjID; spUser.className = "userTick"; spUser.style.cursor = "pointer"; spUser.style.margin = "2px"; spUser.style.padding = "2px"; //spUser.style.marginRight = "3px";
        spUser.title = "remove"; spUser.style.visibility = "visible"; spUser.style.display = ""; spUser.innerHTML = ObjID;
        if (spUser.addEventListener) {//W3C, FF
            spUser.addEventListener("click", function() { removeByID(ObjID); }, false);
        } else if (spUser.attachEvent) {// IE
        spUser.attachEvent("onclick", function() { removeByID(ObjID); });
    }
    if (dg(ObjID)) { alert("This user has already been added: " + ObjID); }
    else { dg("store" + TargetID).appendChild(spUser); }
    dg(TargetID).value = ""; Obj.parentNode.innerHTML = '';
    }
}

function addTick(targetID, addVal) {
    if (dg(targetID) && addVal != '') {
        var spVal = document.createElement("span"); spVal.id = "gid" + addVal; spVal.className = "userTick"; spVal.style.cursor = "pointer"; spVal.style.margin = "2px"; spVal.style.padding = "2px";
        spVal.title = "remove"; spVal.style.visibility = "visible"; spVal.style.display = ""; spVal.innerHTML = addVal;
        if (spVal.addEventListener) {//W3C, FF
            spVal.addEventListener("click", function() { removeByID("gid" + addVal); }, false);
        } else if (spVal.attachEvent) {// IE
            spVal.attachEvent("onclick", function() { removeByID("gid" + addVal); });
        }
        if (dg("gid" + addVal)) { alert("This value has already been added: " + addVal); }
        else { dg(targetID).appendChild(spVal); }
    } else {
        dg(targetID).innerHTML = '';
    }
}

function removeByID(ObjID) {if (ObjID) { dg(ObjID).parentNode.removeChild(dg(ObjID)); } }

function checkKeyPress(checkKey, e) {
    var keyPressed = (window.event) ? event.keyCode : e.keyCode; // MSIE or Firefox?
    var EscKC = (window.event) ? 27 : e.DOM_VK_ESCAPE; // MSIE : Firefox
    var EnterKC = (window.event) ? 13 : e.DOM_VK_RETURN;
    //var DownKC = 40; //e.DOM_VK_DOWN alert('D checked: '+ checkKey + "|" + keyPressed);

    if (checkKey == 'Esc' && keyPressed == EscKC) { return true }
    if (checkKey == 'Enter' && keyPressed == EnterKC) { return true }
    if (checkKey == 'Down' && keyPressed == 40) {index0Selected = false; return true }
    if (checkKey == 'Up' && keyPressed == 38) {return true }
}

function getText(oriText) {
    return oriText.replace(/<\/?[^>]+>/gi, '').replace(/{\/?[^}]+}/gi, '').replace(/ {2,}/g, ' ').replace(/\s/, ":").replace(/^\S*$/, ";")
}

function ajaxRequest(url, Parameters, targetID, e, toNew, callerID) {
    if (targetID.indexOf('[silent]') == 0) { } else { ShowAjax("", "<span><img src=\"../graphics/ajaxwait2.gif\" style=\"vertical-align:middle;\" />Executing...</span>"); }
    var httpRq = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        httpRq = new XMLHttpRequest();
        if (httpRq.overrideMimeType) { httpRq.overrideMimeType('text/xml'); }
    } else {
        if (window.ActiveXObject) { // IE
            try { httpRq = new ActiveXObject("Msxml2.XMLHTTP"); }
            catch (e) {
                try { httpRq = new ActiveXObject("Microsoft.XMLHTTP"); }
                catch (e) { HideAjax();return false;}
            }
        }
    }
    //custom block
    if (Parameters) {}
    //end of custom block
    if (targetID.indexOf('[silent]') == 0) {
        httpRq.onreadystatechange = function() { axReSilence(httpRq, targetID); }
        try { httpRq.open("GET", url + Parameters, true); } catch (e) { return false; }
        try { httpRq.send(null) } catch (e) { return false; }
    } else {
        if (!httpRq) { alert('Cannot create an XMLHTTP instance because you have denied ActiveX/plugin to run.\nWorldX data has not been loaded.'); return false; }
        httpRq.onreadystatechange = function() { axReData(httpRq, targetID, toNew, callerID); }
        httpRq.open("GET", url + Parameters, true);
        try { httpRq.send(null) }
        catch (e) { alert("XMLHTTP request error: " + httpRq.status + ", " + e.description); HideAjax(); return false; }
    }

    //if (Parameters.indexOf('a=PMShow') >= 0) {}
    return true;
    //final custom calls
}
//Put actual data into element
function axReData(httpRq,targetID,toNew,callerID) {
    var tempChunk = '';
    if (httpRq.readyState > 0) { } //LoadStatus.innerHTML = "<br /><img src='./graphics/ajaxwait.gif'> processing..." + ((httpRq.readyState / 4)*100) + "%";}
    if (httpRq.readyState == 4) {
        if (httpRq.status == 200 || httpRq.responseText) {
            var reText = httpRq.responseText;
            if (reText.indexOf('[Refresh]') == 0) { reText = reText.replace('[Refresh]', ""); window.location = reText; } //take priority
            else if (reText.indexOf('[silent]') == 0) { }
            if (targetID) {
                if (targetID.indexOf('[Refresh]') >= 0 && targetID.indexOf('[Refresh]') <= 2) {
                    if (reText == 1 || reText == '') { targetID = targetID.replace('[Refresh]', ""); window.location = targetID; }
                    else { alert("Refresh mode error: " + getText(reText)); } //var tmpDiv = document.createElement("div");tmpDiv.innerHTML = httpRq.responseText;tmpDiv.style.display = ""; tmpDiv.style.visibility="visible"; document.body.appendChild(tmpDiv); if = 1 refresh, must return 1
                } else if (targetID.indexOf('[semiSilent]') >= 0 && targetID.indexOf('[semiSilent]') <= 2) {
                    if (reText == 1 || reText == '') { //be slient
                    } else { alert("Semi-silent error: " + getText(reText)); } //do nothing or return error message, in case of toggling a feature
                } else if ((targetID.indexOf('[Alert Box]') >= 0 && targetID.indexOf('[Alert Box]') <= 2) || (reText.indexOf('[Alert Box]') >= 0 && reText.indexOf('[Alert Box]') <= 2)) {
                    alert(reText.replace('[Alert Box]', 'Server Validation Error: \n'));
                } else {
                    if (dg(targetID)) {
                        dg(targetID).innerHTML = reText; dg(targetID).scrollTop = 0;
                        if (toNew == false) { } else { window.location = '#' + targetID; }
                        //custom calls
                        if (callerID) if (callerID.indexOf('messNode') >= 0) { matchHdr(dg(callerID), dg('showHdr')); };
                    }
                }
            } else { if (toNew == true) { openMe2("", "", "", "", httpRq.responseText, "scrollbars=1, "); } else { alert(httpRq.responseText); } }
        } else { alert('There was a problem with the request. \n Status code:' + httpRq.status + "\n" + httpRq.responseText); HideAjax();return false;}
        HideAjax();return true;
    }
}
function axReSilence(httpRq, targetID) {
    if (httpRq.readyState == 4) {
        if (httpRq.status == 200) {
            var reText = httpRq.responseText;
            if (reText.indexOf('[silent]') == 0 || reText.length < 1) { return true; } //if content irrelevant, no updating to element
            targetID = targetID.replace('[silent]', '');
            if (targetID) {
                if (dg(targetID)) { dg(targetID).innerHTML = reText;}
            }
        } else { return false; }
        return true;
    }
}

function openMe2(url, picUrl, nW, nH, content, optStr) {
    var winHandle, nLeft, nTop;
    if (url != "") {
        if (nW == "") nW = screen.width * .9;
        if (nH == "") nH = screen.height * .9;
        nLeft = (screen.width / 2) - (nW / 2); nTop = (screen.height / 2) - (nH / 2) - 20;
        winHandle = window.open(url, "newPopup", optStr + "width=" + nW + ", " + "height=" + nH + ", left=" + nLeft + ", top=" + nTop + ", screenX=" + nLeft + ", screenY=" + nTop);
    } else {
        if (nW == "" && nH == "") { var tmpPic = new Image; tmpPic.src = picUrl; nW = tmpPic.width; nH = tmpPic.height; }
        if (nW == "") nW = 640;
        if (nH == "") nH = 480;
        nLeft = (screen.width / 2) - (nW / 2); nTop = (screen.height / 2) - (nH / 2) - 20;
        winHandle = window.open(url, "", optStr + "width=" + nW + ", " + "height=" + nH + ", left=" + nLeft + ", top=" + nTop + ", screenX=" + nLeft + ", screenY=" + nTop);
        winHandle.document.write('<html><head></head><body topmargin="0" leftmargin="0"><img src="' + picUrl + '" title="' + content + '" style="cursor:pointer;" onclick="window.close();" /></body></html>');
    }
    if (content !== "") winHandle.document.write('<div>' + content + '</div>');
    winHandle.focus();
}
