var resultsRealisateurs = new Array();
var resultsActeurs = new Array();
var resultsPays = new Array();
var resultsTitres = new Array();

    function getLeft(MyObject)
    //Fonction permettant de connaître la position d'un objet
    //par rapport au bord gauche de la page.
    //Cet objet peut être à l'intérieur d'un autre objet.
    {
        if (MyObject.offsetParent)
            return (MyObject.offsetLeft + getLeft(MyObject.offsetParent));
        else 
            return (MyObject.offsetLeft);
    } 
        
    function getTop(MyObject)
    //Fonction permettant de connaître la position d'un objet
    //par rapport au bord haut de la page.
    //Cet objet peut être à l'intérieur d'un autre objet.
    {
        if (MyObject.offsetParent)
            return (MyObject.offsetTop + getTop(MyObject.offsetParent));
        else
            return (MyObject.offsetTop);
    }

    function getHeight(MyObject) 
    { 
        return MyObject.offsetHeight; 
    } 
    
    function setHeight(MyObject,h)
    {
        //MyObject.offsetHeight=h; 
        MyObject.style.height=h;
    }
                       
    function getWidth(MyObject) 
    { 
        return MyObject.offsetWidth; 
    } 
    
    function SetVisibleElement(element,visible)
    {
        obj=document.getElementById(element);
        SetVisible(obj,visible);        
    }
    
    function SetVisible(MyObject,visible)
    {
        if(MyObject!=null)
        {
            if(visible)
            {            
                MyObject.style.display="block";                    
            }
            else
            {
                MyObject.style.display="none";        
            }
        }
    }

    function GetHTMLDefault(Type)
    {
        HTML='<table  style="width:235px;font-family: arial;font-size:10px">';
        HTML=HTML+'<tr>';
        HTML=HTML+'<td style="width:14px;"><img src="images/arrowup.gif" style="border:none;left:10px;"></td>';
        HTML=HTML+'<td style="color:black;">&nbsp;Saisir les trois premières lettres du :</td>';
        HTML=HTML+'</tr>';
        HTML=HTML+'<tr>';
        HTML=HTML+'<td></td>';
        HTML=HTML+'<td>';
        //HTML=HTML+'<li style="list-style-image: url(images/square.gif);">&nbsp;Nom dun réalisateur</li>';
        if(Type==1)
        {
            HTML=HTML+'<li style="list-style-image: url(images/square.gif);color:black;">&nbsp;Nom d\'un réalisateur</li>';
        }
        else
        if(Type==2)
        {
            HTML=HTML+'<li style="list-style-image: url(images/square.gif);color:black;">&nbsp;Nom d\'un acteur</li>';
        }
        else
        if(Type==3)
        {
            HTML=HTML+'<li style="list-style-image: url(images/square.gif);color:black;">&nbsp;Nom d\'un pays</li>';
        }
        else
        if(Type==4)
        {
            HTML=HTML+'<li style="list-style-image: url(images/square.gif);color:black;">&nbsp;Titre du film</li>';
        }
        HTML=HTML+'</td>';
        HTML=HTML+'</tr>';    
        HTML=HTML+'<tr>';
        HTML=HTML+'<td></td>';
        HTML=HTML+'<td style="text-align:right;">';
        HTML=HTML+'<a href="#">Fermer</a>';
        HTML=HTML+'</td>';
        HTML=HTML+'</tr>';
        HTML=HTML+'</table>';
        return HTML;
    }

    function GetInfoDiv(Type)
    {
        if(Type==1)
        {
            return document.getElementById("RealisateurInfoDiv");
        }
        else
        if(Type==2)
        {
            return document.getElementById("ActeurInfoDiv");
        }
        else
        if(Type==3)
        {
            return document.getElementById("PaysInfoDiv");
        }
        else
        if(Type==4)
        {
            return document.getElementById("TitreInfoDiv");
        }
        return null;        
    }
    
    function GetInputTextValue(Type)
    {
        if(Type==1)
        {               
            return document.getElementById('realisateurText').value;
        }
        else
        if(Type==2)
        {               
            return document.getElementById('acteurText').value;
        }
        else
        if(Type==3)
        {               
            return document.getElementById('paysText').value;
        }        
        else
        if(Type==4)
        {               
            return document.getElementById('titre_fr').value;
        }        
        return null;
    }
    
    function SetInputTextValue(Type,valeur)
    {           
        if(Type==1)
        {               
            valeur=valeur.replace(/%20/g,' ');            
            document.getElementById('realisateurText').value=valeur;
        }
        else
        if(Type==2)
        {               
            valeur=valeur.replace(/%20/g,' ');            
            document.getElementById('acteurText').value=valeur;
        }
        else
        if(Type==3)
        {               
            valeur=valeur.replace(/%20/g,' ');            
            document.getElementById('paysText').value=valeur;
        }                
        else
        if(Type==4)
        {   
            i=valeur.indexOf('{');            
            no=valeur.substr(i+1,valeur.length-i-2);
            valeur=valeur.substr(0,i);
            
            valeur=valeur.replace(/%20/g,' ');            
            document.getElementById('titre_fr').value=valeur;
            document.getElementById('titre_no').value=no;
        }                
    }
    
    function HideOthers(Type)
    {
        if(Type==1)
        {
            SetVisible(GetInfoDiv(2),false);
            SetVisible(GetInfoDiv(4),false);
        }
        if(Type==2)
        {
            SetVisible(GetInfoDiv(1),false);
            SetVisible(GetInfoDiv(4),false);
        }
        if(Type==4)
        {
            SetVisible(GetInfoDiv(1),false);
            SetVisible(GetInfoDiv(2),false);                
        }                                               
    }
    
    function ShowInfoDiv(sender,Type)
    {
        Div=GetInfoDiv(Type);
    
        if(Div!=null)        
        {
            HideOthers(Type)
                           
            DivLeft=getLeft(sender);
            DivTop=getTop(sender);        
            Div.style.left=DivLeft+'px';
            Div.style.top=DivTop+sender.offsetHeight+'px';
            
            NomRecherche=GetInputTextValue(Type);
            
            if(NomRecherche.length<3)
            {
                Div.innerHTML=GetHTMLDefault(Type);                                
            }
            else
            {
                processKeyEvent(null,Type);                    
            }
            SetVisible(Div,true);
            ShowOrHideComboBoxs(Div);
        }
    }

    function HideInfoDiv(Type)
    {
        Div=GetInfoDiv(Type);
        if(Div!=null)
        {
            NomRecherche=GetInputTextValue(Type);        
            if(NomRecherche.length<3)
            {
                SetVisible(Div,false);
                ShowOrHideComboBoxs(Div);
            }
        }
    }

    function ShowValue(TitreAndId,Type)
    {   
        url=TitreAndId;//sender.href;
        //i=url.indexOf('###');
        //url=url.substr(i+2);
        //alert(url);
        SetInputTextValue(Type,url);
        Div=GetInfoDiv(Type);    
        if(Div!=null)
        {
            SetVisible(Div,false);
            ShowOrHideComboBoxs(Div);
            document.frm_explorez.submit();
        }
    }

    function processRequest(Type) {
        fetchResults(Type); 
    }

    function fetchResults(Type) {
        nomRecherche=GetInputTextValue(Type);
        var hostname=window.location.hostname;
        if(hostname.toUpperCase()=='LOCALHOST')
        {
            hostname=hostname+"/videocentreville";
        }
        //var url = 'http://' + hostname + '/videocentreville/getResults.php?type=' + Type + '&search=' + nomRecherche;
        var url = 'http://' + hostname + '/getresults.php?type=' + Type + '&search=' + nomRecherche;
        //alert(url);
        var head = document.getElementsByTagName('head')[0];
        if(Type==1) { scriptTagName='psScriptRealisateur'; }
        else if(Type==2) { scriptTagName='psScriptActeur'; }
        else if(Type==3) { scriptTagName='psScriptPays'; }
        else if(Type==4) { scriptTagName='psScriptTitre'; }

        var scriptTag = document.getElementById(scriptTagName);
        
        if (scriptTag)
            head.removeChild(scriptTag);
                                    
        script = document.createElement('script');
        script.src = url;
        script.type = 'text/javascript';
        script.id = scriptTagName;
        
        head.appendChild(script);
    }

    function processData(Type) 
    {
        DivHtml='<table style="width:350px;font-family: arial;font-size:10px">';
        if(Type==1)
        {            
            if (resultsRealisateurs.length > 0) 
            {                   
                for (i=0;i<resultsRealisateurs.length;i++) 
                {
                    if(i==0)
                    {
                        html_line='<tr><td><a id=a_black href=# onClick="javascript:ShowValue(\''+resultsRealisateurs[i]+'\',1);return true;" >'+resultsRealisateurs[i]+'</a></td><td align="right"><a href="#" onClick="javascript:CloseInfoDiv(1);return false;">Fermer</a></td></tr>';
                    }
                    else
                    {
                        html_line='<tr><td colspan="2"><a id=a_black href=# onClick="javascript:ShowValue(\''+resultsRealisateurs[i]+'\',1);return true;" >'+resultsRealisateurs[i]+'</a></td></tr>';
                    }
                    DivHtml=DivHtml+html_line;
                }                
            }         
        }
        if(Type==2)
        {       
            if (resultsActeurs.length > 0) 
            {                                
                for (i=0;i<resultsActeurs.length;i++) 
                {
                    if(i==0)
                    {
                        html_line='<tr><td><a id=a_black href=# onClick="javascript:ShowValue(\''+resultsActeurs[i]+'\',2);return true;" >'+resultsActeurs[i]+'</a></td><td align="right"><a href="#" onClick="javascript:CloseInfoDiv(2);return false;">Fermer</a></td></tr>';
                    }
                    else
                    {
                        html_line='<tr><td colspan="2"><a id=a_black href=# onClick="javascript:ShowValue(\''+resultsActeurs[i]+'\',2);return true;" >'+resultsActeurs[i]+'</a></td></tr>';
                    }
                    DivHtml=DivHtml+html_line;
                }                                
            }            
        }
        if(Type==3)
        {               
            if (resultsPays.length > 0) 
            {                   
                for (i=0;i<resultsPays.length;i++) 
                {
                    if(i=0)
                    {
                        html_line='<tr><td><a id=a_black href=# onClick="javascript:ShowValue(\''+resultsPays[i]+'\',3);return true;" >'+resultsPays[i]+'</a></td><td align="right"><a href="#" onClick="javascript:CloseInfoDiv(3);return false;">Fermer</a></td></tr>';
                    }
                    else
                    {
                        html_line='<tr><td colspan="2"><a id=a_black href=# onClick="javascript:ShowValue(\''+resultsPays[i]+'\',3);return true;" >'+resultsPays[i]+'</a></td></tr>';
                    }
                    DivHtml=DivHtml+html_line;
                }                                                      
            }            
        }
        if(Type==4)
        {                    
            if (resultsTitres.length > 0) 
            {                
                for (i=0;i<resultsTitres.length;i++) 
                {                       
                    strTitre=resultsTitres[i];
                    j=strTitre.indexOf('{');                    
                    if(j>0)
                    {
                        strTitre=strTitre.substr(0,j);
                    }
                    
                    if(i==0)
                    {                    
                        html_line='<tr><td colspan="2"><a id=a_black href=# onClick="javascript:ShowValue(\''+resultsTitres[i]+'\',4);return true;" >'+strTitre+'</a></td><td align="right"><a href="#" onClick="javascript:CloseInfoDiv(4);return false;">Fermer</a></td></tr>';
                    }
                    else
                    {
                        html_line='<tr><td colspan="2"><a id=a_black href=# onClick="javascript:ShowValue(\''+resultsTitres[i]+'\',4);return true;" >'+strTitre+'</a></td></tr>';
                    }
                    DivHtml=DivHtml+html_line;
                }                                
            }            
        }        
        DivHtml=DivHtml+'</table>';                
        //alert(DivHtml);
        Div=GetInfoDiv(Type);
        Div.innerHTML=DivHtml;        
        ShowOrHideComboBoxs(Div);        
    }

    function ShowOrHideComboBoxs(Div)
    {
        ShowOrHideComboBox(Div,'pays');
        ShowOrHideComboBox(Div,'annee');
        ShowOrHideComboBox(Div,'format');
        ShowOrHideComboBox(Div,'categorie1');
        ShowOrHideComboBox(Div,'categorie2');
        ShowOrHideComboBox(Div,'categorie3');
        ShowOrHideComboBox(Div,'categorie4');
        ShowOrHideComboBox(Div,'categorie5');
        ShowOrHideComboBox(Div,'excategorie');
        ShowOrHideComboBox(Div,'langue');
        ShowOrHideComboBox(Div,'results_by_page');
    }
    
    function ShowOrHideComboBox(div,element)
    {
        DivHeight=getHeight(div);
        DivTop=getTop(div);
        obj=document.getElementById(element);        
        obj_tmp=document.getElementById(element+'_tmp');        
        if(getTop(obj)<DivHeight+DivTop && Div.style.display=="block") //+getHeight(obj)
        {
            //if(getTop(obj)+getHeight(obj)>DivHeight+DivTop)
            //{
            //    setHeight(div,getTop(obj)+getHeight(obj)-DivTop);
            //}
            SetVisible(obj,false);        
            SetVisible(obj_tmp,true);
        }
        else
        {
            SetVisible(obj,true);        
            SetVisible(obj_tmp,false);                        
        }
    }
    
    
    function processKeyEvent(event,Type)
    {
        nomRecherche=GetInputTextValue(Type);        
            
        if(nomRecherche.length<3)
        {
            Div=GetInfoDiv(Type);
            Div.innerHTML=GetHTMLDefault(Type);        
            ShowOrHideComboBoxs(Div);
            return;
        }
        
        processRequest(Type);       
    }
    
    function AddFilm(nofilm)//,titre)
    {           
        var hostname=window.location.hostname;
        //if(hostname.toUpperCase()!='LOCALHOST')
        //{
        //    hostname=hostname+":218";
        //}
        //var url = 'http://' + hostname + '/videocentreville/AddFilm.php?noFilm=' + nofilm;//+'&titre='+titre;
        var url = 'http://' + hostname + '/addfilm.php?noFilm=' + nofilm;//+'&titre='+titre;
        var head = document.getElementsByTagName('head')[0];
        scriptTagName='psScriptAddFilm';

        var scriptTag = document.getElementById(scriptTagName);
        
        if (scriptTag)
            head.removeChild(scriptTag);
                                    
        script = document.createElement('script');
        script.src = url;
        script.type = 'text/javascript';
        script.id = scriptTagName;
        
        head.appendChild(script);                                
    }
    
    /*function AddFilmDiv(nofilm,titre)    
    {
        link='<a href="#'+nofilm+'" onclick="PopupCentrer(\'fiche.php?noFilm='+nofilm+'\',760,600,\'toolbar=yes,status=no,left=0,top=0,scrollbars=yes,resizable=yes\')">';                 
    
        div=document.getElementById('a_voir');
        html=div.innerHTML;
        i=html.indexOf('<!--Debut-->');
        html=html.substr(0,i+'<!--Debut-->'.length)+
             '<tr><td>'+link+titre+'</a></td></tr>'+
             html.substr(i+'<!--Debut-->'.length);
        div.innerHTML=html;
    } */
    
    function AddFilmDiv(textShow)        
    {    
        div=document.getElementById('a_voir');
        html=div.innerHTML;
        i=html.indexOf('<!--Debut-->');
        j=html.indexOf('<!--Fin-->');
        html=html.substr(0,i+'<!--Debut-->'.length)+        
             textShow+
             html.substr(j);
        div.innerHTML=html;        
    }
  
    function setValue(element,valeur)
    {      
        MyObject=document.getElementById(element)
        if(MyObject!=null)
        {
            MyObject.value=valeur;
        }
    }
    
    function retablir()
    {
        setValue("titre_fr","");
        setValue("realisateur","");
        setValue("realisateurText","");
        setValue("acteur","");
        setValue("acteurText","");
        setValue("pays","");
        setValue("paysText","");
        setValue("annee","");
        setValue("format","");
        setValue("categorie1","");
        setValue("categorie2","");
        setValue("categorie3","");
        setValue("categorie4","");
        setValue("categorie5","");
        setValue("excategorie","");
        setValue("langue","");   
        setValue("titre_no","");           
        
        var div=GetInfoDiv(1);
        if(div!=null)
        {
            div.style.display="none";                     
        }
        div=GetInfoDiv(2);
        if(div!=null)
        {
            div.style.display="none";                     
        }
        div=GetInfoDiv(3);
        if(div!=null)
        {
            div.style.display="none";                     
        }
        div=GetInfoDiv(4);
        if(div!=null)
        {
            div.style.display="none";                     
        }        
        //placeIt();
    }
    
    function CloseInfoDiv(Type)
    {           
        var div=GetInfoDiv(Type);
        if(div!=null)
        {
            div.style.display="none";                     
        }                
        ShowOrHideComboBoxs(Div);
    }
    
    function PopupCentrer(page,largeur,hauteur,options) 
    {
        var top=(screen.height-hauteur)/2;
        var left=(screen.width-largeur)/2;
        window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
    } 
    
    function ClickAchetez(obj_a,img_name)
    {
        var img_obj=document.getElementById(img_name);
        //img_obj.src='images\\loading.gif';
        obj_a.title='Un instant svp...';
        
        ChangerImage(img_name,-1);
    }
    
    function ChangerImage(img_name,idx)
    {
        var img_obj=document.getElementById(img_name);
                    
        idx=idx+1;
        if(idx>30)
            idx=0;
        
        if(idx<=9)
            cero='0'
        else
            cero='';
        
        img_obj.src='images//loading//loading'+cero+idx+'.gif';        
                
        window.setTimeout("ChangerImage('"+img_name+"',"+idx+");",200);        
    }