
var selIndex;
var subTotalAdditions;
var binPrice;
var binDeliveryCharge;
var subTotal;
var purchaseType = "";

function calculateCost() {
var subTotal120 = 0;
var subTotal240 = 0;
var subTotal1100 = 0;
var subCompostBins = 0;

//*check to see at least one bin has been requested*/
//if (document.frmLicence.txt120Bins.selectedIndex==0 & document.frmLicence.txt240Bins.selectedIndex==0 & document.frmLicence.txt1100Bins.selectedIndex==0 & document.frmLicence.txtCompostBins.selectedIndex==0)
//   {
//        document.getElementById("txt120Bins").focus();
//        window.alert("Please select a minimum of 1 bin from the options below.");
//        return false;
//    }

/*check to see which radio button choosen*/
if (document.frmLicence.rdoPurchaseType.rdotrade.checked == true)
    {purchaseType = "Price";}
else
    {purchaseType = "PriceDomestic";}

   
if (document.getElementById("txt120Bins").value > 0) 
{
    var txt120Bins = document.getElementById("txt120Bins").value;    
    
    binPrice = document.getElementById('1'+ purchaseType).innerHTML;/*get price for bins*/
    binPrice = Right(binPrice,String(binPrice).length-1);/*remove £*/
    binPrice = parseFloat(binPrice);
    
    if (document.frmLicence.rdoDelColl.rdoDelivered.checked == true){
        binDeliveryCharge = document.getElementById('1DelCost').innerHTML; /*get delivery for bins*/
        binDeliveryCharge = Right(binDeliveryCharge,String(binDeliveryCharge).length-1);/*remove £*/
        binDeliveryCharge = parseFloat(binDeliveryCharge);
    }
    else{
        binDeliveryCharge = 0;
    }
    
    subTotal120 = (txt120Bins * binPrice) + binDeliveryCharge;
 }   
 
 
if (document.getElementById("txt240Bins").value > 0) 
{

    var txt240Bins = document.getElementById("txt240Bins").value;    
    
    binPrice = document.getElementById('2'+ purchaseType).innerHTML;/*get price for bins*/
    binPrice = Right(binPrice,String(binPrice).length-1);/*remove £*/
    binPrice = parseFloat(binPrice);
    
    if (document.frmLicence.rdoDelColl.rdoDelivered.checked == true){
        binDeliveryCharge = document.getElementById('2DelCost').innerHTML; /*get delivery for bins*/
        binDeliveryCharge = Right(binDeliveryCharge,String(binDeliveryCharge).length-1);/*remove £*/
        binDeliveryCharge = parseFloat(binDeliveryCharge);
        }
    else{
        binDeliveryCharge = 0;
    }
    
    subTotal240 = (txt240Bins * binPrice) + binDeliveryCharge;
 }  
 
 
if (document.getElementById("txt1100Bins").value > 0) 
{

    var txt1100Bins = document.getElementById("txt1100Bins").value;    
    
    binPrice = document.getElementById('3'+ purchaseType).innerHTML;/*get price for bins*/
    binPrice = Right(binPrice,String(binPrice).length-1);/*remove £*/
    binPrice = parseFloat(binPrice);
    
    if (document.frmLicence.rdoDelColl.rdoDelivered.checked == true){
        binDeliveryCharge = document.getElementById('3DelCost').innerHTML; /*get delivery for bins*/
        binDeliveryCharge = Right(binDeliveryCharge,String(binDeliveryCharge).length-1);/*remove £*/
        binDeliveryCharge = parseFloat(binDeliveryCharge);
        }
    else{
        binDeliveryCharge = 0;
    }
    
    subTotal1100 = (txt1100Bins * binPrice) + binDeliveryCharge;
 }  
    
    
    
 if (document.getElementById("txtCompostBins").value > 0) 
{

    var txtCompostBins = document.getElementById("txtCompostBins").value;    
    
    binPrice = document.getElementById('4'+ purchaseType).innerHTML;/*get price for bins*/
    binPrice = Right(binPrice,String(binPrice).length-1);/*remove £*/
    binPrice = parseFloat(binPrice);
    
    if (document.frmLicence.rdoDelColl.rdoDelivered.checked == true){
        binDeliveryCharge = document.getElementById('4DelCost').innerHTML; /*get delivery for bins*/
        binDeliveryCharge = Right(binDeliveryCharge,String(binDeliveryCharge).length-1);/*remove £*/
        binDeliveryCharge = parseFloat(binDeliveryCharge);
        }
    else{
        binDeliveryCharge = 0;
    }
    subCompostBins = (txtCompostBins * binPrice) + binDeliveryCharge;
 }   
    
    document.getElementById("txtTotalCost").value = formatCurrency((subTotal120 + subTotal240 + subTotal1100 + subCompostBins));
    
    return true;
}


function Right(str, n)
/***
IN: str - the string we are RIGHTing
    n - the number of characters we want to return

RETVAL: n characters from the right side of the string
***/
{
    if (n <= 0)     // Invalid bound, return blank string
       return "";
    else if (n > String(str).length)   // Invalid bound, return
       return str;                     // entire string
    else { // Valid bound, return appropriate substring
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

/*converts number to a currency format*/
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '£' + num + '.' + cents);
}
