// JavaScript Document

var v = new Array();
v["username"] = 'utente'; // [nome del campo] = 'descrizione che volete compaia all'interno del campo'
v["password"] = 'password';
v["email"] = 'email';
v["SearchedTxt"] = 'cerca nel sito';
//v["SearchedTxt"] = '...';

function InputDel(campo) {
	if (campo.value.search(v[campo.name])== 0 ) {
		campo.value= '';
	}	
}

function InputFill(campo) {
	if (campo.value=='') {
		campo.value= v[campo.name];
	}	
}

function check_login(theForm){
	if (theForm.username.value == "" || theForm.username.value == "utente") {
    	alert("Per favore inserire il proprio nome utente.\nGrazie.");
    	theForm.username.focus();
    	return (false);
	}
	if (theForm.password.value == "" || theForm.password.value == "password") {
    	alert("Per favore inserire la propria password.\nGrazie.");
    	theForm.password.focus();
    	return (false);
	}
}

function check_ricerca(theForm){
	if (theForm.SearchedTxt.value == "cerca nel sito" || theForm.SearchedTxt.value == " ") {
    	alert("E' necessario inserire il testo da ricercare.\nGrazie.");
    	theForm.SearchedTxt.focus();
    	return (false);
	}
}

function check_newsletter(theForm){	
	if (theForm.email.value == "") {
    	alert("Per favore inserire il proprio indirizzo Email, grazie.");
    	theForm.email.focus();
    	return (false);
	}
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theForm.email.value))) {
		alert("Indirizzo E-Mail non valido, si prega di verificarlo.\nGrazie.");
		theForm.email.focus();
		return (false);
	}	
	if (!theForm.privacy.checked) {
	alert("Non e' possibile proseguire senza acconsentire al trattamento dei dati personali, grazie.");
	theForm.privacy.focus();		
	return (false);
	}  	
}

function check_contatti(theForm) {		
	if (theForm.nome.value == "") {
    	alert("Per favore inserire il proprio nome, grazie.");
    	theForm.nome.focus();
    	return (false);
		}
	if (theForm.cognome.value == "") {
    	alert("Per favore inserire il proprio cognome, grazie.");
    	theForm.cognome.focus();
    	return (false);
		}	
	if (theForm.email.value == "") {
    	alert("Per favore inserire il proprio indirizzo Email, grazie.");
    	theForm.email.focus();
    	return (false);
	}
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theForm.email.value))) {
		alert("Indirizzo E-Mail non valido, si prega di verificarlo.\nGrazie.");
		theForm.email.focus();
		return (false);
	}	
	if (theForm.testo.value == "") {
    	alert("Per favore inserire il testo del messaggio, grazie.");
    	theForm.testo.focus();
    	return (false);
		}
	if (theForm.code.value == "") {
    	alert("Per favore inserire il codice di sicurezza, grazie.");
    	theForm.code.focus();
    	return (false);
		}
	if ((theForm.privacy) && theForm.privacy.type == 'checkbox') {
		if (!theForm.privacy.checked) {
		alert("Non è possibile proseguire senza acconsentire al trattamento dei dati personali, grazie.");
		theForm.privacy.focus();		
		return (false);
		}  	
	}
}

function check_commenti(theForm){			
	if (theForm.titolo.value == "") {
    	alert("Per favore inserire titolo del commento, grazie.");
    	theForm.titolo.focus();
    	return (false);
		}			
	if (theForm.autore.value == "") {
    	alert("Per favore inserire il proprio nome, grazie.");
    	theForm.autore.focus();
    	return (false);
		}
	if (theForm.email.value == "") {
    	alert("Per favore inserire il proprio indirizzo Email, grazie.");
    	theForm.email.focus();
    	return (false);
	}
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theForm.email.value))) {
		alert("Indirizzo E-Mail non valido, si prega di verificarlo.\nGrazie.");
		theForm.email.focus();
		return (false);
	}	
	if (theForm.testo_rtf.value == "") {
	alert("Per favore inserire il proprio commento, grazie.");
	theForm.testo_rtf.focus();		
	return (false);
	}  	
}

function check_out(theForm) {
		if (theForm.modalita.selectedIndex == 0) {
    	alert("Per favore scegliere la modalità di pagamento, grazie.");
    	theForm.modalita.focus();
    	return (false);
		}	
	if (theForm.nome.value == "") {
    	alert("Per favore inserire il proprio nome, grazie.");
    	theForm.nome.focus();
    	return (false);
		}
	if (theForm.cognome.value == "") {
    	alert("Per favore inserire il proprio cognome, grazie.");
    	theForm.cognome.focus();
    	return (false);
		}	
	if (theForm.email.value == "") {
    	alert("Per favore inserire il proprio indirizzo Email, grazie.");
    	theForm.email.focus();
    	return (false);
	}
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theForm.email.value))) {
		alert("Indirizzo E-Mail non valido, si prega di verificarlo.\nGrazie.");
		theForm.email.focus();
		return (false);
	}	
	if (theForm.nazione.selectedIndex == 0) {
    	alert("Per favore inserire la nazione, grazie.");
    	theForm.nazione.focus();
    	return (false);
		}
	if (theForm.provincia.selectedIndex == 0 || theForm.provincia.value == "") {
    	alert("Per favore inserire la provincia, grazie.");
    	theForm.provincia.focus();
    	return (false);
		}
	if (theForm.comune.selectedIndex == 0 || theForm.comune.value == "") {
    	alert("Per favore inserire il comune, grazie.");
    	theForm.comune.focus();
    	return (false);
		}
	if (theForm.indirizzo.value == "") {
    	alert("Per favore inserire il proprio indirizzo, grazie.");
    	theForm.indirizzo.focus();
    	return (false);
		}
	if (theForm.cap.value == "") {
    	alert("Per favore inserire il proprio cap, grazie.");
    	theForm.cap.focus();
    	return (false);
		}
	if (!theForm.privacy.checked) {
		alert("Non è possibile proseguire senza acconsentire al trattamento dei dati personali, grazie.");
		theForm.privacy.focus();		
		return (false);
		}  	
}

function PopupPic(sPicURL) { 
// dimensioni iniziali finestra prima che si riadatti
 popwidth = 250;
 popheight = 400;
 // apre il popup e passa la url immagine
 window.open( "/popups/pop_fit.html?"+sPicURL, "", 
 "left=" + Math.floor((screen.width - popwidth) / 2) + ",top=" + Math.floor((screen.height - popheight) / 2) +
 "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width="+popwidth+",height="+popheight); 
}


function URLEncode(s)
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = s;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	
	return encoded;
};



function URLDecode(s)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = s;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   
   return plaintext;
};
