var update=true;
var reload=false;
var origval=[];
function hunround(val)
{
return 5*Math.round(val/5);
}
function kosar_req(pid,qty)
{  

  new Ajax.Request("kosar.php", {
  method: "get",
  parameters: {id: pid, n: qty},
  onSuccess: function(transport){

      var tvvv;
      var response='semmi.'; 
      response = transport.responseText || "Szerverhiba, elnézést kérünk.(no response text)";
      $("kosar_box").update(response);
      if(!update)
      {
        //tvvv= parseInt($('total').innerHTML);
        if(reload)
        window.location.reload();
      }
    },
    onFailure: function(){ alert("Szerverhiba, elnézést kérünk. (xmlhttp request failed.)"); }
  });
      //alert("Success! \n\n" + response);

}
function chit(event)
{
 
var fv = Form.getInputs('orderform','radio','ro').find(function(radio) { return radio.checked; }).value;
  switch(fv)
  {
  case 'profile':
  $('inpdat').hide();
  $('rdat').show();
  break;
  case 'save':
  $('inpdat').show();
  $('rdat').hide();
  break;
  case 'custom':
  $('inpdat').show();
  $('rdat').hide();
  
  }
}

function kosarba(pid, qty)
{
  //var theval;
  //theval=$F('qty'+pid);

dirset(pid);
//kosar_req(pid,theval);
  //if(!update) updatetotal(pid,theval);
if(update) Lightbox.showBoxString('<div class="tvvb"><p>A terméket a kosárba tette.</p><div id="vissza" class="kosctl folytat" onclick="Lightbox.hideBox()">Még nézelődöm</div><a id="rendel" class="kosctl tovabb" href="/shop/kosar.html">Megrendelem</a></div>',boxWidth,boxHeight);
$('kosarban'+pid).show();
$('kosarba'+pid).hide();
}
function updatetotal(id,mul)
{

var theval, thepr, tt,tw,tpw;
theval= parseInt($('total').innerHTML);
thepr=mul*parseInt($('ar'+id).innerHTML);
tw=parseInt($('total_w').innerHTML);
tpw=mul*parseInt($('w'+id).innerHTML);

tw=tw+tpw;
tt=theval+thepr;
if(tt==0) reload=true;
$("total").update(tt);
  new Ajax.Request("totals.php", {
  method: "get",
  parameters: {total: tt,gtw:tw},
  onSuccess: function(transport){

      var response='semmi.'; 
      response = transport.responseText || "Szerverhiba, elnézést kérünk.(no response text)";
      $("totals").update(response);
    },
    onFailure: function(){ $("totals").update("Szerverhiba, elnézést kérünk. Kérjük nyomja meg az F5-öt, vagy a frissítés gombot. (xmlhttp request failed.)"); }
  });

//$("total_rnd").update(hunround(theval+thepr));
}


function startdirset(pid)
{
  origval[pid]=$F('qty'+pid);
  //$('dirset'+pid).show();
  //$('spinset'+pid).hide();

}
function ccldirset(pid)
{
  //$('dirset'+pid).hide();
  //$('spinset'+pid).show();
  $('qty'+pid).value=origval[pid];
}
function remv(pid)
{
    startdirset(pid);
    $('qty'+pid).value=0;
//     alert("remo");
    dirset(pid);
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }



function dirset(pid)
{
  var theval;
  theval=$F('qty'+pid);
  if(!IsNumeric(theval))
  {
  ccldirset(pid);
  return;
  
  }
  origval[pid]=theval;//   alert(theval);
  if(!update) updatetotal(pid,theval-origval[pid]);

  ccldirset(pid);
  if(theval==0)
  {
    if(!update) $('tb'+pid).hide();
    $('kosarban'+pid).hide();
    $('kosarba'+pid).show();
    $('qty'+pid).value=1;
  } else
    $('qty'+pid).value=theval;
  kosar_req(pid,theval);


}


