// JavaScript Document

var optionDB = { 
       // target:        '#risposta',   // target element(s) to be updated with server response 
		dataType : 		'json',
        //beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
};

function showResponse(responseText, statusText){
	$.unblockUI();
	if (responseText.result == 'false'){
		$('#msg').html("Attenzione <br />" + responseText.msg);
		setTimeout("$(\"#msg\").text(\"\");",4000);	
	}else{
		$('#formregistrazione').html(responseText.msg);		
	}
}

function SettaCampiIscrizione(){
	
	
	if ($("input:radio[@name='aziendaPrivato']:checked").val()=='P'){		
		$('#codiceFiscaleLbl').html("Codice fiscale");
		$('#ragSocDiv').hide();
		$('#pIVADiv').hide();
	}else{		
		$('#codiceFiscaleLbl').html("Codice fiscale*");
		$('#ragSocDiv').show();
		$('#pIVADiv').show();
	}
}

function cambiaLabelProvincia (){
	label = ($('#stato').val()=="Italy") ? "Provincia*" : "Provincia";
	$('#provLabel').html(label);
}


function CtrlForm (){
	
	error = "";
	
	error += $('#cognomeNome').val()=="" ? "inserire il cognome ed il nome <br />" : "";
	
	if ($("input:radio[@name='aziendaPrivato']:checked").val()=='A'){
		error += $('#codiceFiscale').val()=="" ? "inserire il codice fiscale<br />" : "";
		error += $('#ragSoc').val()=="" ? "inserire la ragione sociale <br />" : "";
		error += $('#pIVA').val()=="" ? "inserire il numero di partita IVA <br />" : "";
	}
	
	error += $('#categoria').val()=="" ? "inserire 'Sono un' <br />" : "";
	if ($('#categoria').val()=="Insegnante") error += $('#tipoInsegnamento').val()=="" ? "inserire insegno presso <br />" : "";
	error += $('#numero').val()=="" ? "inserire il numero civico <br />" : "";
	error += $('#CAP').val()=="" ? "inserire il CAP <br />" : "";
	error += $('#localita').val()=="" ? "inserire la localit&agrave; <br />" : "";
	error += $('#stato').val()=="" ? "inserire lo stato <br />" : "";
	stato = $('#stato').val();
	
	if ((stato=="Italy") && ($('#provincia').val()=="")) error+= "inserire la provincia <br />";
	
	error += $('#email').val()=="" ? "inserire un indirizzo email <br />" : "";
	error += validEmail($('#email').val()) ? "" : "Inserire un indirizzo email valido <br />";
	
	password = $('#password').val();
	error += $('#password').val()=="" ? "inserire una password <br />" : "";
	error += $('#password2').val()=="" ? "ripetere la password <br />" : "";
	error += $('#password2').val()!=$('#password').val() ? "le due password sono differenti <br />" : "";
	error += (password.length<8) ? "inserire una password di almeno 8 caratteri <br />" : "";
	
	error += $('#trattamento').attr("checked") ? "" : "accettare il trattamento dei dati <br />";
	
	
	
	if (error==''){
		$.blockUI();
		
		$('#registrazione').ajaxForm(optionDB);
		$('#registrazione').submit();	
	}else{
		$('#msg').html("Attenzione <br />" + error);
		setTimeout("$(\"#msg\").text(\"\");",5000);
	}
}

function validEmail(email){
	if (email=="") return false;
	if (email.indexOf("@")==-1) return false;
	if (email.indexOf("@")>email.lastIndexOf(".")) return false;
	return true;
	
}