﻿/// <reference name="MicrosoftAjax.js" />

var TheLIOldLoad   = window.onload
var TheLIOldResize = window.onresize
var TheLIOldScroll = window.onscroll
var TheLIBack      = null
var TheLIBox       = null
var TheLoginDIV    = null
var TheRegisterDIV = null
var TheForgotDIV   = null
var TheLIWaitDIV   = null
var TheLIWaitTXT   = ''
var TheDomain      = ''
var TheLIResultDIV = ''
var TheLIResultTXT = ''
var TheHideItems   = new Array()

window.onload = function()
{
    if (TheLIOldLoad) TheLIOldLoad()
    Login.WindowLoad()

    window.onresize = function()
    {
        if (TheLIOldResize) TheLIOldResize()
        Login.WindowResize()
    }
    window.onscroll = function()
    {
        if (TheLIOldScroll) TheLIOldScroll()
        Login.WindowScroll()
    }
}

var Login =
{
    HideItems:  true,
    OnClose:    null,
    Params:     null,
    WindowLoad: function()
    {
        TheLIBack      = $get('liback')
        TheLIBox       = $get('libox')
        TheLoginDIV    = $get('logindv')
        TheRegisterDIV = $get('registerdv')
        TheForgotDIV   = $get('forgotdv')
        TheLIWaitDIV   = $get('liwaitdv')
        TheLIWaitTXT   = $get('liwaittxt')
        TheLIResultDIV = $get('liresult')
        TheLIResultTXT = $get('liresulttxt')

        document.body.appendChild(TheLIBack)
        document.body.appendChild(TheLIBox)
    },
    LIKeyDown: function(e)
    {
        var keynum;

        if (window.event) keynum = e.keyCode
        else if (e.which) keynum = e.which

        switch (keynum)
        {
            case Sys.UI.Key.enter: Login.Login(); return false;
        }

        return true
    },
    FPKeyDown: function(e)
    {
        var keynum;

        if (window.event) keynum = e.keyCode
        else if (e.which) keynum = e.which

        switch (keynum)
        {
            case Sys.UI.Key.enter: Login.Forgot(); return false;
        }
        
        return true
    },
    RGKeyDown: function(e)
    {
        var keynum;

        if (window.event) keynum = e.keyCode
        else if (e.which) keynum = e.which

        switch (keynum)
        {
            case Sys.UI.Key.enter: Login.Register(); return false;
        }

        return true
    },
    WindowResize: function()
    {
        if (TheLIBack && TheLIBox)
        {
            TheLIBack.style.height = document.documentElement.clientHeight + document.documentElement.scrollTop  + "px"
            TheLIBack.style.width  = document.documentElement.clientWidth  + document.documentElement.scrollLeft + "px"
            TheLIBox .style.height = document.documentElement.clientHeight + document.documentElement.scrollTop  + "px"
            TheLIBox .style.width  = document.documentElement.clientWidth  + document.documentElement.scrollLeft + "px"
        }
    },
    WindowScroll: function()
    {
        if (TheLIBack && TheLIBox)
        {
            TheLIBack.style.height = document.documentElement.clientHeight + document.documentElement.scrollTop  + "px"
            TheLIBack.style.width  = document.documentElement.clientWidth  + document.documentElement.scrollLeft + "px"
            TheLIBox .style.height = document.documentElement.clientHeight + document.documentElement.scrollTop  + "px"
            TheLIBox .style.width  = document.documentElement.clientWidth  + document.documentElement.scrollLeft + "px"
        }
    },
    OpenLogin: function(showback, hideitems, title)
    {
        Login.WindowResize()
        Login.WindowScroll()

        window.scrollTo(0, 0)

        if (showback) Login.Expand(TheLIBack)

        $get('lisubtitle').innerHTML = title ? title : ''

        Login.Collapse(TheLIWaitDIV)
        Login.Collapse(TheLIResultDIV)
        Login.HideItems = hideitems

        if (Login.HideItems)
        {
            for (var i = 0; i < TheHideItems.length; i++)
            {
                TheHideItems[i].style.visibility = 'hidden'
            }
        }

        $get('lititle').innerHTML = 'My Colorado: Login'

        Login.Expand(TheLIBox)
        Login.Expand(TheLoginDIV)

        $get('liusernametb').focus()
    },
    CloseLogin: function()
    {
        if (Login.HideItems)
        {
            for (var i = 0; i < TheHideItems.length; i++)
            {
                TheHideItems[i].style.visibility = 'visible'
            }
        }

        Login.Collapse(TheLIBack)
        Login.Collapse(TheLIBox)
        Login.Collapse(TheLoginDIV)
        Login.Collapse(TheRegisterDIV)
        Login.Collapse(TheForgotDIV)
        Login.Collapse(TheLIWaitDIV)
        Login.Collapse(TheLIResultDIV)

        if (Login.OnClose) Login.OnClose()
    },
    ShowLogin: function()
    {
        $get('lititle').innerHTML = 'My Colorado: Login'
        
        Login.Collapse(TheLIWaitDIV)
        Login.Collapse(TheForgotDIV)
        Login.Collapse(TheRegisterDIV)
        Login.Collapse(TheLIResultDIV)
        Login.Expand(TheLoginDIV)

        $get('liusernametb').focus()
    },
    ShowForgot: function()
    {
        $get('lititle').innerHTML = 'My Colorado: Forgot Password'
        
        Login.Collapse(TheLIWaitDIV)
        Login.Collapse(TheLoginDIV)
        Login.Collapse(TheRegisterDIV)
        Login.Collapse(TheLIResultDIV)
        Login.Expand(TheForgotDIV)
    },
    ShowRegister: function()
    {
        $get('lititle').innerHTML = 'My Colorado: Register'

        Login.Collapse(TheLIWaitDIV)
        Login.Collapse(TheLoginDIV)
        Login.Collapse(TheForgotDIV)
        Login.Collapse(TheLIResultDIV)
        Login.Expand(TheRegisterDIV)
    },
    Login: function()
    {
        var TheUserNameTB = $get('liusernametb')
        var ThePasswordTB = $get('lipasswordtb')

        if (TheUserNameTB.value.trim() == '')
        {
            alert('Please enter an Email!')
            TheUserNameTB.focus()
            return
        }

        if (ThePasswordTB.value.trim() == '')
        {
            alert('Please enter a Password!')
            ThePasswordTB.focus()
            return
        }

        if ((TheUserNameTB.value.indexOf('@') == -1) || (TheUserNameTB.value.indexOf('.') == -1))
        {
            alert('The Email you entered is Invalid!')
            TheUserNameTB.focus()
            return
        }

        TheLIWaitTXT.innerHTML = 'Logging in to your Account'

        Login.Collapse(TheLoginDIV)
        Login.Expand  (TheLIWaitDIV)

        Login.MakeRequest('xml/Login.ashx?f=lgi&email=' + encodeURIComponent(TheUserNameTB.value) +
                                           '&password=' + encodeURIComponent(ThePasswordTB.value) +
                                                  '&r=' + Math.random(), 'GET', null, Login.LoginComplete, null)
    },
    LoginComplete: function(params)
    {
        if (params.responseData)
        {
            var TheUser = eval(Sys.Serialization.JavaScriptSerializer.deserialize(params.responseData, false));

            if (TheUser.Error)
            {
                alert(TheUser.Error)
                Login.ShowLogin()
            }
            else
            {
                Login.SetCookie('TheSiteUser', TheUser.Token)

                $get('liusernametb').value = ''
                $get('lipasswordtb').value = ''

                if ($get('loginlink'))
                {
                    $get('loginlink').innerHTML = '&gt;View <span style="font: bold 8px Arial;">MY</span>COLORADO'
                }
                
                if (Login.OnClose)
                {
                    Login.CloseLogin()
                    Login.OnClose(true, Login.Params)
                }
            }

            return
        }

        alert('Login Failed')
    },
    Forgot: function()
    {
        var TheUserNameTB = $get('fpusernametb')

        if (TheUserNameTB.value.trim() == '')
        {
            alert('Please enter an Email!')
            return
        }

        if ((TheUserNameTB.value.indexOf('@') == -1) || (TheUserNameTB.value.indexOf('.') == -1))
        {
            alert('The Email you entered is Invalid!')
            TheUserNameTB.focus()
            return
        }
        
        TheLIWaitTXT.innerHTML = 'Sending your Password'

        Login.Collapse(TheForgotDIV)
        Login.Expand  (TheLIWaitDIV)

        Login.MakeRequest('xml/Login.ashx?f=fgp&email=' + encodeURIComponent(TheUserNameTB.value) +
                                                  '&r=' + Math.random(), 'GET', null, Login.ForgotComplete, null)
    },
    ForgotComplete: function(params)
    {
        if (params.responseData)
        {
            var TheUser = eval(Sys.Serialization.JavaScriptSerializer.deserialize(params.responseData, false));

            if (TheUser.Error)
            {
                TheLIResultTXT.innerHTML = TheUser.Error + '<br/><div style="margin-top: 5px;"><a href="#" onclick="Login.ShowLogin()">Login</a> | <a href="#" onclick="Login.ShowRegister()">Register</a> | <a href="#" onclick="Login.ShowForgot()">Try Again</a></div>'
                Login.Collapse(TheLIWaitDIV)
                Login.Expand(TheLIResultDIV)
            }
            else
            {
                TheLIResultTXT.innerHTML = TheUser.Message + '<br/><div style="margin-top: 5px;"><a href="#" onclick="Login.ShowLogin()">Login Now</a></div>'
                Login.Collapse(TheLIWaitDIV)
                Login.Expand(TheLIResultDIV)
                $get('liusernametb').value = $get('fpusernametb').value
                $get('fpusernametb').value = ''
            }
            return
        }

        alert('Forgot Failed')
    },
    Register: function()
    {
        var TheFirstNameTB = $get('regfirstnametb')
        var TheLastNameTB  = $get('reglastnametb')
        var TheUserNameTB  = $get('regusernametb')
        var ThePasswordTB  = $get('regpasswordtb')
        var TheRPasswordTB = $get('regrpasswordtb')

        if (TheFirstNameTB.value.trim() == '')
        {
            alert('Please enter a First Name!')
            TheFirstNameTB.focus()
            return
        }

        if (TheLastNameTB.value.trim() == '')
        {
            alert('Please enter a Last Name!')
            TheLastNameTB.focus()
            return
        }

        if (TheUserNameTB.value.trim() == '')
        {
            alert('Please enter an Email!')
            TheUserNameTB.focus()
            return
        }

        if ((TheUserNameTB.value.indexOf('@') == -1) || (TheUserNameTB.value.indexOf('.') == -1))
        {
            alert('The Email you entered is Invalid!')
            TheUserNameTB.focus()
            return
        }

        if (ThePasswordTB.value == '')
        {
            alert('Please enter a Password!')
            ThePasswordTB.focus()
            return
        }

        if (ThePasswordTB.value != TheRPasswordTB.value)
        {
            alert('Password and Re-enter Password are not the same!')
            TheRPasswordTB.focus()
            return
        }

        TheLIWaitTXT.innerHTML = 'Sending your Registration'

        Login.Collapse(TheRegisterDIV)
        Login.Expand(TheLIWaitDIV)

        var TheUser = new Object()

        TheUser.FirstName = TheFirstNameTB.value.trim()
        TheUser.LastName  = TheLastNameTB .value.trim()
        TheUser.UserName  = TheUserNameTB .value.trim()
        TheUser.Password  = ThePasswordTB .value.trim()
        TheUser.LastLogin = new Date()

        var ThePostData = Sys.Serialization.JavaScriptSerializer.serialize(TheUser)

        Login.MakeRequest('xml/Login.ashx?f=reg&r=' + Math.random(), 'POST', ThePostData, Login.RegisterComplete, null)
    },
    RegisterComplete: function(params)
    {
        if (params.responseData)
        {
            var TheUser = eval(Sys.Serialization.JavaScriptSerializer.deserialize(params.responseData, false));

            if (TheUser.Error)
            {
                alert(TheUser.Error)
                Login.ShowRegister()
            }
            else
            {
                Login.SetCookie('TheSiteUser', TheUser.Token)

                $get('regfirstnametb').value = ''
                $get('reglastnametb') .value = ''
                $get('regusernametb') .value = ''
                $get('regpasswordtb') .value = ''
                $get('regrpasswordtb').value = ''

                if (Login.OnClose)
                {
                    Login.CloseLogin()
                    Login.OnClose(true, Login.Params)
                }
            }

            return
        }
        
        alert('Register Failed')
    },
    MakeRequest: function(url, verb, data, func, obj)
    {
        var TheRequest = new Sys.Net.WebRequest()

        TheRequest.set_url(url)
        TheRequest.set_httpVerb(verb)

        if (data)
        {
            data = "pi=" + escape(data)
            TheRequest.set_body(data)
            TheRequest.get_headers()["Content-Length"] = data.length;
        }

        TheRequest.set_userContext(new RequestParams(func, obj))
        TheRequest.add_completed(Login.MakeRequestComplete)
        TheRequest.invoke()
    },
    MakeRequestComplete: function(executer, eventArgs)
    {
        var TheParams = executer.get_webRequest().get_userContext()

        if (executer.get_responseAvailable())
        {
            TheParams.responseData = executer.get_responseData()
            if (TheParams.func) TheParams.func(TheParams)
        }
        else
        {
            TheParams.responseData = null
            if (TheParams.func) TheParams.func(TheParams)
        }
    },
    Collapse: function(el)
    {
        if (el)
        {
            el.style.visibility = 'hidden'
            el.style.display    = 'none'
        }
    },
    Expand: function(el)
    {
        if (el)
        {
            el.style.visibility = 'visible'
            el.style.display    = 'block'
        }
    },
    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();
    }
}

function RequestParams(afunc, obj) 
{
    this.func         = afunc
    this.responseData = null
    this.obj          = obj
    this.Error        = null
}