/* 
 * util.js
 *  commonly used javascript functions that we are going to need
 *  on lots of different pages
 */

// call this function to get a cross-browser compatible XMLHttpRequest object
function ajaxOpen() {
	var request;
	try
  	{  // IE 7.0+, Firefox, Opera 8.0+, Safari
  		request=new XMLHttpRequest();
  	}
	catch (e)
  	{  // IE 6.0+,
  		try
    	{
    		request=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e)
    	{
    		try
      		{   // IE 5.5
      			request=new ActiveXObject("Microsoft.XMLHTTP");
      		}
    		catch (e)
      		{
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
      	}
    }
    return request;
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

String.prototype.URLEncode = function () {
	var output = '';
	var x = 0;
	clearString = this.toString();
	var regex = /(^[a-zA-Z0-9_.]*)/;
	while (x < clearString.length) {
	var match = regex.exec(clearString.substr(x));
	if (match != null && match.length > 1 && match[1] != '') {
		output += match[1];
	  x += match[1].length;
	} else {
	  if (clearString[x] == ' ')
	    output += '+';
	  else {
	    var charCode = clearString.charCodeAt(x);
	    var hexVal = charCode.toString(16);
	    output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
	  }
	  x++;
	}
	}
	return output;
	}
String.prototype.escapeHTML = function () {                                        
    return(                                                                 
        this.replace(/&/g,'&amp;').                                         
            replace(/>/g,'&gt;').                                           
            replace(/</g,'&lt;').                                           
            replace(/"/g,'&quot;')                                         
    );                                                                      
};
String.prototype.unescapeHTML = function () {                                        
    return(                                                                 
        this.replace(/&amp;/g,'&').                                         
            replace(/&gt;/g,'>').                                           
            replace(/&lt;/g,'<').                                           
            replace(/&quot;/g,'"')                                         
    );                                                                      
};

function debug(msg) {
	var debugp = document.getElementById('debug'); 
	debugp.innerHTML += msg + "<br />";
}
