function parseParam(param){
	param = encodeURIComponent(param);
	return param;
}

var cart_url = 'lib/toysroom/cart/cart.php?';

function addToCart1(id_item, l){             
  var qty = 1;
  if (document.getElementById("q_"+id_item))
    var qty = document.getElementById("q_"+id_item).value;
  location.href = cart_url+"l="+l+"&op=add&id_item="+id_item+"&qty="+parseParam(qty);
}

function addToCart2(id_item, l){             
  var qty = 1;
  if (document.getElementById("q_"+id_item))
    var qty = document.getElementById("q_"+id_item).value;
  if (document.getElementById("t_"+id_item))
    var t = document.getElementById("t_"+id_item).value;
  if (document.getElementById("v_"+id_item))
    var v = document.getElementById("v_"+id_item).value;
  location.href = cart_url+"l="+l+"&op=add&id_item="+id_item+"&qty="+parseParam(qty)+"&t="+parseParam(t)+"&v="+parseParam(v);
}



function updateCart(id_item, n, l){
  var qty = 1;
  if (document.getElementById("q_"+id_item+'_'+n))
    var qty = document.getElementById("q_"+id_item+'_'+n).value;
  if (document.getElementById("t_"+id_item+'_'+n))
    var t = document.getElementById("t_"+id_item+'_'+n).value;
  if (document.getElementById("v_"+id_item+'_'+n))
    var v = document.getElementById("v_"+id_item+'_'+n).value;
  location.href = cart_url+"l="+l+"&op=upd&id_item="+id_item+"&qty="+parseParam(qty)+"&t="+parseParam(t)+"&v="+parseParam(v)+"&n="+parseParam(n);
}

function delFromCart(id_item, n, l){             
  location.href = cart_url+"l="+l+"&op=del&id_item="+id_item+"&n="+parseParam(n);
}

function delAllCart(l){             
  location.href = cart_url+"l="+l+"&op=delAll";  
}

function conferma(l){
  var go = true;
  for (i=0;i<document.forms[0].length;i++){
  	var name 	= document.forms[0].elements[i].name;
  	var value = document.forms[0].elements[i].value;
  	if (name){
  		if (name.substring(0,2) == "t_" && value == "0"){
        go = false;
      }
  		if (name.substring(0,2) == "v_" && value == "0"){
        go = false;
      }
  		if (name.substring(0,2) == "q_" && value == "0"){
        go = false;
      }
    }
  }
  
  if (l == "IT"){
    var error1 = "Scegliere area di spedizione";
    var error2 = "Riempire campi mancanti";
    var suffix = "";
  }
  if (l == "EN"){
    var error1 = "Choose shipping area";
    var error2 = "Please, fill empty fields";
    var suffix = "-en";
  }  
  
  if (document.getElementById("area_sped").value == 0)
    alert(error1);
  else if (go == false)
    alert(error2);
  else             
    location.href = "conferma"+suffix+".php";  
}


function checkArea(value, l){
  location.href = cart_url+"l="+l+"&op=area&area_sped="+value
}

function checkFatturazione(){
  if (document.getElementById("fatturazione").checked == false){
    document.getElementById("conferma2").style.display = "none";
  }
  else if (document.getElementById("fatturazione").checked == true){
    document.getElementById("conferma2").style.display = "block";
  }  
}


function checkFatturazione2(){
  if (document.getElementById("fatturazione2").checked == false){
    document.getElementById("indirizzo_sped").value = '';
    document.getElementById("citta_sped").value = '';
    document.getElementById("cap_sped").value = '';
    document.getElementById("provincia_sped").value = '';
    document.getElementById("stato_sped").value = '';
  }
  else if (document.getElementById("fatturazione2").checked == true){
    document.getElementById("indirizzo_sped").value = document.getElementById("indirizzo_fat").value;
    document.getElementById("citta_sped").value = document.getElementById("citta_fat").value;
    document.getElementById("cap_sped").value = document.getElementById("cap_fat").value;
    document.getElementById("provincia_sped").value = document.getElementById("provincia_fat").value;
    document.getElementById("stato_sped").value = document.getElementById("stato_fat").value;
  }  
}


function ordina(l){
  
  if (l == "IT"){
    var error1 = "Scegliere area di spedizione";
    var error2 = "Riempire campi mancanti";
    var suffix = "";
  }
  if (l == "EN"){
    var error1 = "Choose shipping area";
    var error2 = "Please, fill empty fields";
    var suffix = "-en";
  }  
  
  var colorKO = '#999999';
  if (document.getElementById("nome").value == ""){
    document.getElementById("nome").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("cognome").value == ""){
    document.getElementById("cognome").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("telefono").value == ""){
    document.getElementById("telefono").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("email").value == ""){
    document.getElementById("email").style.backgroundColor = colorKO;
    var found = error2;
  }
  /*if (document.getElementById("cf").value == ""){
    document.getElementById("cf").style.backgroundColor = colorKO;
    var found = error2;
  }*/
  if (document.getElementById("fatturazione").checked == true && document.getElementById("azienda_fat").value == ""){
    document.getElementById("azienda_fat").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("fatturazione").checked == true && document.getElementById("pi_fat").value == ""){
    document.getElementById("pi_fat").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("fatturazione").checked == true && document.getElementById("indirizzo_fat").value == ""){
    document.getElementById("indirizzo_fat").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("fatturazione").checked == true && document.getElementById("cap_fat").value == ""){
    document.getElementById("cap_fat").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("fatturazione").checked == true && document.getElementById("citta_fat").value == ""){
    document.getElementById("citta_fat").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("fatturazione").checked == true && document.getElementById("provincia_fat").value == ""){
    document.getElementById("provincia_fat").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("fatturazione").checked == true && document.getElementById("stato_fat").value == ""){
    document.getElementById("stato_fat").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("indirizzo_sped").value == ""){
    document.getElementById("indirizzo_sped").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("cap_sped").value == ""){
    document.getElementById("cap_sped").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("citta_sped").value == ""){
    document.getElementById("citta_sped").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("provincia_sped").value == ""){
    document.getElementById("provincia_sped").style.backgroundColor = colorKO;
    var found = error2;
  }
  if (document.getElementById("stato_sped").value == ""){
    document.getElementById("stato_sped").style.backgroundColor = colorKO;
    var found = error2;
  }
  
  if (found == error2){
    alert(error2);
  }
  else{
    document.getElementById("form_conferma").action = 'lib/toysroom/cart/cart.php?l='+l+'&op=conferma';
    document.getElementById("form_conferma").submit();  
  }
}


function colorOK(field){
  var color = "#FFFFFF";
  document.getElementById(field).style.backgroundColor = color;
}

function conferma_ordine(l){
  location.href = 'lib/toysroom/cart/cart.php?l='+l+'&op=conferma_ordine';
}


function indietro_conferma(l){
  if (l == "IT"){
    var suffix = "";
  }
  if (l == "EN"){
    var suffix = "-en";
  }  
  location.href = "conferma"+suffix+".php";
}
