// =========================================================================================
// /bibliopac//bin/bibliopac.js
// Última actualização: 22-Abr-2005 2:33
// =========================================================================================
//                             Copyright (C) 2002-2004 - BIBILIOsoft, Lda
// -----------------------------------------------------------------------------------------
// Este módulo contem várias funções em javascript para utilização no Interface BIBLIOpac
// 
//             Não é autorizada a utilização destas rotinas noutros produtos 
// =========================================================================================

function checkDBlist(grupo,chkstat) 
// Verifica a lista das bases seleccionadas (por grupo)
{
 var fieldList = document.form_search.elements;
 var fieldId;
 var pos;
 for (i = 0; i < fieldList.length; i++)
     {
      field = fieldList[i];
      if (field.type != 'checkbox' ){continue;}
      if (grupo == "all") 
         {       	
	   field.checked = true;
         } else 
         {
           fieldId = field.id;
           pos = grupo.length;
           if (fieldId.substring(0,pos) == grupo)
              {
               field.checked = chkstat;
              }           
         }
     }  
     return true;
}



function ImageLoadFailed()
{
  window.event.srcElement.style.display = "None";
}


function verify_images(image_name)
{
// <body OnLoad="verify_images();">

    for (var i = 0; i < document.images.length; i++)
       { 
        img = document.images[i];
        if ((img.complete != null) && (!img.complete))
           {
            alert(img.src);
            img.src = image_name;
           }

       }
}


function PrintThisPage()
{
  if (Math.round(parseFloat(navigator.appVersion)*1000)>=4000)
	{
	window.toolbar=false;
	window.print();
	}
  return true;	
}



function remLink()
{
  if (window.sList && window.sList.open && !window.sList.closed)
      window.sList.opener = null;
}
			

function rightTrim(str)
{
	var strin=str+" ";
	s=strin.length;
	var p=s;
	while (strin.charAt(p)==" " || p<=0){p=p-1};
	return strin.substring(1,p-1);    
}

function leftTrim(str)
{
	var strin=" "+str;
	var s=strin.length;
	var p=0;
	while (strin.charAt(p)==" " || p>=s){p=p+1};
	return strin.substring(p-1,s);
}


function GotoField(str)
{
  var index=str;
  var prefix=form_terms.prefix.value;
  x = prefix.length;
  var temp=index;
  if (""+index.substring(0,x)==prefix) temp=""+index.substring(x,1000);
  return temp;
}



function pick(prefix,symbol,index)
{
  type=window.name;
  var temp,sep="";
  var tprefix=""+prefix;
  var tindex=""+index;
  x = tprefix.length;

  if (type=="list11")
     {// PesqAva
      if (window.opener.document.form_search.expression.value!="") sep=" ";
      if (window.opener.document.form_search.sel_oper[1].checked) operator=" "+window.opener.document.form_search.sel_oper[1].value+" ";
      if (window.opener.document.form_search.sel_oper[2].checked) operator=" "+window.opener.document.form_search.sel_oper[2].value+" ";
          temp=rightTrim(leftTrim(window.opener.document.form_search.expression[index].value+=sep+symbol));
      if (window.opener && !window.opener.closed)
          window.opener.document.form_search.expression[index].value=temp;
          window.close();
     } else
      {// PesqSimpl
       sindex=eval(type.charAt(5));
       temp=rightTrim(leftTrim(window.opener.document.form_search.sexpression[sindex].value+=sep+symbol));
       if (""+symbol.substring(0,x)==tprefix) temp=""+symbol.substring(x,1000);
       if (window.opener && !window.opener.closed)
          window.opener.document.form_search.sexpression[sindex].value=temp;
          window.close();
      }
}



function FormWorldSearch(url_site,express)
{
	var exp=FindReplace(url_site,"(#express#)",express);
 	newwin=window.open(exp,"","width=550,height=550,toolbar=no,scrollbars=yes,resizable=yes,maximize=yes");
	window.title=express+" ["+exp+"]";
	return true;
}

function RecordServices(express,base)
{
      if (window.opener && !window.opener.closed)
        {
	window.opener.document.search_result.expression.value=express;
	if (base)window.opener.document.search_result.db.value=base;
 	window.opener.document.search_result.method="post";
	window.opener.document.search_result.submit();
	window.opener.document.focus;
        }
        window.close();
}



function ReSubmitMultisearch(base)
{
	if (base) document.search_result.db.value=base;
        document.search_result.action.value="SEARCH";
 	document.search_result.method="post";
	document.search_result.submit();
}


function ReSubmitAutor(express)
{
	if (base) document.search_result.db.value=base;
        document.search_result.action.value="SEARCH";
 	document.search_result.method="post";
	document.search_result.submit();
}


function RecordServicesMultibase(express,base)
{
      if (window.opener && !window.opener.closed)
        {
        window.opener.document.search_result.expression.value=express;
	window.opener.document.search_result.multi_search.value="ON";
        window.opener.document.search_result.action.value="SEARCH";
 	window.opener.document.search_result.method="post";
	window.opener.document.search_result.submit();
	window.opener.document.focus;
        }
        window.close();
}



function SelectMenuOption(mnu_option)
{
	document.form_options.action.value=mnu_option;
	document.form_options.method="post";
	document.form_options.submit();
	return true;
}



function FormNewsSubmit(express)
{
	document.form_news.expression.value=unescape(express);
	document.form_news.submit();
	return true;
}



function ReSubmit(express)
{
	document.search_result.expression.value=unescape(document.search_result.rexpression.value);
	document.search_result.submit();
	return true;
}




function NewSearchCOL(express)
{
  tmp=express;
  dim=express.length;
  car=express.charAt(dim-1);
  if (car==")") 
     {
      tmp=tmp.substring(0,dim-1);
      if (tmp.length<30) tmp=tmp+"$";
     }
	document.search_result.expression.value=unescape(ChkChars(tmp));
	document.search_result.submit();
	return true;
}


function NewSearch(express)
{
	document.search_result.expression.value=unescape(ChkChars(express));
	document.search_result.submit();
	return true;
}


function NewMultiSearch(express)
{
	document.form_search.expression.value=unescape(ChkChars(express));
	document.form_search.submit();
	return true;
}



function ChkLinkHref(tipo,formato,linha_in)
{
  var dim=0;
  var tmp="",car="";

  tmp=linha_in;
  dim=tmp.length;
  car=tmp.charAt(dim-1);
  if (tipo=="1" && car==")") tmp=tmp.substring(0,dim-1);

  return(param + ChkChars_2(LimpaMais(tmp)));
}



function HistoryNewSearch(express)
{	document.search_result.expression.value=unescape(express);
	document.search_result.submit();
	return true;
}



function SubmitSearchFormSimpl()
{
var op_checked;
var x=form_search.trunc.length;
for (i=0;i<x;i++)
    {
 	form_search.sexpression[i].value=FindReplace(form_search.sexpression[i].value,"$","");
      if (form_search.sexpression[i].value!="" && form_search.trunc[i].checked=="1")
         {
		if (form_search.sexpression[i].value.indexOf("$")<0) 
  	      {form_search.sexpression[i].value=form_search.sexpression[i].value+form_search.trunc[i].value;}
         } 
    }
}




function FindReplace(linha,str1,str2)
{
   var s1,s2;
   var linha_in=linha;
   var c=linha_in.length;
   var p=0;

   while ((p>-1))
      {
       p=linha_in.substring(0,c).lastIndexOf(str1);
       if (p>=0)
           {
            s1=linha_in.substring(0,p);
            s2=linha_in.substring(p+str1.length,linha_in.length);
            linha_in=s1+str2+s2;
            c=p;
           };
      }
  return linha_in;    
}





function ViewSelection()
{
	document.search_result.VIEW_SELECT_RECORDS="VIEW_SELECT_RECORDS.x=2";
/*	document.search_result.target="_blank"; */
	document.search_result.method="post";
	document.search_result.submit();
	document.search_result.target="";
	return true;
}



function ChkChars(termo)
{
  var i=0;
  var sep="";
  termo=termo.toUpperCase();
  lista=new Array("+","*","^"," AND "," OR "," NOT "," AND NOT "," (F) "," (G) ","(",")");
  if (termo.indexOf("\"")<=0)
     {
       for (i=0;i<11;i++)
           {
            if (termo.indexOf(lista[i])>=0)
               {
                sep="\"";
                break;
               }
          }
     }
  return unescape(sep+termo+sep);
}



function Check(field)
{
	if (checkflag!="true")
	{
		for(i=0;i<field.length;i++)
              {
		   field[i].checked=true;
              }
   		checkflag="true";
		return "off";
	} else
         {
		for(i=0;i<field.length;i++)
		{
		 field[i].checked=false;
		}
        	checkflag="false";
		return "on";
	    }
}


function help(fich)
{
	newwin=window.open(fich,"","width=550,height=550,scrollbars=1");
	return true;
}

function openURL(link)
{
	newwin=window.open(link,"","");
	return true;
}


function LoanStatus(link)
{
	newwin=window.open(link,"","width=550,height=350,scrollbars=1");
	return true;
}



function OpenUrlSelf(newurl)
{
 popup=window.open("","_self","");
 ndoc=popup.document;
 ndoc.location.href=newurl;
}



function _form_user_login(prefix,symbol,index)
{
   window.opener.document.form.id_user.value='';
   window.close();
}




function emailCheck(emailStr,msgout)
{
  var checkTLD=1;
  var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
  var emailPat=/^(.+)@(.+)$/;
  var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
  var validChars="\[^\\s" + specialChars + "\]";
  var quotedUser="(\"[^\"]*\")";
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
  var atom=validChars + '+';
  var word="(" + atom + "|" + quotedUser + ")";
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
  var matchArray=emailStr.match(emailPat);

if (matchArray==null) 
   {
//alert("Email address seems incorrect (check @ and .'s)")
    alert(msgout+"\n["+emailStr+"] ? (@.)");
    return false;
   }
var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i++) 
   {
    if (user.charCodeAt(i)>127)
       {
//alert("Ths username contains invalid characters.");
	    alert(msgout+"\n["+emailStr+"] ? username");
        return false;
       }
   }
for (i=0; i<domain.length; i++)
   {
    if (domain.charCodeAt(i)>127)
       {
//        alert("Ths domain name contains invalid characters.");
	    alert(msgout+"\n["+emailStr+"] ? domain");
        return false;
       }
   }
if (user.match(userPat)==null)
   {
//    alert("The username doesn't seem to be valid.");
    alert(msgout+"\n["+emailStr+"] ? username");

    return false;
   }

var IPArray=domain.match(ipDomainPat);

if (IPArray!=null)
   {
	for (var i=1;i<=4;i++)
      {
       if (IPArray[i]>255)
        {
//alert("Destination IP address is invalid!");
 	     alert(msgout+"\n["+emailStr+"] ? IP address");
         return false;
        }
      }
    return true;
  }

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++)
   {
    if (domArr[i].search(atomPat)==-1)
       {
//alert("The domain name does not seem to be valid.");
        alert(msgout+"\n["+emailStr+"] ? domain");
        return false;
       }
   }
if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1)
   {
//alert("The address must end in a well-known domain or two letter " + "country.");
    alert(msgout+"\n["+emailStr+"] ? country");
    return false;
   }
if (len<2)
   {
//alert("This address is missing a hostname!");
    alert(msgout+"\n["+emailStr+"] ? hostname");
    return false;
   }
	return true;
}


function showDetails(divID)
{     
   var strShow
       strShow = divID+"Show";
   var strHide
       strHide = divID+"Hide";
   document.all[divID].style.display = "block";
   document.all[strShow].style.display = "none";
   document.all[strHide].style.display = "block";
}

function hideDetails(divID)
{
   var strShow
       strShow = divID+"Show";
   var strHide
       strHide = divID+"Hide";
   document.all[divID].style.display = "none";
   document.all[strShow].style.display = "block";
   document.all[strHide].style.display = "none";
}



// =========================================================================================
// (c) BIBLIOsoft, Lda
// =========================================================================================
