var linetotalID;
var unitpriceID;
var currentLineQty;
var updateDiscountTable;

function getFile(pURL) {
if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc 
	xmlhttp=new XMLHttpRequest();
	xmlhttp.onreadystatechange=postFileReady;
	xmlhttp.open("GET", pURL, true);
	xmlhttp.send(null);
} else if (window.ActiveXObject) { //IE 
	xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
	if (xmlhttp) {
		xmlhttp.onreadystatechange=postFileReady;
		//alert('getFile: ' + pURL)
		xmlhttp.open('GET', pURL, false);
		xmlhttp.send();
	}
}
}

// function to handle asynchronous call
function postFileReady() {
var docElement;
var lineTotalResponse;
var lineTotalFloat;
var unitPrice;
var sColonIndex;
var unitPriceElement;
var discPerc;
var spnWebDiscAmount;
var spnWebDiscTotal;
var discountAmount;
var discountTotal;
var totalPriceRnd;


if (xmlhttp.readyState==4) { 
	if (xmlhttp.status==200) {
		docElement = document.getElementById(linetotalID);
		unitPriceElement = document.getElementById(unitpriceID);
		lineTotalResponse = xmlhttp.responseText;
		sColonIndex = lineTotalResponse.indexOf(';', 0)
		lineTotalFloat = parseFloat(lineTotalResponse.substring(0, sColonIndex));
		unitPrice = parseFloat(lineTotalResponse.substr(sColonIndex + 1));
		
		docElement.innerHTML = '&pound;' + lineTotalFloat.toFixed(2);
		unitPriceElement.innerHTML = '&pound;' + unitPrice.toFixed(2);
		calculateTotal(totalArr);
		
		if (updateDiscountTable)
		{
			discPerc = document.Form1.hdnDiscPerc.value;
			spnWebDiscAmount = document.getElementById(spnWebDiscAmountCtrl);
			spnWebDiscTotal = document.getElementById(spnWebDiscTotalCtrl);
			
				
			totalPriceRnd = totalPrice.toFixed(2);
			discountAmount = ((totalPriceRnd * (discPerc / 100)).toFixed(2));
			spnWebDiscAmount.innerHTML = discountAmount;
			discountTotal = ((totalPriceRnd - discountAmount).toFixed(2));
			spnWebDiscTotal.innerHTML = discountTotal;
			
			updateDiscountTable = false;
		}
	}
}
}

function lt(lineTotalCtrl, productCode, quantityCtrl, UnitPricectrl, currentLineQuantity)
{
	var quantity;
	var strURL;
	
	currentLineQty = currentLineQuantity;
	quantity = (parseInt(document.getElementById(quantityCtrl).value)>0?parseInt(document.getElementById(quantityCtrl).value):0) + currentLineQty;
	linetotalID = lineTotalCtrl;
	unitpriceID = UnitPricectrl;
	strURL ='LineTotalCalculator.aspx?quantity=' + quantity + '&productCode=' + productCode;
	updateDiscountTable = false;
	getFile(strURL);
}

function ltd(lineTotalCtrl, productCode, quantityCtrl, UnitPricectrl, currentLineQuantity, spnWebDiscAmountCtrl, spnWebDiscTotalCtrl)
{
	updateDiscountTable = true;
	lt(lineTotalCtrl, productCode, quantityCtrl, UnitPricectrl, currentLineQuantity);	
}