function trim(str)
   {
     var s = str.replace(/^(\s)*/, '');
     s = s.replace(/(\s)*$/, '');
     return s;
};


function reverseString(str){
  	var revString = ""; 

	for (var i = 0; i <= str.length; i++) {
		revString = str.charAt (i) + revString
	}
	
	return revString;
};


HBI = function(){
	//HBI is a class
};

HBI.removeClass = function(el, className) {
	if (!(el && el.className)) {
		return;
	}
	var cls = el.className.split(" ");
	var ar = new Array();
	for (var i = cls.length; i > 0;) {
		if (cls[--i] != className) {
			ar[ar.length] = cls[i];
		}
	}
	el.className = ar.join(" ");
};

HBI.addClass = function(el, className) {
	HBI.removeClass(el, className);
	el.className += el.className.length?" " + className:className;;
};

HBI.setClass = function(el, className) {
	el.className = className;
};

HBI.hasClass = function(el, className) {
	if (!(el && el.className)) {
		return false;
	}
	var cls = el.className.split(" ");
	for (var i = cls.length; i > 0;) {
		if (cls[--i] == className) {
			return true;
		}
	}
	return false;
};


HBI.createElement = function(type, parent) {
	var el = null;
	if (document.createElementNS) {
		// use the XHTML namespace; IE won't normally get here unless
		// _they_ "fix" the DOM2 implementation.
		el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
	} else {
		el = document.createElement(type);
	}
	if (typeof parent != "undefined") {
		parent.appendChild(el);
	}
	return el;
};


HBI.addEvent = function(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else {
		el["on" + evname] = func;
	}
};

HBI.removeEvent = function(el, evname, func) {
	if (el.detachEvent) { // IE
		el.detachEvent("on" + evname, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, true);
	} else {
		el["on" + evname] = null;
	}
};


HBI.getChildNodesByTagName = function(control,tagName){
	if(!control.hasChildNodes()) return;
	ar = new Array();
	for(var i =0; i < control.childNodes.length; i++){
		if(control.childNodes[i].tagName == tagName){
			ar[ar.length] =  control.childNodes[i];
		}
	}
	return ar;
}


HBI.setSiteLocale = function(siteLocale){
	document.cookie = "SITELOCALE=" + siteLocale + ";path=/";
}

HBI.getSiteLocale = function(){
	return HBI.getCookie("SITELOCALE");
}

HBI.getCookie = function(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;

	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
			
	return unescape(dc.substring(begin + prefix.length, end));
};

HBI.emailCheck = function (str) {
	var at					= "@";
	var dot					= ".";
	var lat					= str.indexOf(at);
	var lstr				= str.length;
	var ldot				= str.indexOf(dot);
	
	if (str.indexOf(at) == -1) {
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
	   return false;
	}


	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
	  return false;
	}
	
	if (str.indexOf(at,(lat+1))!=-1) {
		return false;
	}
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
		return false;
	}
	
	if (str.indexOf(dot,(lat+2))==-1) {
		return false;
	}
	
	if (str.indexOf(" ")!=-1) {
		return false;
	}

	return true;
};

/*
 * Remove non-display characters such as &nbsp;
 */
HBI.removeNonDisplayChar = function(sText) {
	var s = sText.replace(/(&nbsp;)/g, '');
	return s;
};


/*
 * Remove non-display characters such as &nbsp;
 */
HBI.removeHTMLTags = function(sText) {
	//var s = sText.replace(/(\s)*/, '');
	//s = s.replace(/(\s)*$/, '');
	//reReplace(arguments.text, "</*[^>]*>", " ", "all")
	
	var s = sText.replace(/(<\/*[^>]*>)/g, '');
	return s;
};


/* 
 * Replace problematic Word "smart" quotes and other crap with web-safe alternatives
 */
 
HBI.replaceWordOddities = function(sText) {
	var replacements, regex, key, textnodes, node, s;

	replacements = {
					    "\xa0": " ",
					    "\xa9": "(c)",
					    "\xae": "(r)",
					    "\xb7": "*",
					    "\u2018": "'",
					    "\u2019": "'",
					    "\u201c": '"',
					    "\u201d": '"',
					    "\u2026": "...",
					    "\u2002": " ",
					    "\u2003": " ",
					    "\u2009": " ",
					    "\u2013": "-",
					    "\u2014": "--",
					    "\u2122": "(tm)"
				    };

	regex = {};

	for (key in replacements) {
    	regex[key] = new RegExp(key, 'g');
	}

	for (key in replacements) {
        sText = sText.replace(regex[key], replacements[key]);
    }
    
    return sText; 
	
};

// Cookie manipulations.. I guess these are general enough be here... 

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function delCookie( name, path, domain ) {
	if ( GetCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

