function validQty(qtyField) {
	var passed = true 
	for (i=0; i < qtyField.value.length; i++) {
		if (((qtyField.value.charAt(i) < "0") || (qtyField.value.charAt(i) > "9")) && (qtyField.value.charAt(i) != " ")) {		  
				i = qtyField.value.length
				passed = false  
	  	}
	}
  	var qty = trim(qtyField.value) 
	if (qty.indexOf(" ") > 0){
		passed = false  	  
	} 
	return passed
}

function trim(strText) {
  // this will get rid of leading spaces
  while (strText.substring(0,1) == ' ')
    strText = strText.substring(1, strText.length);

  // this will get rid of trailing spaces
  while (strText.substring(strText.length-1,strText.length) == ' ')
    strText = strText.substring(0, strText.length-1);

  return strText;
}  
  
function updateTotal(qty,price) {
	var validNumber = validQty(qty)
  
  	if (validNumber) { 
  		qty.value = trim(qty.value)
      var rowNum = parseInt(qty.name.substr(3))
		  var oldQtyField = "oldQty" + rowNum
		  var rowTotalField = "rowTotal" + rowNum
      var oldQty = window.document.orderBlankForm[oldQtyField].value

      //  if the qty is blank set it to 0 for calculations
      if (qty.value == "")
      	qty.value = 0
	  	var qtyDiff = qty.value - oldQty

      var addTotal = parseFloat(Math.round(qtyDiff * price * 100))

      //	if rowtotal is blank set it to 0 for calculations
      if (window.document.orderBlankForm[rowTotalField].value == "") 
      	window.document.orderBlankForm[rowTotalField].value = 0
      window.document.orderBlankForm[rowTotalField].value = parseFloat(((window.document.orderBlankForm[rowTotalField].value * 100) + addTotal) / 100)
      
      //	if orderBlankTotal is blank set to 0 for calculations
	  	if (window.document.orderBlankForm.orderBlankTotal.value == "")
	    	window.document.orderBlankForm.orderBlankTotal.value = 0

	  	var oldTotal = parseInt(window.document.orderBlankForm.orderBlankTotal.value * 100)
	  	var newTotal = oldTotal + addTotal
	  	window.document.orderBlankForm.orderBlankTotal.value = parseFloat(newTotal / 100)
	  	window.document.orderBlankForm[oldQtyField].value = qty.value
      
      // Focus on the quantity field of the next line item
		  var version = navigator.appVersion.substring(0,(navigator.appVersion.indexOf(" ")))
		  if( !(navigator.appName == "Netscape" && version >= "5.0")) {
      	var nextLineName = "qty" + (rowNum + 1)
      	window.document.orderBlankForm[nextLineName].focus()
		  }
	}
	else {
	   var currQtyField = "qty" + qty.name.substr(3)
	//	alert("|"+currQtyField+" | "+qty.name+" | "+qty.value)
	  	window.document.orderBlankForm[currQtyField].value = '0';
	  	myFocus(window.document.orderBlankForm[currQtyField])
			alert("Please enter a valid item quantity")
	}
}

function myFocus(field) {
//	alert("in myFocus method")
//	alert(field.name)
//	alert(field.value)
  	field.focus()
}
  
function updateRowTotal(total,price) {
	var rowNum = parseInt(total.name.substr(8))
	var qtyField = "qty"+rowNum
   var qty = window.document.orderBlankForm[qtyField].value      
   //var rowTotalField = qtyFieldNbr + 3
   var totalValue = parseFloat(Math.round(qty * price * 100))

   //	if rowtotal is blank set it to 0 for calculations
	if (total.value == "") 
   	total.value = 0
	total.value = parseFloat(totalValue / 100) 
}  
  
function submitForm() {
	window.document.orderBlankForm.submit()
}

function clearForm() {
	var fieldName = "orderBlankTotal"
	window.document.orderBlankForm[fieldName].value = 0	

	var totalRows = (window.document.orderBlankForm.elements.length - 6) / 4
	for (i=1; i <= totalRows; i++) {
		var qtyBoxName = "qty" + i
		var oldQtyBoxName = "oldQty" + i
		var rowTotalBoxName = "rowTotal" + i
		window.document.orderBlankForm[qtyBoxName].value = ""
		window.document.orderBlankForm[oldQtyBoxName].value = "0"
		window.document.orderBlankForm[rowTotalBoxName].value = ""
	}
}

