(function(a){a.fn.addOption=function(){var e=function(a,f,l,k,g){var c=document.createElement("option");c.value=f,c.text=l;var e=a.options,h=e.length;if(!a.cache){a.cache={};for(var d=0;d<h;d++)a.cache[e[d].value]=d}if(g||g==0)for(var i=c,b=g;b<=h;b++){var j=a.options[b];a.options[b]=i;e[b]=i;a.cache[e[b].value]=b;i=j}if(typeof a.cache[f]=="undefined")a.cache[f]=h;a.options[a.cache[f]]=c;if(k)c.selected=true},a=arguments;if(a.length==0)return this;var b=true,d=false,c,g,f;if(typeof a[0]=="object"){d=true;c=a[0]}if(a.length>=2){if(typeof a[1]=="boolean"){b=a[1];startindex=a[2]}else if(typeof a[2]=="boolean"){b=a[2];startindex=a[1]}else startindex=a[1];if(!d){g=a[0];f=a[1]}}this.each(function(){if(this.nodeName.toLowerCase()!="select")return;if(d)for(var a in c){e(this,a,c[a],b,startindex);startindex+=1}else e(this,g,f,b,startindex)});return this};a.fn.ajaxAddOption=function(f,b,c,d,e){if(typeof f!="string")return this;if(typeof b!="object")b={};if(typeof c!="boolean")c=true;this.each(function(){var g=this;a.getJSON(f,b,function(b){a(g).addOption(b,c);if(typeof d=="function")if(typeof e=="object")d.apply(g,e);else d.call(g)})});return this};a.fn.removeOption=function(){var b=arguments;if(b.length==0)return this;var c=typeof b[0],a,d;if(c=="string"||c=="object"||c=="function"){a=b[0];if(a.constructor==Array){for(var f=a.length,e=0;e<f;e++)this.removeOption(a[e],b[1]);return this}}else if(c=="number")d=b[0];else return this;this.each(function(){if(this.nodeName.toLowerCase()!="select")return;if(this.cache)this.cache=null;var c=false,f=this.options;if(!!a)for(var g=f.length,e=g-1;e>=0;e--){if(a.constructor==RegExp){if(f[e].value.match(a))c=true}else if(f[e].value==a)c=true;if(c&&b[1]===true)c=f[e].selected;if(c)f[e]=null;c=false}else{if(b[1]===true)c=f[d].selected;else c=true;c&&this.remove(d)}});return this};a.fn.sortOptions=function(b){var c=a(this).selectedValues(),d=typeof b=="undefined"?true:!!b;this.each(function(){if(this.nodeName.toLowerCase()!="select")return;for(var b=this.options,e=b.length,c=[],a=0;a<e;a++)c[a]={v:b[a].value,t:b[a].text};c.sort(function(a,b){o1t=a.t.toLowerCase(),o2t=b.t.toLowerCase();return o1t==o2t?0:d?o1t<o2t?-1:1:o1t>o2t?-1:1});for(var a=0;a<e;a++){b[a].text=c[a].t;b[a].value=c[a].v}}).selectOptions(c,true);return this};a.fn.selectOptions=function(e,d){var b=e,c=typeof e;if(c=="object"&&b.constructor==Array){var g=this;a.each(b,function(){g.selectOptions(this,d)})}var f=d||false;if(c!="string"&&c!="function"&&c!="object")return this;this.each(function(){if(this.nodeName.toLowerCase()!="select")return this;for(var c=this.options,d=c.length,a=0;a<d;a++)if(b.constructor==RegExp){if(c[a].value.match(b))c[a].selected=true;else if(f)c[a].selected=false}else if(c[a].value==b)c[a].selected=true;else if(f)c[a].selected=false});return this};a.fn.copyOptions=function(b,d){var c=d||"selected";if(a(b).size()==0)return this;this.each(function(){if(this.nodeName.toLowerCase()!="select")return this;for(var e=this.options,f=e.length,d=0;d<f;d++)(c=="all"||c=="selected"&&e[d].selected)&&a(b).addOption(e[d].value,e[d].text)});return this};a.fn.containsOption=function(f,d){var a=false,c=f,e=typeof c,b=typeof d;if(e!="string"&&e!="function"&&e!="object")return b=="function"?this:a;this.each(function(){if(this.nodeName.toLowerCase()!="select")return this;if(a&&b!="function")return false;for(var f=this.options,g=f.length,e=0;e<g;e++)if(c.constructor==RegExp){if(f[e].value.match(c)){a=true;b=="function"&&d.call(f[e],e)}}else if(f[e].value==c){a=true;b=="function"&&d.call(f[e],e)}});return b=="function"?this:a};a.fn.selectedValues=function(){var a=[];this.selectedOptions().each(function(){a[a.length]=this.value});return a};a.fn.selectedTexts=function(){var a=[];this.selectedOptions().each(function(){a[a.length]=this.text});return a};a.fn.selectedOptions=function(){return this.find("option:selected")}})(jQuery)
