///////////////////////////////////////////////////////////////
// File Name: 	janus.js
///////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
// Shared javascript functions for janus
///////////////////////////////////////////////////////////////

/// <reference path="jquery-1.3.2.min-vsdoc.js"/>

// Show calendar for selecting a date in a form
function showPopupCalendar(dateField) {
	setDateField(dateField);
	var height = 230;
	var width = 210;
	var wintop = (screen.availHeight - height) / 2;
	var winleft = (screen.availWidth - width) / 2;
	var options = "dependent=yes,width=" + width + ",height=" + height + ",left=" + winleft + ",top=" + wintop + ",titlebar=yes";
	top.newWin = window.open("calendar.html","cal",options);
	top.newWin.focus();
}

// Give focus to the first field in the first form on the page.
function placeFocus() {
    var mainForm = getEl("aspnetForm");
    if (mainForm){
	    for (var i=0; i < mainForm.elements.length; i++) {
		    if (mainForm.elements[i].type == "text" || mainForm.elements[i].type == "textarea" || mainForm.elements[i].type == "password") {
		        try {
    			    mainForm.elements[i].focus();
		        } catch(e) {
		        }
			    break;
		    }
	    }
	}
}

// Get element by id.  Handle controls using master pages.
function getEl(id) {
    if (document.getElementById(id)) {
        return document.getElementById(id);
    } else if (document.getElementById("ctl00_main_" + id)) {
        return document.getElementById("ctl00_main_" + id);
    } else if (document.getElementById("ctl00_ctl00_main_main_" + id)) {
        return document.getElementById("ctl00_ctl00_main_main_" + id);
    } else if (document.getElementById("ctl00_ctl00_main_top_" + id)) {
        return document.getElementById("ctl00_ctl00_main_top_" + id);
    } else if (document.getElementById("_ctl0_main_" + id)) {
        return document.getElementById("_ctl0_main_" + id);
    } else if (document.getElementById("_ctl0_ctl0_main_main_" + id)) {
        return document.getElementById("_ctl0_ctl0_main_main_" + id);
    }
}

function getElByName(name) {
    return document.getElementsByName(name)[0];
}

//Get an element by control id
//Matches elements with an id that ends in the given id, to match controls within controls
function getControl(tagName,id,parent) {
    if (!parent) parent = document;
    var els = parent.getElementsByTagName(tagName);
    for (var i=0; i < els.length; i++) {
        if (els[i].id.search(id + "$") > -1) {
            return els[i];
        }
    }
    return null;
}

function selectValue(selectObj,value) {
	for (var i=0; i < selectObj.options.length; i++) {
		if (selectObj.options[i].value==value) {
			selectObj.selectedIndex = i;
			break;
		}
	}
}

function arrayContains(arr,val) {
	for (var i = 0; i < arr.length; i ++) {
		if (arr[i] == val) {
			return true;
			break;
		}
	}
	return false;
}


///////////////////////////////////////////////////////////////
// Keywords
///////////////////////////////////////////////////////////////

function validateKeyword(myKeyword){
	//ensures that the user entered a valid keyword in the keyword text box
	var myExp = /^[\-\"\'\)\(a-zA-Z0-9\s]+$/
	return myExp.test(myKeyword);
}

function addKeyword(formObj,baseName){
    if (baseName==null) baseName = "";
	//moves a keyword from the keyword text box to the keyword list box
	if(validateKeyword(getEl(baseName+"txtKeyword").value)){
		var keywordValue = getEl(baseName+"txtKeyword").value;
		var keywordName = getEl(baseName+"txtKeyword").value;
		var keywordMenu = getControl("SELECT",baseName+"lbKeywordMenu");
		var keywordCount = keywordMenu.options.length;
		already_exists = false;
		for(i=0;i<keywordCount;i++){
			if(keywordValue == keywordMenu.options[i].value){
				already_exists = true;
				alert("The keyword '" + keywordName + "' is already in the list.")
			}
		}
		if(!already_exists){
			keywordMenu.options[keywordCount] = new Option(keywordName,keywordValue,1,1);
			getEl(baseName+"txtKeyword").value = ""
			saveKeywords(formObj,baseName);
		}
	}else{
	    alert("Invalid keyword.")
	}
}

function removeKeyword(formObj,baseName){
    if (baseName==null) baseName = "";
	var keywordMenu = getControl("SELECT",baseName+"lbKeywordMenu");
	//removes the selected keyword from the keyword list box
	if(keywordMenu.selectedIndex >= 0){
		keywordMenu.remove(keywordMenu.selectedIndex)
		saveKeywords(formObj,baseName);
	}
}

function saveKeywords(formObj,baseName) {

    if (baseName == null) baseName = '';
    
	var keywordMenu = getControl("SELECT",baseName+"lbKeywordMenu");
	var selectedKeywords = getControl("INPUT",baseName+"selectedKeywords");
	//save the selected keywords to a hidden field so its available upon submit
	itemCount = keywordMenu.options.length;
	myList = "";
	for(i=0;i<itemCount;i++){
		myList = myList + keywordMenu.options[i].value;
		if(i != (itemCount-1)){
			myList = myList  + "|";
		}
	}
	selectedKeywords.value = myList;
}


/////////////////////////////////////
// popupWindows
/////////////////////////////////////
function popupWindow2(windowName,options) {
	eval("var " + windowName + " = window.open('','" + windowName + "','" + options + "')");
	eval(windowName + ".focus()");
	return eval(windowName);
}
function popupDialog(windowName,width,height,options) {
	if (options == null) options = "status=no,toolbar=no,menubar=no,location=no";
	height = Math.min(screen.availHeight - 200, height);
	width = Math.min(screen.availWidth - 100, width);
	var top = (screen.availHeight - height) / 2 - 50;
	var left = (screen.availWidth - width) / 2;
	options = "height=" + height + ",width=" + width + ",top=" + top + ",left=" + left + "," + options;
	return popupWindow2(windowName,options);

}

function createPopupDiv(id,className,contents) {
	var el = document.createElement("DIV");
	el.id = id;
	el.className = className;
	el.innerHTML = contents;
	var closeLink = document.createElement("DIV");
	closeLink.style.marginTop = "15px";
	closeLink.style.cursor = "pointer";
	closeLink.style.textAlign = "right";
	closeLink.style.textDecoration = "underline";
	closeLink.innerHTML = "Close Window";
	el.appendChild(closeLink);
	document.body.appendChild(el);
	el.onclick = function () { showHide(this,"hide"); }
	return el;
}
function showPopupDiv(id) {
	showHide(getEl(id),"show");
}

//Cross-browser function to add an event listener
function xAddEvent(obj, name, handler) {
	if (obj.attachEvent) {
		obj.attachEvent("on" + name, handler);
	} else {
		obj.addEventListener(name, handler, false);
	}
}
function xGetEventSrcElement(e) {
	if (window.event) {
		return window.event.srcElement;
	} else if (e && e.currentTarget) {
		return e.currentTarget;
	}
}



///////////////////////////////////////////////////////////////
// Form Validation functions for use with CustomValidator control
///////////////////////////////////////////////////////////////

// Date
// To use, set the following parameters in the CustomValidator:
// 	ControlToValidate
function ValidateDate(source, arguments){
	//checks to see if the date is valid
	success = true;

	if (!isDate(arguments.Value)){
		getEl(source.getAttribute("ControlToValidate")).select();
		success = false;
	}

	arguments.IsValid = success;
}

//checks to be sure that the expiration date is after the production date
function ValidateExpirationDateOrder(source, value) {
	success = true;
	var expDate=getControl("INPUT","txtExpirationDate");
	if (isDate(expDate.value)){
		//expiration date is a valid date
		var prodDate=getControl("INPUT","txtProductionDate");
		if (isDate(prodDate.value)){
			//production date is a valid date
			//check to see if production date is later then expiration date
			if (Date.parse(prodDate.value) > Date.parse(expDate.value)) {
				success = false;
			}
		}
	}
	value.IsValid = success;
}

//checks to be sure that the expiration date is not more than 1 year in the future
function ValidateExpirationDateRange(source, value) {
	success = true;
	var expDate=getControl("INPUT","txtExpirationDate");
	if (isDate(expDate.value)){
		//expiration date is a valid date
		var maxDate=getControl("INPUT","maxExpirationDate");
		if (isDate(maxDate.value)){
			//check to see if expiration date is later then max date
			if (Date.parse(expDate.value) > Date.parse(maxDate.value)) {
				success = false;
			}
		}
	}
	value.IsValid = success;
}

// Validate length
// To use, set the following parameters in the CustomValidator:
// 	MaxLength
function validateLength(source, arguments) {
	//checks to be sure the field does not exceed a particular length
	success = true;

	maxLength = source.getAttribute("MaxLength");

	if (arguments.Value.length > maxLength) {
		success = false;
	}

	arguments.IsValid = success;
}


// Validate selected items list
// checks to be sure that select box contains at least one option
// To use, set the following parameters in the CustomValidator:
// 	SelectedList - the id of the select list to check
function ValidateSelectedList(source, arguments) {
	success = true;
	// get select object from validator's ControlToValidate parameter
	selectObj = getEl(source.getAttribute("SelectedList"))
	if (selectObj.options.length <= 0) {
		success = false;
	}
	arguments.IsValid = success;
}

// Validate number of items checked for checkboxgroup
// To use, set the following parameters in the CustomValidator:
// 	CheckBoxList - the name of the checkboxlist which will prefix each checkbox id
//	minChecked - the minimum number of options to be checked
//	maxChecked - the maximum number of options to be checked
function ValidateNumChecked(source, arguments) {
	success = true;

	// get attributes

	checkBoxList = source.getAttribute("CheckBoxList")
	minChecked = source.getAttribute("MinChecked")
	maxChecked = source.getAttribute("MaxChecked")

    // get the checkbox table that holds the list
    checkTable = getEl(checkBoxList);

    if (checkTable != null)
    {
	    // get all input elements
	    allInputElements = document.getElementsByTagName("INPUT")
	    numChecked = 0;

	    // loop through all input elements and count checked

	    for (i = 0; i < allInputElements.length; i++) {
		    if (allInputElements[i].id.indexOf(checkTable.id) == 0) {
			    if (allInputElements[i].checked) {
				    numChecked ++;
			    }
		    }
	    }

	    // Make sure the number checked is valid
	    if (numChecked < minChecked || numChecked > maxChecked) {

		    success = false;
	    }
	}
	arguments.IsValid = success;
}


// Validate extension for file name
// Checks to be sure that the file extension is in the given list.
// To use, set the following parameters in the CustomValidator:
// 	extensions - a comma separated list of valid extensions
function ValidateExtension(source, arguments) {
	success = true;

	var extensions = source.getAttribute("extensions").toLowerCase().split(",");
	var extension = "";

	if (arguments.Value != "") {
		if (arguments.Value.lastIndexOf(".") == -1) {
			success = false;
		} else {
			extension = arguments.Value.substring(arguments.Value.lastIndexOf("."),arguments.Value.length).toLowerCase();

			var foundExt = false;
			for (var i = 0; i < extensions.length; i ++) {
				if (extension == extensions[i]) {
					foundExt = true;
					break;
				}
			}
			if (!foundExt) {
				success = false;
			}
		}
	}

	arguments.IsValid = success;
}

// Validate prefix
// Checks to be sure that the value begins with particular characters.
// To use, set the following parameters in the CustomValidator:
// 	prefixes - a comma separated list of valid prefixes
function ValidatePrefix(source, arguments) {
	success = true;

	var prefixes = source.getAttribute("prefixes").toLowerCase().split(",");

	if (arguments.Value != "") {
		var foundPrefix = false;
		for (var i = 0; i < prefixes.length; i ++) {
			if (arguments.Value.indexOf(prefixes[i]) == 0) {
				foundPrefix = true;
				break;
			}
		}
		if (!foundPrefix) {
			success = false;
		}
	}

	arguments.IsValid = success;
}


// Validate a title
// Make sure a title is provided for a file or url
// To use, set the following parameters in the CustomValidator:
// 	TitleControl
// 	FileControl (optional)
// 	URLControl (optional)
function ValidateTitle(source, value) {
	success = true;

	// Note that ControlToValidate parameter cannot be used because it assumes that a blank field is okay.

	var title = getEl(source.getAttribute("TitleControl")).value;
	var newFile = "";
	var oldFile = "";
	var url = "";
	var fileOption = false;
	var urlOption = false;

	// Get filename and url
	if 	(source.getAttribute("FileControl") != null) {
		fileOption = true;
		newFile = getEl(source.getAttribute("FileControl")).value;
	}
	if 	(source.getAttribute("FileLabel") != null) {
		oldFile = getEl(source.getAttribute("FileLabel")).innerHTML;
	}
	if 	(source.getAttribute("URLControl") != null) {
		urlOption = true
		url = getEl(source.getAttribute("URLControl")).value;
		if (url == "http://") url = "";
	}

	// If file or url provided, require title
	if ((newFile != "" || oldFile != "" || url != "") && title == "") {
		success = false;
	}
	// If title provided, require file or url.  Set error message accordingly
	if (title != "" && newFile == "" && oldFile == "" && url == "") {
		if (fileOption && !urlOption) {
			source.setAttribute("errormessage","Please upload a file to go with this title.");
		}
		if (!fileOption && urlOption) {
			source.setAttribute("errormessage","Please provide a url to go with this title.");
		}
		if (fileOption && urlOption) {
			source.setAttribute("errormessage","Please upload a file or provide a url to go with this title.");
		}
		source.innerHTML = source.getAttribute("errormessage")
		success = false;
	}

	value.IsValid = success;
}

function ValidateHiddenField(source, value) {
	//Make sure a value is set for a hidden field
	success = true;

	hiddenFieldID = source.getAttribute("HiddenFieldID");

	if (getEl(hiddenFieldID).value == "") {
		success = false;
	}
	value.IsValid = success;
}



///////////////////////////////////////////////////////////////
// Utility functions
///////////////////////////////////////////////////////////////

function isDate(dtStr){
	//checks to see if the string passed in is a valid date
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (month<1 || month>12){
		return false
	}
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
	return true
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function formatAsMoney(mnt) {
	mnt -= 0;
	mnt = (Math.round(mnt*100))/100;
	return (mnt == Math.floor(mnt)) ? mnt + '.00'
			  : ( (mnt*10 == Math.floor(mnt*10)) ?
					   mnt + '0' : mnt);
}

function round(n,d){
    return Math.round(n * Math.pow(10,d))/Math.pow(10,d);
}

function stripCharsInBag(s, bag){
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

// This function replaces extended unicode characters that sometimes do not print correctly when exporting to Excel
// These include the em-dash, en-dash, left quote, right quote, etc
function replacedExtendedChars(s)
{

	var retString;
	var re;

	retString = s;

	// em-dash and en-dash replaced with a hyphen
	re = new RegExp("[\u2013]|[\u2014]", "g");
	retString = retString.replace(re,"-");

	// left and right quote replaced with '
	re = new RegExp("[\u2018]|[\u2019]", "g");
	retString = retString.replace(re,"'");

	// left and right double quote with "
	re = new RegExp("[\u201C]|[\u201D]", "g");
	retString = retString.replace(re, "\"");

	return retString;
}

// Moves the selected options in first select box to second select box
function moveOptions(selectObj1,selectObj2) {
	for(var i=0; i < selectObj1.options.length; i++) {
		if (selectObj1.options[i].selected) {
			optionExists = false;
			for(var n=0; n < selectObj2.options.length; n++) {
				if (selectObj1.options[i].value == selectObj2.options[n].value) {
					optionExists = true;
					break;
				}
			}
			if (!optionExists) {
				selectObj2.options[selectObj2.options.length] = new Option(selectObj1.options[i].text,selectObj1.options[i].value,1,1);
			}
			selectObj1.remove(i);
			i--;
		}
	}
}

// Check or uncheck all checkboxes under a particular parent node and/or with an id of a particular pattern
function checkAll(checked, parentNodeId, pattern) {
	var parentNode = document;
	if (parentNodeId != null) parentNode = getEl(parentNodeId);
	var checkboxes = parentNode.getElementsByTagName("INPUT");
	for (var i = 0; i < checkboxes.length; i ++) {
		if (checkboxes[i].type == "checkbox" && (pattern == null || (checkboxes[i].id && checkboxes[i].id.search(pattern) > -1))) {
			if (!checkboxes[i].getAttribute("readOnly") && !checkboxes[i].getAttribute("disabled")) {
				checkboxes[i].checked = checked;
			}
		}
	}
}

function checkMax(parentNodeId, maxChecked) {
    var parentNode = document;
    var count = 0;
    
	if (parentNodeId != null) parentNode = getEl(parentNodeId);
	var checkboxes = parentNode.getElementsByTagName("INPUT");
	for (var i = 0; i < checkboxes.length; i ++) {
		if (checkboxes[i].type == "checkbox") {
			if (checkboxes[i].checked) count++;
		}
	}
	
	if (count > maxChecked)
	{
	    if (maxChecked==1)
	       alert("You may only select one section or topic");
	    else
	       alert("You may only select " + maxChecked.toString() + " sections or topics");
	   
	   return false;
	}
	
	return true;
}
///////////////////////////////////////////////////////////////
// Functions to work with select objects
///////////////////////////////////////////////////////////////

// Get a comma-separated list of values from a select box
function getSelectedList(selectObj) {
	var myList = "";
	for(i=0; i < selectObj.options.length; i++) {
		if (i > 0) myList += ",";
		myList += selectObj.options[i].value;
	}
	return myList;
}

// Remove selected options
function removeSelected(selectObjID) {
	var list = getEl(selectObjID);
	for (var i = list.options.length - 1; i > -1 ; i --) {
		if (list.options[i].selected) {
			list.options[i] = null;
		}
	}
}

// Save option values in a hidden field.
// Used to save a list of options whether or not they are selected
function saveOptionValues(selectObjID,hiddenObjID) {
	var list = getEl(selectObjID);
	var hdn = getEl(hiddenObjID);
	hdn.value = "";
	for (var i = 0; i < list.options.length; i ++) {
		if (hdn.value.length != "") hdn.value += ",";
		hdn.value += list.options[i].value;
	}
}

// Function to sort select box options alphabetically
function sortOptions(selectObj) {
	var optionArray = new Array;
	//put options in array
	for (var i = 0; i < selectObj.options.length; i ++) {
		optionArray[i] = new Array(selectObj.options[i].value,selectObj.options[i].text);
	}
	//sort using custom function
	optionArray.sort(sortOptions_alpha);
	//set values and text of original options
	for (var i = 0; i < selectObj.options.length; i ++) {
		selectObj.options[i].value = optionArray[i][0];
		selectObj.options[i].text = optionArray[i][1];
	}
}
//custom sort function
function sortOptions_alpha(opt1, opt2) {
	if (opt1[1] > opt2[1]) {
		return 1;
	} else {
		return -1;
	}
}




function clearSpanTag(span) {
	var spanTag;

	spanTag = getEl(span);
	if (spanTag==null) return;
	spanTag.innerHTML = '';
}

///////////////////////////////////////////////////////////////
// Functions to swap sibling objects
///////////////////////////////////////////////////////////////
function moveObjUp(o) {
	if (o.previousSibling != null) {
		var p = o.parentNode;
		var s = o.previousSibling;
		var c = p.removeChild(o);
		p.insertBefore(c,s);
	}
}
function moveObjDown(o) {
	if (o.nextSibling != null) {
		var p = o.parentNode;
		var s = p.removeChild(o.nextSibling);
		p.insertBefore(s,o);
	}
}

/////////////////////////////////////
// showHide: toggle visibility for an object
/////////////////////////////////////
var zIndex = 100;
function showHide(obj,sh) {
	if (obj) {
		if (sh == null) {
			if (obj.style.visibility == "hidden") {
				sh = "show";
			} else {
				sh = "hide";
			}
		}
		if (sh == "show") {
			_setSelectVisibility("hidden",obj);
			zIndex ++;
			obj.style.zIndex = zIndex;
			obj.style.visibility = "visible";
		} else {
			_setSelectVisibility("visible",obj);
			obj.style.visibility = "hidden";
		}
	}
}

/////////////////////////////////////
// toggleDisplay: toggle display for an object
/////////////////////////////////////
function toggleDisplay(obj,sh,type) {
	if (sh == null) {
		if (obj.style.display != "none") {
			sh = "hide";
		} else {
			sh = "show";
		}
	}
	if (type == null) {
		type = "inline";
	}
	if (sh == "show") {
		obj.style.display = "inline";
	} else {
		obj.style.display = "none";
	}
}



/////////////////////////////////////
// functions to preserve scroll height of window after postback
// To use:
// - do setScrollHeight() on page load
// - add hidden form field with id of scrollHeight
// - do saveScrollHeight() before post back
/////////////////////////////////////
function setScrollHeight(e) {
	if (getEl("scrollHeight").value != "") {
		try {
			window.document.body.scrollTop = getEl("scrollHeight").value;
		} catch(e) {}
	} else {
		placeFocus();
	}
}

function getScrollHeight() {
	var h = window.pageYOffset || window.document.body.scrollTop || window.document.documentElement.scrollTop;
	return h ? h : 0;
}

// saveScrollHeight() called on postback
function saveScrollHeight() {
	getEl("scrollHeight").value = getScrollHeight()
}

/////////////////////////////////////
//// Select objects are windowed and therefore don't obey the zIndex in IE (at least at the time of this writing)
//// Therefore, they need to be hidden if they are in the way of an object.
/////////////////////////////////////
function _setSelectVisibility(visibility,obj) {
	// get object position
	var oTop = _totalOffsetTop(obj);
	var oLeft = _totalOffsetLeft(obj);
	var oBottom = oTop + obj.offsetHeight;
	var oRight = oLeft + obj.offsetWidth;

	// loop through select objects
	var selectObjs = document.getElementsByTagName("SELECT");
	for (var i = 0; i < selectObjs.length; i ++) {
		selectObj = selectObjs[i];
		if (visibility == "hidden") {
			// get select object position
			var sTop = _totalOffsetTop(selectObj);
			var sLeft = _totalOffsetLeft(selectObj);
			var sBottom = sTop + selectObj.offsetHeight;
			var sRight = sLeft + selectObj.offsetWidth;

			// If select object overlaps menu, hide it.
			if (((oTop < sTop && sTop < oBottom) || (oTop < sBottom && sBottom < oBottom) || (sTop < oTop && sBottom > oBottom) || (sTop > oTop && sBottom < oBottom))
				&& ((oLeft < sLeft && sLeft < oRight) || (oLeft < sRight && sRight < oRight) || (sLeft < oLeft && sRight > oRight) || (sLeft > oLeft && sRight < oRight))) {
				//Make sure select is not contained by object
				var isChild = false;
				var parent = selectObj.parentNode;
				while (parent && !isChild) {
				    isChild = (parent == obj);
			        parent = parent.parentNode;
				}
				if (!isChild) selectObj.style.visibility = "hidden";
			}
		} else {

			// Make sure all select objects are visible again.
			selectObj.style.visibility = "inherit";
		}
	}
}
function _totalOffsetLeft(el) {
	// Return the true x coordinate of an element relative to the page.
	return el.offsetLeft + (el.offsetParent ? _totalOffsetLeft(el.offsetParent) : 0);
}

function _totalOffsetTop(el) {
	// Return the true y coordinate of an element relative to the page.
	return el.offsetTop + (el.offsetParent ? _totalOffsetTop(el.offsetParent) : 0);
}
/////////////////////////////////////

/*
General function to merge rows using javascript
	table: table element
	startRowIndex: index of the first row that should be combined
	idCellIndex: index of the column that contains a unique identifier for the cells to be merged
	firstMergeCellIndex: index of the first column to merge
	numCellsToMerge: number of cells to merge, starting with firstMergeCellIndex
	addNumericColumns: if values in merged cells are numeric, they will be added together
*/
function mergeRows(table,startRowIndex,idCellIndex,firstMergeCellIndex,numCellsToMerge,totalNumericColumns) {
	var mergeRows,cellTotals,rowInex,row;
	if (totalNumericColumns == null) totalNumericColumns = false;
	mergeRows = new Array();
	cellTotals = new Array();
	rowIndex = startRowIndex;
	while (table && table.rows.length > rowIndex) {
		row = table.rows[rowIndex]
		mergeRows.length = 0;
		cellTotals.length = 0;
		//get rows that need to be merged
		//if the contents of the idCell are the same, then they will be merged.
		while (table.rows[rowIndex + mergeRows.length + 1]
			&& table.rows[rowIndex + mergeRows.length + 1].cells[idCellIndex]
			&& row.cells[idCellIndex].innerHTML == table.rows[rowIndex + mergeRows.length + 1].cells[idCellIndex].innerHTML) {
			mergeRows[mergeRows.length] = table.rows[rowIndex + mergeRows.length + 1];
		}
		//remove cells from merged rows
		for (var i = 0; i < mergeRows.length; i++) {
			for (var n = 0; n < numCellsToMerge; n++) {
				if (totalNumericColumns) {
					if (!isNaN(parseFloat(mergeRows[i].cells[firstMergeCellIndex].innerHTML))) {
						if (cellTotals[n] == undefined) cellTotals[n] = 0;
						cellTotals[n] += parseFloat(mergeRows[i].cells[firstMergeCellIndex].innerHTML);
					}
				}
				mergeRows[i].removeChild(mergeRows[i].cells[firstMergeCellIndex]);
			}
		}
		//increase rowspan for cells in top row
		if (mergeRows.length > 0) {
			for (var i = 0; i < numCellsToMerge; i++) {
				if (totalNumericColumns) {
					if (!isNaN(parseFloat(row.cells[firstMergeCellIndex + i].innerHTML)) && cellTotals[i] != undefined) {
						row.cells[firstMergeCellIndex + i].innerHTML = parseFloat(row.cells[firstMergeCellIndex + i].innerHTML) + cellTotals[i];
					}
				}
				row.cells[firstMergeCellIndex + i].rowSpan = mergeRows.length + 1;
			}
		}
		rowIndex = rowIndex + mergeRows.length + 1;
	}
}

//Add nbsp to empty cells so that their borders show
function fillEmptyCells() {
    var tds = document.getElementsByTagName("TD");
    for (var i=0; i < tds.length; i++) {
        if (tds[i].innerHTML.search(/[^\s]/) == -1) {
            tds[i].innerHTML = "&nbsp;";
        }
    }
}

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function ValidateEmail(emailaddr){
    var returnval=emailfilter.test(emailaddr)
    return returnval
}

function checkEmailBox(strID,emailTxtBox, omitStr){
    if (ValidateEmail(getEl(emailTxtBox).value) == true){
        getEl(strID).checked = true;
        getEl(omitStr).checked = false;
    } else {
        getEl(strID).checked = false;
     }
}

function uncheckProvidedIDs(omitStr, sendEmailStr, sendToPrintStr){
    if (getEl(omitStr).checked == true){
        //alert(omitStr+','+sendEmailStr+','+altEmailStr);
        getEl(sendEmailStr).checked = false;
        getEl(sendToPrintStr).checked = false;
       } 
 }
 function uncheckOmit(omitStr, sendEmailStr, sendToPrintStr){
    if (getEl(sendEmailStr).checked == true || getEl(sendToPrintStr).checked == true){
        //alert(omitStr+','+sendEmailStr+','+sendToPrintStr);
        getEl(omitStr).checked = false;
       } 
}

// Generic rollover image functions
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


///////////////////////////////////////////////////////////////
// ContentTable: utility for building a table of subitems for a content type
///////////////////////////////////////////////////////////////
//ContentTables object for accessing row objects on a page
ContentTables = {}
//ContentTable
function ContentTable(/*string*/name,/*Object*/properties) {
    this.name = name;
    for (prop in properties){
        this[prop] = properties[prop];
    }
    if (!("canReorder" in this)) this.canReorder = false;
    if (!("editUrl" in this)) this.editUrl = null;
    if (!("viewUrl" in this)) this.viewUrl = null;
    if (!("isRequired" in this)) this.isRequired = false;
    if (!("useEditDialog" in this)) this.useEditDialog = false;

    this.htmlTable = getEl("tbl" + name);
    this.htmlTable.style.display = "none";
    this.htmlTbody = this.htmlTable.getElementsByTagName("TBODY")[0];
    this.rows = [];
    this.columns = [];
    this.columnDict = {};
    this.nextId = 0;
}
ContentTable.prototype = {
    addColumn: function(/*string*/id,/*Object*/properties){
        return new ContentColumn(this,id,properties);
    },
    // Generate popup window
    popup: function(url,width,height) {
        ContentTables.currentTable = this;
        if (url==null) url = this.editUrl;
        if (width==null) width=770;
        if (height==null) height=500;
        this.SelectWindow = popupDialog(this.name,width,height,"resizable=1,scrollbars=1,location=1,status=1");
        this.SelectWindow.location = url;
    },
    // Generate popup window to add an item
    addPopup: function(url,width,height) {
        ContentRows.currentRow = null;
        this.popup();
    },
    // Adds an empty row
    addEmpty: function() {
        var content = {};
        for (var i=0; i<this.columns.length; i++) {
            content[this.columns[i].id] = ["",""];
        }
        var cr = this.add(content);
        cr.isTemporary = true;
        cr.showDialog();
    },
    // Saves a row by updating the current row or adding a new one
    save: function(content) {
        if (ContentRows.currentRow) {
            ContentRows.currentRow.update(content);
        } else {
            this.add(content);
        }
    },
    // Add a to the list, by dynamically creating table row.
    add: function(content) {
        var rows = this.htmlTbody.rows;
        var row, cell, box, img, hdn, html;

        // If adding first row, show table
        this.htmlTable.style.display = "";

        //do not add if already selected
        if (rows.length>0 && this.primaryKeyColumn) {
            for (var i=0;i<this.rows.length;i++){
                if (this.rows[i].getPrimaryKeyValue == content[this.primaryKeyColumn.id][0]) {
                    return false;
                }
            }
        } 

        //create row
        var cr = new ContentRow(this);
        
        //create contents of edit dialog
        if (this.useEditDialog){
            var html = '<table>';
            for (var i=0; i<this.columns.length; i++) {
                var c = this.columns[i];
                html += '<tr valign=baseline><td>';
                if (c.isRequired) html+="*";
                html += c.label + ':</td><td>' + c.render(cr,content[c.id]) + '</td></tr>';
            }
            html += '</table>';

            var el = getEl("popupHolder").appendChild(document.createElement("div"));
            el.id = cr.htmlRow.id + "_dialog";
            el.innerHTML = html;
        }

        //Display columns
        for (var i=0; i<this.columns.length; i++) {
            var c = this.columns[i];
            if (content[c.id]==null) content[c.id] = [""];
            if (this.useEditDialog) {
                if (c.view) {
                    cell = cr.htmlRow.appendChild(document.createElement("TD"));
                    cell.id = cr.htmlRow.id + "_" + c.id;
                    cell.innerHTML = c.getContent(cr)[1];
                }
            }else{
                cell = cr.htmlRow.appendChild(document.createElement("TD"));
                cell.id = cr.htmlRow.id + "_" + c.id;
                cell.innerHTML = c.render(cr,content[c.id]);
            }
        }

        if (this.canReorder){
            //cell for move up/move down buttons
            cell = cr.htmlRow.appendChild(document.createElement("TD"));
            cell.id = cr.htmlRow.id + "_subIndex";
            cell.className = "button";
        }
        
        if (this.editUrl!=null || this.useEditDialog){
            cell = cr.htmlRow.appendChild(document.createElement("TD"));
            cell.className = "button";
            img = cell.appendChild(document.createElement("IMG"));
            img.src = "/assets/images/manager/cms_edit_b.gif";
            img.alt = "Edit";
            img.style.cursor = "pointer";
            img.onclick = function(){ContentRows.getParentRow(this).edit()}
        }

        //remove button
        cell = cr.htmlRow.appendChild(document.createElement("TD"));
        cell.className = "button";
        img = cell.appendChild(document.createElement("IMG"));
        img.src = "/assets/images/misc/b_x2.gif";
        img.alt = "Remove";
        img.onclick = function(){ContentRows.getParentRow(this).remove()}
        img.style.cursor = "pointer";
        
        //Save id in hidden field with remove button.
        var hdn = document.createElement("INPUT")
        hdn.type = "hidden";
        hdn.name = this.name + "_ids";
        hdn.value = cr.id;
        cell.appendChild(hdn);

        this.setOrder();
        
        return cr;
    },
    // Get row index
    getRowIndex: function(id) {
        for (var i=0;i<this.rows.length;i++){
            if (this.rows[i].id == id) {
                return i;
                break;
            }
        }
        return -1;
    },
    // Get row for an item
    getRow: function(id) {
        return this.rows[this.getRowIndex(id)];
    },
    // Remove based on id
    remove: function(id) {
        if (this.getRow(id)){
            this.getRow(id).remove(false);
        }
    },
    // Set move up/move down buttons
    setOrder: function() {
        if (this.canReorder) {
            for (var i = 0; i < this.rows.length; i++) {
                var row = this.rows[i];
                var cell = getEl(row.htmlRow.id + "_subIndex");
                cell.innerHTML = "";
                var hdn = document.createElement("INPUT");
                hdn.type = "hidden";
                hdn.name = cell.id + "_Field";
                hdn.value = String(i);
                cell.appendChild(hdn);
                if (this.rows.length <= 1) {
                    cell.style.display="none";
                } else {
                    cell.style.display="";
                    if (i != this.rows.length - 1) {
                        img = cell.appendChild(document.createElement("IMG"));
                        img.src = "/assets/images/misc/b_arrowdown.gif";
                        img.alt = "Move Down";
                        if (i == 0) {
                            img.style.margin="0px 32px 0px 3px";
                        } else {
                            img.style.margin="0px 3px";
                        }
                        img.style.cursor="pointer";
                        img.onclick = function(){ContentRows.getParentRow(this).moveDown()}
                    }
                    if (i != 0) {
                        img = cell.appendChild(document.createElement("IMG"));
                        img.src = "/assets/images/misc/b_arrowup.gif";
                        img.alt = "Move Up";
                        if (i == this.rows.length - 1) {
                            img.style.margin="0px 3px 0px 32px";
                        } else {
                            img.style.margin="0px 3px";
                        }
                        img.style.cursor="pointer";
                        img.onclick = function(){ContentRows.getParentRow(this).moveUp()}
                    }
                }
            }
        }
    },
    //Validate all required fields
    validate: function(source, arguments) {
        arguments.IsValid = true;
        if (this.isRequired && this.rows.length == 0) {
            arguments.IsValid = false;
        } else {
            var content;
            for (var i=0;i<this.rows.length;i++){
                if (!this.rows[i].validate()) {
                    arguments.IsValid = false;
                    break;
                }
            }
        }
    }
}

//ContentRows object for accessing row objects on a page
ContentRows = {
    getParentRow: function(el){
        while (el.tagName!="TR"&&el.parentNode) el = el.parentNode;
        return ContentRows[el.id];
    }
}

//ContentRow constructor
function ContentRow(/*ContentTable*/ct) {
    this.contentTable = ct;
    this.id = this.contentTable.nextId;
    this.contentTable.nextId++;
    this.htmlRow = ct.htmlTbody.appendChild(document.createElement("TR"));
    this.htmlRow.id = ct.name + "_" + this.id;
    this.contentTable.rows.push(this);
    ContentRows[this.htmlRow.id] = this;
    ContentRows.currentRow = this;
}
ContentRow.prototype = {
    //Get the primary key values for the row
    getPrimaryKeyValue: function() {
        return this.contentTable.primaryKeyColumn.getContent(this)[0];
    },
    // validate row
    validate: function() {
        for (var j=0;j<this.contentTable.columns.length;j++){
            var c = this.contentTable.columns[j];
            if (!c.validate(this)) {
                this.errorMessage = c.errorMessage;
                return false;
                break;
            }
        }
        return true;
    },
    // Update the content of a row
    update: function(content) {
        var cell;
        for (var i=0; i<this.contentTable.columns.length; i++) {
            var c = this.contentTable.columns[i];
            cell = getEl(this.htmlRow.id + "_" + c.id);
            if (content[c.id]) {
                cell.innerHTML = c.render(this,content[c.id]);
            }
        }
        return true;
    },
    // update from dialog
    updateFromDialog: function() {
        if (this.validate()){
            var c,content;
            for (var i=0; i<this.contentTable.columns.length; i++) {
                c = this.contentTable.columns[i];
                content = c.getContent(this);
                if (c.view) {
                    getEl(this.htmlRow.id + "_" + c.id).innerHTML = content[1];
                }
            }
            this.editDialog.hide();
            this.isTemporary = false;
            return true;
        } else {
            alert(this.errorMessage);
            return false;
        }
    },
    // Ordering functions
    moveUp: function() {
        this.setDefaultChecked(this.htmlRow);
        moveObjUp(this.htmlRow);
        var i = this.contentTable.getRowIndex(this.id);
        this.contentTable.rows.splice(i,1);
        this.contentTable.rows.splice(i-1,0,this);
        this.contentTable.setOrder();
    },
    moveDown: function() {
        this.setDefaultChecked(this.htmlRow.nextSibling);
        moveObjDown(this.htmlRow);
        var i = this.contentTable.getRowIndex(this.id);
        this.contentTable.rows.splice(i,1);
        this.contentTable.rows.splice(i+1,0,this);
        this.contentTable.setOrder();
    },
    setDefaultChecked: function(htmlRow) {
        // Set defaultChecked attribute for checkboxes.  This is necessary to not lose checked state when rows are moved
        var inputs = htmlRow.getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i ++) inputs[i].defaultChecked = inputs[i].checked;
    },
    // edit an item in the list
    edit: function() {
        ContentRows.currentRow = this;
        if (this.contentTable.useEditDialog) {
            this.showDialog();
        } else {
            this.contentTable.popup(this.contentTable.editUrl + "&id=" + this.id);
        }
    },
    // show edit dialo
    showDialog: function() {
        if (!this.editDialog) {
            this.editDialog = new Ext.Window({ 
                renderTo:getEl("popupHolder"),
                contentEl:getEl(this.htmlRow.id + "_dialog"),
                title:this.contentTable.label + " Add/Edit",
                modal:true,
                shadow:true,
                width:500,
                height:300,
                resizable:false,
                draggable:false,
                collapsible:false,
                constrain:false,
                buttons:[
                    {text:"OK", handler:this.updateFromDialog, scope:this},
                    {text:"Cancel", handler:function(){this.editDialog.hide()}, scope:this}
                ]
            });
            this.editDialog.on("hide", this.cancelEdit, this);
            //set the container property of the window to the body.
            //This is not a supported property, so it might break if Ext is updated!
            this.editDialog.container = document.body;
        }
        this.editDialog.show(this.htmlRow);
    },
    // cancel edit
    cancelEdit: function() {
        if (this.contentTable.useEditDialog) {
            if (!this.validate() && this.isTemporary) this.remove(false);
        }
    },
    // Delete an item in the list
    remove: function(doConfirm) {
        if (this.htmlRow != null) {
            if (doConfirm == null) doConfirm = true;
            if (!doConfirm || confirm("Are you sure you want to delete this record?")) {
	            this.htmlRow.parentNode.removeChild(this.htmlRow);
                this.contentTable.rows.splice(this.contentTable.getRowIndex(this.id),1);
                if (this.editDialog) {
                    getEl("popupHolder").removeChild(getEl(this.editDialog.id));
                }
	            // If deleting last row, delete header row
	            var rows = this.contentTable.rows;
	            if (rows.length == 0) {
		            this.contentTable.htmlTable.style.display = "none";
	            }
	            if (doConfirm) {
		            this.contentTable.setOrder();
	            }
            }
        }
    }
}

//ContentColumn constructor
function ContentColumn(/*ContentTable*/contentTable,/*String*/id,/*Object*/properties) {
    this.contentTable = contentTable;
    this.id = id;
    for (prop in properties){
        this[prop] = properties[prop];
    }
    if (!("type" in this)) this.type = "text";
    if (!("isRequired" in this)) this.isRequired = false;
    if (!("isPrimaryKey" in this)) this.isPrimaryKey = false;
    if (!("validators" in this)) this.validators = [];
    if (this.isRequired) this.validators.unshift(ContentColumnValidator.Required);

    this.contentTable.columns.push(this);
    this.contentTable.columnDict[id]=this;
    if (this.isPrimaryKey) {
        this.contentTable.primaryKeyColumn = this;
    }
}
ContentColumn.prototype = {
    getFieldId: function(/*ContentRow*/contentRow){
        return contentRow.htmlRow.id + "_" + this.id + "_Field";
    },
    render: function(/*ContentRow*/contentRow,/*Array*/content){
        var html;
        var htmlId = this.getFieldId(contentRow);
        if (content.length==0) content[0] = "";
	    switch (this.type) {
		    case "readOnly":
                html = '<input type="hidden" id="' + htmlId + '" name="' + htmlId + '" value="' + content[0] + '">';
                html += '<div id="' + htmlId + '_Name">' + content[1] + '</div>';
			    break;
		    case "text":
                html = '<input type="text" id="' + htmlId + '" name="' + htmlId + '" value="' + content[0] + '" class="text">';
			    break;
		    case "datefield":
                html = '<input type="text" id="' + htmlId + '" name="' + htmlId + '" value="' + content[0] + '" class="date">';
                html += "<img src=/assets/images/calendar.gif alt=Calendar onclick=showPopupCalendar(getEl('" + htmlId + "'))>";
			    break;
		    case "wysiwyg":
		        html = '<textarea id="' + htmlId + '" name="' + htmlId + '" readOnly=true style=float:left class="wysiwyg">' + content[0] + '</textarea>';
                html += "<input type=button onclick=eWebEditPro.edit('" + htmlId + "') value=Edit name=editButton/>"
		        break;
		    case "file":
                html = '<input type="file" id="' + htmlId + '" name="' + htmlId + '" class="file">';
                if (content[0].length > 0) {
                    html += '<input type="hidden" id="' + htmlId + '_CurrentFile" name="' + htmlId + '_CurrentFile" value=' + content[0] + '>';
                    html += '<br><a href="' + this.path + content[0] + '" target="_blank">' + content[0] + '</a>';
                }
			    break;
	        case "selectBox":
                html = '<select id="' + htmlId + '" name="' + htmlId + '" class="select">';
                for (var i=0;i<this.values.length;i++){
                    html += '<option value="' + this.values[i].id + '"';
                    if (this.values[i].id==content[0]) {
                        html += ' selected';
                    }
                    html += '>' + this.values[i].name + '</option>';
                }
                html += '</select>';
			    break;
	    }
        return html;
    },
    isEmpty: function(/*ContentRow*/contentRow){
        return (this.getContent(contentRow)[0]=="");
    },
    validate: function(/*ContentRow*/contentRow){
        //loop through all validators for the column
        for (var v=0;v<this.validators.length;v++){
            var validator = this.validators[v];
            if (!validator(contentRow,this)) return false;
        }
        return true;
    },
    setContent: function(/*ContentRow*/contentRow,/*Array*/content){
        var htmlId = this.getFieldId(contentRow);
        if (content.length==0) content[0] = "";
	    switch (this.type) {
	        case "selectBox":
                /*Not Implemented*/
			    break;
			default:
			    getEl(htmlId).value = content[0];
			    break;
	    }
    },
    getContent: function(/*ContentRow*/contentRow){
        //content array value and display value
        var content = [];
        var htmlId = this.getFieldId(contentRow);
	    switch (this.type) {
		    case "readOnly":
                content[0] = getEl(htmlId).value;
                content[1] = getEl(htmlId + "_Name").value;
			    break;
		    case "text":
                content[0] = getEl(htmlId).value;
			    break;
		    case "wysiwyg":
                content[0] = getEl(htmlId).value;
			    break;
		    case "datefield":
                content[0] = getEl(htmlId).value;
			    break;
		    case "file":
                content[0] = getEl(htmlId).value;
                if (content[0].length == 0 && getEl(htmlId + "_CurrentFile")) {
                    content[0] = getEl(htmlId + "_CurrentFile").value;
                }
                if (content[0].length > 0) {
                    content[0] = '<a href="' + this.path + content[0] + '" target="_blank">' + content[0] + '</a>';
                }
			    break;
	        case "selectBox":
	            var options = getEl(htmlId).options;
	            var values = [];
	            var texts = [];
                for (var i=0;i<options.length;i++){
                    if (options[i].selected) {
                        values[values.length] = options[i].value;
                        texts[texts.length] = options[i].text;
                    }
                }
                content[0] = values.join(",");
                content[1] = texts.join(",");
			    break;
	    }
        if (content.length==1) content[1] = content[0];
        return content;
    }
}

ContentColumnValidator = {
    Required:function(row,col){
        if (col.isEmpty(row)) {
            col.errorMessage = col.label + " is required.";
            return false;
        } else {
            return true;
        }
    },
    Date:function(row,col){
        var value = col.getContent(row)[0];
        if (value != ""){
            if (isNaN(Date.parse(value))) {
                col.errorMessage = col.label + " must be a valid date.";
                return false;
            } else {
                //Make sure 
                col.setContent(row,[value.replace(/\/([0-9]{2})$/,"/20$1")]);
                return true;
            }
        } else {
            return true;
        }
    },
    Url:function(row,col){
        var value = col.getContent(row)[0];
        if (value != "" && value.search(/^https?:\/\/[^ ]+$/) == -1){
            col.errorMessage = col.label + " must be a valid url starting with http:// or https://";
            return false;
        }else{
            return true;
        }
    }
}