currentObj = "";
var maximum = 70; //maximum delivery charge
var minimum = 0.30;

function onCartLoad(){
	if(!(document.getElementById && window.frames)){ //if automatic updates not supported then write the button
		obj = document.getElementById("recalculate");
		var inputObj = document.createElement('INPUT');
       	inputObj.name="recalulate";
		inputObj.type="submit";
		inputObj.value="Re calculate..";
	    obj.appendChild(inputObj);
	}
	//alert(document.getElementById("totalHidden").value);
}

function setVars(obj){
	if(document.getElementById && window.frames){	
		currentObj = obj;
		previousValue = obj.value;
	}
}

function updateOrder(obj){
	
	if(document.getElementById && window.frames){	
		if(obj.value == previousValue || obj.value==""){ //value hasn't changed OR no value so do nothing
			 return; 
		}else{ //value has changed do update the records
			if(!obj.value.match(/^[0-9]+$/)){
				obj.value = previousValue;
				alert("Please enter whole numbers only");
				return;
			}
						
			objName = obj.getAttributeNode("name").value;
			pos = objName.substring(3,objName.length);
			qty = obj.value;
				
			//also update the price
			unitPrice = document.getElementById("unitPrice"+pos).value;
			document.getElementById("price"+pos).innerHTML=addDecimal(Math.round((unitPrice*qty)*100)/100);
	
			changeNum = obj.value - previousValue; //change in number
			change = Math.round((unitPrice*changeNum)*100)/100; //round number to two decimal places
			thetotal = document.getElementById("totalHidden").value;
			
			newTotal =  Math.round((parseFloat(thetotal) + parseFloat(change))*100)/100;
			
			//update previous value
			previousValue = obj.value;
			document.getElementById("hiddenfr").src ="/loader/clipboardSet.php?pos="+pos+"&qty="+qty+sessionPass; //update the session
		}
	}
}

function updateOrderDelivery(ob){ //this function is called when the user enables/disables delivery
	if(document.getElementById && window.frames){
		deliv = Math.round(document.getElementById("delivHidden").value*100)/100;
		deliveryAmount = (deliv>maximum)? maximum:((deliv<minimum)?minimum:deliv);
		deliveryAmount =  parseFloat(deliveryAmount)
		if(ob.checked){
			//document.getElementById("deliveryCharge").innerHTML = addDecimal(deliveryAmount);
			//newTotal = eval(document.getElementById("totalHidden").value) + deliveryAmount;
			//document.getElementById("totalHidden").value = Math.round(newTotal*100)/100;
			//document.getElementById("total").innerHTML =  addDecimal(Math.round(newTotal*100)/100);
			
			//must set a session variable to indicate that the goods will be picked up
			document.getElementById("hiddenfr").src ="/loader/clipboardSet.php?setDelivery=1"+sessionPass; //update the session

		}else{
			//alert("totalHidden: "+document.getElementById("totalHidden").value+" deliveryAmount: "+deliveryAmount);
			document.getElementById("deliveryCharge").innerHTML = "0.00";
			newTotal = document.getElementById("totalHidden").value - deliveryAmount;
			document.getElementById("totalHidden").value = newTotal;
			document.getElementById("total").innerHTML =  addDecimal(Math.round(newTotal*100)/100);
			document.getElementById("hiddenfr").src ="/loader/clipboardSet.php?unsetDelivery=1"+sessionPass; //update the session
		}
	}
	
}

function addDecimal(amount){
	s = new String(amount); // just creates the info string
	decimal = s.indexOf("."); // Finds decimal place
	if (decimal == -1) { //Checks if number is a whole number
		s+= ".00"; // Appends a decimal point & 2 "0"s to whole number
	} else {
		if (decimal == (s.length - 2)) { // Checks for single-decimal point number
			s+= "0"; //Adds a single trailing "0"
		}
	}
	return s;
}

cms_addOnloadEvents(onCartLoad);