﻿/// <reference name="MicrosoftAjax.js" />

var BaseUI =
{
    Popup:         null,
    SearchTB:      null,
    SearchBTN:     null,
    Background:    null,
    OnPopupClosed: [],
    WindowLoad:    function()
    {
        BaseUI.Background = document.createElement('div')
        BaseUI.SetClass(BaseUI.Background, 'overlay')
        document.body.appendChild(BaseUI.Background)

        if ((Sys.Browser.agent == Sys.Browser.InternetExplorer) && (Sys.Browser.version == 6))
        {
            BaseUI.Background.style.position = 'absolute'
            BaseUI.Background.style.height   = '100%'
            BaseUI.Background.style.width    = '100%'

            $addHandlers(window, { "resize": BaseUI.WindowResize, 
                                   "scroll": BaseUI.WindowResize }, this)

            BaseUI.WindowResize()
        }
        
        BaseUI.PrintDiv = document.createElement('div')
        BaseUI.PrintDiv.id = 'printDiv'
        document.body.appendChild(BaseUI.PrintDiv)
        
        BaseUI.Popup   = document.getElementById('puPanel')
        BaseUI.Callout = document.getElementById('callout')
        
        if (BaseUI.Popup != null)
        {
            BaseUI.Popup.DataPanel = document.getElementById('puDataPanel')
            BaseUI.Popup.CloseBTN  = document.getElementById('puCloseBTN')
            
            $addHandler(BaseUI.Popup.CloseBTN, 'click', BaseUI.Popup_CloseBTN_Click)
        }
        
        BaseUI.Collapse(BaseUI.Callout)
        BaseUI.Collapse(BaseUI.Popup)
    },
    ShowPopup: function()
    {
        if (Sys.Browser.agent == Sys.Browser.Safari)
        {
            setTimeout('document.body.scrollTop = 0;', 100);
        }
        else
        {
            window.scrollTo(0, 0)
        }
        
        BaseUI.Expand(BaseUI.Background)
        BaseUI.Expand(BaseUI.Popup)
    },
    HidePopup: function()
    {
        BaseUI.Popup_CloseBTN_Click()
    },
    Popup_CloseBTN_Click: function()
    {
        BaseUI.Collapse(BaseUI.Popup)
        BaseUI.Collapse(BaseUI.Background)

        Array.forEach(BaseUI.OnPopupClosed, function(method)
        {
            if (method != null) method()
        })
    },
    WindowResize: function()
    {
        BaseUI.Background.style.height = document.documentElement.clientHeight + document.documentElement.scrollTop  + "px"
        BaseUI.Background.style.width  = document.documentElement.clientWidth  + document.documentElement.scrollLeft + "px"
    },
    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)
            }
        }
    },
    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'
        }
    },
    MakeRequest: function(url, verb, data, func, obj) 
    {
        try 
        {
            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
        }
        catch (error) 
        {
            if (func) func('Communication Error')
        }
    },
    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)
    },
    GetCookie: function(check_name) 
    {
        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 == check_name) 
            {
                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
    },
    SetCookie: function(cookieName, cookieValue, nDays) 
    {
        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) + ";expires=" + expire.toGMTString();
    },
    SelectOptionByValue: function(sel, value)
    {
        for (var i = 0; i < sel.options.length; i++)
        {
            var theOption = sel.options[i]
            
            if (theOption.value == value)
            {
                theOption.selected = true
                break
            }
        }
    },
    AddOption:    function(sel, text, value, tag)
    {
        var opt = document.createElement('option')
        opt.text  = text
        opt.value = value
        opt.tag   = tag
        sel.options[sel.options.length] = opt
    },
    DeleteCookie: function(cookieName) 
    {
        var today  = new Date();
        var expire = new Date();

        expire.setTime(today.getTime() + 3600000 * 24 * -1);
        document.cookie = cookieName + "=" + ";path=/;expires=" + expire.toGMTString();
    },
    
    AddMask: function(el, msk, styleclass, maskstyleclass)
    {
        el.Mask           = msk
        el.StyleClass     = styleclass
        el.MaskStyleClass = maskstyleclass

        if (el.value == '') 
        {
            el.value = el.Mask
            BaseUI.SetClass(el, el.MaskStyleClass)
        }
        else
        {
            BaseUI.SetClass(el, el.StyleClass)
        }
               
        $addHandler(el, 'focus', function()
        {
            if (this.value == this.Mask)
            {
                this.value = ''
                BaseUI.SetClass(this, this.StyleClass)
            }
        })
        $addHandler(el, 'blur', function()
        {
            if (this.value.trim() == '')
            {
                this.value = this.Mask
                BaseUI.SetClass(this, this.MaskStyleClass)
            }
        })
    }

}

function RequestParams(afunc, obj) 
{
    this.func         = afunc
    this.responseData = null
    this.obj          = obj
    this.Error        = null
}

Sys.Application.add_load(BaseUI.WindowLoad)
