// ##############################################################################################
// # Ajaxelerate 2.3																			#
// # Ajax Framewrk for managing WebServices in HTML												#
// # Author: Mauricio Giraldo Mutis <me@bymurdock.com>											#
// # Version 2.0: March 26, 2007																#
// # Version 2.1: March 28, 2007																#
// # Version 2.2: March 29, 2007																#
// # Version 2.3: April 02, 2007. First functional version of text fields and combo box			#
// # Version 2.4: April 11, 2007. Fully functional input fields									#
// # Requires zXML for cross browser support													#
// # Tested in IE6, IE7, Firefox2																#
// # All rights reserved Mauricio Giraldo Mutis													#
// # (R) 2006-2007																				#
// ##############################################################################################

// Load HTTP Content

function loadWSHttp(webservice,div)
{
	obj = document.getElementById(div);
	obj.innerHTML = "Loading... <img src='images/ajax_loader.gif' width='16' height='16' />";
	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("get",webservice,true);
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState == 4){
			obj.innerHTML = oXmlHttp.responseText;
		}
	}
	oXmlHttp.send(null);
}

function loadWSHttpPersonalized(webservice,div,message)
{
	obj = document.getElementById(div);
	obj.innerHTML = message + "<img src='images/ajax_loader.gif' width='16' height='16' />";
	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("get",webservice,true);
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState == 4){
			obj.innerHTML = oXmlHttp.responseText;
		}
	}
	oXmlHttp.send(null);
}
// Load XML Document
function loadXMLWS(webservice,action,ID,functionName)
{
	var loading = document.getElementById("loading");
	loading.style.display = "";
	var oXmlDom = zXmlDom.createDocument();
	oXmlDom.onreadystatechange = function(){
		if(oXmlDom.readyState == 4){
			loading.style.display = "none";
			eval(functionName);
		}
	}
	oXmlDom.load(webservice);
}

// Build and HTML Table
function htmlTable()
{
	this.html = "";
	this.headers = new Array;
	this.extraCols = new Array;
	this.footer = new String;
	this.content = new Array;
	this.buildHtml = build;
	this.id = 0;
	this.mode = "full";
	this.doScript = "";
	this.bgcolor = "";
	this.cellspacing = "1";
	this.cellpadding = "1";
	this.width = "100%";
	this.webservice = "";
	function build()
	{
		var colCounter = new Number;
		colCounter = 0;
		var temp = new Array;
		this.html += "<table cellspacing = '" + this.cellspacing + "' cellpadding = '" + this.cellpadding + "' width = '" + this.width + "'>\n";
		if(this.headers.length>0)
		{
			this.html += "<tr>\n";
			for(i=0;i<this.headers.length;i++)
			{
				this.html += "<th align='center'>\n";
				this.html += this.headers[i];
				this.html += "</th>\n";
			}
			this.html += "</tr>\n";
		}
		for(i=0;i<this.content.length;i++)
		{
			var tempString = new String;
			this.html += "<tr>\n";
			tempString = this.content[i] + " ";
			try{
				temp = tempString.split("@@");
			}catch(e)
			{
				tempString = "@@";
				temp = tempString.split("@@");
			}
			for(j=0;j<temp.length;j++)
			{
				this.html += "<td>\n";
				this.html += temp[j] + "\n";
				this.html += "</td>\n";
				colCounter = j;
			}
			if(this.extraCols.length > 0)
			{
				for(k=0;k<this.extraCols.length;k++)
				{
					this.html += "<td>\n";
					this.html += this.extraCols[k].replace("{ID}",temp[this.id]).replace("{ID}",temp[this.id]).replace("{ID}",temp[this.id]);
					//this.html += this.extraCols[k].replace("{ID}",temp[this.id] + " ");
					this.html += "</td>\n";
				}
			}
			this.html += "</tr>\n";
		}
		if(this.footer.length>0)
		{
			//this.html += "<tr><td colspan='" + (colCounter + 1) + "'>[" + this.footer.replace("{ID}",temp[this.id]) + "]</td></tr>";
			this.html += "<tr><td colspan='" + (colCounter + 1) + "'>[" + this.footer + "]</td></tr>";
		}
		this.html += "</table>\n";
		return this.html;
	}
}

// Load an HTML Form
function htmlForm()
{
	this.html = "";
	this.headers = new Array;
	this.content = new String;
	this.fields = new String;
	this.fieldsMode = new Array;
	this.buildHtml = build;
	this.id = 0;
	this.mode = "full";
	this.doScript = "";
	this.bgcolor = "#EEEEEE";
	this.cellspacing = "1";
	this.cellpadding = "1";
	this.width = "100%";
	this.webservice = "";
	this.mode = "new";
	this.action = new String;
	this.actionLabel = "Submit";
	this.Combolabels = new Array;
	this.Combovalues = new Array;
	this.inputObject = new Array;
	function build()
	{
		var temp = new Array;
		this.html += "<form id='ajaxForm' name='ajaxForm' action='' method='post'>\n";
		this.html += "<table bgcolor = '" + this.bgcolor + "' cellspacing = '" + this.cellspacing + "' cellpadding = '" + this.cellpadding + "' width = '" + this.width + "'>\n";
		var tempString = new String;
		var tempFields = new String;
		tempString = this.content + " ";
		tempFields = this.fields + " ";
		fieldsArray = tempFields.split("@@");
		try{
			var temp = tempString.split("@@");
		}catch(e)
		{
			tempString = "@@";
			temp = tempString.split("@@");
		}
		for(j=0;j<temp.length;j++)
		{
			this.html += "<tr>\n";
			this.html += "<td>\n";
			if(this.headers.length>0)
			{
				this.html += this.headers[j] + "\n";
			}
			else
			{
				this.html += fieldsArray[j] + "\n";
			}
			//
			// Debug
			//
			//this.inputObject[fieldsArray[j]]
			this.html += "</td>\n";
			this.html += "<td>\n";
			try{
				if(this.inputObject[fieldsArray[j]].id == fieldsArray[j])
				{
					if(this.inputObject[fieldsArray[j]].mode == "combo")
					{
						var inputField = new htmlCombo;
						inputField.name = fieldsArray[j];
						inputField.labels = this.inputObject[fieldsArray[j]].labels;
						inputField.values = this.inputObject[fieldsArray[j]].values;
						inputField.value =  this.inputObject[fieldsArray[j]].defaultValue;
						this.html += inputField.drawHTML();
					}
					else if(this.inputObject[fieldsArray[j]].mode == "hidden")
					{						
						var inputField = new hidden;
						inputField.name = fieldsArray[j];
						inputField.value =  temp[j];//this.inputObject[fieldsArray[j]].defaultValue;
						this.html += inputField.drawHTML();
					}
					else
					{						
						var inputField = new input;
						inputField.name = fieldsArray[j];
						inputField.value =  temp[j];//this.inputObject[fieldsArray[j]].defaultValue;
						this.html += inputField.drawHTML();
					}
				}
			}catch(e){}
			this.html += "</td>\n";
			this.html += "</tr>\n";
		}
		if(this.action.length>0)
		{
			this.html += "<tr><td colspan='2'><input type='button' name='Submit' value='" + this.actionLabel + "' onClick='" + this.action + "'></td></tr>";
		}
		this.html += "</table>\n";
		this.html += "</form>";
		return this.html;
	}
}
// Load a Combo Box
function htmlCombo()
{
	this.html = new String;
	this.labels = new Array;
	this.values = new Array;
	this.name = new String;
	this.value = new String;
	this.selected = "";
	this.drawHTML = draw;
	function draw()
	{
		this.html += "<select name='" + this.name + "' id='" + this.name + "'>\n";
		for(i=0;i<this.values.length;i++)
		{
			if(this.value == this.values[i]) this.selected = " selected";
			else this.selected = "";
			this.html += "<option value='" + this.values[i] + "'" + this.selected + ">" + this.labels[i] + "</option>\n";
		}
		this.html += "</select>\n";
		return this.html;
	}
}
// Load a Text Field
function input()
{
	//var build = buildInput;
	this.html = new String;
	this.name = new String;
	this.value = new String;
	this.drawHTML = draw;
	function draw()
	{
		return "<input name='" + this.name + "' type='text' id='" + this.name + "' value='" + this.value + "'>\n";		
	}
}
// Load a Hidden Field
function hidden()
{
	//var build = buildInput;
	this.html = new String;
	this.name = new String;
	this.value = new String;
	this.drawHTML = draw;
	function draw()
	{
		return this.value + "<input name='" + this.name + "' type='hidden' id='" + this.name + "' value='" + this.value + "'>\n";		
	}
}
// Display a message in the HTML rendered
function displayMessage(msg)
{
	document.getElementById("htmlOutput").innerHTML = "<font color='#FF0000'>/!\ " + msg + "</font>";
}
// ##########################################################################
// ##########################       DATA       ##############################
// ##########################################################################
// Grab the values of the XML and return the array
function rawdataset(xml)
{
	var values = new Array;
	var root = xml.documentElement;
	var fragment = xml.documentElement;
	var dataset = root.getElementsByTagName("NewDataSet");
	try
	{
		for(j=0;j<dataset[0].childNodes.length;j++)
		{
			var temp = "";
			var node = dataset[0].childNodes;
			if(node[j].nodeName != "#text")
			{
				for(i=0;i<node[j].childNodes.length;i++)
				{
					var name = node[j].childNodes[i].nodeName;
					if(name != "#text")
					{
						var value = node[j].childNodes[i].text;
						temp += value + "@@";
					}
				}
				this.values.push(temp);
			}
		}
		return values;
	}
	catch(e)
	{
		//alert("Can't fill Dataset.");
		return false;
	}
}
// DataCollector Object
// This object generates a DataSet and a DataTable based on the user needs.
// The DataSet can be used to fill tables in html.
// The DataTabke can be used to fill forms in html.
function dataCollector()
{
	var xml = new zXmlDom.createDocument();
	this.dataset = buildDataset;
	this.datatable = buildDatatable;
	this.result = new Boolean;
	function buildDataset()
	{
		this.values = new Array;
		this.result = new Boolean;
		var root = this.xml.documentElement;
		var fragment = this.xml.documentElement;
		var xmldataset = root.getElementsByTagName("NewDataSet");
		try
		{
			for(j=0;j<xmldataset[0].childNodes.length;j++)
			{
				var temp = "";
				var node = xmldataset[0].childNodes;
				if(node[j].nodeName != "#text")
				{
					for(i=0;i<node[j].childNodes.length;i++)
					{
						var name = node[j].childNodes[i].nodeName;
						if(name != "#text")
						{
							var value = node[j].childNodes[i].text;
							temp += value + "@@";
						}
					}
					this.values.push(temp);
				}
			}
			this.result = true;
		}
		catch(e)
		{
			//alert("Can't fill Dataset.\Reason:\n" + e.description);
			this.result = false;
		}
	}
	function buildDatatable()
	{
		this.result = new Boolean;
		this.values = new Array;
		this.fields = new String;
		var root = this.xml.documentElement;
		var fragment = this.xml.documentElement;
		var dataset = root.getElementsByTagName("Table");
		try
		{
			var temp = "";
			for(i=0;i<dataset[0].childNodes.length;i++)
			{
				var name = dataset[0].childNodes[i].nodeName;
				if(name != "#text")
				{
					var value = dataset[0].childNodes[i].text;
					temp += value + "@@";
					this.fields += dataset[0].childNodes[i].nodeName + "@@";
				}
			}
			this.values.push(temp);
			this.result = true;
		}
		catch(e)
		{
			//alert("Can't fill Datatable.\Reason:\n" + e.description);
			this.result = false;
		}
		
	}
}
// This is the object that allow to work with input values
function inputObject(id,values,labels,defaultValue,mode)
{
	this.id = id;
	this.values = values;
	this.labels = labels;
	this.defaultValue = defaultValue;
	this.mode = mode;
}
// This is the object for a record in JS
function recordObject(ID,content)
{
	return true;	
}
// *********************** Ticket Framework ***********************

function getRequestBody(oForm)
{
	var aParams = new Array();
	for(var i=0;i <oForm.elements.length; i++)
	{
		var sParam = encodeURIComponent(oForm.elements[i].name);
		sParam += "=";
		sParam += encodeURIComponent(oForm.elements[i].value);
		aParams.push(sParam);
	}
	return aParams.join("&");
}

function sendRequest(oForm)
{
	//var oForm = document.forms[1];
	var sBody = getRequestBody(oForm);
	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("post", oForm.action, true);
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	oXmlHttp.onreadystatechange = function()
	{
		if(oXmlHttp.readyState == 4)
		{
			if(oXmlHttp.status == 200)
			{
				//ticketForm = document.getElementById("ticketForm");
				//ticketForm.submit();
				alert(oXmlHttp.responseText);
				//return true;
			}
			else
			{
				alert("Ticket cannot be completed. The pager cannot be contacted.");
				//alert(oXmlHttp.statusText);
				//return false;
			}
		}
	};
	oXmlHttp.send(sBody);
}

function sendPager()
{
	pagerForm = document.getElementById("pagerForm");
	ticketForm = document.getElementById("ticketForm");
	// *** Build Pager Text ***
	var ticket = new String;
	ticket = "Ticket ID # " + ticketForm.id.value + ": ";
	ticket += ticketForm.url.value + ", ";
	ticket += ticketForm.detail.value + ", ";
	// ************************
	pagerForm.txtMensaje.value = ticket;
	sendRequest(pagerForm);
}
