﻿// Helper functions

function $(str)  //retrieves node with the "str" id
{
    return document.getElementById(str);
}

//trims leading and trailing whitespaces    Note that Ajax.js also has a .trim() function
String.prototype.trims = function () {   
return enc(this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));  //trims first, then encode (see enc function below)
}     
    
function isEmpty(aTextField) 
{
    if ((aTextField.value.length==0) || (aTextField.value==null) || (aTextField.value.trims()=="")) 
    {
        return true;
    }
    else { return false; }
}	

//encode special html character to somewhat protect against XSS
function enc(text) {
var text = text.replace(/&/g,"&amp;");
text = text.replace(/</g,"&lt;");
text = text.replace(/>/g,"&gt;");
text = text.replace(/\r\n/gi,"<br>"); 
text = text.replace(/\n/gi,"<br>");
text = text.replace(/\r/gi,"<br>");  //changing this means also changing encTxtUpdate() (default.js)
text = text.replace(/javascript/gi,"");
return(text);
}
//decode these chars to user text is readable/editable
function dec(text) {
var text = text.replace(/&amp;/g,"&");
text = text.replace(/<br>/g,"\r\n");
text = text.replace(/&lt;/g,"<");
text = text.replace(/&gt;/g,">");
//text = text.replace(/\n/g,"<br>");
//text = text.replace(/\r/g,"<br>");
return(text);
}

//this function allows is a subsitute for document.createElement when creating a DOM element with a name tag (works across browsers)
var createElementWithName = ( function(){
  try {
    var el = document.createElement( '<div name="foo">' );
    if( el.tagName !== 'DIV' || el.name !== 'foo' ){
      throw 'create failed';
    }
    return function( tag, name ){
      return document.createElement( '<' + tag + ' name="' +
        name + '"></' + tag + '>' );
    };
  }catch( e ){
    return function( tag, name ){
      var el = document.createElement( tag );
      el.setAttribute( 'name', name );
      return el;
    };
  }
})();

function createLink(link)
{
    if (link.search("http://") != -1)
    { 
        return "<a href = '"+link+"' target='_blank'>"+link+"</a>"
    }
    else   // add 'http://'
    {
        return "<a href = 'http://"+link+"' target='_blank'>"+link+"</a>"
    }
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		//alert("getCookie returned null");
		return null;
	}
	if ( start == -1 ) {//alert("getCookie returned null start=-1");
	return null;}
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	//alert("getCookie returned: "+unescape( document.cookie.substring( len, end ) ));
	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() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
    
    //alert("cookie set: name="+name+", value="+value+", expires="+expires+", path="+path+", domain="+domain+", secure="+secure)
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

    //add event
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

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;
}