String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function jumpToLocation(location) {
	if (location.length == 0) {
		alert("Please select a value from the list.");
		return;
	}
	document.location.href = location;
}

var windowMap = new Array();

function popupWindow(url, targetId, width, height, toolbar, scrollbars, location, statusbar, menubar, resizable) {
	var oldWindow = windowMap[targetId];
	if ((! oldWindow) && (oldWindow != null) && (window != oldWindow)) {
		oldWindow.close();
	}
	if (targetId == null) {
		targetId = 'default_' + (new Date()).getTime();
	}
	if (width == null) {
		width = window.width;
	}
	if (height == null) {
		height = window.height;
	}
	if (toolbar == null) {
		toolbar = 0;
	} 
	if (scrollbars == null) {
		scrollbars = 0;
	}
	if (location == null) {
		location = 0;
	}
	if (statusbar == null) {
		statusbar = 0;
	}
	if (menubar == null) {
		menubar = 0;
	}
	if (resizable == null) {
		resizable = 0;
	}
	windowMap[targetId] = window.open(
		url, 
		targetId, 
		'toolbar=' + toolbar + ',' +
			'scrollbars=' + scrollbars + ',' +
			'location=' + location + ',' +
			'statusbar=' + statusbar + ',' +
			'menubar=' + menubar + ',' +
			'resizable=' + resizable + ',' +
			'width=' + width + ',' +
			'height=' + height
	);
}

function validEmail(emailString) {
	if (emailString.trim().length > 0) {
		if (emailString.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
			// The email is OK.
			return true;
		} else {
			return false;
		}
	}
}

function isNumeric(inputString) {
	var numbers = "0123456789";
	var s = inputString.trim();
	var c = "";
	for (i = 0; i < s.length; i++) {
		c = s.charAt( i );
		if (numbers.indexOf(c) == -1) {
			return false;
		}
	}
	return true;
}

function updateTextAreaCounter(textAreaId, maxLength) {
	var textArea = document.getElementById(textAreaId);
	var counter = document.getElementById(textAreaId + '_counter_textfield');
	
	if (textArea.value.length > maxLength) {
		textArea.value = textArea.value.substring(0, maxLength);
		alert("You cannot enter more than " + maxLength + " characters.");
	} else {
		counter.value = maxLength - textArea.value.length;
	}
}

/**
 * This function is depricated in favor of the one above (updateTextAreaCounter(...)
 */
function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		// if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		
		alert("Due to system requirements, you cannot enter more than " + maxlimit + " characters\n\nWe apologize for any inconvenience.");
	}
	
	// update 'characters left' counter
	countfield.value = maxlimit - field.value.length;
}

//JavaScript to expand the size of the text area as you type
function adjustRows(textArea, minimumRows) {
	while (textArea.scrollHeight < textArea.offsetHeight && textArea.rows > minimumRows) {
		textArea.rows--;
	}
	while (textArea.scrollHeight > textArea.offsetHeight) {
		textArea.rows++;
	}
	while (textArea.rows < minimumRows) {
		textArea.rows++;
	}
	textArea.rows++;
}

function updateDivDisplay(fieldId, displayId) {
	var currentValue = $("#" + fieldId).val();
	$("#" + displayId).load(
		"/learnimation-admin/editor/PreviewContent.do #FCKeditorContent",
		{ 'content' : currentValue }
	);
}

$(document).ready(function() {
	$.fn.center = function () {
		this.css("position","absolute");
		this.css("top", ( $(window).height() - this.height() ) / 2 + $(window).scrollTop() + "px");
		this.css("left", ( $(window).width() - this.width() ) / 2 + $(window).scrollLeft() + "px");
		return this;
	}
	
	$.fn.encHTML = function() { 
		return this.each(function() { 
			var me = $(this); 
			var html = me.html(); 
			me.html(html.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')); 
		}); 
	}; 
	 
	$.fn.decHTML = function() { 
		return this.each(function() { 
			var me = $(this); 
			var html = me.html(); 
			me.html(html.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>')); 
		}); 
	};
	
	$.fn.isChildOf = function(b){
	    return (this.parents(b).length > 0);
	};
});


