﻿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';
    }
}