///////////////////////////////////////////////////////////////
// File Name: 	janus.js
///////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
// Shared javascript functions for janus
///////////////////////////////////////////////////////////////

/// <reference path="jquery-1.3.2.min-vsdoc.js"/>

//addYears method for date object
Date.prototype.addYears = function(n) {
    var dt = new Date(this);
    dt.setFullYear(this.getFullYear() + n);
    return dt;
}

//diffYears method for date object
Date.prototype.diffYears = function(dt2) {
    return dt2.getFullYear() - this.getFullYear();
}

//Set validation error for an asp.net validator
function setValidatorErrorMessage(id, msg) {
    $("span[id$=" + id + "]").each(function() {
        $(this).html(msg);
        eval(this.id).errormessage = msg;
    })
}

// 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 inputs = $("#aspnetForm :input:visible");
    if (inputs.length > 0) {
        try {
		    inputs.get(0).focus();
		} catch (e) {
        }
    }
}

// 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);
    } else if ($("*[id$=" + id + "]").length > 0) {
       return $("*[id$=" + id + "]")[0];
    }
}

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)){
        success = false;
        getEl(source.getAttribute("controltovalidate") || eval(source.id).controltovalidate).select();
    }

    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 && !checkboxes[i].disabled) 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;
	}
}



///////////////////////////////////////////////////////////////
// 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];}
}

$(document).ready(function() {
    if ($(".chzn-select").length > 0)
        $(".chzn-select").chosen();
});
