﻿var alphaNumericSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
if (typeof Image == 'undefined')
    Image = function() { return document.createElement('Image'); }

function getTopSpaceForCentering(elem, initialHeight)
{
    if (typeof (elem) == "string")
        elem = document.getElementById(elem);
    if (!initialHeight)
        initialHeight = 0;

    var topSpaceValue = Math.round((getClientHeightValue() - initialHeight - elem.offsetHeight) / 2);
    return topSpaceValue < 0 ? 0 : topSpaceValue;
}

function getLeftSpaceForCentering(elem, initialLeft)
{
    if (typeof (elem) == "string")
        elem = document.getElementById(elem);
    if (!initialLeft)
        initialLeft = 0;

    var leftSpaceValue = Math.round((getClientWidthValue() - elem.offsetWidth) / 2);
    
    return leftSpaceValue < 0 ? 0 : leftSpaceValue;
}

function prepareTdTopSpace(initialHeight)
{
    var wrapperRef = document.getElementById('wrapper');
    var tdTopSpaceRef = document.getElementById('tdTopSpace');
    tdTopSpaceRef.style.height = "0px";
    tdTopSpaceRef.style.height = getTopSpaceForCentering(wrapperRef, initialHeight) + "px";
}

function prepareTdLeftSpace(initialLeft)
{
    var wrapperRef = document.getElementById('wrapper');
    var tdLeftSpaceRef = document.getElementById('tdLeftSpace');
    tdLeftSpaceRef.style.width = "0px";
    tdLeftSpaceRef.style.width = getLeftSpaceForCentering(wrapperRef, initialLeft) + "px";
}

function getClientHeightValue()
{
    var clientHeightValue = 0;

    if (typeof (window.innerWidth) == 'number')
    {
        //Non-IE
        clientHeightValue = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        clientHeightValue = document.documentElement.clientHeight;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        clientHeightValue = document.body.clientHeight;
    }

    return clientHeightValue;
}

function getClientWidthValue()
{
    var clientWidthValue = 0;

    if (typeof (window.innerWidth) == 'number')
    {
        //Non-IE
        clientWidthValue = window.innerWidth;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        clientWidthValue = document.documentElement.clientWidth;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        clientWidthValue = document.body.clientWidth;
    }

    return clientWidthValue;
}

function getElementActualLeft(element)
{
    if (typeof (element) == 'string')
        element = document.getElementById(element);

    var leftPos;
    var parentElement;

    if (element == null)
        return 0;

    leftPos = element.offsetLeft;
    parentElement = element.offsetParent;
    while (parentElement != null)
    {
        leftPos += parentElement.offsetLeft;
        parentElement = parentElement.offsetParent;
    }

    return leftPos;
}

function getElementActualTop(element)
{
    if (typeof (element) == 'string')
        element = document.getElementById(element);

    var topPos;
    var parentElement;

    if (element == null)
        return 0;

    topPos = element.offsetTop;
    parentElement = element.offsetParent;
    while (parentElement != null)
    {
        topPos += parentElement.offsetTop;
        parentElement = parentElement.offsetParent;
    }

    return topPos;
}

function onlyNum(e)
{
    if (!e)
        e = window.event;
    
    var specialKeyKodes = new Array(8, 9, 37, 39, 46);
    
    if (browserRef.isIE)
    {
        for (var i = 0; i < specialKeyKodes.length; i++)
        {
            if (e.keyCode == specialKeyKodes[i])
                return true;
        }
        
        if (e.keyCode >= 48 && e.keyCode <= 57)
            return true;
        else
            return false;
    }
    else
    {
        var str = '0123456789';
        var key1 = e.which;
        var keychar1 = String.fromCharCode(key1);
        
        for (var i = 0; i < specialKeyKodes.length; i++)
        {
            if (key1 == specialKeyKodes[i])
                return true;
        }

        if (str.indexOf(keychar1) == -1)
            return false;
        else
            return true;
    }
}

function onlyNumWithSpace(e)
{
    if (browserRef.isIE)
    {
        if ((window.event.keyCode >= 48) && (window.event.keyCode <= 57) || (window.event.keyCode == 32))
            return true;
        else
            return false;
    }
    else
    {
        var str = '0123456789';
        var key1 = e.which;
        var keychar1 = String.fromCharCode(key1);
        if (key1 == 8 || key1 == 0)
            return true;

        if (str.indexOf(keychar1) == -1)
            return false;
    }
}

function scrollToElement(elementRef)
{
    if (typeof (elementRef) == 'string')
        elementRef = document.getElementById(elementRef);
        
    var selectedPosX = 0;
    var selectedPosY = 0;

    while (elementRef != null)
    {
        selectedPosX += elementRef.offsetLeft;
        selectedPosY += elementRef.offsetTop;
        elementRef = elementRef.offsetParent;
    }

    window.scrollTo(selectedPosX, selectedPosY);
}

function trim(stringToTrim)
{
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function isValidEmail(emailValue)
{
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(trim(emailValue)))
        return true;
    else
        return false;
}

function getScrollCoordinates()
{
    var scrollPoints = new Array();

    if (document.all)
    {
        if (!document.documentElement.scrollLeft)
            scrollPoints[0] = document.body.scrollLeft;
        else
            scrollPoints[0] = document.documentElement.scrollLeft;

        if (!document.documentElement.scrollTop)
            scrollPoints[1] = document.body.scrollTop;
        else
            scrollPoints[1] = document.documentElement.scrollTop;
    }
    else
    {
        scrollPoints[0] = window.pageXOffset;
        scrollPoints[1] = window.pageYOffset;
    }

    return scrollPoints;
}

function getScrollCoordinatesX()
{
    var scrollCoordinatesX;
    if (document.all)
    {
        if (!document.documentElement.scrollLeft)
            scrollCoordinatesX = document.body.scrollLeft;
        else
            scrollCoordinatesX = document.documentElement.scrollLeft;
    }
    else
        scrollCoordinatesX = window.pageXOffset;

    return scrollCoordinatesX;
}

function getScrollCoordinatesY()
{
    var scrollCoordinatesY;

    if (document.all)
    {
        if (!document.documentElement.scrollTop)
            scrollCoordinatesY = document.body.scrollTop;
        else
            scrollCoordinatesY = document.documentElement.scrollTop;
    }
    else
        scrollCoordinatesY = window.pageYOffset;

    return scrollCoordinatesY;
}

function pdControlsOnMouseOver(element, topOffset, leftOffset)
{
    element.style.position = 'relative';
    element.style.top = topOffset;
    element.style.left = leftOffset;
}

function pdControlsOnMouseOut(element)
{
    element.style.position = 'static';
    element.style.top = '0px';
    element.style.left = '0px';
}

function addClickEvent(elem, func)
{
    var oldOnClick = elem.onclick;

    if (typeof elem.onclick != 'function')
    {
        elem.onclick = func;
    }
    else
    {
        elem.onclick = function()
        {
            if (oldOnClick)
                oldOnClick();

            func();
        }
    }
}

function addMouseMoveEvent(elem, func)
{
    var oldOnMouseMove = elem.onmousemove;

    if (typeof elem.onmousemove != 'function')
    {
        elem.onmousemove = func;
    }
    else
    {
        elem.onmousemove = function()
        {
            if (oldOnMouseMove)
                oldOnMouseMove();

            func();
        }
    }
}

function addLoadEvent(func)
{
    var oldonload = window.onload;

    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function()
        {
            if (oldonload)
                oldonload();

            func();
        }
    }
}

function addScrollEvent(func)
{
    var oldOnScroll = window.onscroll;

    if (typeof window.onscroll != 'function')
    {
        window.onscroll = func;
    }
    else
    {
        window.onscroll = function()
        {
            if (oldOnScroll)
                oldOnScroll();

            func();
        }
    }
}

function addResizeEvent(func)
{
    var oldOnResize = window.onresize;

    if (typeof window.onresize != 'function')
    {
        window.onresize = func;
    }
    else
    {
        window.onresize = function()
        {
            if (oldOnResize)
                oldOnResize();

            func();
        }
    }
}

function changeLocation(locationAddress)
{
    window.location = locationAddress;
}

function setCheckBoxAlign(divID)
{
    if (!browserRef.isOpera)
    {
        var divRef = document.getElementById(divID);
        divRef.style.position = 'relative';
        divRef.style.left = '-4px';
    }
}

function getMaxZIndex()
{
    var allElems = document.getElementsByTagName ? document.getElementsByTagName("*") : document.all;
    var maxZIndex = 0;
    for (var i = 0; i < allElems.length; i++)
    {
        var elem = allElems[i];
        var cStyle = null;
        if (elem.currentStyle)
            cStyle = elem.currentStyle;
        else if (document.defaultView && document.defaultView.getComputedStyle)
            cStyle = document.defaultView.getComputedStyle(elem, "");

        var sNum;
        if (cStyle)
            sNum = Number(cStyle.zIndex);
        else
            sNum = Number(elem.style.zIndex);

        if (!isNaN(sNum))
            maxZIndex = Math.max(maxZIndex, sNum);
    }

    return maxZIndex;
}

function getCaretPosition(ctrl)
{
    var caretPos = 0;
    if (document.selection)
    {
        ctrl.focus();
        var sel = document.selection.createRange();
        sel.moveStart('character', -ctrl.value.length);
        caretPos = sel.text.length;
    }
    else if (ctrl.selectionStart || ctrl.selectionStart == '0')
        caretPos = ctrl.selectionStart;
        
    return caretPos;
}

function setCaretPosition(ctrl, pos)
{
    if (ctrl.setSelectionRange)
    {
        ctrl.focus();
        ctrl.setSelectionRange(pos, pos);
    }
    else if (ctrl.createTextRange)
    {
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
}

var isRegisteredChkUpdatePanelScripts = false;
function registerChkUpdatePanelScripts()
{
    if (!isRegisteredChkUpdatePanelScripts)
    {
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(
            function(sender, args)
            {
                args.get_panelsUpdated().each(function(item)
                {
                    if (item.runScriptFunctionAfterUpdated)
                        item.runScriptFunctionAfterUpdated();
                })

                isRegisteredChkUpdatePanelScripts = true;
            });
    }
}
function addFunctionToUpdatePanel(updatePanel, func)
{
    var oldFunction = updatePanel.runScriptFunctionAfterUpdated;

    if (typeof updatePanel.runScriptFunctionAfterUpdated != 'function')
        updatePanel.runScriptFunctionAfterUpdated = func;
    else
    {
        updatePanel.runScriptFunctionAfterUpdated = function()
        {
            if (oldFunction)
                oldFunction();

            func();
        };
    }
}

function clientValidateForDrp(source, argument)
{
    var selectedIndex = document.getElementById(source.controltovalidate).selectedIndex;

    if (selectedIndex == 0)
        argument.IsValid = false;
    else
        argument.IsValid = true;
}

function clientValidateForLabeledTxt(source, argument)
{
    var elementRef = document.getElementById(source.controltovalidate);
    if (trim(elementRef.value) == trim(elementRef.title))
        argument.IsValid = false;
    else
        argument.IsValid = true;
}

function clientValidateForDateControl(source, argument)
{
    //must be bigger then 1800
    if (!/^(0?[1-9]|[12][0-9]|3[01])[\/\.](0?[1-9]|1[0-2])[\/\.](1[89][0-9][0-9]|2[0-9][0-9][0-9])$/.test(argument.Value))
        argument.IsValid = false;
    else
        argument.IsValid = true;
}

function clientValidateForCurrency2Digit(source, argument)
{
    if (!/^(0|[1-9][0-9]*)([,\.][0-9]{1,2})?$/.test(argument.Value))
        argument.IsValid = false;
    else
        argument.IsValid = true;
}

function clientValidateForCurrency4Digit(source, argument)
{
    if (!/^(0|[1-9][0-9]*)([,\.][0-9]{1,4})?$/.test(argument.Value))
        argument.IsValid = false;
    else
        argument.IsValid = true;
}

function controlTxtLength(txtID, spanID, maxCharacterCount)
{
    var txtRef = document.getElementById(txtID);
    var spanRef = document.getElementById(spanID);

    if (txtRef.value.length > maxCharacterCount)
        txtRef.value = txtRef.value.substring(0, maxCharacterCount);

    spanRef.innerHTML = (maxCharacterCount - txtRef.value.length);
}

function labeledTxtGainFocus(element)
{
    if (trim(element.value) == trim(element.title))
        element.value = '';
}

function labeledTxtLostFocus(element)
{
    if (trim(element.value).length == 0)
        element.value = element.title;
}

function isOverElement(e, elem)
{
    if (!e)
        e = window.event;
        
    if (typeof (elem) == "string")
        elem = document.getElementById(elem);

    var clientX, clientY, x1, x2, y1, y2;

    clientX = e.clientX + getScrollCoordinatesX();
    clientY = e.clientY + getScrollCoordinatesY();
    
    x1 = getElementActualLeft(elem);
    x2 = x1 + elem.offsetWidth;
    y1 = getElementActualTop(elem);
    y2 = y1 + elem.offsetHeight;
    
    if (clientX >= x1 && clientX <= x2 && clientY >= y1 && clientY <= y2)
        return true;
    else
        return false;
}

function removeAllChildNodes(elem)
{
    if (typeof (elem) == "string")
        elem = document.getElementById(elem);

    while (elem.hasChildNodes())
        elem.removeChild(elem.lastChild);
}

function runJSOfHref(ancElement)
{
    if (typeof (ancElement) == 'string')
        ancElement = document.getElementById(ancElement);

    if (ancElement.tagName.toLowerCase() != 'a')
        return;
        
    var indexOfStartPosition = ancElement.href.indexOf(':');
    if (indexOfStartPosition != -1)
        eval(ancElement.href.substring(indexOfStartPosition + 1));
}

function setPanelVisibility(divID, isVisible)
{
    $(divID).style.display = isVisible ? 'block' : 'none';
}
