//alert("TEST!!! 1");

var reflst = new Array();  //referrer list
var reflog;                //Whole Cookie
var ppclog;                //Whole Cookie

//BASE 64
  var origkeyStr = "ABCDEFGHIJKLMNOP" + 
                   "QRSTUVWXYZabcdef" + 
                   "ghijklmnopqrstuv" + 
                   "wxyz0123456789+/" + 
                   "="; 

  var keyStr = "ABCDEFGHIJKLMNOP" + 
               "QRSTUVWXYZabcdef" + 
               "ghijklmnopqrstuv" + 
               "wxyz0123456789-/" + 
               "_"; 

  
  function encode64(finput) { 
     var bt,input;
     //input = escape(input); 
     bt =  Base64_utf8_encode(finput);
     if ( bt.length == finput.length )
     {
	//already UTF8 encoded.
	input = finput;
     }else{
	input = bt;
     }
    

     var output = ""; 
     var chr1, chr2, chr3 = ""; 
     var enc1, enc2, enc3, enc4 = ""; 
     var i = 0; 
  
     do { 
        chr1 = input.charCodeAt(i++); 
        chr2 = input.charCodeAt(i++); 
        chr3 = input.charCodeAt(i++); 
  
        enc1 = chr1 >> 2; 
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 
        enc4 = chr3 & 63; 
   
        if (isNaN(chr2)) { 
           enc3 = enc4 = 64; 
        } else if (isNaN(chr3)) { 
           enc4 = 64; 
        } 
  
        output = output + 
           keyStr.charAt(enc1) + 
           keyStr.charAt(enc2) + 
           keyStr.charAt(enc3) + 
           keyStr.charAt(enc4); 
        chr1 = chr2 = chr3 = ""; 
        enc1 = enc2 = enc3 = enc4 = ""; 
     } while (i < input.length); 

	output = escape(output);
     return output; 
  } 
  
  function decode64(input) { 
     var output = ""; 
     var chr1, chr2, chr3 = ""; 
     var enc1, enc2, enc3, enc4 = ""; 
     var i = 0; 
  
     // remove all characters that are not A-Z, a-z, 0-9, +, /, or = 
     //var base64test = /[^A-Za-z0-9\+\/\=]/g;
     var base64test = /[^A-Za-z0-9\-\/\_]/g; 
     if (base64test.exec(input)) { 
        alert("There were invalid base64 characters in the input text.\n" + 
              "Valid base64 characters are A-Z, a-z, 0-9, '-', '/',and '_'\n" + 
              "Expect errors in decoding."); 
     } 
     //input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 
     input = input.replace(/[^A-Za-z0-9\-\/\_]/g, ""); 
  
     do { 
        enc1 = keyStr.indexOf(input.charAt(i++)); 
        enc2 = keyStr.indexOf(input.charAt(i++)); 
        enc3 = keyStr.indexOf(input.charAt(i++)); 
        enc4 = keyStr.indexOf(input.charAt(i++)); 
  
        chr1 = (enc1 << 2) | (enc2 >> 4); 
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 
        chr3 = ((enc3 & 3) << 6) | enc4; 
  
        output = output + String.fromCharCode(chr1); 
  
        if (enc3 != 64) { 
           output = output + String.fromCharCode(chr2); 
        } 
        if (enc4 != 64) { 
           output = output + String.fromCharCode(chr3); 
        } 
  
        chr1 = chr2 = chr3 = ""; 
        enc1 = enc2 = enc3 = enc4 = ""; 
  
     } while (i < input.length); 
  
     return Base64_utf8_decode(output); 
  } 

  function Base64_utf8_encode(string) { 
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);
		//alert(c);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {

		//alert("C2! = " + c );
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
		//alert("C3! = " + c );
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }
        return utftext;
    }


  function Base64_utf8_decode(utftext) { 
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }


//BASE 64

function decryptXOR(CodeKey, DataIn)
{
	var strDataOut = "";
	var codechar = "";
	for(ii=0;ii<DataIn.length;ii++)
	{
		//codechar = CodeKey.charCodeAt((ii%CodeKey.length));
		codechar = CodeKey.charCodeAt(((ii+1)%CodeKey.length));
		strDataOut+=String.fromCharCode(codechar^DataIn.charCodeAt(ii));
	}
        return strDataOut;
}


function html_entity_decode(str)
{
    //jd-tech.net
    var  tarea=document.createElement('textarea');
    tarea.innerHTML = str; return tarea.value;
    tarea.parentNode.removeChild(tarea);
}

////////////////////////////////////////////////////////////////////////

//var d = new Date();
//print(ISODateString(d));

function ISODateString(d) {
    function pad(n){
        return n<10 ? '0'+n : n
    }
    return d.getUTCFullYear()+'-'
    + pad(d.getUTCMonth()+1)+'-'
    + pad(d.getUTCDate())+' '
    + pad(d.getUTCHours())+':'
    + pad(d.getUTCMinutes())+':'
    + pad(d.getUTCSeconds())+''
}



////////////////////////////////////////////////////////////////////////

function htmlentities (hestr, quote_style) {
    // Convert all applicable characters to HTML entities
    tmp_str = "";

    for (var hei=0;hei<hestr.length;hei++)
    {
	hech = hestr.substr(hei, 1);
        //hecv = hestr.charCodeAt(hei);
        hecv = hech.charCodeAt(0);
	if (  (hecv < 48) || ((hecv > 57) && (hecv < 65)) || (hecv > 122)  )
	{
		tmp_str = tmp_str + '&#' + hecv.toString(10) + ';';
	}else{
		tmp_str = tmp_str + hech;
	}
    }
    //alert(tmp_str + ' == ' + tmp_str.length );
    return tmp_str;
}



////////////////////////////////////////////////////////////////////////
////http://stackoverflow.com/questions/1933602/how-to-getelementbyclass-instead-of-getelementbyid-with-javascript
////////////////////////////////////////////////////////////////////////
function getElementsByClassName(node,classname) {
  if (node.getElementsByClassName) { // use native implementation if available
    return node.getElementsByClassName(classname);
  } else {
    return (function getElementsByClass(searchClass,node) {
        if ( node == null )
          node = document;
        var classElements = [],
            els = node.getElementsByTagName("*"),
            elsLen = els.length,
            pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

        for (i = 0, j = 0; i < elsLen; i++) {
          if ( pattern.test(els[i].className) ) {
              classElements[j] = els[i];
              j++;
          }
        }
        return classElements;
    })(classname, node);
  }
}








////////////////////////////////////////////////////////////////////////
// http://icodesnip.com/search/detecter%20le/
var smooth_timer;

function getBrowser(){
var BrowserDetect = {
  init: function () {
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    this.version = this.searchVersion(navigator.userAgent)
            || this.searchVersion(navigator.appVersion)
            || "an unknown version";
    this.OS = this.searchString(this.dataOS) || "an unknown OS";
  },
  searchString: function (data) {
    for (var i=0;i<data.length;i++) {
      var dataString = data[i].string;
      var dataProp = data[i].prop;
      this.versionSearchString = data[i].versionSearch || data[i].identity;
      if (dataString) {
        if (dataString.indexOf(data[i].subString) != -1)
          return data[i].identity;
      }
      else if (dataProp)
      return data[i].identity;
    }
  },
  searchVersion: function (dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index == -1) return;
    //return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    var tmpss1 = index+this.versionSearchString.length+1;
    var tmpss2 = dataString.substring(tmpss1);
    var tmpss3 = parseFloat(tmpss2);
    return tmpss3
  },
  dataBrowser: [
          {       string: navigator.userAgent,
                  subString: "Chrome",
                  versionSearch: "Chrome",
                  identity: "chrome"
          },
          {       string: navigator.userAgent,
                  subString: "OmniWeb",
                  versionSearch: "OmniWeb/",
                  identity: "OmniWeb"
          },
          {
                  string: navigator.vendor,
                  subString: "Apple",
                  identity: "Safari"
          },
          {
                  prop: window.opera,
                  identity: "Opera"
          },
          {
                  string: navigator.vendor,
                  subString: "iCab",
                  identity: "iCab"
          },
          {
                  string: navigator.vendor,
                  subString: "KDE",
                  identity: "Konqueror"
          },
          {
                  string: navigator.userAgent,
                  subString: "Firefox",
                  identity: "Firefox"
          },
          {
                  string: navigator.vendor,
                  subString: "Camino",
                  identity: "Camino"
          },
          {               // for newer Netscapes (6+)
                  string: navigator.userAgent,
                  subString: "Netscape",
                  identity: "Netscape"
          },
          {
                  string: navigator.userAgent,
                  subString: "MSIE",
                  identity: "Explorer",
                  versionSearch: "MSIE"
          },
          {
                  string: navigator.userAgent,
                  subString: "Gecko",
                  identity: "Mozilla",
                  versionSearch: "rv"
          },
          {               // for older Netscapes (4-)
                  string: navigator.userAgent,
                  subString: "Mozilla",
                  identity: "Netscape",
                  versionSearch: "Mozilla"
          }
  ],
  dataOS : [
          {
                  string: navigator.platform,
                  subString: "Win",
                  identity: "Windows"
          },
          {
                  string: navigator.platform,
                  subString: "Mac",
                  identity: "Mac"
          },
          {
                  string: navigator.platform,
                  subString: "Linux",
                  identity: "Linux"
          }
  ]
};
  BrowserDetect.init();
  //return BrowserDetect.browser;
  return BrowserDetect;
}






///// http://blog.firetree.net/2005/07/04/javascript-find-position/
  function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosYOld(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function findPosY(obj) {
	yPos = obj.offsetTop;
	tempEl = obj.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}




//////////////////////////////////////////////////////
////////////////////// C is 4 cookie /////////////////
function createCookie(name,value,days) {
	try
	{

		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	catch (e)
	{
		idontcare = true;
	}
}

function readCookie(name) {
	try
	{
		var nameEQ = name + "=";
		//alert(document.cookie);
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	catch (e)
	{
		idontcare = true;
		return null;
	}
}

function eraseCookie(name) {
	createCookie(name,"",-1);

}

function cookNav(cnlevel, cnlink, cntext, cncode) {

	var maxlev = 6;
	var myLevels=new Array();
	var cntarget = "navspace";
	var cnoutput = "";

	if (document.domain.indexOf("store.") >= 1) return;
	if (document.domain.indexOf("secure.") >= 1) return;


	if ( cncode === undefined ) {
		cncode = 'XXXX';
	}


	//make Home Page level 0...
	if (cnlink == "/")
	{
		cnlevel = 0;
	}

	//Erase cookies below
	for(var i=cnlevel;i <= maxlev;i++)
	{
		cntmp = "level" + i;
		eraseCookie(cntmp);
		myLevels[i] = "";
	}
	//Read cookies above
	for(var i=cnlevel;i >= 0;i--)
	{
		cntmp = "level" + i;
		myLevels[i] = readCookie(cntmp);
	}

	//Set Current
	cnlevel += "";		//Explicit conversion to string.
	cntmp = "level" + cnlevel;
	cnlevel =  parseInt(cnlevel);		//Explicit conversion to numeric.
	//myLevels[cnlevel] = cntext + "||" + cnlink;
	myLevels[cnlevel] = "b64x" + encode64(cntext + "||" + cnlink + "||" + cncode);
	createCookie(cntmp, myLevels[cnlevel],2);


	//Draw nav HTML
	if (cnlevel >= 0)
	{
		navcnt = 0;
		for(var i=0;i <= maxlev;i++)
		{

			if (  (myLevels[i]) && (myLevels[i].length > 2) )
			{
				if (cnoutput.length > 1)
				{ 
					cnoutput += " &raquo; ";
				}
				cntemp = myLevels[i];
				if ((cntemp.length > 4) && (cntemp.substring(0,4) == "b64x"))
				{
					cntemp = decode64( cntemp.substring(4) );
				}
				cnparts = cntemp.split("||"); 


				cnoutput += " <a href='" + cnparts[1] + "'>" + cnparts[0] + "</a> ";
				navcnt++;
			}
		}
		if (navcnt > 1)
		{
			var ele = document.getElementById(cntarget);
			if (ele)
			{
				ele.innerHTML = cnoutput;
			}else{
				//alert(" ?!@#*! ");
			}
		}
	}

	//return true;
}

function cookRef() {
         //var reflst = new Array();
         var d = new Date();
         //var reflog = readCookie("reflog");
         reflog = readCookie("reflog");
	 ppclog = readCookie("ppclog");

         var reflin = '';
         var refurl = '';
         var thsurl = '';
	 var seplst = '___';
	 var sepitm = '@@@';

	 //time@@@ref@@@current-url___ 

	 if (document.referrer) refurl = escape(document.referrer);


	 if ( (reflog) && (reflog.length) && (reflog.length >= 10) )
         {
              reflst = reflog.split(seplst, 15);
         }

         //print();
         if ( (refurl) && (refurl.length) && (refurl.length >= 10) && (document.domain) && (refurl.substr(0,55).indexOf(document.domain) <= 1) )
         {
		if (window.location.href) thsurl = escape(window.location.href); 

		reflin = ISODateString(d) + sepitm + refurl + sepitm + thsurl ;
		reflog = reflin;


                var ppc_param = gup('ppc');
         	if (ppc_param.length <= 1) ppc_param = gup('gclid');	//google does this automatically

         	if (ppc_param.length >= 1)
	 	{
			//ppclog = readCookie("ppclog");
			if ( (ppclog) && (ppclog.length) && (ppclog.length >= 10) )
			{
				ppclog = seplst + ppclog;
			}else{
				ppclog = "";
			}

			//Create separate PPC cookie
			ppclog = reflin + ppclog ;
			createCookie("ppclog", ppclog, 120);
	 	}


		for (cri=0;cri<reflst.length;cri++)
		{
			//2011-12-13
			if (cri > 3) break; 
			if ((reflog) && (reflog.length > 2424)) break;  

			if ( (reflst[cri]) && (reflst[cri].length > 1) )
			{
				reflin = reflst[cri];
				reflog = reflog + seplst + reflin;
			}
		}

	 	createCookie("reflog", reflog, 120);

        	//recalc reflst for access by rest of page.
		if ( (reflog) && (reflog.length) && (reflog.length >= 10) )
         	{
             	 reflst = reflog.split(seplst, 15);
         	}
	 }
}


function gup( name )
{
  //parse URL paramters
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


//////////////////////////////////////////////////////


