
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"").replace(/\s+/g," ");
}

// windows opened with OpenChildWindow have a window.name of <user specified name>__<ParentTimeStamp>
// use window.Name (js is case sensitive) in code and let jUtility manage window.name
window.Name = window.name.split('__')[0];

var ParentDate = new Date();
var ParentTimeStamp = ParentDate.getTime();
var ChildWindow = new Array();

function OpenChildWindow(url, name, width, height, scrollbars, resizable, status, location, toolbar, menubar) {
	CloseChildWindow(name);
	var w = window.screen.availWidth;
	var h = window.screen.availHeight;
	if (width > w) width = w;
	if (height > h) height = h;
	var left = (w - width) / 2;
	var top = (h - height) / 2.5;
	ChildWindow[name] = window.open(url, name+'__'+ParentTimeStamp, 'top='+top+',left='+left+',width='+width+',height='+height+',scrollbars='+scrollbars+',resizable='+resizable+',status='+status+',location='+location+',toolbar='+toolbar+',menubar='+menubar);
	if (ChildWindow[name]) ChildWindow[name].focus();
}

function CloseChildWindow(name) {
	if (name == null) {
		for (name in ChildWindow)
			if (ChildWindow[name])
				if (!ChildWindow[name].closed)
					ChildWindow[name].close();
	} else {
		if (ChildWindow[name])
			if (!ChildWindow[name].closed)
				ChildWindow[name].close();
	}
}

function OpenWindow(url, name, width, height, scrollbars, resizable, status, location, toolbar, menubar) {
	var w = window.screen.availWidth;
	var h = window.screen.availHeight;
	if (width > w) width = w;
	if (height > h) height = h;
	var left = (w - width) / 2;
	var top = (h - height) / 2.5;
	var NewWindow = window.open(url, name+'__'+ParentTimeStamp, 'top='+top+',left='+left+',width='+width+',height='+height+',scrollbars='+scrollbars+',resizable='+resizable+',status='+status+',location='+location+',toolbar='+toolbar+',menubar='+menubar);
	NewWindow.focus();
}

function ResizeWindow(width, height) {
	var w = window.screen.availWidth;
	var h = window.screen.availHeight;
	if (width > w) width = w;
	if (height > h) height = h;
	var left = (w - width) / 2;
	var top = (h - height) / 2.5;
	window.resizeTo(width, height);
	window.moveTo(left, top);
}

function SortSelectOptions(select) {
	var options = new Array(select.options.length);
	for(var i = 0; i < options.length; i++) {
		options[i] = new Option (select.options[i].text, select.options[i].value, select.options[i].defaultSelected, select.options[i].selected);
		options[i].style.color = select.options[i].style.color;
	}
	options.sort(SortSelectOptionsCompare);
	select.options.length = 0;
	for(var i = 0; i < options.length; i++) {
		select.options[i] = options[i];
	}
}

function SortSelectOptionsCompare(option1, option2) {
	return option1.text < option2.text ? -1 : option1.text > option2.text ? 1 : 0;
}

function CountCharacters(textArea, maxLength, counterField, counterFieldText) {	
	if(textArea != null && textArea.value != null && maxLength != null && counterField != null && counterFieldText != null) {
		if (textArea.value.length > maxLength) {
			alert("This field may not exceed " +  maxLength + " characters in length.");
			textArea.value = textArea.value.substring(0, maxLength);
			counterField.innerHTML = (maxLength - textArea.value.length).toString() + counterFieldText;
		} else {
			counterField.innerHTML = (maxLength - textArea.value.length).toString() + counterFieldText;
		}
	}
}

function IsDate(date, showAlert) {
   
	if (!date) return true;
	
    var pattern = /^(\d{1,2})(\-|\/|\.)(\d{1,2})(\-|\/|\.)(\d{4})$/;
	var matchArray = date.match(pattern);

	if (matchArray == null) {
		if (showAlert) alert("Please enter a valid date in the MM/DD/YYYY format.");
		return false;
	}
	
	var month = parseInt(matchArray[1]);
	var day = parseInt(matchArray[3]);
	var year = parseInt(matchArray[5]);

	if (month < 1 || month > 12) {
		if (showAlert) alert("Please enter a month between 1 to 12.");
		return false;
	}
	
	if (day < 1 || day > 31) {
		if (showAlert) alert("Please enter a day between 1 to 31.");
		return false;
	}

	if (year < 1800) {
		if (showAlert) alert("Please enter a year greater than 1800.");
		return false;
	}

	year = year < 1000? (year > 50 && year < 100? year + 1900: year + 2000): year;
	var isLeapYear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	if (((month == 4 || month == 6 || month == 9 || month == 11) && day == 31)
		|| (month == 2 && (day > 29 || (day == 29 && !isLeapYear)))) {
		if (showAlert) alert(month + "/" + year + " does not have " + day + " days.");
		return false;
	}
	
	return true;
}

function IsTime(time) {

	if (!time) return true;
	
	var pattern = /^(\d{1,2}):(\d{2}) *(AM|PM)?$/i;
	var matchArray = time.match(pattern);

	if (matchArray == null)
		return false;
	
	var hour = parseInt(matchArray[1]);
	var minute = parseInt(matchArray[2]);
	var suffix = matchArray[3];
	
	if ((suffix && (hour < 1 || hour > 12)) || (!suffix && (hour < 0 || hour > 23)))
		return false;
	
	if (minute < 0 || minute > 59)
		return false;

	return true;
}

// a FireFox window forgets its original opener if you navigate to another url
// (i.e. after clicking a DataGridView on the DropMenu, window.opener == window)
// use window.Opener to refer to the original window.opener, for FireFox a page specific
// cookie set to expire after one hour stores the name of the original window.opener
window.Opener = window.opener;
if (!document.all && window.Opener) {
	if (window.name != window.Opener.name)
		SetCookie("window.opener.name", window.Opener.name);
	else
		window.Opener = window.open("", GetCookie("window.opener.name"));
}
function SetCookie(name, value) {
	var date = new Date();
	date.setTime(date.getTime() + 3600000);
	document.cookie = name + "=" + escape(value) + "; expires=" + date.toGMTString() + "; path=" + document.location.pathname;
}
function GetCookie(name) {
	var cookies = document.cookie.split("; ");
	for (var i = 0; i < cookies.length; i++) {
		var crumb = cookies[i].split("=");
		if (crumb[0] == name) return unescape(crumb[1]);
	}
	return null;
}

function $(e){return document.getElementById(e);}

function HideShowElement(element){
    if($(element).style.display == 'none'){
        $(element).style.display = '';
    }else{
        $(element).style.display = 'none';
    }
}