// cart.js
// Version 2.0-musrus
// Written by Dan Shoop <shoop@iWiring.Net>
// iWiring.Net
//
// All rights reserved. Copyright 1997, 1998, 1999, 20000
//
// Requires: 
//    JavaScript Version 1.2
//    (This implies Netscape/MSIE Version 4.n, or later, browsers.)
//
// Description:
// This code manages a shopping cart implemented in a cookie
//
// Caveats:
//   - item numbers must not contain = | or : as these are used as
//     delimiters in the cartcookie. 
//   - items may not contain  whitespace or any characters 
//     which are not allowed in cookies: ";,
//
//
// History:
// v2.0 - This version of the cartcode assumes multiple cookies as well as
//        handling quantities of items placed into the cart. This version
//        also checks to make sure cart items are not duplicated on the cart.
// v1.0 - Assumed single cookie for document, namely the cartcookie. Qtys were
//        not stored in the cart, but were determined at checkout.
//
//
// Notes:
//   - cart values are stored in the form item:qt|item:qt...
//   - the cartcookie expires one month after last being set
//

function storeCartCookie(cookieValue)
{
    var when = new Date()
    when.setMonth(when.getMonth()+1)
    var expireDate = when.toGMTString()
    document.cookie = "cartcookie=" + cookieValue + ";expires=" + expireDate + ";path=/" + ";domain=" + document.domain
 }
  
function getCartCookie()
{
	if ((document.cookie == null) || (document.cookie == "") || (document.cookie == "cartcookie="))
	{
	    // no cookies at all, create an empty cartcookie
		storeCartCookie("")
		return ""
	}
	else
	{   
	    // we have cookies, check for cartcookie
		if (document.cookie.indexOf("cartcookie=") >= 0)
		{
		    //we have a cartcookie, separate the cartcookie from all the document cookies
			cookieString = unescape(document.cookie)
			cartCookieArray = cookieString.split("cartcookie=")
			cartContents = cartCookieArray[1].split(";")
			return cartContents[0]
		}
		else
		{
			// the cartCookie did not exist
		    storeCartCookie("")
			return ""
		}
	}
}

//old
//gSecureServer = "https://server3.webdudes.com/musicarussica";

//new
gSecureServer = "https://secure.musicarussica.com";

function addItemToCartNew2(cat, mus_rus_ID, qt) 
{
	//EPH 7/11/05 -- when we have mrid and not id
	document.location = gSecureServer + '/cart_addto.lasso?redir=1&cat='+cat+'&'+cat+'mrid=%22'+escape(mus_rus_ID)+'%22&qt='+qt;
	return true;
}

function addItemToCartNew(cat, id, qt) 
{
	//EPH 4/30/05
	document.location = gSecureServer + '/cart_addto.lasso?redir=1&cat='+cat+'&id='+id+'&qt='+qt;
	return true;
}

function addItemToCartNewAAV(cat, id, qt) 
{
	//EPH 11/11/05
	document.location = gSecureServer + '/cart_addto.lasso?aav=1&redir=1&cat='+cat+'&id='+id+'&qt='+qt;
	return true;
}

function addItemToCart(catalog, itemNumber, qt, noalert) {
    // the cart looks like catalog:itemnumber:qt[|...]
    
	var cartContents = getCartCookie()
	var itemPair = new Array()	
	var checkString = new String()
	
	if (cartContents == "") {
	    // the cart is empty, simply set cartContents to the catalog & item
		cartContents = catalog + ":" + itemNumber + ":" + qt
	    msg = "Your cart was empty, but this item (#"+catalog+"."+itemNumber+") has now been added to your cart."
	}
	else {
	    // break the cartContents into cartArray where cartArray[n] = catalog:item_no
	    cartArray=cartContents.split("|")
	    
	    // check to see if this item is on the cart, if so, update it with the new quantity
	    var foundOnCart = "";
	    for (i=0; i < cartArray.length; i++) {
	       // check to see if item is on the cart already
	       checkString = catalog + ":" + itemNumber
	       cartEntry = cartArray[i]
	       if (cartEntry.indexOf(checkString) == 0) 
	      {
	          if (qt <= 0)
	          {
			     foundOnCart = "removed from";
			     cartArray[i] = "";
	          }
	          else
	          {
	          	cartArray[i] = catalog + ":" + itemNumber + ":" + qt;
	            foundOnCart = "already in";
	          }
	       }
	    }
	    if (foundOnCart != "") {
	        // the item was found on the cart, reconstruct cartContents from the cartArray
	        cartContents = cartArray.join("~");
	        cartContents = cartContents.replace(/~+/gi,"~"); //middle item removed
	        cartContents = cartContents.replace(/~$/,""); //beginning item removed
	        cartContents = cartContents.replace(/^~/,""); //end item removed
	        cartContents = cartContents.replace(/~/gi,"|"); 
	        msg = "This item (#"+catalog+"."+itemNumber+") was "+foundOnCart+" your cart."
	    }
	    else {
	        // the item was not on the cart, simply add it to the end of the cartContents
			cartContents = cartContents + "|" + catalog + ":" + itemNumber + ":" + qt
	        msg = "This item (#"+catalog+"."+itemNumber+") has been added to your cart."
	    }
    }
    
    //always store the cookie regardless if item was already on cart, since it updates the expiry
	storeCartCookie(cartContents)
	
	//EPH 4/27/05
	if (noalert) return msg;
	
	alert(msg);
	return true;
}

function emptyCartNew()
{
	//EPH 4/30/05
	document.location = gSecureServer + '/cart_addto.lasso?redir=1&empty=1';
	return true;
}

function emptyCart(noalert)
//removes all items from a browsers cart
{
	storeCartCookie("");
	
	msg = 'Your cart has been emptied.';
	if (noalert) return msg;
	
	alert(msg);
	return true;
}

function MakeBuyLink(cat,id,name)
{
	//EPH 4/26/05
	url = gSecureServer + '/cart_addto.lasso?cat='+cat+'&id='+id;//'
	document.write("<a onclick=\"window.open('"+url+"','shoppingcart','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=300,height=200,left=0,top=0');return false;\" target=_blank href=\""+url+"\">"+name+"</a>");
}
