var localeEmailAlreadyInUse_profile_reg = 'El em@ail que has escrito ya est&acute; siendo utilizando. Introduce otra direccion em@il'; 	  
var localeRequestError_profile_reg 	= 'Ooops... Error en la solicitud :-( ';
var localePwdSixChars_profile_reg 	= 'Especifica una contrasena de al menos 6 caracteres';
var localePwdNotUsername_profile_reg 	= 'Escribe una contrasena diferente del usuario';
var localePwdConfirm_profile_reg 	= 'Especifica una contrasena de al menos 6 caracteres';
var localeUserSixChars_profile_reg ='Especifica un usuario de al menos 6 caracteres';
var localeInsertMail_profile_reg = 'Introduce una em@il';
var localeInsertUser_profile_reg = 'Introduce un usuario';
var localeInsertSex_profile_reg = 'Introduce el sexo';
var localeInsertProv_profile_reg = 'Introduce provincia';
var localeInsertBirth_profile_reg = 'Introduce Fecha de nacimiento';

function verificaPWD (f) {
 if (f.email.value == "") {
  alert(localeIsertMail_profile_reg);
  return false;
 }
 return true;
}

function emailSuccess() {
 emailVerified = true
 document.getElementById("sem_email").src = iconImages[1].src;
 document.getElementById("error_email").innerHTML = "";
}

function emailError() {
 emailVerified = false;
 document.getElementById("sem_email").src = iconImages[2].src;
 document.getElementById("error_email").innerHTML = localeEmailAlreadyInUse_profile_reg;
}

function checkEmail(email,callSuccess,callError) {
 if (email.length > 0) {
  var errorStr = "Errore nella richiesta";
  $.ajax({
   type: "POST",
   url: "/gp/webService.do?dispatch=checkEmail",
   data: "email="+email,
   error: function(){
     fuepsAlert(errorStr);
   },
   success: function(msg){
     var res = ""; 
     $(msg).find("result").each(function(){res = $(this).text();});   
     if (res == "ok")
      callSuccess();
     else
      callError();
   }
  });
 }
}

function checkNewPwd(){
 var checkPwd = true;
 if (document.formDati.password.value.length > 0) {
  if (document.formDati.password.value.length < 6) {
   checkPwd = false;
   document.getElementById("sem_password").src = iconImages[2].src;
   document.getElementById("error_password").innerHTML = localePwdSixChars_profile_reg;
  }
  else if (document.formDati.username.value == document.formDati.password.value) {
   checkPwd = false;
   document.getElementById("sem_password").src = iconImages[2].src;
   document.getElementById("error_password").innerHTML = localePwdNotUsername_profile_reg;
  }
  
  if (checkPwd) {
   document.getElementById("sem_password").src = iconImages[1].src;
   document.getElementById("error_password").innerHTML = "";
  }
 } 
}

function checkConfirmPwd(){
 var checkPwd = true;
 if (document.formDati.passwordConfirm.value.length > 0) {
  if (document.formDati.passwordConfirm.value.length < 6) {
   checkPwd = false;
   document.getElementById("sem_conferma").src = iconImages[2].src;
   document.getElementById("error_conferma").innerHTML = localePwdSixChars_profile_reg;
  }
  else if (document.formDati.passwordConfirm.value != document.formDati.password.value) {
   checkPwd = false;
   document.getElementById("sem_conferma").src = iconImages[2].src;
   document.getElementById("error_conferma").innerHTML = localePwdConfirm_profile_reg;
  }
  
  if (checkPwd) {
   document.getElementById("sem_conferma").src = iconImages[1].src;
   document.getElementById("error_conferma").innerHTML = "";
  }
 }
}


function isEmailNew(email,old){
	if (email != old){
		checkEmail(email,emailSuccess,emailError);
	}else{
		emailSuccess();
	}
}



String.prototype.trim = function() {	return this.replace(/^\s+|\s+$/g,""); }


//=====================================================
function goSubmit(askFullData) {

if (document.formDati.username.value == document.formDati.password.value) {
 alert(localePwdNotUsername_profile_reg);
 return;
}
if (document.formDati.passwordConfirm.value!=''){
if (document.formDati.passwordConfirm.value!='' && document.formDati.passwordConfirm.value != document.formDati.password.value) {
 alert(localePwdConfirm_profile_reg);
 return;
}
}


if (document.formDati.birth_d.value == '') {
 alert(localeInsertBirth_profile_reg);
 return;
}

if (document.formDati.birth_m.value == '') {
 alert(localeInsertBirth_profile_reg);
 return;
}

if (document.formDati.birth_y.value == '') {
 alert(localeInsertBirth_profile_reg);
 return;
}

if (document.formDati.email.value == '') {
 alert(localeInsertMail_profile_reg);
 return;
}

if (document.formDati.username.value == '') {
alert(localeInsertUser_profile_reg);
return;
}

if (document.formDati.username.value.length < 6) {
alert(localeUserSixChars_profile_reg);
return;
}

if (document.formDati.gender.selectedIndex == -1) {
alert(localeInsertSex_profile_reg);
return;
}

if (document.formDati.province.selectedIndex <= 0) {
alert(localeInsertProv_profile_reg);
return;
}

try {
	  if (!emailVerified) {
	   alert(localeEmailAlreadyInUse_profile_reg);
	   return;
	  }
} catch (e) {}


/*if (askFullData) {
	var fullDataErrorMsg = '';
	fullDataErrorMsg = verifyFullData();
	if (fullDataErrorMsg!='') {
		fuepsAlert(fullDataErrorMsg);
		return false;
	}
} else {
	setAnagraficaDefaults();
} */


	document.formDati.submit();
}
