
// A Function that is called everytime a value is entered in any enabled textbox 
	function compute(){
		computeNetRevenue()
		computePostageCost()
		computeTotalCostAndBreakEven()
	}

	// A Function that computes for the Net Revenue
	function computeNetRevenue() {
			if(checkNumber(myForm.listPrice.value, myForm.listPrice) && checkNumber(myForm.cost.value,  myForm.cost)){
				if (myForm.listPrice.value.length != 0 && myForm.cost.value.length != 0){
					var listPrice = myForm.listPrice.value
					var cost = myForm.cost.value
					var netRevenue
					
					listPrice = parseFloat( listPrice );        
	    			cost = parseFloat( cost );
	    			netRevenue = listPrice - cost;  
					myForm.netRevenue.value = Math.round(netRevenue*100)/100
					
					myForm.postageCost.disabled = false
				}else{
					myForm.netRevenue.value = ""
					myForm.orderNum.value = ""
					myForm.orderRate.value = ""
				}
			}
	}

	// A Function that computes for the Postage Cost 
	function computePostageCost() {
			if(checkNumber(myForm.postageCost.value, myForm.postageCost)){
				if (myForm.postageCost.value.length != 0){
					var postageCost = myForm.postageCost.value
					var poCost
					
					postageCost = parseFloat( postageCost );       
					poCost=postageCost*1000
					myForm.poCost.value = Math.round(poCost*100)/100
					
					myForm.printingCost.disabled = false
					myForm.listRental.disabled = false
					myForm.lshopCost.disabled = false
				}else{
					myForm.poCost.value = ""
					myForm.totalCost.value = ""
				}
			}
	}
	
	// A Function that computes for the Total Cost and the Break Evens 
	function computeTotalCostAndBreakEven() {
			if(checkNumber(myForm.postageCost.value, myForm.postageCost) && checkNumber(myForm.printingCost.value, myForm.printingCost) && checkNumber(myForm.listRental.value, myForm.listRental) && checkNumber(myForm.lshopCost.value, myForm.lshopCost)){
				if (myForm.netRevenue.value.length != 0 && myForm.poCost.value.length != 0 && myForm.printingCost.value.length != 0 && myForm.listRental.value.length != 0 && myForm.lshopCost.value.length != 0){
					var netRevenue = myForm.netRevenue.value
					var poCost = myForm.poCost.value
					var printingCost = myForm.printingCost.value
					var listRental = myForm.listRental.value
					var lshopCost = myForm.lshopCost.value
					var totalCost, poCost, orderNum, orderRate
					
					netRevenue = parseFloat( netRevenue ); 
					poCost = parseFloat( poCost ); 
					printingCost = parseFloat( printingCost );       
					listRental = parseFloat( listRental );       
					lshopCost = parseFloat( lshopCost );       
					totalCost = lshopCost + listRental + printingCost + poCost
					orderNum = totalCost/netRevenue
					orderRate = orderNum/10
					
					myForm.totalCost.value = Math.round(totalCost*100)/100
					myForm.orderNum.value = Math.round(orderNum)
					myForm.orderRate.value = Math.round(orderRate*100)/100 + " %" 
				}else{
					if(myForm.printingCost.value.length == 0 || myForm.listRental.value.length == 0 || myForm.lshopCost.value.length == 0){
						myForm.totalCost.value = ""
					}
					myForm.orderNum.value = ""
					myForm.orderRate.value = ""
				}
			
			}
	}
	
	// A Function that checks if the input is in correct format 
	function checkNumber(input, widget) {
		
		//check for non-integer inputs
		var backup=""
		for (var i = 0; i < input.length; i++) {
			var ch = input.substring(i, i + 1)
			if ((ch < "0" || "9" < ch) && ch != '.') {
				widget.value=backup
				return false
      		}else{
				backup=backup+ch
			}
   		}
		
		//check for multiple points input
		var pointNum=0
		backup=""
		for (var i = 0; i < input.length; i++) {
			var ch = input.substring(i, i + 1)
			if (ch == '.'){
				pointNum++
				if(pointNum==2){
					widget.value=backup
					return false
				}else{
					backup=backup+ch
				}
      		}else{
				backup=backup+ch
			}
   		}
		
		return true
	}
	
	
	// A Function that disables the selected widgets
	function init(){
		myForm.listPrice.focus()
		myForm.netRevenue.disabled = true
		myForm.postageCost.disabled = true
		myForm.poCost.disabled = true
		myForm.printingCost.disabled = true
		myForm.listRental.disabled = true
		myForm.lshopCost.disabled = true
		myForm.totalCost.disabled = true
		myForm.orderNum.disabled = true
		myForm.orderRate.disabled = true
	}
	
	// A Function that clears the form
	function clearForm(){
		myForm.listPrice.value = ""
		myForm.cost.value = ""
		myForm.netRevenue.value = ""
		myForm.postageCost.value = ""
		myForm.poCost.value = ""
		myForm.printingCost.value = ""
		myForm.listRental.value = ""
		myForm.lshopCost.value = ""
		myForm.totalCost.value = ""
		myForm.orderNum.value = ""
		myForm.orderRate.value = ""
		init();
	}
	
	//A Function that prints the form directly to the printer
	function printForm(){
		window.print()
	}
