(function ($) {
    $.fn.selectChain = function (options) {
        var defaults = {
            key: "catid",
            value: "txt"
        };

        var settings = $.extend({}, defaults, options);

        if (!(settings.target instanceof $)) settings.target = $(settings.target);

        return this.each(function () {
            var $$ = $(this);

            $$.change(function () {
                var data = null;
                if (typeof settings.data == 'string') {
                    data = settings.data + '&' + this.name + '=' + $$.val();
                } else if (typeof settings.data == 'object') {
                    data = settings.data;
                    data[this.name] = $$.val();
                }



                settings.target.empty();

                $.ajax({
                    url: settings.url,
                    data: data,
                    type: (settings.type || 'get'),
                    dataType: 'json',
                    success: function (json) {
						//console.log(json);
                        var options = [], i = 0, o = null;


                        for (i = 0; i < json.length; i++) {
                            // required to get around IE bug (http://support.microsoft.com/?scid=kb%3Ben-us%3B276228)
                            o = document.createElement("OPTION");
                            //o.value = typeof j[i] == 'object' ? j[i][settings.key] : j[i];
                            //o.text = typeof j[i] == 'object' ? j[i][settings.value] : j[i];
							o.value = typeof json[i] == 'object' ? json[i][settings.key] : j[i];
                            //o.text = typeof json[i] == 'object' ? json[i][settings.value] : j[i];
                            o.text = StrFromChar(typeof json[i] == 'object' ? json[i][settings.value] : j[i]);

                            settings.target.get(0).options[i] = o;
                        }

						// hand control back to browser for a moment
							setTimeout(function () {
						    settings.target
			                                .find('option:first')
			                                .attr('selected', 'selected')
			                                .parent('select')
			                                .trigger('change');
						}, 0);
                    },
                    error: function (xhr, desc, er) {
                        // add whatever debug you want here.
						//alert("an error occurred");
                    }
                });
            });
        });
    };



})(jQuery);

function StrFromChar(textstring)
{
	var regexp = /(\&{1}\#{1}\d+\;{1})/gi; //FIND ISO LATIN CODE
	var regexpnumbers = /(\d+)/gi; //FIND DIGITS

	var match_arr = textstring.match(regexp);
	if(match_arr)
	{
		for(i=0;i<match_arr.length;i++)
		{
			var foundiso = match_arr[i];
			var foundisonumber = foundiso.replace("&#","").replace(";","")
			textstring = textstring.replace(foundiso, String.fromCharCode(foundisonumber));
		}
	}
	return textstring;
}


//RL.mxdecode(json[i][settings.value])
/*
RL = new function(){
	this.strFromChar = function(){
		self = arguments[0];
		var regexp = /\&{1}\#{1}\d+\;{1}/gi; //FIND ISO LATIN CODE
		//result = self.replace(regexp, ("$1").replace(regexpnumbers, "$1") );
		result = self.replace(regexpnumbers,"$1");
		//result = self.replace(regexp,"$1");
		//result = result.replace("&#233;",String.fromCharCode(64));
		return result;
	};
};
*/
