﻿var exclude=1;
var agt=navigator.userAgent.toLowerCase();
var win=0;var mac=0;var lin=1;
if(agt.indexOf('win')!=-1){win=1;lin=0;}
if(agt.indexOf('mac')!=-1){mac=1;lin=0;}
var lnx=0;if(lin){lnx=1;}
var ice=0;
var ie=0;var ie4=0;var ie5=0;var ie6=0;var com=0;var dcm;
var op5=0;var op6=0;var op7=0;
var ns4=0;var ns6=0;var ns7=0;var mz7=0;var kde=0;var saf=0;
if(typeof navigator.vendor!="undefined" && navigator.vendor=="KDE"){
	var thisKDE=agt;
	var splitKDE=thisKDE.split("konqueror/");
	var aKDE=splitKDE[1].split("; ");
	var KDEn=parseFloat(aKDE[0]);
	if(KDEn>=2.2){
		kde=1;
		ns6=1;
		exclude=0;
		}
	}
else if(agt.indexOf('webtv')!=-1){exclude=1;}
else if(typeof window.opera!="undefined"){
	exclude=0;
	if(agt.indexOf("opera/5")!=-1||agt.indexOf("opera 5")!=-1){op5=1;}
	if(agt.indexOf("opera/6")!=-1||agt.indexOf("opera 6")!=-1){op6=1;}
	if(agt.indexOf("opera/7")!=-1||agt.indexOf("opera 7")!=-1){op7=1;}
	}
else if(typeof document.all!="undefined"&&!kde){
	exclude=0;
	ie=1;
	if(typeof document.getElementById!="undefined"){
		ie5=1;
		if(agt.indexOf("msie 6")!=-1){
			ie6=1;
			dcm=document.compatMode;
			if(dcm!="BackCompat"){com=1;}
			}
		}
	else{ie4=1;}
	}
else if(typeof document.getElementById!="undefined"){
	exclude=0;
	if(agt.indexOf("netscape/6")!=-1||agt.indexOf("netscape6")!=-1){ns6=1;}
	else if(agt.indexOf("netscape/7")!=-1||agt.indexOf("netscape7")!=-1){ns6=1;ns7=1;}
	else if(agt.indexOf("gecko")!=-1){ns6=1;mz7=1;}
	if(agt.indexOf("safari")!=-1 || (typeof document.childNodes!="undefined" && typeof document.all=="undefined" && typeof navigator.taintEnabled=="undefined")){mz7=0;ns6=1;saf=1;}
	}
else if((agt.indexOf('mozilla')!=-1)&&(parseInt(navigator.appVersion)>=4)){
	exclude=0;
	ns4=1;
	if(typeof navigator.mimeTypes['*']=="undefined"){
		exclude=1;
		ns4=0;
		}
	}
if(agt.indexOf('escape')!=-1){exclude=1;ns4=0;}
if(typeof navigator.__ice_version!="undefined"){exclude=1;ie4=0;}

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function SetCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function GetCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function DeleteCookie(name, path, domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixdate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function GetUsrs()
{
	var usrs = new Array();
	var idx = y001usr.substring(0,2);
	var j = 0;
	while (idx != '')
	{
	    if (idx.substring(0,1) == '0')
		    idx = idx.substring(1);
		var usr0 = '';
		for (var i = 0; i < parseInt(idx); i++)
		{
			j += 2;
			usr0 += y001usr.substring(j, j+2);
		}
		var usr = ConvertPwd(usr0);
        usrs[usrs.length] = usr;
		j += 2;
		idx = y001usr.substring(j,j+2);
	}
	return(usrs);
}

function GetPwds()
{
	var pwds = new Array();
	var idx = y001kb.substring(0,2);
	var j = 0;
	while (idx != '')
	{
	    if (idx.substring(0,1) == '0')
		    idx = idx.substring(1);
		var pwd0 = '';
		for (var i = 0; i < parseInt(idx); i++)
		{
			j += 2;
			pwd0 += y001kb.substring(j, j+2);
		}
		var pwd = ConvertPwd(pwd0);
        pwds[pwds.length] = pwd;
		j += 2;
		idx = y001kb.substring(j,j+2);
	}
	return(pwds);
}

function ConvertPwd(pwd0)
{
    var pwd = "";
	for (var i1 = 0; i1 < pwd0.length; i1 += 2)
    {
       var h1 = pwd0.charAt(i1);
       var h2 = pwd0.charAt(i1 + 1);
       c = String.fromCharCode(parseInt(h1 + h2,16) - 1);
       pwd += c;
    }
    return (pwd);
}

function CheckPwd(pwd)
{
    var pwds = GetPwds();
    for (var i = 0; i < pwds.length; i++)
    {
        if (pwd == pwds[i])
        {
            var usrs = GetUsrs();
            if (typeof(usrs[i]) != '')
            {
                SetCookie("loggedinusr",usrs[i]);
            }
            else
            {
                DeleteCookie("loggedinusr");
            }
            return(true);
        }
    }
    return(false);
}

function CheckPassword()
{
    pwd = GetCookie("passphrase");
    if (!pwd)
    {
        var pwd = prompt("Enter password:","");
        if (!pwd || pwd == '')
        {
            alert("Access denied");
            return;
        }
        if (!CheckPwd(pwd))
        {
            alert("Sorry, invalid password");
            return;
        }
        else
        {
            SetCookie("passphrase",pwd);
            return(true);
        }
    }
    else
    {
        if (!CheckPwd(pwd))
        {
            alert("Sorry, invalid password");
            DeleteCookie("passphrase");
            DeleteCookie("loggedinusr");
            return(false);
        }
        return(true);
    }
}

var encodedSource = "";
var Generating = false;
var HTMLResult = "";
var currentIndex = 0;
var generatorTI;

function generateHTML()
{
    if (Generating)
        return;
    
    Generating = true;
    if (currentIndex >= encodedSource.length)
    {
        Generating = false
        self.clearTimeout(generatorTI);
        var e = document.getElementById("encodedBody");
        if (e)
        {
            e.innerHTML = HTMLResult;
        }
        return;
    }    
    else
    {
        self.clearTimeout(generatorTI);
        var nextJ = currentIndex + 7500;
        for (j = currentIndex; j < nextJ && j < encodedSource.length; j +=2, currentIndex +=2)
        {
            //e.innerHTML += currentIndex + "<br>"
            var h1 = encodedSource.charAt(j);
		    var h2 = encodedSource.charAt(j+1);
		    HTMLResult += (String.fromCharCode(parseInt(h1 + h2,16)));
        }
    }
    generatorTI = self.setTimeout(generateHTML, 1);
    Generating = false;
}
function ydecode(src)
{
    if (CheckPassword())
    {
        //if (!ie)
        {
	        for (var i = 0; i < src.length; i += 2)
	        {
		        var h1 = src.charAt(i);
		        var h2 = src.charAt(i+1);
		        document.write (String.fromCharCode(parseInt(h1 + h2,16)));
	        }
	    }
	    /*
	    else
	    {
            var e = document.getElementById("encodedBody");
            if (e && src.length > 10000)
            {
                e.innerHTML = "Please wait...";
            }
	        encodedSource = src;
	        generatorTI = self.setTimeout(generateHTML, 1);
	    }
	    */
    }
    else
    {
        // not logged in or invalid password
    }
}

function SetPassword()
{
    var frm = document.getElementById("loginform");
    if (frm)
    {
        var pwd = frm.login.value;
        if (pwd == '')
        {
            alert("Please enter your password");
            frm.login.focus();
        }
        else
        {
            SetCookie("passphrase",pwd);
            if (CheckPassword())
            {
                document.location.reload();
            }
        }
    }
}

function Logout()
{
    DeleteCookie("loggedinusr");
    DeleteCookie("passphrase");
    document.location.reload();
}

function ShowLogout()
{
    if (CheckPwd(GetCookie("passphrase")))
    {
        var usr = GetCookie("loggedinusr");
        document.write("<br>Logged in (" + usr + ")<br>");
        document.write("<a href=\"javascript:void(0)\" onclick=\"Logout()\">Logout</a>");
    }
    else
    {
        document.write("<style>.salespr { display: none }</style>");
    }
}

function canShow(src, altsrc)
{
    /* decodes content but only if logged in */
    if (CheckPwd(GetCookie("passphrase")))
    {
        for (var i = 0; i < src.length; i += 2)
        {
	        var h1 = src.charAt(i);
	        var h2 = src.charAt(i+1);
	        document.write (String.fromCharCode(parseInt(h1 + h2,16)));
        }
    }
    else if (altsrc)
    {
        for (var i = 0; i < altsrc.length; i += 2)
        {
	        var h1 = altsrc.charAt(i);
	        var h2 = altsrc.charAt(i+1);
	        document.write (String.fromCharCode(parseInt(h1 + h2,16)));
        }
    }
}

function whenReferred(src)
{
    if (!CheckPwd(GetCookie("passphrase")) && (GetCookie("Referrer") && GetCookie("Referrer") != ""))
    {
        for (var i = 0; i < src.length; i += 2)
        {
	        var h1 = src.charAt(i);
	        var h2 = src.charAt(i+1);
	        document.write (String.fromCharCode(parseInt(h1 + h2,16)));
        }
    }
}

function ReferrerSwitch(loggedinText,notloggedinReferred,notloggedinNotReferred)
{
    var src;
    
    if (CheckPwd(GetCookie("passphrase")))
    {
        src = loggedinText;
    }
    else if (!GetCookie("Referrer") || GetCookie("Referrer") == "")
    {
        src = notloggedinNotReferred;
    }
    else
    {
        src = notloggedinReferred;
    }
    
    for (var i = 0; i < src.length; i += 2)
    {
        var h1 = src.charAt(i);
        var h2 = src.charAt(i+1);
        document.write (String.fromCharCode(parseInt(h1 + h2,16)));
    }
}

// save the referrer
if (GetCookie("ReferrerSet") != "yes")
{
    SetCookie("ReferrerSet", "yes");
    var ref = document.referrer;
    
    // see if referrer is a search engine
    var isSearchEngine = false;
    for (var i = 0; i < searchEngines.length; i++)
    {
        if (ref.indexOf(searchEngines[i]) >-1 )
        {
            isSearchEngine = true;
            break;
        }
    }
    
    if (isSearchEngine)
    {
        // remove direct searches
        var referrerException = false;
        for (var i = 0; i < referrerExceptions.length; i++)
        {
            if (ref.indexOf(referrerExceptions[i])>-1)
            {
                referrerException = true;
                break;
            }
        }
        if (referrerException)
            SetCookie("Referrer","");
        else
            SetCookie("Referrer", document.referrer);
    }
    else
    {
        SetCookie("Referrer","");
    }
}

function removeSalesPrs()
{
    if (!CheckPwd(GetCookie("passphrase")))
    {
        var salesprs = document.getElementsByClassName("salespr");
        if (salesprs)
        {
            var nodes = new Array();
            for ( var i = 0; i < salesprs.length; i++)
            {
                nodes[nodes.length] = salesprs[i];
            }
            for ( var i = 0; i < nodes.length; i++)
            {
                if (nodes[i].removeNode)
                    nodes[i].removeNode(true);
                else
                    nodes[i].parentNode.removeChild(nodes[i]);
            }
        }
    }
}
