(function()
{
	//fucntion that adds options to selectbox
	jQuery.fn.addOption = function()
	{
		var a = arguments;
		if(a.length == 0) 
		{
			return this;
		}
		
		//default true
		var secOpt = true;
		//multiple items
		var multOpt = false;
		if(typeof a[0] =="object")
		{
			multOpt= true;
			var items = a[0];
		}
		if(a.length >=2)
		{
			if(typeof a[1] =="boolean") secOpt = a[1];
			else if(typeof a[2] == "boolean") secOpt = a[2];
			if(!multOpt)
			{
			  	var optv = a[0];
			  	var optt = a[1];
			}
		}
	
		this.each(function()
		{
			if(this.nodeName.toLowerCase() != "select") 
			{
				return;
			}
			
			if(multOpt)
			{
				for(var i in items)
				{ 	
					$(this).addOption(i, items[i], secOpt);
				}
			}
			else
			{
				var option = document.createElement("option");
				option.value = optv;
				option.text = optt;
				var i;
				var r =false;
				//get options
				var o = this.options;
				//get number of options
				var optL = o.length;
				//go through existing options
				for(i= 0; i < optL; i++) 
	     				{	
	     					//replace option
	     					if(o[i].value == option.value)
	     					{
	     					      r = true;
	     					      break;
	     					 }
	     				}
	     				if(i < optL && !r) i = optL;
	     				this.options[i] = option;
	     				if(secOpt)
	     				{
	     					o[i].selected = true;
	     				}
	     			}
		});
	};
	
	//function that removes list in the selectbox
	
	jQuery.fn.removeOption = function()
	{
		var a = arguments;
		if(a.length ==0)
		{
			return this;
		}
		var kind = typeof a[0];
		if(kind == "string")
		{	
			var v = a[0];	
		}
		else if(kind == "object" ||  kind == "function")
		{
			var v = a[0]; //regular expression
		}
		else if(kind == "number")
		{
			var i = a[0];
		}
		else
		{
			return this;
		}
		this.each(function()
		{
				if(this.nodeName.toLowerCase() != "select")
				{
					return;
				}
				if(v)
				{
					//get the options
					var opt = this.options;
					//get the amount of options
					var optL = opt.length;
					for(var i=optL-1; i>=0; i--)
					{
							if(v.constructor == RegExp)
							{
									if (opt[i].value.match(v))
									{
											opt[i] = null;
									}
									
							}
							else if(opt[i].value == v)
							{
									opt[i] = null;
							}		
					
					}
				}
				else
				{	
					this.remove(i);
				}
		}
		)
		return this;
		
	}
	
	
	//function that allows the dropbox selection to stay selected
	
	jQuery.fn.selectOptions = function(value, clear)
	{
		var v = value;
		var kindV = typeof value;
		var c = clear || false;
		//has to be a string or regExp
		if(kindV != "string" && kindV != "function" && kindV != "object")
		{
			return this;
		}
		this.each(function()
		{
				if(this.nodeName.toLowerCase() != "select")
				{
					return this;
				}
				//get options
				var opt = this.options;
				//get number of options
				var optL = opt.length;
				
				for(var i = 0; i<optL; i++)
				{
						if(v.constructor == RegExp)
						{
								if(opt[i].value.match(v))
								{
										opt[i].selected = true;
								}
								else if(c)
								{
										opt[i].selected = false;
								}
						}
						else
						{
								if(opt[i].value == v)
								{
										opt[i].selected = true;
								}
								else if(c)
								{
										opt[i].selected = false;
								}
						}
				}
		});
		
		return this;	
	}

	//function that sorts selectboxes ascending or descending order
	jQuery.fn.sortOptions = function(ascending)
	{
		var a = typeof ascending == "undefined" ? true : ascending;
		this.each(function()
		{
			if(this.nodeName.toLowerCase() != "select")
			{
				return;
			}
			//get options
			var opt = this.options;
			//get number of options
			var optL = opt.length;
			//create an array for sorting
			var sortArray = [];
			//go through options, and add to the sortArray
			for(var i = 0; i<optL; i++)
			{
				sortArray[i] =
				{
					v: opt[i].value,
					t: opt[i].text
				};
			}
			//sort items in array
			sortArray.sort(function(opt1, opt2)
			{
				//make it case insensitive
				opt1t = opt1.t.toLowerCase();
				opt2t = opt2.t.toLowerCase();
				//if the same, no sorting is needed
				if(opt1t == opt2t)
				{
					return 0;
				}
				
				if(a)
				{	
					return opt1t < opt2t ? -1 : 1;
				}
				else
				{
					return opt1t > opt2t ? -1 : 1;
				}
			});
			
			//change the options to match the sorted array
			for(var i =0; i<optL; i++)
			{
				opt[i].text = sortArray[i].t;
				opt[i].value = sortArray[i].v;
			}
		});
		
		return this;
	}

	
	
	




















})();	
