function getHTTPObject() {
	var xmlhttp;
	/*@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 @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		xmlhttp.overrideMimeType("text/xml"); 
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function getElementsByClassName(needle)	{
	  var         my_array = document.getElementsByTagName("*");
	  var         retvalue = new Array();
	  var        i;
	  var        j;
	
	  for (i = 0, j = 0; i < my_array.length; i++)
	  {
		var c = " " + my_array[i].className + " ";
		if (c.indexOf(" " + needle + " ") != -1)
		  retvalue[j++] = my_array[i];
	  }
	  return retvalue;
}

function printPop(){
	//var principal = document.getElementById("principal");
	//alert(principal.innerHTML);
	//window.open("print.php?page="+page, "myprint");
	var header_outer = document.getElementById("header_outer");
	var menu = document.getElementById("menu");
	var barre_laterale = document.getElementById("barre-laterale");
	var field_article_path = document.getElementById("field_article_path");
	var autopub = document.getElementById("autopub");
	var footer = document.getElementById("footer");
	
	header_outer.style.display="none";
	menu.style.display="none";
	barre_laterale.style.display="none";
	autopub.style.display="none";
	field_article_path.style.display="none";
	footer.style.display="none";
	
	//les style 
	var conteneur = document.getElementById("conteneur");
	conteneur.style.background="none";
	
	var principal = document.getElementById("principal");
	principal.style.margin="0px;"
	
	var article =  getElementsByClassName("article")
	for (var el in article){
		article[el].style.width = "750px";
	}
	
	var photo =  getElementsByClassName("photo")
	for (var el in photo){
		photo[el].style.display = "none"; 
	}
	
	var intro = document.getElementById("intro");
	intro.style.width = "400px";
	intro.style.margin = "0px 0px 0px 30px";
	
	var icones =  getElementsByClassName("icones")
	for (var el in icones){
		icones[el].style.display = "none";
	}
	
	window.print();
	
	//apres impression, on refait la page
	window.setTimeout(
			function() {
				header_outer.style.display="block";
				menu.style.display="block";
				barre_laterale.style.display="block";
				autopub.style.display="block";
				field_article_path.style.display="block";
				footer.style.display="block";
				
				//les style
				conteneur.style.background="url(fileadmin/templates/ccir/main/img/generic/fond_pointille.gif) no-repeat 205px 6px #fff";
				intro.style.width = "270px";
				principal.style.margin="6px  0px 0px 205px  ";
				for (var el in article){
					article[el].style.width = "400px";
				}
				for (var el in photo){
					photo[el].style.display = "block";
				}
				for (var el in icones){
					icones[el].style.display = "block";
				}
				intro.style.margin = "30px 0px 0px 0px";
			},
			1000 // twenty seconds
	);
}


function to_pdf(){

	var http = getHTTPObject();
	var timeoutId;
	http.open("POST","xhtml2pdf/print_cci.php", true);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			if (http.status == 200){
				window.clearTimeout(timeoutId);
				var htmlDoc = http.responseText;
				window.open('xhtml2pdf/print_cci.pdf');
			}
		}
	}
	http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	http.send("none");
	timeoutId = window.setTimeout(
		function() {
			switch (http.readyState) {
				case 1:
				case 2:
				case 3:
					http.abort();
					document.getElementById("statut").innerHTML = "<h2>Error Loading Data</h2>";
					alert("Oops. There was an error retreiving data from the server. Please try again in a few moments.");
					break;
				default:
					break;
			}
		},
		20000 // twenty seconds
	);
}

function printPDF(){
	var intro = document.getElementById("intro");
	var article;
	
	for (i=0; i<document.getElementsByTagName("*").length; i++) {
	  if (document.getElementsByTagName("*").item(i).className == "article"){
	   article = document.getElementsByTagName("*").item(i);
	  }
	}

	html = "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"fr\">";
	html += "<head><meta http-equiv=\"Content-Type\" content=\"text/HTML; charset=iso-8859-1\"  /><title>CCIR</title></head>";
	html += "<body><p>"+intro.innerHTML+article.innerHTML+"</p></body></html>";
	
	var http = getHTTPObject();
	var timeoutId;
	http.open("POST","xhtml2pdf/write_to_disk.php", true);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			if (http.status == 200){
				window.clearTimeout(timeoutId);
				var htmlDoc = http.responseText;
				to_pdf();
			}
		}
	}
	http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	http.send("html="+escape(html));
	timeoutId = window.setTimeout(
		function() {
			switch (http.readyState) {
				case 1:
				case 2:
				case 3:
					http.abort();
					document.getElementById("statut").innerHTML = "<h2>Error Loading Data</h2>";
					alert("Oops. There was an error retreiving data from the server. Please try again in a few moments.");
					break;
				default:
					break;
			}
		},
		20000 // twenty seconds
	);
}

/*
moteur de recherche
*/
function clear_search_input(){
	var header_form_input = document.getElementById("header_form_input");
	header_form_input.onfocus = function onfocus(event){
		if(header_form_input.value=="rechercher") header_form_input.value=" ";
	}
	header_form_input.onblur = function onblur(event){
		if(header_form_input.value==" ") header_form_input.value="rechercher";
	}
}