/* 
 * General DOM API
 *
 * $Id: api.js,v 1.4 2006/05/27 01:01:03 dread Exp dread $
 *
 */ 

/*
 * $Log: api.js,v $
 */



/*
// minimal support for DOM or one of the intermediate DOMs

var IE = (document.all) ? 1 : 0;
var DOM = (document.getElementById) ? 1 : 0;
var NS4 = (document.layers) ? 1 : 0;
var MAC = ((navigator.appVersion.indexOf("PPC") >0) || (navigator.appVersion.indexOf("Mac") >0)) ? 1 : 0;

var DHTML = (DOM || IE || NS4)

*/

function isFunction(a) { return typeof a == 'function'; }

function isObject(a) { return (a && typeof a == 'object') || isFunction(a); }

function isArray(a) { return isObject(a) && (a instanceof Array);}

function isDate(a) { return isObject(a) && (a instanceof Date);}

function isBoolean(a) { return typeof a == 'boolean'; }

function isNull(a) { return typeof a == 'object' && !a; }

function isNumber(a) { return typeof a == 'number' && isFinite(a); }

function isString(a) { return typeof a == 'string'; }

function isUndefined(a) { return typeof a == 'undefined'; } 

function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}


function addLoadEvent(func) {
	var oldfunc = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldfunc();
			func();
		}
	}
}

function getEventTarget(evt, tag) {
	var targ = null;

	if (! tag)
		tag='INPUT';

	if ((evt.target) && (evt.target.nodeName == tag)) {
		targ = evt.target;
	} else if ((evt.srcElement) && (evt.srcElement.nodeName == tag)) {
		targ = evt.srcElement;
	} else if ((evt.originalTarget) && (evt.originalTarget.nodeName == tag)) {
		targ = evt.originalTarget;
	} else if (evt.nodeName == tag) {
		 targ = evt;
	}
	return targ;
}


/*
 * Positioning
 */

function getAbsolutePos(e) {
	e = (isString(e)) ? document.getElementById(e) : e;
	var pos = { x: e.offsetLeft, y: e.offsetTop, w: e.offsetWidth, h: e.offsetHeight };
	/*
	if (e.style.marginTop || e.style.marginLeft) {
		var m = parseInt(e.style.marginLeft);
		pos.x -= m;
		m = parseInt(e.style.marginTop);
		pos.y -= m;
	}
	if (e.offsetParent) {
		var p = getAbsolutePos(e.offsetParent);
		pos.x += p.x;
		pos.y += p.y;
	}
	*/
	if (e.offsetParent) {
		while (e.offsetParent) {
			e = e.offsetParent;
			pos.x += e.offsetLeft;
			pos.y += e.offsetTop;
		}
	}
	/* elseif (e.x) {			(Netscape 4)
		pos.x = e.x;
		pos.y = e.y;
	}
	*/
	return pos;
}

function getMouse(evt) {							 // Get mouse button state, absolute position
	var m = { x: 0, y: 0, btn: 0 };

	if (evt.pageX || evt.pageY) {
		m.x = evt.pageX;
		m.y = evt.pageY;
	} else if (evt.clientX || evt.clientY) {
		m.x = evt.clientX + (document.element.scrollLeft ? document.element.scrollLeft : document.body.scrollLeft);
		m.y = evt.clientY + (document.element.scrollTop ? document.element.scrollTop : document.body.scrollTop);
	}
	m.btn = evt.button;
	return m;
}

var Mouse = {x: 0, y: 0, btn: 0};

function MouseEventHandler(e) {
	var evt = (e) ? e : window.event;
	Mouse = getMouse(evt);
} 

/*
Arrays
*/

// push and shift for IE5

function Array_push() {
	for (var i = 0; i < arguments.length; i++) 
		this[this.length] = arguments[i];
	return this.length;
}

if (typeof Array.prototype.push == "undefined") 
	Array.prototype.push = Array_push;

function Array_shift() {
	var response = this[0];
	for (var i = 0; i < this.length-1; i++) 
		this[i] = this[i + 1];
	this.length--;
	return response;
}

if (typeof Array.prototype.shift == "undefined") 
	Array.prototype.shift = Array_shift;


function fillArray(start, many, val) {
	var size = start + many;
	var a = new Array(size);

	for (var i=start; i < size; i++) {
		a[i] = val;
	}
	return a;
}

/*
 * Strings
 */

function replaceString(s, r, str) { 
	return str.split(s).join(r); 
}

function ltrimString(str) {						// leading white space
	var s = "" + str;
	return s.replace(/^\s+/g, '');
}

function rtrimString(str) {						// trailing white space
	var s = "" + str;
	return s.replace(/\s+$/g, '');
}

function trimString(str) {						// leading & trailing white space
	var s = "" + str;						 
	return s.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function padString(str, ln, c) {					// positive len pad left, negative pad right
	if (! ln)
		return str;
	
	if (! c) 
		c = " ";
	c = "" + c;
	if (c.length < 1)
		return str;

	var s = "" + str;							 // don't know if we got a string or number param
	var len = (ln < 0) ? 0-ln : ln;

	if (ln > 0) {
		while (s.length < len) 
			s = c + s;
	} else {
		while (s.length < len) 
			s = s + c;
	}
	return s;
}

if (typeof String.prototype.ltrim == 'undefined')
	String.prototype.ltrim = function() { return this.replace(/^\s+/g,''); }

if (typeof String.prototype.rtrim == 'undefined')
	String.prototype.rtrim = function() { return this.replace(/\s+$/g,''); }

if (typeof String.prototype.trim == 'undefined')
	String.prototype.trim = function()  { return this.replace(/^\\s+|\\s+$/g,''); }

if (typeof String.prototype.pad == 'undefined')
	String.prototype.pad = function(l,c) { return padString(this, l, c); };

function ordinal(n) {
	var sfx ='th';
	var str = "" + n;
	n = parseInt(n, 10);
	if ((n < 4) || (n > 20)) {
		switch(n % 10) {
			case 1: sfx ='st'; break;
			case 2: sfx ='nd'; break;
			case 3: sfx ='rd'; break;
		}
	}
	return str + sfx;
}

/*
 * HTML Controls
 */

function name2id(name) {
	name = trimString(name);
	if (name.indexOf('[') != -1) {
		var id = name.replace(']', '');
		id = id.replaceString('[', '_');
		id = id.replace("'", '');
		return id;
	}
	return name;
}

/* Emulate document.getElementById on document.all only browsers.
 * stolen from http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html#getEl
 */

if((!document.getElementById) && document.all){
	document.getElementById = function(id){
		var tempEl = null, el = document.all[id];
		if(el){ //document.all returned something.
			if((!el.id)||(el.id != id)){
				/* Either this is a collection or the only element
				   available under the property name provided as the
				   - id - parameter is a named element:
				*/
				if(el.length){ //assume it is a collection.
					/* But it might be an element with a NAME
					   corresponding with the id parameter that has
					   collection-like behaviour such as a form or a
					   select element so proceed with caution:
					*/
					for(var c = 0;c < el.length;c++){
						if((el[c].id)&&(el[c].id == id)){
							/* Set tempEl to the first match and
							   break out of the - for - loop:
							*/
							tempEl = el[c];
							break;
						}
					}
					/* el will be set to null if the loop did not
					   find an element with the corresponding ID
					   because the default null value of tempEl
					   will not have changed:
					*/
					el = tempEl;
				}else{ //only a named element is available for id.
					/* document.getElementById should not return named elements
					   only an IDed element so set el to null:
					*/
					el = null;
				}
			} //else we have our element (the ID matches).
		}else{ //el is undefined so make it null;
			el = null;
		}
		/* The returned value will be the first element confirmed as
		   having the corresponding ID or it will be null:
		*/
		return el;
	};
}

function setContent(e, html) {
	var oldvalue = null;
	var el = (isString(e)) ? document.getElementById(e) : e;
	if (el) {
		if (el.innerHTML) {
			oldvalue = el.innerHTML;
			el.innerHTML = html;
		}
	}
	return oldvalue;

}

function setElementValue(e, v) {
	var oldvalue = null;
	var el = (isString(e)) ? document.getElementById(e) : e;
	if (el) {
		if (el.value) {
		 	oldvalue = el.value;
		 	el.value = v;
		 }
	}
	return oldvalue;
}

function showElement(name) {
	if ((document.layers) && (document.layers[name] != null)) {
		document.layers[name].visibility = 'show';
	} else if (document.all) {
		document.all[name].style.visibility = 'visible';
	}
}

function hideElement(name) {
	if ((document.layers) && (document.layers[name] != null)) {
		document.layers[name].visibility = 'hide';
	} else if (document.all) {
		document.all[name].style.visibility = 'hidden';
	}
}

function showDiv(e) {
	var el = (isString(e)) ? document.getElementById(e) : e;
	if (el)
		el.style.display = 'block';
	return false;
}

function hideDiv(e) {
	var el = (isString(e)) ? document.getElementById(e) : e;
	if (el)
		el.style.display = 'none';
	return false;
}

function getAllChildren(el) {
	return (el.all) ? el.all : el.document.getElementsByTagName('*');
}

function getChildrenByTag(tag) {
	var el = arguments.length > 1 ? arguments[1] : document;
	if (isString(el)) 
		el = document.getElementById(el);
	if (el) {
		if (el.getElementsByTagName) {
			return el.getElementsByTagName(tag);
		} else if (el.all) {
			if (tag == '*') 
				return el.all;
			return  el.all.tags[tag];
		}
	}
	return false;
}

function setElementIds(el) {
	if (isString(el)) 
		el = document.getElementById(el);
	
	if (isArray(el)) {
		var len = el.length;
		for (var i = 0 ; i < len; i++) {
			var x = el[i];
			if (! x.id && x.name) 
				x.id = name2id(x.name);
		}
	}
}

function getElementsByTag(tag) {
	var a_tag = null; 

	if (document.getElementsByTagName) {
		a_tag = document.getElementsByTagName(tag);
	} else if (document.all) {
		a_tag = document.all.tags(tag);
		if (! a_tag )
			a_tag = document.all;
	} else if (document.layers) {
		a_tag = document.layers;
	}
	return a_tag;
}

function getElementsByTagNameRegex(tag, name) {
	var nameRE;
	var a_tag = getElementsByTag(tag);

	if ( a_tag && a_tag.length > 0) {
		if (name == '*')
			return a_tag;
		if (arguments.length > 2 ) {
			nameRE= new RegExp(name, arguments[2]);
		} else {
			nameRE= new RegExp(name);
		}
		
		var a_fnd = new Array();

		var len = a_tag.length;
		for (var i=0; i < len; i++) {
			var el = a_tag[i];
			if (el.name) {
				var m = nameRE.exec(el.name);
				if (m) 
					a_fnd.push(el);
				
			}
		}
		return a_fnd;
	}
	return null;
}

function getElementsByTagClass(tag, c) {
	var a_tag = getElementsByTag(tag);
	if (! a_tag)
		return null;
	if (c == '*')
		return a_tag;

	var a_fnd = new Array();
	var wcls = ' ' + c + ' ';

	var len = a_tag.length;
	for (var i=0; i < len; i++) {
		var el = a_tag[i];
		var wcn = ' ' + el.className + ' ';
		if (wcn.indexOf(wcls) != -1) 
			a_fnd.push(el);
	}
	return a_fnd;
}

function enableGroup(name, enable) {
	var group = document.getElementsByName(name);

	if (group) {
		var len = group.length;
		for (var i = 0; i < len; i++)
			group[i].disabled = ! enable;
	}
}

function clearRadio(name) {
	var radio = document.getElementsByName(name);

	if (radio) {
		var len = radio.length;
		for (var i = 0; i < len; i++)
			radio[i].checked = false;
	}
}

// Javascript detection

function detectJS() {
	var idName = 'detectjs', phpName = 'detectjs.php';
	var el = document.getElementById(idName);
	if (el) {
		var src = phpName + '?src=' + el.src;
		el.src = src;
	}
}

addLoadEvent(detectJS);

