// Standard eon JavaScript functions for tenant applications
// Designed for DOM-compatible browsers


// BROWSER SNIFFERS:
// Use them like so:
// if (platformtype() == "win")...
// if (browsertype() == "ie")...
// if (browserlevel() != "dom")...

function platformtype() // Windows, Mac, or other
	{
	if ( (navigator.platform.indexOf("32") != -1) || (navigator.platform.indexOf("Win") != -1) )
		{
		return "win";
		}
	else if ( (navigator.platform.indexOf("PPC") != -1) || (navigator.platform.indexOf("Mac") != -1) )
		{
		return "mac";
		}
	else
		{
		return ("other");
		}
	}
platformType = platformtype;

function browsertype() // IE, Netscape, or other
	{
	var browser = navigator.appName;
	if (browser.indexOf("Microsoft") != -1)
		{
		return ("ie");
		}
	else if (browser.indexOf("Netscape") != -1)
		{
		return ("ns");
		}
	else
		{
		return ("other");
		}
	}
browserType = browsertype;

function browserlevel() // DOM-compatible (like IE5+ and NS6+), IE4 level (sort of DOM-compatible), NS4 level (weird buggy), or older
	{
	if (document.getElementById)
		{
		return ("dom");
		}
	else if (document.all)
		{
		return ("ie4");
		}
	else if (document.layers)
		{
		return ("ns4");
		}
	else
		{
		return ("old");
		}
	}
browserLevel = browserlevel;

function browservalidate()
	{
	if (browserlevel()!="dom")
		{
		var msg = "Your browser is not DOM-compatible; it probably needs to be updated.  Please install a DOM-compatible browser such as Internet Explorer 5 or later (www.microsoft.com) or Netscape Navigator 6 or later (www.netscape.com)."
		alert(msg);
		}
	else return false;
	}


// PRELOAD IMAGES
// Usage:
// <body onLoad="preloadImages('image_1.gif','image_1_over.gif','image_2.gif','image_2_over.gif')">

function preloadimages()
	{
	if (!document.imageArray)
		{
		document.imageArray = new Array();
		}
    var i;
	var j = document.imageArray.length;
	var a = preloadImages.arguments;
	for(i=0; i < a.length; i++)
		{
		document.imageArray[j] = new Image;
		document.imageArray[j].src = a[i];
		j++;
		}	
	}
preloadImages = preloadimages;

// IMAGE SWAP WITH STICKY IMAGES
//
// Usage:
// <a href="#" onMouseOver="imgover('myImage', 'myImage_over.gif')" onMouseDown="imgstick('myImage', 'myImage_down.gif')" onMouseOut="imgout('myImage')"><img name="myImage" src="myImage.gif"></a>
//
// If you only want one image to stick at a time, call unStickAll first on mouse down:
// ... onMouseDown="unstickall(); imgstick('myImage', 'myImage_down.gif')" ...
//
// If you want to stick an image onLoad, do:
// onLoad="imgstick('myImage', 'myImage_down.gif')"

function imgover(imgName, overImgSrc)
	{
	if (document.images[imgName])
	{
		var m = document.images[imgName];
		if (!m.stuck)
 			{
			m.original = m.src;
			m.src = overImgSrc;
			m.over = m.src;
			}
		}
	}
imgOver = imgover;

function imgout(imgName)
	{
	if (document.images[imgName])
		{
		var m = document.images[imgName];
		if (!m.stuck)
			{
			m.src = m.original;
			}
		}
	}
imgOut = imgout;
 
function imgstick(imgName, downImgSrc)
	{
	if (document.images[imgName])
		{
		var m = document.images[imgName];
		if (!m.stuck)
			{
			if (!m.over)  // in case stick happens without a mouseover
				{
				m.original = m.src;
				m.over = m.src;
				}
			m.src = downImgSrc;
			m.stuck = true;
			}
		else
			{
			m.src = m.over;
			m.stuck = false;
			}
		}
	}
imgStick = imgstick;
 
function unstickall()
	{
	for (var i=0; i < document.images.length; i++)
		{
		if (document.images[i])
			{
			var m = document.images[i];
			if (m.original)
				{
				m.src = m.original;
				}
			m.stuck = false;
			}
		}
	}
unStickAll = unstickall;


// ROW HIGHLIGHTER
// Be careful about setting row colors in CSS!  Can stop this function from working.
//
// Usage:
// <tr id="myRow" onMouseOver="highlight(this)" onMouseOut="unHighlight(this)" onClick="makeSelected(this)">
//
// Default highlight colors are below.  To override colors for a particular page, set these variables in your HTML page below the functions.js include.

var colorHighlighted = "#eeeeee";
var colorSelected = "#cccccc";
var highlightedItem = "undefined";
var selectedItem = "undefined";

function highlight(item)
	{
	if (typeof(item)!="object") item = document.getElementById(item);
	if (item != selectedItem)
		{
		item.style.backgroundColor = colorHighlighted;
		highlightedItem = item;
		}
	}

function unhighlight(item)
	{
	if (typeof(item)!="object") item = document.getElementById(item);
	if (item != selectedItem)
		{
		highlightedItem.style.backgroundColor = "";
		}
	}
unHighlight = unhighlight;

function makeselected(item)
	{
	if (typeof(item)!="object") item = document.getElementById(item);
	if (selectedItem != "undefined")
		{
		selectedItem.style.backgroundColor = "";
		}
	item.style.backgroundColor = colorSelected;
	selectedItem = item;
	}
makeSelected = makeselected;


// TAB SELECTION

function deselectsiblingtabs(el)
	{
	var divs = el.parentNode.getElementsByTagName('DIV');
	for (var i=0; i<divs.length; i++)
		{
		if (divs[i].className=="tabselected")
			{
			divs[i].className="tab";
			}
		}
	}

function selecttab(el)
	{
	if (typeof(el)!="object") el = document.getElementById(el);
	deselectsiblingtabs(el);
	el.className="tabselected";
	}


// GET POSITION OF ELEMENT

function getrealleft(el)
	{
	xPos = el.offsetLeft;
	tempEl = el.offsetParent;
	while (tempEl != null)
		{
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
		}
	return xPos;
	}
getRealLeft = getrealleft;
	
function getrealtop(el)
	{
	xPos = el.offsetTop;
	tempEl = el.offsetParent;
	while (tempEl != null)
		{
		xPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
		}
	return xPos;
	}
getRealTop = getrealtop;


// OVERLAP PROTECTION for show and hide so <select> elements don't overlap in IE	

function overlap(a, b)
	{
	var combinedWidth = a.offsetWidth + b.offsetWidth;
	var shrinkwrapWidth = Math.max(getRealLeft(a)+a.offsetWidth, getRealLeft(b)+b.offsetWidth) - Math.min(getRealLeft(a), getRealLeft(b));

	var combinedHeight = a.offsetHeight + b.offsetHeight;
	var shrinkwrapHeight = Math.max(getRealTop(a)+a.offsetHeight, getRealTop(b)+b.offsetHeight) - Math.min(getRealTop(a), getRealTop(b));

	if	(shrinkwrapWidth < combinedWidth && shrinkwrapHeight < combinedHeight)
		{
		return true;
		}
	else return false;
	}

function showinterferers(el)
	{
	if (!document.all) return false;
	if (el.tagName=='SELECT') return false;
	var interferers = document.getElementsByTagName('SELECT');
	for (var i=0; i<interferers.length; i++)
		{
		if (interferers[i].shouldToggle==true)
			{
			interferers[i].style.visibility = "";
			interferers[i].shouldToggle = false;
			}
		}
	}
showInterferers = showinterferers;
	
function hideinterferers(el)
	{
	if (!document.all) return false;
	if (el.tagName=='SELECT') return false;
	var interferers = document.getElementsByTagName('SELECT');
	for (var i=0; i<interferers.length; i++)
		{
		if (el.contains(interferers[i]))
			{
			continue;
			}
		else if (el.style.visibility!="hidden" && el.style.display!="none" && interferers[i].style.visibility!="hidden" && overlap(el, interferers[i]))
			{
			interferers[i].style.visibility = "hidden";
			interferers[i].shouldToggle = true;
			}
		}
	}
hideInterferers = hideinterferers;

function toggleinterferers(el)
	{
	if (!document.all) return false;
	if (el.tagName=='SELECT') return false;
	var interferers = document.getElementsByTagName('SELECT');
	for (var i=0; i<interferers.length; i++)
		{
		if (el.style.visibility!="hidden" && el.style.display!="none" && interferers[i].style.visibility!="hidden" && overlap(el, interferers[i]))
			{
			interferers[i].style.visibility = "hidden";
			interferers[i].shouldToggle = true;
			}
		else if (interferers[i].shouldToggle==true)
			{
			interferers[i].style.visibility = "";
			interferers[i].shouldToggle = false;
			}
		}
	}
toggleInterferers = toggleinterferers;

	
// TOGGLE (SHOW OR HIDE) AN ELEMENT
// Requires the overlap protections functions (hideInterferers, overlap, etc.)
// need to collaborate with disabling an input text (or any, really) field before hiding 

function show()
	{
	for (var i=0; i<arguments.length; i++)
		{
		var obj = arguments[i];
		if (typeof(obj) != "object") obj = document.getElementById(obj);
		obj.style.display="";
		hideInterferers(obj);
		}
	}

function hide()
	{
	for (var i=0; i<arguments.length; i++)
		{
		var obj = arguments[i];
		if (typeof(obj) != "object") obj = document.getElementById(obj);
		showInterferers(obj);
		obj.style.display="none";
		}
	}


// From dhtmlapi.js -- not sure these are being used anymore

// toggleDisplay is used for expanding/collapsing in clickthru links
function toggledisplay(togglerImgId, elId)
	{
	if (document.getElementById(elId).style.display=="")
		{
		document.getElementById(elId).style.display="none";
		document.getElementById(togglerImgId).src="../../menu/images/expand.gif";
		}
	else
		{
		document.getElementById(elId).style.display="";
		document.getElementById(togglerImgId).src="../../menu/images/collapse.gif";
		}
	}	
toggleDisplay = toggledisplay; 

function toggledisplayold(togglerImg, obj)
	{
	if (document.all[obj].style.display=="")
		{
		document.all[obj].style.display="none";
		document.all[togglerImg].src="../../menu/images/expand.gif";
		}
	else
		{
		document.all[obj].style.display="";
		document.all[togglerImg].src="../../menu/images/collapse.gif";
		}
	}
toggleDisplayOld = toggledisplayold;

// used for cpnmgr inpage help
function togglevis(elId)
	{
	var el = document.getElementById(elId);
	if (el.style.visibility=="") el.style.visibility="hidden";
	else el.style.visibility="";
	toggleInterferers(el);
	}
toggleVis = togglevis;


// DISABLE SUBMIT BUTTONS FUNCTION
// useful during submission of form to keep users from clicking twice
// may not want this if there is a "cancel" button that submits (most will just reload the page)

function disablebuttons(f)
{
	for (var i=0;i<f.length;i++)
	{
		var e = f.elements[i];
		if ((e.name != 'cancel') && (e.type=='submit'))
		{
		e.disabled = true;
		}
	}
}
disableButtons = disablebuttons;

// MAIN CONFIRM
function mainconfirm(msg)
{	
	if (!msg)
		{
		msg = "\n Are you sure? x\n";
		}
	return confirm(msg);
}
mainConfirm = mainconfirm;

// GENERIC CONFIRM BUTTON FUNCTION
function genericconfirm(msg,destination,area)
{
	if (mainConfirm(I18N_uif[msg]))
	{
	if (destination)
		{
		window.open(destination,area);
		}
	else // such as cancel
		{
		location.reload();
		}
	}
}
genericConfirm = genericconfirm;

// CHECKBOX CHECK ALL FUNCTION
// requires that the top (master) checkbox be named "allbox"


function checkall(whichForm)
{
	for (var i=0;i<document.forms[whichForm].elements.length;i++)
	{
		var e = document.forms[whichForm].elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox'))
		e.checked = document.forms[whichForm].allbox.checked;
	}
}
CheckAll = checkall;

function checkcheckall(whichForm)
{
	var TotalBoxes = 0;
	var TotalOn = 0;
	for (var i=0;i<document.forms[whichForm].elements.length;i++)
	{
		var e = document.forms[whichForm].elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox'))
		{
			TotalBoxes++;
			if (e.checked)
			{
				TotalOn++;
			}
		}
	}
	if (TotalBoxes==TotalOn)
	{document.forms[whichForm].allbox.checked=true;}
	else
	{document.forms[whichForm].allbox.checked=false;}
}
CheckCheckAll = checkcheckall;

function disablechecks(whichForm, doDisable)
	{
	for (var i=0; i<document.forms[whichForm].elements.length; i++)
		{
		var e = document.forms[whichForm].elements[i];
		if (e.type=='checkbox')
			{
			e.disabled = doDisable;
			}
		}
	}
disableChecks = disablechecks;

function selectedradiovalue(radioInput)
	{
	for (var i=0; i<radioInput.length; i++)
		{
		if (radioInput[i].checked)
			{
		return radioInput[i].value; 
			}
		}
	return false;
	}
selectedRadioValue = selectedradiovalue;

// NEW WINDOW

function new_window(url,height)
	{
	if (!height)
		{
		height=600;
		}
	link = window.open(url,"Link","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=600,height="+height); 
	}


// EXPANDING AND COLLAPSING WINDOWS

// old
function resizesubframeifneeded()
	{
	if (parent.frameset.rows == "100%,*")
		{
		parent.frameset.rows = "50%,*";
		}
	}
resizeSubframeIfNeeded = resizesubframeifneeded;

// new 

function showdetailframeifneeded(frameName)
	{
	if (!frameName) frameName = "applicationframe";
	if (parent.document.getElementById(frameName).rows == "100%,*")
		{
		parent.document.getElementById(frameName).rows = "50%,*";
		}
	}
showDetailFrameIfNeeded = showdetailframeifneeded;
showPreviewFrameIfNeeded = showdetailframeifneeded;
showpreviewframeifneeded = showdetailframeifneeded;

function showdetailframefull(frameName)
	{
	if (!frameName) frameName = "applicationframe";
	parent.document.getElementById(frameName).rows = "*,100%";
	}
showDetailFrameFull = showdetailframefull;
showPreviewFrameFull = showdetailframefull;
showpreviewframefull = showdetailframefull;

// new 
function fullscreen(frameName)
	{
	if (!frameName) frameName = "frameset";
	if (parent.document.getElementById(frameName))
		{
		parent.document.getElementById(frameName).rows="20,*";
		}
	}

function halfscreen(frameName)
	{
	if (!frameName) frameName = "frameset";
	if (parent.document.getElementById(frameName))
		{
		parent.document.getElementById(frameName).rows="45%,*";
		}
	}


// EXPANDING AND COLLAPSING SEARCH WINDOW		

function getnamedobject(id) {
	if (parent.document.getElementById) {
		return parent.document.getElementById(id);
	}
	else if (parent.document.all) {
		return parent.document.all[id];
	}
	else {
		return null;
	}
}
getNamedObject = getnamedobject;

function showsearchwin() {
	getNamedObject("search").rows = "300,30,*";
}
showSearchWin = showsearchwin;

function hidesearchwin() {
	getNamedObject("search").rows = "30,30,*";
}
hideSearchWin = hidesearchwin;

function showorhide() {
	if (parent.outer.rows == "30,*") {
		parent.outer.rows = "300,*";
	}
	else {
		parent.outer.rows = "30,*";
	}
}
showOrHide = showorhide;


// RELOADING LISTFRAME WHEN NECESSARY

function reloadlist()
	{
	if (window.name=="previewframe" || window.name=="detailframe")
		{
		var fr = window.parent.frames("listframe");
		if (fr)
			{
			fr.location.reload();
			}
		}
	}
reloadList = reloadlist;


// -------------------------------------------------------------
// FUNCTIONS UNLIKELY TO BE USED UNTIL LATER VERSIONS


// AUTOADVANCE TO NEXT FORM INPUT FIELD
// After user types in a given number of characters, jumps them automatically to any other field.
//
// Usage:  
// <input type="text" name="DOBMonth" value="" size="2" maxlength="2" onkeyup="jumpToField(document.myForm.DOBMonth, document.myForm.DOBDay, 2)">

function jumptofield(from,to,len)
	{
	if(from.value.length>=len)
		{
		to.focus();
		}
	}

// HELP HINTS

function placehint(obj, referenceObj)
	{
	document.getElementById(obj).style.left = getRealLeft(referenceObj) + referenceObj.offsetWidth + "px";
	document.getElementById(obj).style.top = getRealTop(referenceObj) + "px";
/*		
		var inWid = 0;
		if (self.innerWidth) inWid = self.innerWidth;
		else if (document.body) inWid = document.body.clientWidth;

		// "200" should really be:  document.getElementById(hintObj).offsetWidth
		if (parseInt(document.getElementById(hintObj).style.left) + 200 > inWid)
			{
			document.getElementById(hintObj).style.left = inWid - 200;
			document.getElementById(hintObj).style.top = (getRealTop(focusedField) + focusedField.offsetHeight) + "px";
			}
*/
	}
placeHint = placehint;

function showhint(obj)
	{
	//if (document.getElementById("toggleHints").checked)
		{
		show(obj);
		}
	}
showHint = showhint;
	
function hidehint(obj)
	{
	hide(obj);
	}
hideHint = hidehint;

function grayout()
	{
	for (var i=0; i<arguments.length; i++)
		{
		var obj = arguments[i];
		if (typeof(obj) != "object") obj = document.getElementById(obj);
		obj.style.background = "#dddddd";
		}
	}

function ungray()
	{
	for (var i=0; i<arguments.length; i++)
		{
		var obj = arguments[i];
		if (typeof(obj) != "object") obj = document.getElementById(obj);
		obj.style.background = "";
		}
	}

function redrawgrayouts(set)
	{
	for (var i=0; i<set.length; i++)
		{
		var el = document.getElementById(set[i]);
		if (el.style.display=="none")
			{
			for (var j=0; j<el.parentNode.childNodes.length; j++)
				{
				el.parentNode.childNodes[j].style.background = "#dddddd";
				}
			}
		else
			{
			for (var j=0; j<el.parentNode.childNodes.length; j++)
				{
				el.parentNode.childNodes[j].style.background = "";
				}
			}
		}
	}
redrawGrayouts = redrawgrayouts;




// -------------------------------------------------------------
// FUNCTIONS UNLIKELY EVER TO BE USED


function extra_window(url,height, width) {
if (!width)
	width=660;
if (!height)
	height=330;
link = window.open(url,"Link","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width="+width+",height="+height); 
}	


// RELOAD DOCUMENT. For example when you want active frame (or location) to reload when clicking a button

function refresh()
	{
	location.reload();
	}


// TOGGLE (SHOW OR HIDE) A DIV ELEMENT IN A GIVEN FRAME

var toggleDiv = new Array();

function showHide(x,targetFrame)
	{
	if (toggleDiv[x])
		{
		parent.frames[targetFrame].document.all.item(x).style.display="none";
		}
	else
		{
		parent.frames[targetFrame].document.all.item(x).style.display="";
		}
	toggleDiv[x] = !(toggleDiv[x]);
	}