function pre_qtychange(me)
{
    if(!me)
        return;
    var d=document.getElementById('qty_div');
    if(d)
   {
       if(me.value=="custom")
           d.style.display="block";
       else
           d.style.display="none";
    }
    d=document.getElementById('theForm');
    if(!d)
        return;
    if(me.value=="custom")
    {
        d.quantity.value="6";
        d.quantity.focus();
    }
    else
        d.quantity.value="1";
}

function o_doUpdate()
{
    var theForm=document.getElementById('theForm');
    if(!theForm)
        return;
    var h=document.getElementById('theFormHide');
    if(!h)
        return;
    var p=theForm.pre_qty.value;
    var temp, temp2, temp3;
    
    //item_name
    var p2=theForm.item_name;
    p2.value=h.pname.value;
    if(p=="FL")
        p2.value=h.pname.value+' Family License';
    else if(p=="SBL")
        p2.value=h.pname.value+' Small Business License';
   
    //price
    p2=theForm.amount;
    p2.value=h.price.value;
    if(p=="FL")
        p2.value=h.price2.value;
    else if(p=="SBL")
        p2.value=h.price3.value;
    else if(p=="custom")
    {
         temp=theForm.quantity.value;
         if(isNaN(temp))
               p2.value=h.price.value;
         else if(temp<6)
               p2.value=h.price.value;
          else if(temp<21)
               p2.value=h.price4.value;
          else if(temp<51)
               p2.value=h.price5.value;
          else
               p2.value=h.price6.value;
    }
    if((h.cp.value>0)&&(h.cp.value<1))
        p2.value=(p2.value*(1-h.cp.value)).toFixed(2);

    //You save etc
    var bShow=false;
    if(h.price.value!=theForm.amount.value)
        bShow=true;
    showDiv('sv_div1', bShow);
    showDiv('sv_div2', bShow);
    showDiv('sv_div3', bShow);
    showDiv('sv_div4', bShow);

    temp3=theForm.amount.value*theForm.quantity.value;
    p2=document.getElementById('sv_6');
    p2.innerHTML='$'+temp3.toFixed(2);
    if(bShow)
    {
        temp=theForm.quantity.value;
        if(theForm.pre_qty.value=="FL")
            temp=3;
        if(theForm.pre_qty.value=="SBL")
            temp=5;
        temp2=h.price.value*temp;
        p2=document.getElementById('sv_2');
        p2.innerHTML='$'+temp2.toFixed(2);
        p2=document.getElementById('sv_4');
        p2.innerHTML='$'+(temp3-temp2).toFixed(2);
        
    }
    
    //cur_price as in ([qty] * price (price)
   p2=document.getElementById('cur_price');
   p2.innerHTML=(theForm.amount.value*1).toFixed(2);
   p2=document.getElementById('r_price');
   if(theForm.amount.value!=h.price.value)
       p2.innerHTML='('+(h.price.value*1).toFixed(2)+')';
   else
       p2.innerHTML="";

   //custom var
   theForm.custom.value='pid='+h.id.value+'|pre_qty='+theForm.pre_qty.value;
   if(theForm.cpcode.value)
       theForm.custom.value+='|cpcode='+theForm.cpcode.value;
   if(theForm.regname&&theForm.regname.value)
       theForm.custom.value+='|rn='+theForm.regname.value;
   
  //modify swreg link:
  p2=document.getElementById('swreg');
  if(p2)
      p2.href='/purchase/orderswreg.php?p='+h.id.value+'&q='+theForm.quantity.value+'&vp='+theForm.amount.value+'&t='+theForm.custom.value;
}
function qtykeypressed(theEvent)
{
    var keynum;
    var keychar;
    var numcheck;
	 if(theEvent && theEvent.which){
	 theEvent = theEvent;
	 keynum = theEvent.which;
	 }
	 else{
	 theEvent = event;
	 keynum = theEvent.keyCode;
	 }	 

    keychar = String.fromCharCode(keynum);
    numcheck =/\d/;
    if(!numcheck.test(keychar))
	{
	    if((keynum!=13)&&(keynum!=8)&&(keynum!=83))
		     return false;
    }
   return true;
}
function o_applycpcode(theSelf)
{
    var theForm=document.getElementById('theForm');
    if(!theForm)
        return;
    if(theForm.cpcode.value=='')
        return;
    theForm.action=theSelf;
    theForm.submit();
}
function keypress_cpcode(theEvent, theSelf)
{
    var keynum;
    var keychar;
    var numcheck;
	 if(theEvent && theEvent.which){
	 theEvent = theEvent;
	 keynum = theEvent.which;
	 }
	 else{
	 theEvent = event;
	 keynum = theEvent.keyCode;
	 }	 

    keychar = String.fromCharCode(keynum);
    if(keynum==13)
    o_applycpcode(theSelf);
    return true;
}
function keypress_rn(theEvent)
{
    var keynum;
    var keychar;
    var numcheck;
	 if(theEvent && theEvent.which){
	 theEvent = theEvent;
	 keynum = theEvent.which;
	 }
	 else{
	 theEvent = event;
	 keynum = theEvent.keyCode;
	 }	 

    keychar = String.fromCharCode(keynum);
    if(keynum==13)
    {
         rnchange();
         return false;
    }
    return true;
}

