﻿/*
Copyright (c) 2008 Bill Davidsen (wdavidsen@yahoo.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/*************************************************************************
*	Function libary for all SCS controls
* 
*	Note: This script requires the script block below on the script level of calling page.
*			SCS Controls automatically register this text block for you.
*
*		if (document.images){
*			_isCSS = (document.body && document.body.style) ? true : false;
*			_isDOM = (_isCSS && document.getElementById) ? true : false;
*			_isIE5 = (_isCSS && document.all) ? true : false;
*			_isNN4 = (document.layers) ? true : false;
*			_isNN6 = (navigator.appName == 'Netscape' && navigator.product == 'Gecko') ? true : false;
*		}
*
*************************************************************************/

function _SCS_onKeyDown(e)
{
	var event_ = _SCS_getEvent(e);
			
	// check to see it access keys are enabled and if they were pressed
	if (_SCS_shortcutData.length > 0)
	{
	    var key = (event_.charCode) ? event_.charCode : event_.keyCode;
	    
	    if (key < 65 || key > 90) 
			return;
			
	    var letter = String.fromCharCode(key).toUpperCase();
	    var modifier = -1;
	    var shortcutType = "";
	    
		if (event_.ctrlKey && event_.altKey)
			modifier = 2;
			
	    else if (event_.ctrlKey && event_.shiftKey)
			modifier = 3;			
	
		else if (event_.ctrlKey)
			modifier = 0;
	
		else if (event_.altKey)
			modifier = 1;
		else
			return;
			
		if (letter.length > 0)
		{
			for (i = 0; i < _SCS_shortcutData.length; i++)
			{
				var sets = _SCS_shortcutData[i].split(",");  
								
				for (j = 0; j < sets.length; j++)
				{       						
					// 0 = target item id, 
					// 1 = letter, 
					// 2 = modifier
					// 3 = shortcut type, 
					// 4 = control type 
					var data = sets[j].split(":");
					
					if (data[1].toUpperCase() == letter && data[2].toUpperCase() == modifier)
					{
						if (modifier != 1)
							shortcutType = data[4];
						else
							shortcutType = "B";
							
						eval("_SCS_" + data[4] + "_processShortcutKey(data[0], shortcutType)");
						return _SCS_cancelEventBubble(event_);
					}
				}
			}
		}  
	}
}

function _SCS_setPosition (e, obj, xcoord, xoffset, ycoord, yoffset)
{  
	var style = obj.style;
	
	style.pixelLeft = xcoord + xoffset;
	style.pixelTop = ycoord + yoffset;
}

function _SCS_execClickEvent(id)
{
	var obj = _SCS_getObject(id);
	obj.click();
}

function _SCS_getObject(id) 
{  
	var obj;
  
	if (id == null || id == "")
		return null;
		
	if (_isDOM){
		obj = document.getElementById(id);
	}
	//else if (_isIE4){
	//	obj = document.all(id)
	//}
	else if (_isNN4){
		obj = _SCS_seekLayer(document, id);
	}
	return obj;
}

function _SCS_seekLayer(objDoc, name)
{
	var obj;
	
	for (var i=0; i < objDoc.layers.length; i++)
	{
		if (objDoc.layers[i].name == name)
		{
			obj = objDoc.layers[i];			
			break;
		}
		// dive into nested layers if necessary
		if (objDoc.layers[i].document.layers.length > 0)
		{
			obj = _SCS_seekLayer(document.layers[i].document, name);
		}
	}
	return obj;
}

function _SCS_setVisibility(obj, visibleFlag)
{ 	
	if (obj == null)
		return
		
	if (visibleFlag)
		obj.style.visibility = "visible";
	else
		obj.style.visibility = "hidden";
}

function _SCS_getEventCoordinates(e) 
{   
	var event_ = _SCS_getEvent(e);
	var target = _SCS_getEventTarget(event_);
		
	var xcoord = event_.clientX;
	var ycoord = event_.clientY;

	var scrollSpace = _SCS_getScrollSpace();	
	
	xcoord += document.body.scrollLeft + scrollSpace[1];
	ycoord += document.body.scrollTop + scrollSpace[0];
	
	return new Array(xcoord, ycoord);
}

function _SCS_setClass(id, className)
{ 
	var obj = _SCS_getObject(id);
		
	if (obj != null && obj.className != className)	
		obj.className = className;
}

function _SCS_getParentNode(obj, tagName, numChecks)
{
	if (obj == null) return null;
			
	for (var i = 0; i < numChecks; i++) 
	{				
		obj	= obj.parentNode;
		
		if (obj == null)
			return null;
						
		if (obj.tagName == tagName) 
			return obj;
	}	
}

function _SCS_getChildNode(obj, tagName, numChecks)
{
	if (obj == null) return null;
	
	for (var i = 0; i < numChecks; i++) 
	{		
		if (i == 0)	  
			obj	= obj.firstChild;
		else
			obj = obj.nextSibling;
			
		if (obj == null)
			break;
							
		// skip text nodes
		if (obj.nodeType == 3)   
		{
			obj = obj.nextSibling;
			
			if (obj == null)
				break;
		}
		
		// skip breaks and rules
		if (obj.tagName == "BR"  || obj.tagName == "HR") 
		{
			obj = obj.nextSibling;
			
			if (obj == null) 
				break;
		}
		
		if (obj.tagName == tagName) 
			return obj;
	}
	return null;			
}

function _SCS_getNextNode(obj, numChecks, skipTextNodes)
{
	if (obj == null) return null;
	
	for (var i = 0; i < numChecks; i++) 
	{		
		obj = obj.nextSibling;
			
		if (obj == null) 
			break;
					
		// skip text nodes
		if (skipTextNodes && obj.nodeType == 3)   
			continue;		
		
		if (obj != null)
			return obj;
	}
	return null;			
}

function _SCS_getPrevNode(obj, numChecks, skipTextNodes)
{
	if (obj == null) return null;
	
	for (var i = 0; i < numChecks; i++) 
	{		
		obj = obj.previousSibling;
		
		if (obj == null) 
			break;
							
		// skip text nodes
		if (skipTextNodes && obj.nodeType == 3)   
			continue;		

		if (obj != null)
			return obj;
	}
	return null;			
}

function _SCS_getNestedNode(obj, tagName, position)
{
	if (obj == null) return null;
	
	var nodes = obj.getElementsByTagName(tagName);
	
	if (position <= nodes.length)
		return nodes[position - 1];
		
	return null;
}

function _SCS_getNodeCount(collection, tagName)
{
	var count = 0;
	for (var i = 0; i < collection.length; i++)
	{
		if (collection[i].tagName == tagName)
			count++;
	}
	return count;
}

function _SCS_getEvent(e)
{
	// the IE event object is global, unlike all other browsers,
	// which require event to be passed as an argument
	if (_isIE5)
		return event;

	return e;
}

function _SCS_getEventTarget(e)
{
	if (e == null) 
		return null;
	
	var target = (e.target) ? e.target : ((e.srcElement) ? e.srcElement : null);
	return target;
}

function _SCS_getScrollSpace()
{
	var scrollTop = document.body.scrollTop ? document.body.scrollTop : 
		document.documentElement.scrollTop;
		
	var scrollLeft = document.body.scrollLeft ? document.body.scrollLeft : 
		document.documentElement.scrollLeft;
		
	return new Array(scrollTop, scrollLeft);
}	

function _SCS_cancelEventBubble(e)
{
	if (_isIE5)
	{
		e.cancelBubble = true;		
		e.returnValue = false;
	}
	else if (e.cancelable)
	{
		e.stopPropagation();
	}
	return false;
}

function _SCS_clickAnchor(anchor)
{
	if (anchor != null)
	{
		var urlTarget = anchor.target.toLowerCase();
			
		if (urlTarget.length == 0)
		{
			self.location.href = anchor.href;
		}
		else if (urlTarget == "_top")
		{
			top.location.href = anchor.href;
		}
		else if (urlTarget == "_blank" || urlTarget == "_window")
		{
			window.open(anchor.href, "", "");
		}
		else
		{
			var frame = _SCS_findWindowFrame(top);
			
			if (frame != null)
			{
				frame.location.href = anchor.href;
			}
			else
			{
				window.open(anchor.href, "", "");
			}
		}
		_SCS_cancelEventBubble(event);
	}
}

function _SCS_findWindowFrame(currentWindow, name)
{
	for (var i = 0; i < currentWindow.frames.length; i++)
	{
		var frame = null; 
				
		if (currentWindow.frames[i].name.toUpperCase() == name.toUpperCase())
		{
			frame = currentWindow.frames[i];
			break;
		}
		// lets worry about recursion later
		//else if (currentWindow.frames[i].frames.length > 0)
		//{
		//	frame = _SCS_findWindowFrame(currentWindow.frames[i], name)
		//}		
	}
	return frame;	
}

function _SCS_execMouseOverEvent(object)
{
	if (object.onmouseover != null)
		object.onmouseover();
}

function _SCS_execMouseOutEvent(object)
{
	if (object.onmouseout != null)
		object.onmouseout();
}

function _SCS_buildBacker(id)
{
	var html = "<i"+"fr"+"ame id='" + id + "' src='javascript:false;' scrolling='no' frameborder='0' ";
	html += "style='position:absolute;top:0px;left:0px;width:0;height:0;displaynone;'></i"+"fr"+"ame>";
	
	return html;
}

function _SCS_sizeBacker(backer, div)
{
	backer.style.width = div.offsetWidth;
	backer.style.height = div.offsetHeight;
	backer.style.left = div.offsetLeft;
	backer.style.top = div.offsetTop;
	backer.style.zIndex = div.style.zIndex - 1;
}

function _SCS_hideBacker(obj)
{
	if (obj == null) return;
	
	if (_isIE5)
	{
		var backer = document.getElementById(obj.id + "_fr");
		
		if (backer != null)
			backer.style.display = "none";
	}
}

function _SCS_insertGroupBacker(obj)
{
	if (obj == null) return;
		
	var backer = null;
	var id = obj.id + "_fr";
	
	if (_isIE5)
	{
		backer = document.getElementById(id);
			
		if (backer == null)
		{
			var html = _SCS_buildBacker(id);
			obj.insertAdjacentHTML("beforeBegin", html);				
			
			backer = document.getElementById(id);			
		}
		
		if (backer != null)
		{
			_SCS_sizeBacker(backer, obj);
			
			if (obj.style.visibility == "visible")
				backer.style.display = "block";
		}
	}
	return backer;
}

function _SCS_debugClear()
{
	if (debug && document.forms[0].debug != null)
		document.forms[0].debug.value = "";
}

function _SCS_debugPrint(text)
{
	if (debug && document.forms[0].debug != null)
		document.forms[0].debug.value = text + "\r\n" + document.forms[0].debug.value;
}