
function trim(s) {
	return s.replace(/^\s+|\s+$/g,"");
}

function ltrim(s) {
	return s.replace(/^\s+/,"");
}

function rtrim(s) {
	return s.replace(/\s+$/,"");
}


function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function empty(v) {
	if (typeof(v) == "undefined") return true;
	if (v == null) return true;
	if (v == "") return true;
	if (v == "0") return true;
	if (v == false) return true;
	if (isArray(v) && v.length == 0) return true;
	return false;
}

function isSet(v) {
	return (typeof(v) != "undefined");
}

function dump(arr, level) {
	// PHP print_r
	var dumped_text = "";
	if (!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for (var j=0; j<level+1; j++) {
		level_padding += "    ";
	}

	// Array/Hashes/Objects
	if (typeof(arr) == 'object') { 
		for (var item in arr) {
			var value = arr[item];
	 
			// if array
			if(typeof(value) == 'object') { 
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else {
		// Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
} 


function popup(loc, winName, otherFeatures, winWidth, winHeight, centered){
	// A run o' the mill customizable popup window function
	if (otherFeatures == null) otherFeatures = '';
	var features = '';
	if (winWidth != null && winHeight != null) {
		features += 'width=' + winWidth + ', height=' + winHeight + ',';
		if (centered) {
			features += 'left=' + (screen.width - winWidth)/2 + ',';
			features += 'top=' + (screen.height - winHeight)/2 + ',';
		}
	}
	features += otherFeatures;
	window.open(loc, winName, features);
	
}


function flash(el, opts) {
	// shows message for short period of time (message duration in seconds)
	/*
	var opts = extend({
		effect: "blind",
		duration: 1,
		msgDuration: 4
	}, opts);
	
	msgDuration: 0 leaves on indefinitely
	
	*/
	var opts = extend({
		speed: 1000,
		msgDuration: 3000
	}, opts);
	
	$("#" + el).fadeIn(opts.speed);
	if (opts.msgDuration != 0) {
		setTimeout(function() { $("#" + el).fadeOut(opts.speed); }, opts.msgDuration);
	}
}


function getPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	var elPos = new Array();
	elPos['left'] = curleft;
	elPos['top'] = curtop;
	return elPos;
}


function extend(oSelf, oOther) {
	// Like prototype.js Object.extend({default: val, ...}, oName)
	if (oSelf == null) {
		oSelf = {};
	}
	for (var i = 1; i < arguments.length; i++) {
		var o = arguments[i];
		if (typeof(o) != 'undefined' && o != null) {
			for (var j in o) {
				oSelf[j] = o[j];
			}
		}
	}
	return oSelf;
}


function quoteJSON(str) {
	// escape double-quotes, braces
	str = String(str);
	str = str.replace(/\"/g, "\\\"");
	str = str.replace(/\}/g, "\\\}");
	str = str.replace(/\{/g, "\\\{");
	return '"' + str + '"';
}

function ajaxSuccess(status) {
	return (status >= 200 && status < 300);
}

function show(id, top, left) {
	var el = document.getElementById(id);
	if (el)
		el.style.display = 'block';
}

function hide(id) {
	var el = document.getElementById(id);
	if (el)
		el.style.display = 'none';
}

var PRELOADED_IMAGES = new Array();
function preload() {
	for (i = 0; i < preload.arguments.length; i++) {
		PRELOADED_IMAGES[i] = new Image();
		PRELOADED_IMAGES[i].src = preload.arguments[i];
	}
}

