/* %~%@ < 4.003 > @%~% */
/* %~%# < 7/24/2009 > #%~% */
/* %~%! < cart4shared > !%~% */

/*
========================================================
These can be changed in your parent scripts
========================================================
*/
var cartEmptyCartText = "Cart is Empty";
var cartFullCartTextPre = '';
var cartFullCartTextPost = ' <b><a href="/basket.asp" class="checkout">CHECKOUT</a></b> ';
var cartCookiesDisabled = "/cookiesdisabled.asp";
var cartMiniCartFrameID = "MyCart";
var cartMiniCartFrameURL = "/MiniCart.asp";

c4addLoadEvent(function() {
    CartInitCookies();
})

c4addLoadEvent(function() {
    c4CheckVisible();
})

function CartInitCookies() {
	if (GetCookie("cart") == null){
		DeleteCookie ("cart");
		SetCookie ("cart", null,null,"/");
	}
	if (GetCookie("cart") == ""){
		DeleteCookie ("cart");
		SetCookie ("cart", null,null,"/");
	}
	if (GetCookie("cart") == "null"){
		DeleteCookie ("cart");
		SetCookie ("cart", null,null,"/");
	}
	if (GetCookie("subtotal") == null){
		SetCookie ("subtotal", "0.00",null,"/");
	}
	if (GetCookie("subtotal") == ""){
		SetCookie ("subtotal", "0.00",null,"/");
	}
	if (GetCookie("subtotal") == "null"){
		SetCookie ("subtotal", "0.00",null,"/");
	}
}

function c4toggle_layer (obj) {
	var lay = document.getElementById(obj);
	var dis = lay.style.display;
	lay.style.display = 'block';
}

function c4ShowCookie() {
	c4CheckCookies()

	window.defaultStatus="Current Product Subtotal: $"+formatcurrency(parseFloat("0"+GetCookie("subtotal")));
	var sho="";

	sho = cartEmptyCartText;

	if (parseFloat("0"+GetCookie("subtotal")) > 0) {
		sho = cartFullCartTextPre + "$" + formatcurrency(parseFloat("0"+GetCookie("subtotal"))) + cartFullCartTextPost;
	}
	
	if (document.getElementById) {
		if (document.getElementById("divCart")) {
			document.getElementById("divCart").contents = sho;
			document.getElementById("divCart").innerHTML = sho;
		}
	}

	if (document.getElementById) {
		if (document.getElementById("divMasterCart")) {
			document.getElementById("divMasterCart").style.visibility = "visible";
			document.getElementById("divMasterCart").visibility = "visible";
			document.getElementById("divMasterCart").style.display = 'block';
		}
	}

	//Call Hook
	c4ShowCookieHOOK();
}

function c4BuyProduct(Item,myPrice) {
	c4CheckCookies()

	//Call Hook
	if (c4BuyProductFirstHOOK(Item,myPrice,arguments[2],arguments[3],arguments[4])!=true){ return;}

	var OldCookie;
	var CartSum = 0;
	var Price = myPrice;
	var PriceShift = 0;
	var Size = "";
	var Quantity = 1;
	var Qty11='';
	
	if (arguments.length>2) {
			Size = arguments[2];
			var myValue = "";
			// Handle single & multiple options by detecting if element 0 has a length
			// If there is no element 0 then there is only one option listed
			try {
				// This tests to see if element 0 has a length (ie, its a select box)
				if (Size[0].length){
					// Multiple Options
					// This will get all selects + hidden elements with same name
					Size = document.getElementsByName( Size[0].name )
					for (var h = 0; h < Size.length; h++){
						var opt = Size[h].value
						myValue += opt;
						var parms = opt.split("`")
						PriceShift += parseFloat( parms[1] );
						if ( h < Size.length-1) {
							myValue += ",";
						}
					}
				} else {
					// If there is no array then it falls here
					// Single Option
					var opt = Size.value
					myValue = opt;
					var parms = opt.split("`")
					PriceShift = parseFloat(parms[1]);
				}
			}
			catch(er) {
				// If there is no array or the first element has no length (ie, its NOT a select box)
				// alert(er);
				if (Size) {
					var opt = Size.value
					myValue = opt;
					if (opt.length>0){
						var parms = opt.split("`")
						PriceShift = parseFloat(parms[1]);
					}
				}
			}
			
			//Call Hooks
			Price += c4BuyProductPriceShiftHOOK(Item,Price,Size,myValue,PriceShift);
			Size = c4BuyProductSizeHOOK(Item,Price,Size,myValue,PriceShift);
	}


	// Quantity addition
	if (arguments.length>3) {
		Qty11 = arguments[3];
		Quantity = Qty11.value
	}

	OldCookie = GetCookie("cart");
	if (parseFloat("0"+GetCookie("subtotal")) != 0){
		SetCookie ("cart", OldCookie+"|"+Item+"~"+Price+"~"+Size+"~"+Quantity,null,"/");
		SetCookie ("cart", OldCookie+"|"+Item+"~"+Price+"~"+Size+"~"+Quantity,null,"/_cart/_checkcookie.asp");
	} else {
		SetCookie ("cart", Item+"~"+Price+"~"+Size+"~"+Quantity,null,"/");
	}
	
	OldCookie=GetCookie("subtotal");
	if (OldCookie != "null"){
		CartSum = parseFloat("0" + OldCookie)
		CartSum = CartSum + parseFloat(Price * Quantity)
	} else {
		CartSum=parseFloat(Price * Quantity)
	}

	SetCookie ("subtotal","" + formatcurrency (CartSum),null,"/");

	c4ShowCookie();
	c4ReloadCartFrame();
	
	//Call Hook
	c4BuyProductHOOK(Item,myPrice,Size,Qty11);
}

function c4ReloadCartFrame() {
	if (document.getElementById) {
		if (document.getElementById(cartMiniCartFrameID)) {
			frames[cartMiniCartFrameID].location.href=cartMiniCartFrameURL;
		}
	}
}
function c4CheckVisible() {
	c4CheckCookies()

	if (parseFloat("0"+GetCookie("subtotal")) > 0) {
			c4ShowCookie();
		}
}

function c4tweekprice(pricediv,cbo,baseprice) {
var sho;
	c4CheckCookies()
	if (document.getElementById) {
			sho = document.getElementById(pricediv).contents;
			sho = document.getElementById(pricediv).innerHTML;
	 }
	if (document.all) {
			sho = document.all[pricediv].contents;
			sho = document.all[pricediv].innerHTML;
	 }
	var myValue="";
	var mySum=parseFloat(baseprice);
	// Handle single & multiple options by detecting if element 0 has a length
	// If there is no element 0 then there is only one option listed
	try {
		if (cbo[0].length){
			for (var h = 0; h < cbo.length; h++){
				mySum += parseFloat(c4GetRightString(cbo[h].value,'`'));
			}
		} else {
			mySum += parseFloat(c4GetRightString(cbo.value,'`'));
		}
	}
	catch(er)
	{
		//alert(er);
	}
	
	sho = "$"+formatcurrency(parseFloat(mySum));	
	if (document.getElementById) {
			document.getElementById(pricediv).contents = sho;
			document.getElementById(pricediv).innerHTML = sho;
	 }
	if (document.all) {
			document.all[pricediv].contents = sho;
			document.all[pricediv].innerHTML = sho;
	 }

	//Call Hook
	c4tweekpriceHOOK(pricediv,cbo,baseprice)
}

function c4GetRightString(fullString, subString) {
   if (fullString.indexOf(subString) == -1) {
      return "0";
   } else {
      return (fullString.substring(fullString.indexOf(subString)+subString.length, fullString.length));
   }
}

function c4handleError() {
     return true;
}

function c4noenter() {
	return true;
}

function c4addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function c4CookiesEnabled() {
    var GetsCookie = (navigator.cookieEnabled)?true:false

    // If the browser does not support cookie check
    if(typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){

        // Try setting up a test cookie
        document.cookie = "SampleCookie";

        // And see if it got set successfully
        GetsCookie = (document.cookie.indexOf("SampleCookie")!=-1)?true:false
    }
    return GetsCookie;
}

function c4CheckCookies() {
	if (!c4CookiesEnabled()) {
		top.location.href = cartCookiesDisabled
	}
}

/* 
Optional Hook functions
Do NOT modify these, re-define them in your parent document
*/
function c4BuyProductSizeHOOK(Item,Price,Size,myValue,PriceShift){return myValue}
function c4BuyProductPriceShiftHOOK(Item,Price,Size,myValue,PriceShift){return PriceShift}
function c4tweekpriceHOOK(pricediv,cbo,baseprice) {}
function c4BuyProductHOOK(Item,myPrice) {}
function c4ShowCookieHOOK() {}
function BuyClickEvent(){}
function c4BuyProductFirstHOOK(Item,myPrice,arg2,arg3,arg4){return true;}
