 
;(function($) {

/**
 * Sort options (ascending or descending) in a select box (or series of select boxes)
 *
 * @name     sortOptions
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     jQuery
 * @param    Boolean ascending   (optional) Sort ascending (true/undefined), or descending (false)
 * @example  // ascending
 * $("#myselect").sortOptions(); // or $("#myselect").sortOptions(true);
 * @example  // descending
 * $("#myselect").sortOptions(false);
 *
 */ 
 
$.fn.sortOptions = function(ascending)
{
	// get selected values first        
	var sel = $(this).selectedValues();
	var a = typeof(ascending) == "undefined" ? true : !!ascending;
        try{
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
                        if(this.id == "playlistPosition") return;
                        if(this.id == "months") return;
                        if(this.id == "materialMethod") return;
                        if(this.id == "autoresponder_value") return;
                        if(this.id == "datafield") return;
                        if(this.id == "nof") return;
                        if(this.id == "labeltype") return;
                        if(this.id == "labeltype") return;
                        if(this.id == "tagId") return;
                        if(this.id == "dates") return;
                        if(this.id == "privilage") return;
						if(this.id == "autoResponderFields") return;
						if(this.id == "customFormId") return;
                        if(this.id == "frompiece") return;
						if(this.id == "categoryId") return;
						if(this.id == "labelId") return;
						if(this.id == "moreactions") return;
						if(this.id == "criticalCustomFormId") return;
						if(this.id == "checklistCustomFormId") return;
						if(this.id == "productCategoryId") return;
						if(this.id == "searchCategory") return;
						if(this.id == "searchTag") return;
						if(this.id == "priceCategoryId") return;
						if(this.id == "priceListTypeId") return;
						if(this.id == "fromactivity") return;
						if(this.id == "toactivity") return;
						if(this.id == "sortReportJob") return;
						if(this.id == "sortScheduledJob") return;
						if(this.name == "months") return;
						if(this.id == "updateOrderStatus") return;						
			// get options
			var o = this.options;
			// get number of options
			var oL = o.length;
			// create an array for sorting
			var sA = [];
			// loop through options, adding to sort array
			var flag=0;
                        var addNewFlag=0;
                        var addNewText="";
                        var addNewValue="";
			var text=(o[0]==undefined)?"":o[0].text;
			var textValue=(o[0]==undefined)?"":o[0].value;
			var textId=(o[0]==undefined)?"":o[0].id;
			var disabled=(o[0]==undefined)?false:o[0].disabled;
                        var selected=(o[0]==undefined)?false:o[0].selected;
			if(o[0]!=undefined && (o[0].text.toLowerCase()=="please select" || o[0].text.toLowerCase()=="view all"
			|| o[0].text.toLowerCase()=="-please select-" || o[0].text.toLowerCase()=="select" || o[0].text.toLowerCase()=="all")){
				flag=1;
				for(var i = 1; i<oL; i++){
                                if(o[i].text.toLowerCase()!="add new" || o[i].text.toLowerCase()=="addnew"){
				if(o[i].id){
					sA[i] = {
						v: o[i].value,
						t: o[i].text,					
						d: o[i].id,
						e: o[i].disabled,
                                                s: o[i].selected,
                                                c: $(o[i]).attr("style")
					}
				}else{
					sA[i] = {
						v: o[i].value,
						t: o[i].text,	
						e: o[i].disabled,
                                                s: o[i].selected,
                                                c: $(o[i]).attr("style")
					}
				}
                                }else{
                                    addNewText=o[i].text;
                                    addNewValue=o[i].value;
                                }
				}				
				
			}else{
                            for(i = 0; i<oL; i++){
                              if(o[i].text.toLowerCase()!="add new" || o[i].text.toLowerCase()=="addnew"){
				if(o[i].id){				
					sA[i] = {
						v: o[i].value,
						t: o[i].text,					
						d: o[i].id,	
						e: o[i].disabled,
                                                s: o[i].selected,
                                                c: $(o[i]).attr("style")
					}
				}else{
					sA[i] = {
						v: o[i].value,
						t: o[i].text,	
						e: o[i].disabled,
                                                s: o[i].selected,
                                                c: $(o[i]).attr("style")
					}
				}
			     }else{
                                    addNewText=o[i].text;
                                    addNewValue=o[i].value;
                             }
                           }
			}
			
			
			// sort items in array
			sA.sort(
				function(o1, o2)
				{
					// option text is made lowercase for case insensitive sorting
					o1t = o1.t.toLowerCase(), o2t = o2.t.toLowerCase();
					// if options are the same, no sorting is needed
					if(o1t == o2t) return 0;					
					if(a)
					{
						return o1t < o2t ? -1 : 1;
					} else{
						return o1t > o2t ? -1 : 1;
					}
				}
			);
			
			// change the options to match the sort array                        
			if(flag==1){
                                if(o.length!=0 &&  o[0]!=undefined){
                                    this.options[0] = new Option(text,textValue, false, true);
                                    if(disabled)
                                    o[0].disabled=disabled;
                                    if(o[0].id)
                                    o[0].id=textId;
                                    if(selected)
                                    o[0].selected=selected;
                                }
                                if(addNewFlag==1){
                                    oL=oL-1;
				}
				for(i = 0; i<oL; i++){	
                                    if(sA[i]!=undefined && o.length!=0 && o[i+1]!=undefined && sA[i].d){
										o[i+1].disabled=false;
                                        o[i+1].text = sA[i].t;
                                        o[i+1].value = sA[i].v;
                                        o[i+1].id = sA[i].d;
                                        if(sA[i].e)
                                        o[i+1].disabled=sA[i].e;
                                        if(sA[i].s)
                                        o[i+1].selected=sA[i].s;
                                        if(sA[i].c)
                                        $(o[i+1]).attr("style",sA[i].c);
                                    }else if(sA[i]!=undefined && o.length!=0  && o[i+1]!=undefined){
										o[i+1].disabled=false;
                                        o[i+1].text = sA[i].t;
                                        o[i+1].value = sA[i].v;
                                        if(sA[i].e)
                                        o[i+1].disabled=sA[i].e;
                                        if(sA[i].s)
                                        o[i+1].selected=sA[i].s;
                                        if(sA[i].c)
                                        $(o[i+1]).attr("style",sA[i].c);
                                    }
                                    if(o.length!=0  && o[i+1]!=undefined && o[i+1].disabled){
                                        $(o[i+1]).css("text-decoration","line-through");
                                    }else if(o.length!=0  && o[i+1]!=undefined){
                                        $(o[i+1]).css("text-decoration","");
                                    }
				}
                                if(addNewFlag==1){
                                    o[oL].text = addNewText;
                                    o[oL].value = addNewValue;
                                }
			}else{
                            if(addNewFlag==1){
                                oL=oL-1;
                            }
                            for(i = 0; i<oL; i++){
                                if(sA[i]!=undefined && sA[i].d){
									    o[i].disabled=false;
                                        o[i].text = sA[i].t;
                                        o[i].value = sA[i].v;
                                        o[i].id = sA[i].d;
                                        if(sA[i].e)
                                        o[i].disabled=sA[i].e;
                                        o[i].selected=sA[i].s;
                                        if(sA[i].c)
                                        $(o[i]).attr("style",sA[i].c);
                                }else if(sA[i]!=undefined){
									    o[i].disabled=false; 
                                        o[i].text = sA[i].t;
                                        o[i].value = sA[i].v;
                                        if(sA[i].e)
                                        o[i].disabled=sA[i].e;
                                        o[i].selected=sA[i].s;
                                        if(sA[i].c)
                                        $(o[i]).attr("style",sA[i].c);
                                }
                                if(o[i]!=undefined && o[i].disabled!=undefined && o[i].disabled){
                                        $(o[i]).css("text-decoration","line-through");
                                }else if(o[i]!=undefined){
                                        $(o[i]).css("text-decoration","");
                                }                                
                            }
                            if(addNewFlag==1){
                                o[oL].text = addNewText;
                                o[oL].value = addNewValue;
                            }
			}			
		}
	);
        }catch(e){
	}
	return this;
};

$.fn.selectOptions = function(value, clear)
{
	var v = value;
	var vT = typeof(value);
	// handle arrays
	if(vT == "object" && v.constructor == Array)
	{
		var $this = this;
		$.each(v, function()
			{
      				$this.selectOptions(this, clear);
    			}
		);
	};
	var c = clear || false;
	// has to be a string or regular expression (object in IE, function in Firefox)
	if(vT != "string" && vT != "function" && vT != "object") return this;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return this;
			// get options
			var o = this.options;
			// get number of options
			var oL = o.length;
			for(var i = 0; i<oL; i++)
			{
				if(v.constructor == RegExp)
				{
					if(o[i].value.match(v))
					{
						o[i].selected = true;
					}
					else if(c)
					{
						o[i].selected = false;
					}
				}
				else
				{
					if(o[i].value == v)
					{
						o[i].selected = true;
					}
					else if(c)
					{
						o[i].selected = false;
					}
				}
			}
		}
	);
	return this;
};



$.fn.selectedValues = function()
{
	var v = [];
	this.selectedOptions().each(
		function()
		{
			v[v.length] = this.value;
		}
	);
	return v;
};

$.fn.selectedTexts = function()
{
	var t = [];
	this.selectedOptions().each(
		function()
		{
			t[t.length] = this.text;
		}
	);
	return t;
};

$.fn.selectedOptions = function()
{
	return this.find("option:selected");
};

})(jQuery);
