	// Créé par Vincent Guesnard aka Kolibot ( contact@kolibot.fr )
	// Droits d'exploitation réservés à Sicesi
  // Dernière modification : 17/05/08

  function getFiches(data,fromHistory,hash,limit,nombre,order,tri,type) {
	// alert(data);
    if(data!=0) {
      if (fromHistory==0) {
        var pars = Form.serialize(data);
		// alert(pars);
        dsHistory.addFunction(function () { getFiches(pars,1); } );
      } else {
        var pars = data;
      }
    } else {
      var pars = 'hash='+hash+'&limit='+limit+'&nombre='+nombre+'&tri='+tri+'&order='+order+'&type='+type;
      dsHistory.addFunction(function () { getFiches(pars,1); } );
    }
  	var myAjax = new Ajax.Request(url_home+'ajax/fiches.php',
  				{ method : 'post', parameters : pars, onComplete : setFiches });
	  $('fiches').innerHTML ='<p style="text-align:center;margin-top:100px;"><img src="http://www.prix-portables.fr/images/ajax-loader2.gif" alt="Chargement en cours" /></p>';
  }
  function setShopbotPannel(merchantcount,url,marque,nom,pricemin,lastsell) {
    if(merchantcount!=0) {
			if(merchantcount == 1) { pannel_temp = '1 magasin';	} else { pannel_temp = merchantcount + ' magasins'; }
			pannel =  '<a href="' + url + '.html" title="Comparer les prix ' + marque + ' ' + nom + '" style="color:#6CB212"><span class="shopbot_price"><span style="font-size:13px">&agrave; partir de </span><br /><strong>' + pricemin + '&euro;</strong></span></a><br /><a href="' + url + '.html" title="Comparer les prix ' + marque + ' ' + nom + '" style="color:#6CB212"><span class="shopbot_price">Comparer</span></a><br /><span style="color: #747474;font-size:10px">dans ' + pannel_temp + '</span>';
			} else {
				if(lastsell==0) { pannel_temp = 'Prix bient&ocirc;t<br /> mis &agrave; jour'; } else { pannel_temp = 'Plus de prix <br />depuis le<br />' + lastsell; }
				pannel = '<a href="' + url + '/description.html" title="Description ' + marque + ' ' + nom + '" style="color:#6CB212"><span class="shopbot_price">' + pannel_temp + '</span></a>';
			}
			return pannel;
  }
 	function set_Pannel_Table_Content(url,marque,nom,photo,desc,noteavg,notecount,pannel_shopbot) {
		return '<tr><td class="comparo_table_img"><a href="' + url + '.html" title="Comparer les prix ' + marque + ' ' + nom + '"><img src="http://www.prix-portables.fr/inc/vignette.php?file=../images/fiches/' + photo + '" alt="Image du portable" /></a></td><td class="comparo_table_description"><a href="' + url + '.html" title="Comparer les prix ' + marque + ' ' + nom + '"><span class="title_laptop_comparo">' + marque + ' ' + nom + '</span></a><br />' + desc + '<br />' + URL_Image_Note(noteavg) + '<span style="vertical-align:middle;padding-top:1px">' + notecount + ' avis</span></td><td class="comparo_table_shopbot">' + pannel_shopbot + '</td></tr>';
	}
	function URL_Image_Note(note) {
		if(note == 0.0) { note = '0.0'; } else if(note > 0.0 && note <= 0.5) { note = '0.5'; } else if(note > 0.5 && note <= 1.0) { note = '1.0'; } else if(note > 1.0 && note <= 1.5) { note = '1.5'; } else if(note > 1.5 && note <= 2.0) { note = '2.0'; } else if(note > 2.0 && note <= 2.5) { note = '2.5'; } else if(note > 2.5 && note <= 3.0) { note = '3.0'; } else if(note > 3.0 && note <= 3.5) { note = '3.5'; } else if(note > 3.5 && note <= 4.0) { note = '4.0'; } else if(note > 4.0 && note <= 4.5) { note = '4.5'; } else if(note > 4.5 && note <= 5.0) { note = '5.0'; } 
		return '<img src="http://www.prix-portables.fr/images/front/stars/petites/' + note + '.gif" class="etoile" alt="note : ' + note + '/5" />';       
	}
	function set_Pannel(pannel) {
		return '<table class="tableresults" cellspacing="1">' + pannel + '</table>';
	}
	function url_decode(string) {
    return unescape(String(string).replace(/\+/g, " "));
  }
	function setPaginationTop(hash,total_fiche,limit,nombre,typer) {
	  if(total_fiche<nombre) { nombre = total_fiche; }
	  endnb = limit + nombre;
	  if(endnb>total_fiche) { endnb = total_fiche; }
    return '<tr class="rowresults"><th colspan="3"><div style="float:right">Trier par <strong>Prix </strong><a href="" title="Du plus petit prix au plus grand prix" style="text-decoration:none;" onclick="getFiches(0,0,\'' + hash + '\',0,15,\'prix\',\'ASC\',' + typer + ');return false;">&nbsp;&#8593;&nbsp;</a><a href="" title="Du plus grand prix au plus petit prix"  style="text-decoration:none;" onclick="getFiches(0,0,\'' + hash + '\',0,15,\'prix\',\'DESC\',' + typer + ');return false;">&nbsp;&#8595;&nbsp;</a><strong> Nom </strong><a href=""  style="text-decoration:none;" onclick="getFiches(0,0,\'' + hash + '\',0,15,\'nom\',\'ASC\',' + typer + ');return false;">&nbsp;&#8593;&nbsp;</a><a href=""  style="text-decoration:none;" onclick="getFiches(0,0,\'' + hash + '\',0,15,\'nom\',\'DESC\',' + typer + ');return false;">&nbsp;&#8595;&nbsp;</a><strong> Popularit&eacute; </strong><a href=""  style="text-decoration:none;" onclick="getFiches(0,0,\'' + hash + '\',0,15,\'note\',\'ASC\',' + typer + ');return false;">&nbsp;&#8593;&nbsp;</a><a href=""  style="text-decoration:none;" onclick="getFiches(0,0,\'' + hash + '\',0,15,\'note\',\'DESC\',' + typer + ');return false;">&nbsp;&#8595;&nbsp;</a><strong> Marque </strong><a href=""  style="text-decoration:none;" onclick="getFiches(0,0,\'' + hash + '\',0,15,\'marque\',\'ASC\',' + typer + ');return false;">&nbsp;&#8593;&nbsp;</a><a href=""  style="text-decoration:none;" onclick="getFiches(0,0,\'' + hash + '\',0,15,\'marque\',\'DESC\',' + typer + ');return false;">&nbsp;&#8595;&nbsp;</a></div><div style="float:left">R&eacute;sultats des offres ' + eval(limit+1) + ' &agrave; ' + endnb + ' sur ' + total_fiche + '</div></th></tr>';
  }
  function setPaginationBottom(total_fiche,limit,nombre,hash,tri,order,typer) {
    nbpage=Math.ceil(total_fiche/nombre);
    pagecourante=Math.ceil(limit/nombre)+1;
    var chaine = '';
    if(limit > 0) { chaine = '<div style="float:left"><a href="" onclick="getFiches(0,0,\'' + hash + '\',' + eval(limit-nombre) + ',' + nombre + ',\'' + order + '\',\'' + tri + '\',' + typer + ');return false;" style="font-weight:bold">&laquo; Page pr&eacute;c&eacute;dente</a></div>'; }
    if(pagecourante<nbpage) { chaine += '<div style="float:right"><a href="" onclick="getFiches(0,0,\'' + hash + '\',' + eval(limit+nombre) + ',' + nombre + ',\'' + order + '\',\'' + tri + '\',' + typer + ');return false;" style="font-weight:bold">Page suivante &raquo;</a></div>'; }
    chaine += '<div align="center">Page : ';
    for(i=0;i<=nbpage-1;i++) {
      limit = (i*nombre);
      if(i != (nbpage-1)) { sep = ', '; } else { sep = ''; }
      j = i+1;
      if(j==pagecourante) {
  			chaine += ' '+j+''+sep+'';
  			bool = false;
      } else if((j<=3)||(j>=nbpage-2)||(j==pagecourante-1)||(j==pagecourante+1)) {
  	    chaine += '<a href="" onclick="getFiches(0,0,\'' + hash + '\',' + eval((j-1)*nombre) + ',' + nombre + ',\'' + order + '\',\'' + tri + '\',' + typer + ');return false;">'+j+'</a>'+sep+' '; 
        bool = false;
      } else if((j==4)&&((pagecourante<4)||(pagecourante>5))) {
  			if(!bool) { chaine += ' ... ';bool = true; }
  		} else if((j==nbpage-3)&&((pagecourante>nbpage-3)||(pagecourante<nbpage-4))) {
  			if(!bool) { chaine += ' ... ';bool = true; }
  		}
    }
    return ('<tr class="rowresults"><th colspan="3">'+chaine + '</div></th></tr>');
  }
  
  function marquepage(title,url){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
	}
  
  function setFiches(obj) {
    var fiches;
    total_fiche = eval(obj.responseXML.getElementsByTagName('fichescount')[0].firstChild.nodeValue);
	admin = obj.responseXML.getElementsByTagName('admin')[0].firstChild.nodeValue;
    if(admin==1){
      $('requete').innerHTML = url_decode(obj.responseXML.getElementsByTagName('requete')[0].firstChild.nodeValue);
    }
    fiches = obj.responseXML.getElementsByTagName('fiche');
	if(total_fiche>0) {
      typer = obj.responseXML.getElementsByTagName('typer')[0].firstChild.nodeValue;
      if(typer!=4) {
        hash = obj.responseXML.getElementsByTagName('hash')[0].firstChild.nodeValue;
      } else {
        hash = '';
      }
	  limit = eval(obj.responseXML.getElementsByTagName('limit')[0].firstChild.nodeValue);
      nombre = eval(obj.responseXML.getElementsByTagName('nombre')[0].firstChild.nodeValue);
      tri = obj.responseXML.getElementsByTagName('tri')[0].firstChild.nodeValue;
      order = obj.responseXML.getElementsByTagName('order')[0].firstChild.nodeValue;
      bool = true;
	  if(typer==1) {
        pathname = 'Recherche par crit&egrave;res';
        pathurl = 'filtres/criteres/'+hash;
      } else if(typer==2) {
        pathname = 'Recherche via l\'assistant';
        pathurl = 'filtres/assistant/'+hash;
      } else if(typer==3) {
		pathname = 'Recherche par mots cl&eacute;s';
		keywords_tmp = obj.responseXML.getElementsByTagName('keywords')[0].firstChild.nodeValue;
		pathurl = 'recherche/'+keywords_tmp;
		$('search_key').setAttribute('value', url_decode(keywords_tmp));
      } else {
        pathname = 'Index';
        bool = false;
      }
	 if(bool) {
        if(order=='note' && tri == 'DESC' && limit==0 && nombre==15) {
          full_url =  pathurl + '.html';
        } else {
          full_url =  pathurl + '/tri/'+order+'/'+tri+'/Page'+limit+','+nombre+'.html';
        }
        $('special_pathway').innerHTML = ' &gt; <a href="' + url_home + full_url + '"> ' + pathname + '</a> &gt; Comparateur de Prix';
      } else {
        if(order=='note' && tri == 'DESC' && limit==0 && nombre==15) {
          full_url =  'index.html';
        } else {
          full_url =  'tri/'+order+'/'+tri+'/Page'+limit+','+nombre+'.html';
        }
        $('special_pathway').innerHTML = ' &gt; Comparateur de Prix';
      }
      if(typer==2) {
        pathname = 'Recherche via lassistant';
      }
	  $('geturl').innerHTML = '<a href="' + url_home + full_url + '">Obtenir l\'adresse de cette page</a>';
	  //$('geturl').innerHTML = '<a href="javascript:marquepage(' + url_home + full_url + ')">Enregistrez votre recherche</a>';
	  //$('geturl').innerHTML = '<a href="javascript:if(document.all)window.external.AddFavorite(+ url_home + full_url +); else if(window.sidebar)window.sidebar.addPanel (+ url_home + full_url +);">Enregistrez votre recherche</a>';
      var pannel = '';
      for(x = 0; x < fiches.length; x++) {
        id = fiches[x].getElementsByTagName('id')[0].firstChild.nodeValue;
        nom = url_decode(fiches[x].getElementsByTagName('nom')[0].firstChild.nodeValue);
        marque = url_decode(fiches[x].getElementsByTagName('marque')[0].firstChild.nodeValue);
        type = url_decode(fiches[x].getElementsByTagName('type')[0].firstChild.nodeValue);
        photo = url_decode(fiches[x].getElementsByTagName('photo')[0].firstChild.nodeValue);
        noteavg = fiches[x].getElementsByTagName('noteavg')[0].firstChild.nodeValue;
        notecount = fiches[x].getElementsByTagName('notecount')[0].firstChild.nodeValue;
        pricemin = fiches[x].getElementsByTagName('pricemin')[0].firstChild.nodeValue;
        desc = url_decode(fiches[x].getElementsByTagName('desc')[0].firstChild.nodeValue);
        merchantcount = fiches[x].getElementsByTagName('merchantcount')[0].firstChild.nodeValue;
        lastsell = url_decode(fiches[x].getElementsByTagName('lastsell')[0].firstChild.nodeValue);
        url = url_decode(fiches[x].getElementsByTagName('url')[0].firstChild.nodeValue);
        pannel_shopbot = setShopbotPannel(merchantcount,url,marque,nom,pricemin,lastsell);
        pannel += set_Pannel_Table_Content(url,marque,nom,photo,desc,noteavg,notecount,pannel_shopbot);
      }
	  pannel = setPaginationTop(hash,total_fiche,limit,nombre,typer) + pannel + setPaginationBottom(total_fiche,limit,nombre,hash,tri,order,typer);
      $('fiches').innerHTML = set_Pannel(pannel);
	  
    } else {
      $('fiches').innerHTML = '<p style=\'text-align:center;color:rgb(191,0,0);font-weight:bold;\'>Aucun portable ne correspond &agrave; vos crit&egrave;res de recherche.<br />Conseil : ne cochez que les options indispensables &agrave; vos yeux.</p>';
    }
	
    //refresh('pub2','OAS_RICH(\'Top\')');
  }  
  function refresh(id,js) {
    $(id).innerHTML = '';
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.language = 'Javascript';
    if(null == script.canHaveChildren || script.canHaveChildren) { 
      script.appendChild(document.createTextNode(js));
    } else { 
      script.text = js; 
    }
    var a = document.getElementById(id);
    a.appendChild(script);
  }
