function getHTTPObject()
{
	if(window.XMLHttpRequest)
		return new XMLHttpRequest(); 
	else if(window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP"); 
	else {
		return; 
	}
}

var SFcounter_page = 0;
function refreshCounter(lng)
{
	setTimeout("refreshCounter('"+lng+"')",500);
	
	var cp = parseInt(SFcounter_page) + 9;
	SFcounter_page = cp;
	cp = number_format(cp, 0);
	if(document.getElementById('counterp'))
		document.getElementById('counterp').innerHTML = formatToCounter(cp.toString());
		
}


function getCounterPage(lng)
{
		//setTimeout("getCounterPage('"+lng+"')",5000);
		var xhr_object = null;
		xhr_object = getHTTPObject();
		var dDate=new Date();
		xhr_object.open("GET", "/mag/"+lng+"/counterSF.php?page=1&dt="+dDate.getTime(), true); 
		
		xhr_object.onreadystatechange = function() { 
			if(xhr_object.readyState == 4) {
				var reponse = xhr_object.responseText;
				//alert(reponse);
				SFcounter_page = reponse;
				
			}
		}
		//alert(SFcounter_pages);
		xhr_object.send(null);
}

function formatToCounter(number)
{
	var counter = "";
	for(var i=0; i<number.length;i++)
	{
		if(number[i]=='.')
			counter += '<span class="t9">.</span>';
		else if(number[i]==',')
			counter += '<span class="t9">,</span>';
		else
			counter += '<span class="n'+number[i]+'">&nbsp;</span>';
	}
	return counter;
}

function getCounter(lng)
{
		setTimeout("getCounter('"+lng+"')",10000);
		
		getCounterPage(lng);
		
		var xhr_object = null;
		xhr_object = getHTTPObject();
		var dDate=new Date();
		xhr_object.open("GET", "/mag/"+lng+"/counterSF.php?dt="+dDate.getTime(), true); 
		
		xhr_object.onreadystatechange = function() { 
			if(xhr_object.readyState == 4) {
				var reponse = xhr_object.responseText;
				if(document.getElementById('number'))
					document.getElementById('number').innerHTML = reponse;
			}
		}
		xhr_object.send(null);
}


function loadCategory(listbox, asc_id, asv_parentid, selected_value, site, lang, store_id)
{
	if(listbox){
		var xhr_object = null;
		xhr_object = getHTTPObject();
		//alert("/mag/getCategory.php?asc_id="+asc_id+"&asv_parentid="+asv_parentid+"&langid="+lang+"&store_id="+store_id);
		if(site==null)
			site="BO";
		if(site=="BO")
			xhr_object.open("GET", "/getCategory.php?asc_id="+asc_id+"&asv_parentid="+asv_parentid, true); 
		else
			xhr_object.open("GET", "/mag/getCategory.php?asc_id="+asc_id+"&asv_parentid="+asv_parentid+"&langid="+lang+"&store_id="+store_id, true);
		
		reinitList(listbox);
	
		xhr_object.onreadystatechange = function() { 
			if(xhr_object.readyState == 4) {
				var reponse = xhr_object.responseText;
				var ligne=reponse.split("<br/>");
				for (var i=0; i<ligne.length; i++) {
					if(ligne[i]!='') {
						var champ=ligne[i].split("|||");
						var idx = listbox.options.length;
						listbox.options[idx] = new Option(champ[1], champ[0]);
						if(selected_value==champ[0]) {
							listbox.options[idx].selected = true;
						}
					}
				}
	
			}
		}
	
		xhr_object.send(null);
	}
}

var numit_temp=0;
function getBasket(lang)
{
	var supp=0;
	var right=0;
	var xhr_object = null;
	xhr_object = getHTTPObject();
	
	xhr_object.open("GET", "/mag/getBasket.php?lang="+lang+"&rnd="+Math.random(), true); 

	xhr_object.onreadystatechange = function() { 
		if(xhr_object.readyState == 4) {
			var reponse = xhr_object.responseText;
			//alert(reponse);
			if(document.getElementById('totalit_basket'))
				supp=1;
				
			if(document.getElementById('d_basket_detail'))
				right=1;
			
			var ligne=reponse.split("<br/>");
			var numit=0; var totalit=0;
			var basket_html = "";
			for (var i=0; i<ligne.length; i++) {
				if(ligne[i]!='') {
					var champ=ligne[i].split("|||");
					if(supp=='1' || right=='1'){
						var champ12 = parseInt(champ[1])*parseFloat(champ[2]);
						totalit += champ12;
						if(supp=='1'){
							numit +=parseInt(champ[1]);
						}
						if(right=='1'){ 
							var champ1 = parseInt(champ[1])-1;
							var tp;
							if(champ[1]==1) tp='del'; else tp='rem';
							var champ2 = parseInt(champ[1])+1;
							//var vl_champ2 = Math.round(champ12*100)/100;
							var vl_champ2 = champ12.toFixed(2);
							basket_html = basket_html + "\r\n<span class=\"primary\">"+champ[3]+"</span><div class=\"quantity\"><input class='btn_quantity' type=button value='-' onclick=\"quantity('moins',document.getElementById('input_"+i+"'));updateBasket('"+champ[0]+"', '"+tp+"', '&bskit_id="+champ[4]+"&ctx_qty_"+champ[4]+"="+champ1+"&lang="+lang+champ[5]+"')\"><input id='input_"+i+"' type='hidden' disabled value='"+champ[1]+"'/><span class=\"quantity_value\">"+champ[1]+"</span><input class='btn_quantity' type='button' value='+' onclick=\"quantity('plus',document.getElementById('input_"+i+"'));updateBasket('"+champ[0]+"', 'add', '&bskit_id="+champ[4]+"&ctx_qty_"+champ[4]+"="+champ2+"&lang="+lang+champ[5]+"')\"></div><span class=\"price\">"+vl_champ2+sf_currency+"</span>";
						}
					}
				}
			}
			if(totalit>0){
				//totalit=Math.round(totalit*100)/100;
				totalit=totalit.toFixed(2);
			}
			if(supp=='1'){
				showChar(numit);
				if(numit>0){ document.getElementById('numit_basket').innerHTML = numit;}
				if(totalit>0){ 
					document.getElementById('totalit_basket').innerHTML = totalit;
					showChar(numit,1);
				}else{
					document.getElementById('totalit_basket').innerHTML = '';
				}
			}
			if(right=='1'){
				if(document.getElementById('total_dbasket') && document.getElementById('d_basket_detail')){
					document.getElementById('d_basket_detail').innerHTML = basket_html;
					showChar_dbasket(totalit);
					document.getElementById('total_dbasket').innerHTML = totalit;
				}
			}			
		}
	}
	
	xhr_object.send(null);
}

function reinitList(listbox)
{
	if(listbox){
		listbox.options.length = 0; 
		listbox.options[listbox.options.length] = new Option("", 0);
	}
}

function loadSentensPopupGroupeDefaut(valueCPID, typeCP, store_id, GPdefaut)
{
	var xhr_object = null;
	xhr_object = getHTTPObject();
	xhr_object.open("GET", "/mag/groupSelect.php?valueCPID="+valueCPID+"&typeCP="+typeCP+"&store_id="+store_id+"&GPdefaut="+GPdefaut, true);
	
	xhr_object.onreadystatechange = function() { 
		if(xhr_object.readyState == 4) 
		{
			var reponse = xhr_object.responseText;
			if(reponse=="ok")
			{
				document.location.reload();
			}
			else
			{
				document.getElementById('divSelectionGroup').style.visibility='hidden';
				document.getElementById('divGroupDefaut').style.visibility='visible';
			}
		}
	}
		xhr_object.send(null);
}

function getPrice(product_id){
	
	var nb=1;
	if(document.getElementById('nb'))
		nb = document.getElementById('nb').value
		
	var optid_array = new Array();	
	var i=1;
	while(document.getElementById('hidden_choice_opt_'+i))
	{
		optid_array.push(document.getElementById('hidden_choice_opt_'+(i)).value);
		i++;
	}
		
	//calcul prix
	var xhr_object = null;
	xhr_object = getHTTPObject();
	xhr_object.open("GET", "/mag/getPrice.php?product_id="+product_id+"&nb="+nb+"&optid_array="+optid_array, true); 
	xhr_object.onreadystatechange = function() { 
		if(xhr_object.readyState == 4) {
			var getPrice = xhr_object.responseText;
			var champ=getPrice.split("|");
			
			if(document.getElementById('ajaxPriceTTC'))
				document.getElementById('ajaxPriceTTC').innerHTML = champ[0];
			
			if(document.getElementById('ajaxPriceHT'))
				document.getElementById('ajaxPriceHT').innerHTML =  champ[1];				
		}
	}
	xhr_object.send(null);
}

function checkStockAndAdd(pdtId, action, param){
	var optid_array = new Array();
	var listoptid_array = new Array();
	var opt_param="";
	if(action.substring(0,4)!="list")
	{
		if(action=="addAndRefresh")
		{
			var i=1;
			while(getValueFromParams(param, 'hidden_choice_opt_'+i)!=null)
			{
				optid_array.push(getValueFromParams(param, 'hidden_choice_opt_'+i));
				listoptid_array.push(getValueFromParams(param, 'hidden_choice_listopt_'+i));
				i++;
			}
		}
		else
		{
			var i=1;
			while(document.getElementById('hidden_choice_opt_'+i))
			{
				optid_array.push(document.getElementById('hidden_choice_opt_'+i).value);
				listoptid_array.push(document.getElementById('hidden_choice_listopt_'+i).value);
				opt_param = opt_param + "&input_choice_opt_"+i + "=" + document.getElementById('input_choice_opt_'+i).value;
				opt_param = opt_param + "&hidden_choice_opt_"+i + "=" + document.getElementById('hidden_choice_opt_'+i).value;
				opt_param = opt_param + "&hidden_choice_listopt_"+i + "=" + document.getElementById('hidden_choice_listopt_'+i).value;
				i++;
			}
		
		}
	}
	else
	{
		if(action=="listPopup")
			action="popup";	
	}
	
	var xhr_object = null;
	xhr_object = getHTTPObject();
	
	var bid = getValueFromParams(param, 'bid');
	
	if(bid!=null){
		xhr_object.open("GET", "/mag/getStockReservation.php?bid="+bid+param+"&rnd="+Math.random(), true); 
		
	}else
		xhr_object.open("GET", "/mag/getStock.php?pdtId="+pdtId+"&listoptid_array="+listoptid_array+"&optid_array="+optid_array+"&rnd="+Math.random(), true); 
		
	xhr_object.onreadystatechange = function() { 
		if(xhr_object.readyState == 4) {
			var reponse = xhr_object.responseText;
			
			if(reponse=="0"){
				document.getElementById('alertStock').style.display = "inline";
				//setTimeout("document.getElementById('alertStock').style.display = 'none';",1500);
			}
			else if(reponse=="notSelected1"){
				document.getElementById('alertOpt').style.display = "inline";
				//setTimeout("document.getElementById('alertOpt').style.display = 'none';",1500);
			}
			else if(reponse=="notSelected2"){
				document.getElementById('alertOpt').style.display = "inline";
				//setTimeout("document.getElementById('alertOpt').style.display = 'none';",1500);
			}
			else{
				document.getElementById('alertStock').style.display = "none";
				if(action=="pdtToBasket"){
					document.getElementById('formulaire').action="/mag/"+getValueFromParams(param, "lang")+"/basket.php?action=insert&"+param+opt_param;
					document.getElementById('formulaire').submit();
				}
				else if(action=="formToBasket"){
					document.getElementById('form1').action="/mag/"+getValueFromParams(param, "lang")+"/form_post.php?action=home&"+param+opt_param;
					document.getElementById('form1').submit();
				}
				else if(action=="popup"){
					param += '&instock='+reponse;
					setBasket(pdtId, "set", param+opt_param);
				}
				else if(action=="addAndRefresh"){
					param += '&instock='+reponse;
					setBasket(pdtId, "add", param+opt_param);
				}
				else if(action=="listPdtToBasket"){
					document.getElementById('formulaire_list').action="/mag/"+getValueFromParams(param, "lang")+"/basket.php?action=insert&"+param+opt_param;
					document.getElementById('formulaire_list').submit();
				}
				else if(action=="listPdtBskToBasket"){
					document.getElementById('formulaire_bsk_list').action="/mag/"+getValueFromParams(param, "lang")+"/basket.php?action=insert&"+param+opt_param;
					document.getElementById('formulaire_bsk_list').submit();
				}
				else if(action=="listPdtPageToBasket"){
					document.getElementById('formulaire2').action="/mag/"+getValueFromParams(param, "lang")+"/basket.php?action=insert&"+param+opt_param;
					document.getElementById('formulaire2').submit();
				}
				else if(action=="listPdtListToBasket" || action=="listPdtHomeToBasket"){
					document.getElementById('formulaire').action="/mag/"+getValueFromParams(param, "lang")+"/basket.php?action=insert&"+param+opt_param;
					document.getElementById('formulaire').submit();
				}
				else if(action=="listPdtSearchToBasket"){
					document.getElementById('formulaire_search').action="/mag/"+getValueFromParams(param, "lang")+"/basket.php?action=insert&"+param+opt_param;
					document.getElementById('formulaire_search').submit();
				}
				else if(action=="listPdtBskToShoppingCart"){
					document.getElementById('formulaire_bsk_list').action="/mag/"+getValueFromParams(param, "lang")+"/shoppingcart.php?action=insert&"+param+opt_param;
					document.getElementById('formulaire_bsk_list').submit();
				}
			}
		}
	}
	xhr_object.send(null);
}

function updateBasket(pdtId, action, param){
	
	if(action=="rem") // On retire une unite
	{
		setBasket(pdtId, "rem", param);	
	}
	else if(action=="del") // On supprime le produit
	{
		setBasket(pdtId, "del", param);	
	}
	else if(action=="add") // On ajoute une unite
	{
		checkStockAndAdd(pdtId, "addAndRefresh", param);
	}
}


function setBasket(pdtId, action, param){
	var xhr_object = null;
	xhr_object = getHTTPObject();
	xhr_object.open("GET", "/mag/setBasket.php?id="+pdtId+"&type="+action+"&rnd="+Math.random()+param, true); 
	xhr_object.onreadystatechange = function() { 
		if(xhr_object.readyState == 4) {
			getBasket(getValueFromParams(param, "lang"));
			var reponse = xhr_object.responseText;
			if(action!="del" && action!="rem")
			{
				if(getValueFromParams(param, "bid")!=null){
					if(document.getElementById('id_btn_getprestation'))
						document.getElementById('id_btn_getprestation').setAttribute('disabled', 'disabled');
					document.getElementById('popup_div_reservation').style.display = 'inline';
					//setTimeout("document.getElementById('popup_div_res_bkjs').style.display = 'none';",2250);
				}else{	
					var instock = getValueFromParams(param, "instock");
					var stockorder = getValueFromParams(param, "nb");
					if(parseInt(stockorder)>parseInt(instock)){
						popup_alert('stocklimit','popup_div_stocklimit',instock)
						setTimeout("document.getElementById('popup_div_stocklimit').style.display = 'none';",2250);
	
					}else{
						document.getElementById('popup_div_bkjs').style.display = 'inline';
						document.getElementById('content_popup_div_bkjs').innerHTML = reponse;
						//setTimeout("document.getElementById('popup_div_bkjs').style.display = 'none';",2250);
					}
				}
			}
		}
	}
	xhr_object.send(null);
}

function getValueFromParams(params, param)
{
	var tbParam=params.split("&");
	for (var i=0; i<tbParam.length; i++)
	{
		if(tbParam[i].substring(0,param.length)==param)
		{
			var val=tbParam[i].split("=");
			return val[1];
		}
	}
	return null;
}

function getReversoForm(number, idco, host)
{
	var xhr_object = null;
	xhr_object = getHTTPObject();
	
	xhr_object.open("GET", "/mag/getReversoForm.php?number="+number+"&idco="+idco+"&host="+host+"&rnd="+Math.random(), true);
	
	xhr_object.onreadystatechange = function() { 
		if(xhr_object.readyState == 4) {
			var reponse = xhr_object.responseText;
			
			eval(reponse);
			
			}
		}
	xhr_object.send(null);
}


