﻿/* Element Retrieval Methods
===================================================================================== */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
function getElementsBySelector(all_selectors) {
	var selected = new Array();
	if(!document.getElementsByTagName) return selected;
	all_selectors = all_selectors.replace(/\s*([^\w])\s*/g,"$1");
	var selectors = all_selectors.split(",");
	// Grab all of the tagName elements within current context	
	var getElements = function(context,tag) {
		if (!tag) tag = '*';
		// Get elements matching tag, filter them for class selector
		var found = new Array;
		for (var a=0,len=context.length; con=context[a],a<len; a++) {
			var eles;
			if (tag == '*') eles = con.all ? con.all : con.getElementsByTagName("*");
			else eles = con.getElementsByTagName(tag);

			for(var b=0,leng=eles.length;b<leng; b++) found.push(eles[b]);
		}
		return found;
	}

	COMMA:
	for(var i=0,len1=selectors.length; selector=selectors[i],i<len1; i++) {
		var context = new Array(document);
		var inheriters = selector.split(" ");

		SPACE:
		for(var j=0,len2=inheriters.length; element=inheriters[j],j<len2;j++) {
			//This part is to make sure that it is not part of a CSS3 Selector
			var left_bracket = element.indexOf("[");
			var right_bracket = element.indexOf("]");
			var pos = element.indexOf("#");//ID
			if(pos+1 && !(pos>left_bracket&&pos<right_bracket)) {
				var parts = element.split("#");
				var tag = parts[0];
				var id = parts[1];
				var ele = document.getElementById(id);
				if(!ele || (tag && ele.nodeName.toLowerCase() != tag)) { //Specified element not found
					continue COMMA;
				}
				context = new Array(ele);
				continue SPACE;
			}

			pos = element.indexOf(".");//Class
			if(pos+1 && !(pos>left_bracket&&pos<right_bracket)) {
				var parts = element.split('.');
				var tag = parts[0];
				var class_name = parts[1];

				var found = getElements(context,tag);
				context = new Array;
 				for (var l=0,len=found.length; fnd=found[l],l<len; l++) {
 					if(fnd.className && fnd.className.match(new RegExp('(^|\s)'+class_name+'(\s|$)'))) context.push(fnd);
 				}
				continue SPACE;
			}

			if(element.indexOf('[')+1) {//If the char '[' appears, that means it needs CSS 3 parsing
				// Code to deal with attribute selectors
				if (element.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?['"]?([^\]'"]*)['"]?\]$/)) {
					var tag = RegExp.$1;
					var attr = RegExp.$2;
					var operator = RegExp.$3;
					var value = RegExp.$4;
				}
				var found = getElements(context,tag);
				context = new Array;
				for (var l=0,len=found.length; fnd=found[l],l<len; l++) {
 					if(operator=='=' && fnd.getAttribute(attr) != value) continue;
					if(operator=='~' && !fnd.getAttribute(attr).match(new RegExp('(^|\\s)'+value+'(\\s|$)'))) continue;
					if(operator=='|' && !fnd.getAttribute(attr).match(new RegExp('^'+value+'-?'))) continue;
					if(operator=='^' && fnd.getAttribute(attr).indexOf(value)!=0) continue;
					if(operator=='$' && fnd.getAttribute(attr).lastIndexOf(value)!=(fnd.getAttribute(attr).length-value.length)) continue;
					if(operator=='*' && !(fnd.getAttribute(attr).indexOf(value)+1)) continue;
					else if(!fnd.getAttribute(attr)) continue;
					context.push(fnd);
 				}

				continue SPACE;
			}

			//Tag selectors - no class or id specified.
			var found = getElements(context,element);
			context = found;
		}
		for (var o=0,len=context.length;o<len; o++) selected.push(context[o]);
	}
	return selected;
}
function getChild(startParent) {
  EndChild = startParent.firstChild ;
  while(EndChild.nodeType != 1){
    EndChild = EndChild.nextSibling ;
  }
  return EndChild;
}

/* Flash Border Removal
===================================================================================== */
function initFlashBorders() {
    if (document.body.outerHTML && (document.getElementsByTagName('object')).length) {
		var objs = document.getElementsByTagName('object') ;
		var i = objs.length - 1 ;
		do {
			if (objs[i].getAttribute('type') == 'application/x-shockwave-flash') {
				var o = objs[i] ;
				var h = o.outerHTML ;
				var params = '' ;
				var j = o.childNodes.length - 1 ;
				do {
					var p = o.childNodes[j] ;
					if (p.tagName == "PARAM") params += p.outerHTML ;
				} while (j--) ;
				var tag = h.split(">")[0] + ">" ;
				o.outerHTML = tag + params + o.innerHTML + " </object>" ;
			}
		} while (i--)
	}
}

/* Form Element Methods
===================================================================================== */
function initClearInputs() {
  inputs = document.getElementsByTagName('input');
  for (i = 0; i < inputs.length; i++) {
    if ((inputs[i].getAttribute('value')) && (inputs[i].getAttribute('type') == 'text')) {
            inputs[i].setAttribute('default',inputs[i].getAttribute('value'));
            inputs[i].onfocus = function() {
				if (this.value == this.getAttribute('default')) {
					this.value = '' ;
				}
			}
			inputs[i].onblur = function() {
				if (this.value == '') {
					this.value = this.getAttribute('default');
				}
			}
    }
  }
}

/* The DOM Loaded Event Queue
===================================================================================== */
var _loadQueue = new Array();

function addLoadEvent(func){
    _loadQueue.push(func);
}

/* The DOM Loaded Script */

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", processEventQueue, false);
}
/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    if (window.location.protocol != 'https:')
    {
        document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
        var script = document.getElementById("__ie_onload");
        script.onreadystatechange = function() {
            if (this.readyState == "complete") {
                processEventQueue(); // call the onload handler
            }
        };
    }
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            processEventQueue(); // call the onload handler
        }
    }, 10)
}

/* for other browsers */
window.onload = processEventQueue;

/* dom loaded call */
function processEventQueue() {
    // quit if this function has already been called
	if (arguments.callee.done) return;
    
    // flag this function so we don't do the same thing twice
	arguments.callee.done = true;
    
    // kill the timer
	if (_timer) clearInterval(_timer);

    // process the event queue
    for (i = 0; i < _loadQueue.length; i++){
       setTimeout(_loadQueue[i],0);
    }
    
    // clear the event queue
    _loadQueue = new Array();
}
