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 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');
}
//recomendar
function showPopRecomendar(linkid){
  try{
     var punto_riferimento = document.getElementById("recomendarBottom");
     var top = findOffsetTop(punto_riferimento) + punto_riferimento.offsetHeight;
     top = top+500;

     new Ajax.Updater('popRecomendar', 'recomendar.htm', {asynchronous:true});
     var elTapun = $('elTapun');
     elTapun.style.width=document.body.offsetWidth;
     elTapun.style.height=top+"px";
     elTapun.style.display='inline';

     var popRecomendar = $('popRecomendar');
     popRecomendar.style.display='inline';
     
     punto_riferimento = document.getElementById(linkid);
     var top = findOffsetTop(punto_riferimento) + punto_riferimento.offsetHeight;
     top = top-300;
     popRecomendar.style.top = top + "px";
     
  }catch(e){}
}
function fRecomendarSubmit(){
  var params = Form.serialize($('fRecomendar'));
  new Ajax.Updater('fRecomendar', 'ctl_servlet', {asynchronous:true, parameters:params});
}
function fRecomendarCancelar(){
  Element.hide('popRecomendar')
  Element.hide('elTapun');
}
//misDocumentos
function misDocumentosSave(ID_DOCUMENTO, ID_CARPETA, TIPODOC_ID, TITULO_DOCUMENTO){
  try{
     var params = "_f=300&ID_DOCUMENTO=" + ID_DOCUMENTO
                + "&ID_CARPETA=" + ID_CARPETA
                + "&TIPODOC_ID=" + TIPODOC_ID
                + "&TITULO_DOCUMENTO=" + TITULO_DOCUMENTO;
     //new Ajax.Updater('AJAXOUT', 'ctl_servlet', {asynchronous:true, method:'post', parameters:params});
     new Ajax.Request('ctl_servlet', {asynchronous:true, method:'post', parameters:params, onSuccess:misDocumentosSaveOut});
  }catch(e){
     alert("Error: "+e);
  }
}
function misDocumentosSaveOut(originalRequest){
  //$('starSpanTop').update("");
  //$('starSpanTop').innerHTML;
  //$('starSpanBottom').update("");
  //$('starSpanBottom').innerHTML;
  
  if(originalRequest.responseText=="ok"){
     $('starLinkTop').removeClassName('star');
     $('starLinkTop').addClassName('icon-ok');
     $('starLinkBottom').removeClassName('star');
     $('starLinkBottom').addClassName('icon-ok');
	 /*$('misDocumentos').update(parseInt($('misDocumentos').innerHTML)+1);
	 $('misDocumentos').innerHTML;
	 //modificar la cookie
	 var cookie = Get_Cookie('CK_MENU_DX');
	 alert(cookie.substring(cookie.indexOf("$"),cookie.length));
	 Set_Cookie('CK_MENU_DX',($('misDocumentos').innerHTML)+cookie.substring(cookie.indexOf("$"),cookie.length),0,'/','','');*/
  }else{
     $('starLinkTop').removeClassName('star');
     $('starLinkTop').addClassName('icon-ko');
     $('starLinkBottom').removeClassName('star');
     $('starLinkBottom').addClassName('icon-ko');
  }
}
//revistas
function showDivBibliografia(ref, linkid){
  try{
     document.getElementById('divBiblioText').innerHTML="";
     var punto_riferimento = document.getElementById(linkid);
     var top = findOffsetTop(punto_riferimento) + punto_riferimento.offsetHeight;
     var left = findOffsetLeft(punto_riferimento);
     document.getElementById('divBiblio').style.top = top + "px";
     //document.getElementById('divBiblio').style.left = left + "px";
     if(document.getElementById('li_'+ref)){
        var refTxt = document.getElementById('li_'+ref).innerHTML;
        document.getElementById('divBiblioText').innerHTML=refTxt;
        document.getElementById('divBiblio').style.display='inline';
     }
  }catch(e){}
}
function showDivBibliografias(refs, linkid){
  try{
     document.getElementById('divBiblioText').innerHTML="";
     var punto_riferimento = document.getElementById(linkid);
     var top = findOffsetTop(punto_riferimento) + punto_riferimento.offsetHeight;
     var left = findOffsetLeft(punto_riferimento);
     document.getElementById('divBiblio').style.top = top + "px";
     //document.getElementById('divBiblio').style.left = left + "px";

     var refTxt="";
     refs = stringToArray(refs, '-');
     var first = refs[0];
     first = first/1;
     var last = refs[refs.length-1];
     last = last/1;
     for(var ref=first; ref<=last; ref++){
     	if(document.getElementById('li_'+ref)){
           refTxt+=document.getElementById('li_'+ref).innerHTML+"<br />";
     	}
     }
     if(refTxt!=""){
        document.getElementById('divBiblioText').innerHTML=refTxt;
        document.getElementById('divBiblio').style.display='inline';
     }
  }catch(e){}
}
//suscripcion
function setListaZonageo(){
  var idx = document.getElementById("listaZonageo").selectedIndex;
  var val = document.getElementById("listaZonageo").options[idx].value;
  var txt = document.getElementById("listaZonageo").options[idx].text;
  document.getElementById("idxListaZonageo").value=idx;
  document.getElementById("valListaZonageo").value=val;
  document.getElementById("txtListaZonageo").value=txt;
}
function setListaPerfil(){
  var idx = document.getElementById("listaPerfil").selectedIndex;
  var val = document.getElementById("listaPerfil").options[idx].value;
  var txt = document.getElementById("listaPerfil").options[idx].text;
  document.getElementById("idxListaPerfil").value=idx;
  document.getElementById("valListaPerfil").value=val;
  document.getElementById("txtListaPerfil").value=txt;
}
function fSuscripcionPreciosSubmit(){
  var params = Form.serialize($('fSuscripcionPrecios'));
  new Ajax.Updater('suscripcion_precios', 'ctl_servlet?_f=7062', {asynchronous:true, parameters:params});
}
//log-pdf
function runAjaxRequest(inUrl){
  var ajaxRequest = new Ajax.Request(inUrl,{Method: 'post'});
}
function Set_Cookie( name, value, expires, path, domain, secure ){
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
function Get_Cookie( name ) {		
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
