// JavaScript Document

// funzione per riempire gli input con Int 

var v = new Array();
v["username"] = 'Utente'; // [nome del campo] = 'descrizione che volete compaia all'interno del campo'
v["password"] = 'Password';
v["testo_rtf"] = 'Scrivi qui il tuo messaggio è necessario essere registrato';
//v["name"] = 'Inserisci il tuo Nome*';
//v["surname"] = 'Inserisci il tuo Cognome*';
//v["email"] = 'Inserisci la tua Email*';

function InputDel(campo) {
	if (campo.value.search(v[campo.name])== 0 ) {
		campo.value= '';
	}	
}

function InputFill(campo) {
	if (campo.value=='') {
		campo.value= v[campo.name];
	}	
}

// Funzione per controllare l'invio della pagina

function ControlloInvio(theForm) {
	if (theForm.mail.value == "" || theForm.mail.value == "email*") {
    	alert("Per favore inserire la mail del destinatario, grazie.");
    	theForm.mail.focus();
    	return (false);
	}
	if (theForm.mittente.value == "" || theForm.mittente.value == "mittente*") {
    	alert("Per favore inserire il nome del mittente, grazie.");
    	theForm.mittente.focus();
    	return (false);
	}

}


// Funzione per controllare il numero di caratteri nel testo della mail

function CheckChars1(obj) {
  l_max = 250;
  l = obj.value.length;
  obj.form.total_chars1.value = l;
  if (l <= l_max) {
    return true;
  } else {
    window.alert('Attenzione: il testo supera i '+l_max+' caratteri consentiti');
    obj.value=obj.value.substring(0,l_max);
    obj.form.total_chars1.value = l_max;
    return false;
  }
}

function CheckChars2(obj) {
  l_max = 3000;
  l = obj.value.length;
  document.form2.total_chars2.value = l;
  if (l <= l_max) {
    return true;
  } else {
    window.alert('Attenzione: il testo supera i '+l_max+' caratteri consentiti');
    obj.value=obj.value.substring(0,l_max);
    document.form2.total_chars2.value = l_max;
    return false;
  }
}

function CheckChars3(obj) {
  l_max = 400;
  l = obj.value.length;
  document.form2.total_chars2.value = l;
  if (l <= l_max) {
    return true;
  } else {
    window.alert('Attenzione: il testo supera i '+l_max+' caratteri consentiti');
    obj.value=obj.value.substring(0,l_max);
    document.form2.total_chars2.value = l_max;
    return false;
  }
}

function CheckChars4(obj) {
  l_max = 1000;
  l = obj.value.length;
  document.form2.total_chars2.value = l;
  if (l <= l_max) {
    return true;
  } else {
    window.alert('Attenzione: il testo supera i '+l_max+' caratteri consentiti');
    obj.value=obj.value.substring(0,l_max);
    document.form2.total_chars2.value = l_max;
    return false;
  }
}

// funzioni per generare i pdf 

function PopUp(page,w,h,t,s,m) 
	{
	popwidth = w;
	popheight = h;
    if (!t) {t = 'no'};
	if (!s) {s = 'no'};
	if (!m) {m = 'no'};
	OpenWin = this.open(page, "prn", "toolbar=" + t + ", menubar=" + m +", location=no, scrollbars=" + s + ", resizable=no, width="+popwidth+", height="+popheight+", left=" + Math.floor((screen.width - popwidth) / 2) + ", top=" + Math.floor((screen.height - popheight) / 2));
}

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;
};

function ControlloNewsletter(theForm){
 if (theForm.name.value == "") {
 alert("Per favore inserisci il tuo nome.\nGrazie.");
 theForm.name.focus();
 return (false);
 }
 else if(theForm.surname.value == "") {
 alert("Per favore inserisci il tuo cognome.\nGrazie.");
 theForm.surname.focus();
 return (false);
 }
 else if (theForm.email.value == "") {
 alert("Per favore inserisci la tua email.\nGrazie.");
 theForm.email.focus();
 return (false);
 }
 else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theForm.email.value))) {
 alert("Indirizzo E-Mail non valido, si prega di verificarlo");
 theForm.email.focus();
 return (false);
 }
 else if (theForm.privacy.checked == false) {
 alert("Per procedere è necessario acconsentire al trattamento dei dati");
 theForm.privacy.focus();
 return (false);
 }
}

/*
Controllo form Newsletter con javascript che fa scomparire il contenuto dei campi 
function ControlloNewsletter(theForm){
 if (theForm.name.value == "" || theForm.name.value == "Inserisci il tuo Nome*") {
 alert("Per favore inserisci il tuo nome.\nGrazie.");
 theForm.name.focus();
 return (false);
 }
 else if(theForm.surname.value == "" || theForm.surname.value == "Inserisci il tuo Cognome*") {
 alert("Per favore inserisci il tuo cognome.\nGrazie.");
 theForm.surname.focus();
 return (false);
 }
 else if (theForm.email.value == "" || theForm.email.value == "Inserisci la tua Email*") {
 alert("Per favore inserisci la tua email.\nGrazie.");
 theForm.email.focus();
 return (false);
 }
 else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theForm.email.value))) {
 alert("Indirizzo E-Mail non valido, si prega di verificarlo");
 theForm.email.focus();
 return (false);
 }
 else if (theForm.privacy.checked == false) {
 alert("Per procedere è necessario acconsentire al trattamento dei dati");
 theForm.privacy.focus();
 return (false);
 }
}*/


