
function enforce_integer(sender,max)
{var v=parseInt(sender.value,10);v=(isNaN(v))?0:Math.min(v,max);sender.value=v;}
function create_input(form,src_name,src_id,dest_name,dest_seq)
{var input=document.createElement('input');input.type='hidden';input.name=dest_name+dest_seq;input.value=form.elements[src_name+src_id].value;form.appendChild(input);}
function remove_one_input(form,name,seq)
{if(form.elements[name+seq])
{form.removeChild(form.elements[name+seq]);}}
function remove_input(form)
{for(var k=1;k<=10;k++)
{remove_one_input(form,'quantity_',k);remove_one_input(form,'item_name_',k);remove_one_input(form,'amount_',k);remove_one_input(form,'item_number_',k);remove_one_input(form,'shipping_',k);remove_one_input(form,'shipping2_',k);}}
function get_shipping(form,item_count)
{return((item_count>0)?parseFloat(form.lshipping_1.value):0)+
(Math.max(item_count-1,0)*parseFloat(form.lshipping_2.value));}
function loop_items(callback)
{for(var k=0;k<document.forms[0].elements.length;k++)
{var name=document.forms[0].elements[k].name;if(name.indexOf('lquantity_')==0)
{var id=name.substr(name.indexOf('_')+1);callback(document.forms[0].elements[k],id);}}}
function update_totals(sender)
{var n=sender.name;var id=n.substr(n.indexOf('_')+1);var amt=document.forms[0].elements['lamount_'+id].value;var qty=sender.value;var tot=amt*qty;var ot=XAM.Core.GetObjectRef('total_'+id);ot.innerHTML=tot.toFixed(2);}
XAM.Core.SetOnLoadCallback(function()
{loop_items(function(element)
{element.onblur=function()
{enforce_integer(this,this.form.maxqty.value);};});document.forms[0].bt_reset.onclick=function()
{loop_items(function(element)
{element.value='0';});document.forms[0].elements['bt_update'].onclick();};document.forms[0].bt_update.onclick=function()
{var item_count=0,grand_tot=0;loop_items(function(element,id)
{var amt=document.forms[0].elements['lamount_'+id].value;var otot=XAM.Core.GetObjectRef('total_'+id);var qty=parseInt(element.value);var tot=amt*qty;otot.innerHTML=(tot!=0)?tot.toFixed(2):'';grand_tot+=tot;item_count+=qty;});var ship_tot=get_shipping(this.form,item_count);grand_tot+=ship_tot;this.form.elements['paypal-direct'].value='Make Payment $'+grand_tot.toFixed(2);this.form.elements['paypal-direct'].disabled=(grand_tot!=0)?0:1;var stot=XAM.Core.GetObjectRef('shipping');stot.innerHTML=(ship_tot!=0)?ship_tot.toFixed(2):'';var gtot=XAM.Core.GetObjectRef('gtotal');gtot.innerHTML=grand_tot.toFixed(2);};document.forms[0].elements['paypal-direct'].onclick=function()
{var seq=0,item_count=0;remove_input(this.form);loop_items(function(element,id)
{if(element.value!='0')
{item_count+=parseInt(element.value);create_input(element.form,'lquantity_',id,'quantity_',++seq);create_input(element.form,'litem_name_',id,'item_name_',seq);create_input(element.form,'lamount_',id,'amount_',seq);create_input(element.form,'litem_number_',id,'item_number_',seq);}});if(item_count>0)
{this.form.handling_cart.value=get_shipping(this.form,item_count);this.form.submit();}};});
