

var bPopupsBlocked; // Will check onUnload()   = IsPopupBlocker();	
            
    // www.the-cool-place.co.uk/javascript/tutorial/javascript1.html 	  
	 	     	

function getBrowserType() {
var sType="";

       	if(document.layers)	sType="NN4";
        if(document.all)  sType="IE";
        if(!document.all && document.getElementById) sType="NN6";
        return sType;
}

var sBrowserType = getBrowserType();

// disable right-click
   document.oncontextmenu=function() { return false; } 


// Just for testing
function doReportXY(e) {
   alert("Here in doReportXY");
   try {alert ("ClientX=" + e.clientX + "   ClientY="+e.clientY); } catch(Ex) {};
   // just bails in NN
   //try {alert ("pageX=" + e.pageX + "   pageY="+e.pageY); } catch(Ex) {};
   try {alert ("x=" + e.x + "   y="+e.y); } catch(Ex) {};
   //try {alert ("screenX=" + e.screenX + "   screenY="+e.screenY); } catch(Ex) {};
   alert("Finished in doReportXY");
}




function doBeforeUnloadOffer(bBackpageActive) {

    // Actually this "Before" event is likely only IE

    // bBackpageActive 
    //  if = false means X,Y thresholds are set to prevent offer launch
    //    E.g. For Checkout page where user may wish to click back-page to Buy page.
    //  if = true means we want offer to launch (this will happen most of time)

     var e = window.event;
     var bRealClose = false;
     
     var  sOffer1 = "______________________________________________________________________________________________";
     var  sOffer2 = "                                                     D O N' T   G O   Y E T  --  S A V E  $5.00   N O W  !                        ";
     var  sOffer3 = "---------------------------------------------------------------------------------------------------------------------------------------------";
     var  sOffer4 = "Save $5 using your Instant Rebate Code below (1st-time customers):                                                     ";
     var  sOffer5 = "                                                                                                                                                                                                                ";
     var  sOffer6 = "   1. Click the [CANCEL] button below.                                    ";
     var  sOffer7 = "                                                                                             ";
     var  sOffer8 = "   2. Enter Code:--> MYDEAL <-- into the Affilate box below on this page.";
     var  sOffer9 = "                                                                      ";
     var sOffer10 = "   3. See your savings during Checkout.  ";   
     var sOffer11 = "                                  ";        
     var sOffer12 = "______________________________________________________________________________________________";
     var sOffer13 = "   NOTE: This 1st-time offer *cannot* be combined with other offers E.g. Free 3rd Tube Special";    

	 // Check IE or other e.g. NN (x,y's do not work)
     try {
        if (  (true == bBackpageActive) && ("IE" == sBrowserType) && ((e.clientX < 0) || (e.clientY < 0))    )  { bRealClose = true;  }
        if (  (false == bBackpageActive) && ("IE" == sBrowserType) && ((e.clientX > 60) && (e.clientY < 0))    )  { bRealClose = true;  }
          // For Checkout, Need X>60 and Y<0 so BackPage (Left 60) does not trigger bRealClose
} catch(Ex) { // nothing
     }
     try {
        if (  ("IE" != sBrowserType) &&  ((e.pageX < 0) || (e.pageY < 0))   )  { bRealClose = true;   }
     } catch(Ex) { //nothing 
     }

     if (bRealClose) {
        var oWin = window.open("abandon.html","abwin","width=770,height=580,");
        if (oWin==null || typeof(oWin)=="undefined") 
           { bPopupsBlocked = true; }           
        else 
           { bPopupsBlocked = false; } // and it worked
        if (bPopupsBlocked)          
          // format is a mess
          //   e.returnValue = sOffer1 + sOffer2+ sOffer3  + sOffer4 + sOffer5+ sOffer6 + sOffer7 + sOffer8+ sOffer9+ sOffer10 + sOffer11+ sOffer12 +sOffer13;  
          e.returnValue = "We regret to see you go. SAVE $5.00 --> Enter PROMO_CODE:[ MYDEAL ] at the bottom of the Buy Page. Press CANCEL to stay.";
      } // bRealClose  
      
}


// ******************** OTHER **************************************************

	// intended to be called from root \ path (buy,checkout)

	function USChamberPop() {
    		window.open("chambercert.html","","width=770,height=340,");
	}
 
			
	function gteepopup(){
    		var popurl="return_policy.html";
    		winpops=window.open(popurl,"","width=595,height=350,scrollbars,");
	}
	

	
	function doVSignInfo() {
		alert("Your personal information is secure.  For performance, our Checkout page is displayed in standard mode, but " +
			"after you enter your data it is encrypted and sent over the internet using SSL.");
	}

	//**** AW - Added 1/30/06 *******
	function FormatCurrency(num) {
                num = num.toString().replace(/\$|\,/g,'');
                if(isNaN(num))
                num = "0";
                sign = (num == (num = Math.abs(num)));
                num = Math.floor(num*100+0.50000000001);
                cents = num%100;
                num = Math.floor(num/100).toString();
                if(cents<10)
                cents = "0" + cents;
                for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
                num = num.substring(0,num.length-(4*i+3))+','+
                num.substring(num.length-(4*i+3));
                //  return (((sign)?'':'-') + '$' + num + '.' + cents);
                // lose leading $
                return (((sign)?'':'-') + num + '.' + cents);
             }


// *************** COOKIE & QUERY STR HANDLING ****************
// ******************* AW - Added 5/9/06 *************************




	
	// **************************************************************
	// Cookies handling from http://www.quirksmode.org/js/cookies.html
	//


function createCookie(name,value,days)
{
	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=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	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;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}

// **************************************************************
// http://www.scottandrew.com/weblog/jsjunk#getqueryargs 
/* getQueryArgs(global)
{
  var args = {};
  var loc = window.location.href;
  var q = loc.indexOf("?");
  if (q==-1) return false;
  loc = loc.substring(q+1);
  var pairs = loc.split("&");
  for (var i=0; i<pairs.length;i++){
    if (global) eval(pairs[i]);
    var keyval = pairs[i].split("=");
    args[keyval[0]] = unescape(keyval[1]);
  }
  return args;
} */
// **************************************************************




// THIS LOOKS GOOD
// http://www.codeproject.com/jscript/JSCookieQueryStrWrapper.asp
// QueryString Class 
// & Cookies Class
//

/*
COOKIES - Instantiate global instances of these classes and initialize them:

    window.gCookies = new Cookies();
    window.gQueryString = new QueryString();
    gCookies.Read();
    gQueryString.Read();

Retrieving some sample data from these objects:

    var iObjId = parseInt(gCookies.GetValue("","myObjId"));
    var reportId = gQueryString.GetValue("reportId");
    
QUERY_STR - These data structures can store and retrieve data safely until it is later utilized by
writing out the cookie to the document (Cookie's Write method), or reassembling the
query string (ToString method) and navigating to another page with it.

    myQueryString.Clear();
    myQueryString.SetValue("workspaceId", workspaceId);
    ...
    myLink.href = document.location.pathname + myQueryString.ToString();
              
The QueryString class is emptied of all key/values by calling the Clear method. 
The Cookie class does not have a clear Clear method. To destroy a cookie, you must 
set its value to null, and once Write() is called, it will be forced to expire.

    gCookies.SetValue("", "MyIntegerOption", 1);
    gCookies.SetValue("", "MyCookieToExpire", null)
    gCookies.Write();
*/

function QueryString() {
	var data = [];
	this.Read = function() 
	{
		var aPairs, aTmp;
		var queryString = new String(window.location.search);
		queryString = queryString.substr(1, queryString.length); //remove "?"
		aPairs = queryString.split("&");	
		
		for (var i=0 ; i<aPairs.length; i++)
		{
			aTmp = aPairs[i].split("=");
			data[aTmp[0]] = aTmp[1];
		}
	}
	
	this.GetValue = function( key )
	{
		return data[key];
	}
	this.SetValue = function( key, value )
	{
		if (value == null)
			delete data[key];
		else 
			data[key] = value;
	}
	this.ToString = function()
	{
		var queryString = new String(""); 
		
		for (var key in data)
		{	
			if (queryString != "")
				queryString += "&"
			if (data[key])
				queryString += key + "=" + data[key];		
		}
		if (queryString.length > 0)
			return "?" + queryString;
		else
			return queryString;
	}
	this.Clear = function()
	{
		delete data;
		data = [];
	}
}


// AW 5-16-06 Don't use this

/*
function Cookies() {
	var cookieData = [];
	
	this.Read = function()
	{
		var pairs = new String(window.document.cookie).split(";");	
		var tmp, cookieName, keyName;
		for (var i=0 ; i<pairs.length; i++)
		{
			tmp = pairs[i].split("=");
			
			if (tmp.length == 3)
			{
				cookieName = new String(tmp[0]);
				cookieName = cookieName.replace(" ", "");
				
				if (cookieData[cookieName] == null)
					cookieData[cookieName] = [];
				cookieData[cookieName][tmp[1]] = unescape(tmp[1]);
			}
			else //length = 2
			{
				keyName = tmp[0];
				keyName = keyName.replace(" ", "");
				if (keyName.substring(0,12)!="ASPSESSIONID") 
				{
					if (cookieData[""] == null)
						cookieData[""] = [];
					cookieData[""][keyName] = unescape(tmp[1]);
				}
			}	
		}	
		
	}
	
	this.GetValue = function( cookie, key )
	{
		if (cookieData[cookie] != null)
			return cookieData[cookie][key];
		else
			return null;
	}
	this.SetValue = function( cookie, key, value )
	{
		if (cookieData[cookie] == null)
			cookieData[cookie] = [];
		cookieData[cookie][key] = value;
	}
	this.Write = function()
	{
	
		var toWrite;
		var thisCookie;
		var expireDateKill = new Date();
		var expireDate = new Date();
		//AW 5-16-06 Modified to keep
		var days = 7;  
		// days=7: Good amount of time so if later clicks another ad/condition
		//   is tracked; Also if re-order later; do not want to show sale from Current ads
		//Old 
		//  expireDate.setYear(expireDate.getFullYear() + 10);
		//  expireDateKill.setYear(expireDateKill.getFullYear() - 10);
        //New       
        //From Quirksmode above: date.setTime(date.getTime()+(days*24*60*60*1000)) 
        expireDate.setTime(expireDate.getTime() + (days*24*60*60*1000));
		expireDateKill.setTime(expireDateKill.getTime() - (days*24*60*60*1000));

		var pairs = new String(window.document.cookie).split(";");	
		var tmp, cookieName, keyName;
		for (var i=0 ; i<pairs.length; i++)
		{
			tmp = pairs[i].split("=");
			if (tmp.length == 3)	
			{		
				window.document.cookie = tmp[0] + "=" + tmp[1] + "='';expires=" + expireDateKill.toGMTString();
			}
			else
			{
				keyName = tmp[0];
				keyName = keyName.replace(" ", "");
				if (keyName.substring(0,12)!="ASPSESSIONID") 
					window.document.cookie = keyName + "='';expires=" + expireDateKill.toGMTString();
			}
		}

		for (var cookie in cookieData)
		{
			toWrite = "";
			thisCookie = cookieData[cookie];
			for (var key in thisCookie)
			{
				if (thisCookie[key] != null)
				{
					if (cookie == "")
						toWrite = key + "=" + thisCookie[key];
					else
						toWrite = cookie + "=" + key + "=" + escape(thisCookie[key]);						
					toWrite += "; expires=" + expireDate.toGMTString();
					window.document.cookie = toWrite;	
				}
			}
		}
	}
}


*/





//******************************************************************************
// ***************** OLD CODE - ARCHIVE ****************************************
//******************************************************************************

/* AW 5/27/05:  We'll need to use Poptest code directly inline

   OLD from https://www.AidanceProducts.com/buy.aspx?prod=terrasil
            function IsPopupBlocker() {
	            var oWin = window.open("","testpopupblocker","width=100,height=50,top=5000,left=5000");
	            if (oWin==null || typeof(oWin)=="undefined") {
		            return true;
	            } else {
		            oWin.close();
		            return false;
	            }
            }
*/
         

/*
function doAbandonOffer () {
    window.open("abandon.html","","width=770,height=680,");
    //document.location.href="abandon.html";
    //alert("Re-pointed doc");
}*/

/*
function doUnload() {
	alert("Here doUnload");

    if ("IE" == sBrowserType) {
       if ((window.event.clientX < 0) || (window.event.clientY < 0)) {
           doAbandonOffer();
        }
    } else {   // assume NN6
	  if ((window.event.pageX < 0) || (window.event.pageY < 0)) {
           doAbandonOffer();
       }
	 } //else NN6
}
*/

/*
function openpopup(){
				var popurl="return_policy.html"
			winpops=window.open(popurl,"","width=520,height=140,scrollbars,")
			}
*/