function codiceFISCALE(cfins)
   {
   var cf = cfins.toUpperCase();
   var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
   if (!cfReg.test(cf))
      return false;
   var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
   var s = 0;
   for( i = 1; i <= 13; i += 2 )
      s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
   for( i = 0; i <= 14; i += 2 )
      s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
   if ( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
      return false;
   return true;
   }
   
function isEmail(string) {
if (string.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
return 1;
else
return 0;
}

/* controlla FORM */
function CheckForm(tipo){
if (tipo == 1){
	if(document.step1.nome.value == ""){
		alert("Compilare i campi obbligatori!");
		document.step1.nome.focus();
		return false;
    }
	if(document.step1.cognome.value == ""){
		alert("Compilare i campi obbligatori!");
		document.step1.cognome.focus();
		return false;
    }
	if((document.step1.nazione[0].checked == false) && (document.step1.nazione[1].checked == false)){
		alert("Inserire NAZIONALITA'!")
		return false;
	} else if (document.step1.nazione[0].checked == true){
		if((document.step1.data.value == "") || (document.step1.data.value == "gg/mm/aaaa")){
			alert("Compilare DATA!");
			document.step1.data.focus();
			return false;
		} else {
			if (document.step1.data.value.substring(2,3) != "/" ||
			   document.step1.data.value.substring(5,6) != "/" ||
			   isNaN(document.step1.data.value.substring(0,2)) ||
			   isNaN(document.step1.data.value.substring(3,5)) ||
			   isNaN(document.step1.data.value.substring(6,10))) {
				  alert("Inserire nascita in formato gg/mm/aaaa");
				  document.step1.data.value = "";
				  document.step1.data.focus();
				  return false;
			} else if (document.step1.data.value.substring(0,2) > 31) {
			   alert("Impossibile utilizzare un valore superiore a 31 per i giorni");
			   document.step1.data.select();
			   return false;
			} else if (document.step1.data.value.substring(3,5) > 12) {
			   alert("Impossibile utilizzare un valore superiore a 12 per i mesi");
			   document.step1.data.value = "";
			   document.step1.data.focus();
			   return false;
			} else if (document.step1.data.value.substring(6,10) < 1900) {
			   alert("Impossibile utilizzare un valore inferiore a 1900 per l'anno");
			   document.step1.data.value = "";
			   document.step1.data.focus();
			   return false;
			} else if (document.step1.data.value.substring(6,10) > 2009) {
			   alert("Impossibile utilizzare un valore superiore al 2009 per l'anno");
			   document.step1.data.value = "";
			   document.step1.data.focus();
			   return false;
			}
		}
		if((document.step1.sesso[0].checked == false) && (document.step1.sesso[1].checked == false)){
			alert("Compilare SESSO!");
			return false;
		}
		if(document.step1.comune.value == ""){
			alert("Compilare COMUNE");
			document.step1.comune.focus();
			return false;
		}
		if(document.step1.prov.value == "0"){
			alert("Compilare PROVINCIA!");
			document.step1.prov.focus();
			return false;
		}
		if(document.step1.codicefiscale.value == ""){
				alert("Compilare CODICE FISCALE!");
				document.step1.codicefiscale.focus();
				return false;
		} else if(document.step1.codicefiscale.value != ""){
			var CFok=codiceFISCALE(document.step1.codicefiscale.value)
			if (CFok==false){
				alert("CODICE FISCALE non valido!");
				document.step1.codicefiscale.focus();
				return false;
			  }
		}
	} else if (document.step1.nazione[1].checked == true){
		if(document.step1.nazioneid.value == "0"){
			alert("Compilare NAZIONE");
			document.step1.nazioneid.focus();
			return false;
		}
	}
   if (isEmail(document.step1.email.value) == 0 ) {
		alert("E-MAIL errata o non confermata")
		document.step1.email.focus();
		return false
	}
   if (isEmail(document.step1.emailconferma.value) == 0 ) {
		alert("E-MAIL errata o non confermata")
		document.step1.email.focus();
		return false
	}
   if (document.step1.email.value != document.step1.emailconferma.value) {
		alert("E-MAIL errata o non confermata")
		document.step1.email.focus();
		return false
	}
	if((document.step1.acconsento1[0].checked == false) && (document.step1.acconsento1[1].checked == true)){
		alert("Devi acconsentire alla trattazione dei dati per l'Italia.");
		return false;
	}
}
if (tipo == 2){
	if (isEmail(document.recupera.email.value) == 0 ) {
		alert("Inserire E-MAIL corretta!")
		document.recupera.email.focus();
		return false
	}
}
if (tipo == 3){ //step di registrazione dati personali ********************************************+
		if(document.step2.nome.value == ""){
			alert("Compilare i campi obbligatori!");
			document.step2.nome.focus();
			return false;
		}
		if(document.step2.cognome.value == ""){
			alert("Compilare i campi obbligatori!");
			document.step2.cognome.focus();
			return false;
		}
			if((document.step2.data.value == "") || (document.step2.data.value == "gg/mm/aaaa")){
				alert("Compilare DATA!");
				document.step2.data.focus();
				return false;
			} else {
				if (document.step2.data.value.substring(2,3) != "/" ||
				   document.step2.data.value.substring(5,6) != "/" ||
				   isNaN(document.step2.data.value.substring(0,2)) ||
				   isNaN(document.step2.data.value.substring(3,5)) ||
				   isNaN(document.step2.data.value.substring(6,10))) {
					  alert("Inserire nascita in formato gg/mm/aaaa");
					  document.step2.data.value = "";
					  document.step2.data.focus();
					  return false;
				} else if (document.step2.data.value.substring(0,2) > 31) {
				   alert("Impossibile utilizzare un valore superiore a 31 per i giorni");
				   document.step2.data.select();
				   return false;
				} else if (document.step2.data.value.substring(3,5) > 12) {
				   alert("Impossibile utilizzare un valore superiore a 12 per i mesi");
				   document.step2.data.value = "";
				   document.step2.data.focus();
				   return false;
				} else if (document.step2.data.value.substring(6,10) < 1900) {
				   alert("Impossibile utilizzare un valore inferiore a 1900 per l'anno");
				   document.step2.data.value = "";
				   document.step2.data.focus();
				   return false;
				} else if (document.step2.data.value.substring(6,10) > 2009) {
				   alert("Impossibile utilizzare un valore superiore al 2009 per l'anno");
				   document.step2.data.value = "";
				   document.step2.data.focus();
				   return false;
				}
			}
			if(document.step2.idJob.value == 0){
				alert("Compilare il SETTORE e la POSIZIONE DESIDERATI");
				document.step2.idJob.focus();
				return false;
			}
			if((document.step2.sesso[0].checked == false) && (document.step2.sesso[1].checked == false)){
				alert("Compilare SESSO!");
				return false;
			}
			if(document.step2.comune.value == ""){
				alert("Compilare COMUNE");
				document.step2.comune.focus();
				return false;
			}
			if(document.step2.prov.value == "0"){
				alert("Compilare PROVINCIA!");
				document.step2.prov.focus();
				return false;
			}
			if(document.step2.codicefiscale.value == ""){
					alert("Compilare CODICE FISCALE!");
					document.step2.codicefiscale.focus();
					return false;
			} else if(document.step2.codicefiscale.value != ""){
				var CFok=codiceFISCALE(document.step2.codicefiscale.value)
				if (CFok==false){
					alert("Compilare correttamente il CODICE FISCALE");
					document.step2.codicefiscale.focus();
					return false;
				  }
			}
		   if (isEmail(document.step2.email.value) == 0 ) {
				alert("E-MAIL errata o non confermata")
				document.step2.email.focus();
				return false
			}
			if(document.step2.stato_civile.value == "0"){
				alert("Compilare STATO CIVILE");
				document.step2.stato_civile.focus();
				return false;
			}
			if(document.step2.telefono1.value == ""){
				alert("Compilare TELEFONO 1");
				document.step2.telefono1.focus();
				return false;
			}
			if(document.step2.res_paese.value == "0"){
				alert("Compilare PAESE DI RESIDENZA");
				document.step2.res_paese.focus();
				return false;
			}
			if(document.step2.res_indirizzo.value == ""){
				alert("Compilare INDIRIZZO DI RESIDENZA");
				document.step2.res_indirizzo.focus();
				return false;
			}
			if(document.step2.res_citta.value == ""){
				alert("Compilare COMUNE DI RESIDENZA");
				document.step2.res_citta.focus();
				return false;
			}
			if(document.step2.res_provincia.value == "0"){
				alert("Compilare PROVINCIA DI RESIDENZA");
				document.step2.res_provincia.focus();
				return false;
			}
			if((document.step2.res_provincia.value == "ES") && (document.step2.res_paese.value == "Italia")){
				alert("Compilare PROVINCIA DI RESIDENZA");
				document.step2.res_provincia.focus();
				return false;
			}
			if(document.step2.res_cap.value == ""){
				alert("Compilare CAP DI RESIDENZA");
				document.step2.res_cap.focus();
				return false;
			}
			if((document.step2.dom_provincia.value != "") && (document.step2.dom_provincia.value == "ES") && (document.step2.dom_paese.value == "Italia")){
				alert("Compilare PROVINCIA DI DOMICILIO");
				document.step2.dom_provincia.focus();
				return false;
			}

		}
if (tipo == 4){ //step di registrazione dati personali ********************************************+
		if(document.step3.livello1.value == "0"){
			alert("Compilare LIVELLO 1!");
			document.step3.livello1.focus();
			return false;
		}
}
if (tipo == 5){ //step di registrazione dati personali ********************************************+
		if((document.step4.esperienze[0].checked == false) && (document.step4.esperienze[1].checked == false)){
			alert("Specificare se hai avuto ESPERIENZE LAVORATIVE!");
			return false;
		}
		if((document.step4.esperienze[0].checked == true) && (document.step4.esperienze[1].checked == false)){
			if(document.step4.contratto1.value == "0"){
				alert("Compilare CONTRATTO 1!");
				document.step4.contratto1.focus();
				return false;
			}
			if(document.step4.idJob1.value == "0"){
				alert("Compilare POSIZIONE 1!");
				document.step4.idJob1.focus();
				return false;
			}
			if(document.step4.settore1.value == "0"){
				alert("Compilare SETTORE 1!");
				document.step4.settore1.focus();
				return false;
			}
		}
		if((document.step4.esperienze[0].checked == false) && (document.step4.esperienze[1].checked == true)){
			return true;
		}
}
if (tipo == 6){
			if(document.step5.inglese1.value == "0"){
				alert("Compilare INGLESE SCRITTO!");
				document.step5.inglese1.focus();
				return false;
			}
			if(document.step5.inglese2.value == "0"){
				alert("Compilare INGLESE PARLATO!");
				document.step5.inglese2.focus();
				return false;
			}
		if((document.step5.office[0].checked == false) && (document.step5.office[1].checked == false)){
			alert("Specificare se si è a conoscenza delle APPLICAZIONI OFFICE!");
			return false;
		}
}

else {
      return true;
}
}


function ajaxFileUpload(idField)
{
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
		$("#uploadOk").hide();
	})
	.ajaxComplete(function(){
		$(this).hide();
		$("#uploadOk").show();
	});

	$.ajaxFileUpload
	(
		{
			url:'act_cv.php?act=7&idField='+idField,
			secureuri:false,
			fileElementId:'fileToUpload',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert("a"+data.error);
					}else
					{
						alert("b"+data.msg);
					}
				}
				
			},
			error: function (data, status, e)
			{
				
			}
			
			
		}
	)
	
	var sURL = unescape(top.location.pathname);
			//top.location.reload();
	return false;

}


function saveNote(idCv){
	$.post("act_cv.php?act=1", { id: idCv, note: $("#note").val() },
		function(data){
			alert($("#note").val())
			if (data == 1){
				errore = 0;
			}
			
		});
}


function selectJT(idJT){
	$.post("act_jbt.php?act=1", { idField: idJT},
		function(data){
			if (data != 0){
				vet = data.split("#");
				$("#jobtitle").val(vet[0]);
				$("#jtBtn").val("Salva le modifiche");
				$("#act").val("1");
				$("#idFieldSave").val(idJT);
				$("#idArea").selectOptions(vet[1]);
			}
			else{
				alert("errore");
			}
		});
}

function saveJT(){
	
	if ($("#act").val() == 0){
			$.post("act_jbt.php?act=2", {description: $("#jobtitle").val(), idArea: $("#idArea").val()},
			function(data){
				if (data != 0){
					document.location.reload();
				}
				else{
					alert("errore");
				}
			});
	}
	else{
			$.post("act_jbt.php?act=3", { idField: $("#idFieldSave").val(), description: $("#jobtitle").val()},
			function(data){
				if (data != 0){
					document.location.reload();
				}
				else{
					alert("errore");
				}
			});
	}
}

function delJT(idJT){
	$.post("act_jbt.php?act=4", { idField: idJT},
		function(data){
			if (data != 0){
				document.location.reload();
			}
			else{
				alert("errore");
			}
		});
}

function setOpportunitaView(view){
	if(view == undefined){
		view = 1;
	}
	$.post("act_cv.php?act=3", { view: view},
		function(data){
			if (view == 0){
				$("#OpportBox").hide();
				$("#OpenOpp").show();
				WideMain();
			}
			else{
				$("#OpportBox").show();
				$("#OpenOpp").hide();
				SmallMain();
				
			}
			
		});
}

function showOpportunity(view){
	if(view == undefined){
		view = 1;
	}
	
	if (view == 0){
		$("#OpportBox").hide();
		$("#OpenOpp").show();
		WideMain();
	}
	else{
		$("#OpportBox").show();
		$("#OpenOpp").hide();
		SmallMain();
	}
	
	
}

function saveCV(){
	var errore = "";
	if ($("#nome").val() == ""){
		errore = errore + "Il campo Nome &egrave; obbligatorio<br />";
	}
	if ($("#cognome").val() == ""){
		errore = errore + "Il campo Cognome &egrave; obbligatorio<br />";
	}
	if ($("#idJob").val() == "0"){
		errore = errore + "Il campo Posizione desiderata &egrave; obbligatorio<br />";
	}
	if (errore == ""){
		document.saveCv.submit();
	}
	else{
		$("#errore").html(errore);
	}
}

function selectJobTitle(idArea, selectFinale, selector){
	
	var vet,n,valore,selectedValue;
	var myOptions;
	selectedValue = 0;
	$.post("act_cv.php?act=8",
			{idArea:idArea},
			function(response)
			{
				
				$("#"+selectFinale).empty(); 
				
				myOptions = {
					"0" : "Seleziona una voce"
				}
				$("#"+selectFinale).addOption("0", "Seleziona una voce");
				
				vet = response.split(";");
				
				for (n = 0; n<(vet.length-1);n++){
					str = vet[n];
					valore = str.split("#");
					$("#"+selectFinale).addOption(valore[0], valore[1]);
					
					if (selector == valore[0]){
						selectedValue = n + 1;
						
					}
				}
				
				if (selector == undefined){
					$("#"+selectFinale).selectOptions('0');
				}else{
					$("#"+selectFinale).selectOptions(''+selector+'');
				}
				
				
			});
}
function selectJobTitleRicerca(idArea, selectFinale){
	
	var vet,n,valore;
	
	var myOptions;
	
	
	
	$.post("act_cv.php?act=8",
			{idArea:idArea},
			function(response)
			{
				
				$("#"+selectFinale).empty(); 
				
				myOptions = {
					"0" : "Seleziona una voce"
				}
				$("#"+selectFinale).addOption("0", "Seleziona una voce");
				
				vet = response.split(";");
				
				for (n = 0; n<(vet.length-1);n++){
					str = vet[n];
					valore = str.split("#");
					
					$("#"+selectFinale).addOption(valore[0], valore[1]);
				}
				$("#"+selectFinale).sortOptions(false); 
				
				
				$("#"+selectFinale).selectOptions('1', true);
								
			});
}


	

