//----------------------------------------------------------------------//
//	General Javascript functions/classes								//
//		Full documentation to come										//
//																		//
//		Dependencies: none												//
//		Known Issues: HighLight and unHighLight are sometimes cannot	//
//						be found.										//
//----------------------------------------------------------------------//

//----------------------------------------------------------------------//
//	isUndefined function												//
//		Defines isUndefined for browsers that don't implement it		//
//		@return true when the object is undefined, otherwise false		//
//----------------------------------------------------------------------//
function isUndefined(v) {
    var undef;
    return v===undef;
}

//----------------------------------------------------------------------//
// DOM Functions for showing and hiding elements						//
//		showElement			=? Shows the element with id element		//
//		showElementInline 	=? Same as showElement, except the element	//
//							=? will be displayed inline					//
//		hideElement			=? hides an element by removing entirely	//
//							=? from the document flow					//
//																		//
//	@param the id string												//
//	@return none														//
//----------------------------------------------------------------------//
function showElement(element) { document.getElementById(element).style.display = 'block'; }
function showElementInline(element) { document.getElementById(element).style.display = 'inline'; }
function hideElement(element) { document.getElementById(element).style.display = 'none'; }

function toggleElement(element) {
	var node = document.getElementById(element);
	if (node.style.display != 'none') node.style.display = 'none';
	else node.style.display = 'block';
}

function toggleElementInline(element) {
	var node = document.getElementById(element);
	if (node.style.display != 'none') {
		node.style.display = 'none';
		node.blur();
	} else {
		node.style.display = 'inline';
		node.focus();
	}
}

//----------------------------------------------------------------------//
// Removes all children from an element object							//
//----------------------------------------------------------------------//
function removeAllChildren(obj) {
	if (!obj) return null;
	
	while (obj.firstChild) {
		obj.removeChild(obj.firstChild);	
	}
}

//----------------------------------------------------------------------//
// DOM Functions for focusing elements									//
//----------------------------------------------------------------------//
function FocusElement(element)
{
	var node = document.getElementById(element);
	if (node) node.focus();
}

//----------------------------------------------------------------------//
//	Deselect function													//
//----------------------------------------------------------------------//
function Deselect(element)
{
	var node=document.getElementById(element);

	for (var i=0;i < node.length; i++) {
		node.options[i].selected = false;
	}
}

//----------------------------------------------------------------------//
//	Popup functions														//
//		These popup functions are designed with usuability in mind		//
//		I.e. They do not mangle the href attribute of the 'a' element	//
//		They also degrade well when javascript is off and will behave	//
//		like standard popups.											//
//																		//
//		Usuage:															//
//			<a href="www.google.co.nz" target="_blank" 					//
//				onClick="link_popup(this); return false;">CLICK ME</a>	//
//																		//
//		The functions can also be used on elements other than links		//
//		<input onClick="raw_popup('www.google.co.nz'); return false;"	//
//																		//
//		Popup Features:													//
//			Both link_popup and raw_popup can be passed a features 		//
//			string. This is the standard html popup string. When		//
//			no custom features are passed the default (_POPUP_FEATURES)	//
//			will be used.												//
//----------------------------------------------------------------------//

// Default popup features
var _POPUP_FEATURES = 'location=0, statusbar=0, menubar=0, width=700px,  height=650px';

//----------------------------------------------------------------------//
//	raw_popup function													//
//	@param string path to the popup										//
//	@param html popup target, defaults to _blank						//
//	@param features string (see popup functions note)					//
//	@return the window object											//
//----------------------------------------------------------------------//
function raw_popup(url, target, features) {
  if (isUndefined(features)) features = _POPUP_FEATURES;
  if (isUndefined(target))  target = '_blank';

  var theWindow = window.open(url, target, features);
  theWindow.focus();
  return theWindow;
}

//----------------------------------------------------------------------//
// link_popup function													//
//	@param string path to the popup										//
//	@param features string (see popup functions note)					//
//	@return the window object											//
//----------------------------------------------------------------------//
function link_popup(src, features) {
    return raw_popup(src.getAttribute('href'),  src.getAttribute('target') || '_blank',  features);
}

//----------------------------------------------------------------------//
//	fixE function														//
//		Documentation to come											//
//----------------------------------------------------------------------//
function fixE(e)
{
	if (typeof e == 'undefined') e = window.event;
	return e;
}

var today = new Date(); 
var defaultExpiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days 


/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, path, expires, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

//----------------------------------------------------------------------//

function getEvntParent(e) {
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
		
	return targ;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getPageEventCoords(evnt) {
	var coords = {'left':0, 'top':0};
	
	if (evnt.pageX) {
		coords.left = evnt.pageX;		
		coords.top = evnt.pageY;
	} else if (evnt.clientX) {
		coords.left = evnt.clientX + document.body.scrollLeft - document.body.clientLeft;
		coords.top  = evnt.clientY + document.body.scrollTop  - document.body.clientTop;
		
		// Include HTML element space, if applicable
		if (document.body.parentElement && document.body.parentElement.clientLeft) {
			var bodParent = document.body.parentElement;
			coords.left += bodParent.scrollLeft - bodParent.clientLeft;
			coords.top  += bodParent.scrollTop  - bodParent.clientTop;
		}
	}
	return coords;
}

function setSelectionRange(obj, selectionStart, selectionEnd) {
  if (obj.setSelectionRange) {
    obj.focus();
    obj.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (obj.createTextRange) {
    var range = obj.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}