function getIndex(what) {
	for (var i=0;i<document.lists.elements.length;i++)
	  if (what == document.lists.elements[i])
		return i;
	return -1;
}

Number.prototype.toDecimals=function(n){
n=(isNaN(n))? 2: n;
var nT=Math.pow(10,n);
function pad(s){
	  s=s||'.';
	  return (s.length>n)? s: pad(s+'0');
}
return (isNaN(this))? this: (new String(Math.round(this*nT)/nT)).replace(/(\.\d*)?$/,pad);
}

function is_numeric(num) {
var exp = new RegExp('^[0-9-.]+$','g');
return exp.test(num);
}

var x;

function changeOrder(url) {
// create the object, careful to the MSFT/Other method
	if (window.XMLHttpRequest) { x = new XMLHttpRequest(); }
	else if (window.ActiveXObject) { x = new ActiveXObject("Microsoft.XMLHTTP"); }
// executing the request, passing the targetted object
	x.open("GET", url, true);
	x.onreadystatechange = function () {processRequestChange()};
	x.send(null);
}

function processRequestChange() {
	if (x.readyState == 4) {
		if(x.status == 200) {
		  var rd = x.responseText;
			if (rd == 'reload') { clearAllFields(); }
			else if (rd.substring(1, 9) == 'fieldset') { document.getElementById("checkout").innerHTML = rd; }
			else {  document.getElementById("billing_info").innerHTML = rd; }
		}
	}
}

function amount(what, info, type, counter) {
var ia = info.split("|");
/*var iam = document.lists.elements[getIndex(what)].value;*/
var iam = what.value;
var price = ia[0];
var ic = ia[1];
var it = price*iam;
/*var st = document.lists.elements[getIndex(what)+1];*/
var st = document.getElementById("sub_"+counter);
st.value = '$'+it.toDecimals(2);
if (type == true) { changeOrder('/billing_info.php?code='+ic+'&value='+it); }
calculateTotal();
}

function calculateTotal() {
var c = document.lists.elements["items"];
var ca = 0;
for (var i = 0; i < c.length; i++) {
  if (c[i].value != "") { ca = ca+parseInt(c[i].value); }
}
var itm = document.lists.elements["sub"];
var sa = "";
for (var i = 0; i < itm.length; i++) {
  if (itm[i].value != "") { sa += itm[i].value; }
}
mv = sa.split("$");
var gt = 0.00;
for (var i = 0; i < mv.length; i++) {
  if (is_numeric(mv[i])) {
	var tp = parseFloat(mv[i]);
	gt = gt+tp;
  }
}
document.getElementById("total").innerHTML = 'total items: '+ca+' | total price: $'+gt.toDecimals(2)+' <a href="#" onclick="viewTab(this, \'results\');">Checkout</a>';
}

function clearAllFields() {
  var af = document.lists.getElementsByTagName("input");
  for (var i = 0; i < af.length; i++) {
    af[i].value = "";
  }
  document.getElementById("total").innerHTML = 'total items: 0 | total price: $0.00 <a href="#" onclick="viewTab(this, \'results\');">Checkout</a>';
  document.getElementById("billing_info").innerHTML = '';
  viewTab(document.getElementById("first"), 'tab_a');
}

function viewTab(obj, what) {
  var at = document.getElementsByTagName("table");
  for (var i = 0; i < at.length; i++) {
    if (at[i].getAttribute("id") != 'sitenav') {
      at[i].style.display = "none";
    }
  }
  document.getElementById("menu").style.display = "block";
  document.getElementById(what).style.display = "block";
  var atb = document.getElementById("menu").getElementsByTagName("a");
  for (var i=0; i < atb.length; i++) {
  	if (atb[i].className == 'here') { atb[i].className = 'none'; }
	if (atb[i].className == 'last_here') { atb[i].className = 'last'; }
  }
  if (obj.className == 'last') { obj.className = 'last_here'; }
  else { obj.className = 'here'; }
  if (what == 'results') {
    var tabs = document.getElementsByTagName("a");
    for (var i = 0; i <tabs.length; i++) { tabs[i].className = "none"; }
    document.getElementById("checkout").style.display = "block";
  } else {
	document.getElementById("checkout").style.display = "none";
  }
}