// ---------------------------------- UTILITY.JS ------------------------------------------------------------//

//Adds a method to arrays to find where a specific item is in the array
Array.prototype.indexOf = function (query) {
	var val = -1;
	for (var i=0; i<this.length; i++)
	{
		if (this[i] == query)
		{
			return i;
			break;
		}
	}
}

function GetRTEValue(rteName){
	var rte = "rte"+rteName;
	var data = null;
	updateRTE(rte);	

	var oHdnField = document.getElementById('hdn' + rte);
	
	if (oHdnField)
	{
		var data = oHdnField.value;
		
		var regexp = new RegExp("\n", "g");
		data = data.replace(regexp, ' ');
		var regexp = new RegExp("\r", "g");
		data = data.replace(regexp, ' ');
	};
	
	return data;
};

function Updating(containerID, text) 
{
	var containerEl = document.getElementById(containerID);	
	var updatingEl = document.createElement("div");

	if (!containerEl){
		alert("Could not find container: " + containerID + " update will continue.");
		return;
	};

	if (text == null || text == "")
	{
		text = "Updating ...";	
	}
	updatingEl.innerHTML = "<img src='/_images/updatingB.gif' style='position:relative;top:3px;' /> &nbsp;" + text;
	updatingEl.id = "UpdatingEl";
	updatingEl.className= "UpdatingEl";
	
	
	var contElHeight = containerEl.offsetHeight;

	var contTop = getElementTop(containerEl);
	var contLeft = getElementLeft(containerEl);

	updatingEl.style.paddingTop = ((contElHeight / 4) - 10).toString() + "px";
	updatingEl.style.height = (contElHeight - ((contElHeight / 4) - 10)).toString()+"px";
	updatingEl.style.width = containerEl.offsetWidth.toString()+"px";
	updatingEl.style.top = contTop.toString()+"px";
	updatingEl.style.left = contLeft.toString()+"px";
		
	containerEl.parentNode.appendChild(updatingEl);
		
	updatingEl = null;
}

function Update()
{
	var updatingEl = document.getElementById("UpdatingEl");
	if (updatingEl)	updatingEl.parentNode.removeChild(updatingEl);
	
	var arrUpEls = new Array();
	elementGetElementsByClass(document.documentElement, "div", "UpdatingEl", arrUpEls);
	for (i=0;i<arrUpEls.length;i++)
	{
		arrUpEls[i].parentNode.removeChild(arrUpEls[i]);
	}
	
}

if (window.Node && Node.prototype)
{
	Node.prototype.__defineGetter__("innerXml", function () {
        var oSerializer = new XMLSerializer();
        var frag = this.ownerDocument.createDocumentFragment();
        for(var i = 0; i < this.childNodes.length; i++) {
         frag.appendChild(this.childNodes[i]);
        }
        return oSerializer.serializeToString(frag, "text/xml");
    });
}

function sortNumber(a,b)
{
	return a - b
}

function RegisterEvent(el, ev, func) {
	if (el.attachEvent != null) {
		el.attachEvent("on" + ev, func);
	} else {
		el.addEventListener(ev, func, true);
	}
}

function UnregisterEvent(el, ev, func) {
	if (el.attachEvent != null) {
		el.detachEvent("on" + ev, func);
	} else {
		el.removeEventListener(ev, func, true);
	}
}

function addClass (el, className)
{
	if (el.className.indexOf(className) == -1)
	{
		el.className = el.className + " " + className;
	}
}

function removeClass (el, className)
{
	var result = el.className;
	var loc = el.className.indexOf(className);
	if (loc != -1)
	{
		result = el.className.substring(0, loc) + el.className.substring(loc + className.length);
	}
	el.className = result;
}

function getElementsByClassExclude (tagname, classname, ar, exclude)
{
	var alltags = document.all? document.all : document.getElementsByTagName(tagname);
	_allTagsIterate(classname, alltags, exclude, ar);
}

function getElementsByClass (tagname, classname, ar)
{
		getElementsByClassExclude (tagname, classname, ar, "");
}

function elementGetElementsByClass (element, tagname, classname, ar)
{
	var alltags = new Array();
	alltags = element.getElementsByTagName(tagname);
	_allTagsIterate(classname, alltags, "", ar);
}

function _allTagsIterate (classname, alltags, exclude, ar)
{
	var Classes;
	for (i=0; i<alltags.length; i++){
		Classes = new Array();
		Classes = alltags[i].className.split(" ");	
		if (Classes.indexOf(classname) > -1)
		{
			ar.push(alltags[i]);
		}
	}
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

String.prototype.trim = trimString;

function windowWidthGet () {
	var windowWidth;
	
	if (window.innerWidth)
	{
		windowWidth = window.innerWidth;
	}
	else
	{
		windowWidth = document.documentElement.offsetWidth;
	}
	
	return windowWidth;
}

function windowHeightGet () {
	var windowHeight;
	
	if (window.innerWidth)
	{
		windowHeight = window.innerHeight;
	}
	else
	{
		windowHeight = document.documentElement.offsetHeight;
	}
	
	return windowHeight;
}

function getElementLeft (element) {
	xPos = eval(element).offsetLeft;
	tempEl = eval(element).offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return xPos;
}

function getElementTop (element) {
	yPos = eval(element).offsetTop;
	tempEl = eval(element).offsetParent;
	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}

function killBubble(event)
{
	if (event)
	{
		event.stopPropagation();
	} else if (window.event) {
		window.event.cancelBubble = true;	
	}	
}

function clearBadCharacters(str)
{
	//str = str.replace(/</g, "");
	//str = str.replace(/>/g, "");
	//str = str.replace(/\\/g, "");
	//str = str.replace("/", "");
	
	return str;
}

/* ---------------TODAYS DATE ------------------------------ */
Style = 2; //pick a style from below

/*------------------------------
Style 1: March 17, 2000
Style 2: Mar 17, 2000
Style 3: Saturday March 17, 2000
Style 4: Sat Mar 17, 2000
Style 5: Sat March 17, 2000
Style 6: 17 March 2000
Style 7: 17 Mar 2000
Style 8: 17 Mar 00
Style 9: 3/17/00
Style 10: 3-17-00
Style 11: Saturday March 17
--------------------------------*/

months = new Array();
months[1] = "January";  months[7] = "July";
months[2] = "February"; months[8] = "August";
months[3] = "March";    months[9] = "September";
months[4] = "April";    months[10] = "October";
months[5] = "May";      months[11] = "November";
months[6] = "June";     months[12] = "December";

months2 = new Array();
months2[1] = "Jan"; months2[7] = "Jul";
months2[2] = "Feb"; months2[8] = "Aug";
months2[3] = "Mar"; months2[9] = "Sep";
months2[4] = "Apr"; months2[10] = "Oct";
months2[5] = "May"; months2[11] = "Nov";
months2[6] = "Jun"; months2[12] = "Dec";

days = new Array();
days[1] = "Sunday";    days[5] = "Thursday";
days[2] = "Monday";    days[6] = "Friday";
days[3] = "Tuesday";   days[7] = "Saturday";
days[4] = "Wednesday";

days2 = new Array();
days2[1] = "Sun"; days2[5] = "Thu";
days2[2] = "Mon"; days2[6] = "Fri";
days2[3] = "Tue"; days2[7] = "Sat";
days2[4] = "Wed";

function GetTodaysDate()
{
	todaysdate = new Date();
	date  = todaysdate.getDate();
	day  = todaysdate.getDay() + 1;
	month = todaysdate.getMonth() + 1;
	yy = todaysdate.getYear();
	year = (yy < 1000) ? yy + 1900 : yy;
	year2 = 2000 - year; year2 = (year2 < 10) ? "0" + year2 : year2;
	
	dateline = new Array();
	dateline[1] = months[month] + " " + date + ", " + year;
	dateline[2] = months2[month] + " " + date + ", " + year;
	dateline[3] = days[day] + " " + months[month] + " " + date + ", " + year;
	dateline[4] = days2[day] + " " + months2[month] + " " + date + ", " + year;
	dateline[5] = days2[day] + " " + months[month] + " " + date + ", " + year;
	dateline[6] = date + " " + months[month] + " " + year;
	dateline[7] = date + " " + months2[month] + " " + year;
	dateline[8] = date + " " + months2[month] + " " + year2;
	dateline[9] = month + "/" + date + "/" + year2;
	dateline[10] = month + "-" + date + "-" + year2;
	dateline[11] = days[day] + " " + months[month] + " " + date;

	return dateline[Style];
};

