// JavaScript Document
var	flag = false;

function PopolaAree()
{
	var aree = new Array("Direzione generale","Finanziaria","Acquisti","Amministrativa","Commerciale / Vendite","Logistica","Marketing",
						 "Produzione","Controllo  qualità","Risorse umane","Tecnica / Sistemi informativi","Ricerca e sviluppo");

	elAree = document.forms[0].elements['Area'];
	elFunz = document.forms[0].elements['Ruolo'];
		
	for(i = 0; i< aree.length; i++)
		elAree.options[i + 1] = new Option( aree[i],  aree[i]);
		
	elAree.selectedIndex = 0;
	elFunz.selectedIndex = 0;

	flag = true;
}


function PopolaRuoli()
{
	var funz =  new Array("Presidente","Amministratore delegato/unico","Amministratore","Titolare","Socio amministratore",
						  "Socio accomandatario", "Direttore generale", "Dirigente", "Responsabile", "Impiegato");

	elAree = document.forms[0].elements['Area'];
	elFunz = document.forms[0].elements['Ruolo'];
	confine = 7;
	
	id = elAree.selectedIndex;
	
	j = elFunz.length;
	for(i = 1; i < j; i++)
		elFunz.options[i] = null;
	
	if(id == 0)
	{
		elFunz.options.length = 1;
		return;
	}
	
	if(id == 1)
	{			
		for(i = 0; i < confine; i++)
			elFunz.options[i + 1] = new Option( funz[i], funz[i]);
	}
	else
	{
		for(i = confine; i< funz.length; i++)
			elFunz.options[i - confine + 1] = new Option(funz[i], funz[i]);
	}
}


function TestRuoli()
{
	elAree = document.forms[0].elements['Area'];
	
	if(flag)
	{
		if(elAree.selectedIndex == 0)
		{
			alert("Selezionare prima l'area, grazie.");
		}
	}
	else
		flag = true;
}


function trim(s) 
{
	while (s.substring(0,1) == ' ')
	{
		s = s.substring(1, s.length);
	}
	while (s.substring(s.length-1, s.length) == ' ')
	{
		s = s.substring(0,s.length-1);
	}
	return s;
}


function Verifica()
{
	var err = "";
	var	txt1 = "I seguenti dati sono obbligatori:";
	var	txt2 = "L'e-mail non è corretta";
	var	txt3 = "caratteri";

	if(document.forms[0].elements['LINGUA'] != null)
	{
		if(document.forms[0].elements['LINGUA'].value != "")
		{
			lingua = document.forms[0].elements['LINGUA'].value;
			
			if(lingua == "inglese")
			{
				txt1 = "The following fields are required:";
				txt2 = "E-mail is wrong";
				txt3 = "characters";
			}
		}
	}	
		

	nEls = document.forms[0].elements.length;
	
	//	Questi campi sono obbligatori
	for(i = 0; i < nEls; i++)
	{
		tag = document.forms[0].elements[i];
		
		if(tag.value.length > 400)
		{
			err += tag.name + " - max 400 " + txt3 + "\r\n";
			continue;
		}

		if(tag.name.charAt(0) == "_")
			continue;
			
		if(tag.name == "XID")
			continue;
			
		if(tag.type == "checkbox")
			continue;
			
		if(tag.type == "radio")
		{
			for(j = i+1; j < nEls; j++)
			{
				if(document.forms[0].elements[j].name == tag.name)
					break;
			}
			
			if(j == nEls)
			{
				for(j = 0; j < nEls; j++)
				{
					if(document.forms[0].elements[j].name == tag.name && document.forms[0].elements[j].checked == true)
						break;
				}
				
				if(j == nEls)
					err += tag.name + "\r\n";
			}
			
			continue;
		}

		if(tag.name == "Email" && trim(tag.value) != "")
		{
			var mod_ema = /^([a-z0-9&_\-]+\.?)+@([a-z0-9\-]+\.)+[a-z]{2,4}$/;
			
			tag.value = trim(tag.value);
			tag.value = tag.value.toLowerCase();

			if (tag.value.match(mod_ema) == null)
				err += txt2 + "\r\n";
				
			continue;
		}
		
		if(trim(tag.value) == "")
			err += tag.name + "\r\n";
	}

	
	if(err != "")
	{
		err = txt1 + "\r\n\r\n" + err;
		alert(err);
	}
	else
	{
		for(i = 0; i < nEls; i++)
		{
			var tag = document.forms[0].elements[i];
			
			if(tag.type == "checkbox" && tag.checked == false)
			{ 
				var el = document.createElement("INPUT");
				el.type = "hidden";
				el.name = tag.name;
				el.value = "NO";
								
				var n = document.getElementById(tag.name).parentNode;

				for(j = 0; j < n.childNodes.length; j++)
				{
					if(n.childNodes[j].name == tag.name && n.childNodes[j].type == "checkbox")
					{
						n.insertBefore(el,n.childNodes[j]);
						break;
					}
				}
				
				i ++;
				nEls ++;
			}
		}
	}

	document.esito = (err == "");
}



