
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/*---------------- Fonction galerie ------------------------*/

	var current_pic = 1;
	var max_pic = 0;
	var next_pic = 0;
	var tabImg_zoom = new Array();
	var tabZoom = new Array();
	
	function swap_pic(sens) {
	
		if(sens == 0) next_pic = current_pic - 1;
		else next_pic = current_pic + 1;
	
		if(next_pic > 0 & next_pic <= max_pic) {
			document.getElementById('galerie_pic_' + next_pic).style.display = 'block';
			document.getElementById('galerie_pic_' + current_pic).style.display = 'none';
			current_pic = next_pic;
			check_size(next_pic);
		}

		if(current_pic == 1) document.getElementById('swap0').style.display = "none";
		else document.getElementById('swap0').style.display = "block";
		
		if(current_pic == max_pic) document.getElementById('swap1').style.display = "none";
		else document.getElementById('swap1').style.display = "block";
	}
	

	function check_size(numPic) {

		name = "pic_" + numPic;
	
		/*if(document.getElementById(name).height > 405) {
			document.getElementById(name).height = 405;
			tabImg_zoom[numPic] = 1;
			tabZoom[numPic] = document.getElementById(name).src;
		}*/
		
		if(document.getElementById(name).width > 269) {
			document.getElementById(name).width = 269;
			tabImg_zoom[numPic] = 1;
			tabZoom[numPic] = document.getElementById(name).src;
		} else {
			if(document.getElementById(name).height > 405) {
				document.getElementById(name).height = 405;
				tabImg_zoom[numPic] = 1;
				tabZoom[numPic] = document.getElementById(name).src;
			}
		}
		
		if(tabImg_zoom[numPic] == 1) document.getElementById('zoom_btn').style.visibility = 'visible';
		else document.getElementById('zoom_btn').style.visibility = 'hidden';
	}

	
	function swap_tool(numTool) {
		for(i=1;i<=4;i++) {
			if(i==numTool) {
				document.getElementById('fiche_toolbar_layer' + numTool).style.display = 'block';
				document.getElementById('arrow_tool' + numTool).style.visibility = 'visible';
				document.getElementById('select_tool' + numTool).style.color = 'red';
			} else {
				if(document.getElementById('fiche_toolbar_layer' + i)) {
					document.getElementById('fiche_toolbar_layer' + i).style.display = 'none';
					document.getElementById('arrow_tool' + i).style.visibility = 'hidden';
					document.getElementById('select_tool' + i).style.color = 'black';
				}
			}
		}
	}
		//-------------------------------------------------------------------------------
		// Custom ComboBox
		





/******* CAttribut *******/
var CAttribut_validate = new Array;

function CAttItem (code_ean, option_id, majoration_prix, qte_stock){
	this.Datas = new Array;
	this.EAN = code_ean;
	this.OptID = option_id;
	this.Majoration = majoration_prix;
	this.Stock = qte_stock;
}
CAttItem.prototype.AddProperty = function (attribut_id, attribut_detail_id){
	newID = this.Datas.length;
	this.Datas[newID] = new Array;
	this.Datas[newID]["AttDetailID"] = attribut_detail_id;
	this.Datas[newID]["AttID"] = attribut_id;
}


function CAttribut (ObjectName, pagesURL){
	this.ObjectName = ObjectName;
	
	this.DelaiLivTag = "DelaiLivTag";
	this.StockTag = "StockTag";
	this.MajorationTag = "MajorationTag";
	this.AttSelectName = "sel_attrib";
	this.DefaultValue = "----------";
	this.HiddenOptID = "option_id";
	this.HiddenEAN = "code_ean";
	this.LinkedCheckBox = "";

	this.KitController = null;
	
	this.PrixBase = 0;
	this.Devise = "EUR";
	this.Majoration = 0;
	this.pagesURL = pagesURL;
	this.SelectOK = false;
	
	this.s_DelaiLiv = 0;
	this.DelaiLiv = new Array;
	this.DelaiLiv[0] = new Array;
	this.DelaiLiv[1] = new Array;
	this.DelaiLiv[2] = new Array;
	this.DelaiLiv[3] = new Array;
	this.DelaiLiv[4] = new Array;
	this.DelaiLiv[5] = new Array;
	this.DelaiLiv[6] = new Array;
	this.DelaiLiv[0][0] = 1;
	this.DelaiLiv[1][0] = 2;
	this.DelaiLiv[2][0] = 3;
	this.DelaiLiv[3][0] = 4;
	this.DelaiLiv[4][0] = 5;
	this.DelaiLiv[5][0] = 6;
	this.DelaiLiv[6][0] = 7;
	
	this.EANList = new Array;
	this.AttArbo = new Array;
	this.AttArboDef = new Array;
	
	/* fabrication arbo */
	this.PrevEAN = "";
	this.cItem = 0;
	this.levelCount = 0;
	
	this.SelectedList = new Array;
	
	CAttribut_validate[CAttribut_validate.length] = ObjectName;
}

/*** 
	enregistrement d'un input checkbox ou radio lié au produit
	si cet input est déselectionné/décoché, 
	alors this.IsOk() renvoie TRUE dans tous les cas
***/
CAttribut.prototype.SetLinkedCheckBox = function (InputName){
	this.LinkedCheckBox = InputName
}

CAttribut.prototype.SetEANHiddenInput = function (InputName){
	this.HiddenEAN = InputName
}
CAttribut.prototype.SetOptionIDHiddenInput = function (InputName){
	this.HiddenOptID = InputName
}
CAttribut.prototype.SetAttSelectName = function (InputName){
	this.AttSelectName = InputName
}
CAttribut.prototype.SetMajorationTag = function (InputName){
	this.MajorationTag = InputName
}
CAttribut.prototype.SetDefaultOption = function (Libelle){
	this.DefaultValue = Libelle
}

CAttribut.prototype.SetDelaiLivText = function (DelaiID, DelaiText){
	for(i=0; i<this.DelaiLiv.length; i++){
		if(this.DelaiLiv[i][0] == DelaiID)
			this.DelaiLiv[i][1] = DelaiText;
	}
}

CAttribut.prototype.IsOk = function (){
	if(this.LinkedCheckBox != ""){
		chkbox = document.getElementById(this.LinkedCheckBox);
		if(chkbox){
			if(!chkbox.checked)	return true;
			else return this.SelectOK;
		}else{
			return this.SelectOK;
		}
	}else{
		return this.SelectOK;
	}
}

CAttribut.prototype.Add = function (libelle, attribut_id, libelle_opt, attribut_detail_id, majoration_prix, qte_stock, code_ean, tri, option_id){
	
	if(this.PrevEAN != code_ean){
		//recherche si EAN existant
		var found = false;
		for(var i=0; i<this.EANList.length; i++){
			if(this.EANList[i].EAN == code_ean){
				found = true;
				this.cItem = i;
			}
		}
		if(!found){
			this.cItem = this.EANList.length;
			this.EANList[this.cItem] = new CAttItem(code_ean, option_id, majoration_prix, qte_stock);
		}
		this.PrevEAN = code_ean;
	}
	this.EANList[this.cItem].AddProperty(attribut_id, attribut_detail_id);
	
	ItemDatasCount = this.EANList[this.cItem].Datas.length-1;
	this.NewArboEntry(this.AttArbo, 0, ItemDatasCount, libelle_opt, attribut_detail_id, code_ean, libelle, attribut_id);
}

CAttribut.prototype.NewArboEntry = function (Arbo, cLevel, ToLevel, eText, eID, EAN, eDef, eDefID){
	if(cLevel < ToLevel){
		this.NewArboEntry(Arbo[this.EANList[this.cItem].Datas[cLevel]["AttDetailID"]][0], cLevel+1, ToLevel, eText, eID, EAN, eDef, eDefID);
		return;
	}else{
		if(cLevel > this.levelCount) this.levelCount = cLevel;
	
		found = false;
		for(var i in Arbo){
			if(i == eID){
				found = true;
				break;
			}
		}
		
		if(found){
			Arbo[eID][2][Arbo[eID][2].length] = EAN;
		}else{
			Arbo[eID] = new Array;
			Arbo[eID][0] = new Array;
			Arbo[eID][1] = eText;
			Arbo[eID][2] = new Array;
			Arbo[eID][2][0] = EAN;
			
			if(cLevel+1 > this.AttArboDef.length){
				this.AttArboDef[cLevel] = new Array;
				this.AttArboDef[cLevel][0] = eDefID;
				this.AttArboDef[cLevel][1] = eDef;
			}
		}
	}
}

CAttribut.prototype.RefreshCombo = function (Level, Value){
	//if(Value == 0) return true;
	

	var ArboNode = new Array;
	ArboNode[0] = this.AttArbo;

	if(Level == this.levelCount){

		for(i=0; i<this.SelectedList.length; i++){
			ArboNode = ArboNode[0][this.SelectedList[i]];
		}
		if(Value != 0){
			var SelectedEAN = ArboNode[0][Value][2][0];
			for(i=0; i<this.EANList.length; i++){
				if(this.EANList[i].EAN == SelectedEAN){
					//var divMajor = document.getElementById(this.MajorationTag);
					//divMajor.innerHTML = this.EANList[i].Majoration + " " + this.Devise;
					this.Majoration = this.EANList[i].Majoration;
					eval("document.form_panier."+this.HiddenOptID+".value = '" + this.EANList[i].OptID + "'");
					eval("document.form_panier."+this.HiddenEAN+".value = '" + this.EANList[i].EAN + "'");
					this.SelectOK = true;
					
					if(this.KitController){
						this.KitController.CalcValue();
					}
					
					break;
				}
			}
		}
		return true;
	}else{
		eval("document.form_panier."+this.HiddenEAN+".value = ''");
		eval("document.form_panier."+this.HiddenOptID+".value = 0");
		//var divMajor = document.getElementById(this.MajorationTag);
		//divMajor.innerHTML = "NC";
		this.Majoration = 0;
		if(this.KitController){
			this.KitController.CalcValue();
		}
	}
	this.SelectOK = false;
	
	if(this.SelectedList.length > Level)
		while(this.SelectedList.length > Level){
			document.getElementById(this.AttSelectName + "l" + (this.SelectedList.length)).options.length = 1;
			this.SelectedList.pop();
		}
	this.SelectedList.push(Value);
	
	
	var ISelect = document.getElementById(this.AttSelectName + "l" + (Level+1));
	ISelect.options.length = 1;
	if(Value == 0) return true;

	for(i=0; i<this.SelectedList.length; i++){
		ArboNode = ArboNode[0][this.SelectedList[i]];
	}
	
	//ISelect.options[ISelect.options.length] = new Option(this.DefaultValue, 0);
	for(var i in ArboNode[0]){
		ISelect.options[ISelect.options.length] = new Option(ArboNode[0][i][1], i);
	}
	
	//auto select des prochains combo, si plus qu'un choix dispo
	if(ArboNode[2].length == 1){
		var NextAttID = 0;
		for(NextAttID in ArboNode[0]){}
		ISelect.selectedIndex = 1;
		this.RefreshCombo(Level+1, NextAttID);
	}

	return true;
}

CAttribut.prototype.DisplayCombo = function (AttributID) {
	var Opts = ""
	
	for(i=0; i<this.AttArboDef.length; i++){
		if(AttributID == this.AttArboDef[i][0]){

			document.write("<select id='"+this.AttSelectName+"l"+i+"' name='"+this.AttSelectName+this.AttArboDef[i][0]+"' onchange='"+this.ObjectName+".RefreshCombo("+i+", this.value)'>");
			document.write("<option value='0'>"+this.DefaultValue+"</option>");
			if(i == 0)
				for(var z in this.AttArbo){
					document.write("<option value='"+z+"'>"+this.AttArbo[z][1]+"</option>");
				}
			document.write("</select>");
		}
	}
}

/* debug function */
function DumpArbo(Arbo, level){
	var s_level = "---";
	for(i=0; i<level; i++) s_level += "---";
	for(var i in Arbo){
		document.write(s_level + " " + Arbo[i][1] + " ("+ i +")<br>");
		//for(var j in Arbo[i][2])
			//document.write(s_level + " > " + Arbo[i][2][j] + "<br>");
		if(Arbo[i][0].length) DumpArbo(Arbo[i][0], level+1);
	}
}
//DumpArbo(C.AttArbo, 0)

function KitController (ObjectName, pagesURL){
	this.ObjectName = ObjectName;
	this.elems = new Array;
	this.BaseValue = 0;
	this.Value = 0;	
	this.PriceTag = "";
	this.Devise = "EUR";
}
KitController.TypeMandatory = 1;
KitController.TypeAlternative = 2;
KitController.TypeOption = 3;

KitController.prototype.AddMandatory = function (value, AttObject){
	var nid = this.elems.length;
	this.elems[nid] = new Array;
	this.elems[nid][0] = KitController.TypeMandatory;
	this.elems[nid][1] = value;
	this.elems[nid][2] = AttObject;
	
	this.BaseValue += value;
}

KitController.prototype.AddElem = function (value, AttObject, CheckInput, Type, Groupe){
	var nid = this.elems.length;
	this.elems[nid] = new Array;
	this.elems[nid][0] = Type;
	this.elems[nid][1] = value;
	this.elems[nid][2] = AttObject;
	this.elems[nid][3] = CheckInput;	
	this.elems[nid][4] = Groupe;
	this.elems[nid][5] = document.getElementById(CheckInput).checked;
	
	/*** add handler on the product's checkbox ***/
	/*var FnName = (Type == KitController.TypeAlternative)? "AlternativeCheck" : "OptionCheck" ;
	eval("document.getElementById('"+CheckInput+"').onclick = " + this.ObjectName + "." + FnName);*/
}

KitController.prototype.AddAlternative = function (value, AttObject, CheckInput, Groupe){
	this.AddElem(value, AttObject, CheckInput, KitController.TypeAlternative, Groupe)
}

KitController.prototype.AddOption = function (value, AttObject, CheckInput){
	this.AddElem(value, AttObject, CheckInput, KitController.TypeOption, 0)
}

KitController.prototype.CalcValue = function(e){
	/*** locate elem ***/
	var elem = null;
	if(e){
		for(i=0; i<this.elems.length; i++){
			if(this.elems[i][0] == KitController.TypeMandatory) continue;
			if(this.elems[i][3] == e.id){
				elem = this.elems[i];
				break;
			}
		}
	}

	/*** calc total value ***/
	this.Value = this.BaseValue;
	for(i=0; i<this.elems.length; i++){
		if(this.elems[i][0] == KitController.TypeAlternative){//if good elem type
			if(elem != null && elem[0] == KitController.TypeAlternative && elem[4] == this.elems[i][4]){
				/*** if elem is alternative and same group
					 adding elem value, and check it. uncheck other group elem
				***/
				if(elem[3] == this.elems[i][3]){//if good elem
					this.elems[i][5] = true;
				}else{
					this.elems[i][5] = false;
				}
			}
			if(this.elems[i][5]){
				this.Value += this.elems[i][1];
				this.Value += this.elems[i][2].Majoration;
			}

		}else if(this.elems[i][0] == KitController.TypeOption){
			if(elem != null && elem[0] == KitController.TypeOption && elem[3] == this.elems[i][3]){
				if(e.checked){
					this.elems[i][5] = true;
				}else{
					this.elems[i][5] = false;
				}
			}
			if(this.elems[i][5]){
				this.Value += this.elems[i][1];
				this.Value += this.elems[i][2].Majoration;
			}
		}
	}
	
	this.Value = Math.round(this.Value*100)/100;

	document.getElementById(this.PriceTag).innerHTML = this.Value + " " + this.Devise;
}

KitController.prototype.AlternativeCheck = function (e){
	//if (!e){e = window.event;e = e.srcElement;}else{e = e.target;}
	this.CalcValue(e);
}

KitController.prototype.OptionCheck = function (e){
	//if (!e){e = window.event;e = e.srcElement;}else{e = e.target;}
	this.CalcValue(e);
}