/*
 * Created on 8 avr. 2008
 *plugin : tx_hporecherche_pi1
 * formulaire de recherche
 */
 
 var g_hpo_recherche_xhr 	= null;
 var g_with_combo 			= false;
 var g_navigator			= ''; 
 var g_debug_ie6			= false;
 var dim_type 				= 0; 
 var dim_rub  				= 0; 
 var dim_year 				= 0; 
 var dim_mag  				= 0; 



 function hide_show_search(val){
 	
 	if ( !val ) return;

 	if ( val == 1 ){ 	
 		document.getElementById("themes_hporecherche_pi1").style.display = 'block'; 	
 	}
 	else{ 		
 		document.getElementById("themes_hporecherche_pi1").style.display = 'none';
 	} 	
 }

 function toggleSearch()
 {
 	if ( document.getElementById("advanced_search").value == 0 ){
 		document.getElementById("advanced_search").value = 1;
 		document.getElementById("themes_hporecherche_pi1").style.display = 'block';
 		//document.getElementById("toggleSearch").innerHTML = 'masquer les th&egrave;mes'; 		
 	}
 	else{
 		document.getElementById("advanced_search").value = 0;
 		document.getElementById("themes_hporecherche_pi1").style.display = 'none';
 		//document.getElementById("toggleSearch").innerHTML = 'recherche avanc&eacute;e'; 
 	}
 }

/******************************************************************************/

/*modification de l'affichage du resultat de la recherche*/
 function setLoadResult(new_value)
 {
 	document.getElementById("load_result").value = new_value; 
 	document.getElementById("mySearchForm").submit();
 	return true;	
 }

 function activateSearchButton(){
 	document.getElementById("search_button").disabled = false; 
 }

 function enableSearchButton(force_reload){
	
	if ( force_reload == 'true')
		return setLoadResult(1);
	else{		
		document.getElementById("search_button").disabled = false; 		
		return true;
	}
 }

 function toggleFilter(new_value)
 {
 	document.getElementById("filter_type").value = new_value; 	
 	return setLoadResult(1);
 }

 function setPage(new_value)
 {
 	document.getElementById("start_page").value = new_value; 	
 	return setLoadResult(1);
 }
 
 function gotoSearchPage()
 {
 	document.getElementById("mySearchForm").action = 'index.php?id=27';
 	document.getElementById("type_interface").value="full";
 	document.getElementById("mySearchForm").submit();	
 }

/******************************************************************************/

 function node_textContent(node){

	var node_content = '';
	
	if (typeof node.textContent != 'undefined') {
		return node.textContent;
	}
	else if (typeof node.innerText != 'undefined') {
		return node.innerText;
	} 
	else if (typeof node.text != 'undefined') {
		return node.text;
	}
	else return '';

 }

 function get_hpo_recherche_xhr(){
	var xhr = null;

	if(window.XMLHttpRequest)		// Firefox et autres
		xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){	// Internet Explorer 
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else { // XMLHttpRequest non supporte par le navigateur 	
		xhr = false;
	}
	
	return xhr;
 }

 function hpo_recherche_onChangeXhr() {
	
	if( g_hpo_recherche_xhr && 
		(g_hpo_recherche_xhr.readyState 	== 4) && 
		(g_hpo_recherche_xhr.status 		== 200) ) {		

		var root 	= g_hpo_recherche_xhr.responseXML.documentElement;
		var node 	= root.getElementsByTagName('combo');
		
		//destruction des combo
		var obj = document.getElementById("select_element");	   		   
		var g_count_combo = 0;

		obj.innerHTML = "";
		
		for(var i = 0; i < node.length; i++ ){
			var combo 	= node[i];
		    var type 	= combo.getAttribute('name');
		   
   		   	++g_count_combo;
		    switch(type){
		    	case 'type':
		    		select = make_combo(combo, 'tx_hporecherche_pi1[select_type]', g_count_combo);		    		
		    		break;
		    		
		    	case 'rubrique':
		    		select = make_combo(combo, 'tx_hporecherche_pi1[select_rubrique_uid]', g_count_combo);
		    		break;

		    	case 'annee':
			    	select = make_combo(combo, 'tx_hporecherche_pi1[select_year]', g_count_combo);
		    		break;
		    		
		    	case 'magazine':
			    	select = make_combo(combo, 'tx_hporecherche_pi1[select_magazine_uid]', g_count_combo);
		    		break;
		    }

		  	if ((g_debug_ie6 == false) && (g_navigator != 'ie6')){
		    	obj.appendChild(select);
		    }	    
		}

		
		//alert('g_hpo_recherche_xhr is null');

		if( document.getElementById("search_button") ){
			document.getElementById("search_button").disabled = false; 
		}
		
		obj = document.getElementById("old_select_type");
		if (obj){
			var a_toggle 	= document.getElementById("toggleSearch");
			var block_theme = document.getElementById("themes_hporecherche_pi1");
			obj.value 		= get_hpo_recherche_combo_selection("tx_hporecherche_pi1[select_type]"); 
			
			if (a_toggle && block_theme){
				switch(obj.value){
					case 'recensions':								
					case 'auteurs':
						a_toggle.innerHTML = '';
						block_theme.style.display = 'none';
						break;
					
					default:
						a_toggle.innerHTML = 'Afficher/Masquer les th&egrave;mes';
						hide_show_search(document.getElementById("advanced_search").value);
						break;
				}
			}
			
		}
		
		if (document.getElementById("load_result")){
			document.getElementById("load_result").value = '1';
		}
		
		g_count_combo = 0;
		g_hpo_recherche_xhr = null;
	}
	

 }


 function hpo_recherche_onLoadXhr(with_combo){
 	var param 	= "";
 	var xmlPath = g_baseUrl+'index.php?id=27&type=300';

	//alert ('enter in hpo_recherche_onLoadXhr with : ' + g_navigator);

 	if ( g_hpo_recherche_xhr  == null ){
 		g_hpo_recherche_xhr = get_hpo_recherche_xhr();
 		g_with_combo 		= with_combo;

 		if ( get_hpo_recherche_xhr != null  ){
 			try {
				param = get_hpo_recherche_post_variable();
				//alert('param : ' + param);
				g_hpo_recherche_xhr.open("POST", xmlPath, true);
				g_hpo_recherche_xhr.onreadystatechange = hpo_recherche_onChangeXhr;
				g_hpo_recherche_xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				g_hpo_recherche_xhr.send(param);
			}
			catch (e) {
				alert("erreur : " + e);
			}
 		}
 		else{
 			setTimeout("hpo_recherche_onLoadXhr();", 1000);
 		}
 	}
 }
 
 function get_hpo_recherche_post_variable()
 { 	
 	var part_url 	= ''; 	
 	var count_part 	= 0;
	var value		= '';

 	//recuperation du type de selection
 	value = get_hpo_recherche_combo_selection("tx_hporecherche_pi1[select_type]"); 	
 	if (value != ''){
 		part_url += 'tx_hporecherche_pi1[select_type]=' + value;
 		count_part++;
 	}

 	//recuperation de la rubrique
 	value = get_hpo_recherche_combo_selection("tx_hporecherche_pi1[select_rubrique_uid]");
 	if (value != ''){		
		if (count_part)
			part_url += '&';

 		part_url += 'tx_hporecherche_pi1[select_rubrique_uid]=' + value;
 		count_part++;
 	}

 	//recuperation de l'annee
 	value = get_hpo_recherche_combo_selection("tx_hporecherche_pi1[select_year]");
	if (value !=''){
		if (count_part)
			part_url += '&';

 		part_url += 'tx_hporecherche_pi1[select_year]=' + value;
 		count_part++;
 	}

 	//recuperation deu magazine
 	value = get_hpo_recherche_combo_selection("tx_hporecherche_pi1[select_magazine_uid]");
	if (value != ''){
		if (count_part)
			part_url += '&';

 		part_url += 'tx_hporecherche_pi1[select_magazine_uid]=' + value;
 		count_part++;
 	}

	//recuperation des themes selectionnee
	value = get_hpo_recherche_themes();	
	if (value){
		part_url += value;
	}

 	return part_url;
 }
 
 function  get_hpo_recherche_combo_selection(combo_name){
	var comboValue 	= '';
	var obj 		= document.getElementById(combo_name);
	var selIndex 	= 0;

	if ( obj ){
		if ( (g_debug_ie6 == false) && (g_navigator != 'ie6') ){
			selIndex = obj.selectedIndex;
			comboValue 	= obj.options[selIndex].value;
		}
		else{
			comboValue = obj.value;
		}		
	}	

	return comboValue;
 }

 function get_hpo_list_width(combo_name){

	var dim = 0;

	switch(combo_name){
		case 'tx_hporecherche_pi1[select_type]':
			dim = dim_type;
			break;

		case 'tx_hporecherche_pi1[select_rubrique_uid]':
			dim = dim_rub;
			break;

		case 'tx_hporecherche_pi1[select_year]':
			dim = dim_year;
			break;

		case 'tx_hporecherche_pi1[select_magazine_uid]':
			dim = dim_mag;
			break;
	}

	return dim;
 }

 function get_hpo_recherche_themes(){

	var param	= ''; 
	var myForm 	= document.getElementById('mySearchForm'), z = 0;

	if (myForm){
		for(var i=0; i < myForm.length;i++){	    
		    var object = myForm[i];
		    if(object.type == 'checkbox'){			
				if (object.checked == true){				
					param += '&' + object.name + '=' + object.value;				
				}
			}
	    }
    }

    return param;
 }

 function set_hpo_li(val_a, val_b, val_c){
 	
 	var a_li = a_li = document.createElement("a");
 	a_li.setAttribute("href", "#");
 	a_li.setAttribute("onclick", function(){hpoComboSelect(val_a, val_b, val_c); return false;});
 	a_li.innerHTML = val_c;
	
	return a_li;
 } 			

 function make_combo(combo_node, combo_name, count){
 	
	if ( g_debug_ie6 || (g_navigator == 'ie6') ){
		return make_specific_combo(combo_node, combo_name, count);
	}
	else{
		return make_generic_combo(combo_node, combo_name);
	} 	

 }


 function make_specific_combo(combo_node, combo_name, local_count){

   	var string 			= '';
   	var selected_value	= "-";
   	var selected_text	= "-";
   	var span			= document.getElementById("select_element");  	
  	
  	var dl				= document.createElement("dl");
  	var dt_dl			= document.createElement("dt");
	var dd_dl			= document.createElement("dd");
	var span_dt			= document.createElement("span");
	var ul_dd 			= document.createElement("ul");
	var end_script		= null;
	var input 			= null;

	if (g_navigator == 'ie6'){
		end_script = document.createElement("<script type='text/javascript'></script>");
	}
	else{
		end_script = document.createElement("script");
		end_script.setAttribute("type", "text/javascript");
	}
		
  	//set input element
  	if (g_navigator == 'ie6'){  	
  		input = document.createElement('<input value="" type="hidden" id="'+combo_name+'" name="'+combo_name+'"/>');
   	}
   	else{
   		input = document.createElement("input");
   		input.setAttribute("type", "hidden");
   		input.setAttribute("name", combo_name);
   		input.setAttribute("id", combo_name);
   	}
   	
   	//set dl element
   	if (g_navigator == 'ie6'){
	   	dl.setAttribute("className", "select default_select");
	}
	else{
	   dl.setAttribute("class", "select default_select");
	}

   	dl.style.cssText = "width:" + get_hpo_list_width(combo_name) + "px;";

	//set dt of dl element
	if (g_navigator == 'ie6'){					
		dt_dl.setAttribute('onclick',function(){hpoComboShow(local_count);});
	}
	else{
		dt_dl.setAttribute('onclick', 'hpoComboShow('+local_count+')');
	}
	dt_dl.style.cssText = "width:" + get_hpo_list_width(combo_name) + "px;";
	
	
	 
	//set dd of dl element
	string = 'hpoCombo' + local_count;
	dd_dl.setAttribute("id", string);		

	//set ul of dd element
	ul_dd.style.cssText = "width:" + (get_hpo_list_width(combo_name) + 31) + "px;";

	//parcourt de l'arbre xml
	for(var i = 0; i < combo_node.childNodes.length; i++){

    	var item_node 		= combo_node.childNodes[i];    	
    	var current_value 	= item_node.getAttribute("value");
		
		var found = false;
		
		var li_ul = document.createElement("li");
		var a_li = null;
		
    	for( var j = 0; j < item_node.childNodes.length; j++ ){
    		var child_node = item_node.childNodes[j];
			var node_value = node_textContent(child_node);
			
    		if ( child_node.nodeName == "selected"){
    			if ( node_textContent(child_node) == 'Y' || i == 0) {    				
    				selected_value = current_value;
    				found = true;
    			}    			
    		}
    		else if ( child_node.nodeName == "text" ){

				if ( g_navigator == 'ie6' ){										
					a_li = set_hpo_li(combo_name.toString(), current_value.toString(), node_value.toString());					
				}
				else{					
					a_li = document.createElement("a");
					a_li.setAttribute("href", "#");
					a_li.setAttribute("onclick", "hpoComboSelect('" + combo_name +"', '" + current_value + "', '" + node_value+ "'); return false;");
    				a_li.innerHTML = node_value;
				}								    			
    			
    			if ( found ) selected_text = node_value;
			}
    	}

		if ( a_li ){
    		li_ul.appendChild(a_li);    	
	    	ul_dd.appendChild(li_ul);    	
    	}
    }   
   	dd_dl.appendChild(ul_dd);
  
 	//
 	//set span of dt element	
	span_dt.style.cssText = "width:" + (get_hpo_list_width(combo_name) + 10) + "px;";
	string = 'hpoComboTitle' + local_count;
	span_dt.setAttribute("id", string);
    span_dt.innerHTML = selected_text;
    
    dt_dl.appendChild(span_dt);    
    dl.appendChild(dt_dl);
    dl.appendChild(dd_dl);

    //add span child
    input.setAttribute("value", selected_value);
    span.appendChild(input);
    span.appendChild(dl);
    end_script.text = "hpoComboOnChange["+local_count+"] = 'hpo_recherche_onLoadXhr(" + g_with_combo + ");';";
    span.appendChild(end_script);
    
 	return span;
}

function make_generic_combo(combo_node, combo_name){ 
   	
   var select 		= document.createElement("select"); 
   var obj 		= null;
   var selected_index = 0;

   if ( select ) {
   		select.name = combo_name;
   		select.id 	= combo_name   		   		

	    select.innerHTML = '';

	    if ( g_navigator == 'firefox' ){
			select.setAttribute("class", "default_select");
			select.setAttribute("onchange", "hpo_recherche_onLoadXhr();");
		}
		else{
		    select.setAttribute("className", "default_select");
			select.setAttribute("onchange", function(){hpo_recherche_onLoadXhr();});
		}

	    select.setAttribute("id", combo_name);
		select.setAttribute("name", combo_name);

	    for(var i = 0; i < combo_node.childNodes.length; i++){

	    	var item_node = combo_node.childNodes[i];
	    	var option = document.createElement("option");

			option.value = item_node.getAttribute("value");

	    	for( var j = 0; j < item_node.childNodes.length; j++ ){
	    		var child_node = item_node.childNodes[j];

	    		if ( child_node.nodeName == "selected"){
	    			if ( node_textContent(child_node) == 'Y') {
	    				option.selected = true;
	    				selected_index = i;
	    			}
	    			else 
	    				option.selected = false;
	    		}
	    		else if ( child_node.nodeName == "text" )
	    			option.innerHTML = node_textContent(child_node);//node_textContent(child_node);
	    	}

	    	select.appendChild(option);
	    } 

	    select.selectedIndex = selected_index;   
    }

    return select;
 }

 
 function setPrefieldKeywords(){
 	document.getElementById('keywordSearchField').value = document.mySearchForm.keywordPrefield.value;
	document.mySearchForm.submit();
	return false;
 }
  
