//locale contents
var localePwdNotUsername_profile 	= 'Introducir una contraseña diferente del \'usuario';
var localePwdConfirm_profile 		= 'La contraseña no ha podido verificarse correctamente';
var localePwdSixChars_profile 		= 'Especifica una contraseña de al menos 6 caracteres';
var localeBirthDay_profile 		= 'Especifica un día de nacimiento válido';
var localeBirthMonth_profile 		= 'Especifica un mes de nacimiento válido';
var localeBirthYear_profile 		= 'Especifica un año de nacimiento válido';
var localeEmail_profile 		= 'Especifica una dirección de email válida';
var localeUsername_profile 		= 'Escribe un nombre de usuario';
var localeUsernameSixChars_profile 	= 'Nombre de usuario de al menos 6 caracteres';
var localeSex_profile 			= 'El sexo es importante; completa el campo sexo :-D';
var localeEmailInUse_profile 		= 'Esta dirección de email ya está siendo utilizada en nuestra base de datos';
var localeSetUsrPwd_profile 		= 'Introduce usuario y contraseña';
var localeInsertName_profile 		= '<br>Rellena el campo NOMBRE';
var localeInsertSurname_profile 	= '<br>Rellena el campo APELLIDOS';
var localeInsertAddress_profile 	= '<br>Rellena todo el campo DIRECCIÓN';
var localeInsertCity_profile 		= '<br>Rellena el campo CIUDAD\'';
var localeInsertZIPCode_profile 	= '<br>Rellena el campo CÓDIGO POSTAL';
var localeInsertState_profile 		= '<br>Selecciona PAÍS';
var localeInsertFiscalCode_profile 	= '<br>Rellena el campo CÓDIGO FISCAL';
var localeZIPCodeNumeric_profile 	= '<br>El campo CAP debe ser numérico';


// variabile contenente il nome del Cookie di riferimento....
search = "rcsLogin=";

String.prototype.trim = function() {	return this.replace(/^\s+|\s+$/g,""); }


// =====================================================
function goSubmit(action, url, askFullData) {

  if (document.formDati.nuovaPassword) {
   if (document.formDati.login.value == document.formDati.nuovaPassword.value) {
    alert(localePwdNotUsername_profile);
    return;
   }
  } else {
   if (document.formDati.login.value == document.formDati.password.value) {
    alert(localePwdNotUsername_profile);
    return;
   }
   if (document.formDati.confermaPassword.value != document.formDati.password.value) {
    alert(localePwdConfirm_profile);
    return;
   }
  }
  
  if (document.formDati.password.value.length < 6) {
    alert(localePwdSixChars_profile);
    return;
  }
  
  if (document.formDati.giorno.value == '') {
    alert(localeBirthDay_profile);
    return;
  }
  
  if (document.formDati.mese.value == '') {
    alert(localeBirthMonth_profile);
    return;
  }
  
  if (document.formDati.anno.value == '') {
    alert(localeBirthYear_profile);
    return;
  }
  
  if (document.formDati.EMail.value == '') {
    alert(localeEmail_profile);
    return;
  }

  if (document.formDati.login.value == '') {
   alert(localeUsername_profile);
   return;
  }
  
  if (document.formDati.login.value.length < 6) {
   alert(localeUsernameSixChars_profile);
   return;
  }
  
  if (document.formDati.PFSesso.selectedIndex == -1) {
   alert(localeSex_profile);
   return;
  }
  
  try {
	  if (!emailVerified) {
	   alert(localeEmailInUse_profile);
	   return;
	  }
  } catch (e) {}

  
  if (askFullData) {
  	var fullDataErrorMsg = '';
  	fullDataErrorMsg = verifyFullData();
  	if (fullDataErrorMsg!='') {
  		fuepsAlert(fullDataErrorMsg);
  		return false;
  	}
  } else {
  	setAnagraficaDefaults();
  }
  
  
	document.formDati.action = url;
	document.formDati.actionSubmit.value = action;
	document.formDati.submit();
}

function goSubmitNoLoginData(action, url, askFullData) {
  
  if (askFullData) {
  	var fullDataErrorMsg = '';
  	fullDataErrorMsg = verifyFullData();
  	if (fullDataErrorMsg!='') {
  		fuepsAlert(fullDataErrorMsg);
  		return false;
  	}
  } else {
  	setAnagraficaDefaults();
  }
  
  
	document.formDati.action = url;
	document.formDati.actionSubmit.value = action;
	document.formDati.submit();
}
// ============== VERIFICA ======================
function verifica() {
	if (document.login.username.value == "" || document.login.password.value == "") {
		alert(localeSetUsrPwd_profile);
		return false;
	}
	return true;
}
// ================= FORM ======================
function formLogin (url, operation) {
	document.write('<form name="login" method="post" action="/gp/loginServlet" onsubmit="return verifica();">');
	document.write('<input type="hidden" name="debugLevel" value="0">');
	document.write('<input type="hidden" name="applicationName" value="FPSS">');
	document.write('<input type="hidden" name="operation" value="' + operation + '">');
	document.write('<input type="hidden" name="registration" value="/gp/showPage.do?dispatch=consensi">');
	if ("" + url == "") url = "/index.shtml";
	document.write('<input type="hidden" name="url" value="' + url + '">');
	document.write('<input type="hidden" name="urlKo" value="/gp/userlogin.do?dispatch=startLogin&error=true">');
	document.write('<input type="hidden" name="username">');
	document.write('<input type="hidden" name="password">');
	document.write('</form>');
}
// ================= SET ANAGRAFICA DEFAULTS ======================
function setAnagraficaDefaults () {
	if (document.formDati.PFNome.value.trim() == '')
	document.formDati.PFNome.value = '_';
  if (document.formDati.PFCognome.value.trim() == '')
   	document.formDati.PFCognome.value = '_';
  if (document.formDati.indirizzo.value.trim() == '')
   	document.formDati.indirizzo.value = '_';
  if (document.formDati.numeroCivico.value.trim() == '')
   	document.formDati.numeroCivico.value = '0';
  if (document.formDati.localita.value.trim() == '')
   	document.formDati.localita.value = '_';
  if (document.formDati.cap.value.trim() == '')
   	document.formDati.cap.value = '_';
  if (document.formDati.codiceFiscale.value.trim() == '')
   	document.formDati.codiceFiscale.value = '_';
  if (document.formDati.partitaIva.value.trim() == '')
   	document.formDati.partitaIva.value = '_';
  if (document.formDati.toponimo.value.trim() == '')
   	document.formDati.toponimo.value = '_';
  if (document.formDati.nazione.value.trim() == '')
  	document.formDati.nazione.value = '_';
  if (document.formDati.gestoreTelefonico.value.trim() == '')
   	document.formDati.gestoreTelefonico.value = '_';
}

function verifyFullData() {
	var msg = '';
	
	if (document.formDati.PFNome.value.trim() == '')
		msg += localeInsertName_profile;
	if (document.formDati.PFCognome.value.trim() == '')
		msg += localeInsertSurname_profile;
	if (document.formDati.indirizzo.value.trim() == '')
		msg += localeInsertAddress_profile;
	if (document.formDati.numeroCivico.value.trim() == '')
		msg += localeInsertAddress_profile;
	if (document.formDati.toponimo.value.trim() == '')
		msg += localeInsertAddress_profile;
	if (document.formDati.localita.value.trim() == '')
		msg += localeInsertCity_profile;
	if (document.formDati.cap.value.trim() == '')
		msg += localeInsertZIPCode_profile;
	if (document.formDati.nazione.value.trim() == '')
		msg += localeInsertState_profile;
	if (document.formDati.codiceFiscale.value.trim() == '')
		msg += localeInsertFiscalCode_profile;		
  	if (!isNumeric(document.formDati.cap.value))
  		msg += localeZIPCodeNumeric_profile;
  	
	return msg;
}

function isNumeric(text) {
  var validChars = "0123456789";
  var isNumber=true;
  var currentChar;
 
  for (i = 0; i < text.length && isNumber == true; i++) { 
  	currentChar = text.charAt(i); 
    if (validChars.indexOf(currentChar) == -1) {
    	isNumber = false;
    }
  }
  return isNumber;
 }
 
function logon(urlKo) {
	document.login.username.value = document.formDati.tUsername.value;
	document.login.password.value = document.formDati.tPassword.value;
	document.login.urlKo.value = urlKo;
	document.login.submit();
}

function getLoginValue() {
	var value = "notFound";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		if (offset != -1) 
		{
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
				end = document.cookie.length;
			value = document.cookie.substring(offset, end);
			if (value.indexOf("|") > -1) 
				value = value.substring(0, value.indexOf("|"));
		}
	}
	return value;
}

function getIsLoginValid() {
	var value = getLoginValue();
	if (value == "notValid")
		return false;
	else
		return true;
}

