//	Version 1.0

//**********************************************************************************
// PAGE'S NAME			: all_selobj_lib.js
// DESCRIPTION			: Methods to build a Select Object
// AUTHOR				: LLABORIE
// DATE OF CREATION		: 06 26 2001
// DATE OF LAST UPDATE	: 07 02 2001
//**********************************************************************************

// Function to return the good object who is name is nomcalque
function div(nomcalque) {
  var arg = div.arguments;
  var retour;
  var nomcalque = arg[0];
  var style = (arg[1] == "style");
  if (style) {																								// return style proprieties of object
	  if(document.getElementById) {															// ie5 and more and ns6
  	  retour = document.getElementById(nomcalque).style;
  	} else if(document.all) {																	// ie4
    	retour = document.all[nomcalque].style;
  	} else {																									// other (ns 4 and more)
    	retour = document[nomcalque];
  	}
  } else {																										// return simple object
	  if(document.getElementById) {															// ie5 and more and ns6
  	  retour = document.getElementById(nomcalque);
  	} else if(document.all) {																	// ie4
    	retour = document.all[nomcalque];
  	} else {																									// other (ns 4 and more)
    	retour = document[nomcalque];
  	}
  }
  return retour;
}

// Function to initialize the coords of the layer name
function InitializeDiv(name) {
	var taille;
	div(name+'lay','style').top = div(name+'test','style').top ;
	div(name+'lay','style').left = div(name+'test','style').left ;
	div(name+'lay','style').width = div(name+'test','style').width;
	eval("taille = "+name+"Obj.options.length");
	if (taille > 8) {
		div(name+'interlay','style').height=150;
	}
}

// change the visibility proprieties of the layer "name" (ie compatibility)
function swapsel() {
	var arg = swapsel.arguments;
	InitializeDiv(arg[0]);									// initialize proprieties of layer
	var lay = div(arg[0]+'lay' ,'style');		// find the good layer
																					// arg[1] is a Flag to indicate if the layer can be display
	if (arg[1] == 0) {											// if layer can't
		lay.visibility='hidden';							// hidden layer
	} else {
		if (lay.visibility == 'visible') {		// if layer is display
			lay.visibility='hidden';						// hidden layer
		} else {
			KSTObj.TousSauf(arg[0]+'lay');			// call TousSauf Method ro hidden all of layer 
			lay.visibility='visible';						// display layer
		}
	}
}

// Object (Mise en forme)
function ObjMiseEnForme(){
// for extern table
// argument 0 : name of style class
// argument 1 :	thickness of border
// argument 2 : Color of border 
// argument 3 : spacing of Cells
// argument 4 : padding of cells
// argument 5 : Background color 
// argument 6 : Background image
// intern table
// argument 7 : name of style class
// argument 8 : thickness of border
// argument 9 : Color of border
// argument 10 : spacing of cells
// argument 11 : padding of cells
// argument 12 : default background color
// argument 13 : actived background color
// argument 14 : default text color
// argument 15 : actived text color
// argument 16 : default thickness text 
// argument 17 : actived thickness text

	var arg = ObjMiseEnForme.arguments;
	// Générale
	this.styleGen = arg[0];
	this.borderGen = arg[1];
	this.borderColorGen = arg[2];
	this.CellspacingGen = arg[3];
	this.CellpaddingGen = arg[4];
	this.BgColorGen = arg[5];
	this.BackgroundGen = arg[6];
	// Particulier
	this.style = arg[7];
	this.border = arg[8];
	this.borderColor = arg[9];
	this.Cellspacing = arg[10];
	this.Cellpadding = arg[11];
	this.BgColor1 = arg[12];
	this.BgColor2 = arg[13];
	this.FontColor1 = arg[14];
	this.FontColor2 = arg[15];
	this.FontBold1 = arg[16];
	this.FontBold2 = arg[17];
	// in td
	this.align = arg[18];
	
	this.affiche = affichemef;
}

function affichemef(type) {
	var htmlstr = "";
	if (type == "gen") { 
		if (this.styleGen != "") {
			htmlstr += " class=\""+this.styleGen+"\"";
		}
		htmlstr += " Border=\""+this.borderGen+"\"";
		if (this.borderGen > 0) {
			htmlstr += " BorderColor=\""+this.borderColorGen+"\"";
		}
		htmlstr += " Cellspacing=\""+this.CellspacingGen+"\"";
		htmlstr += " Cellpadding=\""+this.CellpaddingGen+"\"";
		if (this.BgColorGen  != "") {
			htmlstr += " bgcolor=\""+this.BgColorGen+"\"";
		}
		if (this.BackgroundGen != "") {
			htmlstr += " Background=\""+this.BackgroundGen+"\"";
		}
	} else {
		if ("td") {
			if (this.style != "") {
				htmlstr += " class=\""+this.style+"\"";
			}
			if (this.BgColor1 != "") {
				htmlstr += " bgcolor=\""+this.BgColor1+"\"";
			}
			if (this.align != "") {
				htmlstr += " align=\""+this.align+"\"";
			}
		} else {
			if (this.style != "") {
				htmlstr += " class=\""+this.style+"\"";
			}
			htmlstr += " Border=\""+this.border+"\"";
			if (this.border > 0) {
				htmlstr += " BorderColor=\""+this.borderColor+"\"";
			}
			htmlstr += " Cellspacing=\""+this.Cellspacing+"\"";
			htmlstr += " Cellpadding=\""+this.Cellpadding+"\"";	
		}
	}
	return htmlstr;
}

// Objet select
function ObjSelect() {
	var arg = ObjSelect.arguments;
	// variables
	this.name = arg[0];					// Name of Object
	this.form	= arg[1];					// Form's name
	this.top = 0;								// coords x of start of layer
	this.left = 0;							// coords y of start of layer
	this.width = arg[2];				// whidth of layer 
	this.zindex = arg[3];				// z-index of layer
	this.bouton = arg[4];				// HTML string to build the button
	this.onchangestr = arg[5];	// String who content a Javascript code invoke whith onChange envent of input tag
	this.TimeID = null;					// Object who represent a SetTimeOut
	
	this.options = new Array();	// Options liste
	this.mef = null;						// link to a "Mise en forme" object
	this.selection = -1;				// n° of selected option
	// Methods
	this.addOption = addOption;	// add an option
	this.affiche = afficheSel;	// build the html code
	this.addmef = addmef;				// build a mef object
	// initialization
	KSTObj.addElement(arg[0]+"lay");
}

function addmef() {
	var arg = addmef.arguments;
	this.mef = new ObjMiseEnForme(arg[0],arg[1],arg[2],arg[3],arg[3],arg[5],arg[6],arg[7],arg[8],arg[9],arg[10],arg[11],arg[12],arg[13],arg[14],arg[15],arg[16],arg[17],arg[18]);
}

function addOption() {
	var arg = addOption.arguments;
	var x = this.options.length;
	if (arg[2] == "default") {
		this.selection = x;
	}
	this.options[x] = new ObjOption(arg[0],arg[1],this.mef,this.name,x,this.form,this.onchangestr);
}

function afficheSel() {
	var htmlstr = "";
	htmlstr += "<table border=0 cellspacing=0 cellpadding=0><tr><td>"
		htmlstr += "<span id=\""+this.name+"test\" style=\"{position:relative}\">";
			htmlstr += "<table border=0 cellpadding=0 cellspacing=0><tr><td>"
			htmlstr += "<input type=\"text\" name=\"" + this.name + "\" size=\""+this.width +"\"";
			htmlstr += " onFocus=\"swapsel('"+this.name+"');this.blur();\"";
			if (this.onchangestr != "") {
				htmlstr += " onChange=\"alert('coucou');"+this.onchangestr+"\"";
			}
			if (this.selection > -1) {
				htmlstr += " value=\""+this.options[this.selection].label+"\"";
			}
			htmlstr += ">";
			htmlstr += "<input type=\"hidden\" name=\""+ this.name + "ID\""; 
			if (this.selection > -1) {
				htmlstr += " value=\""+this.options[this.selection].value+"\"";
			}
			htmlstr += ">";
			htmlstr += "</td><td>"
			htmlstr += "<a name=\""+this.name+"anchor\" href=\"Javascript:swapsel('"+this.name+"');\"";
				//htmlstr += " onClick=\"swapsel('"+this.name+"');\"";
				// on peut rajouter des gestion d'evenement
			htmlstr += ">"+this.bouton+"</a>";
			htmlstr += "</td></tr></table>"
		htmlstr += "</span>"
	htmlstr += "</td></tr></table>"
	htmlstr += "<div name=\""+this.name+"lay\" id=\""+this.name+"lay\" style=\"{position:absolute;top="+this.top+";left="+this.left+";z-index="+this.zindex+";visibility=hidden; overflow=auto;}\">";
	htmlstr += "<table width=\"100%\"";
	htmlstr += this.mef.affiche("gen");
	htmlstr += ">";
	htmlstr += "<tr><td>";
	htmlstr += "<div name=\""+this.name+"interlay\" id=\""+this.name+"interlay\" style=\"overflow=auto;\">";
	htmlstr += "<table width=100%";
	htmlstr += this.mef.affiche("part");
	htmlstr += ">";
	for (i=0;i<this.options.length;i++) {
		htmlstr += this.options[i].affiche();
		//alert(this.options[i].affiche());
		if (htmlstr.length > 2000) {
			document.write(htmlstr);
			//alert(htmlstr);
			htmlstr = "";
		}		
	}
	htmlstr += "</table>";
	htmlstr += "</div>";
	htmlstr += "</td></tr></table></div>";
	document.write(htmlstr);
	//document.write("<code>" + htmlstr + "</code>");
	//alert(htmlstr);
}

function ObjOption() {
	var arg = ObjOption.arguments;
	// Les variables
	this.label = arg[0];
	this.value = arg[1];
	this.mef = arg[2];
	this.name = arg[3];
	this.number = arg[4];
	this.form = arg[5];
	this.onchangestr = arg[6];
	// Les Méthodes
	this.affiche = afficheOpt;
}

function afficheOpt() {
	var htmlStr = "<tr><td";
	htmlStr += this.mef.affiche("td");
	htmlStr += " name=\""+this.name + this.number+"\" id=\""+this.name + this.number+"\" class=\"objetselect\"";
	htmlStr += " onClick=\"document." + this.form + "." + this.name + ".value='" + this.label +"';document." + this.form + "." + this.name + "ID.value='" + this.value +"';swapsel('"+this.name+"');"+this.onchangestr+"\"";
	htmlStr += " onMouseOut=\"div('"+this.name + this.number+"','style').background='"+this.mef.BgColor1+"';div('"+this.name + this.number+"','style').color='"+this.mef.FontColor1+"';div('"+this.name + this.number+"','style').fontweight='"+this.mef.FontBold1+"';\"";
	htmlStr += " onMouseOver=\"div('"+this.name + this.number+"','style').background='"+this.mef.BgColor2+"';div('"+this.name + this.number+"','style').color='"+this.mef.FontColor2+"';div('"+this.name + this.number+"','style').fontweight='"+this.mef.FontBold1+"';\"";
	htmlStr += ">";
	//htmlStr += "<span name=\""+this.name + this.number+"\" id=\""+this.name + this.number+"\" class=\"objetselect\">";
	//htmlStr += "<a href=\"#\"";
	//htmlStr += " onClick=\"document." + this.form + "." + this.name + "chp.value='" + this.value +"';swapsel('"+this.name+"lay','+this.name+"test');\"";
	//htmlStr += " onMouseOver=\"div('"+this.name + this.number+"','style').background='#00FF00';\"";
	//htmlStr += " onMouseOut=\"div('"+this.name + this.number+"','style').background='#FFFFFF';\"";
	// on peut rajouté des gestion d'évenement ici
	//htmlStr += ">";	
	htmlStr += this.label;
	//htmlStr += "</a>";
	//htmlStr += "</span>";
	htmlStr += "</td></tr>";
	return htmlStr;
}

// objet KiSaiTout
function KiSaiTout(){
	this.ObjetSelect = new Array();
	this.State = new Array();
	this.indice = 0;
	
	this.addElement = addElement;
	this.indiceOff = indiceOff;
	this.visible = KSTvisible;
	this.cacher = KSThidden;
	this.TousSauf = TousSauf;
	
}

function addElement() {
	var arg = addElement.arguments;
	this.ObjetSelect[this.indice] = arg[0];
	this.State[this.indice] = 0;
	this.indice ++;
}

function indiceOff(LayName){
	for (i=0;i<this.indice;i++){
		if (this.ObjetSelect[i] == LayName) {
			return i;
		}
	}
	return -1;
}

function KSTvisible(indice){
	this.State[indice] = 1;
}

function KSThidden(indice){
	this.State[indice] = 0;
	div(this.ObjetSelect[indice],'style').visibility = 'hidden';
}

function TousSauf(){
	var arg = TousSauf.arguments;
	var indice;
	if (typeof arg[0] == "string") {
		indice = this.indiceOff(arg[0]);
	} else {
		indice = arg[0];
	}
	for (i=0;i<this.indice;i++) {
		if (indice == i) {
			this.visible(i);
		} else {
			this.cacher(i);
		}
	}
}

var KSTObj = new KiSaiTout();