﻿/// <reference name="MicrosoftAjax.js" />

var BaseUI =
{
    Initialize: function()
    {
    },
    SetCookie: function(cookieName, cookieValue, nDays) 
    {
        if (nDays != null)
        {
            var today  = new Date()
            var expire = new Date()
            
            if (nDays == null || nDays == 0) nDays = 1
            
            expire.setTime(today.getTime() + 3600000 * 24 * nDays)

            document.cookie = cookieName + "=" + encodeURIComponent(cookieValue) + ";path=/;expires=" + expire.toGMTString()
        }
        else
        {
            document.cookie = cookieName + "=" + encodeURIComponent(cookieValue) + ";path=/"
        }
    },
    GetCookie: function(cookieName)
    {
        var a_all_cookies  = document.cookie.split(';')
        var a_temp_cookie  = ''
        var cookie_name    = ''
        var cookie_value   = ''
        var b_cookie_found = false

        for (i = 0; i < a_all_cookies.length; i++) 
        {
            a_temp_cookie = a_all_cookies[i].split('=')
            cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '')

            if (cookie_name == cookieName) 
            {
                b_cookie_found = true;
                if (a_temp_cookie.length > 1) 
                {
                    cookie_value = decodeURIComponent(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''))
                }
                return cookie_value
                break
            }
            a_temp_cookie = null
            cookie_name = ''
        }

        if (!b_cookie_found) return null
    },
    Expand: function(el) 
    {
        if (el) 
        {
            el.style.visibility = 'visible'
            el.style.display    = 'block'
        }
    },
    Collapse: function(el) 
    {
        if (el) 
        {
            el.style.visibility = 'hidden'
            el.style.display    = 'none'
        }
    },
    Show: function(el) 
    {
        if (el) 
        {
            el.style.visibility = 'visible'
        }
    },
    Hide: function(el) 
    {
        if (el) 
        {
            el.style.visibility = 'hidden'
        }
    },
    IsVisible: function(el)
    {
        if (el) 
        {
            return el.style.visibility.toLowerCase() == 'visible'
        }
        
        return false
    },
    RemoveAllChildren: function(el) 
    {
        while (el.childNodes.length > 0) el.removeChild(el.childNodes[0])
    },
    SetClass: function(el, className)
    {
        if (el)
        {
            if ((Sys.Browser.agent == Sys.Browser.InternetExplorer) && (Sys.Browser.version < 8))
            {
                el.className = className
            }
            else
            {
                el.setAttribute("class", className)
            }
        }
    },
    DaysInMonth: function(date) 
    {
        return 32 - new Date(date.getFullYear(), date.getMonth(), 32).getDate();
    },
    MakeRequest: function(url, verb, data, func, obj)
    {
        var TheRequest = new Sys.Net.WebRequest()

        TheRequest.set_url(url)
        TheRequest.set_httpVerb(verb)

        if (data)
        {
            TheRequest.set_body(data)
            TheRequest.get_headers()["Content-Length"] = data.length;
        }

        TheRequest.set_userContext(new RequestParams(func, obj))
        TheRequest.add_completed(BaseUI.MakeRequestComplete)
        TheRequest.invoke()

        return TheRequest
    },
    MakeRequestComplete: function(executer, eventArgs)
    {
        var TheParams = executer.get_webRequest().get_userContext()

        if (executer.get_responseAvailable())
        {
            TheParams.responseData = executer.get_responseData().replace(/\u2028/g, '')
        }
        else
        {
            TheParams.responseData = null
        }
        if (TheParams.func) TheParams.func(TheParams)
    }
}

function RequestParams(afunc, obj) 
{
    this.func         = afunc
    this.responseData = null
    this.obj          = obj
    this.Error        = null
}

Sys.Application.add_load(BaseUI.Initialize)
