var oiindex = [""];
//oiindex[4]="a";
var  orderlines=0;
var lastkey=1;
var order_total = 0
var bad_value=-1
var lfp=9

function transfervalue(val){
				 parent.topframe.document.totalform.testname.value=val
}  

function ctot(frm,i) {
//NOTE  we are storing the form position (ie. i) of the first ordered
//item in oiindex[1], not oiindex[0]

//
// every time we enter this function, record the form position
//
formpos=i
   
    var max_number_of_items = 9

	var item_total=0
//alert("length of oiindex is "+ oiindex.length );
	bad_value=-1
	
	//is this item currently recorded in the order - if so,
	//take out the existing value and recalculate the total
	if (orderlines>0){
		loopindex=1
		//alert("loopindex,orderlines,i,"+loopindex+" , "+orderlines+" , "+i);
		while (loopindex <= orderlines) {
			//alert("inside while loop "+oiindex[loopindex]);
			if (oiindex[loopindex]==i){
				//then this item was in the order before so adjust order total
				// and remove old value from the order
				//alert("order total before  "+order_total+" , "+frm.elements[i+2].value)
				//alert(frm.elements[i+2].value.substring(2,frm.elements[i+2].value.length));
				order_total= order_total - parseFloat(frm.elements[i+2].value.substring(2,frm.elements[i+2].value.length));
				if(isNaN(order_total)){order_total=0.0}
				if(order_total<0){order_total=0.0}
				//alert("order total after  "+order_total);
				
				parent.topframe.document.totalform.TOTAL.value=round_decimals(order_total, 2);
				 frm.TOTAL.value=round_decimals(order_total, 2);
				
				
				frm.elements[i+2].value=""
				oiindex.splice(loopindex,1);
				orderlines=orderlines-1;
				//alert("orderlines= "+orderlines)
				//terminate the loop
				loopindex=orderlines+5;
			}
			loopindex=loopindex+1;					
		}
	}
	
	//alert("in ctot at pos"+i)
		
    // Run through all the form fields
//    for (var i=0; i < frm.elements.length; ++i) {

	    
		//var i=0
		//while (i<frm.elements.length){
					  
        // Get the current field
        form_field = frm.elements[i]
	
        // Get the field's name
        form_name = form_field.name

        // Is it a "product" field?
        if (form_name.substring(0,4) == "PROD") {

            // If so, extract the price from the name
            item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))
						
            // Get the quantity
            if (form_field.value==""){
	            item_quantity=0
            }
            else{            
				item_quantity = parseInt(form_field.value)
			}	
	       		// check validity of quantity
 	       	 
					  if (item_quantity < 0 || isNaN(item_quantity) ){
							alert("Not an acceptable value for the number of items required. Please try again.") 
 								item_quantity=0
		//						parent.mainframe.orderform.elements[i].value=""
								frm.elements[i].value=""
								//record the position of the bad value
									bad_value=i
									}
       						  else {
								 	if (item_quantity > max_number_of_items){
                  	//	  				alert("Number of items seems too large. Contact us buy email if you really want this many items")
														if(!confirm("Number of items seems too large.\n\n"+
															 "Click on OK to confirm that you do wish to order this quantity.\n\n"+
															 "or click CANCEL enter another value")){					 			 
                  						 	item_quantity=0
																//record the position of the bad value
																bad_value=i
															}			
 											   }
 							   		}
           		
	     		  // Update the order total
   					if (item_quantity > 0) {
						//update the array of order line indexes
						orderlines=orderlines+1;
						oiindex[orderlines]=i;
						//alert("itemquantity,orderlines= "+item_quantity+" , "+orderlines)
						//calculate item line total
						item_total= item_quantity * item_price
						//calculate order total
				if(isNaN(order_total)){order_total=0.0}
				if(order_total<0){order_total=0.0}

						order_total += item_total
						// Display item total
						frm.elements[i].value=item_quantity
						//parent.mainframe.oi[0,6]=round_decimals(item_total, 2)
						//frm.elements[i+1].value=parent.mainframe.oi[0,6]
	  				frm.elements[i+2].value=round_decimals(item_total, 2)
	  				}	
	  				if (item_quantity==0){
		  				 frm.elements[i].value=""
		  				 frm.elements[i+2].value=""
	  				}
				}	
			//i=i+1
	//		}
	  
	

    // Display the total rounded to two decimal places
  //      parent.topframe.totalform.TOTAL.value=round_decimals(order_total, 2)
          parent.topframe.document.totalform.TOTAL.value=round_decimals(order_total, 2)

				frm.TOTAL.value=round_decimals(order_total, 2)
				
		//alert("i am  after total before reset focus")
							// if it was a bad value, return focus to bad value
						if (bad_value>=0){
							lfp=bad_value
							 parent.mainframe.document.orderform.elements[bad_value].focus()			
		//alert("i am  after first reset focus statement")
	
							 setTimeout( "parent.mainframe.document.orderform.elements[bad_value].focus();" , 10 );
		//alert("i am  after second reset focus statement ")
						}
//alert("i am  immediately before function return")
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
		value_string= "$ "+ value_string
    return value_string
}

function validateform(){

	if (confirm("Please confirm that you wish to submit your order now.\n\n"+
	"Click OK to submit your order, or click CANCEL to return to the Order Form")){
	}
	else{
		lfp=lfp+3
		parent.mainframe.document.orderform.elements[lfp].focus()
		return false
	}
	if (parent.mainframe.document.orderform.TOTAL.value <= 0){
		if (!confirm("Please confirm that you wish to submit your order now.\n\n"+
		"THERE DOES NOT APPEAR TO BE ANY ITEMS ORDERED.\n\n"+
		"Click OK to submit your order anyway, or click CANCEL to return to the Order Form")){
			parent.mainframe.document.orderform.elements[lfp].focus()
			return false
		}
	}			
	nme=parent.mainframe.document.orderform.elements[0].value
	adrs=parent.mainframe.document.orderform.elements[1].value
	dy=parent.mainframe.document.orderform.elements[2].value
	instruct=parent.mainframe.document.orderform.elements[3].value	
	cc=parent.mainframe.document.orderform.elements[4].value
	if (cc == ""){parent.mainframe.document.orderform.elements[4].value="NO"}
//	if (cc!="Yes" && cc!="yes" && cc!="Y" && cc!="y" && cc!="YES"){
//		parent.mainframe.document.orderform.elements[3].value="NO"}
//	else{
//		parent.mainframe.document.orderform.elements[3].value="YES"}		
	//alert("values are"+nme+","+adrs+","+dy)
	if(nme=="" || adrs=="" || dy==""){
		alert("You must provide your Name, Delivery address and Delivery day!")
		parent.mainframe.document.orderform.name.focus()
		return false
		}
	else{
	//newwindowWrite()
	
newwindowWriteconfirm()
//return false
		//return true
		
	}
		
}


function kpress(e){
var key=document.all?window.event.keyCode:e.which;
lastkey=key
//alert("key code = "+key);
retcode=true
if (key==13){
	retcode = false
//	window.event.keyCode=40;
//	e.which=40;
	key=40;}
if (key==40) {
	if (lfp==4){lfp=5}
	if (lfp==3){lfp=4}
	if (lfp==2){lfp=3}
	if (lfp==1){lfp=2}
	if (lfp==0){lfp=1}
	
	if (lfp>4){lfp = lfp+3}
	if (lfp >= parent.mainframe.document.orderform.elements.length-1){
		lfp=lfp-3}
	parent.mainframe.document.orderform.elements[lfp].focus();
}
if (key==38) {
	if (lfp==0){lfp=0}
	if (lfp==1){lfp=0}
	if (lfp==2){lfp=1}
	if (lfp==3){lfp=2}
	if (lfp==4){lfp=3}
	if (lfp==5){lfp=3}
	if (lfp==6){lfp=3}
	if (lfp==7){lfp=3}
	if (lfp==8){lfp=4}
	if (lfp>8) lfp=lfp-3
	//if(lfp<7)lfp=7
	parent.mainframe.document.orderform.elements[lfp].focus();
}
//if (key==13){
//	window.event.keyCode=40;
//	e.which=40;
//}
return retcode//(key == 13);
//	key=(document.layers)?e.which:window.event.keyCode
//    key2=String.fromCharCode(key)
//   if (key2=="," || key2=="<"){
//	   left();
//   }
//   if (key2=="." || key2==">"){
//	   right();
//   }
} 


function newwindowWriteconfirm(){
var winref=window.open('','mywindow','width=700,height=200,toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,copyhistory=no,resizable=yes,left=0,top=0,screenX=0,screenY=0')
var gt=unescape("%3E");
winref.document.open();
winref.document.writeln("<HTML"+gt);
winref.document.writeln("<head"+gt);
winref.document.writeln("<TITLE>Order Record</TITLE"+gt);
winref.document.writeln("<style type='text/css'"+gt)
winref.document.writeln("body {")
winref.document.writeln("	background: #FFF;")
winref.document.writeln("	color: #000;")
winref.document.writeln("	font: normal normal 12px Verdana, Geneva, Arial, Helvetica, sans-serif;")
winref.document.writeln("	margin: 10px;")
winref.document.writeln("	padding: 0;")
winref.document.writeln("}")
winref.document.writeln("</style"+gt)
winref.document.writeln("</head"+gt);

winref.document.writeln("<body bgcolor='#FFF' text='#000'"+gt);
	winref.document.writeln("<H2>Please do not close your browser or disconnect yet...  please wait until you receive a confirmation message below.</H2"+gt)
	winref.document.writeln("");
	winref.document.writeln(' <font color = "red" >Please ensure you receive a message below confirming the time and date your order was successfully submitted. If you do not get this message, or if you get any messages indicating an error has occurred, please contact us at  <a href="mailto:organicbuyers@austarnet.com.au">organicbuyers@austarnet.com.au</a> and describe the nature of the error, as it may mean you order has not been submitted. </font><br>');
	
	winref.document.writeln(" <br"+gt);
	winref.document.writeln("<H2>Scroll down to see a summary of your order.</H2"+gt)
	winref.document.writeln(" Save or Print this page if you wish to keep a record of your order.<br>");
	winref.document.writeln(" <br"+gt);
winref.document.writeln(" <br><br>");
winref.document.writeln("<TABLE"+gt);
winref.document.writeln("<tr><td><b>Name          : </b></td><td>"+top.parent.mainframe.document.orderform.name.value+"</td></tr>");
winref.document.writeln("<tr><td><b>Address           : </b></td><td>"+top.parent.mainframe.document.orderform.address.value+"</td></tr>");
winref.document.writeln("<tr><td><b>Day       : </b></td><td>"+top.parent.mainframe.document.orderform.day.value+"</td></tr>");
winref.document.writeln("<tr><td><b>Delivery Instructions       : </b></td><td>"+top.parent.mainframe.document.orderform.instruct.value+"</td></tr>");
winref.document.writeln("<tr><td><b>Credit Card       : </b></td><td>"+top.parent.mainframe.document.orderform.ccard.value+"</td></tr>");
winref.document.writeln("</table"+gt);
winref.document.writeln("<br"+gt);
winref.document.writeln("<br"+gt);

winref.document.writeln("<TABLE"+gt);

winref.document.writeln("<thead"+gt);
winref.document.writeln("<tr"+gt);
winref.document.writeln("<th width=10% align=left >Number<br>Ordered </td"+gt);
winref.document.writeln("<th width=60% >Item </td"+gt);
winref.document.writeln("<th width=10% align=left>Item<br>Sub<br>Total </td"+gt);
winref.document.writeln("</tr"+gt);
winref.document.writeln("</thead"+gt);

winref.document.writeln("<tbody"+gt);
orderarraylength=top.parent.mainframe.document.orderform.length

for (var i=9; i < orderarraylength-3; i+=3) {
	if (top.parent.mainframe.document.orderform.elements[i].value!=""){
		winref.document.writeln(" <tr"+gt);
		winref.document.writeln(" <td>"+top.parent.mainframe.document.orderform.elements[i].value+"</td"+gt);
		winref.document.writeln(" <td>"+top.parent.mainframe.document.orderform.elements[i+1].value+"</td"+gt);
		winref.document.writeln(" <td>"+top.parent.mainframe.document.orderform.elements[i+2].value+"</td"+gt);
		winref.document.writeln(" </tr"+gt);
	}
}
winref.document.writeln(" <tr"+gt);
winref.document.writeln(" </tr"+gt);

winref.document.writeln(" <tr"+gt);
winref.document.writeln(" <td></td"+gt);
winref.document.writeln(" <td align= right><b>Indicative Total      :</b></td"+gt);
winref.document.writeln(" <td><b>"+top.parent.mainframe.document.orderform.TOTAL.value+"</b></td"+gt);
winref.document.writeln(" </tr"+gt);

winref.document.writeln("</tbody"+gt);
winref.document.writeln("</TABLE"+gt);
	winref.document.writeln('<input type=button onclick="javascript:window.close()" value="Click here to close this window"'+gt)
winref.document.writeln("</body"+gt);

winref.document.close()
}



function newwindowWrite(calltype){
//alert ("at the start of writing the window")
var winref=window.open('','mywindow','width=750,height=480,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes')
var gt=unescape("%3E");
winref.document.open();
winref.document.writeln("<HTML"+gt);
winref.document.writeln("<head"+gt);
winref.document.writeln("<TITLE>Order Summary so far</TITLE"+gt);
winref.document.writeln("<style type='text/css'"+gt)
winref.document.writeln("body {")
winref.document.writeln("	background: #FFF;")
winref.document.writeln("	color: #000;")
winref.document.writeln("	font: normal normal 12px Verdana, Geneva, Arial, Helvetica, sans-serif;")
winref.document.writeln("	margin: 10px;")
winref.document.writeln("	padding: 0;")
winref.document.writeln("}")
winref.document.writeln("</style"+gt)
winref.document.writeln("</head"+gt);

winref.document.writeln("<body bgcolor='#FFF' text='#000'"+gt);
if (calltype=="confirm"){
//	winref.document.writeln("<H2>Thank You</H2"+gt)
	winref.document.writeln("<H2>This is a summary of your order.</H2"+gt)
//	winref.document.writeln("<H2>You have submitted the following order:</H2"+gt)
	}else{
	winref.document.writeln("<H2>You have <font color=red>NOT </font> submitted your order yet!</H2"+gt)
	winref.document.writeln("<br><H4>This is only a summary of your order <font color=red>so far</font>.")
	winref.document.writeln("<br><br><br>")
	winref.document.writeln(" You will need to return to the order form and click the SUBMIT button before your order will be submitted. </H4"+gt)
	winref.document.writeln("<br><br><h4>When you return to the order form, you can still change your order or add to it.  </H4"+gt)
	winref.document.writeln("<br><br><H2>Please scroll down to see your summary</H2"+gt)
	winref.document.writeln("<br><br><br><br><br>")
		
	winref.document.writeln("<H2>Summary of your order so far:</H2"+gt)
	}
	
if (calltype=="confirm"){
	winref.document.writeln("");
	winref.document.writeln(' <font color = "red" size="-1">Please ensure you receive a message on the order page confirming the time and date your order was successfully submitted. If you do not get this message, or if you get any messages indicating an error has occurred, please contact us at organicbuyers@austarnet.com.au and describe the nature of the error, as it may mean you order has not been submitted. </font><br>');
	
	winref.document.writeln(" <br"+gt);
	winref.document.writeln(" Save or Print this page if you wish to keep a record of your order.<br>");
	winref.document.writeln(" <br"+gt);
//	winref.document.writeln('<input type=button onclick="javascript:window.close()" value="Click here to close this window"'+gt)
	}
	else{
	winref.document.writeln("");
	winref.document.writeln(" <br"+gt);
	winref.document.writeln("<input type=button onclick='javascript:window.close()' value='Click here to return to order form'"+gt)
	}
winref.document.writeln(" <br><br>");
winref.document.writeln("<TABLE"+gt);
winref.document.writeln("<tr><td><b>Name          : </b></td><td>"+top.parent.mainframe.document.orderform.name.value+"</td></tr>");
winref.document.writeln("<tr><td><b>Address           : </b></td><td>"+top.parent.mainframe.document.orderform.address.value+"</td></tr>");
winref.document.writeln("<tr><td><b>Day       : </b></td><td>"+top.parent.mainframe.document.orderform.day.value+"</td></tr>");
winref.document.writeln("<tr><td><b>Delivery Instruction    : </b></td><td>"+top.parent.mainframe.document.orderform.instruct.value+"</td></tr>");
winref.document.writeln("<tr><td><b>Credit Card       : </b></td><td>"+top.parent.mainframe.document.orderform.ccard.value+"</td></tr>");
winref.document.writeln("</table"+gt);
winref.document.writeln("<br"+gt);
winref.document.writeln("<br"+gt);

winref.document.writeln("<TABLE"+gt);

winref.document.writeln("<thead"+gt);
winref.document.writeln("<tr"+gt);
winref.document.writeln("<th width=10% align=left >Number<br>Ordered </td"+gt);
winref.document.writeln("<th width=60% >Item </td"+gt);
winref.document.writeln("<th width=10% align=left>Item<br>Sub<br>Total </td"+gt);
winref.document.writeln("</tr"+gt);
winref.document.writeln("</thead"+gt);

winref.document.writeln("<tbody"+gt);
orderarraylength=top.parent.mainframe.document.orderform.length

for (var i=9; i < orderarraylength-3; i+=3) {
	if (top.parent.mainframe.document.orderform.elements[i].value!=""){
		winref.document.writeln(" <tr"+gt);
		winref.document.writeln(" <td>"+top.parent.mainframe.document.orderform.elements[i].value+"</td"+gt);
		winref.document.writeln(" <td>"+top.parent.mainframe.document.orderform.elements[i+1].value+"</td"+gt);
		winref.document.writeln(" <td>"+top.parent.mainframe.document.orderform.elements[i+2].value+"</td"+gt);
		winref.document.writeln(" </tr"+gt);
	}
}
winref.document.writeln(" <tr"+gt);
winref.document.writeln(" </tr"+gt);

winref.document.writeln(" <tr"+gt);
winref.document.writeln(" <td></td"+gt);
winref.document.writeln(" <td align= right><b>Indicative Total      :</b></td"+gt);
winref.document.writeln(" <td><b>"+top.parent.mainframe.document.orderform.TOTAL.value+"</b></td"+gt);
winref.document.writeln(" </tr"+gt);

winref.document.writeln("</tbody"+gt);
winref.document.writeln("</TABLE"+gt);
if (calltype=="confirm"){
	winref.document.writeln('<input type=button onclick="javascript:window.close()" value="Click here to close this window"'+gt)
	}
	else{
	winref.document.writeln('<input type=button onclick="javascript:window.close()" value="Click here to return to order form"'+gt)
	}
winref.document.writeln("</body"+gt);

winref.document.close()
}


function clearthings(){
//parent.topframe.document.details.inputname.value=""
//parent.topframe.document.details.inputaddress.value=""
//parent.topframe.document.details.inputday.value=""
order_total=0
orderlines=0
parent.topframe.document.totalform.TOTAL.value=""
parent.mainframe.document.orderform.elements[0].value=""
parent.mainframe.document.orderform.elements[1].value=""
parent.mainframe.document.orderform.elements[2].value=""
parent.mainframe.document.orderform.elements[3].value=""
parent.mainframe.document.orderform.elements[4].value=""
parent.mainframe.document.orderform.elements[5].value="YES"
for (var i=9; i < parent.mainframe.document.orderform.length-3; i+=3) {
	parent.mainframe.document.orderform.elements[i].value=""
	parent.mainframe.document.orderform.elements[i+2].value=""
	
	}	
}


function mf(frm){
// move focus	
	if (lastkey==9){
		lfp= lfp+3
	}
	frm.elements[lfp].focus()
}	
	
	
	
if(document.layers){
	document.captureEvents(Event.KEYPRESS);
}
//document.onkeypress=kpress;
document.onkeydown=kpress;

window.onload = function() { parent.topframe.document.headingform.datedisplay.value=parent.mainframe.document.orderform.orderdelivdate.value;
		top.parent.mainframe.document.orderform.name.focus();
		clearthings()}

-->



