function leftTrim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	return sString;
}
function rightTrim(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function checkMail(addr)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(trimAll(addr));
}

function checkCodFisc(cf)
{
   var filter = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
   return filter.test(trimAll(cf));
}

function setFieldFromResponseJS(Fname,requestJS)
{
	if (requestJS.responseJS[Fname] != undefined && trimAll(requestJS.responseJS[Fname]) != '')
	{
		document.modulo[Fname].value = requestJS.responseJS[Fname];
	}
}

function doLoadFromMatricola()
{
	var query = document.getElementById('matricola').value;
	
	if (query != '')
	{
		var req = new JSHttpRequest();
		req.caching = false; // Disabilito CACHING, non so come funziona :-)
		req.open('GET', 'doLoadFromMatricola.php', true);
		req.send({ matricola: query });		
		
		req.onreadystatechange = function() 
		{
			if (req.readyState == 4) 
			{
				setFieldFromResponseJS('nome',req);
				setFieldFromResponseJS('cognome',req);
				setFieldFromResponseJS('facolta',req);
				doLoadCorsoDiStudi();
				
				document.modulo.id_corso.value = req.responseJS.corso_di_studi;
				//setFieldFromResponseJS('corso_di_studi',req);
				setFieldFromResponseJS('telefono',req);
				setFieldFromResponseJS('email',req);
				setFieldFromResponseJS('codice_fiscale',req);
			}
		}
	//	document.modulo.corso_di_studi.value = document.modulo.id_corso.value;
	}	
}


function modulo_studente() 
{
	document.modulo.action = "modulo_studente.php";
	document.modulo.submit();
}



function doLoadProblema() 
{
	var query = document.getElementById('servizio').value;
	
	if (query != 0)
	{
		var req = new JSHttpRequest();
		req.caching = false; // Disabilito CACHING, non so come funziona :-)
		req.open('GET', 'doLoadProblema.php', true);
		req.send({ ids: query });		
		
		req.onreadystatechange = function() 
		{
			if (req.readyState == 4) 
			{
				var selectList = "";
				var remain = req.responseJS.size-1;
				while (remain >= 0) 
				{
					var row = req.responseJS[remain];
					var nominativo = row.nominativo;
					var id_pb = row.id_problema;
					selectList = selectList +  "<option value=" + id_pb + ">" + nominativo + "</option>";
					remain--;
				}
				document.getElementById('div_problema').innerHTML = 
				"<select name=problema id=problema style='width:250pt' onchange='document.modulo.id_problema.value = document.modulo.problema.value; document.modulo.action = \"modulo_studente.php\"; submit(); modulo_studente();'>"
				+ "<option selected value=0> Selezionare un problema </option>"
				+ selectList 
				+ "</select>";
			}
		}
	}
	else
	{
		document.getElementById('div_problema').innerHTML = 'SELEZIONARE UN SERVIZIO PRIMA';
	}
	document.modulo.id_problema.value = '0';
	return '';
}

function doLoadCorsoDiStudi() 
{
	var query = document.getElementById('facolta').value;
	
	if (query != 0)
	{
		var req = new JSHttpRequest();
		req.caching = false; // Disabilito CACHING, non so come funziona :-)
		req.open('GET', 'doLoadCorsoDiStudi.php', true);
		req.send({ idf: query });		
		
		req.onreadystatechange = function() 
		{
			if (req.readyState == 4) 
			{
				var selectList = "";
				var remain = req.responseJS.size-1;
				while (remain >= 0) 
				{
					var row = req.responseJS[remain];
					var nominativo = row.nominativo;
					var id_cr = row.id;
					selectList = selectList +  "<option value=" + id_cr + ">" + nominativo + "</option>";
					remain--;
				}
				document.getElementById('div_corso_di_studi').innerHTML = 
				"<select name=corso_di_studi id=corso_di_studi style='width:250pt' onchange='document.modulo.id_corso.value = document.modulo.corso_di_studi.value;'>" 
				+ "<option selected value=0> Selezionare un corso di studi </option>"
				+ selectList 
				+ "</select>";
			}
		}
	}
	else
	{
		document.getElementById('div_corso_di_studi').innerHTML = 'SELEZIONARE UNA FACOLTA PRIMA';
	}
	document.modulo.id_corso.value = '0';
	return '';
}

function _submit_modulo()
{
	var toCheck = new Array("nome","cognome","matricola");
	var toView = new Array("Nome","Cognome","Matricola");
	
	for (var chk=0; chk<toCheck.length; chk++)
	{
		if (trimAll(document.modulo[toCheck[chk]].value) == '')
		{
			alert("Il campo [" + toView[chk] + "] e' obbligatorio");
			return false;
		}
	}
	
	if (!checkMail(document.modulo.email.value))
	{
		alert("Il campo [E-Mail] e' errato");
		return false;
	}
	
	var cf = document.modulo.codice_fiscale.value;
	cf = cf.toUpperCase();
	document.modulo.codice_fiscale.value = cf;
	
	if (!checkCodFisc(document.modulo.codice_fiscale.value))
	{
		alert("Il campo [Codice Fiscale] e' errato");
		return false;
	}
	
	var toCheck = new Array("facolta","corso_di_studi","servizio","id_problema");
	var toView = new Array("Facolta","Corso di Studio","Servizio","Problema");
	
	for (var chk=0; chk<toCheck.length; chk++)
	{
		if (document.modulo[toCheck[chk]].value == '0')
		{
			alert("Il campo [" + toView[chk] + "] e' obbligatorio");
			return false;
		}
	}
	
	if(trimAll(document.modulo.testo.value) == '')
	{
		alert("Il campo [Descrizione] e' obbligatorio");
		return false;
	}
	
	document.modulo.submit();
}

function resetServizio()
{
	document.modulo.servizio.value = 0;
}

var timeout = null;
function timeSetCorso() 
{
	if (timeout) clearTimeout(timeout);
	timeout = setTimeout(setCorso, 1000);
}

function setCorso()
{
	document.modulo.corso_di_studi.value = document.modulo.id_corso.value;
}