var trillsam = {
    VERSION : "1.0",
    AUTHOR: "Alexander Prinz",
    DEBUG : false,
    debug : function() {
        if (this.DEBUG) {
            console.log(arguments);
        }
    }
};

trillsam.Util = {

    initFormControls : function(formName){
        $("#" + formName + " input").each(function(){
            $(this).bind("mouseover", function(){
                $(this).attr("class", "ui-state-default ui-corner-all ui-state-hover");
            });
            $(this).bind("mouseout", function(){
                $(this).attr("class", "ui-state-default ui-corner-all");
            });
        });
    },

    trim : function(str, chars) {
	    return ltrim(rtrim(str, chars), chars);
    },

    trim : function(str, chars) {
	    chars = chars || "\\s";
	    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    },

    rtrim : function(str, chars) {
	    chars = chars || "\\s";
	    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
    },

    autoSelect : function(element, defaultOnly){
        element.bind('focus', function(){
            if(!defaultOnly || (defaultOnly && this.value == this.defaultValue))
                this.select();
        });
        element.bind('click', function(){
            if(!defaultOnly || (defaultOnly && this.value == this.defaultValue))
                this.select();
        });
    },

    resetForm : function(formId){
        var formElements = ['input:text',
                            'input:radio',
                            'input:checkbox',
                            'input:hidden',
                            'select',
                            'textarea'];
        for(var i = 0; i < formElements.length; i++){
            $("#" + formId + " " + formElements[i]).each(function(){
                if($(this).attr('type') == 'radio'){
                    $(this).attr('checked', '');
                } else {
                    $(this).val("");
                }
            });
        }
    },
    encodeUTF8 : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
    decodeUTF8 : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	},
    showMessage : function(title, message){
        $('#dialog').attr('title', title);
        $('#dialog').dialog('option', 'title', title);
        $('#dialog').text(message);
        $("#dialog").dialog({
		    bgiframe: true,
		    modal: true,
		    buttons: {
			    Ok: function() {
                    $(this).dialog('close');
				    $(this).dialog('destroy');
			    }
		    }
	    });
        $("#dialog").dialog('open');
    },
    stripLineBreaks : function(inOutStr){
        if(inOutStr != null)
            return inOutStr.replace(/[\r\n]+/g, "");
        else
            return '';
    }
}
