// print and manage the cart status information

CartStatus = function(p_loggedIn, p_loginEnabled, p_numItems)
{
	this.init();
	// set if passed in
	if (typeof p_loggedIn !='undefined'){ this.setIsLoggedIn(p_loggedIn); }
	if (typeof p_loginEnabled !='undefined'){ this.setLoginEnabled(p_loginEnabled); }
	if (typeof p_numItems !='undefined'){ this.setNumberOfItems(p_numItems); }
};

CartStatus.prototype = {
	init:function() {
		this.itemsInCart = 0;
		this.isLoggedIn = false;
		this.loginEnabled = true;
		
		// init from coookies or create the cookie
		var ckUtil = new CJL_CookieUtil("cartStatus");
		if (!ckUtil.cookieExists()){
			ckUtil.setSubValue("loggedIn", false);
			ckUtil.setSubValue("itemsInCart", 0);
		    ckUtil.setSubValue("loginEnabled", true);
		}
		// init from cookie
		if (ckUtil.cookieExists()){
			this.isLoggedIn = ckUtil.getSubValue("loggedIn");
			this.itemsInCart = parseInt(ckUtil.getSubValue("itemsInCart"));
			this.loginEnabled = ckUtil.getSubValue("loginEnabled");
			if (this.itemsInCart.toString() == "NaN"){ this.itemsInCart = 0; }
			if (typeof this.loginEnabled == 'undefined') { ckUtil.setSubValue("loginEnabled", true); }
		}
		
		/**
		set the number of items in the cart 
		*/
		this.setNumberOfItems = function(p_num){
			if (isFinite(p_num)){
				ckUtil.setSubValue("itemsInCart", p_num);
			}
		}

		/**
		set login status
		*/
		this.setIsLoggedIn = function(p_val){
			ckUtil.setSubValue("loggedIn", p_val);
		}

		/**
		set login enabled
		*/
		this.setLoginEnabled = function(p_val){
			ckUtil.setSubValue("loginEnabled", p_val);
		}
		
		this.printStatus = function(p_divId){
			if (document.getElementById){
			    var items = "item";
			    if (this.itemsInCart != 1)
			    {
			        items = items + "s";
			    }
				var output = "<p><b>You have " + this.itemsInCart + " " + items + " in your cart.</b></p>";
				output += '<p><a title="View Cart" href="/cart-view.aspx" style="border-style:None;"><img title="View Cart" src="/images/viewcart.png" alt="" border="0" /></a></p>';
				if (this.loginEnabled) {
					if (this.isLoggedIn){
						output += '<p><a title="Edit Profile" href="/account-addedit.aspx" style="border-style:None;"><img title="Edit Profile" src="/images/editprofile.png" alt="" border="0" /></a></p><p><a title="Logout" href="/logout.aspx" style="border-style:None;"><img title="Logout" src="/images/logout.png" alt="" border="0" /></a></p>';
					} else {
						output += '<p><a title="Login" href="/login.aspx" style="border-style:None;"><img title="Login" src="/images/login.png" alt="" border="0" /></a></p>';
					}
				}
				document.getElementById(p_divId).innerHTML = output;
			}
		}		
	}	
};

/**
 * Copyright (C) 2002-2003, CodeHouse.com. All rights reserved.
 * CodeHouse(TM) is a registered trademark.
 *
 * THIS SOURCE CODE MAY BE USED FREELY PROVIDED THAT
 * IT IS NOT MODIFIED OR DISTRIBUTED, AND IT IS USED
 * ON A PUBLICLY ACCESSIBLE INTERNET WEB SITE.
 * 
 * CodeHouse.com JavaScript Library Module: Cookie Utility Class
 *
 * You can obtain this script at http://www.codehouse.com
 */
function CJL_CookieUtil(name, duration, path, domain, secure)
{
   this.affix = "";
   
   if( duration )
   {   	  
      var date = new Date();
	  var curTime = new Date().getTime();

	  date.setTime(curTime + (1000 * 60 * duration));
	  this.affix = "; expires=" + date.toGMTString();
   }
   
   if( path )
   {
      this.affix += "; path=" + path;
   }
   
   if( domain )
   {
      this.affix += "; domain=" + domain;
   }

   if( secure )
   {
      this.affix += "; secure=" + secure;
   }
   
      
   function getValue()
   {
      var m = document.cookie.match(new RegExp("(" + name + "=[^;]*)(;|$)"));

      return m ? m[1] : null;   
   }
   
   this.cookieExists = function()
   {
      return getValue() ? true : false;
   }
      
   this.expire = function()
   {
      var date = new Date();
	  date.setFullYear(date.getYear() - 1);
	  document.cookie=name + "=noop; expires=" + date.toGMTString(); 
   }
        
   this.setSubValue = function(key, value)
   {
      var ck = getValue();

      if( /[;, ]/.test(value) )
      {
         //Mac IE doesn't support encodeURI
		 value = window.encodeURI ? encodeURI(value) : escape(value);
      }

      
      if( value )
      {
         var attrPair = "@" + key + value;

         if( ck )
         {
             if( new RegExp("@" + key).test(ck) )
	         {
		        document.cookie =
				   ck.replace(new RegExp("@" + key + "[^@;]*"), attrPair) + this.affix;
	         }
	         else
	         {
		        document.cookie =
				   ck.replace(new RegExp("(" + name + "=[^;]*)(;|$)"), "$1" + attrPair) + this.affix;
	         }
         }
         else
         {
	        document.cookie = name + "=" + attrPair + this.affix;
         }
      }
      else
      {      
	     if( new RegExp("@" + key).test(ck) )
	     {
	        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), "") + this.affix;
	     }
      }
   }

      
   this.getSubValue = function(key)
   {
      var ck = getValue();

      if( ck )
      {
         var m = ck.match(new RegExp("@" + key + "([^@;]*)"));

	     if( m )
	     {
	        var value = m[1];

	        if( value )
	        { 
	           //Mac IE doesn't support decodeURI
			   return window.decodeURI ? decodeURI(value) : unescape(value);
	        }
	     }
      }
   }
}



