// fonction de création d'un objet XMLHttpRequest

whatToDoWithInfo = '';
whatToDoWithDisplayDataGrid = '';
tableConcern = '';
GalerieConcern = '';

numCalendarToShow = 0;

dataGridOption_stringSearch = '';
dataGridOption_sortByField = '';
dataGridOption_sortType = '';
dataGridOption_instanceName = '';
dataGridOption_pageToDisplay = '';
dataGridOption_idLangues = '';
dataGridOption_idCategoriesGaleries = '';

stateBarNewDisplayValue = '';

numImgFieldToRestore = 0;
numFileFieldToRestore = 0;

var tOut = '';
var tOut2 = '';
var tOutRefresh = '';

var compteurAjaxFunctions = 0; // permet de lancer la bonne fonction au bon moment s'il y en a plus que 2
var ajax_functions = new Array(); // tableau stockant les fonctions à lancer une fois la première requette ajax effectuée.

/*function lounchAjaxFunctions(functions){
	ajaxFunctions = functions;
	eval(ajaxFunctions[0]+';');
}

function checkOtherFunctionToLounch(){
	if(typeof ajaxFunctions!="undefined"){
		if(ajaxFunctions.length >= compteurAjaxFunctions){
			eval(ajaxFunctions[compteurAjaxFunctions]+';');
		} else {
			compteurAjaxFunctions = 0;
		}
	}
}*/
function lounchAjaxFunctions(){
	//if(typeof ajaxFunctions!="undefined"){
		
		clearTimeout(tOut);
		if( compteurAjaxFunctions < ajax_functions.length ){
			//alert(ajax_functions[compteurAjaxFunctions]);
			eval(ajax_functions[compteurAjaxFunctions]);
			compteurAjaxFunctions++;
		} else {
			//alert('toto');
			compteurAjaxFunctions = 0;
			ajax_functions.length = 0;
			
		}
	//}
}

var onload_functions = new Array();

/**
* New function for handling multiple calls to window.onload and window.unload by pentapenguin
*/
window.onload = function()
{
	for (var i = 0; i < onload_functions.length; i++)
	{
		eval(onload_functions[i]);
	}

}

function creeXMLHttpRequestObject(){
	var ajxRequest = null;
	try {
		ajxRequest = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(Error){
		try {
			ajxRequest = new ActiveXObject("MSXML2.XMLHTTP");
		} catch(Error){
			try {
				ajxRequest = new XMLHttpRequest();
			} catch(Error){
				alert("Impossible de créer l'objet XMLHttpRequest");
			}
		}	
	}
	return ajxRequest;
}

function loadingHTML(){
	return '<div class="loading"><div><img src="images/loading.gif" /><span>chargement...</span></div></div>';
}
function loadingHTML2(){
	return '<img src="images/loading.gif" />';
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : Affichage du détail d'un produite   /////////////
/////////////////////////////////////////////////////////////////////
var autoSelectP;
function loadlistProducts(catIds, stringSearch_noarticle, stringSearch_designation, sortByField, sortType, pageToDisplay, autoSelect){
	sendValues = 'load=y';
	if(catIds != undefined && catIds != ""){
		sendValues += '&catIds='+catIds;
	}
	if(stringSearch_noarticle != undefined && stringSearch_noarticle != ""){
		sendValues += '&stringSearch_noarticle='+stringSearch_noarticle;
		//dataGridOption_stringSearch = stringSearch;
	}
	if(stringSearch_designation != undefined && stringSearch_designation != ""){
		sendValues += '&stringSearch_designation='+stringSearch_designation;
		//dataGridOption_stringSearch = stringSearch;
	}
	if(sortByField != undefined && sortByField != ""){
		sendValues += '&sortByField='+sortByField;
		//dataGridOption_sortByField = sortByField;
	}
	if(sortType != undefined && sortType != ""){
		sendValues += '&sortType='+sortType;
		//dataGridOption_sortType = sortType;
	}
	if(pageToDisplay != undefined && pageToDisplay != ""){
		sendValues += '&page='+pageToDisplay;
		//dataGridOption_pageToDisplay = pageToDisplay;
	}
	
	if(autoSelect != undefined && autoSelect != ""){
		autoSelectP = autoSelect;
	} else {
		autoSelectP = 'first';
	}
	
	ajxRequestList.open('post', 'inc/product/listProducts.php');
	ajxRequestList.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajxRequestList.onreadystatechange = showlistProducts;
	ajxRequestList.send(sendValues);
}
refreshLP = 'false';
function refreshlistProducts(){
	refreshLP = 'true';
	ajxRequestList.open('get', 'inc/product/refresh_listProducts.php');
	ajxRequestList.onreadystatechange = showlistProducts;
	ajxRequestList.send(null);
}

function setLimit(limit){
	ajxRequest.open('get', 'inc/pagination/setLimit.php?limit='+limit);
	ajxRequest.onreadystatechange = finaliseLimitSetting;
	ajxRequest.send(null);
	ajax_functions.push('refreshlistProducts();');
}
function setLimitWithoutRefresh(limit){
	ajxRequest.open('get', 'inc/pagination/setLimit.php?limit='+limit);
	ajxRequest.onreadystatechange = finaliseLimitSetting;
	ajxRequest.send(null);
}
function finaliseLimitSetting(){
	if(ajxRequest.readyState == 4){
		tOut = setTimeout("lounchAjaxFunctions()", 50);
	}
}

function showlistProducts(){
	if(ajxRequestList.readyState == 4){
		document.getElementById("product_list").innerHTML = ajxRequestList.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		
		if(autoSelectP == 'last'){
			auto_select('last');
		}
		if(autoSelectP == 'first'){
			//select_first();
			auto_select('first');
		}
		
		fitProductList('true');
		setColDetailProduct();
		
	} else  {
		$$('#product_list .load').set('html', loadingHTML());
	}
}


/////////////////////////////////////////////////////////////////////
///////  FUNCTION : Formulaire de recherche   /////////////
/////////////////////////////////////////////////////////////////////
function clearSearchRemind(){
	ajxRequest.open('get', 'inc/search/clear.php');
	ajxRequest.onreadystatechange = finaliseclearSearchRemind;
	ajxRequest.send(null);
	ajax_functions.push('refreshlistProducts();');
}
function finaliseclearSearchRemind(){
	if(ajxRequest.readyState == 4){
		tOut = setTimeout("lounchAjaxFunctions()", 50);
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : Affichage du détail d'un produite   /////////////
/////////////////////////////////////////////////////////////////////

function loadDetailProduct(product_id){
	/*alert(product_id);*/
	ajxRequestDetail.open('get', 'inc/product/detailProduct.php?product_id='+product_id);
	ajxRequestDetail.onreadystatechange = showDetailProduct;
	ajxRequestDetail.send(null);
	$$('.tool-tip-dp').setStyle('display', 'none');
}

function showDetailProduct(){
	if(ajxRequestDetail.readyState == 4){
		document.getElementById("product_detail").innerHTML = ajxRequestDetail.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		$$('.tool-tip-dp').setStyle('display', 'none');
		var Tips2 = new Tips($$('.Tips2'), {
			className: 'tool-tip-dp'
		});
		var Tips2 = new Tips($$('.Tips2')); // activation des infobulles...
		fitProductDetail();
		setColDetailProduct();
	} else  {
		$$('#product_detail .load').set('html', loadingHTML());
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : Update to cart    ///////////////////////////////
/////////////////////////////////////////////////////////////////////
var nodeClicked = '';
var current_id = null;
var cart_action = '';
var cart_update = false;
var currentNode = '';
function defineNode(node){
	currentNode = node;
}
function update_cart(action, id, qty){
	//alert(action + " " + id + " " + qty);
	/*current_id = id;
	cart_action = action;*/
	$('addToCartBtn'+id).toggleClass('out');
	$('addToCartBtn'+id).toggleClass('in');
	//alert($('addToCartBtn'+id).get('onclick'));
	$('addToCartBtn'+id).removeEvents('click');
	$('addToCartBtn'+id).erase('onclick');
	

	if(action == 'add'){	
		$('addToCartBtn'+id).addEvent('click', function(){
			ajax_functions.push('update_cart(\'remove\', \''+id+'\', 0);');
		});
	} else if(action == 'remove'){
		$('addToCartBtn'+id).addEvent('click', function(){
			//update_cart('add', id, 1);
			ajax_functions.push('update_cart(\'add\', \''+id+'\', 1);');
		});
	}

	cart_update = true;
	ajxRequestWithMsg.open('get', 'inc/cart/updateCart.php?action='+action+'&product_id='+id+'&qty='+qty);
	ajxRequestWithMsg.onreadystatechange = showInfoBis;
	ajxRequestWithMsg.send(null);
	

	
	ajax_functions.push('display_mini_cart();');
	
	/*ajxRequestMiniCart.open('get', 'inc/cart/displayMiniCart.php');
	ajxRequestMiniCart.onreadystatechange = showMiniCart;
	ajxRequestMiniCart.send(null);*/
}
function change_qty(refreshWhat, id, qty){

	ajxRequestWithMsg.open('get', 'inc/cart/updateCart.php?action=setQty&product_id='+id+'&qty='+qty);
	ajxRequestWithMsg.send(null);
	
	/*if(refreshWhat == 'miniCart'){
		display_mini_cart();
	} else if(refreshWhat == 'cart'){
		display_cart();
	}*/
	
	if(document.getElementById('cart')){
		display_mini_cart();
	} else {
		display_cart(false);
	}
	
	//loadDetailProduct(id); // enlever pour voir... ca fonctionne ATTENTION; PEUT ETRE REACTIVER SI ENNUIS
	
}
function display_mini_cart(){
	ajxRequestMiniCart.open('get', 'inc/cart/displayMiniCart.php');
	ajxRequestMiniCart.onreadystatechange = showMiniCart;
	ajxRequestMiniCart.send(null);
	$$('.tool-tip-tipsMC').setStyle('display', 'none');
}
function remove_from_minicart(id){
	ajxRequestWithMsg.open('get', 'inc/cart/removeFromCart.php?product_id='+id);
	ajxRequestWithMsg.onreadystatechange = showInfo;
	ajxRequestWithMsg.send(null);
	ajax_functions.push('display_mini_cart();');
	ajax_functions.push('refreshlistProducts();');
	
	
	// RAJOUTER RAFFRAICHISSEMENT DE LA LISTE DE PRODUIT
	// => ENTRAINE LA SAUVEGARDE AUTO DES VALEURS ENVOYöE::::::
	
	
}
function showMiniCart(){
	
	if(ajxRequestMiniCart.readyState == 4){
		
		change_display('cart', 'block');
		document.getElementById("cart").innerHTML = ajxRequestMiniCart.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		setColMiniCart();
		
		//var TipsMC = new Tips($$('.TipsMC')); // activation des infobulles...
	} else  {
		$$('#cart .load').set('html', loadingHTML());
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : Display CART    /////////////////////////////////
/////////////////////////////////////////////////////////////////////
var autoSelect;
function display_cart(select_item){
	autoSelect = select_item;
	ajxRequestList.open('get', 'inc/cart/displayCart.php');
	ajxRequestList.onreadystatechange = showCart;
	ajxRequestList.send(null);
	$$('.tool-tip').setStyle('display', 'none');
}
function remove_from_cart(id){
	ajxRequestWithMsg.open('get', 'inc/cart/removeFromCart.php?product_id='+id);
	ajxRequestWithMsg.onreadystatechange = showInfo;
	ajxRequestWithMsg.send(null);
	ajax_functions.push('cart_manage_zone();');
	ajax_functions.push('display_cart(true);');
}

function empty_cart(id){
	ajxRequestWithMsg.open('get', 'inc/cart/emptyCart.php');
	ajxRequestWithMsg.onreadystatechange = finalize_empty_cart;
	ajxRequestWithMsg.send(null);
	ajax_functions.push('cart_manage_zone();');
	ajax_functions.push('display_cart(true);');
}
function finalize_empty_cart(){
	if(ajxRequestWithMsg.readyState == 4){
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		//setColMiniCart();
	}/* else  {
		$$('#cart .load').set('html', loadingHTML());
	}*/
}

/*function reload_order( id, save ){
	
	if (confirm("La commande en cours va être remplacée. Continuer ?")) {
		
		ajxRequestList.open('get', 'inc/order/reloadOrder.php?order_id='+id+'&save='+save);
		ajxRequestList.onreadystatechange = showCart;
		ajxRequestList.send(null);
		
	}
	
}*/
command_id = null;
function reload_order( id ){
	
	if(id != 'nothing'){
		command_id = id;
		new MavDialog.Confirm({
			'force': true,
			'title': 'VOH SA',
			'message': 'La commande en cours va être remplacée. Continuer ?',
			'callback': reload_order_confirm_treat
		});
	}
}

function reload_order_confirm_treat(_confirmed) {
	//$('prompt_box').set('value', _prompted);
	/**/if (_confirmed!=null) {
		if (_confirmed) {
			new MavDialog.ExtendConfirm({
				'force': true,
				'title': 'VOH SA',
				'message': 'Voulez vous enregistrer cette commande avant de recharger votre panier ?',
				'callback': finished_reload_order_confirm_treat
			});
		}
	}
}

function finished_reload_order_confirm_treat(_confirmed) {
	//$('prompt_box').set('value', _prompted);
	if (_confirmed!=null) {
		if (_confirmed) {
			ajxRequestList.open('get', 'inc/order/reloadOrder.php?order_id='+command_id+'&save=yes');
			ajxRequestList.onreadystatechange = showCart;
			ajxRequestList.send(null);
			ajax_functions.push('cart_manage_zone();');
		} else {
			ajxRequestList.open('get', 'inc/order/reloadOrder.php?order_id='+command_id+'&save=no');
			ajxRequestList.onreadystatechange = showCart;
			ajxRequestList.send(null);	
			ajax_functions.push('cart_manage_zone();');
		}
	}
}

function showCart(){
	if(ajxRequestList.readyState == 4){
		document.getElementById("product_list").innerHTML = ajxRequestList.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		if(autoSelect == true){
			//select_first();
			auto_select('first');
		}
		
		setColDetailProduct();
		fitProductList('false');
	} else  {
		$$('#product_list .load').set('html', loadingHTML());
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : Update catalogue    /////////////////////////////
/////////////////////////////////////////////////////////////////////
function update_catalogue(action, id){
	catalogue_update = true;
	
	$('addToCatalogueBtn'+id).toggleClass('out');
	$('addToCatalogueBtn'+id).toggleClass('in');
	//alert($('addToCartBtn'+id).get('onclick'));
	$('addToCatalogueBtn'+id).removeEvents('click');
	$('addToCatalogueBtn'+id).erase('onclick');
	
	if(action == 'add'){	
		$('addToCatalogueBtn'+id).addEvent('click', function(){
			ajax_functions.push('update_catalogue(\'remove\', \''+id+'\');');
		});
	} else if(action == 'remove'){
		$('addToCatalogueBtn'+id).addEvent('click', function(){
			//update_cart('add', id, 1);
			ajax_functions.push('update_catalogue(\'add\', \''+id+'\');');
		});
	}

	catalogue_update = true;
		
	ajxRequestWithMsg.open('get', 'inc/catalogue/update.php?action='+action+'&product_id='+id);
	ajxRequestWithMsg.onreadystatechange = toto;
	ajxRequestWithMsg.send(null);
}
function toto(){
	if(ajxRequestWithMsg.readyState == 4){
				
		document.getElementById("infoTraitment").style.display = 'block';
		document.getElementById("infoTraitment").innerHTML = ajxRequestWithMsg.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		t2Out = setTimeout("change_display('infoTraitment', 'none')", 2000);
		
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : Display CATALOGUE    ////////////////////////////
/////////////////////////////////////////////////////////////////////
function display_catalogue(select_item){
	autoSelect = select_item;
	ajxRequestList.open('get', 'inc/catalogue/display.php');
	ajxRequestList.onreadystatechange = showCatalogue;
	
	ajxRequestList.send(null);
}
function clear_catalogue(){
	ajxRequest.open('get', 'inc/catalogue/clear.php');
	ajxRequest.onreadystatechange = clear_catalogue_finish;
	ajxRequest.send(null);
	ajax_functions.push('display_catalogue(false);');
}
function clear_catalogue_finish(){
	if(ajxRequest.readyState == 4){
		//change_display('product_detail', 'none')
		//document.getElementById("product_detail").innerHTML = '';
		//tOut = setTimeout("lounchAjaxFunctions()", 50);
		window.location.reload();
	}
}

function remove_from_catalogue(id){
	ajxRequestWithMsg.open('get', 'inc/catalogue/removeItem.php?product_id='+id);
	ajxRequestWithMsg.onreadystatechange = showInfo;
	ajxRequestWithMsg.send(null);
	ajax_functions.push('catalogue_manage_zone();');
	ajax_functions.push('display_catalogue(true);');
}
catalogue_id = null;
function reload_catalogue( id ){
	/**/if(id != 'nothing'){
		catalogue_id = id;
		new MavDialog.Confirm({
			'force': true,
			'title': 'VOH SA',
			'message': 'La liste en cours va être remplacée. Continuer ?',
			'callback': reload_catalogue_confirm_treat
		});
	}
}

function reload_catalogue_confirm_treat(_confirmed) {
	//$('prompt_box').set('value', _prompted);
	/**/if (_confirmed!=null) {
		if (_confirmed) {
			new MavDialog.ExtendConfirm({
				'force': true,
				'title': 'VOH SA',
				'message': 'Voulez vous enregistrer cette liste avant de recharger le catalogue sélectionné ?',
				'callback': finished_reload_catalogue_confirm_treat
			});
		}
	}
}

function finished_reload_catalogue_confirm_treat(_confirmed) {
	//$('prompt_box').set('value', _prompted);
	if (_confirmed!=null) {
		if (_confirmed) {
			ajxRequestList.open('get', 'inc/catalogue/reload.php?catalogue_id='+catalogue_id+'&save=yes');
			ajxRequestList.onreadystatechange = showCatalogue;
			ajxRequestList.send(null);
			ajax_functions.push('catalogue_manage_zone();');
		} else {
			ajxRequestList.open('get', 'inc/catalogue/reload.php?catalogue_id='+catalogue_id+'&save=no');
			ajxRequestList.onreadystatechange = showCatalogue;
			ajxRequestList.send(null);
			ajax_functions.push('catalogue_manage_zone();');
		}
	}
}
function showCatalogue(){
	if(ajxRequestList.readyState == 4){
		document.getElementById("product_list").innerHTML = ajxRequestList.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		if(autoSelect == true){
			//select_first();
			auto_select('first');
		}
		setColDetailProduct();
		fitProductList('false');
	} else  {
		document.getElementById("product_list").innerHTML = '<div class="loading"><img src="images/loading.gif" /></div>';
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : affichage formulaire d'inscription  /////////////
/////////////////////////////////////////////////////////////////////

function show_form_register(){
	ajxRequestCheckOut.open('get', 'inc/client/formRegister.php');
	ajxRequestCheckOut.onreadystatechange = showFormInCheckOutZone;
	ajxRequestCheckOut.send(null);
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : affichage formulaire de commande  ///////////////
/////////////////////////////////////////////////////////////////////

function show_form_order(){
	ajxRequestCheckOut.open('get', 'inc/order/formOrder.php');
	ajxRequestCheckOut.onreadystatechange = showFormInCheckOutZone;
	ajxRequestCheckOut.send(null);
}

function showFormInCheckOutZone(){
	if(ajxRequestCheckOut.readyState == 4){
		change_display('glob', 'block');
		change_display('checkout', 'block');
		document.getElementById("checkout").innerHTML = ajxRequestCheckOut.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		$$('#loader').setStyle('display', 'none');
		
		new Fx.Accordion($('accordion'), '#accordion h3', '#accordion .content');

		
		/*var container = $('main');
 
		new Drag.Move('checkout', {'container': container});
		*/
		
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : envois du formulaire d'inscription  /////////////
/////////////////////////////////////////////////////////////////////
function register(){
	whatToDoWithInfo = 'refreshForm';
	sendValues = '';
	form = document.forms['formInscription'];
	inputs = form.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++) {
		if(inputs[i].name != 'sendButton'){
			if(sendValues != ''){
				sendValues += '&';
			}
			sendValues += inputs[i].name+'='+inputs[i].value;
		}
	}
	selects = form.getElementsByTagName('select');
	for (var j=0; j<selects.length; j++) {
		if(sendValues != ''){
			sendValues += '&';
		}
		sendValues += selects[j].name+'='+selects[j].options[selects[j].selectedIndex].value;
	}
	textareas = form.getElementsByTagName('textarea');
	for (var k=0; k<textareas.length; k++) {
		if(sendValues != ''){
			sendValues += '&';
		}
		sendValues += textareas[k].name+'='+textareas[k].value;
	}
	
	ajxRequestCheckOut.open('post', 'inc/client/register.php');
	ajxRequestCheckOut.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajxRequestCheckOut.onreadystatechange = showFormInCheckOutZone;
	ajxRequestCheckOut.send(sendValues);
}

// confirmation de l'inscription
function confirm_registration(id){
	//alert('confirm_registration: ' + id);
	ajxRequestWithMsg.open('get', 'inc/client/confirm.php?id='+id);
	ajxRequestWithMsg.onreadystatechange = finalise_confirmation;
	ajxRequestWithMsg.send(null);
	/*document.getElementById("glob").style.display = 'block';
	document.getElementById("infoTraitment").style.display = 'block';
	document.getElementById("infoTraitment").style.height = '60px';*/
}
function finalise_confirmation(){
	if(ajxRequestWithMsg.readyState == 4){
		//alert(ajxRequestWithMsg.responseText);
		document.getElementById("glob").style.display = 'block';
		document.getElementById("infoTraitment").style.display = 'block';
		document.getElementById("infoTraitment").innerHTML = ajxRequestWithMsg.responseText;
		document.getElementById("infoTraitment").style.height = 'auto';
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		t2Out = setTimeout("change_display('infoTraitment', 'none');change_display('glob', 'none')", 5000);
	} else  {
		//document.getElementById("infoTraitment").innerHTML += loadingHTML();
		//$$('#infoTraitment .load').set('html', loadingHTML());
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : envois du formulaire de commande  /////////////
/////////////////////////////////////////////////////////////////////
function order(formActive){
	
	$$('#loader').setStyle('display', 'block');
	
	whatToDoWithInfo = 'refreshForm';
	sendValues = '';
	form = document.forms['formOrder'];
	inputs = form.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++) {
		if(inputs[i].name != 'sendButton' && inputs[i].name != 'send_method'){
			if(inputs[i].name == 'getAccountCHBX'){
				if(sendValues != ''){
					sendValues += '&';
				}
				if(inputs[i].checked==true){
					sendValues += inputs[i].name+'=true';
				} else {
					sendValues += inputs[i].name+'=false';
				}
			} else {
				if(sendValues != ''){
					sendValues += '&';
				}
				sendValues += inputs[i].name+'='+inputs[i].value;
			}	
		}
	}
	selects = form.getElementsByTagName('select');
	for (var j=0; j<selects.length; j++) {
		if(sendValues != ''){
			sendValues += '&';
		}
		sendValues += selects[j].name+'='+selects[j].options[selects[j].selectedIndex].value;
	}
	textareas = form.getElementsByTagName('textarea');
	for (var k=0; k<textareas.length; k++) {
		if(sendValues != ''){
			sendValues += '&';
		}
		sendValues += textareas[k].name+'='+textareas[k].value;
	}
	// récup de la méthode d'envois
	//document.forms['formInscription'].elements['send_method']
	send_method = 'mail';
	for (var l=0; l<form.elements['send_method'].length; l++) {
		if(form.elements['send_method'][l].checked){
			if(sendValues != ''){
				sendValues += '&';
			}
			sendValues += 'send_method='+form.elements['send_method'][l].value;
			
			if(form.elements['send_method'][l].value == 'fax'){
				//ajax_functions.push('print_order();');
				send_method = 'fax';
			}
		}
	}
	sendValues += '&formActive='+formActive;
	
	
	/*if (send_method == 'fax'){
		//alert('checkout_print');
		ajxRequest.open('post', 'inc/checkout/checkout_print.php');
		ajxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajxRequest.onreadystatechange = displayOrderPdf;
		ajxRequest.send(sendValues);
	} else if (send_method == 'mail'){*/
		ajxRequestCheckOut.open('post', 'inc/checkout/checkout.php');
		ajxRequestCheckOut.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajxRequestCheckOut.onreadystatechange = showFormInCheckOutZone;
		ajxRequestCheckOut.send(sendValues);
	/*}*/
	
}

function print_order( $order_id ){
	ajxRequestWithMsg.open('get', 'inc/order/print.php?order_id='+order_id);
	ajxRequestWithMsg.onreadystatechange = displayOrderPdf;
	ajxRequestWithMsg.send(null);
}

function displayOrderPdf(){
	
	//alert('print_order');
	
	if(ajxRequest.readyState == 4){
		//alert(ajxRequest.responseText);
		window.open('order_printer.php?order_id='+ajxRequest.responseText,"newFenetre","width=1024,height=768");
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		$$('#loader').setStyle('display', 'none');
		closeWindow('checkout');
	}
	/*if(ajxRequestWithMsg.readyState == 4){
		window.open('test',"newFenetre","width=1024,height=768");
	}*/
}

function displayOrderPdf2(id){
	window.open('order_printer.php?order_id='+id,"newFenetre","width=1024,height=768");
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : envois du formulaire de contact  /////////////
/////////////////////////////////////////////////////////////////////
function contact(){
	whatToDoWithInfo = 'refreshForm';
	sendValues = '';
	form = document.forms['formContact'];
	inputs = form.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++) {
		if(inputs[i].name != 'sendButton' && inputs[i].name != 'send_method'){
			if(sendValues != ''){
				sendValues += '&';
			}
			sendValues += inputs[i].name+'='+inputs[i].value;
		}
	}
	selects = form.getElementsByTagName('select');
	for (var j=0; j<selects.length; j++) {
		if(sendValues != ''){
			sendValues += '&';
		}
		sendValues += selects[j].name+'='+selects[j].options[selects[j].selectedIndex].value;
	}
	textareas = form.getElementsByTagName('textarea');
	for (var k=0; k<textareas.length; k++) {
		if(sendValues != ''){
			sendValues += '&';
		}
		sendValues += textareas[k].name+'='+textareas[k].value;
	}
	// récup de la méthode d'envois
	for (var l=0; l<form.elements['contact_method'].length; l++) {
		if(form.elements['contact_method'][l].checked){
			if(sendValues != ''){
				sendValues += '&';
			}
			sendValues += 'contact_method='+form.elements['contact_method'][l].value;
		}
	}
	
	ajxRequestFormContact.open('post', 'inc/contact/mail.php');
	ajxRequestFormContact.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajxRequestFormContact.onreadystatechange = showInfoInContactZone;
	ajxRequestFormContact.send(sendValues);
}

function showInfoInContactZone(){
	if(ajxRequestFormContact.readyState == 4){
		document.getElementById("zone_form_contact").innerHTML = ajxRequestFormContact.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : login / logout  /////////////////////////////////
/////////////////////////////////////////////////////////////////////
function login(){
	//whatToDoWithInfo = 'createLoginPage';
	ajxRequestCheckOut.open('post', 'inc/client/login.php');
	ajxRequestCheckOut.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajxRequestCheckOut.onreadystatechange = showFormInCheckOutZone;
	ajxRequestCheckOut.send("login="+document.forms['formLogin'].elements['login'].value+"&passwd="+document.forms['formLogin'].elements['passwd'].value);
	//tOutRefresh = setTimeout("changeLocation('index.php');", 3000);
}

function logout(){
	ajxRequestWithMsg.open('get', 'inc/client/logout.php');
	ajxRequestWithMsg.onreadystatechange = showInfo;
	ajxRequestWithMsg.send(null);
	tOutRefresh = setTimeout("changeLocation('index.php');", 3000);
}


function quickLogin(){
	//whatToDoWithInfo = 'createLoginPage';
	ajxRequestWithMsg.open('post', 'inc/client/quicklogin.php');
	ajxRequestWithMsg.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajxRequestWithMsg.onreadystatechange = showInfo;
	ajxRequestWithMsg.send("login="+document.forms['formQuickLogin'].elements['login'].value+"&passwd="+document.forms['formQuickLogin'].elements['passwd'].value);
	tOutRefresh = setTimeout("changeLocation('"+document.forms['formQuickLogin'].elements['page'].value+"');", 5000);
}

function replace_quickLoginForm(){
	if(ajxRequestQuickLogin.readyState == 4){
		document.getElementById("quickLogin").innerHTML = '<a href="javascript:;" onclick="logout();">logout</a>';
		tOut = setTimeout("lounchAjaxFunctions()", 50);
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : Récupération de mot de passe  ///////////////////
/////////////////////////////////////////////////////////////////////

function forgetPassWord(){
	ajxRequestTinyForm.open('get', 'inc/client/form_forget_password.php');
	ajxRequestTinyForm.onreadystatechange = showFormInTinyFormZone;
	ajxRequestTinyForm.send(null);
}
function resetPassWord(){
	$$('#loader').setStyle('display', 'block');
	ajxRequestTinyForm.open('post', 'inc/client/resetPassWord.php');
	ajxRequestTinyForm.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajxRequestTinyForm.onreadystatechange = showFormInTinyFormZone;
	ajxRequestTinyForm.send("login="+document.forms['formResetPassWord'].elements['login'].value+"&captchaUser="+document.forms['formResetPassWord'].elements['captchaUser'].value);

}

function changePassWord(){
	ajxRequestTinyForm.open('get', 'inc/client/form_change_password.php');
	ajxRequestTinyForm.onreadystatechange = showFormInTinyFormZone;
	ajxRequestTinyForm.send(null);
}
refreshIt = false;
function storeNewPassWord(){
	refreshIt = true;
	ajxRequestTinyForm.open('post', 'inc/client/store_new_password.php');
	ajxRequestTinyForm.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajxRequestTinyForm.onreadystatechange = showFormInTinyFormZone;
	ajxRequestTinyForm.send("oldPwd="+document.forms['formChangePassWord'].elements['oldPwd'].value+"&pwd="+document.forms['formChangePassWord'].elements['pwd'].value+"&captchaUser="+document.forms['formChangePassWord'].elements['captchaUser'].value);
}

function changeProfil(){
	ajxRequestTinyForm.open('get', 'inc/client/form_change_profil.php');
	ajxRequestTinyForm.onreadystatechange = showFormInTinyFormZone;
	ajxRequestTinyForm.send(null);
}
function storeProfil(){
	sendValues = '';
	form = document.forms['formProfil'];
	inputs = form.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++) {
		if(inputs[i].name != 'sendButton'){
			if(sendValues != ''){
				sendValues += '&';
			}
			sendValues += inputs[i].name+'='+inputs[i].value;
		}
	}
	selects = form.getElementsByTagName('select');
	for (var j=0; j<selects.length; j++) {
		if(sendValues != ''){
			sendValues += '&';
		}
		sendValues += selects[j].name+'='+selects[j].options[selects[j].selectedIndex].value;
	}
	
	ajxRequestTinyForm.open('post', 'inc/client/store_profil.php');
	ajxRequestTinyForm.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajxRequestTinyForm.onreadystatechange = showFormInTinyFormZone;
	ajxRequestTinyForm.send(sendValues);
}



function showFormInTinyFormZone(){
	if(ajxRequestTinyForm.readyState == 4){
		change_display('glob', 'block');
		change_display('tinyForm', 'block');
		$$('#loader').setStyle('display', 'none');
		document.getElementById("tinyForm").innerHTML = ajxRequestTinyForm.responseText;
		test = ajxRequestTinyForm.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		
		if(refreshIt == true){
			tOutRefresh = setTimeout("changeLocation('index.php');", 3000);
		}
		/*
		return hs.htmlExpand(null, { 
							 			contentId: 'tinyForm',
										width: 500,
										height: 'auto',
										dimmingOpacity: 0.75, 
										align: 'center',
										wrapperClassName: 'draggable-header',
										preserveContent: false
										
										
									} )
		popUP();*/
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : affichage Gestion commande  /////////////////////
/////////////////////////////////////////////////////////////////////

function order_manager(){
	ajxManager.open('get', 'inc/order/order_manager.php');
	ajxManager.onreadystatechange = show_order_manager;
	ajxManager.send(null);
}

function save_order( title, message ){
	
	new MavDialog.Prompt({
		'force': true,
		'title': title,
		'message': message+' :',
		'callback': save_order_prompt_treat
	});

	
	/*var saisie = prompt("Nom :", "Commande")
	if (saisie!=null) {
		if (saisie!='') {
			ajxManager.open('get', 'inc/order/saveOrder.php?name='+saisie);
			ajxManager.onreadystatechange = show_order_manager;
			ajxManager.send(null);
			ajax_functions.push('display_cart(true);');
		} else {
			alert(saisie+'Veuillez saisir un nom pour la commande');	
		}
	}*/
	
}

function save_order_prompt_treat(_prompted) {
	//$('prompt_box').set('value', _prompted);
	if (_prompted!=null) {
		if (_prompted!='') {
			ajxRequestWithMsg.open('get', 'inc/order/saveOrder.php?name='+_prompted);
			ajxRequestWithMsg.onreadystatechange = showInfo;
			ajxRequestWithMsg.send(null);
			ajax_functions.push('cart_manage_zone();');
		} else {
			alert('vide');	
		}
	}
}

function save_current_order( id ){
	
	ajxRequestWithMsg.open('get', 'inc/order/saveCurrentOrder.php?id='+id);
	ajxRequestWithMsg.onreadystatechange = showInfo;
	ajxRequestWithMsg.send(null);
	
}


function show_order_manager(){
	if(ajxManager.readyState == 4){
		document.getElementById("manager").style.display = 'block';
		document.getElementById("manager").innerHTML = ajxManager.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : affichage Gestion catalogue  /////////////////////
/////////////////////////////////////////////////////////////////////

function catalogue_manager(){
	ajxManager.open('get', 'inc/catalogue/manager.php');
	ajxManager.onreadystatechange = show_catalogue_manager;
	ajxManager.send(null);
}

function save_catalogue( name ){
	ajxManager.open('get', 'inc/catalogue/save.php?name='+name);
	ajxManager.onreadystatechange = show_catalogue_manager;
	ajxManager.send(null);
	ajax_functions.push('display_catalogue(true);');
}

function save_catalogue(){
	
	new MavDialog.Prompt({
		'force': true,
		'title': 'Enregistrer la liste en cours',
		'message': 'Nom :',
		'callback': save_catalogue_prompt_treat
	});
	
}

function save_catalogue_prompt_treat(_prompted) {
	//$('prompt_box').set('value', _prompted);
	if (_prompted!=null) {
		if (_prompted!='') {
			ajxRequestWithMsg.open('get', 'inc/catalogue/save.php?name='+_prompted);
			ajxRequestWithMsg.onreadystatechange = showInfo;
			ajxRequestWithMsg.send(null);
			ajax_functions.push('catalogue_manage_zone();');
		} else {
			alert('vide');	
		}
	}
}

function save_current_catalogue( id ){
	
	ajxRequestWithMsg.open('get', 'inc/catalogue/saveCurrent.php?id='+id);
	ajxRequestWithMsg.onreadystatechange = showInfo;
	ajxRequestWithMsg.send(null);
	
}

/*function print_catalogue( ){
	ajxRequestWithMsg.open('get', 'inc/catalogue/print.php');
	ajxRequestWithMsg.onreadystatechange = affichePdf;
	ajxRequestWithMsg.send(null);
	ajax_functions.push('display_catalogue(true);');
}

function affichePdf(){
	if(ajxRequestWithMsg.readyState == 4){
		//alert('toto');
		window.open("http://shop.voh.ch/catalogues/catalogue.pdf","newFenetre","width=1024,height=768");
	}
}*/



function show_catalogue_manager(){
	if(ajxManager.readyState == 4){
		document.getElementById("manager").style.display = 'block';
		document.getElementById("manager").innerHTML = ajxManager.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : affichage manage Zone for cart  /////////////////
/////////////////////////////////////////////////////////////////////

function cart_manage_zone(){
	ajxManager.open('get', 'inc/order/manage_zone.php');
	ajxManager.onreadystatechange = show_cart_manage_zone;
	ajxManager.send(null);
}
function show_cart_manage_zone(){
	if(ajxManager.readyState == 4){
		document.getElementById("manage_zone").innerHTML = ajxManager.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : affichage manage Zone for catalogue   ///////////
/////////////////////////////////////////////////////////////////////

function catalogue_manage_zone(){
	ajxManager.open('get', 'inc/catalogue/manage_zone.php');
	ajxManager.onreadystatechange = show_catalogue_manage_zone;
	ajxManager.send(null);
}
function show_catalogue_manage_zone(){
	if(ajxManager.readyState == 4){
		document.getElementById("manage_zone").innerHTML = ajxManager.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
	}
}

/////////////////////////////////////////////////////////////////////
///////  FUNCTION : Affichage des informations de traitement   //////
/////////////////////////////////////////////////////////////////////

// fonction d'affichage des informations en retour à un traitement
function showInfo(){
	if(ajxRequestWithMsg.readyState == 4){
		
		document.getElementById("glob").style.display = 'block';
		document.getElementById("infoTraitment").style.display = 'block';
		document.getElementById("infoTraitment").innerHTML = ajxRequestWithMsg.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		t2Out = setTimeout("change_display('infoTraitment', 'none');change_display('glob', 'none')", 6000);
	}
}

function showInfoBis(){
	if(ajxRequestWithMsg.readyState == 4){
		
		document.getElementById("glob").style.display = 'block';
		document.getElementById("infoTraitment").style.display = 'block';
		document.getElementById("infoTraitment").innerHTML = ajxRequestWithMsg.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
		t2Out = setTimeout("change_display('infoTraitment', 'none');change_display('glob', 'none')", 1000);
	}
}

function showFormMessage(){
	if(ajxRequest.readyState == 4){
		change_display("errorFormMsg", "block");
		document.getElementById("errorFormMsg").innerHTML = ajxRequest.responseText;
		tOut = setTimeout("lounchAjaxFunctions()", 50);
	}
}

ajxRequest=creeXMLHttpRequestObject();
ajxManager=creeXMLHttpRequestObject();
ajxRequestQuickLogin=creeXMLHttpRequestObject();
ajxRequestManageZone=creeXMLHttpRequestObject();
ajxRequestList=creeXMLHttpRequestObject();
ajxRequestDetail=creeXMLHttpRequestObject();
ajxRequestMiniCart=creeXMLHttpRequestObject();
ajxRequestCheckOut=creeXMLHttpRequestObject();
ajxRequestTinyForm=creeXMLHttpRequestObject();
ajxRequestFormContact=creeXMLHttpRequestObject();
ajxRequestWithMsg=creeXMLHttpRequestObject();
