// Acquire GET data
function acquireGETdata()
{
	var querystring=location.search.substr(1); // Toglie il ? del location.search
	var dati = new Array(); // Crea un nuovo array dove spezzettare illocation.search
	dati = querystring.split("&"); // Assegna all'array dati tutte le variabili prese dall'url
	for (n=0; n<dati.length; n++) {
		var_name = dati[n].substr(0,dati[n].indexOf("=")) ;
		var_content = dati[n].substr(dati[n].indexOf("=")+1) ;
		var_set = "GET_"+var_name + " = '" + var_content + "';" ;
		eval(var_set);
	}
}

// Controlla la lunghezza massima di un campo di testo
function checkMaxLength(val,max,divtarget) {
	var maxLength = max;
	var currentLength = val.value.length;
	var text = val.value.slice(0,255);
	if (currentLength > maxLength)
	{
		val.value = text;
		getByID(divtarget).innerHTML = "<b>"+(currentLength-1)+"/"+max+" <span style='color: #900;'>Max caratteri raggiunto, il testo è stato tagliato.</span></b>";
	} else {
		getByID(divtarget).innerHTML = "<b>"+currentLength+"/"+max+"</b>";
	}
}

function cancelValue(id) { getByID(id).value = ""; }

// Redirect
function MM_goToURL(){ //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

// Crea una stringa con tutti i valori del form nella pagina
function preparaDatiUpdate(){
	stringa = "";
  	var form = document.forms[0];
  	var numeroElementi = form.elements.length;
 
  	for(var i = 0; i < numeroElementi; i++){
		if(i < numeroElementi-1){
	   		if (form.elements[i].type == "checkbox"){
				stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].checked)+"&";
			} else {
				stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
			}
		} else {
			if (form.elements[i].type == "checkbox"){
				stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].checked);
			} else {
				stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value);
			}
    	}
  	}
}

// Redirect
function cancelValue(id) { getByID(id).value = ""; }

function MM_goToURL(){ //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

// Funzione per ricavare il valore selezionato in un radio button
function getFromRadio(nome_form,id_elemento) {
	var indice = 0;
	for (i = 0; i < document.forms[nome_form].elements[id_elemento].length; i++) {
		if (document.forms[nome_form].elements[id_elemento][i].checked) indice = i;
	}
	var res = document.forms[nome_form].elements[id_elemento][indice];
	if (res == undefined) { res = document.forms[nome_form].elements[id_elemento]; }
	return res;
}

// Apri popup
function popUpMe(location,width,height,resize) {
	width = width == "" ? width = 600 : width;
	height = height == "" ? height = 400 : height;
	resize = resize == "" ? "no" : "yes";
	LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
    window.open(location,"_blank",'width='+width+', height='+height+', top='+TopPosition+', left='+LeftPosition+', toolbar=no, location=no, directories=no, menubar=no, status=no, resizable='+resize+', scrollbars=yes');
}

//Controllo Email
function checkEmail(email) {
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (!email_reg_exp.test(email) || (email == "") || (email == "undefined")) {
	   alert("Inserire un indirizzo Email corretto.");
	   getByID("email").focus();
	   return false;
	} else {
		return true;
	}
}