﻿//
// SAVE AS UTF-8!
//

// Get Element By ID Function
if(!document.getElementById){
	function getElementById(id_var, d, dl){
		if(!d)d=document;dl=d.layers;
			db=document.all?d.all[id_var]:d[id_var];
		if(dl&&!db)for(var i=0;i<dl.length;i++)
			db=getElementById(id_var, dl[i].document);
		if(!db.style)db.style=db;return db;
	}
	document.getElementById=window.getElementById;
}

function replaceChars(entry, out, add) {
	temp = "" + entry; // temporary holder

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
return temp;
}

function splitThousands(pString) {

	var newString = ""
	for (i = 0; i <= pString.length; i++){
		newString = pString.substr(pString.length-i, 1) + newString;
		if (i<pString.length) {
			if (i == 3 || i == 6 || i == 8 || i == 12)
				newString = "," + newString;
		}
	}
	return newString;

}

function formatMoney(pString) {

	var returnString = '';
	
	var pNumber = parseFloat(pString)
	pNumber = pNumber * 100;
	pNumber = Math.round(pNumber);
	pNumber = pNumber / 100;
	pString = pNumber.toString();

	if (pString.lastIndexOf('.') == -1) {
		pString = pString + '.00';
	}

	if (pString.lastIndexOf('.') < pString.length-3) {
		pString = pString.substring(0, pString.lastIndexOf('.')+3);
	}

	if (pString.lastIndexOf('.') == pString.length-1) {
		pString = pString + '00';
	}

	if (pString.lastIndexOf('.') == pString.length-2) {
		pString = pString + '0';
	}
	
	returnString = "£" + pString
	return returnString;
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function getNumber(s) {
	var i;
	var numberString = ""
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (isInteger(c) || c == '_') {
			numberString = numberString + c;
		}
    }
    return numberString;
}

function getElements(pElement, pageElements) {
	var oElement = null;
	var j;
	if (1 == 1) {
		for (j=0; j<(pElement.childNodes.length); j+=1) {
			oElement = pElement.childNodes[j];
			if (oElement.nodeType == 1) {
				if (oElement.id != '') {
	 				pageElements[pageElements.length] = oElement;
				}
			}
			getElements (oElement, pageElements);		
		}
	}
}

function locateElement(elementString) {
	//if (document.getElementById('test')) {
	//	if (document.getElementById(elementString)) {
	//		return document.getElementById(elementString);
	//	}
	//	else {
	//		return false;
	//	}
	//}
	//else {
	//	var pageElements =  new Array();
	//	var oElement;
	//	oElement = false;
	//	var i;
	//	getElements(document.body, pageElements);
	//	for (i=0; i<(pageElements.length); i+=1) {
	//		if (pageElements[i].id == elementString) {
	//			oElement =  pageElements[i];
	//			}
	//	}
	//	return oElement;
	//}

	if (document.getElementById(elementString))
		return document.getElementById(elementString)
	else
		return false;
}


function getTextInputs(pString, pElement, totalQty) {
	var oElement = null;
	var j;
	for (j=0; j<(pElement.childNodes.length); j+=1) {
		oElement = pElement.childNodes[j];
		if (oElement.nodeType == 1) {
			if (oElement.tagName == 'INPUT') {
				if (oElement.id.substring(0,2)=='s-')
					if (isInteger(oElement.value) && oElement.value.length > 0)
						totalQty = totalQty + parseInt(oElement.value);
			}
		}
		totalQty = getTextInputs (pString, oElement, totalQty);		
	}
	return totalQty;
}

function getDropValue(pString) {

	var retValue = false;
	var i = 0;
	
	for (i=0; i<(locateElement(pString).options.length); i+=1) {
			if (locateElement(pString).options[i].selected)
				retValue = locateElement(pString).options[i].value;
	}
	
	return retValue;

}

function getDropText(pString) {

	var retValue = false;
	var i = 0;
	
	for (i=0; i<(locateElement(pString).options.length); i+=1) {
			if (locateElement(pString).options[i].selected)
				retValue = locateElement(pString).options[i].text;
	}
	
	return retValue;

}


function updateValues (pName) {
	
	var oElements = null;
	var totalQty = 0;
	
	totalQty =  getTextInputs('s-', locateElement(pName), totalQty);
	locateElement('t-' + getNumber(pName)).value = totalQty;
	if (!yen)
		locateElement('tp-' + getNumber(pName)).value = formatMoney(replaceChars(locateElement('p-' + getNumber(pName)).value, ',', '.') * totalQty)
	else {
		locateElement('tp-' + getNumber(pName)).value = formatMoney(replaceChars(Math.round(locateElement('p-' + getNumber(pName)).value), ',', '.') * totalQty)
	}
	
	totalQty = 0;
	oElements = document.getElementsByTagName("INPUT");
	
	for (j=0; j<(oElements.length); j+=1) {
		if (oElements[j].id.substring(0,3)=='tp-') {
			totalQty = totalQty + 	parseFloat(replaceChars(oElements[j].value.substr(oElements[j].value.lastIndexOf('Ã‚Â£')+1, oElements[j].value.length), ",", "."));
		}
	}
	locateElement('totalPrice').value = formatMoney(totalQty);
		
}

function reCalcVat() {

	var countrySelected = 0;
	for (i=0; i<(locateElement('countryId').options.length); i+=1) {
			if (locateElement('countryId').options[i].selected)
				countrySelected = locateElement('countryId').options[i].value;
	}
	
	locateElement('vatPrice').value = formatMoney(totalVat * payVat[countrySelected]);
	locateElement('totalPrice').value = formatMoney(totalPrice + (totalVat * payVat[countrySelected]));

}

function calcExVat(pId, vat) {

	var tempValue = 0;
	var tempValueS = 0;

	if (EUbased) {
		tempValue = (parseFloat(locateElement('priceVat-' + pId).value) / (100 + parseFloat(vat))) * 100;
	}
	else {
		tempValue = (parseFloat(locateElement('priceVat-' + pId).value));
	}
	
	if (EUbased) {
		tempValueS = (parseFloat(locateElement('priceSVat-' + pId).value) / (100 + parseFloat(vat))) * 100;
	}
	else {
		tempValueS = (parseFloat(locateElement('priceSVat-' + pId).value));
	}
	
	if (isNaN(tempValue)) {
		tempValue = 0;
	}
	
	if (isNaN(tempValue)) {
		tempValueS = 0;
	}
	
	tempValue=Math.round(tempValue*1000)/1000
	tempValueS=Math.round(tempValueS*1000)/1000
	locateElement('price-' + pId).value = tempValue;
	locateElement('priceS-' + pId).value = tempValueS;
}

function calcWithVat(pId, vat) {

	var tempValue = 0;
	var tempValueS = 0;
	
	if (EUbased) {
		tempValue = (parseFloat(locateElement('price-' + pId).value) / (100)) * (100 + vat);
	}
	else {
		tempValue = (parseFloat(locateElement('price-' + pId).value));
	}
	
	if (EUbased) {
		tempValueS = (parseFloat(locateElement('priceS-' + pId).value) / (100)) * (100 + vat);
	}
	else {
		tempValueS = (parseFloat(locateElement('priceS-' + pId).value));
	}
	
	tempValue=Math.round(tempValue*100)/100
	tempValueS=Math.round(tempValueS*100)/100	
	if (isNaN(tempValue)) {
		tempValue = 0;
	}
	if (isNaN(tempValueS)) {
		tempValueS = 0;
	}
	locateElement('priceVAT-' + pId).value = tempValue
	locateElement('priceSVAT-' + pId).value = tempValueS
}


function checkKeysNumeric(e) {
	if (document.all) {	
		if ((event.keyCode < 48 || event.keyCode > 57) ) {
			event.returnValue = false;
			return false;
		}
	}
	else {
		if ((e.which < 48 || e.which > 57) && e.which != 0 && e.which!=8) {
			e.cancelBubble = true;
			return false;
		}
	}			
}

function checkKeysNumericDecimal(e) {
	if (document.all) {	
		if (event.keyCode != 46 &&((event.keyCode < 48 || event.keyCode > 57))) {
			event.returnValue = false;
			return false;
		}
	}
	else {
		if (e.which != 46 && ((e.which < 48 || e.which > 57) && e.which != 0 && e.which!=8)) {
			e.cancelBubble = true;
			return false;
		}
	}			
}

function nullInput(pElement) {
	//if (pElement.id == 's-2218') {
	//	if (pElement.value != '0' && pElement.value !='')
	//		pElement.value = '1';
	//}
	//if (pElement.id == 's-2249') {
	//	if (pElement.value != '0' && pElement.value !='')
	//		pElement.value = '1';
	//}

	//if (pElement.id == 's-2250') {
	//	if (pElement.value != '0' && pElement.value !='')
	//		pElement.value = '1';
	//}

	//if (pElement.id == 's-2219') {
	//	if (pElement.value !='')
	//		pElement.value = '0';
	//}
	if (pElement.value == '')
		pElement.value = '0';
}


function toggle(pString) {

if (locateElement(pString).style.display == 'none')
	locateElement(pString).style.display = 'block'
else
	locateElement(pString).style.display = 'none'
}

function writeHTML(pString) {
	document.write(pString);
}