var isPressed = false;
var alertWaitPageSubmit = "Espera, la pagina se esta cargando!";
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;
}
//fLoginSub 
var regExpMail  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var alertNoEmail = " - e-mail\n";
var alertNoUsuario = " - usuario\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;
        }
        
     }
     /*
     else if(regExpMail.test(campo)==false){
        alertText += alertNoEmail;
        if(document.getElementById("loginMailLabel")){
           errorId = errorId + "loginMailLabel" + comma;
        }else{
           errorId = errorId + "loginMail" + comma;
        }
     }
     */
  }
  if(document.getElementById("loginUsuario")){
     var campo = document.getElementById("loginUsuario").value;
     if(trim(campo)==""){
        alertText += alertNoUsuario;
        if(document.getElementById("loginUsuarioLabel")){
           errorId = errorId + "loginUsuarioLabel" + comma;
        }else{
           errorId = errorId + "loginUsuario" + comma;
        }
     }
  }
  if(document.getElementById("loginPass")){
     var campo = document.getElementById('loginPass').value;
     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;
  }
}


