function trim(s) {
	if (s == undefined) return '';
	else if (s == '') return '';
	return s.toString().replace(/^(\s|\&nbsp;)*|(\s|\&nbsp;)*$/g,"");
}

function isValidEMail(v) {
	var reg = /^[a-zA-Z0-9.][a-zA-Z0-9-_\.]+@[a-zA-Z0-9-].+\.[a-zA-Z]{2,5}$/;
	return reg.test(v);
}

function generalDialog(title , text , isError , isSuccess) {
	$("#generaldialog").attr("title", title);

	if (isError) {
		text = '<img src="'+JS_SITE_ROOT+'img/error_icon.gif" width="40" height="40" class="generaldialogimagen" />' + text + '<div class="clear"></div>';
	}

	if (isSuccess) {
		text = '<img src="'+JS_SITE_ROOT+'img/success_icon.gif" width="40" height="40" class="generaldialogimagen" />' + text + '<div class="clear"></div>';
	}

	$("#generaldialogtext").html(text);
	$("#generaldialog").dialog({
		width: 500,
		minHeight: 140,
		minWidth: 500,
		modal: true,

		buttons: {
			Close: function() {
				$(this).dialog('close');
			}
		}
	});
}


function isUndefinedOrNothing(v) {
	v = trim(v);
	if (v == '') return true;
	if (v == undefined) return true;
	return false;
}

function urlencode( str ) {

	if (str == undefined) return "";
	if (str == "") return "";

    var histogram = {}, tmp_arr = [];
    var ret = str.toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';

    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }

    return ret;
}


