function ToggleIdOnCheck(check,id) {
  // Clear all:
  document.getElementById('optCard').style.display='none';
  document.getElementById('optTransfer').style.display='none';
  document.getElementById('optCheque').style.display='none';
  if (check.checked)
    document.getElementById(id).style.display=(navigator.appName.indexOf('Internet Explorer')>0) ? 'block' : 'table';
  else
    document.getElementById(id).style.display='none';
}

function RadioValue(form,name) {
  var radio=document.forms[form].elements[name]; if (!radio) return false;
  for (var i=0; i<radio.length; i++)
    if (radio[i].checked)
      return radio[i].value;
  return false;
}


function NicePrice(price) {
  var decimals=2;
  price=Math.round(price*Math.pow(10,decimals))/Math.pow(10,decimals);
  price=price.toString();
  var dp=price.indexOf('.');
  if (dp==-1) {
    places=0;
    price+=(decimals>0) ? '.' : '';
  }
  else
    places=price.length-dp-1;
  pad=decimals-places;
  if (pad>0)
    for (iDecimals=0; iDecimals<pad; iDecimals++)
      price+='0';
  return price;
}

function ById(id) {
  return (document.getElementById) ? document.getElementById(id) : false;
}

function changeOption(i) {
  if(i < 4) {
    if(!(parseInt(document.getElementById('qty'+i).value) > 0)) {
      document.getElementById('qty'+i).value = 1;
    }
    if(!(document.getElementById('idsize'+i).selectedIndex > 0)) {
      document.getElementById('idsize'+i).selectedIndex = 1;
      document.getElementById('idsize'+i).value = 1;
    }
  }
  CalculateCost();
}
function changeOptionA(i,caller) {

  var qtyE = document.getElementById('qty'+i);
  var sizeE = document.getElementById('idsize'+i);
  
  var qtyval = parseInt(document.getElementById('qty'+i).value);
  var sizeind = document.getElementById('idsize'+i).selectedIndex;
    
  if(i < 4) {
    if(caller == qtyE && (qtyval == 0 || qtyval=='')) {
      sizeE.selectedIndex = '';
    }
    if(caller == qtyE && qtyval != 0) {
      if(!(sizeind > 0)) {
        sizeE.selectedIndex = 1;
      }
    }
    
    if(caller == sizeE && sizeind == 0) {
       qtyE.value = '';
    }
    if(caller == sizeE && !(sizeind == 0)) {
      if(!(qtyval > 0)) {
        qtyE.value = 1;
      }
    }
    
    
  }
  CalculateCost();
}

function CalculateCost() {
		// var POSTAGE=9; set in config.ini.php and though form template
		var total=0;
    var tweight=0;
		for (i=0;i<4;i++) {
      if(parseInt(document.getElementById('qty'+i).value) > 0) {
  		  var quantity=document.getElementById('qty'+i).value;
  		  var size=document.getElementById('idsize'+i).value;
  		  
        var cost=prices[size];
  		  total+=(quantity*cost);
        tweight+=(quantity*weights[size]);
        
  		  document.getElementById('price'+i).innerHTML = '$'+NicePrice(cost);
  		  document.getElementById('cost'+i).innerHTML = '$'+NicePrice(quantity*prices[size]);
      } else {
  		  document.getElementById('price'+i).innerHTML = '$'+NicePrice(0);
  		  document.getElementById('cost'+i).innerHTML = '$'+NicePrice(0);
        
      }
		}
    for (var lp in listedprices) {
      var quantity = document.getElementById('qty_lp_'+lp).value;
      document.getElementById('cost_lp_'+lp).innerHTML = '$'+NicePrice(quantity*listedprices[lp]);
      total+=quantity*listedprices[lp];
    }
    if(tweight > 2) {
      document.getElementById('excess').innerHTML = 'weight - '+NicePrice(tweight)+'kg Excess postage may occur';
    } else {
    document.getElementById('excess').innerHTML = '';
    }
    if(total > 0) {    
		  total+=POSTAGE;
    }
		document.getElementById('total').innerHTML = '$'+NicePrice(total);
		document.getElementById('card_total').innerHTML = '$'+NicePrice(total);
		document.getElementById('transfer_total').innerHTML = '$'+NicePrice(total);
		document.getElementById('cheque_total').innerHTML = '$'+NicePrice(total);
}

