
var DisclaimerPopup = new Class(
{

    initialize: function()
    {
        this.queue = [ ];  
        var me = this;
        window.addEvent('domready', function()
        {
            me.container = me.addMarkup();
            me.showFromQueue();
        });
    },
    
    addMarkup: function()
    {
        this.container = $(document.createElement("div"));
        this.container.innerHTML = this.markup;
        document.body.appendChild(this.container);
        $("modal_mask").setOpacity(0.6);
        $("modal_mask").style.display = "none";
        this.reposition();
        window.addEvent("resize", this.reposition.bind(this));
    },
    
    hide: function()
	{
        $$(".flash").each(function(applet)
        {
            applet.style.visibility = "visible";
        });
		
        $("modal_mask").style.display = "none";
		$("disclaimer_popup").style.visibility = "hidden";
		$("disclaimer_content").style.visibility = "hidden";
		$("disclaimer_frame1").style.visibility = "hidden";
		$("disclaimer_frame").src = "";
        this.showFromQueue();
	},
    
    showFromQueue: function()
    {
        if(this.queue && this.queue.length > 0)
        {
            var options = this.queue.shift();
            this.show(options);
        }
    },
    
    showOnLoad: function(options)
    {
        this.queue.push(options);
    },
    
    show: function(options)
	{
        $$(".flash").each(function(applet)
        {
            applet.style.visibility = "hidden";
        });
        //this frame is behind the disclaimer frame
     	var popupframe1 = $("disclaimer_frame1");
     	popupframe1.style.display = "inline-block"; 
 	    popupframe1.style.left =(this.getViewportWidth() / 2) - (popupframe1.offsetWidth / 2) + "px";
 		
 		
		options.onAccept = options.onAccept || function() { };
		options.onCancel = options.onCancel || function() { };
		
		var me = this;
        $("disclaimer_accept").src = options.acceptButton;
        $("disclaimer_cancel").src = options.cancelButton;
		$("disclaimer_frame").src = options.url
		
        DisclaimerPopup.cancel = function()
		{
			me.hide();
			options.onCancel();
		};
		
		DisclaimerPopup.accept = function()
		{
		   // user accepte disclaimer create cookie
            var path = "/";
            var expiredays =  1;
            var exdate = new Date();
            var c_name = "LMI_SITE_DISCLAIMER";
            var value = "yes" ;
 
            //alert("DisclaimerPopup.accept");
            exdate.setDate(exdate.getDate()+ expiredays);
 
            document.cookie = c_name+ "=" + escape(value)+   ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ((path) ? "; path=" + path : "");
 
			me.hide();
			options.onAccept();
			
		};
		
        if(options.className)
        {
            $("disclaimer_popup").className += (" " + options.className);
        }
        
		$("disclaimer_popup").style.visibility = "visible";
		$("disclaimer_content").style.visibility = "visible";
		$("modal_mask").style.display = "block";
	},
    
    reposition: function()
	{
		// reposition popup
		var popup = $("disclaimer_popup");
	
		popup.style.left = Math.max(10, document.documentElement.scrollLeft + 
			(this.getViewportWidth() / 2) - (popup.offsetWidth / 2)) + "px";
		popup.style.top = Math.max(10, document.documentElement.scrollTop + 
			 (this.getViewportHeight() / 2) - (popup.offsetHeight / 2)) + "px";
	  		 
		// reposition modal mask
		var mask = $("modal_mask");
		mask.style.width = Math.max(this.getViewportWidth(), document.body.scrollWidth) + "px";
		mask.style.height = Math.max(this.getViewportHeight(), document.body.scrollHeight) + "px";
	},
    
    getViewportHeight: function()
	{
		if(window.innerHeight!=window.undefined) return window.innerHeight;
		if(document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
		if(document.body) return document.body.clientHeight; 
		return window.undefined; 
	},

	getViewportWidth: function() 
	{
		if(window.innerWidth!=window.undefined) return window.innerWidth; 
		if(document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
		if(document.body) return document.body.clientWidth; 
		return window.undefined; 
	},
    // this frame disclaimer_frame1 is behind the popup disclaimer
    // thi frame is used to solve the problem on newsletter iframe for PDFs. 
    markup: 
    '<iframe id="disclaimer_frame1"  class="disclaimer_frame1" visible="true" frameborder="0" src="" ' +
				'></iframe>' +
	'<div id="modal_mask" class="modal_mask"></div>' +
	'<div id="disclaimer_popup" class="disclaimer_popup">' + 
		'<div id="disclaimer_content" class="disclaimer_content">' +
			'<iframe id="disclaimer_frame" class="disclaimer_frame" frameborder="0" src="" ' +
				'style="border:none;"></iframe>' +
		'</div>' +
        '<div class="disclaimer_buttons">' +
            '<a href="javascript:DisclaimerPopup.accept();"><img id="disclaimer_accept" src="" alt="Accept" /></a> ' +
            '<a href="javascript:DisclaimerPopup.cancel();"><img id="disclaimer_cancel" src="" alt="Cancel" /></a> ' +
        '</div>' +
	'</div> '
});


/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */


var SiteDisclaimer = new Class(
{
    initialize: function()
	{

		var me = this;
		window.addEvent('domready', function()
		{
		     // 02-17-2011 -- DELETE old usertype cookie from users browsers 
		     var userType = Cookie.read("LMI_USERTYPE") || null;		     
		     if  (userType != null  && userType ==  "Financial Advisors and Professionals" )
            {                 
                  var name = "LMI_USERTYPE";  
                  var path = "/";
 
                  document.cookie = name+ '=;expires=Thu, 01-Jan-70 00:00:01 GMT;' + ((path) ? "; path=" + path : "");                   
                  document.cookie = name+ "=" + escape("Intermediaries")+    ((path) ? "; path=" + path : ""); 
            }
            if  (userType != null  && userType ==  "Asset Managers" )
            {                 
                  var name = "LMI_USERTYPE";  
                  var path = "/";
 
                  document.cookie = name+ '=;expires=Thu, 01-Jan-70 00:00:01 GMT;' + ((path) ? "; path=" + path : "");                   
                  document.cookie = name+ "=" + escape("Intermediaries")+    ((path) ? "; path=" + path : "");
            }
            
			var links = $$("a");
			for(var i=0;i<links.length;i++) 
			{
				if(links[i].hasClass("remote"))
				{                                        
					links[i].onclick = me.change.bind(me).pass(links[i]);
				}
			}
		});
	},
    
    require: function(cancelURL)
	{

        if(Cookie.read("LMI_SITE_DISCLAIMER")) 
        {    
            return; 
        }
   
        var query = window.location.search.replace(/^\?/,"").split("&");
        for(var i=0;i<query.length;i++)
        {
            if(query[i].toLowerCase() == "disclaimer=suppress") { return; }
        }
        			
        var language = Cookie.read("LMI_LANG") || false;        
        var userType = Cookie.read("LMI_USERTYPE") || null;
        
        if  (userType == null)
        {
            //alert("usertype is null " + cancelURL);
             window.location = cancelURL; 
        }
        var disclaimerBase = 
            (language ? ("/" + language.toLowerCase()) : "") +
            "/disclaimer" +            
            (userType ? ("/" + userType.replace(/\s/g, "_")) : "");
        var disclaimerURL = disclaimerBase + "/entering.aspx";        
			 //alert("::userType::" + userType);
            //alert (disclaimerURL);
        Disclaimer.showOnLoad(
		{ 
		
			url: disclaimerURL,
            acceptButton: (disclaimerBase + "/accept.gif"),
            cancelButton: (disclaimerBase + "/cancel.gif"),
			onCancel: function() { window.location = cancelURL; },
			onAccept: function()
			{
 
//				 Cookie.write("LMI_SITE_DISCLAIMER", "yes", 
//				 	{ path: "/", duration: false });
	 
                // user accept disclaimer create cookie. this is required for .net to read the cookie
                var path = "/";
                var expiredays =  1;
                var exdate = new Date();
                var c_name = "LMI_SITE_DISCLAIMER";
                var value = "yes" ;
 
                exdate.setDate(exdate.getDate()+ expiredays);
                document.cookie = c_name+ "=" + escape(value)+   ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ((path) ? "; path=" + path : "");
                   
                var serialCookieName = "LMI_" + 
                    (language ? ("_" + language.toLowerCase()) : "") +
                    (userType ? ("_" + userType.replace(/\s/g, "_").toLowerCase()) : "").replace(/_+/, "_");
     
                var path = "/";
                var expiredays =  1;
                var exdate = new Date();
                var c_name = serialCookieName;
                var value = "yes" ;
 
                exdate.setDate(exdate.getDate()+ expiredays);
                document.cookie = c_name+ "=" + escape(value)+   ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ((path) ? "; path=" + path : "");
                   
                   
//                Cookie.write(serialCookieName, "yes",
//                    { path: "/", duration: false });
			}
		});
	},
    
    change: function(link)
	{
	 
        var pairs = (link.getAttribute("rel") || "").split("\\");
        //alert("change rel:: " + pairs);
        var params = { };
        for(var i=0;i<pairs.length;i++)
        {
            var match = pairs[i].match(/^([^:]+):(.*)$/);
            if(match)
            {
                params[match[1]] = match[2];
            }
        }
        		
		var href = link.getAttribute("href");
        var host = link.href.match(/^([A-Z]+:\/\/[^\/]+)/i);
       
        //alert ("change href::" + href); 
		var disclaimerBase = 
            (host ? host[1] : "") +
            (params.lang ? ("/" + params.lang.toLowerCase()) : "") +
            "/disclaimer" +            
            (params.type ? ("/" + params.type.replace(/\s/g, "_").toLowerCase()) : "");
        var disclaimerURL = disclaimerBase + "/entering.aspx";
          
         //alert ("disclaimerURL::" + disclaimerURL);    
          
        var language = Cookie.read("LMI_LANG") || false;        
        var userType = Cookie.read("LMI_USERTYPE") || null;
        var leavingBase =
            (language ? ("/" + language.toLowerCase()) : "") +
            "/disclaimer" +            
            (userType ? ("/" + userType.replace(/\s/g, "_").toLowerCase()) : "");
        var leavingURL = leavingBase + "/leaving.aspx"; 
        
        // add confirmation values to the resulting URL
        //remove query string. Per compliance request 10-22-09
//        urlAdditions = [ "disclaimer=yes" ];
//        if(params.lang) { urlAdditions.push("lang=" + params.lang.toLowerCase()); }
//        if(params.type) { urlAdditions.push("user_type=" + params.type.replace(/\s/g, "%20").toLowerCase()); }
//        if(!href.match(/\?/)) { href += "?"; }
//        else if(!href.match(/&$/)) { href += "&"; }
//        href += urlAdditions.join("&");
        
        
        // construct a cookie for this particular site and type        
        var serialCookieName = "LMI_" + 
            (host ? host[1].replace(/[^\w]+/gi, "_").toLowerCase() : "") +
            (params.lang ? ("_" + params.lang.toLowerCase()) : "") +
            (params.type ? ("_" + params.type.replace(/\s/g, "_").toLowerCase()) : "").replace(/_+/, "_");
         
    
       // var alreadyAccepted = Cookie.read(serialCookieName);
       // 10/20/09 Compliance requirement: Users should have to accept the pop-up every time if they change user type
        var alreadyAccepted   = null;
		var showDisclaimer = function()
		{
			Disclaimer.show(
            { 
                url: disclaimerURL,
                acceptButton: (disclaimerBase + "/accept.gif"),
                cancelButton: (disclaimerBase + "/cancel.gif"),
                onAccept: function()
    			{                   
//                    Cookie.write(serialCookieName, "yes",
//                        { path: "/", duration: false });
                //  create serialCookieName cookie.           
                var path = "/";
                var expiredays =  1;
                var exdate = new Date();
                var c_name =  serialCookieName;
                var value = "yes" ;
                //remove expire date from cookies.
                //exdate.setDate(exdate.getDate()+ expiredays);
                //document.cookie = c_name+ "=" + escape(value)+   ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ((path) ? "; path=" + path : "");
				document.cookie = c_name+ "=" + escape(value) + ((path) ? "; path=" + path : "");


                    //  create LMI_USERTYPE cookie. Need to create this cookie using document.cookie because .Net needs to read it
                    var path = "/";
                    var expiredays = 1;
                    var exdate = new Date();
                    var c_name = "LMI_USERTYPE";
                    var value = (params.type ? ( params.type.replace(/\s/g, " ")) : "_") ;
                    //remove expire date from cookies.
                   // exdate.setDate(exdate.getDate()+ expiredays);
                    
                    document.cookie = c_name+ "=" + escape(value)+    ((path) ? "; path=" + path : "");
                 
                    
                    window.location = href;
                }
			});
		};
		
		if(!link.hasClass("warn"))
		{    
		     //alert(" link.hasClass alreadyAccepted::" + alreadyAccepted);
		   
            if(alreadyAccepted)
            {
               //alert("alreadyAccepted link.hasClass :" + href);
                window.location = href;
            }
			else
            {
                showDisclaimer();
            }
		}
		else
		{
			Disclaimer.show(
            { 
                url: leavingURL, 
                acceptButton: (leavingBase + "/proceed.gif"),
                cancelButton: (leavingBase + "/cancel.gif"),
                onAccept: function()
                {
                    if(alreadyAccepted)
                    {
                        window.location = href;
                    }
        			else
                    {
                        showDisclaimer();
                    }
                }
            });
		}
		
		return false;
	}
});


/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */


var Disclaimer = new DisclaimerPopup();
var SiteDisclaimer = new SiteDisclaimer();

