/* Event Functions */

// Add an event to the obj given
// event_name refers to the event trigger, without the "on", like click or mouseover
// func_name refers to the function callback when event is triggered
function addEvent(obj,event_name,func_name){	
	if (obj.attachEvent){
		obj.attachEvent("on"+event_name, func_name);
	}else if(obj.addEventListener){
		obj.addEventListener(event_name,func_name,true);
	}else{
		obj["on"+event_name] = func_name;
	}
}

// Removes an event from the object
function removeEvent(obj,event_name,func_name){
	if (obj.detachEvent){
		obj.detachEvent("on"+event_name,func_name);
	}else if(obj.removeEventListener){
		obj.removeEventListener(event_name,func_name,true);
	}else{
		obj["on"+event_name] = null;
	}
}

// Stop an event from bubbling up the event DOM
function stopEvent(evt){
	evt || window.event;
	if (evt.stopPropagation){
		evt.stopPropagation();
		evt.preventDefault();
	}else if(typeof evt.cancelBubble != "undefined"){
		evt.cancelBubble = true;
		evt.returnValue = false;
	}
	return false;
}

// Get the obj that starts the event
function getElement(evt){
	if (window.event){
		return window.event.srcElement;
	}else{
		return evt.currentTarget;
	}
}
// Get the obj that triggers off the event
function getTargetElement(evt){
	if (window.event){
		return window.event.srcElement;
	}else{
		return evt.target;
	}
}
// For IE only, stops the obj from being selected
function stopSelect(obj){
	if (typeof obj.onselectstart != 'undefined'){
		addEvent(obj,"selectstart",function(){ return false;});
	}
}

/*    Caret Functions     */

// Get the end position of the caret in the object. Note that the obj needs to be in focus first
function getCaretEnd(obj){
	if(typeof obj.selectionEnd != "undefined"){
		return obj.selectionEnd;
	}else if(document.selection&&document.selection.createRange){
		var M=document.selection.createRange();
		try{
			var Lp = M.duplicate();
			Lp.moveToElementText(obj);
		}catch(e){
			var Lp=obj.createTextRange();
		}
		Lp.setEndPoint("EndToEnd",M);
		var rb=Lp.text.length;
		if(rb>obj.value.length){
			return -1;
		}
		return rb;
	}
}
// Get the start position of the caret in the object
function getCaretStart(obj){
	if(typeof obj.selectionStart != "undefined"){
		return obj.selectionStart;
	}else if(document.selection&&document.selection.createRange){
		var M=document.selection.createRange();
		try{
			var Lp = M.duplicate();
			Lp.moveToElementText(obj);
		}catch(e){
			var Lp=obj.createTextRange();
		}
		Lp.setEndPoint("EndToStart",M);
		var rb=Lp.text.length;
		if(rb>obj.value.length){
			return -1;
		}
		return rb;
	}
}
// sets the caret position to l in the object
function setCaret(obj,l){
	obj.focus();
	if (obj.setSelectionRange){
		obj.setSelectionRange(l,l);
	}else if(obj.createTextRange){
		m = obj.createTextRange();		
		m.moveStart('character',l);
		m.collapse();
		m.select();
	}
}
// sets the caret selection from s to e in the object
function setSelection(obj,s,e){
	obj.focus();
	if (obj.setSelectionRange){
		obj.setSelectionRange(s,e);
	}else if(obj.createTextRange){
		m = obj.createTextRange();		
		m.moveStart('character',s);
		m.moveEnd('character',e);
		m.select();
	}
}

/*    Escape function   */
String.prototype.addslashes = function(){
	return this.replace(/(["\\\.\|\[\]\^\*\+\?\$\(\)])/g, '\\$1');
}
String.prototype.trim = function () {
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};
/* --- Escape --- */

/* Offset position from top of the screen */
function curTop(obj){
	toreturn = 0;
	while(obj){
		toreturn += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return toreturn;
}
function curLeft(obj){
	toreturn = 0;
	while(obj){
		toreturn += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return toreturn;
}

function curRight(obj){
	toreturn = obj.offsetWidth;
	while(obj){
		toreturn += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return toreturn;
}

/* ------ End of Offset function ------- */

/* Types Function */

// is a given input a number?
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

/* Object Functions */

function replaceHTML(obj,text)
{
	while(el = obj.childNodes[0]){
		obj.removeChild(el);
	};
	obj.appendChild(document.createTextNode(text));
}


function FadeInImage(obj)
{
	if(obj == null)
	{
		return;
	}
	
	var val;
	
	if(obj.style.opacity)
	{
		/* Firefox. */
		val = obj.style.opacity * 100.0;	
	}
	else if(obj.filters.alpha.opacity)
	{
		/* IE */
		val = obj.filters.alpha.opacity;	
	}
	else
	{
		return;
	}
	
	val += 10.0;
	
    if (val > 99)
    {
		val=100;
	}
	
	if(obj.style.opacity)
	{
		/* Firefox. */
		obj.style.opacity = val / 100.0;
	}
	else if(obj.filters.alpha)
	{
		/* IE */
		obj.filters.alpha.opacity = val;
    }
    
    m = obj;
    t = setTimeout("FadeInImage(m);", 3);
}

function InitFading(obj)
{	
	if(obj.filters)
	{
		// IE
		obj.style.filter = "Alpha(Opacity=30)";
	}
	else
	{
		// Firefox
		obj.style.opacity = 0.3;
	}
}

function FadeOutImage(obj)
{	
	if(obj == null)
	{
		return;
	}
    if(window.t)
    {
		clearTimeout(t);
	}
	
	if(obj.style.opacity)
	{
		/* Firefox */
		obj.style.opacity = 0.3;
	}
	else if(obj.filters.alpha)
	{
		/* IE */
		obj.filters.alpha.opacity = 30; 
    }
    else
    {
		return;
    }    
}

/**
 * Disables the autocomplete behavior for the sepcified textbox control.
 * @param textbox The input textbox element.
 */
//function DisableAutoComplete(textbox)
//{
//	if(textbox != null)
//	{
//		try
//		{
//			textbox.setAttribute("AutoComplete", "Off");
//		}
//		catch(e)
//		{
//		}
//	}
//}

/**
 * Validates an email address's syntax.
 * @param email The email address that should be validated.
 */
function ValidateEmailAddress(email)
{
	if(email == null)
	{
		return false;
	}
	var regex = new RegExp("^(\\w+[\\.-]{0,1})*\\w+@(\\w+[\\.-]{0,1})*\\w+$");
	return regex.test(email); 
}


/* * * * * * * * * * *
 * String Functions  *
 * * * * * * * * * * */

/**
 * Remove padding white spaces from the left side of the specified string.
 * @param str The string to be left trimmed.
 */ 
function ltrim(str)
{ 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}

/**
 * Remove padding white spaces from the right side of the specified string.
 * @param str The string to be right trimmed.
 */ 
function rtrim(str)
{
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}

/**
 * Remove padding white spaces from the bothe left and right sides of the specified string.
 * @param str The string to be trimmed.
 */ 
function trim(str)
{
	return ltrim(rtrim(str));
}

/**
 * Check whether the specified character is a white space character.
 * @param charToCheck The character to be checked.
 */ 
function isWhitespace(charToCheck)
{
	var whitespaceChars = " \t\n\r\f"; /* Note there is a spcae in the 
										* begining of this string!!!
										* Donot delete it.
										*/
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}
