function Trim(str)
{
	if (str!=undefined) return str.replace(/\s/g, "");
	else return str;
}

//Controlla il form e fa la submit
function CheckForm()
{
	//la form
	var strError="ATTENZIONE\n";
	var Error=false
	FRemail = document.forms("Remail");
	
	if(!CheckOblEsp() || !CheckObl()) 
	{
		alert("Controllare i campi obbligatori");
		return;
	}
	
	//Controllo l'estensione dell'allegato
	if (FRemail.AllegaCV!=null)
	{
		fileName=FRemail.AllegaCV.value;
		if (fileName!="")
		{
			if (fileName.length>4)
			{
				ext=fileName.substr(fileName.length-4);
				ext.toLowerCase( );
				if (ext!=".doc" && ext!=".rtf" && ext!=".pdf")
				{
					strError=strError+"Curriculum allegato solo in formato doc, rtf o pdf\n";
					Error=true;
				}
			}
			else
			{
				strError=strError+"Nome file curriculum non corretto\n";
				Error=true;
			}
		}
	}
	
	//Controllo data di nascita
	if (FRemail.DataNascita!=null)
	{
		data=FRemail.DataNascita.value;
		if (data!="" && !isDate(data))		
		{
			strError=strError+"Data di nascita non corretta\n";
			Error=true;
		}
	}
	
	//Controllo CAP residenza
	if (FRemail.ResidenzaCap!=null)
	{
		cap=FRemail.ResidenzaCap.value;
		if (cap!="" && !isInteger(cap))
		{
			strError=strError+"CAP residenza non corretto\n";
			Error=true;
		}
	}
	
	//Controllo CAP domicilio
	if (FRemail.DomicilioCap!=null)
	{
		cap=FRemail.DomicilioCap.value;
		if (cap!="" && !isInteger(cap))
		{
			strError=strError+"CAP domicilio non corretto\n";
			Error=true;
		}	
	}
	
	//Controllo E-mail
	if (FRemail.Email!=null)
	{
		email=FRemail.Email.value;
		if (email!="" && !isMail(FRemail.Email))
		{
			strError=strError+"Indirizzo e-mail non corretto\n";
			Error=true;
		}	
	}
	
	//Controllo percentuale invalidita
	if (FRemail.PercentualeInvalidita!=null)
	{
		perc=FRemail.PercentualeInvalidita.value;
		if (perc!="" && !isInteger(perc))
		{
			strError=strError+"Percentuale d'invaliditā non valida\n";
			Error=true;
		}
		if (perc>100)
		{
			strError=strError+"Percentuale d'invaliditā maggiore di 100\n";
			Error=true;
		}
	}
	
	//Controllo lunghezze campi blob
	if (FRemail.Corsi!=null)
		if(FRemail.Corsi.value.length>Corsi)
		{
			strError=strError+"Corsi frequentati eccede la lunghezza limite\n";
			Error=true;
		}

	if (FRemail.ConoscenzeInfo!=null)
		if(FRemail.ConoscenzeInfo.value.length>ConoscenzeInfo)
		{
			strError=strError+"Conoscenze informatiche eccede la lunghezza limite\n";
			Error=true;
		}

	if (FRemail.Note!=null)
		if(FRemail.Note.value.length>Note)
		{
			strError=strError+"Le note eccedono la lunghezza limite\n";
			Error=true;
		}

	i=1;
	while (eval('FRemail.NEJobDescription$'+i)!=null)
	{
		if(eval('FRemail.NEJobDescription$'+i+'.value.length')>EsperienzeLavoroJobDescription)
		{
			strError=strError+"La descrizione dell'esperienza lavorativa "+i+" eccede la lunghezza limite\n";
			Error=true;
		}
		i++;
	}

	i=1;
	while (eval('FRemail.NEDal$'+i)!=null)
	{
	data=eval('FRemail.NEDal$'+i+'.value')
	mm=data.substr(0,2);
	dd=data.substr(data.length-4);
	sp=data.substr(2,1);
	
		if (data!="")
		{
		if ((parseInt(mm)>12) || (sp != "/") || (parseInt(dd)>2010) || (parseInt(dd)<1940))
		{
			strError=strError+"Il formato della data inizio dell'esp. lav. "+i+" non č corretto";
			Error=true;
		}
		}
		i++;
	}
	
	i=1;
	while (eval('FRemail.NEAl$'+i)!=null)
	{
	data=eval('FRemail.NEAl$'+i+'.value')
	mm=data.substr(0,2);
	dd=data.substr(data.length-4);
	sp=data.substr(2,1);
	
		if (data!="")
		{
		if ((parseInt(mm)>12) || (sp != "/") || (parseInt(dd)>2010) || (parseInt(dd)<1940))
		{
			strError=strError+"Il formato della data fine dell'esp. lav. "+i+" non č corretto";
			Error=true;
		}
		}
		i++;
	}
		
	i=1;
	while (eval('FRemail.NEMotiviCessazione$'+i)!=null)
	{
		if(eval('FRemail.NEMotiviCessazione$'+i+'.value.length')>EsperienzeLavoroMotiviCessazione)
		{
			strError=strError+"I motivi di cessazione dell'esperienza lavorativa "+i+" eccedono la lunghezza limite\n";
			Error=true;
		}
		i++;
	}

	i=1
	while (eval('FRemail.NEIDAzienda$'+i)!=null)
	{
		if(eval('FRemail.NEAzienda$'+i+'.value.length')==0)
			 eval('FRemail.NEIDAzienda$'+i+'.value=-1');
		else
			 eval('FRemail.NEIDAzienda$'+i+'.value=0');
		i++;
	}

	i=1
	while (eval('FRemail.NEAttuale$'+i)!=null)
	{
		if(eval('FRemail.NEAttuale$'+i+'.checked')==false)
		{
			eval("FRemail.NEAttuale$"+i+".value='FALSE'")
			eval('FRemail.NEAttuale$'+i+'.checked=true')
		}
		i++;
	}

	
	if (Error)
	{
		alert (strError);
		return;
	}
	
	//Attivo l'idannedizdata
	if (FRemail.IDAnnEdizData!=null) FRemail.IDAnnEdizData.disabled=false;

	FRemail.submit();
}

function CheckOblEsp()
{
	var i=0,k,found=false;
	while (i<Obl.length && found!=true)
	{
		if(Obl[i].indexOf("$")>0)
		{
				arr=Obl[i].split("$");
				if (arr[0]=='EsperienzeLavoro' && arr[1]!="Attuale" && arr[1]!="Azienda" && arr[1]!="Periodo")
				{
					k=1;
					
					//Controllo il valore di nome azienda per ogni esperienza di lavoro
					while (eval('document.forms(0).NEAzienda$'+k)!=null)
					{
					
						if (eval('document.forms(0).NEAzienda$'+k+'.value')!='')
						{
							// Controllo che l'obbligatorio in Obl[i] ci sia altrimenti errore
							//if ((eval('document.forms(0).NEDal$'+k+'.value')=='') || (eval('document.forms(0).NEAl$'+k+'.value')=='')) found = true;
							if (eval('document.forms(0).NE'+arr[1]+'$'+k)!=null)
							{
								if (eval('document.forms(0).NE'+arr[1]+'$'+k+'.value')=='')
								{	
									if (arr[0]=='EsperienzeLavoro' && arr[1]=="Periodo")//if (arr[1]=='Periodo') 
									{
										if ((eval('document.forms(0).NEDal$'+k+'.value')=='') || (eval('document.forms(0).NEAl$'+k+'.value')=='')) found = true;
									}
									else
									{
										found=true;
									}
								}
							}
							else
							{
								if (eval('document.forms(0).NEID'+arr[1]+'$'+k+'.value')=='-1')
								{
									if (arr[1]=='Ruolo')
									{
										if (eval('document.forms(0).NEMansione$'+k+'.value')=='')  found=true;
									}
									else
									{
										found=true;
									}
								}
								//
								//
							}
						}
					k++;
					}
				}
				if (arr[0]=='EsperienzeLavoro' && arr[1]=="Azienda")
				{
					if (document.forms(0).NEAzienda$1.value=='') found=true;
				}
				if (arr[0]=='EsperienzeLavoro' && arr[1]=="Periodo")
				{
					if ((eval('document.forms(0).NEDal$'+i+'.value')=='') || (eval('document.forms(0).NEAl$'+i+'.value')=='')) found = true;
				}
				
		}
	i++;
	}
	return !found;
}

function CheckObl()
{
	var i=0,found=false;
	while (i<Obl.length && found!=true)
	{
		if(Obl[i].indexOf("$")>0)
		{
				//Campi innestati
				arr=Obl[i].split("$");	
				if (arr[0]=='Accademici')
					if (!CheckOne("NEIDDiploma$")) found=true;
				if (arr[0]=='LingueConosciute')
					if (!CheckOne("NEIDLingua$")) found=true;
				if (arr[0]=='EsperienzeLavoro' && arr[1]=='Attuale')
					if (!CheckOne("NEAttuale$")) found=true;
		}
		else
		{
				if (Obl[i]=='DisponibilitaContratti')
				{
					if (!CheckOne("NECHContratto$")) found=true;
				}
				else
				{
					if (Trim(eval('document.forms(0).'+Obl[i]+'.value'))=='' || eval('document.forms(0).'+Obl[i]+'.value')=='-1') found=true;
				}
		}	
		i++;	
	}
	return !found;
}

function CheckOne(str)
{
	var i=1,found=false;
	FRemail = document.forms("Remail");
	//alert(str+i+" "+eval('FRemail.'+str+i+'.value'));
	while (eval('FRemail.'+str+i)!=null && found!=true)
	{
		if (str=="NEIDLingua$") // controllo anche il livello associato
		{
			if (eval('FRemail.'+str+i+'.value')!='-1' && eval('FRemail.NEIDLivello$'+i+'.value')!='-1') found=true;
		}
		else
			if (str=="NEAttuale$" || str=="NECHContratto$") // controllo che sia checked
			{
				if (eval('FRemail.'+str+i+'.checked')==true) found=true;
			}
			else
			{
				if (eval('FRemail.'+str+i+'.value')!='-1') found=true;
			}
		
		i++;
	}
	if (found) return true;
}

function CheckRetrib(obj)
{
	if(!isInteger(obj.value)) 
	{
		alert("Solo numeri interi!");
		obj.value="";
	}
}

//Attiva o disattiva la % di invaliditā
function Invalidita(value)
{
	t=document.forms("Remail").PercentualeInvalidita;
	if(value=="INVALIDO") t.disabled=false;
	else
	{
		t.disabled=true;
		t.value="";
	}
}

//Riempie i ruoli in base all'area
function Fill(control,id,arr) 
{
	c=document.getElementsByName(control).item(0);	
	if (c==null) return;
	c.options.length=0;
	N=arr.length;
	c.options[0]=new Option("(non specificato)");
	c.options[0].value=-1;
	j=1;
	if (id!="-1")
	{
		
		for (i=0;i<N;i++) {
			if (arr[i][2] == id) {
				c.options[j]=new Option(arr[i][1]);
				c.options[j].value=arr[i][0];
				j++;
			}
		}
	c.options[j]=new Option('Altro');
	c.options[j].value=0;
	}
}

//Ordina i cambi degli annunci
function FillAnnunci(control)
{
	c=document.getElementsByName("IDAnnEdizData").item(0);	
	N=c.options.length;
	temp=new Array;
	for (i=0;i<N;i++)
	{
		temp[i] = new Array;
		temp[i][0]=c.options[i].value;
		temp[i][1]=c.options[i].innerText;
	}
	/*c.options.length=0;
	N=arr.length;
	c.options[0]=new Option("(non specificato)");
	c.options[0].value=-1;
	for (i=0;i<N;i++)
	{
		c.options[i]=new Option (

	alert (c);*/
}

//Mostra o nasconde le sezioni di una tabella
function Display(table)
{
	id=document.getElementById(table).childNodes;
	// Riga della testata della tabella
	// head=id.item(0).children(0);
	// Seconda riga
	i=1;
	field=id.item(0).children(i);
	while (field!=null)
	{
		MyStyle=field.style;
		if(MyStyle.display=="none" || MyStyle.display=="") 	
		{
			MyStyle.display="block";
		}
		else
		{ 
			MyStyle.display="none";
		}
		i++;
		field=id.item(0).children(i);
	}
}

//Mostra o nasconde le esperienze lavorative
//i č il numero dell'esperienza, dis se true visualizza eltrimenti elimina
function DisplayEsp(i,dis)
{
	c=document.getElementById("esp"+i);
	j=i-1;
	tablebody=document.getElementById("esp"+j).childNodes.item(0);
	link=tablebody.lastChild.childNodes.item(0);
	if (c==null) 
		alert("Spiacente non č possibile inserire altre esperienze lavorative");
	else
	{
		if (dis) 
		{
			eval("document.forms('Remail').Espe"+i+".value=1");
			c.style.display="block";
			if (j==1) 
				link.innerHTML="";
			else
				link.innerHTML="<FONT SIZE='-2'><A HREF='javascript:DisplayEsp("+j+",false)'>Elimina questa esperienza</A></FONT>"
		}
		else 
		{
			eval("document.forms('Remail').Espe"+i+".value=0");
			if (i>1)
			{
				//Cancello tutti i valori dell'esperienza lavorativa
				Esp = document.getElementById("Esp"+i).childNodes(0);
				l=1;
				while(l!=Esp.childNodes.length-1)
				{	
					if(Esp.childNodes(l).childNodes.length)
					if(Esp.childNodes(l).childNodes(1).childNodes(0).value==undefined)
					{
						
						if(l!=1)
							if(Esp.childNodes(l).childNodes(1).childNodes(0).childNodes(0).childNodes(0).childNodes.length>=2)
							{
								Esp.childNodes(l).childNodes(1).childNodes(0).childNodes(0).childNodes(0).childNodes(2).childNodes(0).value="";
								Esp.childNodes(l).childNodes(1).childNodes(0).childNodes(0).childNodes(0).childNodes(2).childNodes(0).checked=false;
							}
						Esp.childNodes(l).childNodes(1).childNodes(0).childNodes(0).childNodes(0).childNodes(0).childNodes(0).value="";
						
					}
					else 
					{
						Esp.childNodes(l).childNodes(1).childNodes(0).value="";
						Esp.childNodes(l).childNodes(1).childNodes(0).checked=false;
					}
					
					l++;
				}
			}		
			c.style.display="none";
			if (j==1)
				link.innerHTML="<FONT SIZE='-2'><A HREF='javascript:DisplayEsp("+i+",true)'>Aggiungi nuova esperienza</A></FONT>"
			else
				link.innerHTML="<FONT SIZE='-2'><A HREF='javascript:DisplayEsp("+i+",true)'>Aggiungi nuova esperienza</A>&nbsp&nbsp&nbsp<A HREF='javascript:DisplayEsp("+j+",false)'>Elimina questa esperienza</A></FONT>"												
		}
		
	}
}

// Controlla se il valore passato č un indirizzo email
function isMail (addr)
{   
  var fail = false;
  var atpos = addr.value.indexOf('@',0);
  if( (addr.value.length < 5) || (atpos == -1)  )
       fail = true;              // too short or no '@'
  if (! fail)
       { if (addr.value.indexOf('.',atpos) == -1)
            fail = true;          // no '.' in address after the @
       };
  return (!fail);
}


// Controllo della data
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1920;
var maxYear=2099;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}

//Seleziona gli elementi nel form in base all'id inviato
//Esegue un confronto e seleziona l'elemento nel combo corretto
//Se l'id č -2 attiva la proprietā checked di un checkbox
function SelectItem(id,campo)
{
	FRemail = document.forms("Remail");
	obj = eval("FRemail."+campo);
	if (id==-2)
	{
		obj.checked=true;
		return
	}	
	N=obj.options.length;
	for(i=0;i<N;i++)
		if (obj.options[i].value==id) obj.options[i].selected=true;
	
}