// mostro / nascondo il <div> delle spedizioni
function OpenCloseDivIndirizziAdmin(obj) {
	if (obj.style.display == 'block') {
		obj.style.display = 'none';
	}
	else {
		obj.style.display = 'block';
	}
}

// mostro / nascondo il <tr> del dettaglio acquisto
function OpenCloseDettaglio(id_obj) {
	obj = document.getElementById('dett' + id_obj);
	if (obj.style.display == 'block') {
		obj.style.display = 'none';
	}
	else {
		obj.style.display = 'block';
	}
}

// mostro / nascondo il <tr> delle provincie nel form di registrazione utente
function ShowTrProvincie(obj, num) {

	// default value for "num"
	num = typeof(num) != 'undefined' ? num : '';

	var trp = document.getElementById('tr_provincie'+num);
	if (obj.value == 468) {
		trp.style.visibility = 'visible';
	} else {
		trp.style.visibility = 'hidden';
	}
}

// mostro / nascondo il <div> con l'input per i monogrammi nelle opzioni camicie
function ShowDivMonogrammi(obj) {
	var dm = document.getElementById('div_monogrammi');
	if (obj.value != 9) {
		dm.style.visibility = 'visible';
	} else {
		dm.style.visibility = 'hidden';
	}
}

// mostro / nascondo il <div> per gli indirizzi
function ShowDivIndirizzi(div) {
	var dm = document.getElementById(div);
	if (dm.style.visibility == 'hidden') {
		dm.style.visibility = 'visible';
		dm.style.height = '100%';
	} else {
		dm.style.visibility = 'hidden';
		dm.style.height = '0px';
	}
}


// verifico i campi della form di registrazione (/registrazione-utente)
function RegUtenteCheckForm() {

	var errori = new Array();
	var frm = document.forms['registrazione'];

	if (frm.tipo_persona.options[frm.tipo_persona.selectedIndex].value == 1) {
		if (frm.f400_nome.value == '') {
			errori.push('inserire il nome');
		}
		if (frm.f400_cognome.value == '') {
			errori.push('il cognome non puo\' essere vuoto');
		}
	} else if(frm.tipo_persona.options[frm.tipo_persona.selectedIndex].value == 2){
		if (frm.f400_nome.value == '') {
			errori.push('inserire la ragione sociale');
		}
	}
	if (frm.f401_citta.value == '') {
		errori.push('inserire la localit\u00E0');
	}
	if (frm.f401_via.value == '') {
		errori.push('inserire l\' indirizzo');
	}
	if (frm.f401_cap.value == '') {
		errori.push('inserire il cap');
	}
	if (frm.f401_partita_iva.value == '') {
			errori.push('inserire la partita iva');
		}
	if (frm.f400_mail.value == '') {
		errori.push('inserire l\' email');
	}

	if (frm.f400_password) {
		if (frm.f400_password.value == '') {
			errori.push('indicare una password');
		}
		if (frm.f400_password.value.length < 6) {
			errori.push('indicare una password lunga almeno 6 caratteri');
		}
		if (frm.f400_password.value == frm.f400_username.value) {
			errori.push('indicare una password diversa dallo username');
		}
	}

	if (frm.f400_username.value == '') {
		errori.push('lo username non puo\' essere vuoto');
	}
	else if (frm.f400_username.value.length < 6) {
		errori.push('lo username deve essere lungo almeno 6 caratteri');
	}

	if(errori.length > 0){
		txt_alert = 'Per completare la registrazione occorre:\n\n';
		for (x=0; x<errori.length; x++) {
			txt_alert = txt_alert + ' - ' + errori[x] + '\n';
		}
		txt_alert = txt_alert + '\n';
		alert(txt_alert);
		return false;
	}
	else {

		// lo username è già in uso ?
		RegUtenteCheckUsernameAvailable(frm.f400_username.value, frm.f400_id_utenti.value);
	}

}
function RegUtenteCheckForm2() {
	var errori = new Array();
	var frm = document.forms['registrazione'];

	if (frm.tipo_persona.options[frm.tipo_persona.selectedIndex].value == 1) {
		if (frm.f400_nome.value == '') {
			errori.push('inserire il nome');
		}
		if (frm.f400_cognome.value == '') {
			errori.push('il cognome non puo\' essere vuoto');
		}
	} else if(frm.tipo_persona.options[frm.tipo_persona.selectedIndex].value == 2){
		if (frm.f400_nome.value == '') {
			errori.push('inserire la ragione sociale');
		}
	}
	if (frm.f401_r451_id_nazioni.value == '') {
		errori.push('inserire la nazione');
	}
	if (frm.f401_citta.value == '') {
		errori.push('inserire la localit\u00E0');
	}
	if (frm.f401_via.value == '') {
		errori.push('inserire l\'indirizzo');
	}
	if (frm.f401_cap.value == '') {
		errori.push('inserire il cap');
	}
	if (frm.f400_tel.value == '') {
		errori.push('inserire il recapito telefonico');
	}
	if (frm.f400_mail.value == '') {
		errori.push('inserire l\'email');
	}
	if (frm.f401_partita_iva.value == '') {
		errori.push('inserire il codice fiscale o la partita iva');
	}
	if (frm.accetta.checked != true) {
		errori.push('dare il consenso al trattamento dei dati personali');
	}

	if (frm.f400_password) {
		if (frm.f400_password.value == '') {
			errori.push('indicare una password');
		}
		if (frm.f400_password.value.length < 6) {
			errori.push('indicare una password di almeno 6 caratteri');
		}
		if (frm.f400_password.value == frm.f400_username.value) {
			errori.push('indicare una password diversa dallo username');
		}
	}

	if (errori.length == 0) {
		frm.submit();
		return false;
	} else {
		txt_alert = 'Per completare la registrazione occorre:\n\n';
		for (x=0; x<errori.length; x++) {
			txt_alert = txt_alert + ' - ' + errori[x] + '\n';
		}
		txt_alert = txt_alert + '\n';
		alert(txt_alert);
		return false;
	}


}

function RegUtenteCheckForm3() {

	var errori = new Array();
	var frm = document.forms['registrazione'];

	if (frm.tipo_persona.options[frm.tipo_persona.selectedIndex].value == 1) {
		if (frm.f400_nome.value == '') {
			errori.push('inserire il nome');
		}
		if (frm.f400_cognome.value == '') {
			errori.push('il cognome non puo\' essere vuoto');
		}
	} else if(frm.tipo_persona.options[frm.tipo_persona.selectedIndex].value == 2){
		if (frm.f400_nome.value == '') {
			errori.push('inserire la ragione sociale');
		}
	}
	if (frm.f400_mail.value == '') {
		errori.push('inserire l\'email');
	}

	if (errori.length == 0) {
		frm.submit();
		return false;
	} else {
		txt_alert = 'Per completare la registrazione occorre:\n\n';
		for (x=0; x<errori.length; x++) {
			txt_alert = txt_alert + ' - ' + errori[x] + '\n';
		}
		txt_alert = txt_alert + '\n';
		alert(txt_alert);
		return false;
	}


}

// verifico la validità dello username, via AJAX
function RegUtenteCheckUsernameAvailable(username, f400_id_utenti) {

  // variabili di funzione
  var
    // assegnazione oggetto XMLHttpRequest
    ajax = assegnaXMLHttpRequest();

  // se l'oggetto XMLHttpRequest non è nullo
  if(ajax) {

    // impostazione richiesta asincrona in GET
    // del file specificato
    ajax.open("post", "/ecommerce/utenti_registrazione_check_username.php", true);
    ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("connection", "close");

    // impostazione controllo e stato della richiesta
    ajax.onreadystatechange = function() {

      // verifica dello stato
      if(ajax.readyState === readyState.COMPLETATO) {
        // verifica della risposta da parte del server
        if(statusText[ajax.status] === "OK") {
          // operazione avvenuta con successo, ritorno al chiamante un ok / ko
          //alert(ajax.responseText);
          user_available = ajax.responseText;
          if (user_available == 'ok1') {
          	RegUtenteCheckForm2();
          } else if (user_available == 'ok2') {
          	RegUtenteCheckForm3();
          }
          else {
          	alert('Lo username che hai scelto \u00E8 gi\u00E0 in uso\nPer favore scegline uno diverso.');
          }
        }
      }
    }

    // invio richiesta
    ajax.send("username="+username+"&f400_id_utenti="+f400_id_utenti);
  }

}

// funzione chiamata dal check-out per salvare il testo del pacchetto regalo
function SalvaPacchettoRegalo(id_acquisto, testo){
	// variabili di funzione
  var
    // assegnazione oggetto XMLHttpRequest
    ajax = assegnaXMLHttpRequest();

  // se l'oggetto XMLHttpRequest non è nullo
  if(ajax){

    // impostazione richiesta asincrona in GET
    // del file specificato
    ajax.open("post", "/ecommerce/check_out_pacchetto_regalo_salva.php", true);
    ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("connection", "close");

    // impostazione controllo e stato della richiesta
    ajax.onreadystatechange = function() {

      // verifica dello stato
      if(ajax.readyState === readyState.COMPLETATO) {
        // verifica della risposta da parte del server
        if(statusText[ajax.status] === "OK") {
          // operazione avvenuta con successo, ritorno al chiamante un ok / ko
          if (ajax.responseText === 'OK') {
			document.metodo_pagamento.pacchetto_regalo_default.value = testo;
          } else {
          	/*Errore*/
          	alert('Errore nel salvataggio del testo del pacchetto regalo.');
          }
        }
      }
    }
  }
    // invio richiesta
   	ajax.send("id_acquisti="+id_acquisto+"&testo="+testo);
}

// funzione chiamata dal check-out per salvare le note per il venditore
function SalvaNoteVenditore(id_acquisto, testo){
	// variabili di funzione
  var
    // assegnazione oggetto XMLHttpRequest
    ajax = assegnaXMLHttpRequest();

  // se l'oggetto XMLHttpRequest non è nullo
  if(ajax){

    // impostazione richiesta asincrona in GET
    // del file specificato
    ajax.open("post", "/ecommerce/check_out_note_venditore_salva.php", true);
    ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("connection", "close");

    // impostazione controllo e stato della richiesta
    ajax.onreadystatechange = function() {

      // verifica dello stato
      if(ajax.readyState === readyState.COMPLETATO) {
        // verifica della risposta da parte del server
        if(statusText[ajax.status] === "OK") {
          // operazione avvenuta con successo, ritorno al chiamante un ok / ko
          if (ajax.responseText === 'OK') {
			document.metodo_pagamento.note_venditore_default.value = testo;
          } else {
          	/*Errore*/
          	alert('Errore nel salvataggio delle note per il venditore.');
          }
        }
      }
    }
  }
    // invio richiesta
   	ajax.send("id_acquisti="+id_acquisto+"&testo="+testo);
}
