var Cache={};
    Cache.session={uid:"",token:"",page:""}
    Cache.admin  ={logUID:"",logDay:"",logTime:"",logDisplay:"",logNotIP:""}
    Cache.ISTREVI_title="ISTREVI - Istituto Storico della Resistenza di Vicenza";
    Cache.IP={ipv4:""}
    Cache.page={name:"",code:"",map:"",url:"",urlencode:"",option:""}
    Cache.articoli={counter:0}
    Cache.search={on:"START",query:""}
    Cache.error="";

var page_href={};
                                                //   alert(navigator.userAgent);

                                                   var Win=navigator.userAgent.indexOf("Win")!=-1;
                                                   var Mac=navigator.userAgent.indexOf("Mac")!=-1;
                                                   var InternetExplorer=navigator.userAgent.indexOf("MSIE")!=-1;
                                                   var	IE=((typeof document.all!="undefined") && (parseInt(navigator.appVersion)>=4))?1:0;
                                                   var Gecko=navigator.userAgent.indexOf("Gecko")!=-1;
                                                   var Safari=navigator.userAgent.indexOf("Safari")!=-1;
                                                   var Opera=navigator.userAgent.indexOf("Opera")!=-1;
                                                   var Konqueror=navigator.userAgent.indexOf("Konqueror")!=-1;

                                                   var MSIE  =navigator.userAgent.indexOf("MSIE")!=-1;
                                                   var MSIE_9=navigator.userAgent.indexOf("MSIE 9")!=-1; 
                                                   var MSIE_8=navigator.userAgent.indexOf("MSIE 8")!=-1; 
                                                   var MSIE_7=navigator.userAgent.indexOf("MSIE 7")!=-1; 
                                                   
                                                                    // alert(MSIE+" v9["+MSIE_9+"] v8["+MSIE_8+"] v7["+MSIE_7+"]");

                                                   var FFOX=navigator.userAgent.indexOf("Firefox")!=-1;
                                                   var CHROME=navigator.userAgent.indexOf("Chrome")!=-1;
                                                   
                                                   var browser="Mozilla"

                                                   if(InternetExplorer) { browser="IE"; }

 function $(id) { return document.getElementById(id); }


 function OpenPage(nextPage,logPage,logTable,logKey)
         {                                                  //   alert(nextPage+"::"+logPage+"::"+logTable+"::"+logKey+" / sourcePage["+sourcePage+"] base_root:"+base_root);
                         if(logPage==undefined) {logPage=nextPage};  
           
                                                       var  pathPage="";
                         if(sourcePage=="NewsletterPAGE") { pathPage="../"; }

                         if(nextPage=="newsletter" 
                         && logPage =="=" 
                         && logTable=="=" 
                         && logKey!=undefined) { 
                                                 nextPage="Newsletter"+logKey;
                                                 logTable="-";
                                                 logPage ="newsletter";
                                               }  

      var pageINP=document.getElementById("PageInput"); 

       // pageINP.action          =pathPage+"index.php";     
          pageINP.action          =pathPage+"index.php";     
          pageINP.target          ="_self";
          pageINP.NextPage.value  =nextPage;
          pageINP.ServerPage.value=sourcePage;
          pageINP.LogTable.value  =logTable;
          pageINP.LogKey.value    =logKey;
          pageINP.LogPage.value   =logPage;
          pageINP.submit();

        /*
         *
          PageInput.action          =pathPage+"index.php";    // base_root+pathPage+"index.php";
          PageInput.target          ="_self";
          PageInput.NextPage.value  =nextPage;
          PageInput.ServerPage.value=sourcePage;
          PageInput.LogTable.value  =logTable;
          PageInput.LogKey.value    =logKey;
          PageInput.LogPage.value   =logPage;
          PageInput.submit();
         *
         */
         }



function Articolo(articolo)        {                   
                                     OpenPage("Statuto","read","statuto",articolo); 
                                   }

function OpenBook(isbn,recensione) {                   
                                     OpenPage("Pubblicazioni_ISTREVI","presentazione",isbn,recensione); 
                                   }


function OpenHomePage()
       {
          winIstrevi=window.open("about:blank","ISTREVI_homepage",
                                 "toolbar=yes,     menubar=yes,  location=yes,   \
                                  scrollbars=yes, resizable=yes,  status=yes,    \
                                  top=10,        left=10,                      \
                                  width=900,    height=700 ");
          winIstrevi.focus();
       }

function OpenPrivacy()
       {
            winPrivacy=window.open("privacy.htm","ISTREVI_privacy",
                                   "left=200,top=70,width=320,height=150, \
                                    fullscreen=no, toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no");

        if(!winPrivacy.opener) winPrivacy.opener = self;
        if( winPrivacy.focus != null) winPrivacy.focus();
       }


function OpenDonnaSkeda(Ytop,Xleft)
     {
      winDonnaSkeda=window.open("about:blank","ISTREVI_Donna",
                                "toolbar=yes,    menubar=yes,  location=yes,   \
                                 scrollbars=yes, resizable=yes,  status=yes,   \
                                 top="+Ytop+",        left="+Xleft+",          \
                                 width=458,    height=600 ");
      winDonnaSkeda.focus();
     }

function OpenAccessPage()
     {
      winAccessPage=window.open("about:blank","_self",
                                "toolbar=yes,    menubar=yes,  location=yes,   \
                                 scrollbars=yes, resizable=yes,  status=yes,   \
                                 top=50,        left=600,                      \
                                 width=458,    height=600 ");
      winAccessPage.focus();
     }

function OpenWatchAccess()
     {
      winOpenWatchAcc=window.open("about:blank","_self",
                                  "toolbar=yes,    menubar=yes,  location=yes,   \
                                   scrollbars=yes, resizable=yes,  status=yes,   \
                                   top=100,        left=550,                      \
                                   width=640,    height=530 ");
      winOpenWatchAcc.focus();
     }

function OpenWatchList()
     {
      winOpenWatchList=window.open("about:blank","_self",
                                  "toolbar=yes,    menubar=yes,  location=yes,   \
                                   scrollbars=yes, resizable=yes,  status=yes,   \
                                   top=40,         left=300,                     \
                                   width=800,    height=600 ");
      winOpenWatchList.focus();
     }

function OpenAdminUser()
     {
      winAdminUser=window.open("about:blank","_self",
                               "toolbar=yes,    menubar=yes,  location=yes,   \
                                scrollbars=yes, resizable=yes,  status=yes,   \
                                top=140,        left=360,                      \
                                width=700,    height=400 ");
      winAdminUser.focus();
     }

function OpenAdminUserITEM()
     {
      winAdminUserITEM=window.open("about:blank","_self",
                                   "toolbar=yes,    menubar=yes,  location=yes,   \
                                    scrollbars=yes, resizable=yes,  status=yes,   \
                                    top=100,        left=700,                      \
                                    width=450,    height=500 ");
      winAdminUserITEM.focus();
     }

function OpenAdminLog()
     {
      winAdminLog=window.open("about:blank","_self",
                              "toolbar=yes,    menubar=yes,  location=yes,   \
                               scrollbars=yes, resizable=yes,  status=yes,   \
                               top=50,        left=350,                      \
                               width=850,    height=610 ");
      winAdminLog.focus();
     }

function OpenAdminLogITEM()
     {
      winAdminLogITEM=window.open("about:blank","_self",
                                   "toolbar=yes,    menubar=yes,  location=yes,   \
                                    scrollbars=yes, resizable=yes,  status=yes,   \
                                    top=100,        left=700,                      \
                                    width=450,    height=500 ");
      winAdminLogITEM.focus();
     }


function RotateMicrophone()
     {
                                                            m="A";

           SpinMicrophoneLoop=setInterval("SpinMicrophone()",350);
     }

function SpinMicrophone()
     {
                                                           MicrophoneImg    =document.getElementById("Microphone"); 
                                                       //  MicrophoneImg_SRC=document.getElementById("Microphone").src;
                                                       //  MicrophoneImg_SEQ=MicrophoneImg_SRC.indexOf("microphone");
    
                                                           MicrophoneTitle  =document.getElementById("TxtPDF");

                                                           switch(m)
                                                                 {
                                                                  case "A": m="B"; 
                                                                            MicrophoneImg.src="img/ico/microphone_B.png";
                                                                            MicrophoneTitle.style.textDecoration="underline";
                                                                            MicrophoneTitle.style.color="#2020c0";
                                                                            break;
                                                                  case "B": m="C"; 
                                                                            MicrophoneImg.src="img/ico/microphone_C.png";
                                                                            MicrophoneTitle.style.textDecoration="underline";
                                                                            MicrophoneTitle.style.color="#2020c0";
                                                                            break;
                                                                  case "C": m="D"; 
                                                                            MicrophoneImg.src="img/ico/microphone_D.png";
                                                                            MicrophoneTitle.style.textDecoration="underline";
                                                                            MicrophoneTitle.style.color="#2020c0";
                                                                            break;
                                                                  case "D": m="E"; 
                                                                            MicrophoneImg.src="img/ico/microphone_C.png";
                                                                            MicrophoneTitle.style.textDecoration="underline";
                                                                            MicrophoneTitle.style.color="#2020c0";
                                                                            break;
                                                                  case "E": m="F"; 
                                                                            MicrophoneImg.src="img/ico/microphone_B.png";
                                                                            MicrophoneTitle.style.textDecoration="underline";
                                                                            MicrophoneTitle.style.color="#2020c0";
                                                                            break;
                                                                  case "F": m="A"; 
                                                                            MicrophoneImg.src="img/ico/microphone_A.png";
                                                                            MicrophoneTitle.style.textDecoration="none";
                                                                            MicrophoneTitle.style.color="#f00000";
                                                                            break;

                                                                  default : m="A"; 
                                                                            MicrophoneImg.src="img/ico/microphone_A.png";
                                                                            MicrophoneTitle.style.textDecoration="none";
                                                                            MicrophoneTitle.style.color="#f00000";
                                                                 }
     }

       

function trim(stringa)
      {
        while(stringa.substring(0,1)==' ') { stringa = stringa.substring(1, stringa.length); }

        while(stringa.substring(stringa.length-1,stringa.length)==' '){ stringa = stringa.substring(0,stringa.length-1);}

       return stringa;
      }

var xmlhttp;
                             /*  The LogClick() function is executed every time a click is pushed on the PDF version button
                                                         the function executes the following:

                                                           - Calls the GetXmlHttpObject() function to create an XMLHTTP object
                                                           - Defines the URL (filename) to send to the server
                                                           - Adds a parameter (q) to the URL 
                                                             with the content of the input field
                                                           - Adds a random number to prevent the server 
                                                             from using a cached file
                                                           - Each time the readyState property changes, 
                                                             the stateChanged() function will be executed
                                                           - Opens the XMLHTTP object with the given URL
                                                           - Sends an HTTP request to the server
                              */                            
function LogClick(obj,usr)
       {              
           xmlhttp=GetXmlHttpObject();
        if(xmlhttp==null)
          {
            alert ("Your browser does not support XMLHTTP!"); return;
          }

           var path_php=""; var obj_vector=obj.split(":");

                             if(obj_vector[0]=="LOG"
                             || obj_vector[0]=="home[27feb11]"
                             || obj_vector[0]=="folder /img"
                             || obj_vector[2]=="newsletter"
                             || obj_vector[3]=="newsletter"
                             || obj_vector[2]=="user message")        { path_php="../";  }
                             
                             if(obj_vector[0]=="pdf[photo]"
                             || obj_vector[1]=="GALLO_2009_cerimonia"
                             || obj_vector[2]=="08PG16")              { path_php="../../";  }

            // console.log(path_php);
                                                    var userID=usr;
                                   if(usr==undefined) { userID="public"; }
       var url=path_php+"lib/log[obj].php?o="+obj+"&u="+userID+"&s="+GetKeyRandom35();

           xmlhttp.onreadystatechange=stateChanged;
           xmlhttp.open("GET",url,true);
           xmlhttp.send(null);
       }
                                                          // -----------------------------------------
                                                          //  executes every time the state 
                                                          //    of the XMLHTTP object changes
                                                          // ---------
                                                          //  When the state changes to 4 ("complete"), 
                                                          //   the content of the txtHint placeholder 
                                                          //    is filled with the response text
                                                          // -----------------------------------------
function stateChanged()
       {
            // document.getElementById("TimeNOTIFY").innerHTML=xmlhttp.readyState;

        if(xmlhttp.readyState==4)
          {
            // document.getElementById("TimeNOTIFY").innerHTML=xmlhttp.responseText;
          }
       } 
                                                           // -------- ============== --------- 
                                                           // creating XMLHttpRequest object 
                                                           //           for different browsers
                                                           // -------- ============== ---------
function GetXmlHttpObject()
       {
         if(window.XMLHttpRequest)
           {                          // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
           }

         if(window.ActiveXObject)
           {                          // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
           }
        return null;
       }
     


var GetKeyRandom10=function(){
                               var char_D=new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"); 
                               var char_K="";
           for(var i=0;i<11;i++) { char_K+=char_D[Math.floor(Math.random()*36)]; }
                         //  alert(char_K);  
                            return char_K;  
}

function GetKeyRandom35()
       {
                        var char_D=new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"); 
                        var char_K="";
    for(var i=0;i<32;i++) { char_K+=char_D[Math.floor(Math.random()*36)]; }
    
                  //  alert(char_K);  
                         
                     return char_K;  
       }

        var KeyRandom35={};
            KeyRandom35.value =GetKeyRandom35();   
            KeyRandom35.status="";   

function GetVerifyCode()
       {
            KeyRandom35.value=GetKeyRandom35();   

                  xmlhttp=GetXmlHttpObject();
               if(xmlhttp==null)
                 {
                   alert ("Your browser does not support XMLHTTP!"); return;
                 }
       
                  var path_php=""; 
                                    
              var url=path_php+"lib/ImageVerificationCode.php?s="+KeyRandom35.value;
                                                                                        // var url=path_php+"lib/ImageGetCode.php?s="+GetKeyRandom35();
                  xmlhttp.onreadystatechange=GeneratedVerifyCode;
                  xmlhttp.open("GET",url,true);
                  xmlhttp.send(null);
       }
function GeneratedVerifyCode()
       {
            // document.getElementById("TimeNOTIFY").innerHTML=xmlhttp.readyState;

        if(xmlhttp.readyState==4)
          {
                                                     var ImageCode="lib/"+xmlhttp.responseText;
          
               document.getElementById("VerifyCODE").src=ImageCode;
               
                            // ContactFORM.CNTkeys.value=KeyRandom35.value; 
                            
           if(KeyRandom35.status=="re-generate")
             {
               LogClick("cnttinfo:code:re-generate "+ImageCode.substr(13,5));

              KeyRandom35.status="";   
             }
          }
       } 
       
function SendContactBody(name,mail,subj,text,code)
       {                                           var text_0=text,
                                                          chr="",
                                                         tlen=text.length
                                                         text="";
                                           for(var c=0;c<tlen;c++)
                                              {                                chr=text_0.charAt(c);
                                               if(text_0.charCodeAt(c)==10)  { chr= "<br>"; }
                                               if(text_0.charCodeAt(c)==" ") { chr= "%20"; }

                                                                         text+=chr;
                                              } 

                  xmlhttp=GetXmlHttpObject();
               if(xmlhttp==null)
                 {
                   alert ("Your browser does not support XMLHTTP!"); return;
                 }
       
                  var path_php=""; 
                                    
              var url=path_php+"lib/ContactBody.php?n="+name+"&e="+mail+"&o="+subj+"&t="+text+"&c="+code+"&k="+KeyRandom35.value;

                  xmlhttp.onreadystatechange=SendContactReplay;
                  xmlhttp.open("GET",url,true);
                  xmlhttp.send(null);
       }
function SendContactReplay()
       {
            // document.getElementById("TimeNOTIFY").innerHTML=xmlhttp.readyState;

        if(xmlhttp.readyState==4)
          {
               var return_code=xmlhttp.responseText;
               
               var return_value=return_code.split("&");
               
                if(return_value[0]=="OK")     
                  {
                //   document.getElementById("MessageCODE").innerHTML=return_value[1]+":"+return_value[2];

                var pageINP=document.getElementById("PageInput"); 

                    pageINP.action          ="index.php";
                    pageINP.target          ="_self";
                    pageINP.NextPage.value  ="Contact_Send";
                    pageINP.ServerPage.value="Contact";
                    pageINP.LogTable.value  ="cnttinfo";
                    pageINP.LogKey.value    =return_value[1]+"&"+return_value[2];
                    pageINP.LogPage.value   ="contact message sended";
                    pageINP.submit();        

                  /*
                   *
                    PageInput.action          ="index.php";
                    PageInput.target          ="_self";
                    PageInput.NextPage.value  ="Contact_Send";
                    PageInput.ServerPage.value="Contact";
                    PageInput.LogTable.value  ="cnttinfo";
                    PageInput.LogKey.value    =return_value[1]+"&"+return_value[2];
                    PageInput.LogPage.value   ="contact message sended";
                    PageInput.submit();        
                   *
                   */
                  }
              else
                  {
                   document.getElementById("MessageCODE").innerHTML=xmlhttp.responseText;
                  }
          }
       } 

function SendProcess()
       {
             $("MessageName").innerHTML="";
             $("MessageMail").innerHTML="";
             $("MessageMailAT").innerHTML="";
             $("MessageSubj").innerHTML="";
             $("MessageText").innerHTML="";
             $("MessageCODE").innerHTML="";
             
         var  err_counter=0,
               at_counter=0,
              dot_counter=0,
             name=trim($("ContactName").value),   
             mail=trim($("ContactEmail").value),
             subj=trim($("ContactObject").value),
             text=trim($("ContactText").value),
             code=trim($("ContactCode").value);
             
         for(var c=0;c<mail.length;c++) { if(mail.charAt(c)=="@") {  at_counter++; } }
         for(var c=0;c<mail.length;c++) { if(mail.charAt(c)==".") { dot_counter++; } }

          if(name=="") { err_counter++; $("MessageName").innerHTML="DIGITARE"; }
          if(mail=="") { err_counter++; $("MessageMail").innerHTML="DIGITARE"; }
          if(subj=="") { err_counter++; $("MessageSubj").innerHTML="DIGITARE"; }
          if(text=="") { err_counter++; $("MessageText").innerHTML="DIGITARE"; }
          if(code=="") { err_counter++; $("MessageCODE").innerHTML="DIGITARE il codice"; }

          if( at_counter!=1
          || dot_counter==0) { err_counter++; $("MessageMailAT").innerHTML="NON VALIDO"; }

          if(err_counter==0) { SendContactBody(name,mail,subj,text,code); }
       } 

var GetSearch=function(p,id){  var q="";    

                    if(p=="cerca"){q=id; Cache.search.query=q;}
                             else {q=page_href[id.substr(4,(id.length-4))]; } // console.log(id+" | "+q);
                
  $("MainCenterPage").innerHTML="<div style='margin-top:100px;text-align:center;'><img src='img/wait/loading.gif'/></div>";
           xmlhttp=GetXmlHttpObject();
        if(xmlhttp==null) { alert ("Your browser does not support XMLHTTP!"); return; }

                          var url="lib/search[result].php?p="+p+"&y="+q+"&t="+GetKeyRandom10();
                          
                          console.log(url);

           xmlhttp.onreadystatechange=PutSearch;
           xmlhttp.open("GET",url,true);
           xmlhttp.send(null);
}  // END-function < GetSearch >

var PutSearch=function(){                          
                                          // document.getElementById("MessageNOTIFY").innerHTML=xmlhttp.readyState;
        if(xmlhttp.readyState==4)
          {
               var  TopPage="<div id='Search_qry'><input id='Search_Value' value='"+Cache.search.query+"'/>"
                           +"                     <img id='Search_BTN' src='img/ico/search[22].png'/></div>";
                           
            var    NextPage="<div id='Search_nav'><table id='Search_pag'><tr>",
                SnippetPage="<div id='Search_res'>";

                       page_href={};  //  re-new object LINK href

                     var result_item= JSON.parse(xmlhttp.responseText);
                     
                                                 var nav_page_counter=0, nav_page_0=0, n=0;
           for(var i=0;i<result_item.length;i++) 
              { if(result_item[i].type=="NAVPAGE") { nav_page_counter++; } }

           for(var i=0;i<result_item.length;i++) 
              {
                if(result_item[i].type=="HEADER")
                  {
                   SnippetPage+="<div class='Search_empty'>"+result_item[i].hdr+"</div>";
                  }

                if(result_item[i].type=="SNIPPET")
                  {
                   SnippetPage+="<div class='Search_cap'><a class='Search_hdr' href='"+result_item[i].href+"' target='_blank'>"+result_item[i].hdr+"</a></div>"
                              + "<div class='Search_tit'>"+result_item[i].tit+"</div>"
                              + "<div class='Search_lnk'>"+result_item[i].href+"</div>";
                  }

                if(result_item[i].type=="NAVPAGE")
                  { n++;  if(nav_page_0==0) {nav_page_0=i;}
                                                                       var link_style="next_page";
                                            if(result_item[i].href=="-") { link_style="curr_page"; }
                   if(nav_page_counter>13 
                   && ( n==8 || n==14 || n==20 || n==26 )
                   && result_item[i].tit!="Avanti"
                   && result_item[nav_page_0].tit=="Indietro")
                     {
                      NextPage+="</tr><tr><td></td>";
                     }
                   if(nav_page_counter>13 
                   && result_item[i].tit=="Avanti"
                   && n>14 )
                     {
                      NextPage+="<td></td><td></td><td></td><td></td><td></td><td></td>";
                     }


                      NextPage+="<td class='Search_nav'><span id='page"+result_item[i].hdr+"' class='"+link_style+"'>"+result_item[i].tit+"</span></td>";
                      
                      page_href[result_item[i].hdr]=result_item[i].href;
                  }
              }
                      NextPage+="</tr></table></div>";  // END-table < Search_pag >  END-div < Search_nav >

             $("MainCenterPage").innerHTML=    TopPage
                                          +   NextPage
                                          +SnippetPage
                                          +"<div style='height:10px'></div>"
                                          +"</div>"                              // END-div < Search_res >
                                          +"<div style='height:10px'></div>";

                   //page_href.url={}; 

           for(var h in page_href)
              {                                  // console.log(h+" | "+page_href[h]);
                if(page_href[h]!="-")
                  {              
                   //page_href.url["page"+h]=page_href[h];

                   $("page"+h).onclick=function	(){GetSearch("pagina",this.id); }  //  console.log(h+" | "+page_href[h]);
                   $("page"+h).title  =page_href[h];

                   // console.log("$(\"page"+h+"\") | "+page_href[h]);
                  }
              }

             $("Search_Value").onkeydown  =function(e){    
                                                       if(!e){ var e = window.event;} 
                                                       if(e.keyCode==13) { GetSearch("cerca",this.value); } 
                                                      }
             $("Search_BTN").onclick      =function() { GetSearch("cerca",$("Search_Value").value); } 
             $("Search_BTN").onmousedown  =function() { this.style.top="9px";this.style.left="14px";}                                                        
             $("Search_BTN").onmouseup    =function() { this.style.top="8px";this.style.left="16px";}                                                        
          }
}  // END-function < PutSearch >

