﻿function SetCookie(name, value, expiration)
{
    var exp = expiration;
    //if (exp == 'none') exp = (new Date((new Date()).getYear()+2, 01, 01)).toGMTString();
    if (exp == 'none') exp = 'Wed, 01 Jan 2020 23:59:59 GMT';
    else if (exp == 'session') exp = '';
    if (exp != '') exp = ' expires=' + exp + ';';
    document.cookie = name + '=' + escape(value) + '; ' + exp + '; path=/';
    //alert(name + '=' + escape(value) + ';' + exp);
}

function GetCookie(name, defaultValue)
{
    //alert('cookies='+document.cookie);
    var cookies = document.cookie.split(';');
    for (i=0;i<cookies.length;i++)
    {
        var cpair = cookies[i].split('=');
        if (cpair.length == 2)
        {
            if (TrimStr(cpair[0]) == name)
            {
                return unescape(TrimStr(cpair[1]));
            }
        }
    }
    return defaultValue;
}

function GetChildElement(obj, index)
{
    var i;
    var j = 0;
    for(i = 0; i < obj.childNodes.length; i++)
    {
        var child = obj.childNodes.item(i);
        if (child.tagName)
        {
            if (j == index)
            {
                return child;
            }
            j++;
        }
    }
    return null;
}

function GetChildByTagAndClass(obj, tagName, className)
{
    var child;
    var i;
    for(i = 0; i < obj.childNodes.length; i++)
    {
        child = obj.childNodes.item(i);
        if ((child.tagName==tagName)&&(child.className==className)) return child;
    }
    for(i = 0; i < obj.childNodes.length; i++)
    {
        child = obj.childNodes.item(i);
        var subChild = GetChildByTagAndClass(child, tagName, className);
        if (subChild) return subChild;
    }
    return null;
}

function TrimStr(str)
{
    var i = 0;
    while((i < str.length) && (str.charAt(i) == ' ')) i++;
    var j = str.length - 1;
    while((j >= 0) && (str.charAt(j) == ' ')) j--;
    return str.substr(i,j-i+1);
}

function GetWindowSize()
{
    var res = {width:0,height:0};
    if (typeof(window.innerWidth) == 'number')
    {
        //Non-IE
        res.width = window.innerWidth;
        res.height = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        res.width = document.documentElement.clientWidth;
        res.height = document.documentElement.clientHeight;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        res.width = document.body.clientWidth;
        res.height = document.body.clientHeight;
    }
    return res;
}

function GetWindowScroll()
{
    var res = {left:0,top:0};
    if (typeof(window.pageYOffset) == 'number' )
    {
        //Netscape compliant
        res.left = window.pageXOffset;
        res.top = window.pageYOffset;
    }
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
    {
        //DOM compliant
        res.left = document.body.scrollLeft;
        res.top = document.body.scrollTop;
    }
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
    {
        //IE6 standards compliant mode
        res.left = document.documentElement.scrollLeft;
        res.top = document.documentElement.scrollTop;
    }
    return res;
}
