var smc_name = "Stretch Mark Cream: ";
var smc_price = 19.95;
var dk1_name = "Dermal-K Cream: ";
var dk1_price = 28.95;


/***** Display shopping cart contents on load *****/

/*
 * n = String object containing the cookie name.
 * v = String object containing the cookie value.
 *	May contain any valid string characters.
 * h = Hours until expiration, or date object
 *	containing the expiration date of the cookie.
 *	If omitted or null, expires the cookie at the
 *	end of the current session.
 * p = String object indicating the path for which the cookie is valid.
 *	If omitted or null, uses the path of the calling document.
 * d = String object indicating the domain for which the cookie is
 *	valid.  If omitted or null, uses the domain of the calling document.
 * s = Boolean (true/false) value indicating whether cookie transmission
 *	requires a secure channel (HTTPS).
 */
function setCookie (n,v,h,p,d,s) {
    if(h){
	if( (typeof(h)=='string') && Date.parse(h) ) {
	    var hN=h;
	} else if(typeof(h)=='number') {
	    var hN=(new Date((new Date()).getTime()+h*3600000)).toGMTString();
	}
    }
    document.cookie=n+'='+escape(v)+((hN)?(';expires='+hN):'')+((p)?';path='+p:'')+((d)?';domain='+d:'')+((s&&(s==true))?';secure':'');
}



function getProd (n) {
    if(document.cookie == '')
	return 0;

    var c = document.cookie;
    s = c.indexOf(n)
    if(s == -1)
	return 0;

    s += n.length;
    e = c.indexOf("+", s);

    if(e == -1)
	return 0;

    var num = Number(c.substring(s, e));
    return(num);
}



function showCartContents (doc) {
    var smc = getProd("SMC1-");
    var dk1 = getProd("DK1-");
    var totalItems = smc + dk1;
    var subtotal = 0.0;

    if (document.getElementById && !window.navigator.cookieEnabled) {
	doc.write("<center><i>Enable cookies to use the shopping cart<hr width=\"25%\">");
	doc.write("Or click Add to Cart to purchase a single product</i></center>");
	return;
    }

    if (totalItems == 0) {
	doc.write("<center><i>Your shopping cart is empty</i></center><br>");
	return;
    }

    // SMC
    if (smc > 0) {
	doc.write(smc_name+"<br>");
	doc.write("Quantity: <b>"+smc+"</b><br>");
	doc.write("Amount: <b>"+formatPrice(smc*smc_price)+"</b><br>");
	doc.write("<a class='shoppingcartcontents' href='javascript:removeSMCFromCart()'>Remove From Cart</a><br><br>");

	subtotal += smc*smc_price;
    }

    // DK1
    if (dk1 > 0) {
	doc.write(dk1_name+"<br>");
	doc.write("Quantity: <b>"+dk1+"</b><br>");
	doc.write("Amount: <b>"+formatPrice(dk1*dk1_price)+"</b><br>");
	doc.write("<a class='shoppingcartcontents' href='javascript:removeDK1FromCart()'>Remove From Cart</a><br><br>");

	subtotal += dk1*dk1_price;
    }

    // Subtotal
    doc.write("<br>");
    doc.write("<center><b>Subtotal: "+formatPrice(subtotal)+"</b></center>");
}



/***** Redisplay shopping cart contents on change *****/

function displayCookieText () {
    if (document.getElementById) {
	var e = document.getElementById('cart');
	var smc = getProd("SMC1-");
	var dk1 = getProd("DK1-");
	var totalItems = smc + dk1;
	var subtotal = 0.0;

	var CENTER;
	var B;
	var A;
	var I		= document.createElement("i");
	var theSmcHref	= "javascript:removeSMCFromCart()";
	var theDk1Href	= "javascript:removeDK1FromCart()";
	var theClass	= "shoppingcartcontents";


	if (totalItems == 0) {
	    // <center><i>Your shopping cart is empty</i></center><br>
		I.appendChild( document.createTextNode('Your shopping cart is empty') );
	      CENTER = document.createElement("center");
	      CENTER.appendChild(I);
	    e.appendChild(CENTER);
	    e.appendChild( document.createElement('br') );
	    return;
	}

	// SMC
	if (smc >= 1) {
	    // smc_name+<br>
	    //	Quantity: <b>"+smc+"</b><br>
	    //	Amount: <b>"+formatPrice(smc*smc_price)+"</b><br>
	    //	<a href='http://www.barmon.com/cgi-bin/removesmcfromcart.cgi' class='shoppingcartcontents' onClick='return removeSMCFromCart()'>Remove From Cart</a><br>
	    e.appendChild(document.createTextNode(smc_name));
	    e.appendChild(document.createElement('br'));
	    e.appendChild(document.createTextNode("Quantity: "));
	      B = document.createElement("b");
	      B.appendChild(document.createTextNode(smc));
	    e.appendChild(B);
	    e.appendChild(document.createElement('br'));
	    e.appendChild(document.createTextNode("Amount: "));
	      B = document.createElement("b");
	      B.appendChild(  document.createTextNode( formatPrice(smc*smc_price) )  );
	    e.appendChild(B);
	    e.appendChild(document.createElement('br'));

	    if (window.navigator.userAgent.indexOf("MSIE ")>0) {
		e.innerHTML+='<A href="'+theSmcHref+'" class="'+theClass+'">Remove From Cart</a>';
	    } else {
		  A = document.createElement("a");
		  A.appendChild(document.createTextNode("Remove From Cart"));
		  A.setAttribute("class", theClass);
		  A.setAttribute("href", theSmcHref);
		e.appendChild(A);
	    }

	    e.appendChild( document.createElement('br') );
	    e.appendChild( document.createElement('br') );

	    subtotal += smc*smc_price;

	}

	// DK1
	if (dk1 >= 1) {
	    // dk1_name+<br>
	    //	Quantity: <b>"+dk1+"</b><br>
	    //	Amount: <b>"+formatPrice(dk1*dk1_price)+"</b><br>
	    //  <a href='http://www.barmon.com/cgi-bin/removedk1fromcart.cgi' class='shoppingcartcontents' onClick='return removeDK1FromCart()'>Remove From Cart</a><br>
	    e.appendChild(document.createTextNode(dk1_name));
	    e.appendChild(document.createElement('br'));
	    e.appendChild(document.createTextNode("Quantity: "));
	      B = document.createElement("b");
	      B.appendChild(document.createTextNode(dk1));
	    e.appendChild(B);
	    e.appendChild(document.createElement('br'));
	    e.appendChild(document.createTextNode("Amount: "));
	      B = document.createElement("b");
	      B.appendChild(  document.createTextNode( formatPrice(dk1*dk1_price) )  );
	    e.appendChild(B);
	    e.appendChild(document.createElement('br'));

	    if (window.navigator.userAgent.indexOf("MSIE ")>0) {
		e.innerHTML+='<A href="'+theDk1Href+'" class="'+theClass+'">Remove From Cart</a>';
	    } else {
		  A = document.createElement("a");
		  A.appendChild(document.createTextNode("Remove From Cart"));
		  A.setAttribute("class", theClass);
		  A.setAttribute("href", theDk1Href);
		e.appendChild(A);
	    }

	    e.appendChild( document.createElement('br') );
	    e.appendChild( document.createElement('br') );

	    subtotal += dk1*dk1_price;
	}

	// Subtotal
	// <br><center><b>Subtotal: "+formatPrice(subtotal)+"</b></center>
	e.appendChild( document.createElement('br') );
	    B = document.createElement("b");
	    B.appendChild(  document.createTextNode( "Subtotal: "+formatPrice(subtotal) )  );
	  CENTER = document.createElement("center");
	  CENTER.appendChild(B);
	e.appendChild(CENTER);
    } else {
	// Reload to show new cart contents.
	location.reload();
    }
}



function formatPrice (price) {
    var dollars = Math.floor(Math.round(price*100)/100);
    var cents = Math.round((price - dollars) * 100);
    var formattedPrice = "$" + dollars.toString() + "." + ((cents < 10) ? "0" : "") + cents.toString();
    return(formattedPrice);
}



function emptyElement () {
    if (document.getElementById) {
	var element=document.getElementById('cart');
	var elementLen=element.childNodes.length;  

	for (i=0;i<elementLen;i++) {
	    element.removeChild(element.childNodes[0]);
	}
    }
}



function updateCookieDisplay () {
    // Opera was CRASHING, so play it safe.
    if (window.navigator.userAgent.indexOf("pera/")>0)
	location.reload();
    else {
	emptyElement();
	displayCookieText();
    }
}



function addSMCToCart (form_this) {
    var smc = getProd("SMC1-")
    var dk1 = getProd("DK1-");
    var QTY = Number(form_this.QTY.value);

    if (QTY == 0)
	QTY = 1;

    smc += QTY;

    setCookie("cart", "SMC1-" + smc + "+DK1-" + dk1 + "+", null , "/", ".barmon.com", false);

    // no cookies; order single product.
    if (document.cookie == '') {
	location="https://www.barmon.com/cart/barmonorder.php?return=" + location + "&cart_contents=SMC1-" + smc + "+DK1-0+";
    } else
	updateCookieDisplay();

    return false;
}



function addDK1ToCart (form_this) {
    var smc = getProd("SMC1-");
    var dk1 = getProd("DK1-")
    var QTY = Number(form_this.QTY.value);

    if (QTY == 0)
	QTY = 1;

    dk1 += QTY;

    setCookie("cart", "SMC1-" + smc + "+DK1-" + dk1 + "+", null , "/", ".barmon.com", false);

    // no cookies; order single product.
    if (document.cookie == '') {
	window.location="https://www.barmon.com/cart/barmonorder.php?return=" + location + "&cart_contents=SMC1-0+DK1-" + dk1 + "+";
    } else
	updateCookieDisplay();

    return false;
}



function removeSMCFromCart () {
    var dk1 = getProd("DK1-");

    setCookie("cart", "SMC1-0+DK1-" + dk1 + "+", null , "/", ".barmon.com", false);
    updateCookieDisplay();
}



function removeDK1FromCart () {
    var smc = getProd("SMC1-");

    setCookie("cart", "SMC1-" + smc + "+DK1-0+", null , "/", ".barmon.com", false);
    updateCookieDisplay();
}

