
function isValidDate(ctrlDate)
{
	var d = new Date();
	var nCurrent_Year = d.getFullYear();
	if (ctrlDate.value == '') return true;
	d.setTime(Date.parse(ctrlDate.value));
	if (isNaN(d) || d.getFullYear() < 1900 || d.getFullYear() > (nCurrent_Year+5))
	{
		alert("Date format should be 'MM/DD/YYYY'.");
		ctrlDate.value = '';
		ctrlDate.focus();
		return false;
	}
	else
	{
		ctrlDate.value = formatShortDate(d);
		return true;
	}
}

function formatShortDate(d)
{
  var cDate  = '';
  var nDays  = d.getDate();
  var nMonth = d.getMonth()+1;
  var nYear  = d.getFullYear();
  if (nYear < 1920) nYear += 100;

  cDate += ((nMonth < 10) ? '0' : '') + nMonth + '/' + ((nDays < 10) ? '0' : '') + nDays + '/' + nYear;

  return cDate;
}

<!-- Original:  Sandeep Tamhankar (stamhankar@hotmail.com) -->
<!-- Web Site:  http://207.20.242.93 -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

function isValidTime(ctrl)
{
	var timeStr = ctrl.value;

	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null)
	{
		alert("Time is not in a valid format.");
		return false;
	}

	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second == "") { second = null; }
	if (ampm == "") { ampm = null }

	if (hour < 0  || hour > 23)
	{
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}
	if (hour <= 12 && ampm == null)
	{
		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time"))
		{
			alert("You must specify AM or PM.");
			return false;
	   	}
	}

	if  (hour > 12 && ampm != null)
	{
		alert("You can't specify AM or PM for military time.");
		return false;
	}

	if (minute<0 || minute > 59)
	{
		alert ("Minute must be between 0 and 59.");
		return false;
	}

	if (second != null && (second < 0 || second > 59))
	{
		alert ("Second must be between 0 and 59.");
		return false;
	}

	return false;
}

function loadDialog(cUrl, cTitle, nWidth, nHeight, cOnLoad)
{
	// turn on transparent background to block all links, buttons and forms
	var bkgrnd 	= document.getElementById("popup-background");
	var popup 	= document.getElementById("popup");
	
	bkgrnd.style.display = "block";
	bkgrnd.style.height = document.documentElement.scrollHeight + "px";
	
	popup.style.width 	= "" + nWidth 	+ "px";
	popup.style.height	= "" + nHeight 	+ "px";
	
	document.getElementById("popup-title").innerHTML = cTitle;
	
	advAJAX.get({
	    url: cUrl,
	    onSuccess : function(obj) 	{
										replaceHtml("popup-content", obj.responseText );
										
										popup.style.display = "block";
										
										centerWindow(popup);
										
										if (typeof(cOnLoad) != "undefined" && cOnLoad.length > 0)
											eval(cOnLoad);
									},
	    onError : function(obj) { alert("Error loading dialog: " + obj.status); }
	});
}

function saveDialog(frm)
{
	closeDialog();
	
	advAJAX.submit(	frm, 
					{
						onSuccess : function(obj) { 
													if (obj.responseText != "")
														alert(obj.responseText);
													else
														document.location.href = document.location.href;
												  },
						onError : function(obj) { alert("Error: " + obj.status); 
					}
	});
}

function closeDialog()
{
	// turn off transparent background
	document.getElementById("popup-background").style.display = "none";

	document.getElementById("popup").style.display = "none";
}

function centerWindow(id)
{
	var dlg	     = (typeof(id) == "string" ? document.getElementById(id) : id);
	var iebody   = (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	var dsocleft = document.all? iebody.scrollLeft : pageXOffset;
	var dsoctop  = document.all? iebody.scrollTop : pageYOffset;
	var viewport = getViewportDimensions();

	dlg.style.top  = "" + Math.max(15, dsoctop  + ((viewport.height - dlg.clientHeight)/2)) + "px";
	dlg.style.left = "" + Math.max(15, dsocleft + ((viewport.width  - dlg.clientWidth )/2)) + "px";

	
	//dlg.style.top  = "" + (dsoctop  + ((document.body.clientHeight - dlg.clientHeight)/2)) + "px";
	//dlg.style.left = "" + (dsocleft + ((document.body.clientWidth  - dlg.clientWidth )/2)) + "px";
}

/* http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/ */

function getViewportDimensions()
{
	var viewportwidth	= 0;
	var viewportheight	= 0;
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	
	if (typeof window.innerWidth != 'undefined')
	{
		viewportwidth 	= window.innerWidth,
		viewportheight 	= window.innerHeight
	}
	else if (typeof document.documentElement != 'undefined'	&& typeof document.documentElement.clientWidth !=	'undefined' && document.documentElement.clientWidth != 0)
	{
		// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		viewportwidth 	= document.documentElement.clientWidth,
		viewportheight 	= document.documentElement.clientHeight
	}
	else
	{
		// older versions of IE
		viewportwidth 	= document.getElementsByTagName('body')[0].clientWidth,
		viewportheight 	= document.getElementsByTagName('body')[0].clientHeight
	}
	
	return { "width" : viewportwidth, "height" : viewportheight };
}


/* 	
*	This is much faster than using (el.innerHTML = value) when there are many
*	existing descendants, because in some browsers, innerHTML spends much longer
*	removing existing elements than it does creating new ones. 
*/
function replaceHtml(el, html) 
{
	var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
	/*@cc_on // Pure innerHTML is slightly faster in IE
	oldEl.innerHTML = html;
	return oldEl;
	@*/
	var newEl = oldEl.cloneNode(false);
	newEl.innerHTML = html;
	oldEl.parentNode.replaceChild(newEl, oldEl);
	/* Since we just removed the old element from the DOM, return a reference
	to the new element, which can be used to restore variable references. */
	return newEl;
};

function getObjectProperties(obj)
{
	var result = "";
	for(var i in obj)
	result += "" + i + " = " + obj[i] + "\n";
	return (result);
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Chris Heilmann :: http://www.alistapart.com/articles/tableruler */
var classHolder = "";

function tableRuler(id) 
{
	var tbl = document.getElementById(id);
	var trs = tbl.getElementsByTagName('tr');
	var lSawHeader = false;
				
	for (var i = 0; i < trs.length; i++)
	{
		if(trs[i].className == "header")
		{
			lSawHeader = true;
		}
		else if (lSawHeader)
		{
			trs[i].onmouseover 	= function() { classHolder = this.className; this.style.cursor="pointer"; this.className = "ruler"; return false; }
			trs[i].onmouseout  	= function() { this.className = classHolder; this.cursor="normal"; return false; }
		}
	}
}

// http://www.webdeveloper.com/forum/archive/index.php/t-92521.html

function setSelectionRange(input, selectionStart, selectionEnd) 
{
	if (input.createTextRange) 
	{
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
	else if (input.setSelectionRange) 
	{
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
	}
}

function setFocus(input)
{
	setSelectionRange(input, 1, 1);
}

function editPage(page_name)
{
	window.open("EditPage.php?page_name="+page_name, "_blank", "width=600,height=600,scrollbars=yes,resizable=yes,status=yes");
}

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};