function findPosX(obj){
     var curleft = 0;
     if (obj.offsetParent){
		  var s = "";
          while (obj.offsetParent){
			   s = obj + "." + s;
               curleft += obj.offsetLeft
               obj = obj.offsetParent;
          }
		  //alert("findPosX="+s);
     }
     else if (obj.x)
          curleft += obj.x;
		  
	 //alert(curleft);
     return curleft;
}

function findPosY(obj){
     var curtop = 0;
     if (obj.offsetParent){
		  var s = "";
          while (obj.offsetParent){
			   s = obj + "." + s;
               curtop += obj.offsetTop
               obj = obj.offsetParent;
          }
		  //alert("findPosY="+s);
     }else if (obj.y)
          curtop += obj.y;
	 //alert(curtop);
     return curtop;
}

var ComboBox_current = "";
var ComboBox_zindex = 500;

function ComboBox(InstanceName, SelectImg, ComboClass, ContentClass, ItemClass){
	/**** private ****/
	this.InstanceName = InstanceName;
	
	this.LibelleName = this.InstanceName + '_ln';
	this.LibelleId = this.InstanceName + '_lid';
	this.ValueName = this.InstanceName + '_vn';
	this.ValueId = this.InstanceName + '_vid';
	
	this.ContentId = this.InstanceName+'_content';	
	this.LibId = this.InstanceName+'_lib';	
	
	this.Items = new Array;
	this.SelectedIndex = 0;

	this.Opened = false;
	this.Height = 130;

	//ComboBox_zindex--;
//	this.zindex = 3000;

	/**** public ****/
	this.SelectImg = SelectImg;
	this.ComboClass = ComboClass;
	this.ContentClass = ContentClass;
	this.ItemClass = ItemClass;
	this.Style = "";
	
	this.OnChange = "";
	this.Disabled = false;
	
	this.value = "";
}

ComboBox.prototype.Draw = function(){
	if(this.Items.length == 0) return;
	
	if(this.value != ""){
		for(i=0; i<this.Items.length; i++){
			if(this.Items[i][1] == this.value){
				this.SelectedIndex = i;
				break;
			}
		}
	}else{
		this.SelectedIndex = 0;
	}

	//Div contenu ComboBox

	//ComboBox
	document.write('<div style="position:relative;z-index:499">');
	document.write('<div id="'+this.ContentId+'" style="text-align:left;position:absolute;top:20px;left:0px;display:none;" class="'+this.ContentClass+'" nowrap>');
	document.write('</div>');
	document.write('<table border="0" cellpadding="0" cellspacing="0">');
	document.write('<tr>');
	document.write('<td>');
	document.write('<input readonly type=text class="'+this.ComboClass+'"');
	document.write('onClick="'+this.InstanceName+'.Open()" ');
	document.write('id="'+this.LibId+'" ');
	if(this.Disabled){
		document.write('style="color:#808080;background:#D4D0C8;'+this.Style+'" ');	
	}else if(this.Style != ""){
		document.write('style="'+this.Style+'" ');
	}
	document.write('OnMouseOver="style.cursor=\'pointer\'" ');	
	document.write('value="'+this.Items[this.SelectedIndex][0]+'">');
	document.write('<input type=hidden id="'+this.ValueId+'" name="'+this.ValueName+'" value="">');
	document.write('<input type=hidden id="'+this.LibelleId+'" name="'+this.LibelleName+'" value="">');
	document.write('</td><td style="width: 17px;">');
	document.write('<a href="javascript:'+this.InstanceName+'.Open()"><img src="'+this.SelectImg+'" border="0"></a>');
	document.write('</td></tr>');
	document.write('</table>');
	document.write('</div>');
	
	this._Select(this.SelectedIndex);
}

ComboBox.prototype.Add = function(Libelle, Valeur){
	var id = this.Items.length;
	this.Items[id] = new Array;
	this.Items[id][0] = Libelle;
	this.Items[id][1] = Valeur;
}



ComboBox.prototype.Open = function(){
	if(this.Disabled) return;

	if(this.Opened){
		this.Close();
		return;
	}

	if(ComboBox_current != ""){
		eval(ComboBox_current + '.Close()');
	}

	var Content = document.getElementById(this.ContentId);

	var NewContent = "";
	NewContent = NewContent + '<table border="0" cellpadding="0" cellspacing="0" width="100%">';
	for(i=0; i<this.Items.length; i++){
		NewContent = NewContent + '<tr><td nowrap class="'+this.ItemClass+'" style="text-align: left;"><a href="javascript:'+this.InstanceName+'.Select('+i+')">'+this.Items[i][0]+'</a></td></tr>';
	}
	NewContent = NewContent + '</table>';

	
	Content.innerHTML = NewContent;

	var divleft = findPosX(document.getElementById(this.LibId)) - 1;
	var divtop = findPosY(document.getElementById(this.LibId)) + document.getElementById(this.LibId).offsetHeight+1;
	
	//divleft = divtop = 0;

	//Content.style.left = divleft
	//Content.style.top = divtop;
	
	
	Content.style.display = "block";
	
	//alert(Content.style.top + "("+divtop+"), " + Content.style.left + "("+divleft+")");
	
	this.Opened = true;
	ComboBox_current = this.InstanceName;
	setTimeout(this.InstanceName + '.AddHandle()', 100);
}



ComboBox.prototype.Close = function(){
	if(ComboBox_current != ""){
		document.getElementById(ComboBox_current+'_content').style.display = "none";
		eval(ComboBox_current+'.Opened = false');
		ComboBox_current = ""
	}
	document.onclick = null;
}

ComboBox.prototype._Select = function(index){
	document.getElementById(this.LibelleId).value = this.Items[index][0];
	document.getElementById(this.ValueId).value = this.Items[index][1];
	this.SelectedIndex = index;
	this.value = this.Items[index][1];
	document.getElementById(this.LibId).value = document.getElementById(this.LibelleId).value
}

ComboBox.prototype.Select = function(index){
	this._Select(index)
	if(this.OnChange != ""){
		this.OnChange = this.OnChange.replace('this', this.InstanceName);
		eval(this.OnChange);
	}
	this.Close();
}

ComboBox.prototype.AddHandle = function(){
	eval('document.onclick = '+this.InstanceName+'.Close');
}


