// Librairie de scripts 


//****************************************************************
// Fonction d'ajout d'articles dans le panier
//****************************************************************
function funcAddBasket(varId, varWebId, varPanierMini, varDevis, varPanierLib) {
	var oXmlHttp = null;
	// Mozilla
	if (window.XMLHttpRequest)
		{
		oXmlHttp = new XMLHttpRequest();
		}
	// IE
	else if (window.ActiveXObject)
		{
		oXmlHttp = new ActiveXObject("Microsoft.XmlHttp");
		}
		
	if (oXmlHttp != null) {
		if (varDevis.toLowerCase() == 'true')
			varDevis = '0';
		else
			varDevis = '1';
		
		var strDetail = '';
		var strName = '';
		for (var k=1; k<8; k++)
			if (document.getElementById('sel_feature_' + k))
				{
				if (k > 1)
					strDetail = strDetail +  " - "
				if (document.getElementById('sel_feature_' + k).selectedIndex != null)
					strDetail = strDetail + document.getElementById('sel_feature_' + k).title + ' : ' + document.getElementById('sel_feature_' + k).options[document.getElementById('sel_feature_' + k).selectedIndex].innerHTML;
				else
					strDetail = strDetail + document.getElementById('sel_feature_' + k).title + ' : ' + document.getElementById('sel_feature_' + k).value;
				}
		
		if (document.getElementById('sel_comment'))
			strDetail = strDetail + " - " + document.getElementById('sel_comment').value;
		
		var intQte
		if (document.getElementById('art_Qte'))
			intQte = document.getElementById('art_Qte').value;
		else
			intQte = 1;

		if (document.getElementById('art_Ref'))
			strName = document.getElementById('art_Ref').innerHTML + ' - ';

		if (document.getElementById('art_Name'))
			strName = strName + document.getElementById('art_Name').innerHTML;
			
			

//alert(strName);
//alert("../_inc/XML_funcAddPanier.asp?strId=" + varId + "&strQte=" + intQte + "&strDetail=" + strDetail + "&strWebId=" + varWebId);		
		oXmlHttp.open("GET","../_inc/XML_funcAddPanier.asp?ms="+ new Date().getTime() +"&strId=" + varId + "&strQte=" + intQte + "&strDetail=" + strDetail + "&strName=" + strName + "&strWebId=" + varWebId + "&strDevis=" + varDevis, false);
		oXmlHttp.setRequestHeader('Content-Type','text/html; charset=iso-8859-1')
		oXmlHttp.setRequestHeader("Cache-Control","no-cache");
		oXmlHttp.setRequestHeader('Pragma','no-cache');
		oXmlHttp.send(null);
//	alert(oXmlHttp.responseText);
		if (oXmlHttp.responseText == '')
			if (varPanierLib != "") {
				alert('Article(s) add in '+ varPanierLib);
				}
			else {
				alert('Article(s) add in basket');
				}
		if ((varPanierMini.toLowerCase() == 'oui') || (varPanierMini.toLowerCase() == 'true')) {
			funcDisplayBasketMini(varWebId, 'mini');
			}
		if (document.getElementById('qte_' + varId))
			document.getElementById('qte_' + varId).value='1';
		}
	}
	
function funcDisplayBasketMini(varWebId) {
	var oXmlHttp = false;
//return;
	// IE
	try
		{
		oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
//		alert("Msxml2.XMLHTTP");
		}
	catch (e)
		{
		try
			{
			oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
//			alert("Microsoft.XMLHTTP");
			}
			catch (E)
			{
			oXmlHttp = false;
			}
		}
	
	// Mozilla
	if (!oXmlHttp && typeof XMLHttpRequest != 'undefined')
		{
		try
			{
			oXmlHttp = new XMLHttpRequest();
//			alert("XMLHttpRequest");
			}
		catch(e)
			{
			oXmlHttp = false;
			}
		}
//alert('ok')		
	if (oXmlHttp) {
		oXmlHttp.open("GET","../_inc/XML_funcDisplayPanier.asp?ms="+ new Date().getTime() +"&strMode=mini&strWebId="+ varWebId, false);
		oXmlHttp.setRequestHeader('Content-Type','text/html; charset=iso-8859-1')
		oXmlHttp.setRequestHeader("Cache-Control","no-cache");
		oXmlHttp.setRequestHeader('Pragma','no-cache');
		oXmlHttp.send(null);
//		alert(oXmlHttp.responseText);
		return eval(oXmlHttp.responseText);
		}
	else
		alert('Functionality nonavailable with your navigator.');
}



function funcPanierRecalc(varQte, varCmdeId, varCmdeDetailId, varPanierMini, varContactId, varWebId) {
	if ((varQte == -1) || (varQte == 0))
		if (!confirm('Do you really want to remove this article from your basket?')) 
			return;
		else
			varQte = -1;
	
	var oXmlHttp = null;
	try {
		oXmlHttp = new ActiveXObject("Msxml2.XmlHttp");
		}
	catch (e) {
		try {
			oXmlHttp = new ActiveXObject("Microsoft.XmlHttp");
			}
		catch (err) {
			oXmlHttp = null;
			}
		}
		
	// Mozilla
	if (!oXmlHttp && typeof XMLHttpRequest != 'undefined')
		{
		try
			{
			oXmlHttp = new XMLHttpRequest();
//			alert("XMLHttpRequest");
			}
		catch(e)
			{
			oXmlHttp = false;
			}
		}

	if (oXmlHttp) {
		oXmlHttp.open("GET","../_inc/XML_funcUpdatePanier.asp?ms="+ new Date().getTime() +"&strCmdeId=" + varCmdeId + "&strId=" + varCmdeDetailId + "&strQte="+ varQte, false);
		oXmlHttp.setRequestHeader('Content-Type','text/html; charset=iso-8859-1')
		oXmlHttp.setRequestHeader("Cache-Control","no-cache");
		oXmlHttp.setRequestHeader('Pragma','no-cache');
		oXmlHttp.send(null);
		eval(oXmlHttp.responseText);
		}

	eval(oXmlHttp.responseText);
	
	if (varPanierMini == 'oui') {
		funcDisplayBasketMini(varWebId);
		} 

	//funcCheckPanier();
	}
	
function funcCheckPanier() {
	var totalTTC = 0;
	var totalSelectTTC = 0;
	var tempElt = '';
	for (var k=0; k<document.all.length; k++)
		{
		if(document.all[k].id.indexOf('cb_')>-1)
			{
			if (document.all[k].checked == true)
				{
//alert(parseFloat(document.getElementById(document.all[k].id.replace('cb_', 'totArticleTTC_')).innerText.replace(' ', '').replace(',','.')));
				totalSelectTTC = totalSelectTTC + parseFloat(document.getElementById(document.all[k].id.replace('cb_', 'totArticleTTC_')).innerText.replace(' ', '').replace(',','.'));
//				alert(totalSelectTTC);
				}
			totalTTC = totalTTC + parseFloat(document.getElementById(document.all[k].id.replace('cb_', 'totArticleTTC_')).innerText.replace(' ', '').replace(',','.'));
//			alert(totalTTC);
			}
		}
	document.getElementById('totalTTC').innerText=formatNumber(totalTTC, 2, ' ');
	document.getElementById('totalSelectTTC').innerHTML='<b>' + formatNumber(totalSelectTTC, 2, ' ') + '</b>';

	if (totalSelectTTC > 0)
		{
		if((document.getElementById('a_Cmde').innerText.indexOf('Ajouter')==0) || (document.getElementById('a_Cmde').innerText.indexOf('Afficher')==0))
			document.getElementById('a_Cmde').innerText = 'Add selected items to your current cart'
		document.getElementById('a_Cmde').style.display = 'inline';
		document.getElementById('span_Cmde').style.display = 'none';
		}
	else
		if((document.getElementById('a_Cmde').innerText.indexOf('Ajouter')==0) || (document.getElementById('a_Cmde').innerText.indexOf('Afficher')==0))
			{
			document.getElementById('span_Cmde').style.display = 'none';
			document.getElementById('a_Cmde').style.display = 'inline';
			document.getElementById('a_Cmde').innerText = 'Show current cart';
			}
		else
			{
			document.getElementById('a_Cmde').style.display = 'none';
			document.getElementById('span_Cmde').style.display = 'inline';
			}
	}
	
function funcValidCmde(varAction)
	{
//		switch (document.getElementById('strAction').value) {
	switch (varAction) {
		case '1':
			document.getElementById('div_CmdeArticle').style.display='inline';
			document.getElementById('div_CmdeCoord').style.display='none';
			document.getElementById('strAction').value='2';
			break;	
		case '2':
			document.getElementById('div_CmdeArticle').style.display='none';
			document.getElementById('div_CmdeCoord').style.display='inline';
			if (funcCtrlSaisie(document.getElementById('formCmde')) == true) {
				document.getElementById('strAction').value='3';
				document.getElementById('formCmde').submit;
			}
			break;
		case '3':
			alert('ok2');
		}
	}

function formatNumber(valeur,decimal,separateur) {
// formate un chiffre avec 'decimal' chiffres après la virgule et un separateur
	var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; 
	var val=Math.floor(Math.abs(valeur));
	if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;}
	var val_format=val+"";
	var nb=val_format.length;
	for (var i=1;i<4;i++) {
		if (val>=Math.pow(10,(3*i))) {
			val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i));
		}
	}
	if (decimal>0) {
		var decim=""; 
		for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";}
		deci=decim+deci.toString();
		val_format=val_format+"."+deci;
	}
	if (parseFloat(valeur)<0) {val_format="-"+val_format;}
	return val_format.replace('.', ',');
}	



function funcDisplayFeature(varArticleId, varFeatureId, varDisplayDispo) {
		if (varArticleId == '')
		{
		return;
		}
		var selIndex = document.getElementById('sel_feature_' + varFeatureId).selectedIndex;
//alert(selIndex);
//alert(varArticleId + ' - ' + varFeatureId + ' - ' + varDisplayDispo );
	var oXmlHttp = null;
	// Mozilla
	if (window.XMLHttpRequest)
		{
		oXmlHttp = new XMLHttpRequest();
		}
	// IE
	else if (window.ActiveXObject)
		{
		oXmlHttp = new ActiveXObject("Microsoft.XmlHttp");
		}
	if (oXmlHttp != null) {
		oXmlHttp.open("GET","../_inc/XML_funcDisplayFeature.asp?ms="+ new Date().getTime() +"&strArticleId=" + varArticleId + "&strFeatureId=" + varFeatureId + "&strDisplayDispo=" + varDisplayDispo + "&strValeur=", false);
		oXmlHttp.setRequestHeader('Content-Type','text/html; charset=iso-8859-1')
		oXmlHttp.setRequestHeader("Cache-Control","no-cache");
		oXmlHttp.setRequestHeader('Pragma','no-cache');
		oXmlHttp.send(null);
//alert(oXmlHttp.responseText);
		eval(oXmlHttp.responseText);
		document.getElementById('sel_feature_' + varFeatureId).selectedIndex = selIndex;
//		funcChangeArticle(varArticleId, varDisplayDispo);
		}
	else
		alert('Unavailable feature with your browser.');

}


function funcChangeArticle() {
//alert('ok');	
	var tabFeature = new Array(7);
	var tabFeature2; // = new Array();
	var nbFeature = 0;
	var tempFeature = '';
	var articleId = '';
	for (var k=1; k<8; k++)
		if(document.getElementById('sel_feature_' + k))
			{
			tabFeature[k-1] = document.getElementById('sel_feature_' + k).value;
//alert(document.getElementById('sel_feature_' + k).selectedIndex + ' - ' + document.getElementById('sel_feature_' + k).value);			
			document.getElementById('selValue_' + k).value = document.getElementById('sel_feature_' + k).selectedIndex;
//			document.getElementById('selValue_' + k).value = document.getElementById('sel_feature_' + k).value;
			nbFeature ++;
			}
//alert(nbFeature);			
	if (nbFeature > 0)
		{
		for (var m=0; m<nbFeature; m++)
			{
			tabFeature2 = tabFeature[m].split('-');
			for (k=0; k<tabFeature2.length; k++)
				{
				articleId = tabFeature2[k];
				for (l=1; l<nbFeature; l++)
					{
//alert('-' + document.getElementById('sel_feature_' + (l+1)).value + '-' + ' ** ' + '-' + tabFeature2[k] + '-');
					tempFeature = '-' + document.getElementById('sel_feature_' + (l+1)).value + '-';
					if (tempFeature.indexOf('-' + tabFeature2[k] + '-') == -1)
						articleId = '';
					}
				if (articleId != '')
					{
//					alert('articleId : ' + articleId);
					document.getElementById('pdtDetail').value = articleId;
					document.getElementById('form_pdtDetail').action = document.location.href.replace(document.location.hash, "") +"#BlocCde";
					document.getElementById('form_pdtDetail').submit();
					return;
					}
				}
			}
		}
	else
		{
		}
	}


function funcChangeArticle_old(varArtId, varDisplayDispo) {
	
	
	
	
	
	var oXmlHttp = null;
	// Mozilla
	if (window.XMLHttpRequest)
		{
		oXmlHttp = new XMLHttpRequest();
		}
	// IE
	else if (window.ActiveXObject)
		{
		oXmlHttp = new ActiveXObject("Microsoft.XmlHttp");
		}
//alert(varArtId + ' - ' + varDisplayDispo);
	if (oXmlHttp) {
		oXmlHttp.open("GET","../_inc/XML_funcChangeArticle.asp?ms="+ new Date().getTime() +"&strId=" + varArtId + '&strDisplayDispo=' + varDisplayDispo, false);
		oXmlHttp.setRequestHeader('Content-Type','text/html; charset=iso-8859-1')
		oXmlHttp.setRequestHeader("Cache-Control","no-cache");
		oXmlHttp.setRequestHeader('Pragma','no-cache');
		oXmlHttp.send(null);
//alert(oXmlHttp.responseText);
		eval(oXmlHttp.responseText);
		funcDisplayFeature (varArtId, 1, varDisplayDispo);
		}
	else
		alert('Unavailable feature with your browser.');
	oXmlHttp = null;
}
