﻿// JScript File
 
function CheckURLAccess(url,chid,catid,nl)
{               
    /*if(HasToolbar())
    {                
        window.open(url,'_self');
        return;
    }
   else
   {*/        
   
        var strChID=""; 
        var strCatID="";
        var strnl="";
        if(nl!=undefined)
        {
            strnl = "&nl=" + nl;
        }
        if(chid!=undefined)
        {
            strChID = "&chid=" + chid;
        }
        if(catid!=undefined)
        {
            strCatID = "&catid=" + catid;
        }
        
        var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
        var xmlhttp;
        var clickedurl = url;
         
        if(window.XMLHttpRequest) 
        {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }                    
        url = "http://" + location.host + "/netpass/checkaccess?action=donetpass" + strChID +  strCatID + strnl + "&url=" + url + "&aff=0";
        
        xmlhttp.open("GET",url,true);            
        xmlhttp.setRequestHeader("Content-Type", contentType);
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 1) 
            {
                //document.getElementById('content').innerHTML="loading..";
            }
            if (xmlhttp.readyState == 4) 
            {
                var docx=xmlhttp.responseXML;                                        
                var MsgNum = docx.getElementsByTagName('MessageNum')[0].firstChild.nodeValue;
                var strUrl;
                var Width;
                var Height;
                var bc;
                
                //var AccessType = docx.getElementsByTagName('accesstype')[0].firstChild.nodeValue;                             
                                                     
                if(MsgNum=="-1" || MsgNum=="-3")
                {
                    strUrl = docx.getElementsByTagName('Url')[0].firstChild.nodeValue;
                    Width = docx.getElementsByTagName('width')[0].firstChild.nodeValue;
                    Height = docx.getElementsByTagName('height')[0].firstChild.nodeValue;  
                    bc = docx.getElementsByTagName('brandcode')[0].firstChild.nodeValue;
                    
                    if(IsSubscribed(bc))
                    {                            
                        window.open(clickedurl,"_self");
                    }
                    else
                    {
                        var cookievalue = readCookie("subscribe");
                        if(cookievalue!=null)
                        {
                            strUrl = strUrl + "&cookievalue=" + cookievalue;
                        }
                        window.open(strUrl,'_self','width=' + Width + ',height=' + Height);
                    }
                }
                else if(MsgNum=="0")
                {
                    strUrl = docx.getElementsByTagName('Url')[0].firstChild.nodeValue;
                    Width = docx.getElementsByTagName('width')[0].firstChild.nodeValue;
                    Height = docx.getElementsByTagName('height')[0].firstChild.nodeValue;
                    var URLParam;
                    URLParam = strUrl.substring(strUrl.indexOf("&redir=")+7)                        
                    //window.open(URLParam,"_self");
                    //window.open(strUrl,'_blank','width=' + Width + ',height=' + Height);                     
                }
                else if(MsgNum=="3")
                {
                    strUrl = docx.getElementsByTagName('Url')[0].firstChild.nodeValue;   
                    if(window.document.URL.indexOf("newssearch")>-1 && navigator.userAgent.indexOf("MSIE")>-1)
                    {
                        window.open(strUrl,"_blank");                            
                    }
                    else
                    {
                        window.open(strUrl,"_self");
                    }
                }
                else if(MsgNum=="-2")
                {
                    strUrl = docx.getElementsByTagName('Url')[0].firstChild.nodeValue;
                    Width = docx.getElementsByTagName('width')[0].firstChild.nodeValue;
                    Height = docx.getElementsByTagName('height')[0].firstChild.nodeValue;
                    //window.open(strUrl,'_blank','width=' + Width + ',height=' + Height); 
                    window.open(clickedurl,"_self");                     
                }
                else if(MsgNum=="1" || MsgNum=="2")
                {                        
                    window.open(clickedurl,"_self");                     
                }
                else
                {                        
                    alert(MsgNum);
                    window.open(clickedurl,"_self"); 
                }                    
            }
        }
        xmlhttp.send(url);
    //}
}

function IsSubscribed(bc)
{
    var subscribePub = readCookie("subscribe");                          
    var subscriber = false;    
    if(subscribePub!=null)
    {
        var ca = subscribePub.split('|');
	    for(var i=0;i < ca.length;i++)
	    {
		    var c = ca[i];
	        if(bc==c)
            {          
                subscriber = true;
                return subscriber;
            }	
	    }
	}	       
    return subscriber;
}

function HasToolbar()
{
    var navInfo = navigator.userAgent; 
    //check if it has the toolbar
    if(navInfo.indexOf("Congoo NetPass") != -1 || navInfo.indexOf("Congoo NetPass)") != -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}