/*
SELECT DROP-DOWN BOX
Acest script genereaza un select dropdown in care pot fi editate toate elementele.
Pornind de la un select genereaza dinamic un input in locul vechiului select pentru afisarea textului si
o lista <ul><li>text</li></ul> pentru optiunile din select

Toti parametrii sunt obligatorii.

Exemplu de apelare:

function select(parentNodeId,id,DisplayTextInputId,inputClass,listClass){

parentNodeId	    - id-ul div-ului parinte pentru selectBox
id		    	    - id-ul selectBox-ului
DisplayTextInputId - id-ul inputului in care va fi afisat textul din selectBox
inputClass               - clasa CSS a inputului in care va fi afisat textul din selectBox
listClass                  - clasa CSS pentru lista de optiuni din selectBox

*/

function getWidth(element)  { return (document.all ? element.offsetWidth : element.clientWidth); }
function getHeight(element) { return (document.all ? element.offsetHeight : element.clientHeight); }
function getXpos(obj) {

		var elem = obj;
		var xPos = 0;

		while (elem) {
			xPos += elem.offsetLeft;
			elem = elem.offsetParent;
		}

		return xPos;
	}


	function getYpos(obj) {

		var elem = obj;
		var yPos = 0;

		while (elem) {
			yPos += elem.offsetTop;
			elem = elem.offsetParent;
		}

	return yPos;

}

		var value = new Array();
		var name = new Array();
		var container;
		var input;
		var contor = 2;
		var parentSelectBox;
		var selectInit;
		var valoareInput;
		var valoareaCurenta;
		var indiceCurent=-1;
		var over = false;

		function select(parentNodeId,selectId,DisplayTextInputId,inputClass,listClass,open,paddingRightVar){
				selectInit = $(selectId);
				parentSelectBox = $(parentNodeId);
				if(parentSelectBox.hasChildNodes())
				{
				    while(parentSelectBox.childNodes.length >= 1)
				    {
				    	parentSelectBox.removeChild(parentSelectBox.firstChild);
				    }
				}

				var selectedValue = selectInit.options[selectInit.selectedIndex];
				container = document.createElement('div');
				container.className = listClass;
				container.style.display = "none";
				selectInit.style.display = "none";
				container.id = selectId+"_div";
				container.onmouseover = function(){over = true;}
				container.onmouseout  = function(){over = false;}
				parentSelectBox.appendChild(container);

				input = document.createElement("input");
				input.type = "text";
				input.id   = DisplayTextInputId;
				input.onkeydown = function(){
					valoareInput = this.value;
				}

				input.onkeyup = new Function("var indicePrecedent = indiceCurent;var valoareInputAux = this.value.toUpperCase();valoareInputAux = valoareInputAux.substr(valoareInput.length, valoareInputAux.length);for(var i=0;i<$('"+selectId+"_ul').childNodes.length;i++){if(($('"+selectId+"_ul').childNodes[i].innerHTML.indexOf(valoareInputAux)==0)&&(valoareaCurenta!=$('"+selectId+"_ul').childNodes[i].innerHTML)&&(indiceCurent<i)){valoareaCurenta = $('"+selectId+"_ul').childNodes[i].innerHTML;$('"+selectId+"_ul').childNodes[i].onclick();$('"+selectId+"_ul').scrollTop = getHeight($('"+selectId+"_ul').childNodes[i])*(i+1);indiceCurent = i;break;}}if(indicePrecedent==indiceCurent){this.value=valoareInput;indiceCurent=-1;}");
				input.className = inputClass;
				input.onclick =function(){
					valoareInput = this.value;
					contor++;
					open = true;
					if(contor%2!=0 && open){
						$(selectId+'_div').style.display = "";
					}else{
						$(selectId+'_div').style.display = "none";
					}
					//select(parentNodeId,id,DisplayTextInputId,inputClass,listClass,open);
				}
				input.onblur = function(){
					if(!over){
						contor = 1;
						open=false;
						this.onclick();
					}
				}
				input.value = selectedValue.text;
				parentSelectBox.appendChild(input);
				var n = selectInit.childNodes.length;
				var contorIntern = 0;
						
				/*$(selectId+'_div').style.width = parseInt($('container_'+selectId).style.width,10) + 'px';*/
				input.style.paddingLeft = 0;
				if(!paddingRightVar)
				{
					input.style.paddingRight = 0;
				}
				else
				{
					input.style.paddingRight = paddingRightVar + 'px';
				}
				
				newSelect = document.createElement('ul');
				newSelect.id = selectId+"_ul";
				newSelect.onclick = function(){if(!document.all){$(DisplayTextInputId).onclick();}else{$(DisplayTextInputId).onclick();$(DisplayTextInputId).blur();} }
				OnChangeFunction = "";
				for(var i=0;i<n;i++)
				{
					if(selectInit.childNodes[i].tagName == "OPTION"){
						newItem = document.createElement('li');
						newItem.onmouseover = function(){over = true;}
						newItem.onmouseout  = function(){over = false;}
						newItem.innerHTML = selectInit.childNodes[i].innerHTML;
						OnChangeFunctionAux = "";
						newItem.onclick = new Function('$(\''+selectInit.name+'\').value = \''+selectInit.childNodes[i].value+'\';$(\''+selectInit.name+'\').onchange(\''+selectInit.childNodes[i].value+'\'); $(\''+DisplayTextInputId+'\').value=\''+selectInit.childNodes[i].innerHTML+'\';'+OnChangeFunctionAux);
						newSelect.appendChild(newItem);
					}
				}

				container.appendChild(newSelect);
				container.onmousemove = function(){over = true;$(DisplayTextInputId).focus()}
				if(contor%2!=0 && open){
					container.style.display = "";
				}else{
					container.style.display = "none";
				}
		}
