var i = 0;
var j = 0;
var Total = 0;
var subTotal = 0;
var shipping = 0;
var firstTime = 1;
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() {
  subTotal = 0;
  Total = 0;
  shipping = 0;
  orderStr = "";
  if (document.buy.book[0].checked) {
  	subTotal += 1495;
	orderStr += "E-Book Download: Relationship Tools for Positive Change <br>\n";
  }
  if (document.buy.book[1].checked) {
  	subTotal += 2995;
	shipping += 1;
	orderStr += "Printed Book: Relationship Tools for Positive Change <br>\n";
  }
  if (document.buy.cards[0].checked) {
	if (document.buy.book[0].checked) subTotal += 995
		else subTotal += 1495;
	orderStr += "Download Files: Relationship Advisor <br>\n";
  }
  if (document.buy.cards[1].checked) {
  	subTotal += 3995;
	shipping += 2;
	orderStr += "Printed Edition: Relationship Advisor (The Relationship Cards) <br>\n";
  }


  dollars(subTotal);
  document.buy.Items.value = numStr;
  
  if (shipping) { 
  	get_shipCost(); 
	document.buy.x_Ship_It.value = "YES";
  } else document.buy.x_Ship_It.value = "NO";

  
  dollars(shipping);
  document.buy.Ship.value = numStr;

  Total = subTotal + shipping;
  dollars(Total);
  document.buy.Amount.value = numStr;
  document.buy.x_Amount.value = numStr;
  document.buy.x_Description.value = orderStr;
  if (!firstTime) {
  	document.buy.Amount.focus();
  	document.buy.Amount.blur();
  } else {firstTime = 0;}
}

function CheckOut() {
	updateAmount();
//	alert(shipping);
//	if (shipping && !confirm("We are on vacation and will not ship printed books until May 8. If you want to order now click OK, otherwise Cancel.")) {
//		return false;
//	}
	if (shipping && document.buy.x_Ship_To.selectedIndex == 0) {
		alert("You have not set a destination country for your shipment. Please select a country in the menu, where it now reads \"Not Being Shipped\"");
		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 select an item to order by clicking in one of the buttons.");
		return false;	
	}
}
