var i = 0;
var j = 0;
var Total = 0;
var numItems = 2;
var shipping = 0;
numStr = new String("");
dpyStr = new String("");
orderStr = new String("");

function get_shipCost(num) {
	i = shipping - 1;
    j = 900 + i * 300;
    if (document.buy.x_Ship_To.selectedIndex == 2) {j = 2000 + i * 350;}
	if (document.buy.x_Ship_To.selectedIndex == 3) {j = 2800 + i * 400;}
	shipping = j;
}

function dollars(num) {
    var lng = 0;
	var dot = 0;
	if (num) {
		dpyStr = num + "";
	    lng = dpyStr.length;
		dot = lng - 2;
		numStr = dpyStr.substring(0,dot) + "." + dpyStr.substring(dot,lng);
	} else {numStr = "0.00";} 
}

function updateAmount() {
  var subTotals = 0;
  var jj=0;
  var k=0;
  var x=0;
  var l=0;
  var ii=0;
  var totalWeight=0;
  prices = new Array()
  prices[0] = 2995;
  prices[1] = 3995;
  prices[2] = 1995;
  prices[3] = 0;
  itemName = new Array()
  itemName[0] = "Printed Book: Relationship Tools for Positive Change ";
  itemName[1] = "Printed Edition: The Relationship Cards ";
  itemName[2] = "Music CD: The Heart Also Opens ";
  itemName[3] = 0;
  weight = new Array()
  weight[0] = 1;
  weight[1] = 2;
  weight[2] = .5;
  weight[3] = 0;
  weight[4] = 0;

  for (i=0; i<numItems; i++) {
	if (document.buy.elements[i].checked == true) {jj = 1;} else {jj  =0;}
	x = jj*weight[i];
	totalWeight += x;
    jj *= prices[i];
    subTotals += jj;
  }
  jj = subTotals;

  orderStr = "";
  if (jj) {
	for (i=0; i<numItems; i++) {
		if (document.buy.elements[i].checked == true) {
			orderStr += itemName[i];
			for (ii=i+1; ii<numItems; ii++) {
				if (document.buy.elements[ii].checked == true) {
					orderStr += "<br>\n";
					ii = numItems;
				}
			}
		}
	}
  }

  document.buy.x_Description.value = orderStr;
  dollars(subTotals);
  document.buy.SubTotal.value = numStr;
  
  shipping = Math.ceil(totalWeight);
  if (shipping) {
  	if (shipping < 1) shipping = 1;
  	get_shipCost(); 
  }
  jj += shipping;

  Total = jj;
  dollars(shipping);
  dpyStr = "";
  l = document.buy.SubTotal.value.length - numStr.length;
  for (i=0; i<l; i++) { dpyStr += " ";}
  document.buy.Shipping.value = dpyStr + numStr;
  dollars(Total);
  document.buy.Amount.value = numStr;
  document.buy.x_Amount.value = numStr;
  document.buy.SubTotal.focus();
  document.buy.Shipping.focus();  
  document.buy.Amount.focus();
  document.buy.Amount.blur();
}

function orderNow(num) {
		str = window.location.search;
		if (str.length) {
			window.location = "shopping-cart.html?3"
		} else window.location = "shopping-cart.html?" + num;
}

function itemOrdered() {
		str = window.location.search;
		if (str.length) {
			i = str.substring(1,str.length);
			if (i == 3) {
				document.buy.elements[0].checked = true;
				document.buy.elements[1].checked = true;
			} else {
				i = i - 1;
				document.buy.elements[i].checked = true;
			}
			updateAmount();
		}
}

function showItem(num) {
	if (num == 1) {
		if (document.buy.elements[1].checked == true) {
			window.location = "book.html?2";
		} else window.location = "book.html";
	} else if (num == 2) {
		if (document.buy.elements[0].checked == true) {
			window.location = "cards.html?1";
		} else window.location = "cards.html";
	}
}

function CheckOut() {
	updateAmount();
	
//	if (!confirm("We are on vacation and will not ship your order until May 8. If you want to order now click OK, otherwise Cancel.")) {
//		return false;
//	}

	if (document.buy.x_Ship_To.selectedIndex == 0) {
		alert("You have not selected a Shipping Address. Pick the country you want this order shipped to by selecting from the menu that says Choose One.");
		return false;	
	}
	if (Total) { 
	//	NO REFUNDS POLICY
		if (shipping && document.buy.x_Ship_To.selectedIndex > 1 && !confirm("Please affirm by clicking OK that you have read and you agree to our policies regarding International Shipments outside of the US. If you do not agree click Cancel. To read our policies first click Cancel and then click on the Customer Support link located under the Purchase Now button. Thank you.")) {return false;}
		return true;
	} else {
		alert("You have not selected an item to order. Please set the quantity value for items you wish to order.");
		return false;	
	}
}
