function test_popup() {
	if(window.childpopup){
		childpopup.close();
	}
}

function payer_commande() {
	test_popup();
	document.getElementById('hidden_payer').value = "Commander";
	document.form_monPanier.submit();
}

function recherche_rapide() {
	test_popup();
	document.getElementById('hidden_quicksearch').value = "OK";
	document.form_monPanier.submit();
}

function reinit() {
	test_popup();
	document.getElementById('hidden_clear').value = "Initialiser";
	document.form_monPanier.submit();
}

function navIsIE() {
	// Function to test if we're under IE
	return typeof(document.all) !=="undefined" && !window.opera;
}

function navIsIE5low() {
	var version = parseFloat(navigator.appVersion.split("MSIE")[1]);

	return navIsIE() && (version <= 5.5);
}
			
function navIsIE6() {
	var version = parseFloat(navigator.appVersion.split("MSIE")[1]);
	return navIsIE() && (version > 5.5) && (version < 7);
}

function navIsIE7() {
	var version = parseFloat(navigator.appVersion.split("MSIE")[1]);
	return navIsIE() && (version >= 6.5) && (version < 8);
}		
function navIsIE8more() {
	var version = parseFloat(navigator.appVersion.split("MSIE")[1]);
	return navIsIE() && (version >= 7);
}		

function resizeWindow() {



	//Check if we need to move panier container in order for it to not overlap header
	var header = document.getElementById("header");
	var panier = document.getElementById("panier");


	if (!panier || !header ) {
		//console.error("An error occured !");
		return;
	} else if (navIsIE6()) {
		panier.style.position = "absolute";
		// Screen too small
		var x_offset;
		
		if (document.documentElement.clientWidth < header.offsetWidth)
			x_offset = header.offsetWidth;
		else 
			x_offset = (document.documentElement.clientWidth - header.offsetWidth)/2 + header.offsetWidth;
			
		var spacer  = 50;
		panier.style.position = "absolute";
		panier.style.left = (x_offset+spacer)+"px";
		panier.style.top = "5px";
		//alert("LEFT : " ++" " + (x_offset+spacer)+"px");
	} else {
		//alert("Move basket ?");			
		
		var x_offset = header.offsetLeft+header.offsetWidth;
		var p_width = panier.offsetWidth;
		var w_width = typeof(window.innerWidth) !== "undefined" ? window.innerWidth : document.documentElement.clientWidth;
		var spacer  = 50;

		var scrollTop = navIsIE() ? document.documentElement.scrollTop : window.scrollY;
		
		//alert("x_offset : "+x_offset+ " p_x " + panier.offsetLeft+" p_width : "+p_width+ " w_width : "+w_width);
		
		// Fix left offset of basket to header offset
		if ((spacer + x_offset + p_width)>w_width) {
			// Absolute position in order to let navigator scrolls
			panier.style.position = "absolute";
			//alert('move');
			panier.style.left = (x_offset+spacer)+"px";
			panier.style.right = "";

			// If we're positionning basket in absolute,need to take scroll into account
			panier.style.top = (20+ parseInt(scrollTop))+"px";
			//alert(20+ parseInt(scrollTop));
			
		// If IE < 8, can't use position:fixed	
		} else{
			//alert('replace');
			panier.style.position = "fixed";
			panier.style.left = "";
			panier.style.right = "20px";
			panier.style.top = "20px";
		}
	}
}

function visibilite(thingId) {

	var targetElement;

	targetElement = document.getElementById(thingId) ;

	targetElement.style.display = "block" ;

	document.getElementById('numerique'+document.getElementById(thingId).id.substring(6)).value = '1';

	maj_panier();

}

function test_panier(idBvt,idOption) {
	var idOpt;
	idOpt = idOption.split("#");
	if (document.getElementById('numerique'+idBvt).value == '1'){
		if(document.getElementById(idOption).checked == true){
			if(idOpt[1] == 2) {
				alert('Option "Rapport Papier" ajoutée');
			} else if(idOpt[1] == 3) {
				alert('Option "Abonnement Bulletins Veille" ajoutée');
			} else if(idOpt[1] == 5) {
				alert('Option "Accès Base de Connaissance" ajoutée');
			}
		}else {
			if(idOpt[1] == 2) {
				alert('Option "Rapport Papier" retirée');
			} else if(idOpt[1] == 3) {
				alert('Option "Abonnement Bulletins Veille" retirée');
			} else if(idOpt[1] == 5) {
				alert('Option "Accès Base de Connaissance" retirée');
			}
		}
		maj_panier();
	}
}

function redirect_site(url) {
	document.getElementById('hidden_redirect_site').value = url;
	document.form_monPanier.submit();
}

function logout() {
	document.getElementById('hidden_logout').value = "1";
	document.form_monPanier.submit();
}

function visibilite_option(thingId) {

	//var targetElement;

	//targetElement = document.getElementById(thingId) ;

	//targetElement.style.display = "block" ;
	
	if(document.getElementById(thingId).style.display == "block") {
		document.getElementById(thingId).style.display = "none";
	} else {
		document.getElementById(thingId).style.display = "block";
	}

}

function visibiliteBis(thingId) {
	var targetElement;
	targetElement = document.getElementById(thingId) ;
	if (targetElement.style.display == "none") {
		targetElement.style.display = "block" ;
	} else {
		targetElement.style.display = "none" ;
	}
}

function monCompte() {
	document.getElementById('hidden_monCompte').value = "1";
	document.form_monPanier.submit();
}

function contact() {
	document.getElementById('hidden_contact').value = "1";
	document.form_monPanier.submit();
}

function connection(param_tmp) {
	document.getElementById('hidden_connect').value = param_tmp;
	document.form_monPanier.submit();
}

function saveP(id_type) {
	document.getElementById('hidden_type_changed').value = id_type;
	document.form_monPanier.submit();
}

function help() {
	document.getElementById('hidden_help').value = "1";
	document.form_monPanier.submit();
}

function hide_show(id_div) {

	if(document.getElementById(id_div).style.display == "none") {

		document.getElementById(id_div).style.display = "block"

	} else {

		document.getElementById(id_div).style.display = "none"

	}

}



function maj_panier(){
	
	//test_popup();
	
	tab_url=new Array();

	/**/

	try{

		var tabChk = new Array();

		var listInput = document.getElementsByTagName("input");
		
		var listSelect = document.getElementsByTagName("select");

		var n = listInput.length;
		
		var o = listSelect.length;

		var i, j;

		j=0;

		var exp;
		
		var exp2;
		
		for (i=0; i<o; i++){

			tabChk[j] = listSelect[i];

			exp3 = new RegExp("qu_","gi");

			if (exp3.test(tabChk[j].id)) {

				if (tabChk[j].value != "") {

					tab_url[tab_url.length]= tabChk[j].id+"="+tabChk[j].value;

				}

			}

		}

		for (i=0; i<n; i++){

			if (listInput[i].type.toLowerCase()=="hidden"){

				tabChk[j] = listInput[i];

				exp2 = new RegExp("numerique","gi");

				if (exp2.test(tabChk[j].id)) {

					if (tabChk[j].value == "1") {

						tab_url[tab_url.length]= tabChk[j].id+"=on";

					} else {

						tab_url[tab_url.length]= tabChk[j].id+"=off";

					}

				}

			}

			if (listInput[i].type.toLowerCase()=="checkbox"){

				tabChk[j] = listInput[i];

				exp0 = new RegExp("option","gi");

				if (exp0.test(tabChk[j].id)) {
					if (tabChk[j].checked) {

						tab_url[tab_url.length]= tabChk[j].id+"=on";

					}

				}

			}

		}

	} catch(e){

		alert("erreur : " + n);

	}

	/**/

	var xhr=null;

	if (window.XMLHttpRequest) {

		xhr=new XMLHttpRequest();

	}

	if (window.ActiveXObject) {

		try {

		  xhr = new ActiveXObject("Msxml2.XMLHTTP");

		} catch (e) {

		  xhr=new ActiveXObject("Microsoft.XMLHTTP");

		}

	}



	var url_xhr = tab_url.join("&");

	//alert('url params : '+url_xhr);

	xhr.open("POST","ajax_panier.php",true);

	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");

	xhr.send(url_xhr);

	xhr.onreadystatechange=function() {

		if (xhr.readyState==4&&xhr.status==200) {
			document.getElementById('div_ajax_panier').innerHTML = xhr.responseText;

		}

	}

}



function maj_recap(){

	tab_url=new Array();

	try{

		var tabChk = new Array();

		var listInput = document.getElementsByTagName("input");
		
		var listSelect = document.getElementsByTagName("select");

		var n = listInput.length;
		
		var o = listSelect.length;

		var i, j;

		j=0;

		var exp;
		
		for (i=0; i<o; i++){

			tabChk[j] = listSelect[i];

			exp3 = new RegExp("qu_","gi");

			if (exp3.test(tabChk[j].id)) {

				if (tabChk[j].value != "") {

					tab_url[tab_url.length]= tabChk[j].id+"="+tabChk[j].value;

				}

			}

		}

		for (i=0; i<n; i++){

			if (listInput[i].type.toLowerCase()=="hidden"){

				tabChk[j] = listInput[i];

				exp2 = new RegExp("numerique","gi");

				if (exp2.test(tabChk[j].id)) {

					tab_url[tab_url.length]= tabChk[j].id+"=1";

				}

			}

			if (listInput[i].type.toLowerCase()=="checkbox"){
				tabChk[j] = listInput[i];
				
				exp0 = new RegExp("option","gi");

				if (exp0.test(tabChk[j].id)) {
					
					/*alert(tabChk[j].id+" avant "+tabChk[j].checked);
					tabChk[j].checked = false;
					alert(tabChk[j].id+" apres "+tabChk[j].checked);*/
					
					if (tabChk[j].checked) {
						tab_url[tab_url.length]= tabChk[j].id+"="+tabChk[j].checked;
					}

				}

			}

		}

	} catch(e){

		alert("erreur : " + n);

	}
	
	tab_url[tab_url.length]="tva_intra="+document.getElementById('tva_intra').value;
	
	tab_url[tab_url.length]="var_error="+document.getElementById('var_error').value;

	/**/

	var xhr=null;

	if (window.XMLHttpRequest) {

		xhr=new XMLHttpRequest();

	}

	if (window.ActiveXObject) {

		try {

		  xhr = new ActiveXObject("Msxml2.XMLHTTP");

		} catch (e) {

		  xhr=new ActiveXObject("Microsoft.XMLHTTP");

		}

	}



	var url_xhr = tab_url.join("&");

	//alert('url params : '+url_xhr);

	xhr.open("POST","ajax_recap.php",true);

	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");

	xhr.send(url_xhr);

	xhr.onreadystatechange=function() {

		if (xhr.readyState==4&&xhr.status==200) {

			document.getElementById('div_ajax_recap').innerHTML = xhr.responseText;

		}

	}

}

function maj_recap_options(){

	tab_url=new Array();

	

	/**/

	try{

		var tabChk = new Array();

		var listInput = document.getElementsByTagName("input");

		var n = listInput.length;

		var i, j;

		j=0;

		var exp;

		for (i=0; i<n; i++){

			if (listInput[i].type.toLowerCase()=="hidden"){

				tabChk[j] = listInput[i];

				exp2 = new RegExp("numerique","gi");

				if (exp2.test(tabChk[j].id)) {

					tab_url[tab_url.length]= tabChk[j].id+"=on";

				}

			}

			if (listInput[i].type.toLowerCase()=="checkbox"){

				tabChk[j] = listInput[i];

				exp0 = new RegExp("option","gi");

				if (exp0.test(tabChk[j].id)) {

					if (tabChk[j].checked) {

						tab_url[tab_url.length]= tabChk[j].id+"=on";

					}

				}

			}

		}

	} catch(e){

		alert("erreur : " + n);

	}
	
	tab_url[tab_url.length]="tva_intra="+document.getElementById('tva_intra').value;
	
	tab_url[tab_url.length]="var_error="+document.getElementById('var_error').value;

	/**/

	var xhr=null;

	if (window.XMLHttpRequest) {

		xhr=new XMLHttpRequest();

	}

	if (window.ActiveXObject) {

		try {

		  xhr = new ActiveXObject("Msxml2.XMLHTTP");

		} catch (e) {

		  xhr=new ActiveXObject("Microsoft.XMLHTTP");

		}

	}



	var url_xhr = tab_url.join("&");

	//alert('url params : '+url_xhr);

	xhr.open("POST","ajax_recap_options.php",true);

	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");

	xhr.send(url_xhr);

	xhr.onreadystatechange=function() {

		if (xhr.readyState==4&&xhr.status==200) {

			document.getElementById('div_ajax_recap').innerHTML = xhr.responseText;

		}

	}

}

function del_panier(id_data) {

	document.getElementById('a_supprimer').value = id_data;

	document.form_monPanier.submit();

}



function get_prerequis(id,id_modele,id_pre,study_id){

	test_popup();

	if (document.getElementById(id_pre).checked == true){

		if (document.getElementById(id).checked == false) {

			document.getElementById(id).checked = true;

			alert('Vous ne pouvez pas decocher cette option. Elle est un prerequis pour une autre option deja cochee.');

		}

		document.getElementById(id).checked = true;

	}

	if (document.getElementById(id).checked == true && id_modele.substring(id_modele.length-1,id_modele.length) != "0"){

		document.getElementById(id_modele).checked = true;

	}
	
	//document.getElementById('numerique'+study_id).value = '1';
	
}

function get_prerequis_options(id,id_modele,id_pre,study_id){

	if (document.getElementById(id_pre).checked == true){

		if (document.getElementById(id).checked == false) {

			document.getElementById(id).checked = true;

			alert('Vous ne pouvez pas decocher cette option. Elle est un prerequis pour une autre option deja cochee.');

		}

		document.getElementById(id).checked = true;

	}

	if (document.getElementById(id).checked == true && id_modele.substring(id_modele.length-1,id_modele.length) != "0"){

		document.getElementById(id_modele).checked = true;

	}
	
}


function popup(nom_de_la_page, nom_de_la_fenetre) {
window.open (nom_de_la_page, nom_de_la_fenetre, config='height=700, width=950, top=10,left=10, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no') 
}

function popup2(nom_de_la_page, nom_de_la_fenetre) {
	childpopup = window.open(nom_de_la_page, nom_de_la_fenetre, config='height=700, width=901, top=10,left=10, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=no, directories=no, status=no') 
}

function delete_panier(id) {
document.getElementById('panier').value = id;
document.formDel.submit();
}

function test_tva_valide(message) {
	var champ = document.getElementById('tva_intra');
	var tva_intra = champ.value;
	// Pour la constitution de l'expression r�guli�re, voir sur http://www.eic.ccip.fr/informations/tva/tva2.htm#som04
	var motif =/^(RO\d{2,10}|GB\d{5}|(ATU|DK|FI|HU|LU|MT|CZ|SI)\d{8}|IE[A-Z\d]{8}|(DE|BG|EE|EL|LT|BE0|PT|CZ)\d{9}|CY\d{8}[A-Z]|(ES|GB)[A-Z\d]{9}|(BE0|PL|SK|CZ)\d{10}|(FR|IT|LV)\d{11}|(LT|SE)\d{12}|(NL|GB)[A-Z\d]{12})$/;
	if (window.RegExp) {
		if (!motif.test(tva_intra)) {
			if (message != '') {
				document.getElementById('error_tva').style.display = 'block';
				document.getElementById('var_error').value = '1';
				document.getElementById('div_tva').style.display = 'none';
				document.getElementById('div_tva2').style.display = 'block';
				document.getElementById('div_ttc2').style.display = 'none';
				document.getElementById('div_ttc').style.display = 'block';
			}
			champ.focus();
			champ.select();
			return false;
		}
		else {
			document.getElementById('error_tva').style.display = 'none';
			document.getElementById('div_tva').style.display = 'block';
			document.getElementById('div_tva2').style.display = 'none';
			document.getElementById('div_ttc').style.display = 'none';
			document.getElementById('div_ttc2').style.display = 'block';
			document.getElementById('var_error').value = '0';
		}
	} else {
		if (tva_intra.length < 4 || tva_intra.length > 14) {
			if (message != '') {
				document.getElementById('error_tva').style.display = 'block';
				document.getElementById('div_tva').style.display = 'none';
				document.getElementById('div_tva2').style.display = 'block';
				document.getElementById('div_ttc2').style.display = 'none';
				document.getElementById('div_ttc').style.display = 'block';
				document.getElementById('var_error').value = '1';
			}
			champ.focus();
			champ.select();
			return false;
		}
		else {
			document.getElementById('error_tva').style.display = 'none';
			document.getElementById('div_tva').style.display = 'block';
			document.getElementById('div_tva2').style.display = 'none';
			document.getElementById('div_ttc').style.display = 'none';
			document.getElementById('div_ttc2').style.display = 'block';
			document.getElementById('var_error').value = '0';
		}
	}
}

function reservation_study(id_study) {
	document.getElementById('etude_reservee').value = id_study;
	document.form_monPanier.submit();
}

function action_study_details(id_study,action) {
	if (action == "reservee") {
		document.getElementById('etude_reservee_details').value = id_study;
	} else if (action == "ajoutee") {
		document.getElementById('etude_ajoutee_details').value = id_study;
	}
	document.getElementById('numerique'+id_study).value = "1";
	document.form_detail_etude.submit();
}

function test_details(id_study,action) {
	if(document.getElementById("numerique"+id_study).value == "1") {
		action_study_details(id_study,action);
	} else {
		alert("Vous n'avez pas cliquer sur le bouton "+action);
	}
}
