// Pictoru's Utils Classes
// SelectUtil Class
var SelectUtil = {
	$:function(id){
		return document.getElementById(id);
	},
	add : function (oL,sN,sV,sS) {
		if(typeof oL=='string')
			oL=this.$(oL);
		var oO = document.createElement('option');
		oO.appendChild(document.createTextNode(sN));
		if (arguments.length = 3)
			oO.setAttribute('value', sV);
		oL.appendChild(oO);
		if(sS==true)
			this.sort(oL);
	},
	addSorted : function (oL,sN,sV) {
		this.add(oL, sN, sV);
		this.sort(oL);
	},
	sort : function(oL){
		if(typeof oL=='string')
			oL=this.$(oL);
		var pos=0;
		while(pos < oL.length){
			var first=oL.options[pos];
			var next=oL.options[pos+1];
			if(next==undefined)
				return;
			if(next.text < first.text){
				oL.insertBefore(next,first);
				this.sort(oL);
			}
			pos++;
		}
	},
	sortByValue : function(oL){
		if(typeof oL=='string')
			oL=this.$(oL);
		var pos=0;
		while(pos < oL.length){
			var f=oL.options[pos];
			var n=oL.options[pos+1];
			if(n==undefined)
				return;
			if(n.value < f.value){
				oL.insertBefore(n,f);
				this.sortByValue(oL);
			}
			pos++;
		}
	},
	remove : function (oL, iI) {
		if(typeof oL=='string')
			oL=this.$(oL);
		oL.remove(iI);
	},
	clear : function(oL){
		if(typeof oL=='string')
			oL=this.$(oL);
		for (var i=oL.options.length-1; i >= 0; i--) {
			this.remove(oL, i);
		}
	},
	setSelectedByValue : function(oL,oV){
		if(typeof oL=='string')
			oL=this.$(oL);
		for(var i=0;i<oL.length;i++){
			if(oL.options[i].value==oV){
				oL.selectedIndex=i;
				break;
			}
		}
	},
	setSelectedByText : function(oL,oT){
		if(typeof oL=='string')
			oL=this.$(oL);
		for(var i=0;i<oL.length;i++){
			if(oL.options[i].text==oT){
				oL.selectedIndex=i;
				break;
			}
		}
	}
};
