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 =  xml.getElementsByTagName("record");
	var record;
	var size = records.length;
	var counter = 0;

	this.next = function()
	{
		record = records[counter];
		counter = counter + 1;
		return counter <= size;
	}

	this.getRecordValue = function( tag )
	{

		var node = record.getElementsByTagName(tag)[0];
		if(node.hasChildNodes())
		{
			return node.firstChild.nodeValue;
         	}
		else
		{
			return "";
		}
	}
	this.getRecordValueAt = function(index, tag )
	{
		var temp = records[index];

		var node = temp.getElementsByTagName(tag)[0];

		if(node.hasChildNodes())
		{
			return node.firstChild.nodeValue;
         	}
		else
		{
			return "";
		}
	}

	this.getValue = function( tag )
	{

		var node = xml.getElementsByTagName(tag)[0];
		

		if(node.hasChildNodes())
		{
			return node.firstChild.nodeValue;
         	}
		else
		{
			return "";
		}

	}
	this.size = function()
	{
		return size;
	}
}

function trim(text)
{
	return text.replace(/^\s+|\s+$/g, '');
}
function Table(column, width, height)
{		
	var count = 0;
	var column = column;
	var table = document.createElement('table');
	var tbody = document.createElement('tbody');
	var tr;
	var td;

	table.border="0";
	table.cellPadding="0";
	table.cellSpacing="0";
	table.appendChild(tbody);
	table.style.width = width;
	table.style.height = height;

	this.align = function(align)
	{
		table.align = align;
	}
	this.trAlign = function(trAlign)
	{
		tr.vAlign = trAlign;
	}
	this.tdAlign = function(tdAlign)
	{
		td.align = tdAlign;
	}

	this.HTML = function()
	{
		return table;
	};
	this.purge = function()
	{
		count = 0;
		table.removeChild(tbody);
		tbody = document.createElement('tbody');
		table.appendChild(tbody);
	}

	this.remove = function()
	{
		tr.removeChild(td);
		td = tr.lastChild;
		count = count - 1;
	}
	this.prepend = function(obj)
	{
		if( count % column == 0)
		{
			tr= document.createElement('tr');
			tbody.insertBefore(tr, tbody.firstChild);
		}

		td = document.createElement('td');
		td.appendChild(obj);
		tr.appendChild(td);
		count = count + 1;

	}
	this.append = function(obj)
	{
		if( count % column == 0)
		{
			tr= document.createElement('tr');
			tbody.appendChild(tr);
		}

		td = document.createElement('td');
		td.appendChild(obj);
		tr.appendChild(td);
		count = count + 1;
	};

}

