var xmlHttp

function showCost(qty,prod,txtpos)
{
if (qty.length==0)
  { 
  var unitcostfield="unitCost"+txtpos;
  var txtfield="txtCost"+txtpos;
  document.getElementById(unitcostfield).innerHTML="0";
  document.getElementById(txtfield).innerHTML="0";
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }        
var url="getcostperitem.asp";
url=url+"?items="+qty;
url=url+"&prod="+prod;
xmlHttp.onreadystatechange=function() 
{
	stateChanged(txtpos,qty);
}
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
} 

function showTable(prod)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }        
var url="getcoststable.asp";
url=url+"?prod="+prod;
xmlHttp.onreadystatechange=function() 
{
	stateChangedtable(prod);
}
xmlHttp.open("get",url,false);
xmlHttp.send(null);

return xmlHttp.responseText;
} 


function stateChanged(txtpos,qty) 
{ 
if (xmlHttp.readyState==4)
   {
   var unitcostfield="unitCost"+txtpos;
   var txtfield="txtCost"+txtpos;   
   var strTotal
   document.getElementById(unitcostfield).innerHTML=round_decimals(xmlHttp.responseText,2); 
   strTotal=round_decimals((xmlHttp.responseText*qty),2)
   document.getElementById(txtfield).innerHTML=strTotal;
   if (txtpos == 1) {
   	paypalform1.amount.value=strTotal;
   	paypalform1.os0.value=qty;
   	}
   if (txtpos == 2) {
   	paypalform2.amount.value=strTotal;
   	paypalform2.os0.value=qty;
   	}   	
   if (txtpos == 3) {
   	paypalform3.amount.value=strTotal;
   	paypalform3.os0.value=qty;
   	}
   if (txtpos == 4) {
   	paypalform4.amount.value=strTotal;
   	paypalform4.os0.value=qty;
   	}
   if (txtpos == 5) {
   	paypalform5.amount.value=strTotal;
   	paypalform5.os0.value=qty;
   	}
   if (txtpos == 6) {
   	paypalform6.amount.value=strTotal;
   	paypalform6.os0.value=qty;
   	}
   if (txtpos == 7) {
   	paypalform7.amount.value=strTotal;
   	paypalform7.os0.value=qty;
   	}
   if (txtpos == 8) {
   	paypalform8.amount.value=strTotal;
   	paypalform8.os0.value=qty;
   	}
   if (txtpos == 9) {
   	paypalform9.amount.value=strTotal;
   	paypalform9.os0.value=qty;
   	}   	
   if (txtpos == 10) {
   	paypalform10.amount.value=strTotal;
   	paypalform10.os0.value=qty;
   	}
   if (txtpos == 11) {
   	paypalform11.amount.value=strTotal;
   	paypalform11.os0.value=qty;
   	}
   if (txtpos == 12) {
   	paypalform12.amount.value=strTotal;
   	paypalform12.os0.value=qty;
   	}   	   
   if (txtpos == 13) {
   	paypalform13.amount.value=strTotal;
   	paypalform13.os0.value=qty;
   	}   	
   if (txtpos == 14) {
   	paypalform14.amount.value=strTotal;
   	paypalform14.os0.value=qty;
   	}
   if (txtpos == 15) {
   	paypalform15.amount.value=strTotal;
   	paypalform15.os0.value=qty;
   	}
   if (txtpos == 16) {
   	paypalform16.amount.value=strTotal;
   	paypalform16.os0.value=qty;
   	}   	   	   	
   if (txtpos == 17) {
   	paypalform17.amount.value=strTotal;
   	paypalform17.os0.value=qty;
   	}
// Updates 0709 : Add extra forms :-
   if (txtpos == 18) {
   	paypalform18.amount.value=strTotal;
   	paypalform18.os0.value=qty;
   	}   	   
   if (txtpos == 19) {
   	paypalform19.amount.value=strTotal;
   	paypalform19.os0.value=qty;
   	}   	
   if (txtpos == 20) {
   	paypalform20.amount.value=strTotal;
   	paypalform20.os0.value=qty;
   	}
   if (txtpos == 21) {
   	paypalform21.amount.value=strTotal;
   	paypalform21.os0.value=qty;
   	}
   if (txtpos == 22) {
   	paypalform22.amount.value=strTotal;
   	paypalform22.os0.value=qty;
   	}   	   	   	
   if (txtpos == 23) {
   	paypalform23.amount.value=strTotal;
   	paypalform23.os0.value=qty;
   	}
   }
}

function stateChangedtable(prod) 
{ 
if (xmlHttp.readyState==4)
   {
   var tdfield="table"+prod;
   var tdresult=xmlHttp.responseText; 
   }
}


function round_decimals(num, dec) {
    var result1 = num * Math.pow(10, dec);
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, dec)
    return pad_with_zeros(result3, dec)
}



function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}



function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}


function listItems(itemList) 
{
   document.write("<TD>")
   for (i = 0;i < itemList.length;i++)
   {
      document.write("<LI>" + itemList[i] + "\n")
   }
   document.write("</UL>\n") 
} 

