﻿var IE8 = Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version >= 8;
var SAFARI = (navigator.userAgent.indexOf('WebKit/') > -1);

// To trim a string
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.isNullOrEmpty = function(value) {
    if (value) {
        if (typeof (value) == 'string') {
            if (value.length > 0)
                return false;
        }
    }
    return true;
}

function getWindowWidth() {
    var windowWidth = 0;
    if (self.innerWidth)
        windowWidth = self.innerWidth;
    else if (document.documentElement && document.documentElement.clientWidth)
        windowWidth = document.documentElement.clientWidth;
    else if (document.body)
        windowWidth = document.body.clientWidth;
    return windowWidth;
}

function getWindowHeight() {
    var windowHeight = 0;
    if (self.innerHeight)
        windowHeight = self.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight)
        windowHeight = document.documentElement.clientHeight;
    else if (document.body)
        windowHeight = document.body.clientHeight;
    return windowHeight;
}

function scrollTop() {
    return filterResults(
        window.pageYOffset ? window.pageYOffset : 0,
        document.documentElement ? document.documentElement.scrollTop : 0,
        document.body ? document.body.scrollTop : 0
    );
}

function filterResults(currentWindow, currentDocumentElement, currentBody) {
    var result = currentWindow ? currentWindow : 0;
    if (currentDocumentElement && (!result || (result > currentDocumentElement)))
        result = currentDocumentElement;
    return currentBody && (!result || (result > currentBody)) ? currentBody : result;
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (obj.nodeName === "TABLE")
            break;
        obj = obj.offsetParent;
        if (!obj.offsetParent)
            break;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (obj.nodeName === "TABLE")
            break;
        obj = obj.offsetParent;
        if (!obj.offsetParent)
            break;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}


function ShowLoader() {
    var loaderCtl = document.getElementById('divShowCaseProgress');
    var ANIMATION_WIDTH = 128;
    var ANIMATION_HEIGHT = 16;
    if (loaderCtl != null) {
        loaderCtl.style.top = scrollTop() + (getWindowHeight() / 2) - (ANIMATION_HEIGHT / 2) + "px";
        loaderCtl.style.left = (getWindowWidth() / 2) - (ANIMATION_WIDTH / 2) + "px";
        loaderCtl.style.display = 'block';
        if (IE8) {
            loaderCtl.style.visibility = 'visible';
        }
    }
}

function HideLoader() {
    var loaderCtl = document.getElementById('divShowCaseProgress');
    if (loaderCtl != null) {
        loaderCtl.style.display = 'none';
        if (IE8) {
            loaderCtl.style.visibility = 'hidden';
        }
    }
}

/* Show the progress bar inline/generic based on the layout. Horizontal - inline, Verical - generic */
function ShowSearchControlLoader(isHorizontal) {
    
    // JMH: Originally this method compared a string to a boolean,
    // so the HideLoader() method was always called. You need to ensure
    // that strings are converted to booleans when comparing values.
    var flag = new Boolean(isHorizontal);
    
    //For vertical layout use the Generic loader
    if (flag == true) {
        var countrySelectorCtl = document.getElementById('countrySelection');
        if (countrySelectorCtl != null) {
            countrySelectorCtl.style.display = 'none';
        }

        var loaderCtl = document.getElementById('progress');
        if (loaderCtl != null) {
            loaderCtl.style.display = 'block';
            if (IE8) {
                loaderCtl.style.visibility = 'visible';
            }
        }
    }
    else {
        ShowLoader();
    }
}

/* Hide the progress bar inline/generic based on the layout. Horizontal - inline, Verical - generic */
function HideSearchControlLoader(isHorizontal) {

    // JMH: Originally this method compared a string to a boolean,
    // so the HideLoader() method was always called. You need to ensure
    // that strings are converted to booleans when comparing values.
    var flag = new Boolean(isHorizontal);
    
    //For vertical layout use the Generic loader
    if (flag == true) {
        var countrySelectorCtl = document.getElementById('countrySelection');
        if (countrySelectorCtl != null) {
            countrySelectorCtl.style.display = 'block';
        }

        var loaderCtl = document.getElementById('progress');
        if (loaderCtl != null) {
            loaderCtl.style.display = 'none';
            if (IE8) {
                loaderCtl.style.visibility = 'hidden';
            }
        }
    }
    else {
        HideLoader();
    }
}

function CheckBrowser(strmsg) {
    if (!(Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version >= 6)) {
        alert(strmsg);
        return false;
    }
    else {
        return true;
    }
}
function RemoveBackground(elementId) {
    var element = $get(elementId);
    if (element !== null) {
        element.style.background = 'none';
    }
}


function toggleVisibility(element, isVisible) {
    if (element !== null) {
        var className = element.className;
        if (!String.isNullOrEmpty(className)) {
            // TODO: Add code to append or remove the correct css class name.
        }
        element.className = (isVisible) ? 'visible' : 'collapsed';
    }
}

function LinkToMe_LinkClick(link, contentContainerId, valueAttributeName) {
    if (link === null || contentContainerId == null || contentContainerId === '') {
        return;
    }

    var contentContainer = $get(contentContainerId);
    if (contentContainer === null) {
        return;
    }

    var target = null;
    if (valueAttributeName !== null || valueAttributeName !== '') {
        var attr = link.attributes.getNamedItem(valueAttributeName);
        if (attr !== null) {
            target = $get(attr.nodeValue);
        }
    }

    contentContainer.style.display = 'block';
    if (target !== null) {
        target.select();
    }
    setPageHeight(null);
    return false;    
}

/*Returns the Height and width of Page and window based on browser*/
function getPageSize() {
            var xScroll, yScroll;

            if (window.innerHeight && window.scrollMaxY) {
                xScroll = document.body.scrollWidth;
                yScroll = window.innerHeight + window.scrollMaxY;
            } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
                xScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
            } else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight) { // Explorer 6 strict mode
                xScroll = document.documentElement.scrollWidth;
                yScroll = document.documentElement.scrollHeight;
            } else { // Explorer Mac...would also work in Mozilla and Safari
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
            }

            var windowWidth, windowHeight;
            if (self.innerHeight) {	// all except Explorer
                windowWidth = self.innerWidth;
                windowHeight = self.innerHeight;
            } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
            } else if (document.body) { // other Explorers
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
            }

            // for small pages with total height less then height of the viewport
            if (yScroll < windowHeight) {
                pageHeight = windowHeight;
            } else {
                pageHeight = yScroll;
            }

            // for small pages with total width less then width of the viewport
            if (xScroll < windowWidth) {
                pageWidth = windowWidth;
            } else {
                pageWidth = xScroll;
            }


            arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
            return arrayPageSize;
        }
function getPageScroll() {
            var xScroll, yScroll;
            if (self.pageYOffset) {
                yScroll = self.pageYOffset;
                xScroll = self.pageXOffset;
            } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
                yScroll = document.documentElement.scrollTop;
                xScroll = document.documentElement.scrollLeft;
            } else if (document.body) {// all other Explorers
                yScroll = document.body.scrollTop;
                xScroll = document.body.scrollLeft;
            }
            arrayPageScroll = new Array(xScroll, yScroll);
            return arrayPageScroll;
        }
