// ----------  IMPORTANT  -------------------
// set the value of products to the total number of 
// products available on the order form
var products = 10;
// ------------------------------------------


// order form calculations

// discount
var discount = "1.0";

// functions 
// round numbers to nearest .01
function format(num){
   finished=Math.floor(num)+".";
   var base=100*(num-Math.floor(num))+0.5;
   finished += Math.floor(base/10);
   finished += Math.floor(base%10);
   return finished;
}
// club discounts - added 02/2007
// if they check more than one, take the biggest discount
function doClubDiscounts(){
//	if(document.orderform.join_12_bottle_club){
		if(document.orderform.join_12_bottle_club.checked) {
			discount = ".80";
		}	
//	}	
//	else if(document.orderform.join_6_bottle_club){
		else if(document.orderform.join_6_bottle_club.checked) {
			discount = ".85";
		}	
//	}	
//	else if(document.orderform.join_3_bottle_club){
		else if (document.orderform.join_3_bottle_club.checked){
			discount = ".90";
		}
//	}

	else { discount = "1.0"; }
}

function doChanges(){

doClubDiscounts();
// doChanges is the main function that does the updating of the form.  
// sweep through the rows, calculating current totals, 
// and keeping a running subtotal.

var subtot = 0;	
var bottles = 0;

// we need to figure total bottles before subtotal, 
// since 12+ bottles gives 10% off

	for(i=1;i<=products;i++){
			 z = "b"+i;
			 if(document.orderform[z]){
			  bottles += Number(document.orderform[z].value);
			 }
		}
// then figure first x row subtotals
	for(x=1;x<=products;x++){
		p = "p"+x;
		b = "b"+x;
		t = "t"+x;
		if(document.orderform[p]){
		  document.orderform[t].value = 
		  format( (document.orderform[p].value * document.orderform[b].value) * Number(discount));
		  // keep a running total of the subtotal 
		  subtot += Number(document.orderform[t].value);
		}
	}	

// update subtotal, rounding to nearest .01	
document.orderform.sub_total.value = format(subtot);
}

function doTotal(){
	//alert('hi');
doChanges();
if(document.orderform.ca_resident.checked){
	document.orderform.ca_tax.value = format(Number(Number(document.orderform.sub_total.value) * .0775));
}
else { document.orderform.ca_tax.value = "0"; }
// calculate the total from the subtotal + shipping, if it has been entered.
if (Number(document.orderform.shipping.value) > 3) {
	document.orderform.total.value = format(
	Number(document.orderform.sub_total.value) + 
	Number(document.orderform.ca_tax.value) +
	Number(document.orderform.shipping.value));
	}
else {
	document.orderform.total.value = format(
	Number(document.orderform.sub_total.value) + 
	Number(document.orderform.ca_tax.value));	
	}
}

var code = 0;
function doDiscount(code){
	var value = "1.0";
	var input = hex_md5(code.toLowerCase());
	// to change codes, generate new one by putting in place of 'blah'.
	// alert('new code: ' + hex_md5('blah'));
	if (input == "c00d87bb008065adab7dba62f90e822a"){ value = ".85"; }
	else if (input == "d91cfcb75db7067f96d076c39005de7c"){ value = ".75"; }
	else { value = "1.0"; }
	return value;
}
 
function doOrder(){
    doTotal();
	document.orderform.submit();
}

// end cookie and popup 
function storeData() {
  var days = 3650;
  var expdate = new Date();
  expdate.setTime (expdate.getTime() + (86400 * 1000 * days));
  setCookie("dontShowPopupAgain", "true", expdate);
  return false;
}
function retrieveData() {
  if (!getCookie("dontShowPopupAgain")){
	if(location.protocol.indexOf("https:") == -1) { 
		window.open("pop.html","EXTLINK","resizable=0,status=0,scrollbars=1,menubar=1,toolbar=1,location=0,top=50,left=50,width=450,height=400");
		storeData();
		}
	}
}
function setCookie (name, value, expires) {
  document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
}
function getCookie(name) {
  var search;
  search = name + "="
  offset = document.cookie.indexOf(search) 
  if (offset != -1) {
    offset += search.length ;
    end = document.cookie.indexOf(";", offset) ;
    if (end == -1)
      end = document.cookie.length;
    return unescape(document.cookie.substring(offset, end));
  }
  else
    return "";
}


// end cookie and popup 


