// 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*';
v["SearchedTxt"] = 'Inserisci qui ciò che cerchi';

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 CheckChars(obj,l_max,t_obj) {
  l = obj.value.length;
  t_obj.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);
    t_obj.value = l_max;
    return false;
  }
}
function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

// 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 == "" || 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);
 }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
};

function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
};


/* copy to clipboard */

function copyText(theSel) {
if (!document.all) return; // IE only
theForm = theSel.form;
theForm.copyArea.value=theSel.options[theSel.selectedIndex].value;
r=theForm.copyArea.createTextRange();
r.select();
r.execCommand('copy');
} 

function setReferer(theform){
  if (theform.key.selectedIndex == 0){
    alert("Per favore selezionare la natura della proposta, grazie.");
    theform.key.focus();
    return false;
  }
  if (theform.titolo.value == ''){
    alert("Per favore dare un titolo della proposta, grazie.");
    theform.titolo.focus();
    return false;
  }  
  if (theform.testo_rtf.value == ''){
    alert("Per favore inserisci la tua proposta, grazie.");
    theform.testo_rtf.focus();
    return false;
  }   
  
  if (theform.key.selectedIndex == 1) {
    theform.referer.value = '/adon.pl?act=src&tmpl=referer&preferenza=1&sid=15';
    return true;
  }
  if (theform.key.selectedIndex == 2) {
    theform.referer.value = '/adon.pl?act=src&tmpl=referer&preferenza=2&sid=16';
    return true;
  }
  if (theform.key.selectedIndex == 3) {
    theform.referer.value = '/adon.pl?act=src&tmpl=referer&preferenza=3&sid=17';
    return true;
  }
}


