if(!window.console) {
  window.console = new function() {
    this.log = function(str) {};
    this.dir = function(str) {};
    this.debug = function(str) {};
  };
}


ObjectCache = {};

function $(id){
    cached = ObjectCache[id];
    if (cached) {
        return cached;
    }
    else {
        new_obj = document.getElementById(id);
        ObjectCache[id] = new_obj;
        return new_obj;
    }
}

function bind(object, method) {
  return function() {
    return method.apply(object, arguments);
  };
}

extend = function(subClass, baseClass) {
   function inheritance() {}
   inheritance.prototype = baseClass.prototype;
   subClass.prototype = new inheritance();
   subClass.prototype.constructor = subClass;
   subClass.baseConstructor = baseClass;
   subClass.superClass = baseClass.prototype;
}

function ValidationError(msg) {
    this.message = msg;
}
extend(ValidationError, Error);

function preventDefault(evt){
    if (typeof(evt.preventDefault) == 'function') evt.preventDefault();
    else if(typeof(evt.cancelBubble) == 'boolean') evt.cancelBubble = true;
}

modalPopup = function(href, name, width, height) {
    w = window.open(href,name, 'height=' + height + ',width=' + width + ',toolbar=no,directories=no,status=no,linemenubar=no,scrollbars=no,resizable=no,modal=yes');
    w.focus()
    return false;
}

function addListener(element, event, listener, bubble) {
  if(element.addEventListener) {
    if(typeof(bubble) == "undefined") bubble = false;
    element.addEventListener(event, listener, bubble);
  } else if(this.attachEvent) {
    element.attachEvent("on" + event, listener);
  }
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function findRadioElement(radioObj, value) {
    if(!radioObj) return "";
    for (nObj in radioObj){
        if(radioObj[nObj].value == value) return radioObj[nObj];
    }
    return false
} 

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function insertAtCursor(myField, myValue) {
    //IE support
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    
    //Mozilla/Firefox/Netscape 7+ support
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        var caretPos = startPos + myValue.replace(/\n/g, '').length;
        myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
        setCaretTo(myField, caretPos);
    } 
    
    //Other Browsers
    else {
        myField.value += myValue;
    }
    
    
}

function setCaretTo(obj, pos) {
    if(obj.createTextRange) { 
        /* Create a TextRange, set the internal pointer to
           a specified position and show the cursor at this
           position
        */ 
        var range = obj.createTextRange(); 
        range.move("character", pos); 
        range.select(); 
    } else if(obj.selectionStart) { 
        /* Gecko is a little bit shorter on that. Simply
           focus the element and set the selection to a
           specified position
        */ 
        obj.focus(); 
        obj.setSelectionRange(pos, pos); 
    } 
}

function getCaretPosition(obj) {
    //FF Only
    if (obj.selectionStart) return obj.selectionStart;
    else return 0;
}

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;
}


Element = {
    remove: function(element){
        if (element.removeNode) element.removeNode(true);
        else element.parentNode.removeChild(element);
    },
    get_parent: function(element){
        if (element.parentElement) return element.parentElement;
        else return element.parentNode;
    }
}

Cookie = {
    create: function(name,value,days) {
    	if (days) {
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = name+"="+escape(value)+expires+"; path=/";
    },

    read: function(name) {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++) {
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
    	}
    	return null;
    },

    erase: function(name) {
    	Cookie.create(name,"",-1);
    }
    
}

