// Teuker was the younger half-brother of Ajax.

// This object is dependant on mr.js

function teukerObject ()
{}

teukerObject.prototype.processResponse = function(xmlString)
{
//alert('teuker->processResponse');
	var xml = mr.xmlRoot(xmlString);
	if (xml.nodeName != 'teuker')
		alert('Unknown teuker response (invalid XML?!)');
	else
	{
		// Evaluate each child as a separate activity
                for (var i=0; i<xml.childNodes.length; i++)
                {
			var cmd = xml.childNodes[i];
//alert(cmd.nodeName);
			switch (cmd.nodeName)
			{
                        case "alert":
				alert(cmd.textContent);
				break;
                        case "createTableHack":
				this.createTableHack(cmd);
				break;
                        case "dhtmlXGrid":
				this.dhtmlXGrid(cmd);
				break;
                        case "setSelectOptions":
				this.setSelectOptions(cmd);
				break;
                        case "setCheckboxes":
				this.setCheckBoxes(cmd);
				break;
                        case "setSuggestions":
				this.setSuggestions(cmd);
				break;
                        case "unsetSuggestions":
				this.unsetSuggestions(cmd);
				break;
			default:
				alert('Unknown teuker command:'+cmd.nodeName);
			}
		}
	}
	delete xml;
};

teukerObject.prototype.createTableHack = function(node)
{
//alert('teuker->createTableHack');
	cs.removeStockTableRows();
	var tbl = document.getElementById('cs_stockTable');
	tbl.visibility="hidden";
	var lastRow = tbl.rows.length;

	for (var i=0; i<node.childNodes.length; ++i, ++lastRow)
	{
		var el = node.childNodes[i];
		var row = tbl.insertRow(lastRow);

		var qty = el.getAttribute('qty');
		var part = el.getAttribute('part');
		var cyl = el.getAttribute('cyl');
		var desc = el.getAttribute('desc');

		mr.setText (mr.appendChild (row, "td", {className : 'qty'} ), qty);
		mr.setText (mr.appendChild (row, "td", {className : 'part'}), part);
		mr.setText (mr.appendChild (row, "td", {className : 'cyl'}), cyl);
		mr.setText (mr.appendChild (row, "td", {className : 'desc'}), desc);
	}
	tbl.visibility="visible";
}

teukerObject.prototype.dhtmlXGridList = function() {}

teukerObject.prototype.dhtmlXGrid = function(node)
{
//alert("teuker response dhtmlXGrid:"+node.getAttribute('ID'));

	// Generate the grid name and the div to which to attach
	// the grid from the ID in the message.  At some point
	// we may need a more generic means of identifying the
	// div into which to attach the grid.  Perhaps an optional
	// 'loc=' attribute of the message

	var gridName = node.getAttribute('ID');
	var gridDivName = gridName+'Grid';
	var dhtmlXGridName = 'grid' + gridName;

	// The grid is stored in the list maintained by this object.
	// If it is found, destroy it, then create a new grid.

	if (this.dhtmlXGridList[dhtmlXGridName] != null)
		this.dhtmlXGridList[dhtmlXGridName]=this.dhtmlXGridList[dhtmlXGridName].destructor();
	this.dhtmlXGridList[dhtmlXGridName] = new dhtmlXGridObject(gridDivName);
	var grid = this.dhtmlXGridList[dhtmlXGridName];

	grid.setImagePath("imgs/"); // !!! Fix

	// Load header data first

	for (var i=0; i<node.childNodes.length; ++i)
	{
		var el = node.childNodes[i];
		var txt = el.firstChild.data;
		switch (el.nodeName)
		{
			case 'colHeaders':	grid.setHeader(txt);		break;
			case 'colInitialWidth':	grid.setInitWidths(txt);	break;
			case 'colAlign':	grid.setColAlign(txt);		break;
			case 'colType':		grid.setColTypes(txt);		break;
			case 'colSortType':	grid.setColSorting(txt);	break;
			case 'colColor':	grid.setColumnColor(txt);	break;
			case 'rowDelimiter':	grid.setDelimiter(txt);		break;
		}
	}
//	grid.setHeader("??? WO#, BW PO#, Cust PO#, Order rcvd, BOC order, BOC shipped, Cust rcvd, Name on POD, Days at BOC, Days to cust");
//	grid.setInitWidths("50,50,80,80,80,80,80,200,80,80");
//	grid.setColAlign("center,center,center,center,center,center,center,center,center,center");
//	grid.setColTypes("ro,ed,ro,ro,ro,ro,ro,ro,ro,ro"); // edit column 2
//	grid.setColSorting("int,int,str,date,date,date,date,str,int,int");
//	grid.setColumnColor("white,white,white,white,white,#d5f1ff");

	// This is a generic wrapper which uses closure to bind the function to
	// the grid and its name.  The grid is used to obtain the updated value
	// and the grid name is passed to the generic 'doOnCellChange()' function
	// for further processing.

	grid.setOnEditCellHandler(
		function(stage,rId,cIn)
		{
//alert (stage);
			if(stage==2) {
				var newVal = grid.cells(rId,cIn).getValue();
//alert (newVal);
				return doOnCellChanged(gridName,rId,cIn,newVal);
			}
			return true;
		}
	);	// Make generic

	grid.setOnCheckHandler(
		function(rId,cIn,isChecked)
		{
//alert (rId+"~"+cIn+"~"+isChecked);
			var cell = grid.cells(rId,cIn);
//			var disabled = cell.isDisabled();
 // alert('disabled:'+disabled);
			return doOnCellChanged(gridName,rId,cIn,isChecked);
		}
	);	// Make generic

	grid.init();

	// Now load row data

	for (var i=0; i<node.childNodes.length; ++i)
	{
		var el = node.childNodes[i];

		if (el.nodeName == 'row')
		{
			var ID = el.getAttribute('ID');
			var txt = el.firstChild.data;
//alert("row "+ID+":"+txt);
			grid.addRow(ID, txt);
//alert('back');
		}
	}
}

teukerObject.prototype.suggest_over = function()
{
	this.className = 'suggestLinkOver';
}

teukerObject.prototype.suggest_out = function()
{
	this.className = 'suggestLink';
}

teukerObject.prototype.set_search = function()
{
	alert('Select id:'+this.id+'.  You are seeing this message because you did not code a callback in the Teuker response.');
}

teukerObject.prototype.setSuggestions = function(node)
{
	var parent = node.getAttribute('within');
//alert('teuker->setSuggestions within '+parent);
	element = document.getElementById(parent);
	element.style.visibility = "hidden";
	element.style.display = "block";
	mr.setAttribute(element, "class", "suggestSearch");

	mr.removeAllChildren(element);

	var callback = node.getAttribute('callback');
//alert('callback:'+callback);

	for (var i=0; i<node.childNodes.length; i++)
	{
		var sel = node.childNodes[i];
		var suggestion = sel.getAttribute('name');
		var uid = sel.getAttribute('uid');
//alert(suggestion+' - '+uid);
		div =  mr.appendChild (element, "div");
		mr.setText(div, suggestion);
		div.id = uid;

		// Negative UIDs are used to "annotate" the
		// suggestion.  For example, to note that
		// there are no valid selections.

		if (uid >= 0)
		{
			div.onmouseover=this.suggest_over;
			div.onmouseout=this.suggest_out;
		}
		if (callback == null)
			div.onclick=this.set_search;
		else
			eval('div.onclick='+callback);
		div.className = 'suggestLink';
	}
	element.style.visibility = "visible";
}

teukerObject.prototype.unsetSuggestions = function(node)
{
//alert('teuker->unsetSuggestions');
	var parent = node.getAttribute('within');
	element = document.getElementById(parent);
	mr.removeAllChildren(element);
//	element.style.visibility = "hidden";
	element.style.display = "none";
	var callback = node.getAttribute('callback');
//alert('callback:'+callback);
	if (callback != null)
		eval(callback);
}

teukerObject.prototype.setCheckBoxes = function(node)
{
//alert('teuker->setCheckBoxes');
	var parent = node.getAttribute('within');
	element = document.getElementById(parent);
	element.style.visibility = "hidden";

	mr.removeAllChildren(element);

	for (var i=0; i<node.childNodes.length; i++)
	{
		var box = node.childNodes[i];
		var boxName = box.getAttribute('name');
//alert('boxName:'+boxName);
		var label = box.getAttribute('label');
		var checked = box.getAttribute('checked');
		var disabled = box.getAttribute('disabled');

		var options = {"type":"checkbox", "name":boxName, "value":"1"};
		if (checked != null && checked != "0" && checked != "false")
			options.checked = "checked";
		if (disabled != null && disabled != "0" && disabled != "false")
			options.disabled = "disabled";
		var labelNode =  mr.setText(mr.appendChild (element, "label"), label);
		var inputNode =  mr.appendChild(labelNode, "input", options);
	}
	element.style.visibility = "visible";
}

teukerObject.prototype.setSelectOptions = function(node)
{
//alert('teuker->setSelectOptions');
	var parent = node.getAttribute('within');
	element = document.getElementById(parent);
	element.style.visibility = "hidden";
	mr.removeAllChildren(element);

	for (var i=0; i<node.childNodes.length; i++)
	{
		var opt = node.childNodes[i];
		var optName = opt.getAttribute('name');
		var value = opt.getAttribute('value');
		var selected = opt.getAttribute('selected');

		var options = {"value":value};
		if (selected != null && selected != "0" && selected != "false")
			options.selected = "selected";

		var optionNode =  mr.setText (mr.appendChild(element, "option", options), optName);
	}
	element.style.visibility = "visible";
}

var tk = new teukerObject;

