//
function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined')
	{
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('onload', fn);
	}
	else
	{
		var oldfn = window.onload;
		if (typeof window.onload != 'function')
		{
			window.onload = fn;
		}
		else
		{
			window.onload = function()
			{
				oldfn();
				fn();
			};
		}
	}
}
//
function addClass(target, classValue)
{
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	if(!pattern.test(target.className))
	{
		if(target.className == '')
		{
			target.className = classValue;
		}
		else
		{
			target.className += " " + classValue;
		}
	}
	return true;
}
//
function removeClass(target, classValue)
{
	var removedClass = target.className;
	var pattern = new RegExp("(^| )" + classValue + "( |$)");

	removedClass = removedClass.replace(pattern, "$1");
	removedClass = removedClass.replace(/ $/, "");
	target.className = removedClass;
	return true;
}
//
function getElementsByAttribute(attribute, attributeValue)
{
	var elementArray = new Array();
	var matchedArray = new Array();
	if(document.all)
	{
		elementArray = document.all;
	}
	else
	{
		elementArray = document.getElementsByTagName("*");
	}
	for(var i = 0; i < elementArray.length; i++)
	{
		if(attribute == "class")
		{
			var pattern = new RegExp("(^| )" + attributeValue + "( |$)");
			if(pattern.test(elementArray[i].className))
			{
				matchedArray[matchedArray.length] = elementArray[i];
			}
		}
		else if(attribute == "for")
		{
			if(elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for"))
			{
				if(elementArray[i].htmlFor == attributeValue)
				{
					matchedArray[matchedArray.length] = elementArray[i];
				}				
			}
		}
		else if(elementArray[i].getAttribute(attribute) == attributeValue)
		{
			matchedArray[matchedArray.length] = elementArray[i];
		}
	}
	return matchedArray;
}
//
function getViewportSize()
{
	var size = [0,0];
	if(typeof window.innerWidth != 'undefined')
	{
		size = [window.innerWidth,
				window.innerHeight];
	}
	else if(typeof document.documentElement != 'undefined' 
		&& typeof document.documentElement.clientWidth != 
		'undefined' && typeof document.documentElement.clientWidth != 0)
	{
		size = [document.documentElement.clientWidth,
				document.documentElement.clientHeight];
	}
	else
	{
		size = [document.getElementsByTagName('body')[0].clientWidth,
				document.getElementsByTagName('body')[0].clientHeight];
	}
}
//
function getScrollingPosition()
{
	var position = [0,0];
	
	if(typeof window.pageYOffset != 'undefined')
	{
		position = [window.pageXOffset, window.pageYOffset];
	}
	else if(typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0 || document.documentElement.scrollLeft > 0)
	{
		position = [document.documentElement.scrollLeft, document.documentElement.scrollTop];		
	}
	else if(typeof document.body.scrollTop != 'undefined')
	{
		position = [document.body.scrollLeft, document.body.scrollTop];				
	}
	
	return position;
}
//
function getCookie(searchName)
{
	var cookies = document.cookie.split(";");
	for(var i = 0; i < cookies.length; i++)
	{
		var cookieCrumbs = cookies[i].split("=");
		var cookieName = cookieCrumbs[0];
		var cookieValue = cookieCrumbs[1];
		if(cookieName == searchName)
		{
			return cookieValue;
		}
	}
	return false;
}
//
function getSubCookie(cookieName, subCookieName)
{
	var cookies = document.cookie.split(";");
	for(var i = 0; i < cookies.length; i++)
	{
		var cookieCrumbs = cookies[i].split("=");
		cookieCrumbs[0] = cookieCrumbs[0].replace(/^\s+/, "");
		if(cookieCrumbs[0] == cookieName)
		{
			var cookieValue = cookieCrumbs[1];
			cookieValue = unescape(cookieValue);
			var subCookies = cookieValue.split("/");
			for(var j = 0; j < subCookies.length; j++)
			{
				var subCookieCrumbs = subCookies[j].split(":");
				if(subCookieCrumbs[0] == subCookieName)
				{
					return subCookieCrumbs[1];
				}		
			}
		}
	}
	return false;
}
//
function identifyBrowser()
{
	var agent = navigator.userAgent.toLowerCase();
	
	if(typeof navigator.vendor != 'undefined' &&
	navigator.vendor == 'KDE' &&
	typeof window.sidebar != 'undefined')
	{
		return 'kde';
	}
	else if(typeof window.opera != 'undefined')
	{
		var version = parseFloat(agent.replace(/.*opera[\/ ]([^ $]).*/, "$1"));
		
		if(version >= 7)
		{
			return 'opera7';
		}
		else if(version >= 5)
		{
				return 'opera5';
		}
		return false;
	}
	else if(typeof document.all != 'undefined')
	{
		if(typeof document.getElementById != 'undefined')
		{
			var browser = agent.replace(/.*ms(ie[\/ ][^ $]+).*/, '$1').replace(/ /, '');
			
			if(typeof document.uniqueID != 'undefined')
			{
				if(browser.indexOf('5.5') != -1)
				{
					return browser.replace(/(.*5\.5).*/, '$1');
				}
				else
				{
					return browser.replace(/(.*)\..*/, '$1');
				}
			}
			else
			{
				return 'ie5mac';
			}
		}
		return false;
	}
	else if(typeof document.getElementById != 'undefined')
	{
		if(navigator.vendor.indexOf('Apple Computer, Inc.') != -1)
		{
			if(typeof window.XMLHttpRequest != 'undefined')
			{
				return 'safari1.2';
			}
			return 'safari1';
		}
		else if(agent.indexOf('gecko') != -1)
		{
			return 'mozilla';
		}
	}
	return false;
}
//
function attachEventListener(target, eventType, functionRef, capture)
{
	if(typeof target.addEventListener != 'undefined')
	{
		target.addEventListener(eventType, functionRef, capture);
	}
	else if(typeof target.attachEvent != 'undefined')
	{
		target.attachEvent('on' + eventType, functionRef);
	}
	else
	{
		eventType = 'on' + eventType;
		
		if(typeof target[eventType] == 'function')
		{
			var oldListener = target[eventType];
			
			target[eventType] = function()
			{
				oldListener();
			
				return functionRef();
			};			
		}
		else
		{
			target[eventType] = functionRef;
		}
	}
}
//
function detachEventListener(target, eventType, functionRef, capture)
{
	if(typeof target.removeListener != 'undefined')
	{
		target.removeEventListener(eventType, functionRef, capture);
	}
	else if(typeof target.detachEvent != 'undefined')
	{
		target.detachEvent('on' + eventType, functionRef);
	}
	else
	{
		target['on' + eventType] = null;
	}
}
//
function getEventTarget(event)
{
	var targetElement = null;
	
	if(typeof event.target != 'undefined')
	{
		targetElement = event.target;
	}
	else
	{
		targetElement = event.srcElement;
	}
	
	while(targetElement.nodeType == 3 && targetElement.parentNode != null)
	{
		targetElement = targetElement.parentNode;
	}
	
	return targetElement;
}
//
function getTarget(event, elem)
{ 
	if (typeof event == "undefined")
	{
    	event = window.event;
	}
	
	var target = getEventTarget(event);

	while (target.nodeName.toLowerCase() != elem)
	{
		target = target.parentNode;
	}
	
	return target;
}
//
function stopDefaultAction(event)
{
	event.returnValue = false;
	
	if(typeof event.preventDefault != 'undefined')
	{
		event.preventDefault();
	}
}
//
function stopEvent(event)
{
	if(typeof event.stopPropagation != 'undefined')
	{
		event.stopPropagation();
	}
	else
	{
		event.cancelBubble = true;
	}
}
//
function getPosition(theElement)
{
	var positionX = 0;
	var positionY = 0;
	
	while(theElement != null)
	{
		positionX += theElement.offsetLeft;
		positionY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}
	
	return [positionX, positionY];
}
//
function makePopup(url, width, height, overflow){
	if(width > 640)
	{
		width = 640;
	}
	if(height > 480)
	{
		height = 480;
	}
	if (overflow == '' || !/^(scroll|resize|both)$/.test(overflow))
	{
		overflow = 'both';
	}

	var win = window.open(url, '',
      'width=' + width + ',height=' + height
      + ',scrollbars=' + (/^(scroll|both)$/.test(overflow) ? 'yes' : 'no')
      + ',resizable=' + (/^(resize|both)$/.test(overflow) ? 'yes' : 'no')
      + ',status=yes,toolbar=yes,menubar=yes,location=yes'
  	);
	win.blur();
	return win;
}
//
function externalLink()
{
	document.onclick = function(e)
	{
  		var target = e ? e.target : window.event.srcElement;

		while (target && !/^(a|body)$/i.test(target.nodeName))
		{
			target = target.parentNode;
		}

		if (target && target.getAttribute('rel') && target.rel == 'external')
		{
			var external = window.open(target.href);
			return external.closed;
		}
	}
}
//
function xhrObject(){
	this.req = null;
	this.url = null;
	this.method = 'GET';
	this.async = true;
	this.status = null;
	this.statusText = '';
	this.postData = null;
	this.readyState = null;
	this.responseText = null;
	this.responseXML = null;
	this.handleResp = null;
	this.responseFormat = 'text'; // 'text', 'xml', or 'object'
	this.mimeType = null;
	//
	this.init = function()
	{
		if (!this.req)
		{
			try
			{
				// Try to create object for Firefox, Safari, IE7, etc.
				this.req = new XMLHttpRequest();
			}
			catch (e)
			{
				try
				{
					// Try to create object for later versions of IE.
					this.req = new ActiveXObject('MSXML2.XMLHTTP');
				}
				catch (e)
				{
					try
					{
						// Try to create object for early versions of IE.
						this.req = new ActiveXObject('Microsoft.XMLHTTP');
					}
					catch (e)
					{
						// Could not create an XMLHttpRequest object.
						alert('alert');
						return false;
					}
				}
			}
		}
		return this.req;
	};
	//
	this.doReq = function()
	{
		if(!this.init())
		{
			alert('Could not create xmlHttpRequest object.');
			return;
		}
		
		this.req.open(this.method, this.url, this.async);
		
		if(this.method == 'POST')
		{
			this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		}
		
		var self = this;
		
		this.req.onreadystatechange = function()
		{
			var resp = null;
			if(self.req.readyState == 4)
			{
				switch(self.responseFormat)
				{
					case 'text':
						resp = self.req.responseText;
						break;
					case 'xml':
						resp = self.req.responseXML;
						self.removeWhitespace(resp);
						break;
					case 'object':
						resp = req;
						break;
				}
				if(self.req.status >= 200 && self.req.status <= 299)
				{
					self.handleResp(resp);
					delete xhrObject;
					xhrObject = null;
				}
				else
				{
					self.handleErr(resp);
				}
			}
		};
		this.req.send(this.postData);
	};
	//
	this.removeWhitespace = function(xmlDoc)
	{
		var i;
		for (i = 0; i < xmlDoc.childNodes.length; i++)
		{
			var currentNode = xmlDoc.childNodes[i];
		
			if (currentNode.nodeType == 1)
			{
				this.removeWhitespace(currentNode);
			}
		
			if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3))
			{
				xmlDoc.removeChild(xmlDoc.childNodes[i--]);
			}
		}
	};
	//
	this.setMimeType = function(mimeType)
	{
		this.mimeType = mimeType;
	};
	//
	this.handleErr = function() 
	{
		var errorWin;
		try
		{
			errorWin = window.open('', 'errorWin');
			errorWin.document.body.innerHTML = this.responseText;
		}
		catch (e) 
		{
			alert('An error occurred, but the error message cannot be '
			+ 'displayed. This is probably because of your browser\'s '
			+ 'pop-up blocker.\n'
			+ 'Please allow pop-ups from this web site if you want to '
			+ 'see the full error messages.\n'
			+ '\n'
			+ 'Status Code: ' + this.req.status + '\n'
			+ 'Status Description: ' + this.req.statusText);
		}
	};
	//
	this.abort = function()
	{
		if(this.req)
		{
			this.req.onreadystatechange = function(){};
			this.req.abort();
			this.req = null;	
		}		
	};
	//
	this.doGet = function(url, hand, format)
	{
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.doReq();		
	};
	//
	this.doPost = function(url, postData, hand, format)
	{
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.method = 'POST';
		this.postData = postData;
		this.doReq();
	}
}
//