var host = "localhost:8080";

function removeProduct(pid)
{    
    var opt = {    
      method: 'post',
      postBody: 'pid='+pid,    
      onSuccess: function(t) {
          reloadCartPage();
      },
      on404: function(t) {},      
      onFailure: function(t) {}
    }
    new Ajax.Request('data/removeproduct.php', opt);
}

function refreshCart()
{
    $('cartform').submit();
}

function reloadCartPage()
{
    location.reload();
}

function addToCart(pid, e)
{  
    var opt = {    
      method: 'post',
      postBody: 'pid='+pid,    
      onSuccess: function(t) {
          setCart(t.responseText, pid);
      },
      on404: function(t) {},      
      onFailure: function(t) {}
    }
    new Ajax.Request('data/addtocart.php', opt);
}

function setCart(output, pid)
{
    if(output == "differ")
    {
        location.href = "http://"+host+"/prodotto/"+pid;
        return 0;
    }    
    if(output == "-1")
        location.href = "login.html";
    var cart = output.split("|");        
    $('itemscart').innerHTML = cart[0];
    $('totcart').innerHTML = cart[1];
    $('order_span').style.display = "block";
    new Effect.Pulsate($('cartimg'+pid));   
}

function changePayment(e)
{
    var tot = parseFloat($('total').innerHTML) - parseFloat($('paycost').innerHTML);
    $('total').innerHTML = tot.toFixed(2);    
    var opt = {    
      method: 'post',
      postBody: 'pid='+e.value,    
      onSuccess: function(t) {
          setPayment(t.responseText);
      },
      on404: function(t) {},      
      onFailure: function(t) {}
    }
    new Ajax.Request('data/getpayment.php', opt);
}

function setPayment(cost)
{    
    if(cost == "-1")
        location.href = "login.html";        
    $('paycost').innerHTML = cost;
    $('paymode').innerHTML = $('pay').options[$('pay').selectedIndex].text;
    setTotal();
}

function changeShipment(e)
{
    var tot = parseFloat($('total').innerHTML) - parseFloat($('paycost').innerHTML);
    $('total').innerHTML = tot.toFixed(2);    
    var opt = {    
      method: 'post',
      postBody: 'pid='+e.value,    
      onSuccess: function(t) {
          setShipment(t.responseText);
      },
      on404: function(t) {},      
      onFailure: function(t) {}
    }
    new Ajax.Request('data/getshipment.php', opt);
}

function setShipment(cost)
{    
    if(cost == "-1")
        location.href = "login.html";        
    $('shipcost').innerHTML = cost;
    setTotal();
}    

function setTotal()
{
    var tot = parseFloat($('baseprice').innerHTML) +
              parseFloat($('paycost').innerHTML) + 
              parseFloat($('shipcost').innerHTML);
              
    $('total').innerHTML = tot.toFixed(2);
}

var win= null;
function showOrderDetail(oid)
{
    var winl = (screen.width-600)/2;
    var wint = (screen.height-500)/2;
    var settings ='height='+500+',';
    settings +='width='+600+',';
    settings +='top='+wint+',';
    settings +='left='+winl+',';
    settings +='scrollbars=1,';
    settings +='resizable=no';
    win=window.open('orderdetail.php?id='+oid,'Ordini',settings);
    if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}



