var last_id=false;var opt_selected=false;var last_mask=000;var main_product;function inCart(name,price){this.name=name;this.price=decimal_format(price);}
var IC=inCart.prototype;IC.toString=function(){text='<tr><td class="gbb_subtotal">'+this.name+'</td><td align="right" class="gbb_subtotal"> $ '
+formatCurrency(this.price)+"</td></tr>\n";return text;}
function Products(id,name,price,input,opts,kind){this.id=id;this.name=name;this.opts=opts;temp_price=new String(price);temp_price.replace(",","");this.price=parseFloat(temp_price);this.opt_price=0;this.opt_name='';this.input=input;this.kind=kind;this.selected=false;this.gebi=false;}
var PT=Products.prototype;PT.toString=function(){text="<tr><td>"+this.name+"</td><td align=\"right\"> $ "+formatCurrency((parseFloat(this.price)+parseFloat(this.opt_price)))+"</td></tr>\n";return text;}
PT.setOpt=function(price,name){opt_price=parseFloat(price);opt_name=name;}
PT.setGebi=function(did){gebi=did;}
var highlighted=[];var productsList=[];var subtotalList=[];function select_mask(bitmask){form_elts=document.forms['cart_quantity'].elements;reset_highlighted('optReqTool');for(si=0;si<productsList.length;si++){if((productsList[si].kind&bitmask)>0){highlight(productsList[si],'gbb_hl');input_trigger(productsList[si].input,productsList[si].id);input_trigger(productsList[si].opts,false);}}
last_mask=bitmask;opt_selected=true;recalc();return true;}
function input_trigger(name,val){if(name==undefined||name==''){return;}
form_obj=eval("document.forms['cart_quantity']['"+name+"']");if(val==false||form_obj.length==undefined||form_obj.length==1){input_mod(form_obj);}else{for(j=0;j<form_obj.length;j++){if(form_obj[j].value==val){input_mod(form_obj[j]);break;}}}}
function input_mod(elName){pricepat=/\$\s*(\d+\.\d+)/;switch(elName.type){case'checkbox':elName.checked=true;break;case'text':elName.value=1;break;case'radio':elName.checked=true;break;case'select-one':case'select-multiple':txt='';break;for(k=0;k<elName.length;k++){if(elName.options[k].selected){txt=elName.options[k].text;matches=pricepat.exec(txt);break;}}
break;default:break;}
return;}
function highlight(product,className){did=document.getElementById('p'+product.id);did.className=className;highlighted.push(product);}
function reset_highlighted(className){for(m=0;m<document.forms['cart_quantity'].gbb.length;m++){document.forms['cart_quantity'].gbb[m].style.background="#D4D0C8";}
opt_selected=false;document.forms['cart_quantity'].reset();document.getElementById('subtotal').innerHTML='';while(highlighted.length>0){product=highlighted.pop();did=document.getElementById('p'+product.id);did.className=className;}}
function recalc(){f=document.forms['cart_quantity'].elements;while(subtotalList.length>0){subtotalList.pop();}
subtotalList.push(main_product);var recalc_i=0;for(recalc_i=0;recalc_i<f.length;recalc_i++){if(f[recalc_i].name==undefined||f[recalc_i].name==''){continue;}
switch(f[recalc_i].type){case'checkbox':case'radio':if(f[recalc_i].checked==true){subtotalList.push(productLookup(f[recalc_i].name,f[recalc_i].value));}
break;case'text':if(parseInt(f[recalc_i].value)>0){var p=productLookup(f[recalc_i].name,false);p.price=decimal_format(f[recalc_i].value*p.price);p.name+='   (qty '+f[recalc_i].value+'@ $'+p.price+' ea.)';subtotalList.push(p,false);}
break;default:break;}}
display_subtotal();return true;}
function extract_price(text){var index=text.indexOf('$');var price_chars='0.00';if(index!=-1){index++;while((text[index]>='0'&&text[index]<='9')||text[index]=='.'){price_chars+=text[index];index++;}}
return price_chars;}
function glean_price(opt_name){product_text='';if(opt_name==undefined||opt_name==''){return 0;}
pricepat=/\$\s*(\d+\.\d+)/;optmenu=eval("document.forms['cart_quantity']['"+opt_name+"']");price_found=0.00;matches=[];found_selected=false;if(optmenu.type=='select-one'){for(g=0;g<optmenu.length;g++){if(optmenu.options[g].selected){product_text=optmenu.options[g].text;matches=pricepat.exec(product_text);if(matches!=undefined&&matches[1]>0){price_found=matches[1];}
found_selected=true;break;}}
if(found_selected==false){product_text=optmenu.options[0].text;matches=pricepat.exec(product_text);if(matches!=undefined&&matches[1]>0){price_found=matches[1];}
found_selected=true;}}
return price_found;}
function productLookup(name,val){var product=false;var p=0;for(p=0;p<productsList.length;p++){if(productsList[p].input==name&&val!=false&&productsList[p].id==val){if(productsList[p].opts!=undefined&&productsList[p].opts!=''){product=new inCart(productsList[p].name,(parseFloat(productsList[p].price)+parseFloat(glean_price(productsList[p].opts))));if(product_text!=''){product.name+=", "+product_text;}}else{product=new inCart(productsList[p].name,productsList[p].price);if(product_text!=''){product.name+=", "+product_text;}}
found=true;}else if(productsList[p].input==name&&val==false){product=new inCart(productsList[p].name,(parseFloat(productsList[p].price)+parseFloat(glean_price(productsList[p].opts))));if(product_text!=''){product.name+=", "+product_text;}}}
return product;}
function decimal_format(num){var p=Math.round(num*100)/100;p=p.toString();if(p.indexOf(".")==-1){p+=".00";}else if(p.indexOf(".")==(p.length-2)){p+="0";}
return p;}
function display_subtotal(){subtotal_output='<table width=550 class="gbb_subtotal_table" cellpadding="4" cellspacing="0">';subtotal_output+='<tr><td class="gbb_subtotal_heading">Product</th><td class="gbb_subtotal_heading" align="right">Price</th></tr>';subtotal_dollars=0.00;var st;for(st=0;st<subtotalList.length;st++){if(subtotalList[st]==false){continue;}
subtotal_output+=subtotalList[st].toString();subtotal_dollars+=parseFloat(subtotalList[st].price);}
subtotal_dollars=formatCurrency(subtotal_dollars);subtotal_output+='<tr><td class="gbb_subtotal_bottom" align="right">Total:</td><td class="gbb_subtotal_bottom" align="right">$ '+subtotal_dollars+'</td></tr>';subtotal_output+='<table>';document.getElementById('subtotal').innerHTML=subtotal_output;}
function rc(){return recalc();}
function formatCurrency(num){num=num.toString().replace(/\$|\,/g,'');if(isNaN(num))
num="0";sign=(num==(num=Math.abs(num)));num=Math.floor(num*100+0.50000000001);cents=num%100;num=Math.floor(num/100).toString();if(cents<10)
cents="0"+cents;for(var i=0;i<Math.floor((num.length-(1+i))/3);i++)
num=num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));return(((sign)?'':'-')+num+'.'+cents);}
