﻿/// <reference name="MicrosoftAjax.js" />
/// <reference name="Login.js" />

var TheHDOldLoad      = window.onload
var TheHDOldResize    = window.onresize
var TheHDOldScroll    = window.onscroll
var TheHDBack         = null
var TheHDBox          = null
var TheHDIBack        = null
var TheHDIBox         = null
var TheHDWait         = null
var TheHDList         = null
var TheHDItem         = null
var TheVADBTN         = null
var TheVADBBTN        = null
var TheSTFNameTB      = null
var TheSTFEmailTB     = null
var TheCurrentHotDeal = null
var TheTSD            = null
var TheTED            = null
var TheShowViewAll    = false
var TheSTFMain        = null

window.onload = function() 
{
    if (TheHDOldLoad) TheHDOldLoad()
    HotDeals.WindowLoad()

    window.onresize = function()
    {
        if (TheHDOldResize) TheHDOldResize()
        HotDeals.WindowResize()
    }
    window.onscroll = function()
    {
        if (TheHDOldScroll) TheHDOldScroll()
        HotDeals.WindowScroll()
    }
}

var HotDeals =
{
    HDID:    null,
    Preview: null,
    OnClose: null,
    ShowViewAllBusiness: false,
    ShowViewAll: function()
    {
        TheShowViewAll = true
    },
    HideMyColorado: function()
    {
        HotDeals.Collapse($get('hdamcbtn'))
    },
    WindowLoad: function()
    {
        TheHDBack     = $get('hdbback')
        TheHDBox      = $get('hdbbox')
        TheHDIBack    = $get('hdibback')
        TheHDIBox     = $get('hdibbox')
        TheHDWait     = $get('hdwait')
        TheHDList     = $get('hdlist')
        TheHDItem     = $get('hddiv')
        TheVADBTN     = $get('hdvadbtn')
        TheVADBBTN    = $get('hdvadbbtn')
        TheSTFNameTB  = $get('stfname')
        TheSTFEmailTB = $get('stfemail')
        TheSTFMain    = $get('hdmstf')

        if (TheShowViewAll) HotDeals.Expand(TheVADBTN)

        document.body.appendChild(TheHDBack)
        document.body.appendChild(TheHDBox)

        if (HotDeals.HDID)
        {
            HotDeals.OpenHotDeal(HotDeals.HDID, 'c')
        }
    },
    STFKeyDown: function(e)
    {
        var keynum;

        if (window.event) keynum = e.keyCode
        else if (e.which) keynum = e.which

        switch (keynum)
        {
            case Sys.UI.Key.enter: HotDeals.SendToFriend(); return false;
        }

        return true
    },
    STFMKeyDown: function(e)
    {
        var keynum;

        if (window.event) keynum = e.keyCode
        else if (e.which) keynum = e.which

        switch (keynum)
        {
            case Sys.UI.Key.enter: HotDeals.SendToFriendMain(); return false;
        }

        return true
    },
    WindowResize: function()
    {
        if (TheHDBack && TheHDBox)
        {
            TheHDBack.style.height = document.documentElement.clientHeight + document.documentElement.scrollTop  + "px"
            TheHDBack.style.width  = document.documentElement.clientWidth  + document.documentElement.scrollLeft + "px"
            TheHDBox .style.height = document.documentElement.clientHeight + document.documentElement.scrollTop  + "px"
            TheHDBox .style.width  = document.documentElement.clientWidth  + document.documentElement.scrollLeft + "px"
        }
    },
    WindowScroll: function()
    {
        if (TheHDBack && TheHDBox)
        {
            TheHDBack.style.height = document.documentElement.clientHeight + document.documentElement.scrollTop  + "px"
            TheHDBack.style.width  = document.documentElement.clientWidth  + document.documentElement.scrollLeft + "px"
            TheHDBox .style.height = document.documentElement.clientHeight + document.documentElement.scrollTop  + "px"
            TheHDBox .style.width  = document.documentElement.clientWidth  + document.documentElement.scrollLeft + "px"
        }
    },
    CreatePrintReport: function()
    {
        if (TheCurrentHotDeal)
        {
            var el  = $get('ThePrintDiv')
            var cel = $get('TheCouponPrintDIV').innerHTML

            var TheAddress    = ''
            var TheHeadline2  = 'Offer Starts - ' + HotDeals.GetDateString(TheCurrentHotDeal.StartDate) + ' | Offer Ends - ' + HotDeals.GetDateString(TheCurrentHotDeal.EndDate)
            var TheCouponCode = ''

            if (TheCurrentHotDeal.Addresses[0])
            {
                TheAddress = TheCurrentHotDeal.Addresses[0].Street1

                if (TheCurrentHotDeal.Addresses[0].Street2.trim() != '')
                {
                    TheAddress += '<br/>' + TheCurrentHotDeal.Addresses[0].Street2
                }

                TheAddress += '<br/>' + TheCurrentHotDeal.Addresses[0].City + ', ' + TheCurrentHotDeal.Addresses[0].State + ' ' + TheCurrentHotDeal.Addresses[0].Zip
            }
            else
            {
                TheAddress = '<br/><br/><br/>'
            }

            if (TheCurrentHotDeal.OfferCode != '') TheCouponCode = 'Coupon Code - ' + TheCurrentHotDeal.OfferCode

            cel = cel.replace('[Headline]',     TheCurrentHotDeal.Headline)
            cel = cel.replace('[Headline2]',    TheHeadline2)
            cel = cel.replace('[BusinessName]', TheCurrentHotDeal.Name)
            cel = cel.replace('[Address]',      TheAddress)
            cel = cel.replace('[Description]',  TheCurrentHotDeal.Description)
            cel = cel.replace('[Disclaimer]',   TheCurrentHotDeal.Disclaimer)
            cel = cel.replace('[OfferCode]',    TheCouponCode)

            el.innerHTML = cel

            var disc = document.createElement('div')

            disc.innerHTML = '<div style="text-transform: none; font-size: 10px; margin-top: 30px;">Some Restrictions May Apply</div>'

            el.appendChild(disc)
        }
    },
    CreateMultiplePrintReport: function(hotdeallist)
    {
        var el = $get('ThePrintDiv')
        var rpt = new Sys.StringBuilder('')

        for (var i = 0; i < hotdeallist.length; i++)
        {
            var TheCurrentHotDeal = hotdeallist[i]

            if (TheCurrentHotDeal.EndDate < new Date()) continue

            var cel           = $get('TheCouponPrintDIV').innerHTML
            var TheAddress    = ''
            var TheHeadline2  = 'Offer Starts - ' + HotDeals.GetDateString(TheCurrentHotDeal.StartDate) + ' | Offer Ends - ' + HotDeals.GetDateString(TheCurrentHotDeal.EndDate)
            var TheCouponCode = ''

            if (TheCurrentHotDeal.Addresses[0])
            {
                TheAddress = TheCurrentHotDeal.Addresses[0].Street1

                if (TheCurrentHotDeal.Addresses[0].Street2.trim() != '')
                {
                    TheAddress += '<br/>' + TheCurrentHotDeal.Addresses[0].Street2
                }

                TheAddress += '<br/>' + TheCurrentHotDeal.Addresses[0].City + ', ' + TheCurrentHotDeal.Addresses[0].State + ' ' + TheCurrentHotDeal.Addresses[0].Zip
            }
            else
            {
                TheAddress = '<br/><br/><br/>'
            }

            if (TheCurrentHotDeal.OfferCode != '') TheCouponCode = 'Coupon Code - ' + TheCurrentHotDeal.OfferCode

            cel = cel.replace('[Headline]',     TheCurrentHotDeal.Headline)
            cel = cel.replace('[Headline2]',    TheHeadline2)
            cel = cel.replace('[BusinessName]', TheCurrentHotDeal.Name)
            cel = cel.replace('[Address]',      TheAddress)
            cel = cel.replace('[Description]',  TheCurrentHotDeal.Description)
            cel = cel.replace('[Disclaimer]',   TheCurrentHotDeal.Disclaimer)
            cel = cel.replace('[OfferCode]',    TheCouponCode)

            rpt.append(cel)

            if (i + 1 < hotdeallist.length)
            {
                if ((i + 1) % 2 == 0)
                {
                    rpt.append('<div style="page-break-after: always;"></div>')
                }
            }
        }

        rpt.append('<div style="text-transform: none; font-size: 10px; margin-top: 30px;">Some Restrictions May Apply</div>')

        el.innerHTML = rpt.toString()
    },
    SendToFriendMainOpen: function()
    {
        var TheCurrentUser = HotDeals.GetCookie('TheSiteUser')

        if (TheCurrentUser)
        {
            HotDeals.WindowResize()
            HotDeals.WindowScroll()

            window.scrollTo(0, 0)

            $get('hdsubtitle').innerHTML = 'Send to Friend'

            $get('hdt')  .style.width = '490px'
            $get('hdtd') .style.width = '482px'
            $get('hdcon').style.width = '490px'

            HotDeals.Collapse(TheHDList)
            HotDeals.Collapse(TheHDItem)
            HotDeals.Collapse($get('hdresult'))
            HotDeals.Expand(TheHDBack)
            HotDeals.Expand(TheHDBox)
            HotDeals.Expand(TheSTFMain)

            for (var i = 0; i < TheHideItems.length; i++)
            {
                TheHideItems[i].style.visibility = 'hidden'
            }

            $get('stfmyourname').focus()
        }
    },
    SendToFriendMain: function()
    {
        var TheCurrentUser = HotDeals.GetCookie('TheSiteUser')

        if (TheCurrentUser)
        {
            var TheNameTB = $get('stfmyourname')
            var TheEmailTB = $get('stfmemail')

            if (TheNameTB.value.trim() == '')
            {
                alert('Please enter Your Name!')
                TheNameTB.focus()
                return
            }

            if (TheEmailTB.value.trim() == '')
            {
                alert('Please enter your Friend\'s Email!')
                TheEmailTB.focus()
                return
            }

            if ((TheEmailTB.value.trim().indexOf('@') == -1) || (TheEmailTB.value.trim().indexOf('.') == -1))
            {
                alert('The Email Address you entered is Invalid!')
                TheEmailTB.focus()
                return
            }
            $get('hdwaitimg').style.marginLeft = '120px'
            $get('hdwaittxt').innerHTML = 'Sending Hot Deals to Friend'
            HotDeals.Collapse(TheSTFMain)
            HotDeals.Expand(TheHDWait)

            HotDeals.MakeRequest('xml/HotDeals.ashx?f=sfm' +
                                                  '&u=' + TheCurrentUser +
                                               '&name=' + encodeURIComponent(TheNameTB.value.trim()) +
                                              '&email=' + encodeURIComponent(TheEmailTB.value.trim()) +
                                                  '&r=' + Math.random(), 'GET', null, HotDeals.SendToFriendMainComplete, null)
        }
    },
    SendToFriendMainComplete: function(params)
    {
        if (params.responseData)
        {
            var TheResult = eval(Sys.Serialization.JavaScriptSerializer.deserialize(params.responseData, false));

            if (TheResult.Error)
            {
                $get('hdresulttxt').innerHTML = TheResult.Error
            }
            else
            {
                $get('hdresulttxt').innerHTML = TheResult.Result
                $get('stfmyourname').value = ''
                $get('stfmemail').value = ''
                //pageTracker._trackEvent('Hot Deal - ' + TheCurrentHotDeal.ListingID + ' - ' + TheCurrentHotDeal.ID, 'Send to Friend')
            }
            HotDeals.Collapse(TheHDWait)
            HotDeals.Expand($get('hdresult'))
            return
        }

        alert('Send to Friend Request Failed')
    },
    PrintAccountHotDeals: function()
    {
        $get('hdt')      .style.width = '690px'
        $get('hdtd')     .style.width = '682px'
        $get('hdcon')    .style.width = '690px'
        $get('hdwaitimg').style.marginLeft = '260px'
        $get('hdsubtitle').innerHTML = ''

        var TheCurrentUser = HotDeals.GetCookie('TheSiteUser')

        if (TheCurrentUser)
        {
            HotDeals.WindowResize()
            HotDeals.WindowScroll()

            window.scrollTo(0, 0)

            HotDeals.Collapse(TheHDList)
            HotDeals.Collapse(TheHDItem)
            HotDeals.Expand(TheHDBack)
            HotDeals.Expand(TheHDBox)
            $get('hdwaittxt').innerHTML = 'Printing Hot Deals'
            HotDeals.Expand(TheHDWait)

            for (var i = 0; i < TheHideItems.length; i++)
            {
                if (TheHideItems[i].checkme)
                {
                    if (TheHideItems[i - 1].options[TheHideItems[i - 1].selectedIndex].getAttribute('C'))
                    {
                        TheHideItems[i].style.visibility = 'hidden'
                    }
                }
                else TheHideItems[i].style.visibility = 'hidden'
            }

            HotDeals.MakeRequest('xml/HotDeals.ashx?f=gad&u=' + TheCurrentUser + '&r=' + Math.random(), 'GET', null, HotDeals.PrintAccountHotDealsComplete, null)
        }
    },
    PrintAccountHotDealsComplete: function(params)
    {
        if (params.responseData)
        {
            var TheHotDeals = eval(Sys.Serialization.JavaScriptSerializer.deserialize(params.responseData, false));

            if (TheHotDeals.Error)
            {
                $get('hdresulttxt').innerHTML = TheHotDeals.Error
                HotDeals.Collapse($get('hdwait'))
                HotDeals.Expand($get('hdresult'))
            }
            else
            {
                HotDeals.CreateMultiplePrintReport(TheHotDeals)
                if (window.print) window.print()
                HotDeals.CloseHotDeal()
            }

            return
        }

        alert('Print Account Hot Deals Request Failed')
    },
    OpenHotDeal: function(id, itemtype)
    {
        $get('hdt')      .style.width = '690px'
        $get('hdtd')     .style.width = '682px'
        $get('hdcon')    .style.width = '690px'
        $get('hdwaitimg').style.marginLeft = '260px'
        $get('hdsubtitle').innerHTML = ''

        HotDeals.WindowResize()
        HotDeals.WindowScroll()

        if (Sys.Browser.agent == Sys.Browser.Safari)
        {
            setTimeout('document.body.scrollTop = 0;', 100);
        }
        else
        {
            window.scrollTo(0, 0)
        }
        
        HotDeals.Collapse(TheSTFMain)
        HotDeals.Collapse(TheHDList)
        HotDeals.Collapse(TheHDItem)
        HotDeals.Collapse($get('hdresult'))
        if (HotDeals.ShowViewAllBusiness) HotDeals.Collapse(TheVADBBTN)
        HotDeals.Expand(TheHDBack)
        HotDeals.Expand(TheHDBox)

        for (var i = 0; i < TheHideItems.length; i++)
        {
            if (TheHideItems[i].checkme)
            {
                if (TheHideItems[i - 1].options[TheHideItems[i - 1].selectedIndex].getAttribute('C'))
                {
                    TheHideItems[i].style.visibility = 'hidden'
                }
            }
            else TheHideItems[i].style.visibility = 'hidden'
        }

        switch (itemtype)
        {
            case 'c':
                $get('hdwaittxt').innerHTML = 'Loading Hot Deal'
                HotDeals.Expand(TheHDWait)
                HotDeals.MakeRequest('xml/HotDeals.ashx?f=ghd&iid=' + id + '&pv=' + (HotDeals.Preview ? '1' : '0') + '&r=' + Math.random(), 'GET', null, HotDeals.GetHotDealComplete, null)
                break
            case 'b':
                $get('hdwaittxt').innerHTML = 'Loading Hot Deals'
                HotDeals.Expand(TheHDWait)
                HotDeals.MakeRequest('xml/HotDeals.ashx?f=gbd&lid=' + id + '&r=' + Math.random(), 'GET', null, HotDeals.GetBusinessHotDealsComplete, null)
                break
        }
    },
    CloseHotDeal: function()
    {
        HotDeals.Collapse(TheHDBack)
        HotDeals.Collapse(TheHDBox)
        HotDeals.Collapse(TheHDWait)
        HotDeals.Collapse(TheHDItem)
        HotDeals.Collapse(TheHDList)
        HotDeals.Collapse(TheSTFMain)
        HotDeals.Collapse($get('hdresult'))

        if (HotDeals.ShowViewAllBusiness) HotDeals.Collapse(TheVADBBTN)

        HotDeals.CloseSub()

        for (var i = 0; i < TheHideItems.length; i++)
        {
            if (TheHideItems[i].checkme)
            {
                if (TheHideItems[i - 1].options[TheHideItems[i - 1].selectedIndex].getAttribute('C'))
                {
                    TheHideItems[i].style.visibility = 'visible'
                }
            }
            else TheHideItems[i].style.visibility = 'visible'
        }

        if (HotDeals.OnClose) HotDeals.OnClose()
        HotDeals.ShowViewAllBusiness = false
        HotDeals.Collapse(TheVADBBTN)
    },
    OpenSub: function()
    {
        HotDeals.Expand(TheHDIBack)
        HotDeals.Expand(TheHDIBox)
    },
    CloseSub: function()
    {
        HotDeals.Collapse(TheHDIBack)
        HotDeals.Collapse(TheHDIBox)
        HotDeals.Collapse($get('hdistf'))
        HotDeals.Collapse($get('hdiwait'))
        HotDeals.Collapse($get('hdiresult'))
    },
    AddToMyColorado: function(id)
    {
        var TheID = id

        if (!TheID) TheID = TheCurrentHotDeal.ID

        var TheCurrentUser = HotDeals.GetCookie('TheSiteUser')

        $get('hdisubtitle').innerHTML = 'Add to My Colorado'

        TheHDIBack.style.height = $get('hdcon').offsetHeight + 'px'
        TheHDIBack.style.width = $get('hdcon').offsetWidth + 'px'

        if (TheCurrentUser)
        {
            $get('hdiwaittxt').innerHTML = 'Adding to My Colorado Account'

            HotDeals.OpenSub()
            HotDeals.Expand($get('hdiwait'))
            HotDeals.MakeRequest('xml/HotDeals.ashx?f=amc&iid=' + TheCurrentHotDeal.ID + '&u=' + TheCurrentUser + '&r=' + Math.random(), 'GET', null, HotDeals.AddToMyColoradoComplete, null)
        }
        else
        {
            Login.Params = new RequestParams()
            Login.Params.obj = new Array()
            Login.Params.obj[0] = HotDeals.AddToMyColorado
            Login.Params.obj[1] = id

            Login.OnClose = HotDeals.LoginClose
            HotDeals.Expand(TheHDIBack)
            Login.OpenLogin(false, false, 'In order to add items to your MyColorado account you must be logged in.')
        }
    },
    LoginClose: function(result, params)
    {
        HotDeals.Collapse(TheHDIBack)

        if (result)
        {
            if (params.obj)
            {
                if (params.obj.length == 2)
                {
                    params.obj[0](params.obj[1])
                    return
                }
                if (params.obj.length == 3)
                {
                    params.obj[0](params.obj[1], params.obj[2])
                    return
                }
            }
        }
    },
    AddToMyColoradoComplete: function(params)
    {
        if (params.responseData)
        {
            var TheResult = eval(Sys.Serialization.JavaScriptSerializer.deserialize(params.responseData, false));

            if (TheResult.Error)
            {
                $get('hdiresulttxt').innerHTML = TheResult.Error
            }
            else
            {
                $get('hdiresulttxt').innerHTML = TheResult.Result
                pageTracker._trackEvent('Hot Deal - ' + TheCurrentHotDeal.ListingID + ' - ' + TheCurrentHotDeal.ID, 'Add to My Colorado')
            }

            HotDeals.Collapse($get('hdiwait'))
            HotDeals.Expand($get('hdiresult'))
            return
        }

        alert('Add to My Colorado Request Failed')
    },
    AddToMyColoradoB: function(id, el)
    {
        var TheCurrentUser = HotDeals.GetCookie('TheSiteUser')

        if (TheCurrentUser)
        {
            el.lid = el.getAttribute("lid")
            el.hdid = id
            el.innerHTML = 'Adding...'

            HotDeals.MakeRequest('xml/HotDeals.ashx?f=amc&iid=' + id + '&u=' + TheCurrentUser, 'GET', null, HotDeals.AddToMyColoradoBComplete, el)
        }
        else
        {
            Login.OnClose = HotDeals.LoginClose

            Login.Params = new RequestParams()
            Login.Params.obj = new Array()
            Login.Params.obj[0] = HotDeals.AddToMyColoradoB
            Login.Params.obj[1] = id
            Login.Params.obj[2] = el

            Login.OpenLogin(true, true)
        }
    },
    AddToMyColoradoBComplete: function(params)
    {
        if (params.responseData)
        {
            var TheResult = eval(Sys.Serialization.JavaScriptSerializer.deserialize(params.responseData, false));

            if (TheResult.Error)
            {
                params.obj.innerHTML = "Error"
            }
            else
            {
                params.obj.innerHTML = "Added!"
                pageTracker._trackEvent('Hot Deal - ' + params.obj.lid + ' - ' + params.obj.hdid, 'Add to My Colorado')
            }
            return
        }

        alert('Add to My Colorado Request Failed')
    },
    SendToFriendOpen: function(id)
    {
        var TheID = id

        if (!TheID) TheID = TheCurrentHotDeal.ID

        $get('hdisubtitle').innerHTML = 'Send to Friend'

        TheHDIBack.style.height = $get('hdcon').offsetHeight + 'px'
        TheHDIBack.style.width  = $get('hdcon').offsetWidth  + 'px'

        HotDeals.Expand($get('hdistf'))
        HotDeals.OpenSub()

        TheSTFNameTB.focus()
    },
    SendToFriend: function()
    {
        var TheName  = TheSTFNameTB.value.trim()
        var TheEmail = TheSTFEmailTB.value.trim()

        if (TheName == '')
        {
            alert('Please enter Your Name!')
            TheSTFNameTB.focus()
            return
        }

        if (TheEmail == '')
        {
            alert('Please enter your Friend\'s Email!')
            TheSTFEmailTB.focus()
            return
        }

        if ((TheEmail.indexOf('@') == -1) || (TheEmail.indexOf('.') == -1))
        {
            alert('The Email Address you entered is Invalid!')
            TheSTFEmailTB.focus()
            return
        }

        $get('hdiwaittxt').innerHTML = 'Sending Hot Deal to Friend'
        HotDeals.Collapse($get('hdistf'))
        HotDeals.Expand($get('hdiwait'))

        HotDeals.MakeRequest('xml/HotDeals.ashx?f=stf' +
                                            '&iid=' + TheCurrentHotDeal.ID +
                                           '&name=' + encodeURIComponent(TheName) +
                                          '&email=' + encodeURIComponent(TheEmail) +
                                              '&r=' + Math.random(), 'GET', null, HotDeals.SendToFriendComplete, null)
    },
    SendToFriendComplete: function(params)
    {
        if (params.responseData)
        {
            var TheResult = eval(Sys.Serialization.JavaScriptSerializer.deserialize(params.responseData, false));

            if (TheResult.Error)
            {
                $get('hdiresulttxt').innerHTML = TheResult.Error
            }
            else
            {
                $get('hdiresulttxt').innerHTML = TheResult.Result
                TheSTFNameTB.value = ''
                TheSTFEmailTB.value = ''
                pageTracker._trackEvent('Hot Deal - ' + TheCurrentHotDeal.ListingID + ' - ' + TheCurrentHotDeal.ID, 'Send to Friend')
            }
            HotDeals.Collapse($get('hdiwait'))
            HotDeals.Expand($get('hdiresult'))
            return
        }

        alert('Send to Friend Request Failed')
    },
    Print: function(id)
    {
        var TheID = id

        if (!TheID) TheID = TheCurrentHotDeal.ID

        pageTracker._trackEvent('Hot Deal - ' + TheCurrentHotDeal.ListingID + ' - ' + TheCurrentHotDeal.ID, 'Print')

        if (window.print) window.print()
    },
    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(HotDeals.MakeRequestComplete)
        TheRequest.invoke()
    },
    GetHotDealComplete: function(params)
    {
        if (params.responseData)
        {
            var TheHotDeal = eval(Sys.Serialization.JavaScriptSerializer.deserialize(params.responseData, false));

            if (TheHotDeal.Error)
            {
                alert(TheHotDeal.Error)
            }
            else
            {
                HotDeals.LoadHotDeal(TheHotDeal)

                HotDeals.Collapse(TheHDWait)
                HotDeals.Expand(TheHDItem)
                if (HotDeals.ShowViewAllBusiness) HotDeals.Expand(TheVADBBTN)
                pageTracker._trackEvent('Hot Deal - ' + TheHotDeal.ListingID + ' - ' + TheHotDeal.ID, 'View')
            }
            return
        }

        alert('Hot Deal Request Failed')
    },
    LoadHotDeal: function(hotdeal)
    {
        TheCurrentHotDeal = hotdeal

        HotDeals.CreatePrintReport()

        var TheBusinessNameEL = $get('hdbusname')
        var TheTitleEL = $get('hdtitle')
        var TheStreet1EL = $get('hdbusstreet1')
        var TheStreet2EL = $get('hdbusstreet2')
        var TheCSZEL = $get('hdbuscitysz')
        var TheMainImage = $get('hdmaindetail')
        var TheMainDetail = null

        TheTitleEL.innerHTML = hotdeal.Headline
        $get('hdtitle2').innerHTML = 'Offer Starts - ' + HotDeals.GetDateString(hotdeal.StartDate) + ' | Offer Ends - ' + HotDeals.GetDateString(hotdeal.EndDate)
        $get('hddesc').innerHTML = hotdeal.Description
        $get('hddisc').innerHTML = hotdeal.Disclaimer
        TheBusinessNameEL.innerHTML = hotdeal.Name

        var TheBusinessLink = null
        var TheTitleLink = null

        for (var i = 0; i < hotdeal.MetaData.length; i++)
        {
            if (hotdeal.MetaData[i].Name == 'BL')
            {
                TheBusinessLink = hotdeal.MetaData[i].Value
                break;
            }
        }

        $clearHandlers(TheBusinessNameEL)
        $clearHandlers(TheTitleEL)

        TheBusinessLink = TheBusinessLink == null ? 'Listing.aspx?did=' + hotdeal.ListingID : TheBusinessLink
        TheTitleLink    = hotdeal.Urls.length > 0 ? hotdeal.Urls[0].Value : TheBusinessLink

        TheBusinessNameEL.href = TheBusinessLink
        TheTitleEL       .href = TheTitleLink
        TheMainImage     .href = TheTitleLink

        $get('hdlink').href = TheTitleLink

        TheBusinessNameEL.HotDeal = TheCurrentHotDeal
        TheTitleEL       .HotDeal = TheCurrentHotDeal
        TheMainImage     .HotDeal = TheCurrentHotDeal
        $get('hdlink')   .HotDeal = TheCurrentHotDeal

        $addHandler(TheBusinessNameEL, "click", function() { pageTracker._trackEvent('Hot Deal - ' + this.HotDeal.ListingID + ' - ' + this.HotDeal.ID, 'Click Title - '     + TheBusinessLink); });
        $addHandler(TheTitleEL,        "click", function() { pageTracker._trackEvent('Hot Deal - ' + this.HotDeal.ListingID + ' - ' + this.HotDeal.ID, 'Click Headline - '  + TheTitleLink);    });
        $addHandler(TheMainImage,      "click", function() { pageTracker._trackEvent('Hot Deal - ' + this.HotDeal.ListingID + ' - ' + this.HotDeal.ID, 'Click Image - '     + TheTitleLink);    });
        $addHandler($get('hdlink'),    "click", function() { pageTracker._trackEvent('Hot Deal - ' + this.HotDeal.ListingID + ' - ' + this.HotDeal.ID, 'Click More Info - ' + TheTitleLink);    });

        for (var i = 0; i < hotdeal.Assets.length; i++)
        {
            if (hotdeal.Assets[i].ItemName == 'MainDetail')
            {
                TheMainDetail = hotdeal.Assets[i]
                break
            }
        }

        if (hotdeal.Addresses[0])
        {
            TheStreet1EL.innerHTML = hotdeal.Addresses[0].Street1

            if (hotdeal.Addresses[0].Street2.trim() != '')
            {
                TheStreet2EL.innerHTML = hotdeal.Addresses[0].Street2
                HotDeals.Expand(TheStreet2EL)
            }
            else
            {
                TheStreet2EL.innerHTML = ''
                HotDeals.Collapse(TheStreet2EL)
            }
            TheCSZEL.innerHTML = hotdeal.Addresses[0].City + ', ' + hotdeal.Addresses[0].State + ' ' + hotdeal.Addresses[0].Zip
        }
        else
        {
            TheStreet1EL.innerHTML = '&nbsp;'
            TheStreet2EL.innerHTML = '&nbsp;'
            TheCSZEL.innerHTML = '&nbsp;'
        }

        if (TheMainDetail)
        {
            $get('hdmaindetail').style.backgroundImage = "url('/" + TheMainDetail.Url + "')"
        }
        else
        {
            $get('hdmaindetail').style.backgroundImage = "url('/images/hdgenlg.jpg')"
        }

        if (HotDeals.ShowViewAllBusiness)
        {
            TheVADBBTN.ListingID = hotdeal.ListingID

            TheVADBBTN.onclick = function()
            {
                HotDeals.OpenHotDeal(this.ListingID, 'b')
            }
        }
    },
    LoadHotDeals: function(hotdeallist)
    {
        HotDeals.RemoveAllChildren(TheHDList)

        var ltitle = document.createElement('div')

        ltitle.innerHTML = 'Hot Deals for ' + hotdeallist[0].Name

        HotDeals.SetClass(ltitle, 'hdltitle')

        TheHDList.appendChild(ltitle)

        for (var i = 0; i < hotdeallist.length; i++)
        {
            var hotdeal = hotdeallist[i]
            var TheListingThumb = null

            for (var j = 0; j < hotdeal.Assets.length; j++)
            {
                if (hotdeal.Assets[j].ItemName == 'ListingThumbnail')
                {
                    TheListingThumb = hotdeal.Assets[j]
                    break
                }
            }

            var itemdiv = document.createElement('div')
            var itemidv = document.createElement('div')
            var itemimg = document.createElement('div')
            var itemttl = document.createElement('h3')
            var itemdsc = document.createElement('div')
            var itemclr = document.createElement('div')

            itemdiv.CouponID = hotdeal.ID
            itemdiv.itemidv = itemidv

            HotDeals.SetClass(itemidv, 'hdlimgbk')

            itemclr.style.clear = 'both'

            if (TheListingThumb)
            {
                itemimg.style.background = "url('/" + TheListingThumb.Url + "') no-repeat"
            }
            else
            {
                itemimg.style.backgroundImage = "url('/images/hdgen.jpg')"
            }

            if (!((Sys.Browser.agent == Sys.Browser.InternetExplorer) && (Sys.Browser.version == 6)))
            {
                itemdiv.onmouseover = function()
                {
                    HotDeals.SetClass(this, 'hdlimv');
                    HotDeals.SetClass(this.itemidv, 'hdlimgbkmo')
                }
                itemdiv.onmouseout = function()
                {
                    HotDeals.SetClass(this, 'hdlimo');
                    HotDeals.SetClass(this.itemidv, 'hdlimgbk')
                }
            }

            itemdiv.onclick = function() { HotDeals.OpenHotDeal(this.CouponID, 'c'); }

            itemdiv.style.cursor = 'pointer'
            HotDeals.SetClass(itemdiv, 'hdlimo')
            HotDeals.SetClass(itemimg, 'hdlimg')

            itemttl.innerHTML = hotdeal.Headline
            itemdsc.innerHTML = hotdeal.Description

            itemidv.appendChild(itemimg)

            itemdiv.appendChild(itemidv)
            itemdiv.appendChild(itemttl)
            itemdiv.appendChild(itemdsc)
            itemdiv.appendChild(itemclr)

            TheHDList.appendChild(itemdiv)
        }
        var divider = document.createElement('div')
        HotDeals.SetClass(divider, 'hdldvd')
        TheHDList.appendChild(divider)
    },
    GetBusinessHotDealsComplete: function(params)
    {
        if (params.responseData)
        {
            var TheHotDeals = eval(Sys.Serialization.JavaScriptSerializer.deserialize(params.responseData, false));

            if (TheHotDeals.Error)
            {
                alert(TheHotDeals.Error)
            }
            else
            {
                if (TheHotDeals.length == 1)
                {
                    pageTracker._trackEvent('Hot Deal - ' + TheHotDeals[0].ListingID + ' - ' + TheHotDeals[0].ID, 'View')
                    HotDeals.OpenHotDeal(TheHotDeals[0].ID, 'c')
                    HotDeals.ShowViewAllBusiness = false
                    HotDeals.Collapse(TheVADBBTN)
                }
                else
                {
                    HotDeals.LoadHotDeals(TheHotDeals)
                    HotDeals.Collapse(TheHDWait)
                    HotDeals.Expand(TheHDList)
                    HotDeals.ShowViewAllBusiness = true
                    HotDeals.Expand(TheVADBBTN)
                }
            }
            return
        }

        alert('Hot Deals Request Failed')
    },
    SetClass: function(el, name)
    {
        if (el)
        {
            el.setAttribute('class', name)
            el.setAttribute('className', name)
        }
    },
    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)
        }
    },
    RemoveAllChildren: function(el)
    {
        while (el.childNodes.length > 0) el.removeChild(el.childNodes[0])
    },
    GetDateString: function(date)
    {
        if (!date) return ''

        var offset = (date.getTimezoneOffset() - 300) * 60 * 1000;

        var TheDate = new Date()

        TheDate.setTime(date.getTime() + offset)

        return TheDate.format("MM/dd/yyyy")
    },
    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'
        }
    },
    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
    }
}
