﻿function OneClick() 
{
    this.action = null;
    this.eventid = 0;
    this.expInMin = 1;
    this.forGeckoMozilla = document.implementation.hasFeature("XPath", "3.0");
    this.GotoUrl = function(e, callback, log, sender, quickactionurl, isquickactionincrement, eventId,quickaction)
    {
        quickactionurl = quickactionurl.replace(/\&amp;/gi,'&');
        if ((isquickactionincrement) && (quickaction == "2" ))
        {
            var params = "eventid=" + eventId;
            var req = buildRequest("Handlers/UpdateAttendance.ashx",true,params);
            req.send(params);
        }
        if (quickactionurl.indexOf(location.host) < 0)
            window.open(quickactionurl, '_blank');
        else
        {
            var frompage = $("frompage");
            var frompageQS = "";
            if (frompage != null)
            {
                var frompageVal = frompage.getAttribute("val");
                if (frompageVal != null)
                {
                    frompageQS = "&fp=" + frompageVal;
                }
            }
            
            location.href = quickactionurl + frompageQS;
            
        }
    }
    
    this.Bookmark = function(e, callback, log, sender,eventid)
    {
        this.eventid = eventid;
        this.action = "bookmark";
        var params      =       "culture=" + $("culture").getAttribute("culture") + 
                                "&eventid=" + eventid +
                                "&frompage=" + $("frompage").getAttribute("val");
                                
        this.makeRequest(params, log, e);
        
    }.virtual(this);
    
     this.CreateBookmarkCookie = function (eventid)
    {
        var usrBkmk = getRealCookie("usrBkmk");
        usrBkmk = (usrBkmk != null && usrBkmk != undefined) ? usrBkmk + ",": "";
        if (usrBkmk.indexOf(eventid) < 0)
               createRealCookie("usrBkmk", usrBkmk + eventid, this.expInMin * 60 * 1000);
    }
    
    this.UpdateBookmarkIcon = function()
    {
        var usrBkmk = getRealCookie("usrBkmk");
        if (usrBkmk != null && usrBkmk != undefined)
        {
            var bkmksplitted = usrBkmk.split(',');   
            if (bkmksplitted != null)
            {
                for (var i = 0; i < bkmksplitted.length; i++)
                {   
                     this.UpdateBookmarkSpan(bkmksplitted[i]);
                }
            }
        }
    }
    
    this.RemoveBookmarkCookie = function(eventId)
    {
        var usrBkmk = getRealCookie("usrBkmk");
        if (usrBkmk != null && usrBkmk != undefined)
        {
            var bkmksplitted = usrBkmk.split(',');   
            if (bkmksplitted != null)
            {
                var j =0;
                var bookmarkcookie = new Array();
                for (var i = 0; i < bkmksplitted.length; i++)
                {   
                   if (bkmksplitted[i] != eventId)
                   {
                        bookmarkcookie[j++] = bkmksplitted[i];
                   }
                }
                createRealCookie("usrBkmk", bookmarkcookie.join(','), this.expInMin * 60 * 1000);
            }
        }
    }
    
    this.selectSingleNode = function(xgData, path)
    {
    
        return (this.forGeckoMozilla ?  xgData.selectSingleNodeWWE(path) : xgData.selectSingleNode(path));
    }
    
    this.makeRequest = function(params, log, e)
    {
        var     req;
        var evt = e;
        var outer = this;
        showProcessing(true, outer.eventid, "smallloading");
        if (outer.action == "bookmark")
        {
              req = buildRequest("Handlers/AddBookmark.ashx",true,params);    
        }
        
		
		req.onreadystatechange = function(){
			if (req.readyState == 4 && req.status == 200)
			{
				var status_code;
				var status_text;
				try
				{
					status_code = req.status;
					status_text = req.statusText;
				}
				catch(e) {
				    reportException(e,target);
				}
				if (status_code == 200 && req.responseText && req.responseText.indexOf("Error.aspx") == -1)
				{
				    if(req.responseXML)
				    {
				        xgData = req.responseXML;
				        var transformed;
				        try
				        {
				           if (outer.action == "bookmark")
				           {
                               var urlAtt = outer.selectSingleNode(xgData, "/root/passport/@url");
                               if (urlAtt != null)
                               {
                                    var url = urlAtt.text || urlAtt.textContent;
                                    location.href = url;
                               }
                               else
                               {
                                    if (outer.selectSingleNode(xgData, "/root/success") != null && outer.selectSingleNode(xgData, "/root/success") != undefined)
                                    {
                                        outer.UpdateBookmarkSpan(outer.eventid);
                                        var bmCount = $("bmCount");
                                        if (bmCount != null)
                                        {
                                            var bookmarkCount =  outer.selectSingleNode(xgData, "/root/count")
                                            if (bookmarkCount != null)
                                            {
                                                bmCount.innerHTML =  bookmarkCount.text || bookmarkCount.textContent;
                                            }
                                        }
                                       outer.CreateBookmarkCookie(outer.eventid);
                                    }
                                    else
                                    {
                                        showProcessing(true, outer.eventid, "bkmk");
                                        var errMsg = outer.selectSingleNode(xgData, "/root/errorMsg");
                                        if (errMsg != null)
                                        {
                                            errMsg = (errMsg.text || errMsg.textContent);
                                            if (errMsg != null && errMsg != undefined)
                                            {
                                                var currBkmkSpan = $("bkmkspan"+ outer.eventid);
                                                if (currBkmkSpan != null && currBkmkSpan != undefined)
                                                {
                                                var eventtitle = currBkmkSpan.getAttribute("eventtitle");
                                                if (eventtitle != null && eventtitle != undefined)
                                                        alert(errMsg.replace("{0}", eventtitle)); 
                                                }        
                                                
                                            }
                                        }
                                    }
                               }
                            }    
				        }catch(e){
				            alert(e.message);
				            reportException(e,outer.target);
				        }
				        rendered = true;
				    }
		        }
		        else
		        {
		            
		        }
                
                
               
		        if(outer.callback && eval("window."+outer.callback) && log)
                {
                    eval(outer.callback+"(e || outer.e || event || outer.sender, log)");
                }
		    }
		} //virtual will only work here in IE7
		req.send(params.replace(/\;/g,'&'));
    }
    
    this.UpdateBookmarkSpan = function(eventid)
    {
        var currBkmk = $("bkmkimg"+eventid);
        var currBkmkSpan = $("bkmkspan"+eventid);
                                        
        if (currBkmk != null && currBkmkSpan != null)
        {
              currBkmk.src = "resources/images/bkmkselected.gif";
              var currBkmkTitle = $("bkmkalreadytitle");
              if (currBkmkTitle != null)
                      currBkmk.alt = currBkmkTitle.getAttribute("bkmkalreadytitle") ;

               currBkmkSpan.innerHTML = currBkmk.parentNode.innerHTML;
                        
        }
    }
   
    this.CheckForBookMarkCookie = function(e)
    {
        var bkmkCookieValue = getCookie("bkmkid");
        if (bkmkCookieValue != null && bkmkCookieValue != undefined && bkmkCookieValue.length > 0)
        {
            removeCookie("bkmkid");
            if ($("lblemail") != null)
            {
                $("invalidemailtxt").style.display = "none";
                oneclick.Bookmark.invoke(e, "afterOneClick", this, bkmkCookieValue);
            }
            else if ($("invalidemailtxt") != null)
            {
                $("invalidemailtxt").style.display = "block";
            }
        }
    }
    
}

function afterOneClick(e, log)
{
    if(e && log)
    {
        log.endTime = new Date().getTime();//.toGMTString();
        log.entry(e,_(e));
    }
}

function showProcessing(yes, eventid,imgname)
{
    var img = $("bkmkimg"+eventid)
    img.src = "resources/images/"+imgname+".gif";
}
var oneclick = new OneClick();