//////////////////////////////////////////////
function UpdateQty(item){
itemId = item.name;
newQty = item.options[item.selectedIndex].value;
document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
}

///////////////////////////////////////////////
function UpdateTotal(item){
itemId = item.name;
newQty = item.options[item.selectedIndex].value;
document.location.href = 'cart.php?sp='+newQty;
}

////////////////////////////////////////////////////
function PopUp_open(url, name, h, w){
var bsotto = 100;

yst=((screen.availHeight)/2)-(h/2)- bsotto;
xst=((screen.availWidth)/2)-(w/2);

parametri='height='+h+',width='+w+',';
parametri+='toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1';
if (navigator.appName.indexOf("Microsoft") != -1){
window.open(url, name, parametri+",left="+xst+",top="+yst);
} else{
window.open(url, name, parametri+",screenX="+xst+",screenY="+yst);
}
}

//////////////////////////////////////////////////////////
function PopUp_open_addcart(url, name, h, w){
var bsotto = 100;

yst=((screen.availHeight)/2)-(h/2)- bsotto;
xst=((screen.availWidth)/2)-(w/2);

parametri='height='+h+',width='+w+',';
parametri+='toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0';
if (navigator.appName.indexOf("Microsoft") != -1){
window.open(url, name, parametri+",left="+xst+",top="+yst);
} else{
window.open(url, name, parametri+",screenX="+xst+",screenY="+yst);
}
}

///////////////////////////////////////////////////
function Reset_fields(nome_form){
nome_form = "document."+nome_form;		
for (i=0;i< eval(nome_form+".length");i++){
var fild_name=eval(nome_form).elements[i].name;		
var field_type=eval(nome_form).elements[i].type;
if (field_type=="text"){
var campo=eval(nome_form).elements[i].name;
eval(nome_form+"."+campo+".value=\"\"");
}
if (field_type=="select-one"){
var campo=eval(nome_form).elements[i].name;
eval(nome_form+"."+campo+".selectedIndex=0");
}		
if (field_type=="checkbox"){
var campo=eval(nome_form).elements[i].name;
eval(nome_form+"."+campo+".checked=0");
}
if (field_type=="radio"){ 
var campo=eval(nome_form).elements[i].name;
campo_radio_lung = nome_form + "." + campo + ".length";
if (eval(campo_radio_lung) > 1) {
for (j=0;j< eval(campo_radio_lung);j++){
eval(nome_form + "." + campo + "[j].checked=0");
}	
} else {
eval(nome_form + "." + campo + ".checked=0");
}
}
if (field_type =="textarea"){
var campo=eval(nome_form).elements[i].name;
eval(nome_form+"."+campo+".value=\"\"");
}
}
}

// CONTROLLO RIEMPIMENTO CAMPI REGISTRAZIONE
function fieldChecked(){
var vuoto = 0;
var alerts = "ATTENZIONE!\nERRORE NELLA COMPILAZIONE DEL MODULO:\n\n";

// DATI UTENTE
if(Trim(document.registr.nome.value) == ""){
alerts += "- Il campo Nome è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.cognome.value) == ""){
alerts += "- Il campo Cognome è vuoto.\n";
vuoto++;
}

//if(Trim(document.registr.birthday1.value) == "" || Trim(document.registr.birthday2.value) == "" || Trim(document.registr.birthday3.value) == ""){
//alerts += "- Il campo Data di nascita è vuoto.\n";
//vuoto++;
//}

//if(checkBirthday() == false){
//alerts += "- La data di nascita non è in un formato corretto. Es. gg-mm-aaaa.\n";
//vuoto++;
//}

//if(!document.registr.sesso[0].checked && !document.registr.sesso[1].checked){
//alerts += "- Il campo Sesso è vuoto.\n";
//vuoto++;
//}

if(Trim(document.registr.indirizzo.value) == ""){
alerts += "- Il campo Indirizzo è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.numciv.value) == ""){
alerts += "- Il campo Numero civico è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.cap.value) == ""){
alerts += "- Il campo Cap è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.citta.value) == ""){
alerts += "- Il campo Città è vuoto.\n";
vuoto++;
}

if(document.registr.provincia.options[document.registr.provincia.selectedIndex].value == ""){
alerts += "- Il campo Provincia è vuoto.\n";
vuoto++;
}

if(document.registr.professione.options[document.registr.professione.selectedIndex].value == ""){
alerts += "- Il campo Professione è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.email.value) == ""){
alerts += "- Il campo E-mail è vuoto.\n";
vuoto++;
}

if(checkEmail() == false){
alerts += "- La mail non è di un formato corretto. Es. mariorossi@dominio.it.\n";
vuoto++;
}

if(Trim(document.registr.telefono.value) == ""){
alerts += "- Il campo Telefono è vuoto.\n";
vuoto++;
}

if(checkTelefono() == false){
alerts += "- Il telefono deve essere composto solo da numeri, slash (/) o trattini (-).\n";
vuoto++;
}

if(Trim(document.registr.pwd.value) == ""){
alerts += "- Il campo Password è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.pwd2.value) == ""){
alerts += "- Il campo Conferma password è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.pwd.value) != Trim(document.registr.pwd2.value)){
alerts += "- Le password inserite non coincidono.\n";
vuoto++;
}

// DATI DI FATTURAZIONE
if(document.registr.fatturazione[0].checked){
alerts += "DATI DI FATTURAZIONE\n\n";

if(Trim(document.registr.nome_f.value) == ""){
alerts += "- Il campo Nome e Cognome o ragione sociale è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.indirizzo_f.value) == ""){
alerts += "- Il campo Indirizzo è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.numciv_f.value) == ""){
alerts += "- Il campo Numero civico è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.cap_f.value) == ""){
alerts += "- Il campo Cap è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.citta_f.value) == ""){
alerts += "- Il campo Città è vuoto.\n";
vuoto++;
}

if(document.registr.provincia_f.options[document.registr.provincia_f.selectedIndex].value == ""){
alerts += "- Il campo Provincia è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.CF.value) == ""){
alerts += "- Il campo Codice Fiscale è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.PI.value) == ""){
alerts += "- Il campo Partita IVA è vuoto.\n";
vuoto++;
}

if(checkPI() == false){
alerts += "- La Partita IVA è errata.\n";
vuoto++;
}

alerts += "\n\nRICORDA! Se non intendi ricevere la fattura seleziona \"NO\".\n";
}

// MAILINGLIST
/*if(document.registr.mailinglist.value != ""){
campo = document.registr.mailinglist.value;
if(checkEmailGeneral(campo) == false){
alerts += "\n\nMAILING LIST\n\n";
alerts += "- La mail non è di un formato corretto. Es. mariorossi@dominio.it.\n";
vuoto++;
}
}*/

if(vuoto == 0){
return true;
} else{
window.alert(alerts);
return false;
}
}

////////////////////////////////////////////////////
function fatturazioneChecked(){
if(!document.registr.fatturazione[0].checked){
document.registr.fatturazione[0].checked = true;
document.registr.fatturazione[1].checked = false;
}
}

///////////////////////////////////////////////////
function fatturazioneControl(){
// CHECKED SI /////////
if(document.registr.fatturazione[0].checked == true){
document.registr.fatturazione[1].checked = false;
document.registr.nome_f.value = document.registr.nome.value+" "+document.registr.cognome.value;
document.registr.indirizzo_f.value = document.registr.indirizzo.value;
document.registr.numciv_f.value = document.registr.numciv.value;
document.registr.cap_f.value = document.registr.cap.value;
document.registr.citta_f.value = document.registr.citta.value;
document.registr.provincia_f.options[document.registr.provincia.selectedIndex].selected = true; //= document.registr.provincia.options[document.registr.provincia.selectedIndex].value;
}

// CHECKED NO ////////////////////////
if(document.registr.fatturazione[1].checked == true){
document.registr.fatturazione[0].checked = false;
document.registr.nome_f.value = "";
document.registr.indirizzo_f.value = "";
document.registr.numciv_f.value = "";
document.registr.cap_f.value = "";
document.registr.citta_f.value = "";
document.registr.CF.value = "";
document.registr.PI.value = "";
document.registr.provincia_f.options[0].selected = true;
}
}

// CONTROLLO RIEMPIMENTO CAMPI 2 - MODIFICA DATI
function fieldChecked2(){
var vuoto = 0;
var alerts = "ATTENZIONE!\nERRORE NELLA COMPILAZIONE DEL MODULO:\n\n";

// DATI UTENTE
if(Trim(document.registr.nome.value) == ""){
alerts += "- Il campo Nome è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.cognome.value) == ""){
alerts += "- Il campo Cognome è vuoto.\n";
vuoto++;
}

//if(!document.registr.sesso[0].checked && !document.registr.sesso[1].checked){
//alerts += "- Il campo Sesso è vuoto.\n";
//vuoto++;
//}

if(Trim(document.registr.indirizzo.value) == ""){
alerts += "- Il campo Indirizzo è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.numciv.value) == ""){
alerts += "- Il campo Numero civico è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.cap.value) == ""){
alerts += "- Il campo Cap è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.citta.value) == ""){
alerts += "- Il campo Città è vuoto.\n";
vuoto++;
}

if(document.registr.provincia.options[document.registr.provincia.selectedIndex].value == ""){
alerts += "- Il campo Provincia è vuoto.\n";
vuoto++;
}

if(document.registr.professione.options[document.registr.professione.selectedIndex].value == ""){
alerts += "- Il campo Professione è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.telefono.value) == ""){
alerts += "- Il campo Telefono è vuoto.\n";
vuoto++;
}

if(checkTelefono() == false){
alerts += "- Il telefono deve essere composto solo da numeri, slash (/) o trattini (-).\n";
vuoto++;
}

// DATI DI FATTURAZIONE
if(document.registr.fatturazione[0].checked){
alerts += "DATI DI FATTURAZIONE\n\n";

if(Trim(document.registr.nome_f.value) == ""){
alerts += "- Il campo Nome e Cognome o ragione sociale è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.indirizzo_f.value) == ""){
alerts += "- Il campo Indirizzo è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.numciv_f.value) == ""){
alerts += "- Il campo Numero civico è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.cap_f.value) == ""){
alerts += "- Il campo Cap è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.citta_f.value) == ""){
alerts += "- Il campo Città è vuoto.\n";
vuoto++;
}

if(document.registr.provincia_f.options[document.registr.provincia_f.selectedIndex].value == ""){
alerts += "- Il campo Provincia è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.CF.value) == ""){
alerts += "- Il campo Codice Fiscale è vuoto.\n";
vuoto++;
}

if(Trim(document.registr.PI.value) == ""){
alerts += "- Il campo Partita IVA è vuoto.\n";
vuoto++;
}

if(checkPI() == false){
alerts += "- La Partita IVA è errata.\n";
vuoto++;
}

alerts += "\n\nRICORDA! Se non intendi ricevere la fattura seleziona \"NO\".\n";
}

if(vuoto == 0){
return true;
} else{
window.alert(alerts);
return false;
}
}

///////////////////////////////////////////////////////////
// CONTROLLO PARTITA IVA /////////////////////////////////
/*function partitaIVA(sz_Codice){
var n_Val,n_Som1=0,n_Som2=0,lcv;
if (sz_Codice.length!=11 || isNaN(parseFloat(sz_Codice)) || parseFloat(sz_Codice)<parseFloat(0)) return false;
   
for (lcv=0;lcv<9;lcv+=2){
n_Val=parseInt(sz_Codice.charAt(lcv));
n_Som1+=n_Val;
n_Val=parseInt(sz_Codice.charAt(lcv+1));
n_Som1+=Math.floor(n_Val/5) + (n_Val<<1) % 10;
}
n_Som2 = 10 - (n_Som1 % 10);
n_Val=parseInt(sz_Codice.charAt(10));
if (n_Som2==n_Val) return true;

return false;
}*/

function partitaIVA(pi)
{
    if( pi == '' )  return false;
    if( pi.length != 11 )
        return false;
    validi = "0123456789";
    for( i = 0; i < 11; i++ ){
        if( validi.indexOf( pi.charAt(i) ) == -1 )
            return false;
    }
    s = 0;
    for( i = 0; i <= 9; i += 2 )
        s += pi.charCodeAt(i) - '0'.charCodeAt(0);
    for( i = 1; i <= 9; i += 2 ){
        c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
        if( c > 9 )  c = c - 9;
        s += c;
    }
    if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
        return false;
    return true;
}

//////////////////////////////////////////
function checkPI(){
var PIvaOk = partitaIVA(Trim(document.registr.PI.value))
if (PIvaOk == false){
return false;
}
return true;
}

/////////////////////////////////////////////
function checkEmail(){
var str = Trim(document.registr.email.value);
var val_mail = "^([a-z0-9]+)([._-]([a-z0-9]+))*[@]([a-z0-9]+)([._-]([a-z0-9]+))*[.]([a-z0-9]){2}([a-z0-9])?$";
var link_reg_exp = new RegExp(val_mail);
if(str != ""){
if(link_reg_exp.test(str)){
return true;
} else{
return false;
}
}
}

///////////////////////////////////////
function checkBirthday(){
var str = Trim(document.registr.birthday1.value)+"-"+Trim(document.registr.birthday2.value)+"-"+Trim(document.registr.birthday3.value);
var val_mail = "^[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}$";
var link_reg_exp = new RegExp(val_mail);
if(str != ""){
if(link_reg_exp.test(str)){
return true;
} else{
return false;
}
}
}

///////////////////////////////////////
function checkTelefono(){
var str = Trim(document.registr.telefono.value);
var val_mail = "^[0-9 \-/]+$";
var link_reg_exp = new RegExp(val_mail);
if(str != ""){
if(link_reg_exp.test(str)){
return true;
} else{
return false;
}
}
}

function Trim(field) { 
return field.replace(/\s+$|^\s+/g,""); 
}

/////////////////////////////////////////////
function checkEmailGeneral(campo){
var str = Trim(campo);
var val_mail = "^([a-z0-9]+)([._-]([a-z0-9]+))*[@]([a-z0-9]+)([._-]([a-z0-9]+))*[.]([a-z0-9]){2}([a-z0-9])?$";
var link_reg_exp = new RegExp(val_mail);
if(str != ""){
if(link_reg_exp.test(str)){
return true;
} else{
return false;
}
}
}

// JavaScript Document
