/**

 * @author Francesco

 */

function controllaPwd(idCampoPassword, idCampoUser) {
	var passwordUtente = document.getElementById(idCampoPassword).value;
	
	//controllo la lunghezza della password
	if(passwordUtente.length < 8) {
		alert("La password deve contenere almeno 8 caratteri");
	}
	
	//se la lunghezza va bene procedo a controllare che contenga almeno un numero o non contenga il nome utente.
	else {
	var passwordSplittata = passwordUtente.split(document.getElementById("username").value);
	
	if(passwordSplittata.length > 1)	
		alert("La password non deve contenere il nome utente");
		
	else if(passwordUtente.search("[0-9]") == -1)
		alert("La password deve contenere almeno un numero");
	}
}
 

function Register() {}



Register.prototype = new AjaxModule();

Register.prototype.constructor = Register;



Register.prototype.server_page = "./include_user/php/subscribe.php";



Register.prototype.fields = [];

Register.prototype.fields.push({id:"username", pattern:/^[\w\W\d\s_-]{3,}$/, message:"Username non valido\n(min. 3 caratteri)."});

Register.prototype.fields.push({id:"userpass", pattern:/^.{8,}$/, message:"Password non valida\n(min. 8 caratteri)."});

Register.prototype.fields.push({id:"email", pattern:/^([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})){1}$/, message:"Indirizzo e-mail non valido."});

Register.prototype.fields.push({id:"nome", pattern:/^[\w\W\d\s]{2,}$/, message:"Nome non valido\n(min. 2 caratteri)."});

Register.prototype.fields.push({id:"cognome", pattern:/^[\w\W\d\s]{2,}$/, message:"Cognome non valido\n(min. 2 caratteri)."});

Register.prototype.fields.push({id:"codice_fiscale", pattern:/[a-zA-Z]{6}[0-9]{2}[a-zA-Z][0-9]{2}[a-zA-Z][0-9]{3}[a-zA-Z]/, message:"Codice Fiscale non valido."});

Register.prototype.fields.push({id:"ragione_sociale", pattern:/([a-zA-Z0-9]){0,}$/, message:"Ragione Sociale non valida\n(min. 1 carattere)."});

Register.prototype.fields.push({id:"partita_iva", pattern:/^[0-9]{0,}$/, message:"Partita IVA non valida\n(Solo numeri)."});

Register.prototype.fields.push({id:"indirizzo", pattern:/([a-zA-Z0-9]){1,}$/, message:"Indirizzo non valido\n(min. 1 carattere)."});

Register.prototype.fields.push({id:"comune", pattern:/([a-zA-Z0-9]){1,}$/, message:"Comune non valido\n(min. 1 carattere)."});

Register.prototype.fields.push({id:"provincia", pattern:/([a-zA-Z0-9]){1,}$/, message:"Provincia mancante."});

Register.prototype.fields.push({id:"captchastring", pattern:/([a-zA-Z0-9]){6}$/, message:"Campo di verifica non valido."});



Register.prototype.submit = function()

{

	this.clearParams();



	this.checkUsername();

}



Register.prototype.checkUsername = function()

{

	this.clearParams();

	

	if (getVal("username").length <= 2)

	{

		alert("Username non valido\n(min. 3 caratteri)");

		getById("username").style.borderColor = "red";

		return;

	}

	if (getVal("email").length <= 0)

	{

		alert("E-Mail non valida");

		getById("email").style.borderColor = "red";

		return;

	}

	

	this.params["username"] = getVal("username");

	this.params["email"] = getVal("email");



	this.setTarget(this.controlla);

	this.makeRequest("./include_user/php/checkusername.php");

}



Register.prototype.controlla = function(response)

{

	var status = new String(response.valueOf());

	

	if (status.valueOf() == "user")

	{

		alert("Username gia' esistente!");

		getById("username").style.borderColor = "red";

		return;

	}

	else getById("username").style.borderColor = "green";



	if (status.valueOf() == "mail")

	{

		alert("E-Mail gia' esistente!");

		getById("email").style.borderColor = "red";

		return;

	}



	getById("email").style.borderColor = "green";



	for (var i = 0; i < Register.prototype.fields.length; i++)

	{

		var check = new String(document.getElementById(Register.prototype.fields[i].id).value);

		if (!check.match(Register.prototype.fields[i].pattern)) 

		{

			alert(Register.prototype.fields[i].message);

			getById(Register.prototype.fields[i].id).style.borderColor = "red";

			return;

		}

		else 

		{

			this.params[Register.prototype.fields[i].id] = check;

			getById(Register.prototype.fields[i].id).style.borderColor = "green";

		}

	}



	rb = document.form_registra.tipo_imprenditore;



	for (var i = 0; i < rb.length; i++)

		if (rb[i].checked == true)

			this.params["tipo_imprenditore"] = rb[i].value;



	if (this.params["tipo_imprenditore"] == null)

	{

		alert("Selezionare un tipo di utente");

		return;

	}



	if (!getById("ok_privacy").checked)

	{

		alert("Per proseguite e' necessario\naccettare le condizioni per la privacy.");

		return;

	}



	this.params["ok_ciao_impresa"] = (getById("ok_ciao_impresa").checked) ? "1" : "0";



	this.setTarget(document.getElementById("php_response"));

	this.makeRequest(Register.prototype.server_page);	

}



var regobj = new Register();
