


function verify(a) {

	msg = new String();
	mandatory = a.mandatory.value;
	mandatory = mandatory.split(',');

	tipo = new Array();
	tipo['pi'] = "num";
	tipo['tel'] = "num";
	tipo['fax'] = "num";
	tipo['nome'] = "str";
	tipo['cognome'] = "str";
	tipo['azienda'] = "str";
	tipo['indirizzo'] = "str";
	tipo['citta'] = "str";
	tipo['prov'] = "str";
	tipo['cap'] = "mum";
	tipo['mail'] = "email";
	tipo['password'] = "password";
	tipo['provincia'] = "str";


	campi = new Array();
	campi['pi'] = "Partita IVA";
	campi['tel'] = "Telefono";
	campi['fax'] = "Fax";
	campi['nome'] = "Nome";
	campi['cognome'] = "Cognome";
	campi['azienda'] = "Azienda";
	campi['indirizzo'] = "Indirizzo";
	campi['citta'] = "Cittā";
	campi['prov'] = "Provincia";
	campi['cap'] = "Codice avviamento postale";
	campi['mail'] = "E-mail";
	campi['password'] = "Password";
	campi['provincia'] = "Provincia";

	for(w=0;w<a.length;w++)
	{
		control = a[w].value;
		for(i=0;i<mandatory.length;i++)
		{
			if( a[w].name=="fields["+mandatory[i]+"]" )
			{
				switch (tipo[mandatory[i]])
				{
		   		case "num" :
						if(isNaN(control) || control == "")
						{
							msg = msg.concat("Controlla che il campo ",campi[mandatory[i]]," sia in formato numerico\n");
							set_bg_input(a,mandatory[i]);
						}
		    	break;
		   		case "email":
		 				if(!checkMail(control))
						{
							msg = msg.concat("Controlla che il campo ",campi[mandatory[i]]," sia una email valida\n");
							set_bg_input(a,mandatory[i]);
						}
		   		break;
		   		case "data" :
		   			if(!checkData(control))
						{
							msg = msg.concat("Controlla che il campo ",campi[mandatory[i]]," sia una data valida\n");
							set_bg_input(a,mandatory[i]);
						}
		   		break;
		   		case "password" :
		   			controlb = a['password2'].value;
		   			q = checkPassword(control,controlb);
		   			if( q == 2 )
						{
							msg = msg.concat("Il campo ",campi[mandatory[i]]," deve essere alemeno di 8 caratteri\n");
							set_bg_input(a,mandatory[i]);
						} else if( q == 3 ) {
							msg = msg.concat("Le due password devono coincidere\n");
							set_bg_input(a,mandatory[i]);
						}
		   		break;
		   	}

		  	if( a[w].value==""){
		  		msg = msg.concat("Il campo ",campi[mandatory[i]]," č obbligatorio\n");
		  		set_bg_input(a,mandatory[i]);
	   		}
	  	}
  	}
  	if( a[w].value!=""){
	  	switch (tipo[mandatory[i]])
			{
		   	case "num" :
					if(isNaN(control) || control == "")
					{
						msg = msg.concat("Controlla che il campo ",campi[mandatory[i]]," sia in formato numerico\n");
						set_bg_input(a,mandatory[i]);
					}
		    break;
		   	case "email":
		 			if(!checkMail(control))
					{
						msg = msg.concat("Controlla che il campo ",campi[mandatory[i]]," sia una email valida\n");
						set_bg_input(a,mandatory[i]);
					}
		   	break;
		   	case "data" :
		   		if(!checkData(control))
					{
						msg = msg.concat("Controlla che il campo ",campi[mandatory[i]]," sia una data valida\n");
						set_bg_input(a,mandatory[i]);
					}
		   	break;
		   	case "password" :
		   		controlb = a['password2'].value;
		   		q = checkPassword(control,controlb);
		   		if( q == 2 )
					{
						msg = msg.concat("Il campo ",campi[mandatory[i]]," deve essere alemeno di 8 caratteri\n");
						set_bg_input(a,mandatory[i]);
					} else if( q == 3 ) {
						msg = msg.concat("Le due password devono coincidere\n");
						set_bg_input(a,mandatory[i]);
					}
		   	break;
		  }
  	}
	}

	if(msg != ""){
		alert(msg);
		return(false);
	}
	return(true);
}

function checkData(a) {
	var re = new RegExp("^([0-9]{2})-([0-9]{2})-([0-9]{4})$");
  var dt = a.match(re);
  if (dt == null ||
  		dt[1] < 1 || dt[1] > 31 ||  // giorno non corretto
      dt[2] < 1 || dt[2] > 12 ||  // mese non corretto
      dt[3] < 4)                  // anno non corretto (minore del 2004)
  {
      return(false);
  }
  return(true);
}

function checkMail(a) {
	var re = new RegExp("^(.+)@(\\w+)\\.(\\w+)$");
  var dt = a.match(re);
  if (dt == null)  {
      return(false);
  }
  return(true);
}

function checkPassword(a,b) {
	if(a.length<8){
		return(2);
	}
	if(a != b){
		return(3);
	}
  return(4);
}

function set_bg_input(form,nome) {
	p = document.getElementsByName("fields["+nome+"]");
	p[0].style.background = '#ffcc00';
	p[0].style.color ='#000000';
}
function change_location(selectLocation){

	for(var e=0; e < selectLocation.length; e++ ){
		if(selectLocation[e].selected)
			window.location.href = 'oc_main.php?sid=' + selectLocation[e].value;
	}
}

