
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);
}


/******* CAttribut *******/
var CAttribut_validate = new Array;

function CAttItem (code_ean, option_id, majoration_prix, qte_stock, delai_liv){
	this.Datas = new Array;
	this.EAN = code_ean;
	this.OptID = option_id;
	this.Majoration = majoration_prix;
	this.Stock = qte_stock;
	this.DelaiLiv = delai_liv;
}
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.SelectClass = "";

	this.KitController = null;
	
	this.PrixBase = 0;
	this.Devise = "EUR";
	this.Majoration = 0;
	this.pagesURL = pagesURL;
	this.SelectOK = false;
	
	this.DelaiLivLibelles = new Array;
	
	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.AddDelaiLivLibelle = function (DelaiID, DelaiText){
	this.DelaiLivLibelles[DelaiID] = DelaiText;
}
CAttribut.prototype.SetDelaiLivTag = function (InputName){
	this.DelaiLivTag = InputName;
}
CAttribut.prototype.SetSelectClass = function (css){
	this.SelectClass = css;
}

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, delai_liv){
	if(this.PrevEAN != code_ean || this.PrevAtt == attribut_id){
		//recherche si EAN existant
		var found = false;
		if(this.PrevEAN != code_ean)
		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, delai_liv);
		}
		this.PrevEAN = code_ean;
		this.PrevAtt = attribut_id;
	}
	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();
					}		
					else
					{
						if(this.DelaiLivTag != "") document.getElementById(this.DelaiLivTag).innerHTML = this.DelaiLivLibelles[this.EANList[i].DelaiLiv];
						if(typeof layer_major != "undefined"){
							layer_major.innerHTML = '<b>Majoration :</b> +' + this.EANList[i].Majoration + " &euro;";
						}
					}			
					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)'" + ((this.SelectClass != "")? " class='" + this.SelectClass + "'" : "") + ">");
			document.write("<option value='0'>"+this.DefaultValue+"</option>");
			if(i == 0){
				var TxtSorted = new Array;
				var InsertIndex = 0;
				var s = 0;
				for(var z in this.AttArbo){
					s = TxtSorted.length - 1;
					if(s != -1){
						while(s >= 0 && TxtSorted[s][1] > this.AttArbo[z][1]){
							TxtSorted[s+1] = TxtSorted[s];
							s--;
						}
						s++;
					}else s = 0;
					TxtSorted[s] = this.AttArbo[z];
					TxtSorted[s][TxtSorted[s].length] = z;
				}
				for(var z in TxtSorted){
					document.write("<option value='"+TxtSorted[z][TxtSorted[z].length-1]+"'>"+TxtSorted[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, DefaultPrix){
	this.ObjectName = ObjectName;
	this.elems = new Array;
	this.BaseValue = DefaultPrix;
	this.MaxValue = this.BaseValue;
	this.Value = 0;	
	this.PriceTag = "";
	this.MaxPriceTag = "";
	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.elems[nid][5] = true;
}

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;
}

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.DynMax = this.MaxValue;
	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.DynMax += this.elems[i][1];
				this.DynMax += this.elems[i][2].Majoration;
				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.DynMax += this.elems[i][1];
				this.DynMax += this.elems[i][2].Majoration;
				this.Value += this.elems[i][1];
				this.Value += this.elems[i][2].Majoration;
			}
		}else if(this.elems[i][0] == KitController.TypeMandatory){
			if(this.elems[i][2]){
				this.DynMax += this.elems[i][2].Majoration;
				this.Value += this.elems[i][2].Majoration;
			}
		}
	}
	
	this.DynMax = Math.round(this.DynMax*100)/100;
	this.Value = Math.round(this.Value*100)/100;

	if(this.PriceTag)
		document.getElementById(this.PriceTag).innerHTML = this.Value + " " + this.Devise;
	if(this.MaxPriceTag)
		document.getElementById(this.MaxPriceTag).innerHTML = this.DynMax + " " + 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);
}


function favoris() {
	if (navigator.appName != 'Microsoft Internet Explorer') { 
		window.sidebar.addPanel("Mistersport","http://www.mistersport.com",""); 
	} else { 
		window.external.AddFavorite("http://www.mistersport.com","Mistersport"); 
	} 
}
