﻿var bValidateFirstTimePopup = new Boolean(true);
var oHyperlinkDivBox = null;

/* General Functions... */
function ftnAjaxCreateRequestObject() {
    //The PHP's no-cache header must be present to avoid the caching-related errors...
    var obj;

    if (window.XMLHttpRequest) {
        // Firefox, Safari, Opera...
        obj = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // Internet Explorer 5+...
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        // There is an error creating the object, just as an old browser is being used...
        //alert('Problem creating the XMLHttpRequest object');
    }
    return obj;
}
function ftnAjaxErrorNotifyByEmail(sMsg, sUrl, sLinenumber) {
    // Block sending of any further emails if javascript errors is in infinite loop while web events or browser is running...
    if (q_iAjaxJsErrorEmailCount < 10) {
        var oHttp = ftnAjaxCreateRequestObject();  //Make the XMLHttpRequest object...
        var sDirFolder = "";

        if (q_sAjaxHttpWebFile.indexOf("w1/") != -1) {
            sDirFolder = "w1";
        } else if (q_sAjaxHttpWebFile.indexOf("w2/") != -1) {
            sDirFolder = "w2";
        } else {
            sDirFolder = "w1";  //This is for the Default.aspx webpage that aren't in either "w1" or "w2" directory...
        }
        
        /* 06/12/2006 - Discontinued and is replaced below... */
        /*
        var httpStr = url.substring(0,7);
        if (httpStr == "http://") { httpStr = "http"; } 
        else if (httpStr == "https:/") { httpStr = "https"; } 
        else { httpStr = "http"; }
        */

        /* q_sAjaxHttpPort variable exist from the parent's web page... */
        if (q_sAjaxHttpPort == "80") {
            q_sAjaxHttpPort = "http";
        } else if (q_sAjaxHttpPort == "443") {
            q_sAjaxHttpPort = "https";
        } else {
            q_sAjaxHttpPort = "http";
        }

        /* alert(window.location.protocol);  //Another option to use ports... */

        /* Do the conversion to a code-name and convert it back in php via javascript_error.php... */
        q_sAjaxHttpQueryStr = q_sAjaxHttpQueryStr.replace(/&/g, "**ampersand**");
        q_sAjaxHttpQueryStr = q_sAjaxHttpQueryStr.replace(/=/g, "**equal**");

        /* q_sAjaxHttpHost & httpUrlFile variable exist from the parent's web page... */
        // Open PHP script for requests...
        oHttp.open("get", q_sAjaxHttpPort + "://" + q_sAjaxHttpHost + "/" + sDirFolder + "/javascripterror.aspx?httpWebFile=" + q_sAjaxHttpWebFile + "&httpQueryStr=" + q_sAjaxHttpQueryStr + "&Url=" + sUrl + "&Line=" + sLinenumber + "&Msg=" + sMsg);
        oHttp.send(null);
        q_iAjaxJsErrorEmailCount++;
        oHttp = null;
    }

    return true;
}
function ftnExpandOrCollapseDiv1(sId) {  //This tag doesn't remember every expanded div(s) on every post-back, alternative function is found below...
    //Notice: The ID on all div tag must have unique name for every expanding/collapsing div(s)...
    var oGetElementById1 = document.getElementById(String(sId) + String("1"));
    var oGetElementById2 = document.getElementById(String(sId) + String("2"));
    var oDisplay1 = oGetElementById1.style.display;

    if (oDisplay1 == '') {
        oGetElementById1.style.display = 'block';
    } else if (oDisplay1 == 'block') {
        oGetElementById1.style.display = 'none';
    } else if (oDisplay1 == 'none') {
        oGetElementById1.style.display = 'block';
    }

    if (oGetElementById2.innerHTML == '-') {
        oGetElementById2.innerHTML = '+';
    } else if (oGetElementById2.innerHTML == '+') {
        oGetElementById2.innerHTML = '-';
    }
}
function zzftnExpandOrCollapseDiv2_Discontinued(sId, sIdMask) {  //This tag is needed to remember every expanded div(s) on every post-back...
    //Notice: The ID on all div tag must have unique name for every expanding/collapsing div(s)...
    var oGetElementById1 = document.getElementById(String(sId) + String("1"));
    var oGetElementById2 = document.getElementById(String(sId) + String("2"));
    var oDisplay1 = oGetElementById1.style.display;

    if (oDisplay1 == '') {
        oGetElementById1.style.display = 'block';
    } else if (oDisplay1 == 'block') {
        oGetElementById1.style.display = 'none';
    } else if (oDisplay1 == 'none') {
        oGetElementById1.style.display = 'block';
    }

    if (oGetElementById2.innerHTML == '-') {
        oGetElementById2.innerHTML = '+';
    } else if (oGetElementById2.innerHTML == '+') {
        oGetElementById2.innerHTML = '-';
    }

    ftnExpandOrCollapseDivIdNameCollector(sIdMask);
}
function zzftnExpandOrCollapseDivIdNameCollector_Discontinued(sIdMask) {
    //Notice: The ID on all div tag must have unique name for every expanding/collapsing div(s)...
    var oGetHiddenElementById1 = document.getElementById("lblHiddenExpandOrCollapseDivValue2");
    var oDiv = document.getElementsByTagName("div");
    var x = 0;
    var xLength = oDiv.length;
    var sId = "";
    var oGetElementById1;
    var sHiddenValue = ""

    for (x = 0; x < xLength; x++) {
        sId = sIdMask;
        sId = sId.replace("_", String(x + 1));

        oGetElementById1 = document.getElementById(sId);
        if (oGetElementById1 == null) {
            break;  //No need to slow down the website by letting the loop kept on running when not needed...
        } else {
            if (oGetElementById1.style.display == "block") {
                if (sHiddenValue.length == 0) {
                    sHiddenValue = sId;
                } else {
                    sHiddenValue = sHiddenValue + ":" + sId;
                }
            }
        }
    }

    oGetHiddenElementById1.value = sHiddenValue;
}
function ftnHyperlinkDivBoxOpen(id) {
    //Close old layer...
    if (oHyperlinkDivBox) { oHyperlinkDivBox.style.visibility = 'hidden'; }

    //Get new layer and show it...
    oHyperlinkDivBox = document.getElementById(id);
    oHyperlinkDivBox.style.visibility = 'visible';
}
// close showed layer
function ftnHyperlinkDivBoxClose() {
    if (oHyperlinkDivBox) { oHyperlinkDivBox.style.visibility = 'hidden'; }
}
function ftnKeyPressHandler(sBtnName, e) {
    var sKey;

    if (window.event) {
        sKey = window.event.keyCode;  //IE...
    } else {
        sKey = e.which;  //Firefox...
    }

    //IE --> 13...
    //Safari --> 10...
    if ((sKey == 10) || (sKey == 13)) {
        var oButton = document.getElementById(sBtnName);
        if (oButton != null) {
            //If we find the button, click it...
            oButton.click();
            event.keyCode = 0;
        }
    }
}
function ftnPopupWindow1(sUrl, sName) {
    var url = sUrl;
    var name = sName;
    var toolbar = "no";
    var location = "no";
    var directories = "no";
    var status = "no";
    var menubar = "no";
    //var scrollbars = "no";
    //var resizable = "no";
    //var width = "660";
    //var height = "500";
    //var top = "0";
    //var left = "0";

    //window.open(url, name, "toolbar=" + toolbar + ",location=" + location + ",directories=" + directories + ",status=" + status + ",menubar=" + menubar + ",scrollbars=" + scrollbars + ",resizable=" + resizable + ",width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + "");
    window.open(url, name, "toolbar=" + toolbar + ",location=" + location + ",directories=" + directories + ",status=" + status + ",menubar=" + menubar + "");
}
function ftnPopupWindow2(sUrl) {
    var url = sUrl;
    var name = "NoName";
    var toolbar = "no";
    var location = "no";
    var directories = "no";
    var status = "no";
    var menubar = "no";
    var scrollbars = "no";
    var resizable = "no";
    var width = "0";
    var height = "0";
    var top = "0";
    var left = "0";

    winHandle = window.open(url, name, "toolbar=" + toolbar + ",location=" + location + ",directories=" + directories + ",status=" + status + ",menubar=" + menubar + ",scrollbars=" + scrollbars + ",resizable=" + resizable + ",width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + "");
}
function ftnQueryStringToJsonParser1(sQueryString) {
    //http://msdn.microsoft.com/en-us/library/bb299886.aspx --> Useful guideline/info on JSON and Javascript...

    var aVariables = sQueryString.split("&");
    var iTotalCount = aVariables.length
    var sNewQueryString = "";
    var iIndexOf = 0;
    var sJunkQueryString = "";

    for (var x = 0; x < iTotalCount; x++) {
        if (x != 0) {
            sNewQueryString = sNewQueryString + ",";
        }
        iIndexOf = aVariables[x].indexOf("=", 0);

        sNewQueryString = sNewQueryString + "'" + aVariables[x].substr(0, iIndexOf) + "':'" + aVariables[x].substr((iIndexOf + 1), (aVariables[x].length - (iIndexOf + 1))) + "'";
    }

    return "{" + sNewQueryString + "}";
}
function ftnValidateFormPopupMsg(target, string) {
   // build out the divs, set attributes and call the fade function //
    var msg;
    var msgcontent;

    //09/24/2009 - (KB927917 - Due to IE's design (internally) that kept causing javascript error and prevent webpage from loading in IE.)...
    //           - (This error is "HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)")...
    //           - The bug workaround is to add "<div id="ValidateFormMsg"><div id="ValidateFormMsgContent"></div></div>" to the html code to keep IE happy...

    //Create javascript objects...
    //#if (!(document.getElementById('ValidateFormMsg'))) {
    //#    msg = document.createElement('div');
    //#    msg.id = 'ValidateFormMsg';
    //#    msgcontent = document.createElement('div');
    //#    msgcontent.id = 'ValidateFormMsgContent';
    //#    document.body.appendChild(msg);
    //#    msg.appendChild(msgcontent);
    //#} else {
        msg = document.getElementById('ValidateFormMsg');
        msgcontent = document.getElementById('ValidateFormMsgContent');
    //#}
    //To clear the pop-up message if not needed...
    if ((target.length == 0) && (string.length == 0)) {
        //bValidateFirstTimePopup = true;
        msgcontent.innerHTML = "";
        msg.style.display = 'none';
        return;  //This exit the function...
    }
    msgcontent.innerHTML = string + "<br /><div style=\"padding-top:2px;text-align:center;\"><a href=\"#\" class=\"aValidate1\" onclick=\"ftnValidateFormPopupMsg('', '');\">Close</a></div>";
    msg.style.display = 'block';
    var msgheight = msg.offsetHeight;
    var targetdiv = document.getElementById(target);
    //05/14/2010 - Cannot use this focus() feature cuz we want to point the pop-up div to a 2nd form object (textbox, drop-down selection, etc.) while the 1st form object is "yellow" highlighted as the original error...
    //#targetdiv.focus();
    var targetheight = targetdiv.offsetHeight;
    var targetwidth = targetdiv.offsetWidth;
    var topposition = ftnValidateFormPopupTopPosition(targetdiv) - ((msgheight - targetheight) / 2);
    var leftposition = ftnValidateFormPopupLeftPosition(targetdiv) + targetwidth + 3;
    if (bValidateFirstTimePopup == true) {
        bValidateFirstTimePopup = false;
        leftposition = leftposition + 9;
    }
    msg.style.top = topposition + 'px';
    msg.style.left = leftposition + 'px';
}
function ftnValidateFormPopupLeftPosition(target) {
    // calculate the position of the element in relation to the left of the browser //
    var left = 0;
    if (target.offsetParent) {
        while (1) {
            left += target.offsetLeft;
            if (!target.offsetParent) {
                break;
            }
            target = target.offsetParent;
        }
    } else if (target.x) {
        left += target.x;
    }
    return left;
}
function ftnValidateFormPopupTopPosition(target) {
    // calculate the position of the element in relation to the top of the browser window //
    var top = 0;
    if (target.offsetParent) {
        while (1) {
            top += target.offsetTop;
            if (!target.offsetParent) {
                break;
            }
            target = target.offsetParent;
        }
    } else if (target.y) {
        top += target.y;
    }
    return top;
}
function ftnWebFormManualSubmission(sUrl) {
    var iIndexOf = sUrl.indexOf("?", 0);
    var sPrefix = sUrl.substr(0, iIndexOf);
    var sQueryString = sUrl.substr((iIndexOf + 1), (sUrl.length - (iIndexOf + 1)));

    if (sQueryString.length > 0) {
        sQueryString = ftnQueryStringToJsonParser1(sQueryString);
    }

    //http://msdn.microsoft.com/en-us/library/bb299886.aspx --> Useful guideline/info on JSON and Javascript...
    var aQueryString = eval("(" + sQueryString + ")");

    var oForm = document.createElement("form");  //document.forms[0]; //document.forms[1];
    //oForm2.__VIEWSTATE.name = "NOVIEWSTATE";
    //oForm2.__VIEWSTATE.value = "";
    oForm.method = "post";
    oForm.action = sPrefix;
    for (var k in aQueryString) {
        var oInput = document.createElement("input");
        oInput.setAttribute("name", k);
        oInput.setAttribute("value", aQueryString[k]);
        oForm.appendChild(oInput);
    }
    document.body.appendChild(oForm);
    oForm.submit();
    document.body.removeChild(oForm);
}
