//areapersonal - fRegistroLeadMinimo/fRegistroLeadAbierto/fRegistroCompra
function setListaTiposDeContacto(){
  var idx = document.getElementById("listaTiposDeContacto").selectedIndex;
  var val = document.getElementById("listaTiposDeContacto").options[idx].value;
  var txt = document.getElementById("listaTiposDeContacto").options[idx].text;
  document.getElementById("idxListaTiposDeContacto").value=idx;
  document.getElementById("valListaTiposDeContacto").value=val;
  document.getElementById("txtListaTiposDeContacto").value=txt;
  listaTiposDeContactoEmpresa(txt);
}
function setListaTratamiento(){
  var idx = document.getElementById("listaTratamiento").selectedIndex;
  var val = document.getElementById("listaTratamiento").options[idx].value;
  var txt = document.getElementById("listaTratamiento").options[idx].text;
  document.getElementById("idxListaTratamiento").value=idx;
  document.getElementById("valListaTratamiento").value=val;
  document.getElementById("txtListaTratamiento").value=txt;
}
function setListaProfesion(){
  var idx = document.getElementById("listaProfesion").selectedIndex;
  var val = document.getElementById("listaProfesion").options[idx].value;
  var txt = document.getElementById("listaProfesion").options[idx].text;
  document.getElementById("idxListaProfesion").value=idx;
  document.getElementById("valListaProfesion").value=val;
  document.getElementById("txtListaProfesion").value=txt;
}
function setListaEspecialidades(){
  var idx = document.getElementById("listaEspecialidades").selectedIndex;
  var val = document.getElementById("listaEspecialidades").options[idx].value;
  var txt = document.getElementById("listaEspecialidades").options[idx].text;
  document.getElementById("idxListaEspecialidades").value=idx;
  document.getElementById("valListaEspecialidades").value=val;
  document.getElementById("txtListaEspecialidades").value=txt;
}
function setListaPaises(){
  var idx = document.getElementById("listaPaises").selectedIndex;
  var val = document.getElementById("listaPaises").options[idx].value;
  var txt = document.getElementById("listaPaises").options[idx].text;
  document.getElementById("idxListaPaises").value=idx;
  document.getElementById("valListaPaises").value=val;
  document.getElementById("txtListaPaises").value=txt;
}
function setListaTiposDeVia(){
  var idx = document.getElementById("listaTiposDeVia").selectedIndex;
  var val = document.getElementById("listaTiposDeVia").options[idx].value;
  var txt = document.getElementById("listaTiposDeVia").options[idx].text;
  document.getElementById("idxListaTiposDeVia").value=idx;
  document.getElementById("valListaTiposDeVia").value=val;
  document.getElementById("txtListaTiposDeVia").value=txt;
}
function setListaProvincias(){
  var idx = document.getElementById("listaProvincias").selectedIndex;
  var val = document.getElementById("listaProvincias").options[idx].value;
  var txt = document.getElementById("listaProvincias").options[idx].text;
  document.getElementById("idxListaProvincias").value=idx;
  document.getElementById("valListaProvincias").value=val;
  document.getElementById("txtListaProvincias").value=txt;
}
//areapersonal - fRegistroLeadMinimo
var alertNoTipoDeContacto = " - Tipo de contacto\n";
var alertNoRegEmail = " - E-mail\n";
var alertNoRegPassword = " - Contraseña\n";
var alertNoRegTratamiento = " - Tratamiento\n";
var alertNoRegNombre = " - Nombre\n";
var alertNoRegApellido1 = " - Primer apellido\n";
var alertNoRegApellido2 = " - Segundo apellido\n";
var alertNoRegPais = " - País\n";
var alertNoRegProfesion = " - Profesión\n";
var alertNoRegEspecialidad = " - Especialidad\n";
//areapersonal - fRegistroLeadAbierto
var alertNoRegDireccion = " - Dirección\n";
var alertNoRegCodigoPostal = " - Código Postal\n";
var alertNoRegPoblacion = " - Población\n";
var alertNoRegProvincia = " - Provincia\n";
var alertNoRegTelefono = " - Teléfono\n";
var alertNoRegAnnoLicenziatura = " - Año de Licenciatura\n";
var alertNoRegCargoProfesional = " - Cargo Profesional\n";
//areapersonal - fRegistroCompra
var alertNoRegNIF = " - NIF\n";
var alertNoClausola = " - Claúsula de privacidad\n";

function fRegistroSubmit(idForm){
  //idForm can be: fRegistroLeadMinimo/fRegistroLeadAbierto/fRegistroCompra
  var alertText = "";
  var comma = ",";
  //Initialize required fields classes
  if((typeof(errorId) != "undefined") && (errorId != "")){
      aErrorId = errorId.split(",");
      for(var i=0; i<=aErrorId.length-1; i++){
         if(aErrorId[i] != ""){
            if(document.getElementById(aErrorId[i]).className=="FieldAlert"){
               document.getElementById(aErrorId[i]).className="FieldRequired";
            }else if(document.getElementById(aErrorId[i]).className=="FieldAlertInput"){
               document.getElementById(aErrorId[i]).className="FieldRequiredInput";
            }
         }
      }
  }
  errorId = "";
  if(document.getElementById("listaTiposDeContacto") && document.getElementById("listaTiposDeContacto").disabled==false){
     var campo = document.getElementById("valListaTiposDeContacto").value;
     if(trim(campo)==""){
        alertText += alertNoTipoDeContacto;
        if(document.getElementById("listaTiposDeContactoLabel")){
           errorId = errorId + "listaTiposDeContactoLabel" + comma;
        }else{
           errorId = errorId + "listaTiposDeContacto" + comma;
        }
     }else if(trim(campo)=="0"){
        alertText += alertNoTipoDeContacto;
        if(document.getElementById("listaTiposDeContactoLabel")){
           errorId = errorId + "listaTiposDeContactoLabel" + comma;
        }else{
           errorId = errorId + "listaTiposDeContacto" + comma;
        }
     }
  }
  if(document.getElementById("regMail") && document.getElementById("regMail").disabled==false){
     var campo = document.getElementById("regMail").value;
     if(trim(campo)==""){
        alertText += alertNoRegEmail;
        if(document.getElementById("regMailLabel")){
           errorId = errorId + "regMailLabel" + comma;
        }else{
           errorId = errorId + "regMail" + comma;
        }
     }else if(regExpMail.test(campo)==false){
        alertText += alertNoRegEmail;
        if(document.getElementById("regMailLabel")){
           errorId = errorId + "regMailLabel" + comma;
        }else{
           errorId = errorId + "regMail" + comma;
        }
     }
  }
  if(document.getElementById("regPassword1") && document.getElementById("regPassword2") && document.getElementById("regPassword1").disabled==false  && document.getElementById("regPassword2").disabled==false){
     var user_pswd = document.getElementById("regPassword1").value;
     var user_pswd2 = document.getElementById("regPassword2").value;
     if(trim(user_pswd)==""){
        alertText += alertNoRegPassword;
        if(document.getElementById("regPassword1Label")){
           errorId = errorId + "regPassword1Label" + comma;
        }else{
           errorId = errorId + "regPassword1" + comma;
        }
     }else if(trim(user_pswd2)==""){
        alertText += alertNoRegPassword;
        if(document.getElementById("regPassword2Label")){
           errorId = errorId + "regPassword2Label" + comma;
        }else{
           errorId = errorId + "regPassword2" + comma;
        }
     }else if((trim(user_pswd)) !=(trim(user_pswd2))){
        alertText += alertNoRegPassword;
        if(document.getElementById("regPassword1Label")){
           errorId = errorId + "regPassword1Label" + comma;
        }else{
           errorId = errorId + "regPassword1" + comma;
        }
        if(document.getElementById("regPassword2Label")){
           errorId = errorId + "regPassword2Label" + comma;
        }else{
           errorId = errorId + "regPassword2" + comma;
        }
     }
  }
  if(document.getElementById("listaTratamiento") && document.getElementById("listaTratamiento").disabled==false){
     var campo = document.getElementById("valListaTratamiento").value;
     if(trim(campo)==""){
        alertText += alertNoRegTratamiento;
        if(document.getElementById("listaTratamientoLabel")){
           errorId = errorId + "listaTratamientoLabel" + comma;
        }else{
           errorId = errorId + "listaTratamiento" + comma;
        }
     }else if(trim(campo)=="0"){
        alertText += alertNoRegTratamiento;
        if(document.getElementById("listaTratamientoLabel")){
           errorId = errorId + "listaTratamientoLabel" + comma;
        }else{
           errorId = errorId + "listaTratamiento" + comma;
        }
     }
  }
  if(document.getElementById("regNombre") && document.getElementById("regNombre").disabled==false){
     var campo = document.getElementById("regNombre").value;
     if(trim(campo)==""){
        alertText += alertNoRegNombre;
        if(document.getElementById("regNombreLabel")){
           errorId = errorId + "regNombreLabel" + comma;
        }else{
           errorId = errorId + "regNombre" + comma;
        }
     }
  }
  if(document.getElementById("regApellido1") && document.getElementById("regApellido1").disabled==false){
     var campo = document.getElementById("regApellido1").value;
     if(trim(campo)==""){
        alertText += alertNoRegApellido1;
        if(document.getElementById("regApellido1Label")){
           errorId = errorId + "regApellido1Label" + comma;
        }else{
           errorId = errorId + "regApellido1" + comma;
        }
     }
  }
  if(document.getElementById("listaPaises") && document.getElementById("listaPaises").disabled==false){
     var campo = document.getElementById("valListaPaises").value;
     if(trim(campo)==""){
        alertText += alertNoRegPais;
        if(document.getElementById("listaPaisesLabel")){
           errorId = errorId + "listaPaisesLabel" + comma;
        }else{
           errorId = errorId + "listaPaises" + comma;
        }
     }else if(trim(campo)=="0"){
        alertText += alertNoRegPais;
        if(document.getElementById("listaPaisesLabel")){
           errorId = errorId + "listaPaisesLabel" + comma;
        }else{
           errorId = errorId + "listaPaises" + comma;
        }
     }
  }
  if(document.getElementById("listaProfesion") && document.getElementById("listaProfesion").disabled==false){
     var campo = document.getElementById("valListaProfesion").value;
     if(trim(campo)==""){
        alertText += alertNoRegProfesion;
        if(document.getElementById("listaProfesionLabel")){
           errorId = errorId + "listaProfesionLabel" + comma;
        }else{
           errorId = errorId + "listaProfesion" + comma;
        }
     }else if(trim(campo)=="0"){
        alertText += alertNoRegProfesion;
        if(document.getElementById("listaProfesionLabel")){
           errorId = errorId + "listaProfesionLabel" + comma;
        }else{
           errorId = errorId + "listaProfesion" + comma;
        }
     }
  }
  if(document.getElementById("listaEspecialidades") && document.getElementById("listaEspecialidades").disabled==false){
     var campo = document.getElementById("valListaEspecialidades").value;
     if(trim(campo)==""){
        alertText += alertNoRegEspecialidad;
        if(document.getElementById("listaEspecialidadesLabel")){
           errorId = errorId + "listaEspecialidadesLabel" + comma;
        }else{
           errorId = errorId + "listaEspecialidades" + comma;
        }
     }else if(trim(campo)=="0"){
        alertText += alertNoRegEspecialidad;
        if(document.getElementById("listaEspecialidadesLabel")){
           errorId = errorId + "listaEspecialidadesLabel" + comma;
        }else{
           errorId = errorId + "listaEspecialidades" + comma;
        }
     }
  }
  //direccion
  var flg_alertNoDireccion = "false";
  if(document.getElementById("listaTiposDeVia") && document.getElementById("listaTiposDeVia").disabled==false){
     var campo = document.getElementById("valListaTiposDeVia").value;
     if(trim(campo)==""){
        flg_alertNoDireccion = "true"; //flg_alertNoDireccion
        if(document.getElementById("listaTiposDeViaLabel")){
           errorId = errorId + "listaTiposDeViaLabel" + comma;
        }else{
           errorId = errorId + "listaTiposDeVia" + comma;
        }
     }else if(trim(campo)=="0"){
        flg_alertNoDireccion = "true";
        if(document.getElementById("listaTiposDeViaLabel")){
           errorId = errorId + "listaTiposDeViaLabel" + comma;
        }else{
           errorId = errorId + "listaTiposDeVia" + comma;
        }
     }
  }
  if(document.getElementById("regDireccionNombreVia") && document.getElementById("regDireccionNombreVia").disabled==false){
     var campo = document.getElementById("regDireccionNombreVia").value;
     if(trim(campo)==""){
        flg_alertNoDireccion = "true"; //flg_alertNoDireccion
        if(document.getElementById("regDireccionNombreViaLabel")){
           errorId = errorId + "regDireccionNombreViaLabel" + comma;
        }else{
           errorId = errorId + "regDireccionNombreVia" + comma;
        }
     }
  }
  if(document.getElementById("regDireccionNumero") && document.getElementById("regDireccionNumero").disabled==false){
     var campo = document.getElementById("regDireccionNumero").value;
     if(trim(campo)==""){
        flg_alertNoDireccion = "true"; //flg_alertNoDireccion
        if(document.getElementById("regDireccionNumeroLabel")){
           errorId = errorId + "regDireccionNumeroLabel" + comma;
        }else{
           errorId = errorId + "regDireccionNumero" + comma;
        }
     }
  }
  
//  if(document.getElementById("regDireccionExtension") && document.getElementById("regDireccionExtension").disabled==false){
     //var campo = document.getElementById("regDireccionExtension").value;
     //if(trim(campo)==""){
     //   flg_alertNoDireccion = "true"; //flg_alertNoDireccion
     //   if(document.getElementById("regDireccionExtensionLabel")){
     //      errorId = errorId + "regDireccionExtensionLabel" + comma;
     //   }else{
    //     errorId = errorId + "regDireccionExtension" + comma;
     //   }
    // }
//  }
  //flg_alertNoDireccion
  
  if(flg_alertNoDireccion=="true"){
     alertText += alertNoRegDireccion;
  }

  if(document.getElementById("regCodigoPostal") && document.getElementById("regCodigoPostal").disabled==false){
     var campo = document.getElementById("regCodigoPostal").value;
     if(trim(campo)==""){
        alertText += alertNoRegCodigoPostal;
        if(document.getElementById("regCodigoPostalLabel")){
           errorId = errorId + "regCodigoPostalLabel" + comma;
        }else{
           errorId = errorId + "regCodigoPostal" + comma;
        }
     }else if(NumericField(campo)==false){
        alertText += alertNoRegCodigoPostal;
        if(document.getElementById("regCodigoPostalLabel")){
           errorId = errorId + "regCodigoPostalLabel" + comma;
        }else{
           errorId = errorId + "regCodigoPostal" + comma;
        }
     }
  }
  if(document.getElementById("regPoblacion") && document.getElementById("regPoblacion").disabled==false){
     var campo = document.getElementById("regPoblacion").value;
     if(trim(campo)==""){
        alertText += alertNoRegPoblacion;
        if(document.getElementById("regPoblacionLabel")){
           errorId = errorId + "regPoblacionLabel" + comma;
        }else{
           errorId = errorId + "regPoblacion" + comma;
        }
     }
  }
  if(document.getElementById("regTelefono") && document.getElementById("regTelefono").disabled==false){
     var campo = document.getElementById("regTelefono").value;
     if(trim(campo)==""){
        alertText += alertNoRegTelefono;
        if(document.getElementById("regTelefonoLabel")){
           errorId = errorId + "regTelefonoLabel" + comma;
        }else{
           errorId = errorId + "regTelefono" + comma;
        }
     }
  }
  if(document.getElementById("regAnnoLicenziatura") && document.getElementById("regAnnoLicenziatura").disabled==false){
     var campo = document.getElementById("regAnnoLicenziatura").value;
     if(trim(campo)==""){
        alertText += alertNoRegAnnoLicenziatura;
        if(document.getElementById("regAnnoLicenziaturaLabel")){
           errorId = errorId + "regAnnoLicenziaturaLabel" + comma;
        }else{
           errorId = errorId + "regAnnoLicenziatura" + comma;
        }
     }else if(NumericField(campo)==false){
        alertText += alertNoRegAnnoLicenziatura;
        if(document.getElementById("regAnnoLicenziaturaLabel")){
           errorId = errorId + "regAnnoLicenziaturaLabel" + comma;
        }else{
           errorId = errorId + "regAnnoLicenziatura" + comma;
        }
     }else if(campo.length<4){
        alertText += alertNoRegAnnoLicenziatura;
        if(document.getElementById("regAnnoLicenziaturaLabel")){
           errorId = errorId + "regAnnoLicenziaturaLabel" + comma;
        }else{
           errorId = errorId + "regAnnoLicenziatura" + comma;
        }
     }
  }
  if(document.getElementById("regCargoProfesional") && document.getElementById("regCargoProfesional").disabled==false){
     var campo = document.getElementById("regCargoProfesional").value;
     if(trim(campo)==""){
        alertText += alertNoRegCargoProfesional;
        if(document.getElementById("regCargoProfesionalLabel")){
           errorId = errorId + "regCargoProfesionalLabel" + comma;
        }else{
           errorId = errorId + "regCargoProfesional" + comma;
        }
     }
  }
  if(document.getElementById("regClausola") && document.getElementById("regClausola").disabled==false){
     if(document.getElementById("regClausola").checked!=true){
        alertText += alertNoClausola;
        errorId = errorId + "regClausola" + comma;
     }
  }
  if(alertText!=""){
     alert("Por favor revisa:\n\n" + alertText);
     if((typeof(errorId) != "undefined") && (errorId != "")){
        errorId = errorId.substr(0, errorId.length-1);
        aErrorId = errorId.split(",");
        for(var j=0; j<=aErrorId.length-1; j++){
            if(aErrorId[j]!=""){
               if(document.getElementById(aErrorId[j]).type=="text" || document.getElementById(aErrorId[j]).type=="password"){
                  document.getElementById(aErrorId[j]).className="FieldAlertInput";
               }
               if(document.getElementById(aErrorId[j]).className=="FieldRequired"){
                  document.getElementById(aErrorId[j]).className="FieldAlert";
               }
            }
        }
     }
     return false;
  }else{
     if(isPressed){
        alert(alertWaitPageSubmit);
        return false;
     }else{
       isPressed = true;
    }
     //fRegistroSubmit
     //idForm can be: fRegistroLeadMinimo/fRegistroLeadAbierto/fRegistroCompra
     document.getElementById(idForm).submit();
     return isPressed;
  }
}

function listaTiposDeContactoEmpresa(inTipo){
  if(inTipo=="Empresas"){
     if(document.getElementById("regApellido1")){
        document.getElementById("regApellido1").disabled=true;
        document.getElementById("regApellido1").style.display="none";
        document.getElementById("regApellido1Label").style.display="none";
        document.getElementById("regApellido1NoRequiredForEmpresa").style.display="none";
     }
     if(document.getElementById("regApellido2")){
        document.getElementById("regApellido2").disabled=true;
        document.getElementById("regApellido2").style.display="none";
        document.getElementById("regApellido2Label").style.display="none";
     }
     if(document.getElementById("listaTratamiento")){
        document.getElementById("listaTratamiento").disabled=true;
        document.getElementById("listaTratamiento").style.display="none";
        document.getElementById("listaTratamientoLabel").style.display="none";
        document.getElementById("listaTratamientoNoRequiredForEmpresa").style.display="none";
     }
     if(document.getElementById("listaProfesion")){
        document.getElementById("listaProfesion").disabled=true;
        document.getElementById("listaProfesion").style.display="none";
        document.getElementById("listaProfesionLabel").style.display="none";
        document.getElementById("listaProfesionNoRequiredForEmpresa").style.display="none";
     }
     if(document.getElementById("listaEspecialidades")){
        document.getElementById("listaEspecialidades").disabled=true;
        document.getElementById("listaEspecialidades").style.display="none";
        document.getElementById("listaEspecialidadesLabel").style.display="none";
        document.getElementById("listaEspecialidadesNoRequiredForEmpresa").style.display="none";
     }
     if(document.getElementById("regAnnoLicenziatura")){
        document.getElementById("regAnnoLicenziatura").disabled=true;
        document.getElementById("regAnnoLicenziatura").style.display="none";
        document.getElementById("regAnnoLicenziaturaLabel").style.display="none";
        document.getElementById("regAnnoLicenziaturaNoRequiredForEmpresa").style.display="none";
     }
     if(document.getElementById("regCargoProfesional")){
        document.getElementById("regCargoProfesional").disabled=true;
        document.getElementById("regCargoProfesional").style.display="none";
        document.getElementById("regCargoProfesionalLabel").style.display="none";
        document.getElementById("regCargoProfesionalNoRequiredForEmpresa").style.display="none";
     }
  }else{
     if(document.getElementById("regApellido1")){
        document.getElementById("regApellido1").disabled=false;
        document.getElementById("regApellido1").style.display="block";
        document.getElementById("regApellido1Label").style.display="inline";
        document.getElementById("regApellido1NoRequiredForEmpresa").style.display="inline";
     }
     if(document.getElementById("regApellido2")){
        document.getElementById("regApellido2").disabled=false;
        document.getElementById("regApellido2").style.display="block";
        document.getElementById("regApellido2Label").style.display="inline";
     }
     if(document.getElementById("listaTratamiento")){
        document.getElementById("listaTratamiento").disabled=false;
        document.getElementById("listaTratamiento").style.display="block";
        document.getElementById("listaTratamientoLabel").style.display="inline";
        document.getElementById("listaTratamientoNoRequiredForEmpresa").style.display="inline";
     }
     if(document.getElementById("listaProfesion")){
        document.getElementById("listaProfesion").disabled=false;
        document.getElementById("listaProfesion").style.display="block";
        document.getElementById("listaProfesionLabel").style.display="inline";
        document.getElementById("listaProfesionNoRequiredForEmpresa").style.display="inline";
     }
     if(document.getElementById("listaEspecialidades")){
        document.getElementById("listaEspecialidades").disabled=false;
        document.getElementById("listaEspecialidades").style.display="block";
        document.getElementById("listaEspecialidadesLabel").style.display="inline";
        document.getElementById("listaEspecialidadesNoRequiredForEmpresa").style.display="inline";
     }
     if(document.getElementById("regAnnoLicenziatura")){
        document.getElementById("regAnnoLicenziatura").disabled=false;
        document.getElementById("regAnnoLicenziatura").style.display="block";
        document.getElementById("regAnnoLicenziaturaLabel").style.display="inline";
        document.getElementById("regAnnoLicenziaturaNoRequiredForEmpresa").style.display="inline";
     }
     if(document.getElementById("regCargoProfesional")){
        document.getElementById("regCargoProfesional").disabled=false;
        document.getElementById("regCargoProfesional").style.display="block";
        document.getElementById("regCargoProfesionalLabel").style.display="inline";
        document.getElementById("regCargoProfesionalNoRequiredForEmpresa").style.display="inline";
     }
  }
}

function getWS_localidad_provincia_autonomia(regCodigoPostal){
  var params="regCodigoPostal="+regCodigoPostal
  var ajaxRequest = new Ajax.Request('ctl_servlet?_f=31002', {method:'post', asynchronous:true, parameters: params, onComplete: setWS_localidad_provincia_autonomia});
}

function setWS_localidad_provincia_autonomia(originalRequest){
  var resp = originalRequest.responseText;
  var func = new Function("return "+resp);
  var objt = func();
  var localidad = objt.localidad;
  var provincia = objt.provincia;
  var autonomia = objt.autonomia;
  if(localidad!=""){
     document.getElementById("regPoblacion").value=localidad;
     //document.getElementById("regProvincia").value=provincia;
  }
}

