/////////////////////////////////////////////////////////////////////////////
//////////////////// Shoppingcart Script ////////////////////////////////////
///           copyright &copy;CBmark Consulting 2004                      ///
///                      http://cbmark.com                                ///
/////////////////////////////////////////////////////////////////////////////

var items = new Array("01 SLD Cabernet Reserve","2000 SLD Cabernet","05 Clements Hills Syrah")                
//new Array("01 SLD Cabernet Reserve");
var prices = new Array("49.00","49.00","8.99");
var quant0 = 0;
var quant1  = 0;
var quant2  = 0;
var quant3 = 0;
var quant = new Array(quant0,quant1,quant2,quant3);
var subtotal = "0.00";
var tax = 0;
var shippingMeth = "";
var sh_costs = 0;
var freeshp = 0;

var total = 0;
total = calcTotal();
var ten = "DSW100";
var twentyfive = "KJW250";
var sh_code = "JJK000";
var skonto = "no";

 
function inCart(q, s){
  var quan = (isNaN(q) ? "" : q);
  	if(quan == "" ||quan == 0){
  		alert("Please enter a valid number")	
  	}else {
		   for (i = 0; i < items.length; i++){
	   		if(s == i)
				eval("quant"+i+"+= parseInt(quan)");
			}
	}
	quant =  [quant0,quant1,quant2,quant3];
	subtotal = subTot();
	tax = calcTax();
}

function updateCart(q,s){
	if (q == "")
	  	q = "0";		
	var quan = (!isNaN(q) ? q : alert("Invalid Entry")); 
	if (isNaN(quan))
		quan = quant[s];
		  for (i = 0; i < items.length; i++){
	   		if(s == i){
				eval ("quant"+i+"=0");
				eval("quant"+i+"= parseInt(quan)");
				}
			}
	quant = [quant0,quant1,quant2,quant3];
	subtotal = subTot();
	tax = calcTax();
	sh_costs = 0;
	total = 0;
	parent.content.location.href = parent.content.location.href;
}

function calcDisc(c){
	if (c == sh_code){
		if (freeshp == 1){
			alert("you have already entered your Shipping Code");			
			return;
			}
		freeshp = 1;  // 6 bottles or more
		parent.content.location.href = parent.content.location.href;
		alert("Free shipping on order of 6 or more bottles");
		return;
		}
	if (skonto == "yes"){
		alert("you have already entered your Discount Code");
		return;
		}
	else if (c == ten){
		for (i = 0; i < items.length; i++){
	 		eval ("prices["+i+"] = format((prices["+i+"] * 90)/100)");
			}
		skonto = "yes";
		parent.content.location.href = parent.content.location.href;
		alert("10% discount for this order");
		return;
		}
	else if (c == twentyfive){
		for (i = 0; i < items.length; i++){
	 		eval ("prices["+i+"] = format((prices["+i+"] * 75)/100)");
			}
		skonto = "yes";
		parent.content.location.href = parent.content.location.href;
		alert("25% discount for this order");
		return;
		}
return;
}


function subTot(){
var stotal = 0;
for (i = 0; i < items.length; i++){
	stotal += Math.round(quant[i]*prices[i]*100)/100;
	}
stotal = format(Math.round(stotal*100)/100);
if (stotal == 0){
	stotal = "0.00";
	}
subtotal = stotal;
return (stotal);
}


function format(amount){
amount -= 0;
return (amount == Math.floor(amount)) ? amount + '.00' : ((amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

function calcTax(){
	var t = 0;
	t = format(subtotal * 9.25 /100);
	t = format(Math.round(t*100)/100);
	return (t);
	}
	
function calcShipping(){
var a = 0; //1-3
var b = 0; //4-6
var c = 0; // 7-11
var d = 0; //  12 or more
var e = 0;  // 6 or more and freeshp
var dozen = 0;
var rest = 0; 
sh_costs = 0;
var botls = quant0+quant1+quant2+quant3;
if (botls >= 12){
	d = 1;						
	rest = botls % 12;
	dozen = (botls - rest) / 12;
	if (freeshp == 1)
		e = 1;                // free shipping on 6 or more
    if ((rest >= 1) && (rest <= 3))	
	 	a = 1;
	if ((rest >= 4) && (rest <= 6))
		b = 1;
	if ((rest >= 7) && (rest <= 11))
		c = 1;   
	} 
else{
	if ((botls >= 1) && (botls <= 3))	
	 	a = 1;
	if ((botls >= 4) && (botls <= 6))	
	 	b = 1;
	if ((botls >= 7) && (botls <= 11))
		c = 1; 
	if ((botls >= 6) && (freeshp == 1))    // free shipping for more than 6
		e = 1;
   }
if (e != 1)
	sh_costs = ((a * 15) + (b * 20.00) + (c * 25.00));  // +(dozen * 29.00)
if (e ==1)
	sh_costs = 0.00;

return format(sh_costs);
}

function calcTotal(){
	var tot = 0;
	tot = (subtotal*1) + (tax*1) + (calcShipping()*1);
	return format(Math.round(tot*100)/100);;
}