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);
  }
}
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 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 = " - password\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";
               document.getElementById(aErrorId[i]).style.borderColor='#809DB9';
            }
         }
     }
  }
  errorId = "";
  if(document.getElementById("loginMail")){
     var campo = document.getElementById("loginMail").value;
     if(trim(campo)==""){
        alertText += alertNoEmail;
        errorId = errorId + "loginMail" + comma;
     }else if(regExpMail.test(campo)==false){
        alertText += alertNoEmail;
        errorId = errorId + "loginMail" + comma;
     }
  }
  if(document.getElementById("loginPass")){
     var campo = $F('loginPass');
     if(trim(campo)==""){
        alertText += alertNoPass;
        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]!=""){
               document.getElementById(aErrorId[j]).style.borderColor='red';
               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;
  }
}
//doymaselect

