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

function highlightField(f) {
	$(f).css("border", "1px solid #a00000");
	$(f).css("background-color", "#FAD7D7");
	$(f).css("color", "#000");

	$(f).unbind('focus');

	$(f).focus(function() {
		removeHighlightField(f);
		$('#erroresjs').hide();
	});
}

function removeHighlightField(f) {
	$(f).css("border", "1px solid #a0a0a0");
	$(f).css("background-color", "");
	$(f).css("color", "");
}


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

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 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;
}
