
<script type="text/javascript">
<!-- //
var urlLoadCategory = "./index.php?"+pmvUrlGetUrlParameters ()+"&cat=";
var urlLoadStatValues = "./index.php?"+pmvUrlGetUrlParameters ()+"&stat=";
// All translate constants
var pdo_select_combo = "{'pdo_select_combo'|translate}";
var pdo_home_link = "{'pdo_home_link'|translate}";
var pdo_no_visit = "{'aucunvisiteur_titre'|translate}";
// Set var for stat
var page_stat_name = "{'pdo_title'|translate}";

var tabCategory = new Array;
i=0;
{foreach from=$InitKey item=raw name=boucle key=keyChapter}
tabCategory[i] = new Array;
tabCategory[i++][0] = '{$raw}';
{/foreach}
i=0;
{foreach from=$InitLib item=raw name=boucle key=keyChapter}
tabCategory[i++][1] = '{$raw}';
{/foreach}
{if $InitKey}
  var xmlhttp = getHTTPObject(urlLoadCategory+tabCategory[i-1][0], 'cat');
  loadStatValues ();
{/if}


{literal}
// From this point : no smarty code, so we can use { and } characters
function getHTTPObject(p_url, p_typeResponse) {
	var xmlhttp = false;
	/* Compilation conditionnelle d'IE */
		/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	@else
	xmlhttp = false;
	@end @*/
	/* on essaie de creer l'objet si ce n'est pas deja  fait */
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlhttp = false;
		}
	}
	if (xmlhttp) {
		/* on definit ce qui doit se passer quand la page repondra */
		xmlhttp.open("GET",p_url,true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200 || xmlhttp.status == 304 ) {
				  if (window.ActiveXObject){
					vXMLDoc = new ActiveXObject("Microsoft.XMLDOM");
					vXMLDoc.async = false;
					vXMLDoc.loadXML(xmlhttp.responseText);
				  } else {
				    var vParser = new DOMParser();
					vXMLDoc = vParser.parseFromString(xmlhttp.responseText, "text/xml");
				  }
				
					switch ( p_typeResponse ) {
						case 'cat':
							updateCategoryList(vXMLDoc);
							break;
						case 'stat':
							updateStatValues(vXMLDoc);
							break;
					}
				}
			}
		}
		xmlhttp.send( null );
	}
	return xmlhttp;
}


function updateCategoryList(p_xml) {
  // Empty select list
  var v_list = document.getElementById("category");
  while (v_list.options.length > 0) {
  	v_list.options[0] = null;
  }
  var a = new Option (pdo_select_combo);
  v_list.options[v_list.options.length] = a;
  
  var allRows = p_xml.getElementsByTagName("item");
  for (var i=0; i<allRows.length; i++) {
    if (allRows[i].getElementsByTagName("complete_name").length > 0) {
      id = allRows[i].getElementsByTagName("idcategory")[0].childNodes[0].nodeValue;
	  a = new Option (
	  			allRows[i].getElementsByTagName("complete_name")[0].childNodes[0].nodeValue+" ("+id+")",
	  			id);
	}
	else {
	  id = allRows[i].getElementsByTagName("idpage")[0].childNodes[0].nodeValue;
      a = new Option (
	  			allRows[i].getElementsByTagName("name")[0].childNodes[0].nodeValue+" ("+id+")",
	  			id);
	}
  	v_list.options[v_list.options.length] = a;
  }
  writeLevel ();
}

function updateStatValues(p_xml) {
  // Empty select list
  var ch="";
  var allRows = p_xml.getElementsByTagName("dates");
  ch = "<table border=1>"

  if (allRows.length >0) {
	  var allDate = allRows[0].getElementsByTagName("item");
	  ch += "<tr>";
	  ch+="<td></td>";
	  for (var i=0; i<allDate.length; i++) {
	  	ch+="<td>"+allDate[i].childNodes[0].nodeValue+"</td>";
	  }
	  ch += "</tr>"
  }
  
  var trouve = false;
  for (i=0; i < tabCategory.length; i ++) {
    trouve = false;
	allRows = p_xml.getElementsByTagName(tabCategory[i][0]);
    if (allRows.length > 0) {
	  allSum = allRows[0].getElementsByTagName("sum");
	  if (allSum.length > 0) {
	    trouve = true;
		ch += "<tr>";
	    ch+="<td>"+allSum[0].getElementsByTagName("data")[0].childNodes[0].nodeValue+"</td>";
	    ch+="<td>"+allSum[0].getElementsByTagName("sum")[0].childNodes[0].nodeValue+"</td>";
	    ch+="<td>"+allSum[0].getElementsByTagName("percentn1")[0].childNodes[0].nodeValue
	    	+ " ("+allSum[0].getElementsByTagName("sumn1")[0].childNodes[0].nodeValue +")</td>";
	    ch+="<td>"+allSum[0].getElementsByTagName("percentn2")[0].childNodes[0].nodeValue
	    	+ " ("+allSum[0].getElementsByTagName("sumn2")[0].childNodes[0].nodeValue +")</td>";
		ch += "</tr>";
	  }
    }
    if (! trouve) {
		ch += "<tr>";
	    ch+="<td>"+tabCategory[i][1]+"</td>";
	    ch+="<td colspan='3'>"+pdo_no_visit+"</td>";
		ch += "</tr>";
    }
  }
  ch += "</table>"
  
  document.getElementById("statVal").innerHTML = ch;
}

function loadStatValues () {
  var v_ch = "";
  var i;
  if (tabCategory.length > 0) {
    v_ch = tabCategory[0][0];
    for (i=1; i < tabCategory.length; i ++) {
      v_ch += ","+tabCategory[i][0];
    }
    pmvUrlAddParameter ('init', v_ch);
	//window.open (urlLoadStatValues + v_ch, "LoadStat");
	var xmlhttp = getHTTPObject(urlLoadStatValues + v_ch, 'stat');
  }
  else {
    document.getElementById("statVal").innerHTML = "";
  }
}

function loadCategory (p_SelectElem) {
  var v_elem = p_SelectElem.options[p_SelectElem.options.selectedIndex];
  var v_sel = v_elem.value;
  var v_txt = v_elem.text;
  var v_nbCat = tabCategory.length;
  tabCategory[v_nbCat] = new Array;
  tabCategory[v_nbCat][0] = v_sel;
  tabCategory[v_nbCat][1] = v_txt;
  //window.open(urlLoadCategory+v_sel);
  var xmlhttp = getHTTPObject(urlLoadCategory+v_sel, 'cat');
  loadStatValues ();
}

function loadPreviousCategory(p_pos) {
  var v_cat = -1;
  if (p_pos >= 0) {
  	v_cat = tabCategory[p_pos][0];
  }
  var i = 0;
  tabCategory.length = p_pos+1;
  var xmlhttp = getHTTPObject(urlLoadCategory+v_cat, 'cat');
  loadStatValues ();
}

function writeLevel () {
  var v_level = document.getElementById("level");
  var i =0;
  var v_ch = "";
  if (tabCategory.length > 0) {
    v_ch += "<a href=\"javascript:loadPreviousCategory(-1)\">"+pdo_home_link+"</a>&nbsp; > &nbsp;";
  }
  for (i=0; i < tabCategory.length; i++) {
    if (i < tabCategory.length-1) {
      v_ch += "<a href=\"javascript:loadPreviousCategory("+i+")\">"+tabCategory[i][1]+"</a>&nbsp; > &nbsp;";
	}
	else {
      v_ch += tabCategory[i][1];
	}
  }
  v_level.innerHTML = v_ch;
}

{/literal}
// -->
</script>
<H1>{'pdo_title'|translate}</H1>
<table id="centrer" border=1>
<tr>
<td><p>{'pdo_lib_level'|translate}</p></td>
<td><p id="level"></p></tr>
<tr>
<td><p>{'pdo_lib_cat'|translate}</p></td>
<td><select id="category" name="category" onchange="loadCategory(this)">
	<option>{'pdo_select_combo'|translate}</option>
{foreach from=$FirstCategoryList item=raw name=boucle key=keyChapter}
	{if $raw.complete_name}
		<option value="{$raw.idcategory}">{$raw.name} ({$raw.idcategory})</option>
	{else}
		<option value="{$raw.idpage}">{$raw.name} ({$raw.idpage})</option>
	{/if}
{/foreach}
    </select>
</tr>
<tr>
<td><p>{'pdo_lib_stat'|translate}</p></td>
<td id="statVal"></tr>
<tr></table>