/******************
 SCHEDA PRODOTTO
*****************/
function disclaimer(image){
	zommFin=window.open("disclaimer.html" , "popup", "left=100, top=80, width=500, height=370, scrollbars=0, status=0, resizable=0, directories=0, toolbars=0, menubar=0");
	zommFin.focus();
}



function ricaricaSchedaProdotto(){
	document.schedaProdotto.submit();
}
function addCart(id){
	thisForm=document.schedaProdotto;

	if(thisForm.quantita.selectedIndex == "0"){
		switch(id){
			case "1":
				alert("Non e' possibile ordinare l'oggetto");
				thisForm.quantita.focus();
				return;
			break;
			case "2":
				alert("Not possible to add cart");
				thisForm.quantita.focus();
				return;
			break;
		}
	}
	thisForm.action='_class/classOperazioniCarrello.php';
	thisForm.submit();
}
function svuotaCarrello(){
	thisForm=document.carrello;
	thisForm.azione.value='emptyCart';
	thisForm.action='_class/classOperazioniCarrello.php';
	thisForm.submit();
}
function ricalcolaCarrello(cont){
	thisForm=document.carrello;
	thisForm.azione.value='updateCart';
	thisForm.cont.value=cont;
	thisForm.action='_class/classOperazioniCarrello.php';
	thisForm.submit();
}
function eliminaArticolo(cont){
	if(confirm("Sei sicuro di voler rimuovere l'articolo?")){
		thisForm=document.carrello;
		thisForm.azione.value='delCart';
		thisForm.cont.value=cont;
		thisForm.action='_class/classOperazioniCarrello.php';
		thisForm.submit();
	}
	else return;
}

/******************
 ERRORI
*****************/
function quantError(lang, res){
	switch(lang){
		case "1":
			alert("Non e' possibile ordinare il numero di articoli richiesto.\nSono rimasti "+ res + " articoli disponibili.");
			return;
		break;
		case "2":
			alert("Not possible to update cart. Too objects.\nThere are "+ res + " available.");
			return;
		break;
	}
}
/******************
 RICERCA
*****************/
function submitRicerca(lang){

	thisForm = document.ricerca;
	valido=false;

	for(var i=0; i<thisForm.elements.length; i++){
		campoType=thisForm.elements[i].type;
		switch (campoType){
			case "select-one":
				if(thisForm.elements[i].selectedIndex != "0") valido=true;
			break;
			case "text":
				if( (thisForm.elements[i].value != "") && (thisForm.elements[i].value != " ") ) valido=true;
			break;
		}
	}
	
	if(!valido){
		switch(lang){
			case "1":
				alert("Effettuare una selezione valida");
			break;
			case "2":
				alert("Please make a valid selection");
			break;
		}
		return;
	}
	else {
		thisForm.start.value="0";
		thisForm.submit();
	}
}
/******************
 REGISTRATI
*****************/
function controlloRegistrati(lang){

	thisForm = document.registrati;
	
	if(thisForm.nome.value==''){
		apriAlert(lang, "nome");
		return;
	}
		
	/*if(thisForm.cognome.value==''){
		apriAlert(lang, "cognome");
		return;
	}
	if(thisForm.indirizzo.value==''){
		apriAlert(lang, "indirizzo");
		return;
	}
	if(thisForm.citta.value==''){
		apriAlert(lang, "citta");
		return;
	}
	if(thisForm.provincia.value==''){
		apriAlert(lang, "provincia");
		return;
	}
	
	//if(!controllaData(thisForm.gg.value, thisForm.mm.value, thisForm.aaaa.value, lang)) return;
	
	if(thisForm.nazione.value==''){
		apriAlert(lang, "nazione");
		return;
	}
	if(thisForm.telefono.value==''){
		apriAlert(lang, "telefono");
		return;
	}
	if(!checkEmailAddress(thisForm.email, lang)) return;*/
	
	if(thisForm.pass1.value==''){
		apriAlert(lang, "password");
		return;
	}
	if(thisForm.pass2.value==''){
		apriAlert(lang, "password");
		return;
	}
	
	
	
	password=thisForm.pass1.value;
	if(password.length<6){
		apriAlert(lang, "lettere");
		return;
	}
	if(thisForm.pass2.value!=thisForm.pass1.value){
		apriAlert(lang, "control");
		return;
	}
	thisForm.action="_class/classAzioni.php";
	thisForm.submit();
	
}

function richiediPassword(lang){

	thisForm = document.richiediPassword;
	
	if(!checkEmailAddress(thisForm.email, lang)) return;
	
	thisForm.action="_class/classAzioni.php";
	thisForm.submit();
	
}

function makeLogin(lang){
	
	thisForm = document.login;
	
	if(!checkEmailAddress(thisForm.user, lang)) return;
	
	if(thisForm.pass.value==''){
		apriAlert(lang, "password");
		return;
	}
	
	thisForm.azione.value='makeLogin';
	thisForm.action="_class/classAzioni.php";
	thisForm.submit();
}

/******************
 ORDINE
*****************/
function controlloOrdine(lang){

	thisForm = document.datiOrdine;
	
	if(thisForm.nome.value==''){
		apriAlert(lang, "nome");
		return;
	}
		
	if(thisForm.cognome.value==''){
		apriAlert(lang, "cognome");
		return;
	}
	if(thisForm.indirizzo.value==''){
		apriAlert(lang, "indirizzo");
		return;
	}
	if(thisForm.citta.value==''){
		apriAlert(lang, "citta");
		return;
	}
	if(thisForm.provincia.value==''){
		apriAlert(lang, "provincia");
		return;
	}
	
	//if(!controllaData(thisForm.gg.value, thisForm.mm.value, thisForm.aaaa.value, lang)) return;
	
	if(thisForm.nazione.value==''){
		apriAlert(lang, "nazione");
		return;
	}
	if(thisForm.telefono.value==''){
		apriAlert(lang, "telefono");
		return;
	}
	if(!checkEmailAddress(thisForm.email, lang)) return;
	
	thisForm.action="ordineConfirmDati.php";
	thisForm.submit();
	
}

function riepilogoOrdine(){
	thisForm=document.riepilogoOrdine;
	
	thisForm.action="_class/classAzioni.php";
	thisForm.submit();
	
}

/******************
 CONTATTACI
*****************/
function controlloContattaci(lang){

	thisForm = document.contatto;
	
	if(thisForm.nome.value==''){
		apriAlert(lang, "nome");
		return;
	}
		
	/*if(thisForm.cognome.value==''){
		apriAlert(lang, "cognome");
		return;
	}
	if(thisForm.indirizzo.value==''){
		apriAlert(lang, "indirizzo");
		return;
	}
	if(thisForm.citta.value==''){
		apriAlert(lang, "citta");
		return;
	}
	if(thisForm.provincia.value==''){
		apriAlert(lang, "provincia");
		return;
	}
	
	//if(!controllaData(thisForm.gg.value, thisForm.mm.value, thisForm.aaaa.value, lang)) return;
	
	
	if(!checkEmailAddress(thisForm.email, lang)) return;*/
	
	/*if(thisForm.accetto.checked==false){
		apriAlert(lang, "privacy");
		return;
	}*/
	
	thisForm.action="_class/classContattaci.php";
	thisForm.submit();
	
}


/******************
 FUNZIONI GENERALI
*****************/

function apriAlert(lang, nome){
	switch(lang){
		case "1":
			switch (nome){
				case "nome":
					alert("Riemprire il campo NOME");
				break;
				case "cognome":
					alert("Riemprire il campo COGNOME");
				break;
				case "indirizzo":
					alert("Riemprire il campo INDIRIZZO");
				break;
				case "citta":
					alert("Riemprire il campo CITTA'");
				break;
				case "provincia":
					alert("Riemprire il campo PROVINCIA");
				break;
				case "nazione":
					alert("Riemprire il campo NAZIONE");
				break;
				case "telefono":
					alert("Riemprire il campo TELEFONO");
				break;
				case "password":
					alert("Riemprire il campo PASSWORD");
				break;
				case "control":
					alert("Le PASSWORD non corrispondono");
				break;
				case "lettere":
					alert("La PASSWORD deve essere almeno di 6 caratteri");
				break;
				case "privacy":
					alert("Accettare le condizioni della PRIVACY");
				break;
			}
			
		break;
		case "2":
			switch (nome){
				case "nome":
					alert("Fill field NAME");
				break;
				case "cognome":
					alert("Fill field LAST NAME");
				break;
				case "indirizzo":
					alert("Fill field ADDRESS");
				break;
				case "citta":
					alert("Fill field CITY");
				break;
				case "provincia":
					alert("Fill field PROVINCE");
				break;
				case "nazione":
					alert("Fill field COUNTRY");
				break;
				case "telefono":
					alert("Fill field PHONE");
				break;
				case "password":
					alert("Fill field PASSWORD");
				break;
				case "control":
					alert("PASSWORD mismatch");
				break;
				case "lettere":
					alert("The PASSWORD must be almost 6 chars");
				break;
				case "privacy":
					alert("Accept PRIVACY legal notice");
				break;
			}
			alert();
		break;
	}
	return;
}

var indirizzoEmail;
function checkEmailAddress(field, lang) {

     var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
     
     if (goodEmail){
        indirizzoEmail = true
     } 
     else {
	 	if(lang=="1")
	        alert('Inserire un indirizzo EMAIL valido');
		else if(lang=="2")
			alert('Insert a valid EMAIL');
        field.focus();
        field.select();
        indirizzoEmail = false;
        }
  	return indirizzoEmail;
}

function controllaData(myDayStr, myMonthStr, myYearStr, lang){
	var myDayStr = document.CheckDate.formDate.value;
	var myMonthStr = document.CheckDate.formMonth.value;
	var myYearStr = document.CheckDate.formYear.value;
	var myDateStr = myDayStr + ' ' + myMonthStr + ' ' + myYearStr;
	
	/* Using form values, create a new date object
	which looks like "Wed Jan 1 00:00:00 EST 1975". */
	var myDate = new Date( myDateStr );
	
	// Convert the date to a string so we can parse it.
	var myDate_string = myDate.toGMTString();
	
	/* Split the string at every space and put the values into an array so,
	using the previous example, the first element in the array is "Wed", the
	second element is "Jan", the third element is "1", etc. */
	var myDate_array = myDate_string.split( ' ' );
	
	/* If we entered "Feb 31, 1975" in the form, the "new Date()" function
	converts the value to "Mar 3, 1975". Therefore, we compare the month
	in the array with the month we entered into the form. If they match,
	then the date is valid, otherwise, the date is NOT valid. */
	if ( myDate_array[2] != myMonthStr ) {
		switch (lang){
			case "1":
				alert( "Inserire una data valida" );
			break;
			case "2":
				alert( myDateStr + '" is NOT a valid date.' );
			break;
		}
	  	return false;
	} 
	else return true;
}
