function GetHRObject(){
	var xhr;
	if(window.XMLHttpRequest || window.ActiveXObject) {
        if(window.XMLHttpRequest) {
                xhr = new XMLHttpRequest(); 
        }
        else { // Internet Explorer <7
                try {
                        xhr = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch(e) {
                        xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
        }
	}
	return xhr;
}


function UpdateBasket()
{
	var HttpReq = GetHRObject();

	if (!HttpReq)
	{
		// x_x
		return;
	}

	HttpReq.onreadystatechange = function()
	{
		if (HttpReq.readyState == 4 && HttpReq.status == 200)
		{
			Data = HttpReq.responseText.split(';');
			ProdCount = document.getElementById('BasketProdCount');
			ProdCount.innerHTML = Data[0] + ' produit(s)';
			Amount = document.getElementById('BasketAmount');
			Amount.innerHTML = '<span style="color:#B91900; font-weight:bold;">' + Data[1] + ' &euro;</span>';
			BskIco = document.getElementById('GHeadBasketIcon');
			BskIco.src = './css/pictures/HeaderBasket' + (Data[0] > 0 ? 'On.gif' : 'Off.gif');
		}
	}

	HttpReq.open('GET', './request/basket_update.php');
	HttpReq.send(null);

}


function ChangeAmount(ItemID, Qte, Price, Name)
{
	var HttpReq = GetHRObject();

	if (!HttpReq)
	{
		document.location.href='./basket.php?action=add&product=' + ItemID + '&qte=' + Qte;
		return;
	}

	HttpReq.onreadystatechange = function()
	{
		if (HttpReq.readyState == 4 && HttpReq.status == 200)
		{
			if (HttpReq.responseText == 'OK')
			{

				var BasketMsgHold = document.createElement("div");
				BasketMsgHold.style.backgroundColor = '#999999';
				BasketMsgHold.style.width = '340px';
				BasketMsgHold.style.height = '200px';
				BasketMsgHold.id = 'ProdBasketAlert';
				BasketMsgHold.style.position = 'absolute';

				var BasketMsg = document.createElement("div");
				BasketMsg.style.width = '340px';
				BasketMsg.style.height = '200px';
				BasketMsg.style.position = 'relative';
				BasketMsg.style.background = 'URL(./css/pictures/BskPopBg.jpg) center no-repeat #FFFFFF';
				BasketMsg.style.border = '2px solid #333333';
				BasketMsg.style.left = '-5px';
				BasketMsg.style.top = '-5px';

				var HTML = '<div style="background:url(./css/pictures/FooterBarBg.gif) center; padding:3px; color:#FFFFFF; font-weight:bold;">Panier</div>'
+ '<div>'
+ '<table width="100%" border="0" cellspacing="0" cellpadding="2">'
+ '<tr>'
+ '<td height="70" colspan="3" valign="middle" style="color:#333333; padding-left:20px;">';
if (Name)
{
	HTML += '<div style="font-size:14px; font-weight:bold;">' + Name + '</div>'
	+ '<div>Cet article a bien &eacute;t&eacute; ajout&eacute; &agrave; votre panier.</div>'
	+ '<div>Quantité ajoutée : <span style="color:#B71800">' + Qte + '</span></div>';
}
HTML += '</td>'
+ '</tr>'
+ '<tr>'
+ '<td width="30%" height="48" align="center" valign="top"><a href="#" onclick="ProdAlertValidate(true); return false;"><img src="./css/pictures/BskIcoStay.gif" width="44" height="37" border="0" alt="Rester sur la page" title="Rester sur la page" /></a></td>'
+ '<td width="40%" height="48" valign="top">&nbsp;</td>'
+ '<td width="30%" height="48" align="center" valign="top"><a href="#" onclick="ProdAlertValidate(false); return false;"><img src="./css/pictures/BskIcoBasket.gif" width="44" height="37" border="0" alt="Visualiser votre panier" title="Visualiser votre panier" /></a></td>'
+ '</tr>'
+ '<tr>'
+ '<td align="center" style="padding-left:20px;">Rester sur la page</td>'
+ '<td>&nbsp;</td>'
+ '<td align="center" style="padding-right:20px;">Visualiser votre panier</td>'
+ '</tr>'
+ '</table>'
+ '</div>'
;
				BasketMsg.innerHTML = HTML;

				BasketMsgHold.appendChild(BasketMsg);
				document.body.appendChild(BasketMsgHold);

				if (navigator.appName=="Microsoft Internet Explorer")
				{
					ScrollY = document.documentElement.scrollTop + document.body.scrollTop;
					ScrollX = document.documentElement.scrollLeft + document.body.scrollLeft;
				}
				else
				{
					ScrollY = window.pageYOffset;
					ScrollX = window.pageXOffset;
				}
				var View = {
					width 	: (
						// W3C Compliant calculation
						window.innerWidth ? 
							window.innerWidth : 
							// IE 6 + 
							( document.documentElement && document.documentElement.clientWidth ) ?
								document.documentElement.clientWidth  : 
								// IE 4
								document.body.clientWidth
					),
					height  : (
						// W3C Compliant calculation
						window.innerHeight ? 
							window.innerHeight  : 
							// IE 6 + 
							( document.documentElement && document.documentElement.clientHeight ) ?
								document.documentElement.clientHeight  : 
								// IE 4
								document.body.clientHeight
					)
				}

				// Fix Bandeau Flash
				hasFlash = false;
				flashs = document.getElementsByTagName('object');
				for(var i = 0; i< flashs.length; i++ ) {
					if( flashs[i].parentNode.className.match( /^asset (.*)wide$/ ) ) {
						hasFlash = true;
						break;
					}
				}
				BasketMsgHold.style.left = (View.width / 2 	- BasketMsgHold.offsetWidth / 2 + ScrollX) + 'px';
				BasketMsgHold.style.top =  ( View.height / 2 	- BasketMsgHold.offsetHeight / 2 + ScrollY ) + 'px';

				if( hasFlash && document.documentElement.scrollTop < 180 ) {
					BasketMsgHold.style.marginTop = ( 187 - document.documentElement.scrollTop ) +'px';
				}

				UpdateBasket();

			}
		}
	}

	HttpReq.open('GET', './request/basket.php?product=' + ItemID + '&qte=' + Qte + '&price=' + Price + '&name=' + Name, true);
	HttpReq.send(null);

}





function ProdAlertValidate(Stay)
{
	ProdAlert = document.getElementById('ProdBasketAlert');
	ProdAlert.parentNode.removeChild(ProdAlert);

	if (!Stay)
		document.location.href = 'panier.php';
}

function ProdListUpdate(Cat, Sort, SortOrder, Page, ShowVisuals)
{
	PlcElem = document.getElementById('Plc');
	PlcElem.innerHTML = '<img src="./css/pictures/Reload.gif" width="16" height="16" align="top" alt="" /> En cours de chargement ...';

	var HttpReq = GetHRObject();

	if (HttpReq)
		Url = './request/prodlist.php?id=' + Cat;
	else
		Url = document.location.pathname + '?action=product_cat&id=' + Cat ;
		
		
	Filters = document.getElementsByName('Filter');
	for (i=0; i<Filters.length; i++)
	{
		F = Filters[i];

		if (F.type == 'checkbox')
		{
			if (F.checked)
				Url += '&filter[' + F.id.substr(6) + ']=' + F.value;
		}
		else
		{
			Url += '&filter[' + F.id.substr(6) + ']=' + F.value;
		}
	}

	Brands = document.getElementById('FilterBrand');
	if (Brands)
		if(Brands.value!='')
			Url += '&manufacturer=' + Brands.value;

	Price = document.getElementById('FilterPriceValue');
	Url += '&price=' + Price.value ;
	Price_min = document.getElementById('price_min');
	Url += '&price_min=' + Price_min.value ;
	Price_max = document.getElementById('price_max');
	Url += '&price_max=' + Price_max.value ;

	// SF
	SfObj = document.getElementById('FilterSF');
	if (SfObj.value)
		Url += '&sf=' + SfObj.value;

	// Tri
	if(Sort || SortOrder)
		Url += '&sort=' + Sort + '&order=' + SortOrder;

	// Page
	if(Page)
		Url += '&page=' + Page;

	if (!HttpReq)
	{
		document.location.href = Url;
		return;
	}

	HttpReq.onreadystatechange = function()
	{
		if (HttpReq.readyState == 4 && HttpReq.status == 200)
		{
			if (HttpReq.responseText)
			{
				ProdSpace = document.getElementById('ProdListArea');
				ProdSpace.innerHTML = HttpReq.responseText;
			}
		}
	}
	
	HttpReq.open('GET', Url, true);
	HttpReq.send(null);
}

function ProdListPpp(Cat, Ppp)
{
	PlcElem = document.getElementById('Plc');
	PlcElem.innerHTML = '<img src="./css/pictures/Reload.gif" width="16" height="16" align="top" alt="" /> En cours de chargement ...';

	var HttpReq = GetHRObject();

	if (HttpReq)
		Url = './request/prodlist.php?id=' + Cat + '&limit_page=' + Ppp;
	else
		Url = document.location.pathname + '?action=product_cat&id=' + Cat + '&limit_page=' + Ppp;

	if (!HttpReq)
	{
		document.location.href = Url;
		return;
	}

	HttpReq.onreadystatechange = function()
	{
		if (HttpReq.readyState == 4 && HttpReq.status == 200)
		{
			if (HttpReq.responseText)
			{
				ProdSpace = document.getElementById('ProdListArea');
				ProdSpace.innerHTML = HttpReq.responseText;
			}
		}
	}

	HttpReq.open('GET', Url, true);
	HttpReq.send(null);
}

function ProdListView(Cat, ViewType)
{
	PlcElem = document.getElementById('Plc');
	PlcElem.innerHTML = '<img src="./css/pictures/Reload.gif" width="16" height="16" align="top" alt="" /> En cours de chargement ...';

	var HttpReq = GetHRObject();

	if (HttpReq)
		Url = './request/prodlist.php?id=' + Cat + '&view=' + ViewType;
	else
		Url = document.location.pathname + '?action=product_cat&id=' + Cat + '&view=' + ViewType;

	if (!HttpReq)
	{
		document.location.href = Url;
		return;
	}

	HttpReq.onreadystatechange = function()
	{
		if (HttpReq.readyState == 4 && HttpReq.status == 200)
		{
			if (HttpReq.responseText)
			{
				ProdSpace = document.getElementById('ProdListArea');
				ProdSpace.innerHTML = HttpReq.responseText;
			}
		}
	}

	HttpReq.open('GET', Url, true);
	HttpReq.send(null);
}

function FilterBrand(Ev, Cat, BrandID)
{
	var Multi = Ev.ctrlKey;

	var SrcElement = (window.event ? Ev.srcElement : Ev.target);
	BrandFilter = document.getElementById('FilterBrand');
	BrandList = BrandFilter.value.split(',');

	// Vérifie si la marque est déjà dans la liste
	BrandState = false;
	BrandIndex = 0;
	if (BrandList.length)
	{
		for (i=0; i<BrandList.length; i++)
		{
			if (BrandList[i] == BrandID)
			{
				BrandState = true;
				BrandIndex = i;
				break;
			}
		}
	}

	if (Multi)
	{
		// Ajoute ou retire de la liste
		SelBrand = document.getElementById('Brand' + BrandID);
		if (BrandState)
		{
			BrandList.splice(BrandIndex, 1);
			SelBrand.className = 'ProdFilterBrOff';
			if(SelBrand.parentNode.className.match( /Brand/i ))
			{
				SelBrandLabel = document.getElementById('BrandLabel' + BrandList[i]);
				SelBrandLabel.className = 'ProdFilterBrLabelOff';
			}
		}
		else
		{
			BrandList.push(BrandID);
			SelBrand.className = 'ProdFilterBrOn';
			if(SrcElement.parentNode.className.match( /Brand/i ))
			{
				SelBrandLabel = document.getElementById('BrandLabel' + BrandList[i]);
				SelBrandLabel.className = 'ProdFilterBrLabelOn';
			}
		}
	}
	else
	{
		// Déselectionne les valeurs précédentes
		if (BrandList.length)
		{
			for (i=0; i<BrandList.length; i++)
			{
				if (!BrandList[i])
					continue;
				SelBrand = document.getElementById('Brand' + BrandList[i]);
				SelBrand.className = 'ProdFilterBrOff';
				if(SelBrand.parentNode.className.match( /Brand/i ))
				{
					SelBrandLabel = document.getElementById('BrandLabel' + BrandList[i]);
					SelBrandLabel.className = 'ProdFilterBrLabelOff';
				}
			}
		}

		SelBrand = document.getElementById('Brand' + BrandID);
		if (BrandState)
		{
			SelBrand.className = 'ProdFilterBrOff';
			BrandList = new Array();
			if(SrcElement.parentNode.className.match( /Brand/i ))
			{
				SelBrandLabel = document.getElementById('BrandLabel' + BrandID);
				SelBrandLabel.className = 'ProdFilterBrLabelOff';
			}
		}
		else
		{
			SelBrand.className = 'ProdFilterBrOn';
			BrandList = new Array(BrandID);
			if(SrcElement.parentNode.className.match( /Brand/i ))
			{
				SelBrandLabel = document.getElementById('BrandLabel' + BrandID);
				SelBrandLabel.className = 'ProdFilterBrLabelOn';
			}
		}
	}

	
	BrandFilter.value = BrandList.toString();
		
	ProdListUpdate(Cat);
}

function SfSwitch(Obj, CodeSf, Field, Val, Cat)
{
	SfHidden = document.getElementById('FilterSF');

	if (Obj.className == 'SfHL')
	{
		Obj.className = 'Sf';
		SfHidden.value = null;
	}
	else
	{

		SfPh = document.getElementById('SfPH');
		SfList = SfPh.childNodes;
		for (i=0; i<SfList.length; i++)
		{
			if (SfList[i].nodeType == 1)
				SfList[i].className = 'Sf';
		}
		Obj.className = 'SfHL';
		SfHidden.value = CodeSf + ';' + Field + ';' + Val;
	}

	ProdListUpdate(Cat);
}

function RdvUpdate(Mois)
{
	PlcElem = document.getElementById('rdv_head');
	PlcElem.innerHTML = '<img src="./css/pictures/Reload.gif" width="16" height="16" align="top" alt="" /> En cours de chargement ...';

	var HttpReq = GetHRObject();

	if (HttpReq)
		Url = './request/rdv.php?mois=' + Mois;
	else
		Url = document.location.pathname + '?mois=' + Mois ;
		
	if (!HttpReq)
	{
		document.location.href = Url;
		return;
	}

	HttpReq.onreadystatechange = function()
	{
		if (HttpReq.readyState == 4 && HttpReq.status == 200)
		{
			if (HttpReq.responseText)
			{
				ProdSpace = document.getElementById('rdv');
				ProdSpace.innerHTML = HttpReq.responseText;
			}
		}
	}
	
	HttpReq.open('GET', Url, true);
	HttpReq.send(null);
}
function date(date)
{	
	PlcDate = document.getElementById('date');
	PlcDate.value = '' + date + '';
}
function timestamp(jourNumero)
{
	PlcTime = document.getElementById('timestamp');
	PlcTime.value = '' + jourNumero + '';
}

function ShowCoup(){
		TOff = document.getElementById('BskCoupOff');
		TOff.style.display = 'none';
		TOn = document.getElementById('BskCoupOn');
		TOn.style.display = 'block';
	}

	function ChangeShipper(Shipper){
		if(document.getElementById('reduc_value') != null){
			ReducCode = document.getElementById('reduction_code').value;
			ReducValue = document.getElementById('reduction_value').value;
			document.location.href = 'panier.php?shipping=' + Shipper + '&reduc_code=' + ReducCode + '&reduc_value=' + ReducValue;
		}
		else{
			document.location.href = 'panier.php?shipping=' + Shipper;
		}
	}

	var HoverCat = null;

	function SwitchPanel(Cat){
		if (HoverCat)	{
			OldCat = document.getElementById('NewProdPanel' + HoverCat);
			OldCat.style.display = 'none';
			OldCat = document.getElementById('NewProdTitle' + HoverCat);
			OldCat.className = 'NewProdHeaderCold';
		}

		NewCat = document.getElementById('NewProdPanel' + Cat);
		NewCat.style.display = '';
		NewCat = document.getElementById('NewProdTitle' + Cat);
		NewCat.className = 'NewProdHeaderHot';
		HoverCat = Cat;
	}

	function HidePanel(){
		for (var i=0; i < arguments.length ; i++)    {
			if(typeof document.getElementById(arguments[i]) != 'undefined')
				document.getElementById(arguments[i]).style.display = 'none';
		}
	}

	function ShowHideElemWOpt(Name, Show, Options) {
		for(var opt in Options)
		{
			var el = document.getElementById(opt)
			if(el)
			{
				switch(el.tagName)
				{
					case 'INPUT' : 
					case 'SELECT' : 
					{
						el.value = Options[opt];
						break;
					}
					default : 
						el.innerHTML = Options[opt];
				}
			}
				
		}
		ShowHideElem(Name, Show);
	}

	function CheckEmail( event ){
		var email = document.getElementById('email').value;
		if( ! new RegExp(/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/i).test( email ) )	{
			document.getElementById('email-error').style.visibility = 'visible';
			return false;
		}
		return true;
	}
	
	function CheckValid( level ){
		if( level != 'OK' )	{
			document.getElementById('valid-error').style.visibility = 'visible';
			return false;
		}
		return true;
	}

