
// JQuery plugin for implementing a maxLength property for inputs
jQuery.fn.maxLength = function(max){
	this.each(function(){
		var type = this.tagName.toLowerCase();
		var inputType = this.type? this.type.toLowerCase() : null;
		if(type == "input" && inputType == "text" || inputType == "password"){
			this.maxLength = max;
		}
		else if(type == "textarea"){
			this.onkeypress = function(e){
				var ob = e || event;
				var keyCode = ob.keyCode;
				var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;
				return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
			};
			this.onkeyup = function(){
				if(this.value.length > max){
					this.value = this.value.substring(0,max);
				}
			};
		}
	});
};

// this string extension is used to remove &#nnn; from localized strings injected into javascript from php.
// example:  &#191;Question?
// properly prints:  ¿Question?
if(String.prototype.fixCharCodes==null) 
	String.prototype.fixCharCodes = function() { return this.replace(/&#(\d+);/g, function (m, n) { return String.fromCharCode(n); })}

// removes whitespace from the beginning and ending of a string.
if(String.prototype.trim==null)
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, '') ; }

// pad the left side of a string
String.prototype.leftPad = function(padString, length) {
	var str = this;
	while (str.length < length)
		str = padString + str;
	return str;
}

String.prototype.removeSpaces = function() { return this.replace(/ /g, ''); }
//String.prototype.ltrim = function() { return this.replace(/ /g,""); }
//String.prototype.rtrim = function() { return this.replace(/ /g,""); }

//
// attach to the keydown event of a textbox to force numeric-only input.
//
// $(document).ready(function() { $('mytextbox').keydown(forceNumericOnlyInput); });
// -or-
// <input type="text" name="mytextbox" onkeydown="javascript:forceNumericOnlyInput()" /> 
//
function forceNumericOnlyInput()
{
	var ev = window.event;
	var key_code = ev.keyCode;
	var oElement = ev.srcElement;
	
	if (ev.shiftKey || ev.ctrlKey || ev.altKey) 
	{
		if(!(key_code == 9 || key_code == 36 || key_code == 35)) // allow shift + tab, home, end
		{
			//alert(key_code);
			return false;
		}
	}
	else
	{
		if(!((key_code == 8) || 		// backspace
			(key_code == 9) ||			// tab
			(key_code == 46) ||			// delete
			(key_code >= 35 && key_code <= 40) || 		// arrows/home/end
			(key_code >= 48 && key_code <= 57) || 		// numbers
			(key_code >= 96 && key_code <= 105)))		// keypad numbers
		{
			return false;
		} 
	}
} 

function makeDate(sDate, sTime)
{
	m = 0; d = 0; y = 0;
	h = 0; mi = 0;
	
	if(sDate.length > 0)
	{
		tmp = sDate.split("/");
		m = parseInt(tmp[0],10)-1; d = parseInt(tmp[1],10); y = parseInt(tmp[2],10);
	}
	if(sTime.length > 0)
	{
		if(sTime.indexOf(":") == -1)
		{
			h = parseInt(sTime,10);
			mi = 0;
		}
		else
		{
			tmp = sTime.split(":");
			h = parseInt(tmp[0],10);
			mi = parseInt(tmp[1],10);
		}
	}
	
	if(sTime.toUpperCase().indexOf("PM") > 0)
		{ if (h < 12) h += 12; }
	else
		{ if (h == 12) h -= 12; }
	return new Date(y, m, d, h, mi);
}



function doWindowResize()
{
	var h = $(window).height() - ($('#header').outerHeight() + $('#footer').outerHeight());
	$('#sidebar').height(h);
	$('#application').height(h);
}


function doInitializeMenuClasses()
{
	$('ul.menu ul').hide();
	$.each($('ul.menu li.selected'), function(){
		$('#' + this.id + ' ul:first').show();
	});
	$('ul.menu li ul li a').click(function() {
		try
		{
			return this.className != 'disabled';
		}
		catch(ex)
		{
		}
		return true;
	});
	$('ul.menu li a').click(function() {
		try
		{
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			$('#' + parent).children('li').removeClass('selected');
			$(this).parent().addClass('selected');
			
			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('normal');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('fast');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('fast');
				checkElement.slideDown('normal');
				return false;
			}
		}
		catch(ex)
		{
		}
	});
}

