/*********************************************************************
**********************************************************************
*  Jump procedures
**********************************************************************
*********************************************************************/
                                
//---------------------------------------------------------------------
// Move to Simple Search Screen
function ToSimpleSearch(initialize)
{
 top.LastQuery="All";

 var a, b=0 ;
 
    for (a=0; a<top.TotalDBases; a++)
    {
        if (top.DBases[a].use)
        {
            b++;
        }
    }
    if (b<1)
    {
        alert("Sélectionnez une base de données");
        return;
    }
    if (initialize) ReInitQuery(4);

    top.Main_Frm.location = "/arn/Search.htm";
}

//---------------------------------------------------------------------
// Move to Expert Search Screen
function ToExpertSearch()
{

    var a,b=0,c = 0;
    for (a=0; a < top.TotalDBases; a++)
    {
        if (top.DBases[a].use) { 
           c++;
           if (a>=1 && a<=4) b++;
        }
    }
   
    if ((c==1 && b==0) || ( b>0 && b==c))
    {
        if (top.DBases[0].use)
            SearchExp("Arcade",0);
        else if (top.DBases[1].use || top.DBases[2].use || top.DBases[3].use || top.DBases[4].use)
            SearchExp("Arno",0);
        else if (top.DBases[5].use)
            SearchExp("Leonore",0);
        else if (top.DBases[6].use)
            SearchExp("Nat",0);
        else if (top.DBases[7].use)
            SearchExp("Prof",0);
        else if (top.DBases[8].use)
            SearchExp("Quidam",0);
    }
    else 
    {
        alert("En mode recherche experte vous devez sélectionner une seule base à la fois. Précisez votre choix." );
        //top.Main_Frm.location = "SearchExp.htm";
    }
}
//---------------------------------------------------------------------
function SearchExp( strName, initialize )
{   
    var a;
    top.LastQuery = strName;
    if ( strName == "Arno")
    {
        var i1,i2,i3,i4;

        i1 = top.DBases[1].use;
        i2 = top.DBases[2].use;
        i3 = top.DBases[3].use;
        i4 = top.DBases[4].use;
        if (!i1 && !i2 && !i3 && !i4)
        {
            i1 = 1;
            i2 = 1;
            i3 = 1;
            i4 = 1;
        }

        for (a=0; a < top.TotalDBases; a++)
            top.DBases[a].use=0;
        
        top.DBases[1].use=i1;
        top.DBases[2].use=i2;
        top.DBases[3].use=i3;
        top.DBases[4].use=i4;
        
        if (initialize)
        {
            ReInitQuery("Arno1551");
            ReInitQuery("Arno1751");
            ReInitQuery("Arno1761");
            ReInitQuery("Arno1851");
        }
        top.Main_Frm.location = "/arn/SearchExp_" + strName + ".htm"; 
    }
    else
    {                            
       for (a=0; a < top.TotalDBases; a++) 
        {
            if ( top.DBases[a].dbname == strName)
            {
                top.DBases[a].use=1;
                if (initialize) { ReInitQuery(strName); }

            } else {
               top.DBases[a].use=0;
            }
        }
        top.Main_Frm.location = "/arn/SearchExp_" + strName + ".htm"; 
    }
}

//---------------------------------------------------------------------
//
function ReturnToSearch(reinit)
{
    if (top.LastQuery!="All")
        SearchExp(top.LastQuery,reinit);
    else
        ToSimpleSearch(reinit);
}

//---------------------------------------------------------------------
//
function ReInitQuery(dbase)
{
   for (a=0; a<top.TotalQueries; a++) {
      if (top.Queries[a].name = dbase) {
         top.Queries[a].Init();
      }
   }

}
//---------------------------------------------------------------------
// Move to Hint Sceen for any field
function ToHint(fieldname,form)
{
      var value = document.forms[form].elements[fieldname].value;
      var val = "/arn-cgi/value_list.pl?fieldname="+escape(fieldname)+"&value=" + escape(value) + "&dir=1";  
      var i;
      val += "&DBases="; 
      for (i=0;i<top.TotalDBases;i++) 
      {       
         if (top.DBases[i].use) 
            val += escape(top.DBases[i].dbname + " ");
      }
      val += "&form=" + form ;
      val += "&size=" + document.forms[form].RPP.options[document.forms[form].RPP.selectedIndex].value;         

      val_wnd = window.open(val, "dist_list", "toolbar=0,location=0,resizable=1,directories=0,status=0,menubar=0,scrollbars=1,height=380,width=520");

//top.Main_Frm.location = val ;


//alert(val) ;
//   val_wnd = window.open (val) ;
//   val_wnd = window.open(val, "dist_list", "toolbar=0,location=0,resizable=1,directories=0,status=0,menubar=0 ,scrollbars=1");

 val_wnd.focus();


}

//------------------------------------------------------------------------------------------------------
//
function ShowLogin()
{
 var url = "/arn-cgi/an_login_form.pl";

// url = escape(url);
// alert(url);
 top.Main_Frm.location = url;
}

//------------------------------------------------------------------------------------------------------
// Quidam: open the content of the GROUPE field in a new window 
function GroupeWindow(groupe)
{
   gwnd=window.open("","gr_wnd","width=200,height=100,toolbar=0,location=0,directories=0,status=0,scrollbars=1,menubar=0,resizable=yes");
	gwnd.document.write('<HEAD><TITLE>Groupe window</TITLE></HEAD>');
	gwnd.document.write('<BODY bgcolor="white">');
	gwnd.document.write(groupe);
	gwnd.document.write('<BR><BR><CENTER><A HREF="javascript:self.close()">Fermer</A></CENTER></BODY>')
   gwnd.document.close();
	gwnd.focus();
}

function ToConsult(file)
{
   var url = "/arn-cgi/open_consult.pl?file=" + file;
  // alert(url);
   top.Main_Frm.location = url;
}

// window OnFocus
//
/*
//---------------------------------------------------------------------
// Move to Hint Screen for 'Noms de personnes' 
function ToHintNDP()
{
    ToHint("NOMS_DE_PERSONNES_FL")   
}

//---------------------------------------------------------------------
// Move to Hint Screen for 'Noms de lieux' 
function ToHintNDL()
{
   ToHint("NOMS_DE_LIEUX_FL")

}

//---------------------------------------------------------------------
// Move to Hint Screen for 'Matieres' 
function ToHintM()
{
   ToHint("MATIERES_FL")
}

//---------------------------------------------------------------------
// Move to Hint Screen for 'Dates' 
function ToHintD()
{
   ToHint("DATES_FL")
}
*/

/*********************************************************************
**********************************************************************
*  Input Queries related procedures
**********************************************************************
*********************************************************************/
function SyncInput2Q(input)
{
 var a,i,list="";

// find proper Queries index (by form name eq Queries name) 
 for(a=top.Queries.length-1; a>=0; a--) {
    if (top.Queries[a].name == input.form.name) {
       break;
    }
 }
 if (a<0) {
    alert("Queries set for " + input.form.name + " not defined!");
    return;
 }
 
// assign value from input to Queries 
 if (input.type == "text") {
    eval("top.Queries[a]." + input.name + "= input.value");
 } else if (input.type == "select-one") {
    eval("top.Queries[a]." + input.name + "= input.options[input.selectedIndex].value");
 } else if (input.type == "select-multiple") {
     for(i=1;i<input.length;i++)
     {
       if(input.options[i].selected)
       {
          list += "\'" + input.options[i].value + "\'" + " " ;
       }
     }
    eval("top.Queries[a]." + input.name + "= list"); 
 } //end else if
}

function SyncQ2Input(frm)
{
 var a, b, c, multiple_array, val;
// find proper Queries index (by form name eq Queries name) 
 for(a=top.Queries.length-1; a>=0; a--) {
    if (top.Queries[a].name == frm.name) {
       break;
    }
 }
 if (a<0) {
    alert("Queries set for " + frm.name + " not defined!");
    return;
 }
 
// assign value from Queries to input
 for (b=frm.elements.length-1; b>=0; b--) {
    if (frm.elements[b].type == "text") {
       frm.elements[b].value = eval("top.Queries[a]." + frm.elements[b].name);
    } else if (frm.elements[b].type == "select-one") {
       val = eval("top.Queries[a]." + frm.elements[b].name);
//       alert("in select with " + val);
       for (c=frm.elements[b].options.length-1; c>=0; c--) {
          if (frm.elements[b].options[c].value == val) {
             frm.elements[b].options[c].selected = true;
             break;          
          }
       }
    } else if (frm.elements[b].type == "select-multiple") {
      val = eval("top.Queries[a]." + frm.elements[b].name);
       //alert("in select with " + val);
       multiple_array = val.split("'") ;
       for(var i=1; i<multiple_array.length; i+=2 ) 
       {
         // alert ("element " + i + " " + multiple_array[i]) ;
          for (c=frm.elements[b].options.length-1; c>=0; c--) {
           if (frm.elements[b].options[c].value == multiple_array[i]) {
              frm.elements[b].options[c].selected = true;
           }
          }
       }
       multiple_array.length = 1 ;
    }//end else if
  }
}


/*********************************************************************
**********************************************************************
*  DBases acces/manipulation procedures
**********************************************************************
*********************************************************************/

//---------------------------------------------------------------------
// SetAllUse sets usage of all DBases to val

function SetAllUse(val)
{
var b;
if (top.Uau) {
	b=0;
} else {
	b=-1;
}
   
   for (a=0; a<top.TotalDBases+b; a++) {
      top.DBases[a].use = val;
   }
}

//---------------------------------------------------------------------
// This function displays current DB stat
function DBStat(idx)
{
// alert("DB idx: " + idx + "\nname: " + top.DBases[idx].dbname + "\nview: " + top.DBases[idx].view + "\nuse: " + top.DBases[idx].use + "\ncurr res:" + top.DBases[idx].current_results);
}

//---------------------------------------------------------------------
// This function displays all current DB stat
function AllDBStat()
{
 for(a=0;a<top.TotalDBases;a++) {DBStat(a);}
}

//---------------------------------------------------------------------
// This function displays current Queries stat
function QStat(idx)
{
 switch(idx) {
   case 4: 
//      alert("Q idx: " + idx + "\nname: " + top.Queries[4].name + "\nl: " + top.Queries[4].LIBRE + "\nndp: " + top.Queries[4].NOMS_DE_PERSONNES_FL + "\nndl: " + top.Queries[4].NOMS_DE_LIEUX_FL + "\nm: " + top.Queries[4].MATIERES_FL + "\nd: " + top.Queries[4].DATES_FL + "\nRPP: " + top.Queries[4].RPP);
      break;
   case 0:
//      alert("Q idx: " + idx + "\nname: " + top.Queries[idx].name + "\nartises: " + top.Queries[idx].artises + "\ntitre: " + top.Queries[idx].titre + "\ntheme: " + top.Queries[idx].theme + "\ntype: " + top.Queries[idx].type + "\nlocalisation: " + top.Queries[idx].localisation + "\nfree: " + top.Queries[idx].free + "\nfree_fld: " + top.Queries[idx].free_fld + "\nRPP: " + top.Queries[idx].RPP);
      break;
 }
}

//---------------------------------------------------------------------
// top.Queries[idx] function displays all current Queries stat
function AllQStat()
{
    for(a=0;a<top.TotalQueries;a++) {QStat(a);}
}

//---------------------------------------------------------------------
//Clear

function ReInitMarkedDocs()
{
    var a;
    top.LastOpenedDoc = -1;
    for (a= 0 ; a < top.MarkedDocs.length;a++)
// czy to nie gubi pamieci ? Ficio_Ask
       top.MarkedDocs[a] = null;
    top.MarkedDocs.length = 0;
}
                                               
//----------------------------------------------------------------------
function ReplaceDocLinks(dlns)
{
  var ss ;
  var re ;
  var nb ;
  var ii ;

  nb = dlns.length ;
  for (ii=0 ; ii<nb ; ii++)
  {
    ss = dlns[ii].href ;
    //alert ("ii = " + ii + "  .." + dlks[ii].href + "..")
    if (ss.indexOf("fastweb.exe?") == -1){ continue ; }
    //re = /\+/gi ;
    //ss = ss.replace(re, "&p=" );
    re = /(.*)fastweb.exe\?/gi ;
    dlns[ii].href = ss.replace(re, "/arn-cgi/doc_post_proc.pl?" ) ; 
  }
}

//----------------------------------------------------------------------
// string join 
function SJoin(sGlue)
{
   var res="";
   if (SJoin.arguments.length >= 2)
   {
        res = SJoin.arguments[1];
        for (var i = 2; i < SJoin.arguments.length; i++)                     
            res += sGlue + SJoin.arguments[i];
   }
   return res;
}



