// JavaScript Document
var lung_minima_password = 6; //lunghezza minima delle password


//funzione che elimina tutti gli spazi da una stringa di testp
function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

//funzione di verifica alfanumerica
function is_alfanumeric(stringa) {
	var myregexp = /^[a-zA-Z0-9]+$/;
	
	if (myregexp.test(stringa) == false) {
		return false;
		} else {
			return true;
		}
	
	}	

//controllo validità email
function is_valid(mail) {
	
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;	
		
		if (!espressione.test(mail.toLowerCase()))	{	    
			return false;
			}

	return true;
	}


//controllo correttezza della data
function controllo_data(stringa){	

	var espressione = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;	
	
	if (!espressione.test(stringa))	{	    
	
		return false;	
	
	} else {		

		anno = parseInt(stringa.substr(6),10);		
		mese = parseInt(stringa.substr(3, 2),10);		
		giorno = parseInt(stringa.substr(0, 2),10);				
		var data=new Date(anno, mese-1, giorno);		
		if(data.getFullYear()==anno && data.getMonth()+1==mese && data.getDate()==giorno){			
			
			return true;		
		
		} else {			
			
			return false;		
		
		}	
		
	}

}

//funzione di controllo form di prima registrazione
function checkPrimaRegistrazione(theForm) {

	//campo Nome obbligatorio
	if (TrimString(theForm.nome.value) == "") {
		alert(ln_var_1);
		theForm.nome.focus();
		return false;
		}

	//campo cognome obbligatorio
	if (TrimString(theForm.cognome.value) == "") {
		alert(ln_var_2);
		theForm.cognome.focus();
		return false;
		}

	//campo indirizzo obbligatorio
	if (TrimString(theForm.indirizzo.value) == "") {
		alert(ln_var_3);
		theForm.indirizzo.focus();
		return false;
		}

	//campo cap obbligatorio
	if (TrimString(theForm.cap.value) == "") {
		alert(ln_var_4);
		theForm.cap.focus();
		return false;
		}

	//campo città obbligatorio
	if (TrimString(theForm.localita.value) == "") {
		alert(ln_var_5);
		theForm.localita.focus();
		return false;
		}

	//campo nazione obbligatorio
	if (TrimString(theForm.cod_nazione.value) == "") {
		alert(ln_var_6);
		theForm.cod_nazione.focus();
		return false;
		}

	//controllo che la mail inserita sia valida
	if (is_valid(theForm.email_address.value) == false) {
		alert(ln_var_7);
		theForm.email_address.focus();
		return false;
		}

	//controllo che le due email corrispondano
	if (TrimString(theForm.email_address.value) != TrimString(theForm.confirm_address.value)) {
		alert(ln_var_8);
		theForm.confirm_address.focus();
		return false;
		}

	//controllo che la mail inserita sia valida
	if ((TrimString(theForm.passwd.value).length < lung_minima_password) || (is_alfanumeric(TrimString(theForm.passwd.value)) == false)) {
		alert(ln_var_9);
		theForm.passwd.focus();
		return false;
		}

	//controllo che le due email corrispondano
	if (TrimString(theForm.passwd.value) != TrimString(theForm.confirm_passwd.value)) {
		alert(ln_var_10);
		theForm.confirm_passwd.focus();
		return false;
		}

	//controllo che la conferma mail sia esatta
	if ((TrimString(theForm.dd.value) == "") || (TrimString(theForm.mm.value) == "") || (TrimString(theForm.yyyy.value) == "")) {
		alert(ln_var_11);
		return false;
		}
		
	if (controllo_data(TrimString(theForm.dd.value)+"/"+TrimString(theForm.mm.value)+"/"+TrimString(theForm.yyyy.value)) == false) {
		alert(ln_var_12);
		return false;		
		}
				
	//controllo che abbia selezionato la lingua
	if (TrimString(theForm.cod_lingua.value) == "") {
		alert(ln_var_18);
		return false;
		}

	//controllo che abbia selezionato la valuta
	if (TrimString(theForm.cod_valuta.value) == "") {
		alert(ln_var_19);
		return false;
		}

	//controllo che abbia selezionato la valuta
	if (theForm.condizioni.checked == false) {
		alert(ln_var_26);
		return false;
		}

	return true;
	
	}
	
//funzione che recupera i parametri del form e fa la ricerca
function getRisultati(theForm) {
	
	//controllo se è stata specificata una keyword
	if (TrimString(theForm.keyword.value) != "") {
		//cambio il parametro della action e invio il form
		theForm.action = "results.php";
		return true;
		}
	
	return true;
	}
	
//funzione controlla la quantità che sto inserendo
function add_to_cart(theForm) {
	
	//controllo se è stata specificata una keyword
	if ((TrimString(theForm.quantita.value) == "") || (isNaN(theForm.quantita.value))) {
		alert(ln_var_13);
		theForm.quantita.focus();
		return false;
		}
	
	theForm.quantita.value = parseInt(theForm.quantita.value);
	return true;
	}
	
//funzione di controllo della quantità onblur
function checkQuantita(e) {
	
	//controllo se è stata specificata una keyword
	if ((TrimString(e.value) == "") || (isNaN(e.value))) {
		alert(ln_var_14);
		e.select();
		return false;
		}
	
	e.value = parseInt(e.value);
	return true;
	}
	
//controllo nella pagina datalost
function checkDatalost(theForm) {
	
		//controllo che la mail inserita sia valida
	if (is_valid(theForm.email.value) == false){
		alert(ln_var_7);
		theForm.email.focus();
		return false;
		}

	return true;
	}
	
	
//funzione che verifica la ricerca submit per Serie
function getSerieSubmit(theForm) {
	
	if (TrimString(theForm.cod_serie.value) != "") {
		return true;
		}
	
	return false;
	}

//funzione che verifica la ricerca submit per Linee
function getLineaSubmit(theForm) {
	
	if (TrimString(theForm.cod_linea.value) != "") {
		return true;
		}
	
	return false;
	}

//funzione che verifica la ricerca select per Serie
function getSerieSelect(theForm) {
	
	if (TrimString(theForm.cod_serie.value) != "") {
		theForm.submit();
		}
	
	}

//funzione che verifica la ricerca select per Linee
function getLineaSelect(theForm) {
	
	if (TrimString(theForm.cod_linea.value) != "") {
		theForm.submit();
		}
	
	}


//funzione che verifica la ricerca submit per Categorie
function getCategorySubmit(theForm) {
	
	if (TrimString(theForm.cod_settore.value) != "") {
		return true;
		}
	
	return false;
	}

//funzione che verifica la ricerca submit per Serie
function getCategorySelect(theForm) {
	
	//controllo se è stata specificata una keyword
	if (TrimString(theForm.cod_settore.value) != "") {
		theForm.submit();
		}
	
	}

//funzione che verifica la ricerca submit per Categorie
function getDescriptionSubmit(theForm) {
	
	if (TrimString(theForm.des_prodotto.value) != "") {
		
		//if (is_alfanumeric(TrimString(theForm.des_prodotto.value)) == true) return true;
		return true;
		
		}
	
	return false;
	}

//funzione di controllo pagina spese di spedizione
function confirmOrder(theForm) {
	
	if (TrimString(theForm.telefono.value) == "") {
		
		alert(ln_var_16);
		theForm.telefono.focus();
		return false;
		}
	
	return true;
	}
	
//funzione di chiusura ordine
function closeOrder(theForm) {
	
	//controllo se ho selezionato la carta di credito, se si, cambio la action del FORM
	
	var scelta = "";
	
	//alert(theForm.tipo_pagamento.length);
	
	if (theForm.tipo_pagamento.length == undefined) {
		
		//alert("qui");

		scelta = theForm.tipo_pagamento.value;
	
	} else {
	
		for (var counter = 0; counter < theForm.tipo_pagamento.length; counter++){ 
			
			if (theForm.tipo_pagamento[counter].checked){ 
				scelta = theForm.tipo_pagamento[counter].value;
			}
		
		}
		
	}

	if (scelta == "CRE") {
		theForm.action = "https://security.papagena-payment.de/3DSecure/papagena_3DSecure.php";
		}
		
	
	//alert(scelta);
	
	if ( (scelta == "") || (scelta == undefined) ) {
		alert("STOP");
		return false;
		}
	
	
	if (theForm.condizioni.checked == false) {
		alert(ln_var_26);
		return false;
		}
	
	var conferma = confirm(ln_var_17);
	if (conferma == false) {
	return false;	
	}
return true;
}

//funzione di chiusura ordine
function closeOrder_solocc(theForm) {
	
	theForm.action = "https://security.papagena-payment.de/3DSecure/papagena_3DSecure.php";		
	//alert(theForm.action);
	var conferma = confirm(ln_var_17);
	
	if (conferma == false) {
		return false;	
	}

return true;

}

//funzione di controllo form modifica profilo
function checkSalvaProfilo(theForm) {
	
	//campo Nome obbligatorio
	if (TrimString(theForm.nome.value) == "") {
		alert(ln_var_1);
		theForm.nome.focus();
		return false;
		}

	//campo cognome obbligatorio
	if (TrimString(theForm.cognome.value) == "") {
		alert(ln_var_2);
		theForm.cognome.focus();
		return false;
		}

	//campo indirizzo obbligatorio
	if (TrimString(theForm.indirizzo.value) == "") {
		alert(ln_var_3);
		theForm.indirizzo.focus();
		return false;
		}

	//campo cap obbligatorio
	if (TrimString(theForm.cap.value) == "") {
		alert(ln_var_4);
		theForm.cap.focus();
		return false;
		}

	//campo città obbligatorio
	if (TrimString(theForm.localita.value) == "") {
		alert(ln_var_5);
		theForm.localita.focus();
		return false;
		}

	//campo nazione obbligatorio
	if (TrimString(theForm.cod_nazione.value) == "") {
		alert(ln_var_6);
		theForm.cod_nazione.focus();
		return false;
		}

	//controllo formale data nascita
	if ((TrimString(theForm.dd.value) == "") || (TrimString(theForm.mm.value) == "") || (TrimString(theForm.yyyy.value) == "")) {
		alert(ln_var_11);
		return false;
		}
		
	if (controllo_data(TrimString(theForm.dd.value)+"/"+TrimString(theForm.mm.value)+"/"+TrimString(theForm.yyyy.value)) == false) {
		alert(ln_var_12);
		return false;		
		}
				
	//controllo che abbia selezionato la lingua
	if (TrimString(theForm.cod_lingua.value) == "") {
		alert(ln_var_18);
		return false;
		}

	//controllo che abbia selezionato la valuta
	if (TrimString(theForm.cod_valuta.value) == "") {
		alert(ln_var_19);
		return false;
		}


	return true;
	}


function checkCambiaPassword(theForm) {
	
	//controllo che abbia inserito la password
	if (TrimString(theForm.oldpsw.value) == false) {
		alert(ln_var_9);
		theForm.oldpsw.focus();
		return false;
		}

	//controllo che la nuova password inserita sia valida
	if ((TrimString(theForm.newpsw.value).length < lung_minima_password) || (is_alfanumeric(TrimString(theForm.newpsw.value)) == false)) {
		alert(ln_var_9);
		theForm.newpsw.focus();
		return false;
		}

	if (TrimString(theForm.oldpsw.value) == TrimString(theForm.newpsw.value)) {
		alert(ln_var_20);
		theForm.newpsw.focus();
		return false;
		}

	//controllo che le due password corrispondano
	if (TrimString(theForm.newpsw.value) != TrimString(theForm.confirmpsw.value)) {
		alert(ln_var_10);
		theForm.confirmpsw.focus();
		return false;
		}


	return true;
	}

	//aggiunta alla wishlist
	function addToWishList(theForm) {
		theForm.action = "add_to_wish.php";
		theForm.submit();
		}
		
	//email to friend
	function emailToFriend(theForm) {
		theForm.action = "email_to_friend.php";
		theForm.submit();
		}
		
	//funzione di controllo invio "dillo ad un amico"
	function checkEmailToFriend(theForm) {

	//controllo se è stato inserito il nome
	if (TrimString(theForm.your_name.value) == "") {
		alert(ln_var_21);
		theForm.your_name.focus();
		return false;
		}

	//controllo che la mail inserita sia valida
	if (is_valid(theForm.your_mail.value) == false) {
		alert(ln_var_22);
		theForm.your_mail.focus();
		return false;
		}
	
	//controllo che la mail inserita sia valida
	if (is_valid(theForm.friend_mail.value) == false) {
		alert(ln_var_23);
		theForm.friend_mail.focus();
		return false;
		}
		
		}
		
	function removeDeliveryAddress(id) {
		var conferma = confirm(ln_var_25);
	
		if (conferma == true) {
		location.href = "remove_delivery_address.php?id="+id;
		}
		
		}
		
//funzione di controllo form modifica indirizzo di spedizione
function checkSalvaIndirizzo(theForm) {
	
	//campo Nome obbligatorio
	if (TrimString(theForm.nome.value) == "") {
		alert(ln_var_1);
		theForm.nome.focus();
		return false;
		}

	//campo cognome obbligatorio
	if (TrimString(theForm.cognome.value) == "") {
		alert(ln_var_2);
		theForm.cognome.focus();
		return false;
		}

	//campo indirizzo obbligatorio
	if (TrimString(theForm.indirizzo.value) == "") {
		alert(ln_var_3);
		theForm.indirizzo.focus();
		return false;
		}

	//campo cap obbligatorio
	if (TrimString(theForm.cap.value) == "") {
		alert(ln_var_4);
		theForm.cap.focus();
		return false;
		}

	//campo città obbligatorio
	if (TrimString(theForm.localita.value) == "") {
		alert(ln_var_5);
		theForm.localita.focus();
		return false;
		}

	//campo nazione obbligatorio
	if (TrimString(theForm.cod_nazione.value) == "") {
		alert(ln_var_6);
		theForm.cod_nazione.focus();
		return false;
		}
				
	return true;
	
	}

//funzione di controllo form modifica indirizzo di spedizione
function checkCreaIndirizzo(theForm) {
	
	//campo Nome obbligatorio
	if (TrimString(theForm.nome.value) == "") {
		alert(ln_var_1);
		theForm.nome.focus();
		return false;
		}

	//campo cognome obbligatorio
	if (TrimString(theForm.cognome.value) == "") {
		alert(ln_var_2);
		theForm.cognome.focus();
		return false;
		}

	//campo indirizzo obbligatorio
	if (TrimString(theForm.indirizzo.value) == "") {
		alert(ln_var_3);
		theForm.indirizzo.focus();
		return false;
		}

	//campo cap obbligatorio
	if (TrimString(theForm.cap.value) == "") {
		alert(ln_var_4);
		theForm.cap.focus();
		return false;
		}

	//campo città obbligatorio
	if (TrimString(theForm.localita.value) == "") {
		alert(ln_var_5);
		theForm.localita.focus();
		return false;
		}

	//campo nazione obbligatorio
	if (TrimString(theForm.cod_nazione.value) == "") {
		alert(ln_var_6);
		theForm.cod_nazione.focus();
		return false;
		}
				
	return true;
	
	}


//impone l'obbligatorietà per irlanda e gb
function setMandatory(theForm) {
	
//	if ((TrimString(theForm.cod_nazione.value) == "GB") || (TrimString(theForm.cod_nazione.value) == "IE")) {
	if (TrimString(theForm.cod_nazione.value) == "GB") {
		theForm.cod_lingua.value = "ENG";
		theForm.cod_valuta.value = "GBP";
		} else {
		theForm.cod_lingua.value = "ENG";
		theForm.cod_valuta.value = "EUR";
	}
	
	}
	
//impone l'obbligatorietà per irlanda e gb
function setMandatoryByValuta(theForm) {
	
/*	if (TrimString(theForm.cod_valuta.value) == "GBP") {
		if ( (TrimString(theForm.cod_nazione.value) != "GB") || (TrimString(theForm.cod_nazione.value) != "IE") ) theForm.cod_nazione.value = "GB";
	} else {
		if ( (TrimString(theForm.cod_nazione.value) == "GB") || (TrimString(theForm.cod_nazione.value) == "IE")) theForm.cod_nazione.value = "";
		}
*/
	if (TrimString(theForm.cod_valuta.value) == "GBP") {
		if ((TrimString(theForm.cod_nazione.value) != "GB")) theForm.cod_nazione.value = "GB";
	} else {
		if ((TrimString(theForm.cod_nazione.value) == "GB")) theForm.cod_nazione.value = "";
	}

}
