/* 
 * Zeus JS
 * By George E. Papadakis
 * Version : 26 July 04
*/

/* Commonly-used variables */
var d			= document;
var dCookie		= d.cookie;

// Commonly-used functions, reduced.
function D(s)		{return document.getElementById(s);}
function DE(o,s)	{return o.getElementsByTagName(s);}


/**
 * getStyle()
 *
 * Gets the computed style of any object.
 *
 * WARNING: Produces unexpected results in Safari.  To achieve best 
 * results, explicitly set the style property for that browser when the 
 * element is rendered.
 *
 * Typical usage:
 * getStyle(object, "display");
 */
function GetStyle(el, style) {
  if (!document.getElementById || !el) return;
  
  if (document.defaultView
      && document.defaultView.GetComputedStyle) {
      return document.defaultView.
        GetComputedStyle(el, "").GetPropertyValue(style);
  }  
  else if (el.currentStyle) {
    return el.currentStyle[style];
  }  
  else { 
    return el.style.display;
  }
}

/* ToggleDisplay ()
 *
 * Will toggle the display property of the style object for any supporting element.
 */
function ToggleDisplay(o)
{
	var display = GetStyle(o, "display");
	if (o.style)
		o.style.display =
			(display != "none") ? "none" : GetDisplayStyleByTagName(o);
}

/* GetDisplayStyleByTagName()
 *
 * Will retrieve the display style of a DOM object based on its type
 */
function GetDisplayStyleByTagName(o)
{
	n = o.nodeName.toLowerCase();
	return
	(
		n == "span"
		|| n == "img"
		|| n == "a"
	)
	? "inline" : "block";

}

/* HideElement ()
 *
 * Hides an element.
 */
function HideElement(o)
{
	if (o && o.style) o.style.display = "none";
}   

/* ShowElement ()
 *
 * Shows an elements thats hidden.
 */
function ShowElement(o)
{
	if (o && o.style) o.style.display = GetDisplayStyleByTagName(o);
}

/* GetElement()
 *
 * Retrieve an element based on its id. Alert if unable
 */
function GetElement(id) {
	var e = D(id);
	if (!e)
	{
		alert("Cannot get element: " + id);
	}
	return e;
}

/* setInnerHTML()
 *
 * Sets the innerHTML of an element or shows an alert if can't be set.
 *
 * Typical usage:
 * setInnerHTML("the-id-of-the-element");
 */
function SetInnerHTML(id, html)
{
	try
	{
		GetElement(id).innerHTML = html;
	}
	catch (ex)
	{
	   	alert("Cannot set inner HTML: " + id);
	}
}

/* ShowProps()
 *
 * Displays all the properties for a givven element
 */
function ShowProps(o)
{
	s=""; for (p in o)
	{
		s+=p+": "+o[p]+"\n<br />";
	}
	document.write(s);
}
 
/* GetLabel ()
 *
 * Retrieve the label element for an input element 
 */
function GetLabel(obj)
{
	thisLabel = obj.parentNode.parentNode.getElementsByTagName("label")[0];
	return thisLabel;
}

/* CreateElementandAppend ()
 *
 * Creates an element and Appends it to another 
 */
function CreateElementandAppend(nodeName, strId, appendTo)
{
	var el = document.createElement(nodeName);
	el.setAttribute("id", strId);
	if (appendTo)
	{
		appendTo.appendChild(el); 
	 }
	 else
	 {
	    document.body.appendChild(el); 
	  }
	return el; 
}

/* CreateElementandInsertBefore ()
 *
 * Creates an element and inserts it before another
 */
function CreateElementandInsertBefore(nodeName, strId, appendTo, sibling)
{
	var el = document.createElement(nodeName);
	el.setAttribute("id", strId);
	if (appendTo)
	{
		appendTo.insertBefore(el, sibling); 
  	}
	else
	{
		document.body.insertBefore(el, sibling); 
	}
	return el; 
}


/* GetXY()
 *
 * Returns the position of any element as an object.
 *
 * Typical usage:
 * var pos = getXY(object);
 * alert(pos.x + " " +pos.y);
 */
function GetXY(el)
{
	var x = el.offsetLeft;
	var y = el.offsetTop;
	if (el.offsetParent != null)
	{
		var pos = GetXY(el.offsetParent);
		x += pos.x;
		y += pos.y;
	}
	return {x: x, y: y}
}


/* AttacheHandlers()
 * Attaches onblur handlers to all text and textarea elements of forms and then trims them 
 */
function AttachHandlers ()
{
	var el , thisForm, formes = document.forms;
	/* Attach Handlers to form elements */
	return;
	for (var f = 0 ; (thisForm = formes[f]) ; f++)
	{
		/* We could add a onsubmit validate handler here */
		
		/* Attache handlers to elements */
		for (var i = 0 ; (el = thisForm.elements[i]) ; i++)
		{
			/* Optional fields indication  | gp */
			if (el.parentNode.tagName == "TD" && el.id.indexOf("optional") == -1 && el.id != "")
			{
				//el.parentNode.className = "required";
			}
			switch (el.type)
			{
				case "text" :
				case "textarea" :
				case "password" :
				
				/* Google toolbar yellow fix */
				//if (thisClass == el.className)
				//{
				//	el.className = thisClass + " googleFix";
				//}
				//el.className = thisClass + " googleFix";

				//else	el.className = "googleFix";
				
				//el.onblur = el.onfocus = function () {Flash(this);}
				break;
			}
		}
	}
}



/* This function scans a radio|checkbox element and see if it has selected items */
function IsValidSel(el)
{
	hidden = el.name;
	hidden = hidden.match (/^q_(.*)(_|$)?/i);
	hidden = hidden[1];
	hidden = "" + hidden.replace (/_.*$/i, "");
	hidden = "dt_" + hidden;
	if (h = document.getElementById(hidden))
	{
		if (h.style.display == "none")
			return true;
	}
	el = document.getElementsByName(el.name);
	for (i = 0 ; i < el.length; i++)
	{
		if (el[i].checked == true)
			return true;
	}
	return false;
}


/* Checked if a form element is valid */
function IsValid(w)
{
	/* optional [?] */
	re = /optional$/;
	if (re.test (w.id))  return true;

	valid = (w.type == "checkbox" || w.type == "radio" ? w.checked : w.value);
	if (w.type == "text")
	{
		if (w.value)
			valid = true;
	}
	
	if (!valid) valid = false;
	return valid;
}


/* Focus | Blur Functions */
function Flash(obj)
{
	isFocused = this.focused;
	if (typeof(isFocused) == "undefined")
		obj.focused = true;
	if (isFocused == true) obj.focused = false;
	else obj.focused = true;

	if (!obj.isFocused)
		obj.value = obj.value.trim();

	return;
	/* We dont want any table/td handling here */

	/* Handle input */
	thisClass = obj.className;
	if (thisClass.indexOf("activeInput") != -1)
		obj.className = thisClass.replace(/ ?activeInput$/gi,"");
	else 
		obj.className = thisClass + " " + "activeInput";


	
	/* Handle label */	
	thisLabel = GetLabel(obj);
	if (thisLabel)
	{
		thisClass = thisLabel.className;
		if (thisClass.indexOf("activeLabel") != -1)
		{
			//thisLabel.className = thisClass.replace(/? activeLabel$/gi,"");
		}
		else 
			thisLabel.className = thisClass + " " + "activeLabel";	
	}
	thisClass = "";
}



/* PROTOTYPE : Trim protoypes trims a strins = removes all whitespace from the begging and end of the string */
String.prototype.trim = function ()
{
	return this.replace(/^\s*|\s*$/g, "")
}

/* PROTOTYPE : pushes elements to an array */
Array.prototype.push = function(v)
{
	this[this.length] = v
	return v
}

/* GetCookie()
 * 
 * Retrieve a cookie value based on its name
 */
function GetCookie(name)
{
	
	var index = dCookie.indexOf(name + "=");
	if (index == -1) return null;
	index = dCookie.indexOf("=", index) + 1; // first character
	var endstr = dCookie.indexOf(";", index);
	if (endstr == -1) endstr = dCookie.length; // last character
	return unescape(dCookie.substring(index, endstr));
}


/* Load handlers (if any) on load */
//window.onload = AttachHandlers;   
