//var racine = "http://localhost/requien/Scripts/";
//var racine = "http://www.epsyllum.net/requien/Scripts/";
var racine = "http://www.bibliothequerequien.org/Scripts/";
var reg= /msie 6/i ;
var ie6 = reg.test(navigator.userAgent) ;

function vide_balise_id(arg){
	var element = document.getElementById(arg);
	while (element.firstChild) { 
		element.removeChild(element.firstChild);
	}
	return element
}
function affiche_balise_id(arg,type){
	var element = document.getElementById(arg);
	element.style.display = type
}
function masque_balise_id(arg){
	var element = document.getElementById(arg);
	element.style.display = "none"
}
function getVar(nomVariable) {
	var infos = location.href.substring(location.href.indexOf("?")+1, location.href.length)+"&"
	if (infos.indexOf("#")!=-1) 
		infos = infos.substring(0,infos.indexOf("#"))+"&"
	
	var variable=0
	{
		nomVariable = nomVariable + "="
		var taille = nomVariable.length
		if (infos.indexOf(nomVariable)!=-1)
		variable = infos.substring(infos.indexOf(nomVariable)+taille,infos.length).substring(0,infos.substring(infos.indexOf(nomVariable)+taille,infos.length).indexOf("&"))
	}
	return variable
} 
function nl2br (str, is_xhtml) {
    // Converts newlines to HTML line breaks  
    // 
    // version: 911.1619
    // discuss at: http://phpjs.org/functions/nl2br    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Maximusya
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');    // *     returns 1: 'Kevin\nvan\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '\nOne\nTwo\n\nThree\n'    
	var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br>';
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
function http_request(sql,php,callback,type){
	var xhr = xhr_connect()
	if (xhr) {
		
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4) {
				if(xhr.status == 200) {
	
					var retour = (type == "txt")?xhr.responseText:xhr.responseXML;
					callback(retour)
				} ;
			}; 
		}
		xhr.open( "POST", racine + php, true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send(sql);
	} else {
		alert("erreur de connexion HTTP");
	}
}
function xhr_connect(){
	var xhr = false
	if (window.XMLHttpRequest){
    	xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		var success = false;
        var ieXHR = new Array(
        "Msxml2.XMLHTTP.7.0","Msxml2.XMLHTTP.6.0",
        "Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0",
        "MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
        for (var I=0; I<ieXHR.length && !success; I++) {
            try{
                xhr = new ActiveXObject(ieXHR[I]);
				success = true;
                //break;
            }catch(e){
            }
        }
    }
	return xhr;	
}
// [TO DO] penser à angliser le truc
var ref_labels = new Object()
ref_labels["FR"] = new Object()
ref_labels["GB"] = new Object()
ref_labels["FR"]["ch_ouv_id"] = ""
ref_labels["FR"]["ch_ouv_auteur_principal"] = "Auteur principal"
ref_labels["FR"]["ch_ouv_auteur_principal_obs"] = ""
ref_labels["FR"]["ch_ouv_auteur_secondaire"] = "Auteur secondaire"
ref_labels["FR"]["ch_ouv_auteur_secondaire_obs"] = ""
ref_labels["FR"]["ch_ouv_titre"] = "Titre"
ref_labels["FR"]["ch_ouv_sous_titre"] = "Sous titre"
ref_labels["FR"]["ch_ouv_lieu_edition"] = "Lieu d'édition"
ref_labels["FR"]["ch_ouv_editeur"] = "Editeur"
ref_labels["FR"]["ch_ouv_date"] = "Date"
ref_labels["FR"]["ch_ouv_collation"] = "Collation (pagination/format)"
ref_labels["FR"]["ch_ouv_collection"] = "Collection"
ref_labels["FR"]["ch_ouv_notes"] = "Notes"
ref_labels["FR"]["ch_ouv_consult_langue"] = "Langue de l'ouvrage"
ref_labels["FR"]["ch_ouv_themes"] = "Thèmes"
ref_labels["FR"]["ch_ouv_format"] = "Format"
ref_labels["FR"]["ch_ouv_mot_cles"] = "Mots clés"
ref_labels["FR"]["ch_ouv_n_inventaire"] = "Numéro d'inventaire"
ref_labels["FR"]["ch_ouv_cotes"] = "C&ocirc;tes"
ref_labels["FR"]["ch_ouv_autres"] = "Autres"
ref_labels["FR"]["ch_ouv_notes_curiosite"] = "Notes curiosités"

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
function fixPNG() {
	
	if(ie6) {
		//alert(racine + 'empty.gif')
		var png = /\.png$/i;
		var imgs = document.getElementsByTagName('img');
		for(var i = 0, l = imgs.length; i < l; i++) {
			if(png.test(imgs.item(i).src)) {
				imgs.item(i).style.width = imgs.item(i).offsetWidth;
				imgs.item(i).style.height = imgs.item(i).offsetHeight;
				imgs.item(i).style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + imgs.item(i).src + '\',sizingMethod=\'scale\')';
				imgs.item(i).src = racine + 'empty.gif';
			}
		}
	}
}

//addLoadEvent()

function imprime(arg){
	
	
	var contenu = document.getElementById(arg).innerHTML;
	var bloc_vignette = document.getElementById('img_thumb')
	if(bloc_vignette == undefined) {
		bloc_vignette = document.getElementById('img_fiche')
	}
	var img_vignette = bloc_vignette.getElementsByTagName('img')[0]
	var src_vignette = img_vignette.src
	var vignette = '<img src="'+src_vignette+'" />'
	
	var w = window.open("", "Impression", "height=500, width=600,toolbar=0, menubar=0, scrollbars=1, resizable=1,status=0, location=0, left=10, top=10");
	
	w.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Document sans nom</title> <link href="http://www.bibliothequerequien.org/css/impression.css" rel="stylesheet" type="text/css" /> </head> <body>')
	w.document.write(vignette)
	w.document.write(contenu) ;
	w.document.write('</body></html>');
	w.window.print();/**/
	//w.window.close();



}

/////
///// MOTEUR DE RECHERCHE GENERAL
/////

function autocomplete_all(arg){
	if (arg != "") {
		var sql = "ch_str="+arg;
		http_request(sql,"search_autocomplete.php",search_pre_result,'txt')
		//alert(sql)	
	} else {
		search_pre_result("")
	}
}
function vide_search_field(){
	var sf = document.getElementById('search_all_terme')
	sf.value = ""
	search_pre_result("")
}
function search_pre_result(arg){
	var div = vide_balise_id('propal_all_terme')
	if (arg != "") {
		div.style.display = "block"
		div.innerHTML = arg;
	} else {
		div.style.display = "none"
	}
}
function verifpage(){
	search_pre_result("")
	var page = window.location.href.split("/")
	return page[page.length - 1]
}

function search_all_fond(strFond){
	page = verifpage()
	if(page != 'consultation.php'){
		  window.location.href = 'http://www.bibliothequerequien.org/pagesFR/consultation.php?categorie=search&strFond='+strFond
		//window.location.href = 'http://localhost/requien/pagesFR/consultation.php?categorie=search&strFond='+strFond
	} else {
		var sql = "strFond=" + strFond
		http_request(sql,'result_list.php',affiche_page_result,'xml')
	}
}

