function CreateXHR(page){
	var xhr_object = null;
	//icoloading();
	   if(window.XMLHttpRequest) // Firefox
		  xhr_object = new XMLHttpRequest();
	   else if(window.ActiveXObject) // Internet Explorer
		  xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	   else { // XMLHttpRequest non supporté par le navigateur
		  //alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		  return;
	   }
	   xhr_object.open("POST", page, true);

	   xhr_object.onreadystatechange = function() {
		  if(xhr_object.readyState == 4){
			eval(xhr_object.responseText);
		  }
	   }
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	return xhr_object;
}

function ftcn(valeur){
	valeur=Number(valeur).toFixed(2);
	if(isNaN(valeur)) valeur='0.00';
	if (valeur==0) valeur='0.00';
	return valeur;
}

/* alias de document.getElementById*/
function OBJ(id){
	return document.getElementById(id);
}




function CalqFicheOn(URL){
	document.getElementById('CalqFicheOnContent').innerHTML='<iframe name="msgboxframe" border="false" frameborder="0" id="msgbox-frame" src="'+URL+'" width="100%" height="100%" style="position:relative; z-index:110;" allowTransparency="true"></iframe>';
	document.getElementById('CalqFicheOn').style.display="block";
	document.getElementById('CalqFicheOnContent').style.display="block";
}
function closePopupWindow(){
	window.parent.document.getElementById('CalqFicheOn').style.display='none';
	window.parent.document.getElementById('CalqFicheOnContent').style.display='none';
}



function avisNote(avisId, avisKeySID, noteValue){
	var xhr_object = null;
	xhr_object=CreateXHR("/AJAXscript/avisNote.php");
	xhr_object.send("avisId="+avisId+"&avisKeySID="+avisKeySID+"&noteValue="+noteValue);
}

function postAvisShowTheStars(){
	nbStar=OBJ('chp_note').options[OBJ('chp_note').selectedIndex].value;

	var c=1;
	while(c<=5){
		if(c<=nbStar) imgSrc="full";
		else imgSrc="empty";
		OBJ('avisStar'+c).src="/img/content/vote/star-"+imgSrc+".gif";
		c++;
	}
}
function countChar(obj){
	charLength=100-(obj.value.length);
	if(charLength>0) OBJ('repCountChar').innerHTML='Reste '+charLength+' caractères';
	else OBJ('repCountChar').innerHTML='';
}








function getWindowHeight(){
	var windowHeight=0;
	if(typeof(window.innerHeight)=='number'){
		windowHeight=window.innerHeight;
	}
	else{
		if(document.documentElement && document.documentElement.clientHeight){
			windowHeight = document.documentElement.clientHeight;
		}
		else{
			if(document.body&&document.body.clientHeight){
				windowHeight=document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function getWindowWidth(){
	var windowWidth=0;
	if(typeof(window.innerWidth)=='number'){
		windowWidth=window.innerWidth;
	}
	else{
		if(document.documentElement && document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		}
		else{
			if(document.body&&document.body.clientWidth){
				windowWidth=document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

var addToBasketItemId=new Array(); //array des id à ajouter au panier
var addToBasketItemQte=new Array(); //Array des qte à ajouter au panier
var addToBasketItemArticleUnique=new Array(); //Array si stock unique ou pas
var addToBasketActionToShowType="SHOW-POPUP";

function popupAddBasket(){
	if (document.cookie.length > 0){
		urlVar='';
		for (var i=0;i<addToBasketItemId.length;i++){
			if(!addToBasketItemQte[i]) addToBasketItemQte[i]=1; //si undefined, on initialise à 1
			
			textUniqueVar="";
			if(addToBasketItemArticleUnique[i]=='1') textUniqueVar="&orderArticleUnique[]=1";
			
			if(i>0) urlVar=urlVar+"&";
			urlVar=urlVar+"orderArticleId[]="+addToBasketItemId[i]+"&orderArticleQte[]="+addToBasketItemQte[i]+textUniqueVar;
		}

		var xhr_object = null;
		xhr_object=CreateXHR("/page/order/0_basketAddAjax.php");
		xhr_object.send(urlVar+"&showType="+addToBasketActionToShowType);
		
		return false;
	}
}

function closeBasket(){
	addToBasketActionToShowType="SHOW-POPUP";
	OBJ('CalqFicheOn').style.display='none';
	OBJ('CalqFicheOnContent').style.display='none';
}

function tailleBasket(){
	MyHeight=getWindowHeight();
	calqHeight=OBJ('CalqFicheOnContent').clientHeight;
	topVal=parseInt(Math.round((MyHeight-calqHeight)/2));
	if(topVal<0) topVal=0;
	OBJ('CalqFicheOnContent').style.top=topVal+"px";

	MyWidth=getWindowWidth();
	calqWidth=OBJ('CalqFicheOnContent').clientWidth;
	leftVal=parseInt(Math.round((MyWidth-calqWidth)/2));
	if(leftVal<0) leftVal=0;
	OBJ('CalqFicheOnContent').style.left=leftVal+"px";
}





function MarqueNavhideButton(){
	OBJ('btShowMarqueOrNotOpen').style.display='none';
	OBJ('btShowMarqueOrNotClose').style.display='none';
}
function MarqueNavShowMarqueAlphabet(){
	MarqueNavhideButton();
	new Effect.SlideUp('blocShowMarqueOrNot', { duration: 0.3 }); 
	setTimeout('MarqueNavafficheMarqueAlphabet()',350);
}
function MarqueNavafficheMarqueAlphabet(){
	OBJ('topMarque').style.display='none';
	OBJ('showAllMarque').style.display='block';
	new Effect.SlideDown('blocShowMarqueOrNot', { duration: 0.6 });
	OBJ('btShowMarqueOrNotClose').style.display='inline';
}

function MarqueNavShowMarqueTop(){
	MarqueNavhideButton();
	new Effect.ScrollTo('blocShowMarqueOrNot', { offset:-200, duration:0.6 });
	setTimeout('MarqueNavafficheMarqueTop()',650);
}
function MarqueNavafficheMarqueTop(){
	OBJ('topMarque').style.display='block';
	OBJ('showAllMarque').style.display='none';
	OBJ('btShowMarqueOrNotOpen').style.display='inline';
}

function ShowImageOnTheFly(elementID, newImgSRC){
	document.getElementById(elementID).innerHTML='<img src="'+newImgSRC+'"/>';
}




/*********   Pour les packs     ***************/

function checkComposant(obj,compCheckLine){
	if(obj.options[obj.selectedIndex].value){
		addToBasketItemId[compCheckLine-1]=obj.options[obj.selectedIndex].value;
		OBJ('compCheck'+compCheckLine).innerHTML='<img src="/img/tpl/Z-Divers/pict/ok.png" alt="ok"/>';
	}
	else OBJ('compCheck'+compCheckLine).innerHTML='&nbsp;'
}

function afficheComposant(obj,compCheckLine){
	if(obj.options[obj.selectedIndex].value){

		var choosePackItem='';
		var c=0;
		for(var i=0; i<document.packConfig.elements.length; i++){
			if(document.packConfig.elements[i].name=="orderArticleId[]"){
				if(document.packConfig.elements[i].options[document.packConfig.elements[i].selectedIndex].value){
					choosePackItem+='&packItem[]='+document.packConfig.elements[i].options[document.packConfig.elements[i].selectedIndex].value;
				}				
			}
		}

		var xhr_object = null;
		xhr_object=CreateXHR("/AJAXscript/packAfficheComp.php");
		xhr_object.send("CompId="+escape(obj.options[obj.selectedIndex].value)+"&lineId="+escape(compCheckLine)+choosePackItem);
	}
	else OBJ('compAffiche'+compCheckLine).style.display="none";
}

function testSiToutEstRempli(theForm){
	for(var i=0; i<theForm.elements.length; i++){
		if(theForm.elements[i].name=="orderArticleId[]"){
			if(!theForm.elements[i].options[theForm.elements[i].selectedIndex].value){
				OBJ('ShowTheErrorOfPack').style.display='block';
				return false;
			}
		}
	}
	OBJ('ShowTheErrorOfPack').style.display='none';
	return true;
}
