﻿
// General Functions
function jsPageRedirect(url) {
    window.location = url;
}
function jsShowPopup(element, url, cssWidth, cssHeight, posLeft, posTop) {
    element.SetContentUrl(url);
    element.ShowAtPos(posLeft, posTop);
    var scrlBoxSize = jsGetScrollBoxSize();
    var setWidth = (parseInt(cssWidth, 10) + parseInt(scrlBoxSize, 10));
    var setHeight = (parseInt(cssHeight, 10) + parseInt(scrlBoxSize, 10));
    element.SetSize(setWidth, setHeight);
}
function jsShowPdfPopup(key, width, height) {
    var uniqueStr = jsGetUniqueString();
    var win = 'undefined';
    var url = 'ShowPdfPage.aspx?obj=' + key + '&ustr=' + uniqueStr;
    var windowName = key;
    var params = 'toolbar=0,'
            + 'location=0,'
            + 'directories=0,'
            + 'status=1,'
            + 'menubar=0,'
            + 'titlebar=1,'
            + 'scrollbars=1,'
            + 'resizable=1,'
            + 'top=50,'
            + 'left=50,'
            + 'width=' + width + ','
            + 'height=' + height;
    win = window.open(url, windowName, params);
}
function jsShowNewWindow(name, url, width, height) {
    var params = 'toolbar=1,'
            + 'location=1,'
            + 'directories=1,'
            + 'status=1,'
            + 'menubar=1,'
            + 'titlebar=1,'
            + 'scrollbars=1,'
            + 'resizable=1,'
            + 'top=50,'
            + 'left=50,'
            + 'width=' + width + ','
            + 'height=' + height;
    var windowName = name;
    win = window.open(url, windowName, params);
}
function jsSwitchWatermark(item, watermark) {
    if (item.focused && item.GetValue != null && item.GetText() == watermark)
        item.SetText(null);
    else if (!item.focused && (item.GetValue == null || item.GetText() == ""))
        item.SetText(watermark);
    var color;
    if (item.GetText() == watermark)
        color = "gray";
    else
        color = "black";
    item.GetInputElement().style.color = color;
    return;
}
function jsGetUniqueString() {
    var currDateTime = new Date();
    return currDateTime.getFullYear().toString()
        + jsGetLeftPad(currDateTime.getMonth(), 2, '0')
        + jsGetLeftPad(currDateTime.getDay(), 2, '0')
        + jsGetLeftPad(currDateTime.getHours(), 2, '0')
        + jsGetLeftPad(currDateTime.getMinutes(), 2, '0')
        + jsGetLeftPad(currDateTime.getSeconds(), 2, '0')
        + jsGetLeftPad(currDateTime.getMilliseconds(), 3, '0');
}
function jsGetLeftPad(text, length, padchar) {
    while (text.length < length) {
        text = padchar + text;
    }
    return text;
}
function jsSetPopUpMinMaxWidth(element, minWidth, maxWidth) {
    var popWidth = element.GetWidth();
    if (parseInt(popWidth, 10) < parseInt(minWidth, 10)) {
        element.SetSize(parseInt(minWidth, 10), 0);
    }
    else if (parseInt(popWidth, 10) > parseInt(maxWidth, 10)) {
        element.SetSize(parseInt(maxWidth, 10), 0);
    }
}
// For easy handling, I separated the Pop-up ralated variables as a separate object. And I passed this object as a ref-type
// reference to the sub methods.
function jsBrowserDimensionObject() {
    this.biWidth = 0;           // Browser inner width
    this.biHeight = 0;          // Browser inner height
    this.bsWidth = 0;           // Browser scrolled width
    this.bsHeight = 0;          // Browser scrolled height
}

function jsPopupObject() {
    this.oWidth = 0;            // Width of the object pointed by mouse.
    this.oHeight = 0;           // Height of the object pointed by mouse.
    this.oLeft = 0;             // Current left position of the object pointed by mouse.
    this.oTop = 0;              // Current top position of the object pointed by mouse.
    this.pUpX = 0;              // Display X co-ordinate of Pop-up
    this.pUpY = 0;              // Display Y co-ordinate of Pop-up
    this.pUpW = 0;              // Pop-up display width. It is dynamic value. It dependns on the content
    this.pUpH = 0;              // Pop-up display height. It is dynamic value. It dependns on the content
    this.pUnused = 10;          // There are some unused spaces appear between Pop-up outer edges and browser inner edges
}
// This method displays the popu-up.
// The display position of Pop-up is depending on the object pointed by mouse.
// Note:- The position NOT depending on the mouse pointer. Because, if we display the pop-up in current mouse pointer
// position, the function will be calling many time (i.e.) in each and every mouse-move on the object.
// It will slow down the performance.
function jsShowPopupAtObject(element, obj) {
    jsShowElementAtObject(element, obj, "PopUp");
}
// This method displays the popu-up.
// The display position of Pop-up is depending on the object pointed by mouse.
function jsShowMenuAtObject(element, obj) {
    jsShowElementAtObject(element, obj, "Menu");
}
function jsShowElementAtObject(element, obj, elementType) {
    var jsBNVObj = new jsBrowserNameVersionObject();    // Initilizing object
    var jsBDObj = new jsBrowserDimensionObject();   // Initilizing object
    var jsPPObj = new jsPopupObject();  // Initilizing object

    if (!jsGetBrowserNameVersion(jsBNVObj))
        alert("Error! The function 'jsGetBrowserNameVersion(JsBNVObj)' doesn't support for this browser.");
    else if (!jsGetBrowserInnerDimensions(jsBDObj, jsBNVObj))
        alert("Error! The function 'jsGetBrowserInnerDimensions(JsBDObj, JsBNVObj)' doesn't support for this browser.");
    else if (!jsGetBrowserScrolledDimensions(jsBDObj, jsBNVObj))
        alert("Error! The function 'jsGetBrowserScrolledDimensions(JsBDObj, JsBNVObj)' doesn't support for this browser.");
    else if (!jsGetObjectDimensions(jsPPObj, obj))
        alert("Error! The function 'jsGetObjectDimensions(JsTTObj, Obj))' dosen't support for this browser.");
    else if (!jsGetObjectPosition(jsPPObj, obj))
        alert("Error! The function 'jsGetObjectPosition(JsTTObj, Obj))' dosen't support for this browser.");
    else {
        jsPPObj.pUpX = parseInt(jsPPObj.oLeft, 10); // This will display in left positions
        jsPPObj.pUpY = parseInt(jsPPObj.oTop, 10) + parseInt(jsPPObj.oHeight);   // This will disply in bottom position

        // Show the pop-pu at correct position --> starts
        if (elementType.toString().toLowerCase() == "popup") {

            // Get pop-up dimensions --> starts
            // To get the dynamic width and height of the Pop-up (tool tip), we need to show pop-up with full data and hide it immediately.
            // If we not get the dimension, we can't specify the exact location for the last row. (i.e.) the space between last row and the browser may be less than pop-up hegiht. Simillarly, the space between right most column and the browser may be less than pop-up width. At this situation, by default pop-up will display a default position. To change that position, we need this steps. (Note:- This araises not only at last row/column, somtimes it may be ariaise after middle row/column).
            element.ShowAtPos(0, 0);
            jsPPObj.pUpW = element.GetWidth();
            jsPPObj.pUpH = element.GetHeight();
            element.Hide();
            // Get pop-up dimensions --> ends

            // Deciding where we need to display (at left or at right) --> starts
            // By our code, the default position is left
            var rightFreeSpace = (parseInt(jsBDObj.biWidth, 10) + parseInt(jsBDObj.bsWidth, 10)) - (parseInt(jsPPObj.oLeft, 10) + parseInt(jsPPObj.pUnused, 10));
            if (parseInt(rightFreeSpace, 10) < parseInt(jsPPObj.pUpW, 10)) {
                jsPPObj.pUpX = parseInt(jsPPObj.pUpX, 10) - parseInt(jsPPObj.pUpW, 10) + parseInt(rightFreeSpace, 10);
            }
            // Deciding where we need to display (at left or at right) --> ends

            // Deciding where we need to display (at bottom or at top) --> starts
            // By our code, the default position  is bottom
            var bottmFreeSpace = (parseInt(jsBDObj.biHeight, 10) + parseInt(jsBDObj.bsHeight, 10)) - (parseInt(jsPPObj.oTop, 10) + parseInt(jsPPObj.oHeight, 10) + parseInt(jsPPObj.pUnused, 10));
            if (parseInt(bottmFreeSpace, 10) < parseInt(jsPPObj.pUpH, 10)) {
                jsPPObj.pUpY = parseInt(jsPPObj.pUpY, 10) - (parseInt(jsPPObj.oHeight, 10) + parseInt(jsPPObj.pUpH, 10));
            }
            // Deciding where we need to display (at bottom or at top) --> ends
        }
        element.ShowAtPos(jsPPObj.pUpX, jsPPObj.pUpY);
        // Show the pop-pu at correct position --> ends
    }
}
function jsBrowserNameVersionObject() {
    this.BrowserName = '';
    this.FullVersion = 0.0;
    this.MajorVersion = 0.0;
}
function jsGetBrowserNameVersion(JsBNVObj) {
    var nVer = navigator.appVersion;
    var nAgt = navigator.userAgent;

    var nameOffset, verOffset, ix;

    JsBNVObj.BrowserName = navigator.appName;
    JsBNVObj.FullVersion = '' + parseFloat(navigator.appVersion);
    JsBNVObj.MajorVersion = parseInt(navigator.appVersion, 10);

    // In MSIE, the true version is after "MSIE" in userAgent
    if ((verOffset = nAgt.indexOf("MSIE")) != -1) {
        JsBNVObj.BrowserName = "Microsoft Internet Explorer";
        JsBNVObj.FullVersion = nAgt.substring(verOffset + 5);
    }
    // In Opera, the true version is after "Opera" 
    else if ((verOffset = nAgt.indexOf("Opera")) != -1) {
        JsBNVObj.BrowserName = "Opera";
        JsBNVObj.FullVersion = nAgt.substring(verOffset + 6);
    }
    // In Chrome, the true version is after "Chrome" 
    else if ((verOffset = nAgt.indexOf("Chrome")) != -1) {
        JsBNVObj.BrowserName = "Chrome";
        JsBNVObj.FullVersion = nAgt.substring(verOffset + 7);
    }
    // In Safari, the true version is after "Safari" 
    else if ((verOffset = nAgt.indexOf("Safari")) != -1) {
        JsBNVObj.BrowserName = "Safari";
        JsBNVObj.FullVersion = nAgt.substring(verOffset + 7);
    }
    // In Firefox, the true version is after "Firefox" 
    else if ((verOffset = nAgt.indexOf("Firefox")) != -1) {
        JsBNVObj.BrowserName = "Firefox";
        JsBNVObj.FullVersion = nAgt.substring(verOffset + 8);
    }
    // In most other browsers, "name/version" is at the end of userAgent 
    else if ((nameOffset = nAgt.lastIndexOf(' ') + 1) < (verOffset = nAgt.lastIndexOf('/'))) {
        JsBNVObj.BrowserName = nAgt.substring(nameOffset, verOffset);
        JsBNVObj.FullVersion = nAgt.substring(verOffset + 1);
        if (JsBNVObj.BrowserName.toLowerCase() == JsBNVObj.BrowserName.toUpperCase()) {
            JsBNVObj.BrowserName = navigator.appName;
        }
    }
    // trim the JsBNVObj.FullVersion string at semicolon/space if present
    if ((ix = JsBNVObj.FullVersion.indexOf(";")) != -1) JsBNVObj.FullVersion = JsBNVObj.FullVersion.substring(0, ix);
    if ((ix = JsBNVObj.FullVersion.indexOf(" ")) != -1) JsBNVObj.FullVersion = JsBNVObj.FullVersion.substring(0, ix);

    JsBNVObj.MajorVersion = parseInt('' + JsBNVObj.FullVersion, 10);
    if (isNaN(JsBNVObj.MajorVersion)) {
        JsBNVObj.FullVersion = '' + parseFloat(navigator.appVersion);
        JsBNVObj.MajorVersion = parseInt(navigator.appVersion, 10);
    }
    //    alert('navigator.appName = ' + navigator.appName);
    //    alert('navigator.userAgent = ' + navigator.userAgent);
    return true;
}
function jsGetScrollBoxSize() {
    var retAns = 25;
    var jsBNVObj = new jsBrowserNameVersionObject();
    if (jsGetBrowserNameVersion(jsBNVObj)) {
        if (jsBNVObj.BrowserName.toLowerCase() == 'microsoft internet explorer') {
            if ((parseFloat(jsBNVObj.MajorVersion) == 8) && (parseFloat(jsBNVObj.FullVersion) == 8.0)) {
                retAns = 20;
            }
        }
    }
    return retAns;
}
// To get dimensions for the browser used by user
// JsBDObj --> jsBrowserDimensionObject
// JsBNVObj --> jsBrowserNameVersionObject
function jsGetBrowserInnerDimensions(JsBDObj, JsBNVObj) {
    if (
        (JsBNVObj.BrowserName.toLowerCase() == 'microsoft internet explorer') ||
        (JsBNVObj.BrowserName.toLowerCase() == 'firefox') ||
        (JsBNVObj.BrowserName.toLowerCase() == 'chrome') ||
        (JsBNVObj.BrowserName.toLowerCase() == 'safari') ||
        (JsBNVObj.BrowserName.toLowerCase() == 'opera')
       ) {
        JsBDObj.biWidth = document.documentElement.offsetWidth;
        JsBDObj.biHeight = document.documentElement.offsetHeight;
        return true;
    }
    if ((document.documentElement.offsetWidth != 'undefined') && (document.documentElement.offsetHeight != 'undefined')) {
        JsBDObj.biWidth = document.documentElement.offsetWidth;
        JsBDObj.biHeight = document.documentElement.offsetHeight;
        return true;
    }
    if ((self.innerWidth != 'undefined') && (self.innerHeight != 'undefined')) {
        JsBDObj.biWidth = self.innerWidth;
        JsBDObj.biHeight = self.innerHeight;
        return true;
    }
    if ((document.body.clientWidth != 'undefined') && (document.body.clientHeight != 'undefined')) {
        JsBDObj.biWidth = document.body.clientWidth;
        JsBDObj.biHeight = document.body.clientHeight;
        return true;
    }
    return false;
}
// To get scrolled dimensions
// JsBDObj --> jsBrowserDimensionObject
// JsBNVObj --> jsBrowserNameVersionObject
function jsGetBrowserScrolledDimensions(JsBDObj, JsBNVObj) {
    if (
        (JsBNVObj.BrowserName.toLowerCase() == 'microsoft internet explorer') ||
        (JsBNVObj.BrowserName.toLowerCase() == 'firefox') ||
        (JsBNVObj.BrowserName.toLowerCase() == 'opera')
       ) {
        JsBDObj.bsWidth = document.documentElement.scrollLeft;
        JsBDObj.bsHeight = document.documentElement.scrollTop;
        return true;
    }
    if (
        (JsBNVObj.BrowserName.toLowerCase() == 'chrome') ||
        (JsBNVObj.BrowserName.toLowerCase() == 'safari')
       ) {
        JsBDObj.bsWidth = document.body.scrollLeft;
        JsBDObj.bsHeight = document.body.scrollTop;
        return true;
    }
    if (document.body) {
        JsBDObj.bsWidth = document.body.scrollLeft;
        JsBDObj.bsHeight = document.body.scrollTop;
        return true;
    }
    if (document.documentElement) {
        JsBDObj.bsWidth = document.documentElement.scrollLeft;
        JsBDObj.bsHeight = document.documentElement.scrollTop;
        return true;
    }
    if ((window.pageXOffset != 'undefined') && (window.pageYOffset != 'undefined')) {
        JsBDObj.bsWidth = document.pageXOffset;
        JsBDObj.bsHeight = document.pageYOffset;
        return true;
    }
    return false;
}
// To get dimensions for the object pointed by mouse
// JsTTObj --> jsToolTipObject
function jsGetObjectDimensions(JsTTObj, Obj) {
    if ((typeof Obj.offsetWidth === 'undefined')
        || (typeof Obj.offsetHeight === 'undefined'))
        return false;

    JsTTObj.oWidth = Obj.offsetWidth;
    JsTTObj.oHeight = Obj.offsetHeight;
    return true;
}
// To get position of the object
// JsTTObj --> jsToolTipObject
function jsGetObjectPosition(JsTTObj, Obj) {
    if ((typeof Obj.offsetLeft === 'undefined')
        || (typeof Obj.offsetTop === 'undefined'))
        return false;

    var destx = Obj.offsetLeft;
    var desty = Obj.offsetTop;
    var thisNode = Obj;
    while (thisNode.offsetParent &&
    (thisNode.offsetParent != document.body)) {
        thisNode = thisNode.offsetParent;
        destx += thisNode.offsetLeft;
        desty += thisNode.offsetTop;
    }
    JsTTObj.oLeft = destx;
    JsTTObj.oTop = desty;
    return true;
}