//------------------------------------------------------------------------------------------------//
//				ajax.js - created by Brendan Ratter 25/4/07				  //
//          this file contains all the necessary functions for ajax functionality		  //
//------------------------------------------------------------------------------------------------//

//function sendRequest: modified from orginal version taken from www.quirksmode.org
function sendRequest(url,callback,postData)
{
	//if supported, make an instance of the XMLHttpRequest
	var req = createXMLHTTPObject();
	if (!req) return;
	
	//if postData argument is not blank, set method to post
	var method = (postData) ? "POST" : "GET";
	//open asychronous http request
	req.open(method,url,true);
	//don'y know if this line is necessary
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData) req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	
	//anonymous function to perform actions based on the readystate
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
//debugging alert			alert('HTTP error ' + req.status);
			return;
		}
		//once redystate = 4 and their are no errors, execute callback
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

//function createXMLHTTPObject: courtesy of PPK at www.quirksmode.org
function createXMLHTTPObject()
{
	//account for known permutations of the XMLHttpRequest object
	var XMLHttpFactories = [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	];
	
	//set to false as default
	var xmlhttp = false;
	
	//loop through each permutation to see if it exists in browser
	for (var i = 0; i < XMLHttpFactories.length; i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

//getResponse function - returns either the xml root node or the response text, depending on the second argument
function getResponse(req,out_type)
{
	if (out_type == 1 || out_type == 'text') return req.responseText
	else return req.responseXML.documentElement;
}

//getChildrenByTagName function - returns an array of all childNodes with a specific tagName
function getChildrenByTagName(xml,tag)
{
	var obj_array = new Array();
	
	for (var i = 0; i < xml.childNodes.length; i++) {
		if (xml.childNodes[i].nodeName.toLowerCase() == tag.toLowerCase()) {
			obj_array.push(xml.childNodes[i]);
		}
	}
	return obj_array;
}

//getChildrenByNodeType function - returns an array of all childNodes with a specific NodeType
function getChildrenByNodeType(xml,node_id)
{
	var obj_array = new Array();
	
	for (var i = 0; i < xml.childNodes.length; i++) {
		if (xml.childNodes[i].nodeType == node_id) {
			obj_array.push(xml.childNodes[i]);
		}
	}
	return obj_array;
}

//xmlExtract function - takes an xml doc as an argument then uses various properties and methods to extract data easily
function xmlExtract (xml)
{
	//an approach could be to have an object that holds the xml doc as a variable and then has methods like get() to pull out and return a specific tag, tht can then have other methods...eg text...to get text.
	
	//set the xml property - any dom property can be accessed using this.xml. ...
	this.xml = xml;
	//set xml to null to save space
	xml = null;
	
	//later put in pointer functionality - so can step through results. maybe a first, last, previous, next method too?
	this.parent;
	this.loopMode = false;
	this.ptr = 0;
	
	//set the value of the text node if any exists - later adapt this to be more comprehensive (eg CDATA)
	this.text;
	var temp = getChildrenByNodeType(this.xml,3);
	if (temp.length != 0) {
		this.text = temp[this.ptr].nodeValue;
	}
	temp = null;
	
	//method to get a child element based on the tag name - later adapt to be able to loop through result set
	this.get = function (tag) {
		var el = getChildrenByTagName(this.xml,tag);
		if (!el || el.length == 0) {
			return false;
		} else {
			//set new object and appply its parent property
			var ret = new xmlExtract (el[this.ptr]);
			ret.parent = this;
			return ret;
		}
	}
}

function bleh(req) {
	var xml = getResponse(req);
	//alert(xml.getElementsByTagName('position').length);
	//alert(getChildrenByTagName(xml,'position')[0].childNodes.length);
	var table = new buildTable();
	table.newTable(2,3);
	table.addCell(0,1,'hey',2,2)
	alert(table.generate());
}

function buildTable() {

	//properties
	this.cells;
	this.width;
	this.height;
	this.classname;
	this.table = document.createElement('table');
	
	//methods
	this.newTable = function (cols,rows) {
		this.width = cols;
		this.height = rows;
		this.cells = new Array (cols);
		for (var i = 0; i < this.cells.length; i++) {
			this.cells[i] = new Array (rows);
		}
	}
	
	this.addCell = function (col,row,classname,colspan,rowspan) {
		if (!colspan) colspan = 1;
		if (!rowspan) rowspan = 1;
		var cell;
		for (var i = 0; i < colspan; i++) {
			for (var j = 0; j < rowspan; j++) {
				var cell = this.cells[i + col][j + row];
				if (i == col && j == row) {alert('here');
					 cell = new newCell(colspan,rowspan);
					 cell.classname = classname;
					 cell.innerHTML = 'some text';
					 alert(cell.innerHTML);
				} else {
					cell = false;
				}
			}
		}
		//alert(this.status);
	}
	
	this.generate = function () {
		var html = '<table';
		if (this.classname) html += ' class="' + this.classname + '"';
		html += '></table>';
		
		return html;
	}
}

function newCell(colspan,rowspan)
{
	this.colspan = colspan;
	this.rowspan = rowspan;
	this.classname;
	this.innerHTML = '';
	
	//this.html = '<td';
	//if (class_name) this.html += ' class="' + classname + '"';
	//if (colspan && colspan != 1) this.html += ' colspan="' + colspan + '"';
	//if (rowspan && rowspan != 1) this.html += ' rowspan="' + rowspan + '"';
	//this.html += '></td>';
	//alert(this.html);
}