function urlencode(str){
var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
var ret = str.toString();
var replacer = function(search, replace, str) {
var tmp_arr = [];
tmp_arr = str.split(search);
return tmp_arr.join(replace);
};
histogram['!']   = '%21';
histogram['%20'] = '+';
ret = encodeURIComponent(ret);
for (search in histogram) {
replace = histogram[search];
ret = replacer(search, replace, ret);
}
return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2){
return "%"+m2.toUpperCase();
});
return ret;
}

function urldecode(str){

var histogram = {}, histogram_r = {}, code = 0, str_tmp = [];
var ret = str.toString();
var replacer = function(search, replace, str) {
var tmp_arr = [];
tmp_arr = str.split(search);
return tmp_arr.join(replace);
};
histogram['!']   = '%21';
histogram['%20'] = '+';
for (replace in histogram) {
search = histogram[replace]; // Switch order when decoding
ret = replacer(search, replace, ret) // Custom replace. No regexing   
}
ret = decodeURIComponent(ret);
return ret;
}
function Query(response)
{
	var xml = response;
	var records;
	var size;
	var next_index;
	var prev_index;
	var curr_index;

	this.init = function( recordName )
	{
		records = xml.getElementsByTagName(recordName);
		size = records.length;
		next_index = 0;
		prev_index = size-1;
		curr_index = 0;
	}

	this.size = function()
	{
		return records.length;
	}
	this.next = function()
	{
		curr_index = next_index;
		next_index = next_index + 1;
		return next_index <= size;
	}
	this.prev = function()
	{
		curr_index = prev_index;
		prev_index = prev_index - 1;
		return prev_index >= -1;
	}

	this.getRecordTag = function( tag )
	{
		var record = records[curr_index];
		var node = record.getElementsByTagName(tag)[0];
		if(node.hasChildNodes())
		{
			return node.firstChild.nodeValue;
         	}
		else
		{
			return "";
		}
	}
	this.getTag = function( tag )
	{
		var node = xml.getElementsByTagName(tag)[0];
		if(node.hasChildNodes())
		{
			return node.firstChild.nodeValue;
         	}
		else
		{
			return "";
		}
	}
}

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

function GetXmlHttpObject()
{
	var obj=null;
	try
	{
	 	// Firefox, Opera 8.0+, Safari
		obj=new XMLHttpRequest();
	}
	catch (e)
	{
	 	// Internet Explorer
	 	try
	  	{
	  		obj=new ActiveXObject("Msxml2.XMLHTTP");
		}
	 	catch (e)
	  	{
			obj=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return obj;
}
function purge(id)
{
	var node = document.getElementById(id);
	while(node.hasChildNodes())
	{
		node.removeChild(node.firstChild);
	}

}
function purge_id(id)
{
	var node = document.getElementById(id);
	while(node.hasChildNodes())
	{
		node.removeChild(node.firstChild);
	}

}
function purge_node(node)
{
	while(node.hasChildNodes())
	{
		node.removeChild(node.firstChild);
	}

}
function purge_node_timeout(node, time)
{
	setTimeout(function(){purge_node(node); node = null; }, time);	
}
function purge_id_timeout(id, time)
{
	var node = document.getElementById(id);
	setTimeout(function(){purge_node(node); node = null; }, time);	
}
function detach_id(id)
{
	var node = document.getElementById(id);
	var parentnode = node.parentNode;
	parentnode.removeChild(node);
}
function detach_node(node)
{
	var parentnode = node.parentNode;
	parentnode.removeChild(node);
}
function $(id)
{
	return document.getElementById(id);
}

