/*

Global variable scope, use this object instead of defining "worldwide global" variables

*/

var dw = {};

/*

Common, useful methods, thingies

*/

var df = {};

//-* A shorter :P getElementById
df.gid = function (obj)
{
	//It's a string, so get the object
	if (typeof obj == "string") obj = document.getElementById(obj);

	return obj;
} //function

//-* (Un)Select all the checkboxes inside a given form
df.chkboxSelectAll = function (formname, status)
{
	obj = df.gid(formname);

	for (var i=0; i < obj.elements.length; i++) 
	{
		obj.elements[i].checked = status;
	} //for
} //function

//-* 
df.radioChecked = function (formname,group)
{
	nodeList = df.gid(formname)[group];

	for (var i=0; i < nodeList.length; i++) 
	{
		if (nodeList[i].type == 'radio')
		{
			if (nodeList[i].checked == true)
				return true;
		} //if
	} //for
	
	return false;
}


/*-*-*-*	EVENTS		*/

//Very basic shit

df.addEvent = function(oTarget, eType, fn, useCapture) 
{
	var oTarget = df.gid(oTarget);

	if (oTarget.addEventListener) 
	{
		oTarget.addEventListener(eType, fn, useCapture);
		return true;
	}
	else if (oTarget.attachEvent) 
	{
		var r = oTarget.attachEvent('on' + eType, fn);
		return r;
	}
	else 
	{
		oTarget['on' + eType] = fn;
	} //if
} //function

/*-*-*-*	VISIBILITY		*/

df.vis = {};

df.vis.display = function(e, status)
{
	if (status)
		df.gid(e).style.display = '';
	else
		df.gid(e).style.display = 'none';
} //function

df.vis.displayToggle = function (e1,e2, eActive)
{
	//eActive == 0 => e1 will be shown, e2 wont
	if (eActive == 0)
	{
		df.vis.display(e1,true);
		df.vis.display(e2,false);
	}
	else
	{
		df.vis.display(e1,false);
		df.vis.display(e2,true);
	} //if
} //function

df.vis.ible = function(e, status)
{
	if (status)
		df.gid(e).style.visibility = 'visible';
	else
		df.gid(e).style.visibility = 'hidden';
} //function

df.vis.ibleToggle = function (e1,e2, eActive)
{
	//eActive == 0 => e1 will be shown, e2 wont
	if (eActive == 0)
	{
		df.vis.ible(e1,true);
		df.vis.ible(e2,false);
	}
	else
	{
		df.vis.ible(e1,false);
		df.vis.ible(e2,true);
	} //if
} //function


/**
 * df._css.js: utilities for manipulating the CSS class of an HTML element.
 *
 * This module defines a single global symbol named df._css. This object
 * contains utility functions for working with the class attribute (className
 * property) of HTML elements. All functions take two arguments: the element
 * e being tested or manipulated and the CSS class c that is to be tested,
 * added, or removed. If element e is a string, it is taken as an element
 * id and passed to document.getElementById().
 */
df._css = {};

// Return true if element e is a member of the class c; false otherwise
df._css.is = function(e, c) 
{
    if (typeof e == "string") e = document.getElementById(e); // element id

    // Before doing a regexp search, optimize for a couple of common cases.
    var classes = e.className;
    if (!classes) return false;    // Not a member of any classes
    if (classes == c) return true; // Member of just this one class

    // Otherwise, use a regular expression to search for c as a word by itself
    // \b in a regular expression requires a match at a word boundary.
    return e.className.search("\\b" + c + "\\b") != -1;
};

// Add class c to the className of element e if it is not already there.
df._css.add = function(e, c) 
{
    if (typeof e == "string") e = document.getElementById(e); // element id
    if (df._css.is(e, c)) return; // If already a member, do nothing
    if (e.className) c = " " + c;  // Whitespace separator, if needed
    e.className += c;              // Append the new class to the end
};

// Remove all occurrences (if any) of class c from the className of element e
df._css.remove = function(e, c) 
{
    if (typeof e == "string") e = document.getElementById(e); // element id
    // Search the className for all occurrences of c and replace with "".
    // \s* matches any number of whitespace characters.
    // "g" makes the regular expression match any number of occurrences
    e.className = e.className.replace(new RegExp("\\b"+ c+"\\b\\s*", "g"), "");
};

