// JavaScript Document

//globals
var columns = Array();
var data = Array();
var dataSource = "";

var limit = 25;
var aCurrent = 0;
var records = 0;
var columDataDelimiter = "kz!!zk";
var rowDelimiter = "zk!kz";
var cellDelimiter = "|!|";

var targetDiv = "";

var table = "";
var tableBody = "";

//ajax requester method/variable

function loadurl(dest) {
	try {
		// Moz supports XMLHttpRequest. IE uses ActiveX.
		// browser detction is bad. object detection works for any browser
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {

		// browser doesn’t support ajax. handle however you want
	}

	// the xmlhttp object triggers an event everytime the status changes
	// triggered() function handles the events
	xmlhttp.onreadystatechange = triggered;



	// open takes in the HTTP method and url.
	xmlhttp.open("GET", dest);

	// send the request. if this is a POST request we would have
	// sent post variables: send("name=aleem&gender=male)
	// Moz is fine with just send(); but
	// IE expects a value here, hence we do send(null);
	xmlhttp.send(null);
}

function triggered() {
	// if the readyState code is 4 (Completed)
	// and http status is 200 (OK) we go ahead and get the responseText
	// other readyState codes:
	// 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		// xmlhttp.responseText object contains the response.
		var response = xmlhttp.responseText;

		var result = response.split(columDataDelimiter);
		columns = result[0].split(cellDelimiter);

		aMakeColumns();



		//http.send(null);

		//get the data
		var rowData = Array();
		var dataA = result[1].split(rowDelimiter);
		for (var countr = 0; countr < dataA.length; countr++)
		{
			data[countr] = dataA[countr].split(cellDelimiter);
		}

		//make the datarows
		aMakeRows();

		table.appendChild(tableBody);
		//throw it at the div!
		targetDiv.appendChild(table);
		return xmlhttp.responseText;
	}
}





//ajax table functions

//startup stuff
function aStartUp(aTargetDiv, AdataSource, Alimit)
{
	http = createRequestObject();
	//set the limit
	if (Alimit > 0)
	limit = Alimit;

	//set the table
	targetDiv = document.getElementById(aTargetDiv);
	table     = document.createElement("table");
	tableBody = document.createElement("tbody");

	//get the columns
	loadurl(AdataSource);

}


//make columns
function aMakeColumns() 
{
	//make a new row
	var myrow = document.createElement("tr");
	//loop through adding the columns
	confirm(columns.length);
	for (var i = 0; i < columns.length; i++)
	{
		//make the element
		myth = document.createElement("th");
		currenttext = document.createTextNode(columns[i]);

		myth.appendChild(currenttext);
		myrow.appendChild(myth);
		//add the element to the row
	}
	//add the blank column
	myth = document.createElement("th");
	myrow.appendChild(myth);
	//add the header row to the tablebody
	tableBody.appendChild(myrow);
}

//make rows
function aMakeRows()
{
	//loop through the records
	for (var n = 0; n < data.length; n++)
	{
		//make the row
		currentRow = document.createElement("tr");
		var specialObjects = Array();
		var ce = Array();
		//loop through the cells
		for (var n2 = 0; n2 < data[n].length; n2++)
		{
				
			//make the cell
			mycurrent_cell = document.createElement("td");

			//put the data in the cell
			currenttext = document.createTextNode(data[n][n2]);

			specialObjects = data[n][n2].split("spoj");
			if (specialObjects.length != 0)
			{
				for (var i = 0; i < specialObjects.length; i++)
				{
					ce = specialObjects[i];
					switch (ce[0])
					{
						case "button":
							
							
							break;
						case "checkboox":
							break;
					}
					
				}
			}
			else 
			{
				mycurrent_cell.appendChild(currenttext);
			}

			//add the cell to the row
			currentRow.innerHtml = mycurrent_cell;
		}
		//add the row to the tablebody
		tableBody.appendChild(currentRow)
	}
}

function aRemoveHtmlSpecialChars(text)
{
	var textI = new String(text);
	textI.replace(/&gt;/,">");
	textI.replace(/&lt;/,"<");
	textI.replace(/&quot;/,"\"");
	return textI;
}
