function findPosX(obj){
     var curleft = 0;
     if (obj.offsetParent){
          while (obj.offsetParent){
               curleft += obj.offsetLeft
               obj = obj.offsetParent;
          }
     }
     else if (obj.x)
          curleft += obj.x;
     return curleft;
}

function findPosY(obj){
     var curtop = 0;
     if (obj.offsetParent){
          while (obj.offsetParent){
               curtop += obj.offsetTop
               obj = obj.offsetParent;
          }
     }else if (obj.y)
          curtop += obj.y;
     return curtop;
}

/****************************************************************************
				<script>
					CBsearch = new ComboBox('CBsearch', "#pageurl#/ximg/arrow_select.gif", "select_search", "select_search_box", "select_search_item");
					CBsearch.LibelleName = 'lib_cat_id'
					CBsearch.ValueName = 'cat_id'
					//CBsearch.OnChange = 'document.form_header_search.submit();'
					CBsearch.Style = 'width:106px';
					CBsearch.Add('#replace(label_TousLesProduits, "'", "\'", "all")#', '');
					
					{boucle GetMenuCat}
						{si cat_id neq 63 and cat_id neq 62}
							CBsearch.Add('#replace(cat_nom, "'", "\'", "all")#', '#cat_id#');
							{si selected}CBsearch.value = '#cat_id#';{finsi}
						{finsi}
					{finboucle}             
					CBsearch.Draw();
				</script>
****************************************************************************/

var ComboBox_current = "";
var ComboBox_zindex = 200;

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 = ComboBox_zindex;

	/**** 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;
	}

	//ComboBox
	document.write('<div style="position:relative;z-index:'+this.zindex+'">');
	
	//Div contenu ComboBox
	document.write('<div id="'+this.ContentId+'" style="position:absolute;display:none;z-index:'+this.zindex+'" 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>')
	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 style="z-index:'+this.zindex+'" border="0" cellpadding="0" cellspacing="0" width="100%">';
	for(i=0; i<this.Items.length; i++){
		NewContent = NewContent + '<tr><td nowrap class="'+this.ItemClass+'"><a href="javascript:'+this.InstanceName+'.Select('+i+')">'+this.Items[i][0]+'</a></td></tr>';
	}
	NewContent = NewContent + '</table>';
	
	Content.innerHTML = NewContent;
	
	//Content.style.left = findPosX(document.getElementById(this.LibId)) - 1;
	//var divtop = findPosY(document.getElementById(this.LibId)) + document.getElementById(this.LibId).offsetHeight - 12;
	//Content.style.top = divtop
	
	Content.style.left = 19;
	Content.style.top = 22;
	Content.style.display = "block";
	
	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');
}

