function MM_validate() { 
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validate.arguments;
  for (i=0; i<(args.length-3); i+=4) { 
  test=args[i+2];
 message=args[i+3];
 val=MM_findObj(args[i]);
   
  
    if (val) { 
  nm=val.name; 
  if ((val=val.value)!="") {
         if (test.indexOf('isEmail')!=-1) { 
     p=val.indexOf('@');
           if (p<1 || p==(val.length-1)) errors+='- '+message+' deve contenere un indirizzo e-mail valido.\n';
         } else if (test!='R') {
           if (isNaN(val)) errors+='- '+message+' deve contenere un numero.\n';
           if (test.indexOf('inRange') != -1) { 
      p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
              if (val<min || max<val) errors+='- '+message+' deve contenere un numero compreso tra '+min+' e '+max+'.\n';
        } 
    } 
  } else if (test.charAt(0) == 'R') 
   errors += '- '+message+' č un campo obbligatorio.\n'; 
  }
 } 
  if (errors) {
   alert('Si sono verificati i seguenti errori:\n'+errors);
 return false;
  } else {
   return true;
 //document.MM_returnValue = (errors == '');
  }
}

// JavaScript Document
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


/*
	funzione di controllo campi dei form
	Marco Monguzzi - monguz@gmail.com
	versione:	1.01
 	utilizzo:
		- nome campo ( attributo NAME dell'INPUT )
		- etichetta campo
		- verifica
			R			deve essere obbligatorio (precede le altre verifiche)
			isChecked	un INPUT di tipo CHECKBOX deve essere selezionato
			isNum		deve essere un numero
			inRangeX:Y	deve essere un numero compreso tra X e Y
			isEmail		deve essere un'email (ci dev'essere la @)
			equals:xxx	deve essere uguale al campo xxx (NON puņ essere usato con R, fare una verifica separata)	
	esempio:
		<form onSubmit="MM_validateForm('M_NAME','username','R', 'M_PASSWORD','password','R','M_PASSWORD','La password di conferma non e\' corretta.','equals:M_PASSWORD_chk','M_AGE','anno di nascita','isNum','M_EMAIL','email','RisEmail', 'consenso', 'il consenso', 'isChecked');return document.MM_returnValue;">
*/
function MM_validateForm() { //v4.0
	var i,p,q,nm,test,num,min,max,equalsTo,errors='',args=MM_validateForm.arguments;	
	
	for (i=0; i<(args.length-2); i+=3) { 
	//	alert(errors);
		test=args[i+2]; 
		val=MM_findObj(args[i]);
		if (val) { 
			nm=val.name; 
			if (args[i+1] != "") {
				nm = args[i+1];
			}
			//	alert("test: \t" + test + "\nname: \t" + val.name + "\nvalue: \t" + val.value + "\ntype: \t" + val.type);
			if ( ( test.indexOf('isChecked') != -1 ) && ( val.type == "checkbox" ) ) {
				if ( val.checked == false ) {
					errors += '- '+nm+' e\' obbligatorio.\n'; 
				}
			} else if (test.indexOf('equals') != -1){
				p = test.indexOf(':');
				equalsTo = test.substring(p+1);
				//alert ("equalsTo : " + equalsTo + " [" + MM_findObj(equalsTo).value + "]");
				if ( val.value != MM_findObj(equalsTo).value ){
					errors += '- ' + nm + '\n';
				}
			} else {
				if ((val=val.value) != "") {
					if (test.indexOf('isEmail')!=-1) { 
						p=val.indexOf('@');
						if (p<1 || p==(val.length-1)) {
							errors+='- '+nm+' deve essere un indirizzo email valido.\n';
						}
					} else if (test!='R') { 
						num = parseFloat(val);
						if (isNaN(val)) {
							errors+='- '+nm+' deve essere un numero.\n';
						}
						if (test.indexOf('inRange') != -1) { 
							p=test.indexOf(':');
							min=test.substring(8,p); 
							max=test.substring(p+1);
							if (num<min || max<num) {
								errors+='- '+nm+' deve essere un numero compreso tra '+min+' e '+max+'.\n';
							}
						} 
					}
				} else if (test.charAt(0) == 'R') {
					errors += '- '+nm+' e\' obbligatorio.\n'; 
				}
			} 
		}
	} 
	if (errors) {
		alert('Ci sono i seguenti errori:\n'+errors);
	}
	document.MM_returnValue = (errors == '');
}

var req=null;
var console=null;
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;
function sendRequest(url,params,HttpMethod){
	if (!HttpMethod){
		HttpMethod="GET";
	}
	req = initXMLHTTPRequest();
	if (req){
		req.onreadystatechange=onReadyState;
		req.open(HttpMethod,url,true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(params);
	}
}
function initXMLHTTPRequest(){
	var xRequest = null;
	if (window.XMLHttpRequest){			//	mozilla/safari
		xRequest = 	new XMLHttpRequest();	
	} else if (window.ActiveXObject){	//	internet explorer
		xRequest =	new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xRequest;
}
function onReadyState(){
	var ready=req.readyState;
	var data=null;
	if (ready==READY_STATE_COMPLETE){
		data =	req.responseText;
	}else{
		data =	"controllo...";// ["+ready+"]";
	}
	console.innerHTML = data;
}
function checkUtente(utente){
	console = document.getElementById('feedbackUtente');
	sendRequest("/_utenti/registrazione_utente_check.aspx?utente=" + utente);
}
function checkEmail(email){
	console = document.getElementById('feedbackEmail');
	sendRequest("/_utenti/registrazione_email_check.aspx?email=" + email);
}

function apriGuida(pagina) {
	window.open(pagina,'guida','toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizeable=yes,top=50,left=100,copyhistory=no,width=550,height=385');
	return false;
}

