var isPressed = false;
var alertWaitPageSubmit = "Espera, la pagina se esta cargando!";
function getBrowser(){
  return navigator.appName + navigator.appVersion;
}
function trim(stringa){
  while(stringa.charAt(0)==" ") stringa=stringa.substr(1);
  while(stringa.charAt(stringa.length-1)==" ") stringa=stringa.substr(0, stringa.length-1);
  return stringa;
}
function NumericField(inVal){
  if(inVal.search(/\D/)!=-1){
     return false;
  }else{
     return true;
  }
}
function showHideDiv(divID){
  var theDiv = document.getElementById(divID);
  if(theDiv){
     if((theDiv.style.display=="none") || (theDiv.style.display=="") || (theDiv.style.display=="undefined")){
     	theDiv.style.display="inline";
     }else{
        theDiv.style.display="none";
     }
  }
}
function findOffsetLeft(element){
  return findOffset(element, "offsetLeft");
}
function findOffsetTop(element){
  return findOffset(element, "offsetTop");
}
function findOffset(element, type){
  var os = 0;
  while(element){
      os += element[type];
      element = element.offsetParent;
  }
  return os;
}
function fSubmit(idForm){
  if(isPressed){
     alert(alertWaitPageSubmit);
     return false;
  }else{
     isPressed = true;
  }
  document.getElementById(idForm).submit();
  return true;
}
function fSubmitAction(idForm, inAction){
  if(isPressed){
     alert(alertWaitPageSubmit);
     return false;
  }else{
     isPressed = true;
  }
  document.getElementById(idForm).action=inAction;
  document.getElementById(idForm).submit();
  return true;
}
function fSubmitActionTarget(idForm, inAction, inTarget){
  document.getElementById(idForm).action=inAction;
  document.getElementById(idForm).target=inTarget;
  document.getElementById(idForm).submit();
  return true;
}
theFunc="";
function submitFormAjax(inUrl, outFunc, idForm, formTarget){
  var theForm = $(idForm);
  var params = Form.serialize($(idForm));
  theFunc = outFunc;
  var ajaxRequest = new Ajax.Request(inUrl,{Method: 'post', parameters: params, onComplete: AjaxResponse});
}
function AjaxResponse(originalRequest){
  if(theFunc==""){
     alert(originalRequest.responseText);
  }else if(theFunc=="updateUnidades"){
     updateUnidades(originalRequest.responseText);
  }
}
function stringToArray(stringa, delim){
  var arrOut = stringa.split(delim);
  return arrOut;
}
function openPop(inURL,inName,inWidth,inHeight){
  re = /&/gi;
  re2 = /\?/gi;
  var theURL = inURL.replace(re,'%26');
  theURL = theURL.replace(re2,'%3F');
  var params = "width="+inWidth+",height="+inHeight+",left=1,top=1,resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes,directories=no,location=no,status=no";
  window.open(theURL,inName,params);
}
function openPopSubmitForm(inURL,inName,inWidth,inHeight,idForm){
  openPop(inURL,inName,inWidth,inHeight);
  document.getElementById(idForm).target=inName;
  document.getElementById(idForm).submit();
}
function setQueryString(frm){
  var queryString = "";
  var numberElements =  frm.elements.length;
  for(var i = 0; i < numberElements; i++){
      if(i < numberElements-1){
         queryString += frm.elements[i].name+"="+encodeURIComponent(frm.elements[i].value)+"&";
      }else{
         queryString += frm.elements[i].name+"="+encodeURIComponent(frm.elements[i].value);
      }
  }
  return queryString;
}
//login
var regExpMail  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var alertNoEmail = " - e-mail\n";
var alertNoPass = " - contraseña\n";
function fLoginSub(idForm){
  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("loginMail")){
     var campo = document.getElementById("loginMail").value;
     if(trim(campo)==""){
        alertText += alertNoEmail;
        if(document.getElementById("loginMailLabel")){
           errorId = errorId + "loginMailLabel" + comma;
        }else{
           errorId = errorId + "loginMail" + comma;
        }
     }
  }
  if(document.getElementById("loginPass")){
     var campo = $F('loginPass');
     if(trim(campo)==""){
        alertText += alertNoPass;
        if(document.getElementById("loginPassLabel")){
           errorId = errorId + "loginPassLabel" + comma;
        }else{
           errorId = errorId + "loginPass" + 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;
     }
     document.getElementById(idForm).submit();
     return true;
  }
}
//popAlert
function showPopAlert(redir_url, redir_params){
  var all_params = "";
  var array_params = redir_params.split('&');
  for(var i=0; i<=array_params.length-1; i++){
      var param = array_params[i];
      all_params+="&"+param;
  }
  new Ajax.Updater('popAlert', 'plantilla_alert.jsp?redir_url='+redir_url+all_params, {asynchronous:true});
  var elTapun = $('elTapun');
  elTapun.style.width=document.body.offsetWidth;
  elTapun.style.height=document.body.scrollHeight;
  elTapun.style.display='inline';
  Element.show('popAlert');
}
//home-ini
//home-end
//rrhh-ini
function setListaAreaProfesional(){
  var idx = $('listaAreaProfesional').selectedIndex;
  var val = $('listaAreaProfesional').options[idx].value;
  var txt = $('listaAreaProfesional').options[idx].text;
  $('idxListaAreaProfesional').value = idx;
  $('valListaAreaProfesional').value = val;
  $('txtListaAreaProfesional').value = txt;
}

function setListaTipoOferta(){
  var idx = $('listaTipoOferta').selectedIndex;
  var val = $('listaTipoOferta').options[idx].value;
  var txt = $('listaTipoOferta').options[idx].text;
  $('idxListaTipoOferta').value = idx;
  $('valListaTipoOferta').value = val;
  $('txtListaTipoOferta').value = txt;
}

var alertNoNombre = " - Nombre\n";
var alertNoApellidos = " - Apellidos\n";
var alertNoFechaNacimiento = " - Fecha Nacimiento\n";
var alertNoPoblacion = " - Poblacion\n";
var alertNoPais = " - Pais\n";
var alertNoEmail = " - E-mail\n";
var alertNoAreaProfesional = " - Área profesional\n";
var alertNoTipoOferta = " - Tipo de oferta\n";

function fEnvioCurriculumSubmit(idForm){
  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";
            }
         }
     }
  }
  errorId = "";
  if(document.getElementById("LabelNombre")){
     var campo = $F('nombre');
     if(trim(campo)==""){
        alertText += alertNoNombre;
        errorId = errorId + "LabelNombre" + comma;
     }else if(campo.search(/\d/) != -1){
        alertText += alertNoNombre;
        errorId = errorId + "LabelNombre" + comma;
     }
  }
  if(document.getElementById("LabelApellidos")){
     var campo = $F('apellidos');
     if(trim(campo)==""){
        alertText += alertNoApellidos;
        errorId = errorId + "LabelApellidos" + comma;
     }else if(campo.search(/\d/) != -1){
        alertText += alertNoApellidos;
        errorId = errorId + "LabelApellidos" + comma;
     }
  }
  if(document.getElementById("LabelFechaNacimiento")){
     var campo = $F('fechanacimiento');
     if(trim(campo)==""){
        alertText += alertNoFechaNacimiento;
        errorId = errorId + "LabelFechaNacimiento" + comma;
     }
  }
  if(document.getElementById("LabelPoblacion")){
     var campo = document.getElementById("poblacion").value;
     if(trim(campo)==""){
        alertText += alertNoPoblacion;
        errorId = errorId + "LabelPoblacion" + comma;
     }else if(campo.search(/\d/) != -1){
        alertText += alertNoPoblacion;
        errorId = errorId + "LabelPoblacion" + comma;
     }
  }
  if(document.getElementById("LabelPais")){
     var campo = document.getElementById("pais").value;
     if(trim(campo)==""){
        alertText += alertNoPais;
        errorId = errorId + "LabelPais" + comma;
     }else if(campo.search(/\d/) != -1){
        alertText += alertNoPais;
        errorId = errorId + "LabelPais" + comma;
     }
  }
  if(document.getElementById("LabelEmail")){
     var campo = document.getElementById("email").value;
     if(trim(campo)==""){
        alertText += alertNoEmail;
        errorId = errorId + "LabelEmail" + comma;
     }else if(regExpMail.test(campo)==false){
        alertText += alertNoEmail;
        errorId = errorId + "LabelEmail" + comma;
     }
  }
  if(document.getElementById("LabelAreaProfesional")){
     var campo = document.getElementById("valListaAreaProfesional").value;
     if(trim(campo)=="" || trim(campo)=="0"){
        alertText += alertNoAreaProfesional;
        errorId = errorId + "LabelAreaProfesional" + comma;
     }
  }
  if(document.getElementById("LabelTipoOferta")){
     var campo = document.getElementById("valListaTipoOferta").value;
     if(trim(campo)=="" || trim(campo)=="0"){
        alertText += alertNoTipoOferta;
        errorId = errorId + "LabelTipoOferta" + 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]).className=="FieldRequired"){
                  document.getElementById(aErrorId[j]).className="FieldAlert";
               }
            }
        }
     }
     return false;
  }else{
     if(isPressed){
        alert(alertWaitPageSubmit);
        return false;
     }else{
        isPressed = true;
     }
     document.getElementById(idForm).submit();
     return true;
  }
}
//rrhh-end
//buscador-ini
function guardarBusquedaSubmit(idForm){
  var searchText = $F('searchText');
  if(searchText!=""){
     document.getElementById(idForm).action="/areapersonal/ctl_servlet?_f=30062";
     document.getElementById(idForm).submit();
  }
  return true;
}
//buscador-end