/*-----------------------------------------------------------------------------*/
/* GENERAL USE FUNCTIONS 													   */
/*-----------------------------------------------------------------------------*/
function limitFieldLength() /*field id, length */
{
		var theField = document.getElementById(arguments[0]);
		var theLength = parseFloat(arguments[1]);
		var theValue = theField.value;
		if(theValue.length > theLength)
		{
			theField.value = theValue.substring(0,theLength);
		}
}
/*-----------------------------------------------------------------------------*/
function in_array(needle, haystack, strict) 
{
    var found = false, key, strict = !!strict;
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
    return found;
}
/*-----------------------------------------------------------------------------*/
function getElementsByClassName() 
{
	var cl = arguments[0];
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) 
	{
		var classes = elem[i].className;
		if (myclass.test(classes))
			retnode.push(elem[i]);
	}
	return retnode;
}
/*-----------------------------------------------------------------------------*/
function getRadioValue()
{
	if(!arguments[0])
		return "";
	else
	{
		var radioObj = document.getElementsByName(arguments[0]);
		var radioLength = radioObj.length;
		if(radioLength == undefined)
			if(radioObj.checked)
				return radioObj.value;
			else
				return "";
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) 
				return radioObj[i].value;
		}
		return "";
	}
}
/*-----------------------------------------------------------------------------*/
function setSelectValue()
{
	if(!arguments[0])
		return "";
	else
	{
		var selectObj = document.getElementById(arguments[0]);
		for (i=0; i< selectObj.options.length; i++)
		{
			if(selectObj.options[i].value == arguments[1])
			{
				selectObj.selectedIndex = i;
				return true;
			}
		}
	}
}
/*-----------------------------------------------------------------------------*/
function addScript ()
{
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = arguments[0];
	if(arguments[1])
	script.id = arguments[1];
	document.getElementsByTagName('head')[0].appendChild(script);  
}
/*-----------------------------------------------------------------------------*/
function removeScript()
{
	if(document.getElementById(arguments[0]))
		document.getElementsByTagName('head')[0].removeChild(document.getElementById(arguments[0]));
}
/*-----------------------------------------------------------------------------*/
function externalLinks() 
{ 
	if(!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a"); 
	for(var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank"; 
		}
	} 
} 
/*-----------------------------------------------------------------------------*/
function AttachEvent(obj,evt,fnc,useCapture)
{
	if (!useCapture) useCapture=false;
	if (obj.addEventListener)
	{
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} 
	else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else
	{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 
/*-----------------------------------------------------------------------------*/
	function MyAttachEvent(obj,evt,fnc)
	{
		if (!obj.myEvents) obj.myEvents={};
		if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
		var evts = obj.myEvents[evt];
		evts[evts.length]=fnc;
	}
/*-----------------------------------------------------------------------------*/
	function MyFireEvent(obj,evt)
	{
		if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
		var evts = obj.myEvents[evt];
		for (var i=0,len=evts.length;i<len;i++) evts[i]();
	}
/*-----------------------------------------------------------------------------*/
function getAvailWidth() 
{
	var width = 0;
	if( document.documentElement && document.documentElement.clientWidth ) 
		width = document.documentElement.clientWidth;
	else if( document.body && document.body.clientWidth ) 
		width = document.body.clientWidth;
	else if( window.innerWidth ) 
		width = window.innerWidth - 18;
	return width;
}
/*-----------------------------------------------------------------------------*/
function getAvailHeight() 
{
	var height = 0;
	if( document.documentElement && document.documentElement.clientHeight ) 
		height = document.documentElement.clientHeight;
	else if( document.body && document.body.clientHeight ) 
		height = document.body.clientHeight;
	else if( window.innerHeight ) 
		height = window.innerHeight - 18;
	return height;
}
/*-----------------------------------------------------------------------------*/
function getScrollX() 
{
	var scrollX = 0;
	if( document.documentElement && document.documentElement.scrollLeft ) 
		scrollX = document.documentElement.scrollLeft;
	else if( document.body && document.body.scrollLeft ) 
		scrollX = document.body.scrollLeft;
	else if( window.pageXOffset ) 
		scrollX = window.pageXOffset;
	else if( window.scrollX ) 
		scrollX = window.scrollX;
	return scrollX;
}
/*-----------------------------------------------------------------------------*/
function getScrollY() 
{
	var scrollY = 0;
	if( document.documentElement && document.documentElement.scrollTop )
		scrollY = document.documentElement.scrollTop;
	else if( document.body && document.body.scrollTop )
		scrollY = document.body.scrollTop;
	else if( window.pageYOffset )
		scrollY = window.pageYOffset;
	else if( window.scrollY ) 
		scrollY = window.scrollY;
	return scrollY;
}
/*-----------------------------------------------------------------------------*/
/* ACTIONS						                                               */
/*-----------------------------------------------------------------------------*/
function setChanges()
{
	if(document.getElementById('changes').value !=1 )
		document.getElementById('changes').value = 1;
}
/*-----------------------------------------------------------------------------*/
function unsetChanges()
{
	if(document.getElementById('changes').value !=0 )
		document.getElementById('changes').value = 0;
}
/*-----------------------------------------------------------------------------*/
/* MODAL DIALOGUE FUNCTIONS		                                               */
/*-----------------------------------------------------------------------------*/
function positionModal()
{
	var availWidth = getAvailWidth();
	var availHeight = getAvailHeight();
	var scrollX = getScrollX();
	var scrollY = getScrollY();
	var modalScreen  = document.getElementById('modalScreen');
	var modalWindow    = document.getElementById('modalWindow');
	var modalShadow    = document.getElementById('modalWindowShadow');
	var w = modalWindow.style.width.replace(/px/,"");
	var h = modalWindow.style.height.replace(/px/,"");
	modalScreen.style.width = (availWidth + scrollX) + 'px';
	modalScreen.style.height = (availHeight + scrollY) + 'px';
	modalWindow.style.marginLeft = (((availWidth - w)/2) + scrollX) + 'px';
	modalWindow.style.marginTop = (((availHeight - h)/2 ) + scrollY) + 'px';
	modalShadow.style.marginLeft = (((availWidth - w)/2) + scrollX + 10 ) + 'px';
	modalShadow.style.marginTop = (((availHeight - h)/2 ) + scrollY + 10) + 'px';
}
/*-----------------------------------------------------------------------------*/
function positionModalModal()
{
	var availWidth = getAvailWidth();
	var availHeight = getAvailHeight();
	var scrollX = getScrollX();
	var scrollY = getScrollY();
	var modalScreen  = document.getElementById('modalModalScreen');
	var modalWindow    = document.getElementById('modalModalWindow');
	var modalShadow    = document.getElementById('modalModalWindowShadow');
	var w = modalWindow.style.width.replace(/px/,"");
	var h = modalWindow.style.height.replace(/px/,"");
	modalScreen.style.width = (availWidth + scrollX) + 'px';
	modalScreen.style.height = (availHeight + scrollY) + 'px';
	modalWindow.style.marginLeft = (((availWidth - w)/2) + scrollX) + 'px';
	modalWindow.style.marginTop = (((availHeight - h)/2 ) + scrollY) + 'px';
	modalShadow.style.marginLeft = (((availWidth - w)/2) + scrollX + 10 ) + 'px';
	modalShadow.style.marginTop = (((availHeight - h)/2 ) + scrollY + 10) + 'px';
}
/*-----------------------------------------------------------------------------*/
function updateModal()
{
	/* do it twice to stop the scrollbars screwing up the screen width */
	positionModal();
	positionModal();
	if(document.getElementById('modalModalScreen').display == 'block')
		positionModalModal();
}
/*-----------------------------------------------------------------------------*/
function showPopupUrl(url,title,w,h)
{
	var timeStamp = new Date().getTime();
	url = url + '?' + timeStamp;
	var output = "<iframe style=\"width:"+ (w-5) +"px; height:"+ (h-22) +"px; padding:0px; margin:0px;\" src=\""+url+"\" scrolling=\"no\" ></iframe>";
	showModal(output,title,w,h);
}
/*-----------------------------------------------------------------------------*/
function showModal(msg,title,w,h,btn)
{
	//hide form fields
	var theElements = document.getElementsByTagName("select");
	for(var i=0;i<theElements.length;i++)
	{
		theElements[i].style.display = 'none';	
	}
	//show modal form fields
	if(document.getElementById('screenForm'))
	{
		var modalElements = document.getElementById('screenForm').elements;
		for(var i=0;i<modalElements.length;i++)
		{
			if(modalElements[i].tagName == "SELECT")
				modalElements[i].style.display = 'inline';	
		}
	}
	if (!title) title = 'Attention!';
	if (!w) w = 300;
	if (!h) h = 200;
	if (!btn) btn = 'close';
	var modalScreen  = document.getElementById('modalScreen');
	var modalWindow  = document.getElementById('modalWindow');
	var modalShadow  = document.getElementById('modalWindowShadow');
	var modalButtons = document.getElementById('modalButtons');
	var controlButtons = new Array();
	var btns = btn.split(",");
	for(var a=0; a<btns.length; a++)
	{
		switch(btns[a])
		{
			case 'close':
				controlButtons.push('<a href="javascript:hideModal();">X</a>');
				break;
		}
	}
	modalWindow.style.width = w + 'px';
	modalWindow.style.height = h + 'px';
	modalShadow.style.width = w + 'px';
	modalShadow.style.height = h + 'px';
	positionModal();
	document.getElementById('modalTitle').innerHTML = title;
	document.getElementById('modalContent').innerHTML = msg;
	modalButtons.innerHTML = "";
	for(var i=0; i<controlButtons.length; i++) modalButtons.innerHTML += controlButtons[i];
	modalScreen.style.display = 'block';
	modalWindow.style.display = 'block';
	modalShadow.style.display = 'block';

}
/*-----------------------------------------------------------------------------*/
function resizeModalModal(w,h)
{
	if (!w) w = 300;
	if (!h) h = 200;
	var modalScreen  = document.getElementById('modalModalScreen');
	var modalWindow  = document.getElementById('modalModalWindow');
	var modalShadow  = document.getElementById('modalModalWindowShadow');
	modalWindow.style.width = w + 'px';
	modalWindow.style.height = h + 'px';
	modalShadow.style.width = w + 'px';
	modalShadow.style.height = h + 'px';
	positionModal();
	modalScreen.style.display = 'block';
	modalWindow.style.display = 'block';
	modalShadow.style.display = 'block';
}
/*-----------------------------------------------------------------------------*/
function resizeModal(w,h)
{
	if (!w) w = 300;
	if (!h) h = 200;
	var modalScreen  = document.getElementById('modalScreen');
	var modalWindow  = document.getElementById('modalWindow');
	var modalShadow  = document.getElementById('modalWindowShadow');
	modalWindow.style.width = w + 'px';
	modalWindow.style.height = h + 'px';
	modalShadow.style.width = w + 'px';
	modalShadow.style.height = h + 'px';
	positionModal();
	modalScreen.style.display = 'block';
	modalWindow.style.display = 'block';
	modalShadow.style.display = 'block';
}
/*-----------------------------------------------------------------------------*/
function showModalModal(msg,title,w,h,btn)
{
	if (!title) title = 'Attention!';
	if (!w) w = 300;
	if (!h) h = 200;
	if (!btn) btn = 'close';
	var modalScreen  = document.getElementById('modalModalScreen');
	var modalWindow  = document.getElementById('modalModalWindow');
	var modalShadow  = document.getElementById('modalModalWindowShadow');
	var modalButtons = document.getElementById('modalModalButtons');
	var controlButtons = new Array();
	var btns = btn.split(",");
	for(var a=0; a<btns.length; a++)
	{
		switch(btns[a])
		{
			case 'close':
				controlButtons.push('<a href="javascript:hideModalModal();">X</a>');
				break;
		}
	}
	modalWindow.style.width = w + 'px';
	modalWindow.style.height = h + 'px';
	modalShadow.style.width = w + 'px';
	modalShadow.style.height = h + 'px';
	positionModalModal();
	document.getElementById('modalModalTitle').innerHTML = title;
	document.getElementById('modalModalContent').innerHTML = msg;
	modalButtons.innerHTML = "";
	for(var i=0; i<controlButtons.length; i++) modalButtons.innerHTML += controlButtons[i];
	modalScreen.style.display = 'block';
	modalWindow.style.display = 'block';
	modalShadow.style.display = 'block';
}
/*-----------------------------------------------------------------------------*/
function hideModal()
{
	//show form fields
	var theElements = document.getElementsByTagName("select");
	for(var i=0;i<theElements.length;i++)
	{
		theElements[i].style.display = 'inline';	
	}
	document.getElementById('modalScreen').style.display = 'none';
	document.getElementById('modalWindow').style.display = 'none';
	document.getElementById('modalWindowShadow').style.display = 'none';
	document.getElementById('modalContent').innerHTML = '';
}
/*-----------------------------------------------------------------------------*/
function hideModalModal()
{
	document.getElementById('modalModalScreen').style.display = 'none';
	document.getElementById('modalModalWindow').style.display = 'none';
	document.getElementById('modalModalWindowShadow').style.display = 'none';
	document.getElementById('modalModalContent').innerHTML = '';
}
/*-----------------------------------------------------------------------------*/
AttachEvent(window,'scroll',updateModal,false);
AttachEvent(window,'resize',updateModal,false);
/*-----------------------------------------------------------------------------*/
/* DIALOGUES					                                               */
/*-----------------------------------------------------------------------------*/
function doError()
{
	var title  =  'Error!';
	var output =  '<h1 style="clear:both; float:left;"><img src="/api/icons/error.png" style="width:32px;height:32px;padding:0px 5px 0px 5px;vertical-align:middle;"/>Error!</h1>';
		output += '<div style="text-align:left;height:150px; width:380px; margin:5px;padding:5px; background-color:#ffffcc; border:1px solid #000000; overflow-y:auto;">';
		output += arguments[0];
		output += '</div>';
		output += '<h1 class="modalButton" style="width:385px;"><a href="javascript:hideModalModal();" title="Close"><img src="/api/icons/ok.png" />OK</a></h1>';
	showModalModal(output,title,'400','300');
}
/*-----------------------------------------------------------------------------*/
function doFormError()
{
	var title  =  'Incorrect Form Entries';
	var output =  '<h1 style="clear:both; float:left;"><img src="/api/icons/alert.png" style="width:32px;height:32px;padding:0px 5px 0px 5px;vertical-align:middle;"/>Please Correct the Following Fields</h1>';
		output += '<div style="text-align:left;height:150px; width:380px; margin:5px;padding:5px; background-color:#ffffcc; border:1px solid #000000; overflow-y:auto;">';
		output += arguments[0];
		output += '</div>';
		output += '<h1 class="modalButton" style="width:385px;"><a href="javascript:hideModalModal();" title="Close"><img src="/api/icons/ok.png" />OK</a></h1>';
	showModalModal(output,title,'400','300');
}
/*-----------------------------------------------------------------------------*/
function doAlert()
{
	var title  =  'Alert!';
	var output =  '<h1 style="clear:both; float:left;"><img src="/api/icons/alert.png" style="width:32px;height:32px;padding:0px 5px 0px 5px;vertical-align:middle;"/>Alert!</h1>';
		output += '<div style="text-align:left;height:150px; width:380px; margin:5px;padding:5px; background-color:#ffffcc; border:1px solid #000000; overflow-y:auto;">';
		output += arguments[0];
		output += '</div>';
		output += '<h1 class="modalButton" style="width:385px;"><a href="javascript:hideModalModal();" title="Close"><img src="/api/icons/ok.png" />OK</a></h1>';
	showModalModal(output,title,'400','300');
}
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
function doLogout()
{
	var title  =  'Log Out?';
	var output =  '<h1 id="logoutBox" style="clear:both; float:left;"><img src="/api/icons/login.png" style="width:32px;height:32px;padding:0px 5px 0px 5px;vertical-align:middle;"/>Logging out...</h1>';
		output += '<div style="text-align:left;height:50px; width:330px; margin:5px;padding:5px; background-color:#ffffcc; border:1px solid #000000; overflow-y:auto;">';
		output += "Are you sure you want to end this session?";
		output += '</div>';
		output += '<h1 class="modalButton" style="width:160px; float:left;"><a href="javascript:hideModalModal();" title="Cancel"><img src="/api/icons/cancel.png" />Cancel</a></h1>';
		output += '<h1 class="modalButton" style="width:160px; float:left;"><a href="javascript:window.location.reload();" title="Log Out"><img src="/api/icons/login.png" />Log Out</a></h1>';
	showModalModal(output,title,'350','200');
}
/*-----------------------------------------------------------------------------*/
function doDiscardChanges()
{
	//if(document.getElementById('rteditorScript'))
	//	removeScript('rteditorScript');
	if(document.getElementById('changes').value > 0)
	{
		var output =  '<h1 style="clear:both; float:left;"><img src="/api/icons/alert.png" style="width:32px;height:32px;padding:0px 5px 0px 5px;vertical-align:middle;"/>Discard Changes?</h1>';
			output += "<p style=\"padding:5px;clear:both;\">Do you wish to close this screen without saving your changes?</p>";
			output += '<h1 class="modalButton" style="width:110px;float:left;"><a href="javascript:hideModalModal();" title="Close"><img src="/api/icons/cancel.png" />Cancel</a></h1>';
			output += '<h1 class="modalButton" style="width:120px;float:left;"><a href="javascript:hideModal();hideModalModal();" title="Close"><img src="/api/icons/ok.png" />Discard</a></h1>';
		showModalModal(output,'Discard Changes?','260','175');
	}
	else
	hideModal();
}
/*-----------------------------------------------------------------------------*/
/* GUI ACTIONS					                                               */
/*-----------------------------------------------------------------------------*/
function apiHighlightIcon()
{
	var theIcons = arguments[0].parentNode.childNodes;
	for( var i=0; i<theIcons.length; i++)
	{
		if(theIcons[i].className == 'apiListIconsIcon')
		theIcons[i].style.backgroundColor = '';
	}
	arguments[0].style.backgroundColor = 'rgb(236,236,255)';
}
/*-----------------------------------------------------------------------------*/
function apiHighlightList()
{
	var theRows = arguments[0].parentNode.childNodes;
	for( var i=0; i<theRows.length; i++)
	{
		if(theRows[i].className == 'apiListTableRow')
		theRows[i].style.backgroundColor = '';
	}
	arguments[0].style.backgroundColor = 'rgb(236,236,255)';
}
	/*-----------------------------------------------------------------------------*/
	var apiListIconsIconPopupTimeOut;
	function apiListIconsIconPopupShow()
	{
		apiListIconsIconPopupTimeOut = setTimeout("",200);
		var theParts = arguments[0].parentNode.childNodes;
		for( var i=0; i<theParts.length; i++)
		{
			if(theParts[i].className == 'apiListIconsIconPopup')
			theParts[i].style.visibility = 'visible';
		}
	}
	/*-----------------------------------------------------------------------------*/
	function apiListIconsIconPopupHide()
	{
		var theParts = arguments[0].parentNode.childNodes;
		for( var i=0; i<theParts.length; i++)
		{
			if(theParts[i].className == 'apiListIconsIconPopup')
			theParts[i].style.visibility = 'hidden';
		}
	}
/*-----------------------------------------------------------------------------*/
function apiHighlightTile()
{
	var theTiles = arguments[0].parentNode.childNodes;
	for( var i=0; i<theTiles.length; i++)
	{
		if(theTiles[i].className == 'apiListTile')
		theTiles[i].style.backgroundColor = '';
	}
	arguments[0].style.backgroundColor = 'rgb(236,236,255)';
}

