var browsernom;
var browsername=navigator.appName;	
var bottommenu=0;
var leftmenu=8;

var save_field = "";


/*
 * Validation du formulaire global
 *  - inscription Email
 *  - inscription Code
 *  - Récupération des données personnelles
 *  - Introduction des licences
 */
function ValidForm(mode) {
	
	str = "mode=" + mode;
	str	=  RemplirValidFormulaire(str);
	
	var xhr_object = null ;
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);
		
	xhr_object.onreadystatechange  = function()
	{ 
		 if(xhr_object.readyState  == 4)
		 {
		  if (xhr_object.status  == 200) {
		  	ret = xhr_object.responseText;
			if (mode == "envoi") {
			  array_texte = ret.split("#");
			  if(array_texte[0] != 'OK'){
				alert(array_texte[1]);
			  } else {
				var divLight = '<div>'+array_texte[1]+'</div>';
				document.getElementById('lightBoxDiv_middle').innerHTML = divLight;
			  }
			} else if (mode == "stamboeknummer") {
			  array_texte = ret.split("#");
			  if(array_texte[0] != 'OK'){
				alert(array_texte[1]);
			  } else {
			  	for (i=0;i<50;i++) {
			  		if (document.getElementById('lock_' + i)) {
						document.getElementById('lock_' + i).style.display = "none";
					}
					if (document.getElementById('locked_' + i)) {
						document.getElementById('locked_' + i).href = document.getElementById('locked_' + i).getAttribute("link");
						document.getElementById('locked_' + i).target = "_blank";
						document.getElementById('locked_' + i).onclick = "";
					}
			  	}
				closeLightBox();
			  }
			}
		  }				
		}
	}

	xhr_object.open( "POST","./ajax/valid_data.php", true ); 
	xhr_object.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_object.send(str);
			
}

function BacktoStep() {

	document.getElementById('renseignement').innerHTML = "";
	document.getElementById('message').innerHTML = "";	
	document.getElementById('threestep').style.display = "none";
	document.getElementById('secondstep').style.display = "block";
	
}

function AjusteMenu() {

	if (document.getElementById("menuhaut")) {

		if (browsernom != "Microsoft") {
			if (screen.width>1024) {
				document.getElementById("menuhaut").className="ajuste";	
			} else {
				document.getElementById("menuhaut").className="ajuste_firefox3";	
			}
			bottommenu 	= 1;
			leftmenu	= 1;
		}
		
		if (document.getElementById("titlemessage")) {
			valhauteur = document.getElementById("titlemessage").offsetHeight + 14;
			if (document.getElementById("idpoint")) {
				document.getElementById("idpoint").style.marginTop= "-" +  valhauteur + "px";
			}
		}
	
	}
	
}

function affichediv(arg1,arg2,arg3) {

	if (document.getElementById('message')) {
		document.getElementById('message').innerHTML = "";
	}
	document.getElementById(arg1).className="dshow";
	document.getElementById(arg2).className="dhidden";
	document.getElementById(arg3).className="dhidden";
}


function RemplirValidFormulaire(str) {
	
	if (document.getElementById('nom')) {
		str =  str + "&nom="  + document.getElementById('nom').value;
	}
	if (document.getElementById('email')) {
		str =  str + "&email="  + document.getElementById('email').value;
	}
	if (document.getElementById('email_dest')) {
		str =  str + "&email_dest="  + document.getElementById('email_dest').value;
	}
	if (document.getElementById('url')) {
		str =  str + "&url="  + document.getElementById('url').value;
	}
	if (document.getElementById('commentaire')) {
		str =  str + "&commentaire="  + document.getElementById('commentaire').value;
	}
	if (document.getElementById('catalog')) {
		str =  str + "&catalog="  + document.getElementById('catalog').value;
	}
	if (document.getElementById('codecard')) {
		str =  str + "&codecard="  + document.getElementById('codecard').value;
	}
	return str;
}

function Affiche_DejaInscrit(value) {
	
	if (value == "0") {
		
		document.getElementById('dejainscrit').style.display="none";
		document.getElementById('dejainscritconcours').style.display="none";
		
	} else if (value == "1") {
		
		document.getElementById('dejainscrit').style.display="block";
		document.getElementById('dejainscritconcours').style.display="none";
		
	} else if (value == "2") {
		
		document.getElementById('dejainscrit').style.display="none";
		document.getElementById('dejainscritconcours').style.display="block";
		
	}
}

function showLightBox(titre){
	var pageWidth;
	var pageHeight;
	var scrollTop = document.documentElement.scrollTop;
	if(scrollTop == 0){
		//Pour Safari et Chrome
		var scrollTop = document.body.scrollTop;	
	}
	
	if( window.innerHeight && window.scrollMaxY ) { // Firefox		
		pageWidth = window.innerWidth + window.scrollMaxX;
		pageHeight = window.innerHeight + window.scrollMaxY;		
	}else if( document.body.scrollHeight > document.body.offsetHeight ) { // IE6,7,8, Opéra
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	}else { // Chrome, Safari
 		pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
 		pageHeight = document.body.offsetHeight + document.body.offsetTop; 
 	}
	
	var d = document.getElementById('noInteractionDiv');
	d.style.position = 'absolute';
	d.style.left = '0px';
	d.style.top = '0px';
	d.style.width = (pageWidth-17) + 'px';
	d.style.height = pageHeight + 'px';
	
	var width;
	if (typeof parent.window.innerWidth != 'undefined'){ // mozilla/netscape/opera/IE7
		width = parent.window.innerWidth;
	}else if (typeof parent.document.documentElement != 'undefined' && typeof parent.document.documentElement.clientWidth != 'undefined' && parent.document.documentElement.clientWidth != 0){ // IE6		
		width = parent.document.documentElement.clientWidth;		       
	}else{ // older versions of IE
	    width = parent.document.getElementsByTagName('body')[0].clientWidth;
	}	
	
	d = document.getElementById('lightBoxDiv');
	var left = ((width - 622)/2);
	d.style.left = left+'px';
	d.style.top = (scrollTop + 200 ) + 'px';
	d.className = 'dshow';
		
	var divLight = '<div>'+titre.replace(/@/g,'"')+'</div>';
	document.getElementById('lightBoxDiv_middle').innerHTML = divLight;
	
}

function showLightBoxAjax(array,title){
	var pageWidth;
	var pageHeight;
	var scrollTop = document.documentElement.scrollTop;
	if(scrollTop == 0){
		//Pour Safari et Chrome
		var scrollTop = document.body.scrollTop;	
	}
	
	if( window.innerHeight && window.scrollMaxY ) { // Firefox		
		pageWidth = window.innerWidth + window.scrollMaxX;
		pageHeight = window.innerHeight + window.scrollMaxY;		
	}else if( document.body.scrollHeight > document.body.offsetHeight ) { // IE6,7,8, Opéra
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	}else { // Chrome, Safari
 		pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
 		pageHeight = document.body.offsetHeight + document.body.offsetTop; 
 	}
	
	var d = document.getElementById('noInteractionDiv');
	d.style.position = 'absolute';
	d.style.left = '0px';
	d.style.top = '0px';
	d.style.width = (pageWidth-17) + 'px';
	d.style.height = pageHeight + 'px';
	
	var width;
	if (typeof parent.window.innerWidth != 'undefined'){ // mozilla/netscape/opera/IE7
		width = parent.window.innerWidth;
	}else if (typeof parent.document.documentElement != 'undefined' && typeof parent.document.documentElement.clientWidth != 'undefined' && parent.document.documentElement.clientWidth != 0){ // IE6		
		width = parent.document.documentElement.clientWidth;		       
	}else{ // older versions of IE
	    width = parent.document.getElementsByTagName('body')[0].clientWidth;
	}	
	
	d = document.getElementById('lightBoxDiv');
	var left = ((width - 622)/2);
	d.style.left = left+'px';
	d.style.top = (scrollTop + 200 ) + 'px';
	d.className = 'dshow';
		
	var args = "array="+array+"&title="+title;
	
	var xhr_object = null ;
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);

	xhr_object.onreadystatechange  = function(){ 
		if(xhr_object.readyState  == 4){
			if (xhr_object.status  == 200) {
				ret = xhr_object.responseText;
				array_texte = ret.split("#");
				
				if(array_texte[0] == 'KO'){
					alert(array_texte[1]);
				}else{
					var divLight = '<div>'+array_texte[1]+'</div>';
					document.getElementById('lightBoxDiv_middle').innerHTML = divLight;
				}
				
			}
		}
	}

	if (array.indexOf('Error')>-1) {
		xhr_object.open( "POST","./ajax/load_error_content.php", true ); 
	} else {
		xhr_object.open( "POST","./ajax/load_lightbox_content.php", true ); 
	}
	xhr_object.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_object.send(args);
	
}

function closeLightBox(){
	var d = document.getElementById('noInteractionDiv');
	d.style.left = '-100px';
	d.style.top = '-100px';
	d.style.width = '10px';
	d.style.height = '10px';
	
	d = document.getElementById('lightBoxDiv');
	d.style.left = '-1000px';
	d.style.top = '-1000px';
}

function changeImage(img){

	source = document.getElementById(img).src;
	if (source.indexOf("\_on")>-1) {
		document.getElementById(img).src = source.replace("\_on","\_off");
	} else {
		document.getElementById(img).src = source.replace("\_off","\_on");
	}
}

function changeClassBkg(obj,style) {
	style_val = obj.className;
	if (style_val.indexOf("\_selected")>-1) {
		/*ByPass */
	} else {
		obj.className = style;
	}
}

function changeImageFil(obj,type) {
	source = document.getElementById(obj + "_left").src ;
	if (type != "on") {
		document.getElementById(obj + "_left").src = source.replace("\_on","\_off");
	} else {
		document.getElementById(obj + "_left").src = source.replace("\_off","\_on");
	}
	document.getElementById(obj + "_center").className = "tdariane_" + type;
	source = document.getElementById(obj + "_right").src ;
	if (type != "on") {
		document.getElementById(obj + "_right").src = source.replace("\_on","\_off");
	} else {
		document.getElementById(obj + "_right").src = source.replace("\_off","\_on");
	}
}

function subscription_newsletter(message){
	if(!document.getElementById("cb_vieprivee").checked){
		showLightBox(message);
		return null;
	}
	var email = document.getElementById("email_newsletter").value;
	var arrayCheckboxes = document.getElementsByTagName("INPUT");
	var str = "";
	for(i=0 ; i<arrayCheckboxes.length ; i++){
		var checkBox = arrayCheckboxes[i];
		if(checkBox.id=='checkbox_newsletter' && checkBox.checked){
			str += (str==""?"":"#")+checkBox.value;
		}
	}
	
	var args = "email="+email+"&ids="+str;
	
	var xhr_object = null ;
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);

	xhr_object.onreadystatechange  = function(){ 
		if(xhr_object.readyState  == 4){
			if (xhr_object.status  == 200) {
				ret = xhr_object.responseText;
				showLightBox(ret);				
			}
		}
	}

	xhr_object.open( "POST","./ajax/save_newsletter.php", true ); 
	xhr_object.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_object.send(args);
	
}
    
// la fonction réceptrice, ici "myFunction" 
function myFunction(response) { 
   if (response.data) {
   	 /*
   	  * Réinterroger la db pour être sûr que le cookie est présent
   	  */
	  getRemoteData('http://e-commerce.deboeck.com/mybasket_verif.php?title_id='+response.data+'&callback=myFunctionCookie');
   }
} 

function myFunctionCookie(response) { 
   if (response.data != "") {	  
	  var array = "ean#title#subtitle";	     	
	  response.data = escape(response.data);
      showLightBoxAjax(array,response.data);
   } else {
      showLightBoxAjax('Error',"error_cookie");
   }
}
   
function AddBasket(title_id,type,catalog,hommage) {
	getRemoteData('http://e-commerce.deboeck.com/basket.php?catalog='+catalog+'&title_id='+title_id+'&type='+type+'&hommage='+hommage+'&callback=myFunction');
}

function AddSearchCookie(field, redirect){
	var request = document.getElementById(field).value;
	if(field.indexOf("hist_") != -1){
		request = document.getElementById(field).getAttribute("value");
		save_field = field;
	}
	while (request.indexOf("'") != -1){
 	   request = request.replace("'", "]]]");
 	}
 	
	getRemoteData('http://search.deboeck.com/index.php?request='+request+'&redirect='+redirect+'&callback=functionSearch');
}
function GetSearchCookie(redirect){
	getRemoteData('http://search.deboeck.com/index.php?redirect='+redirect+'&callback=functionSearch');
	
}

function functionSearch(response){
	var data = response.data;
	while (data.indexOf("]]]") != -1){
 	   data = data.replace("]]]", "'");
 	}
	var arrayRet = data.split("@@@");
	setSessionVarSearch('request',arrayRet[0],'',arrayRet[1]);
}

function setSessionVarSearch(name,value,key,redirect){
	//On appelle un ajax qui va placer la variable de session.
	//Ensuite, on relance la page sur laquelle on se trouve ou la page redirect.
	var str = '&name='+name+"&value="+value+"&key="+key;
	var xhr_object = null ;
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);

	xhr_object.onreadystatechange  = function(){ 
		if(xhr_object.readyState  == 4){
			if (xhr_object.status  == 200) {
				ret = xhr_object.responseText;
				if(save_field != ""){
					save_field = "";
					GotoUrl(redirect);					
				}else{
					document.location.href = redirect;
				}
			}
		}
	}

	xhr_object.open( "POST","./ajax/setSessionVar.php", true ); 
	xhr_object.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_object.send(str);
}

function recherche(searchField){
	var args = "request="+document.getElementById(searchField).value;
	
	var xhr_object = null ;
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);

	xhr_object.onreadystatechange  = function(){ 
		if(xhr_object.readyState  == 4){
			if (xhr_object.status  == 200) {
				ret = xhr_object.responseText;
				showLightBox(ret);				
			}
		}
	}

	xhr_object.open( "POST","./ajax/loadRecherche.php", true ); 
	xhr_object.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_object.send(args);
 
}

function detailOuvrageLightBox(mode,ed_spec_id,type){
	
	var args = "mode="+mode+"&ed_spec_id=" + ed_spec_id+"&type="+type;
	
	var xhr_object = null ;
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);

	xhr_object.onreadystatechange  = function(){ 
		if(xhr_object.readyState  == 4){
			if (xhr_object.status  == 200) {
				ret = xhr_object.responseText;
				showLightBox(ret);				
			}
		}
	}

	if (mode == "imprimer") {
		window.open("generique/ouvrages/imprimer.php?ed_spec_id=" + ed_spec_id);
	} else {
		xhr_object.open( "POST","./ajax/load_detailouvrage.php", true ); 
		xhr_object.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xhr_object.send(args);
	}
 
}

function changeChildStatus(id,children){
	var array = children.split('#');
	if(document.getElementById(id).checked){
		for(c=0 ; c<array.length ; c++){
			var arrayCheckboxes = document.getElementsByTagName("INPUT");
			for(i=0 ; i<arrayCheckboxes.length ; i++){
				var checkBox = arrayCheckboxes[i];
				if(checkBox.id=='checkbox_newsletter' && checkBox.value==array[c]){
					checkBox.checked = true;
				}
			}		
		}
	}else{
		for(c=0 ; c<array.length ; c++){
			var arrayCheckboxes = document.getElementsByTagName("INPUT");
			for(i=0 ; i<arrayCheckboxes.length ; i++){
				var checkBox = arrayCheckboxes[i];
				if(checkBox.id=='checkbox_newsletter' && checkBox.value==array[c]){
					checkBox.checked = false;
				}
			}		
		}
	}
}


function LanceAffichedivsearch(indice) {
	
	str="mode=histo&indice="+indice;
	
	var xhr_object = null ;
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);
		
	xhr_object.onreadystatechange  = function()
	{ 
		 if(xhr_object.readyState  == 4)
		 {
		  if (xhr_object.status  == 200) {
		  	document.getElementById("footer_historique").innerHTML = xhr_object.responseText;
		  }				
		}
	}

	xhr_object.open( "POST","./ajax/valid_data.php", true ); 
	xhr_object.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_object.send(str);			
}	
	
function Affichedivsearch(obj_a,obj_div) {

	Effacedivsearch();
	
	document.getElementById(obj_div).style.left		= parseInt(document.getElementById("td_mosttop_side").offsetWidth + document.getElementById(obj_a).offsetLeft+10) + "px";
	document.getElementById(obj_div).style.top		= parseInt(document.getElementById("footer_ie").offsetTop + document.getElementById(obj_a).offsetTop+22) + "px";
	document.getElementById(obj_div).style.position	= "absolute";
	document.getElementById(obj_div).style.display	= "block";		
}

function Effacedivsearch() {
	
	for (x=0;x<150;x++) {
		obj_div  = "hist_result_"+x;
		if (document.getElementById(obj_div)) {
			document.getElementById(obj_div).style.display	= "none";		
		}
	}
}