/* cms_ajax.js */
/*************************************************************
 * should define both setup and callback functions for 
 * routines found in reg_ajax.cgi -- read data, use transport,
 * use results in callback -- all the direct functionality.
 *************************************************************/
// pretty simple so far...


function refreshCourseSelect(xml) {
	var contenter = xml.getElementsByTagName('Content')[0];
	if(contenter) {
		//for(var i=0;i<contenter.childNodes.length;i++) {
			//alert(i+' : '+contenter.childNodes[i].data+' : '+contenter.childNodes[i].nodeName+' : '+contenter.childNodes[i].nodeType);
		//}
		var coder = contenter.firstChild.data;
		//alert(contenter+' : '+coder);
		document.getElementById('courseSelect').innerHTML = coder;
	}
	pullOverlay(xml);
	xmlErrorDisplay(xml);
	return false;
}

function showReferralDetails(xml) {
	pullOverlay(xml);
	xmlErrorDisplay(xml);
	return false;
}

function pullOverlay(xml) {
	var fullmessage = xml.getElementsByTagName('Overlay')[0];
	if(fullmessage) {
		var ttl = fullmessage.getElementsByTagName('Title')[0].firstChild.data;
		var msg = '';
		if(fullmessage.getElementsByTagName('Text')[0]) {
			msg = fullmessage.getElementsByTagName('Text')[0].firstChild.data;
		} else if(fullmessage.getElementsByTagName('Code')[0]) {
			msg = fullmessage.getElementsByTagName('Code')[0].firstChild;
		} else {
			msg = 'Error retreiving error message (fancy that)!';
		}
		createOverlay(ttl,msg);
	}
	return false;

}

function getItemData(itemCode,itemID) {
	if(REG_AJAX.HASAJAX) {
		// the good way
		REG_AJAX.postRequest('itemData',pullOverlay,null,'itemCode='+itemCode+'&itemID='+itemID);
	} else {
		// the other way
		void(window.open('./?mv=7777&itemCode='+itemCode+'&itemID='+itemID,'_itemDetail',''));
	}
}

function addOrderItem(orderID,itemCode,itemID) {
	if(REG_AJAX.HASAJAX) {
		// the good way
	} else {
		// the other way
	}
}
function remOrderItem() {
	if(REG_AJAX.HASAJAX) {
		// the good way
	} else {
		// the other way
	}
}
function remOrderItemByType() {
	if(REG_AJAX.HASAJAX) {
		// the good way
	} else {
		// the other way
	}
}



/* BELOW HERE IS NOT RELATED TO PAGE FUNCTIONALITY WHATSOEVER -- it's all for error reporting */

// wow, all this for error display... should likely be rolled up into a little ball and added to ANOTHER .js include
// should include an onload for dropping in the required stylesheet info and display element
function errorStatusDisplay(error) {
	return statusDisplay(error,'error',arguments[1]);
}

function statusDisplay(status) {
	var statele = document.createElement('span');
	var typer = arguments[1]?arguments[1]:'';
	var appender = arguments[2]?arguments[2]:'';
	if(typer) { // status type (optional)
		statele.className = typer+'Text';
	}
	var statid = 'statusDisplay'+appender;
	var statbakid = 'statusDisplayBak'+appender;
	var statcontid = 'statusDisplayContainer'+appender;

	statele.appendChild(document.createTextNode(status));
	document.getElementById(statid).appendChild(statele);
	var statwin = document.getElementById(statcontid);
	statwin.errorkillcount = 0;
	statwin.errorcount++;
	document.getElementById(statid).style.height = (15*statwin.errorcount)+'px';
	document.getElementById(statbakid).style.height = (15*statwin.errorcount)+'px';
	if(statwin.errorcount > 0) {
		statwin.style.display = 'block';
	}
	return false;
}

function xmlErrorDisplay(xml) {
	//alert(showXMLTextTree(xml.documentElement,''));
	var errarr = xml.getElementsByTagName('Error');
	for(var i=0;i<errarr.length;i++) {
		var nameapp = '';
		if(errarr[i].getAttribute('level') > 1) {
			nameapp = xml.getElementsByTagName('Caller')[0].firstChild.data;
		}
		errorStatusDisplay(errarr[i].firstChild.value,nameapp);
	}
	return false;
}

function xmlStatusDisplay(xml) {
	var statarr = xml.getElementsByTagName('Status');
	for(var i=0;i<statarr.length;i++) {
		var nameapp = '';
		if(statarr[i].getAttribute('level') > 1) {
			nameapp = xml.getElementsByTagName('Caller')[0].firstChild.data;
		}
		var typeapp = statarr[i].getAttribute('type');
		statusDisplay(statarr[i].firstChild.data,typeapp,nameapp);
	}
	return false;
}

// okay, now we're getting tricky.  This next section is for creating 
// the error display div, positioning it by the mouse cursor, and 
// making sure it maintains its short list of errors (hiding when nonexistant)

function errorDivManager() {
	var appender = arguments[0]?arguments[0]:'';
	var statwin = document.getElementById('statusDisplayContainer'+appender);
	statwin.errorkillcount++;
	if(statwin.errorkillcount>10) {
		var statwinreal = document.getElementById('statusDisplay'+appender);
		var statwinbak = document.getElementById('statusDisplayBak'+appender);
		if(statwin.errorcount>0) {
			statwinreal.removeChild(statwinreal.getElementsByTagName('span')[0]);
			statwin.errorcount--;
		}
		statwin.errorkillcount=0;
		statwinreal.style.height = (15*statwin.errorcount)+'px';
		statwinbak.style.height = (15*statwin.errorcount)+'px';
		if(statwin.errorcount < 1) {
			statwin.style.display = 'none';
		}
	}
	setTimeout('errorDivManager("'+appender+'")',200);
}


function createStatusDisplayWindow() {
	var appender = arguments[0]?arguments[0]:'';
	var stylerules = '<style type="text/css">\n';
	stylerules += '#statusDisplayContainer { position: absolute;bottom:0px;right:0px;width:300px; }\n';
	stylerules += '#statusDisplayContent { position:relative; }\n';
	stylerules += '#statusDisplayBak { position:absolute;top:0px;left:0px;background:#EFEFEF;opacity:.8;filter:alpha(opacity=80);width:300px; }\n';
	stylerules += '#statusDisplay { position:absolute;top:0px;left:0px;width:300px;font-size:8pt;overflow:visible; }\n';
	stylerules += '.Text { color:black;display:block;line-height:15px; }\n';
	stylerules += '.errorText { color:red;font-weight:bold;display:block;line-height:15px; }\n';
	stylerules += '.successText { color:green;font-weight:bold;display:block;line-height:15px; }\n';
	stylerules += '.noticeText { color:blue;display:block;line-height:15px; }\n';
	stylerules += '</style>\n';

	var bodytag = document.getElementsByTagName('body')[0];

	//OHHH this is scary
	bodytag.innerHTML += stylerules;
	
	//much better, but stylesheets are fickle.
	var statuswincont = document.createElement('div');
	statuswincont.id = 'statusDisplayContainer'+appender;
	statuswincont.style.display = 'none';
	statuswincont.style.zIndex = '1000';
	bodytag.appendChild(statuswincont);

	statuswincont.errorkillcount = 0;
	statuswincont.errorcount = 0;
	
	var statuswinint = document.createElement('div');
	statuswinint.id = 'statusDisplayContent'+appender;
	statuswincont.appendChild(statuswinint);
	
	var statuswinbak = document.createElement('div');
	statuswinbak.id = 'statusDisplayBak'+appender;
	statuswinint.appendChild(statuswinbak);
	
	var statuswin = document.createElement('div');
	statuswin.id = 'statusDisplay'+appender;
	statuswin.container = statuswincont;
	statuswinint.appendChild(statuswin);
	
	setTimeout('errorDivManager("'+appender+'")',200);

	statuswin.onclick = function() {
		this.oldborder = this.style.border;
		this.style.border = '2px solid black';
		this.oldbak = this.style.background;
		this.style.background = '#FFFFEF';
		this.container.errorkillcount = -10000;
		this.onclick = function() {
			this.style.border = this.oldborder;
			this.style.background = this.oldbak;
			this.container.errorkillcount = 9;
			this.container.errorcount = 1;
		}
	}

	
}

// these ones should live elsewhere

function showXMLTextTree(node,indent) {
  if(typeof node.nodeName == 'undefined') {
  	errorStatusDisplay('Could not process node! '+indent+' ('+node.nodeType+')');
  }
  var nodestring = indent+node.nodeName;
  //var attarr = new Array();
  //for(attname in node.attributes) {
  //	attarr[attarr.length] = attname+'='+node.getAttribute(attname);
  //}
  //if(attarr.length) {
  //	nodestring += ' ('+attarr.join(', ')+')';
  //}
  nodestring += "\n";
  for(var i = 0;i<node.childNodes.length;i++) {
  	if(node.childNodes[i].nodeType == 1) {
  		nodestring += showXMLTextTree(node.childNodes[i],indent+'--');
  	}
  }
  return nodestring;
	
}



var MOUSEX = 0;
var MOUSEY = 0;
function updateMousePos() {
	e = arguments[0];
	if (!e) var e = window.event;
	if (e.clientX || e.clientY) 	{
		MOUSEX = e.clientX + document.body.scrollLeft;
		MOUSEY = e.clientY + document.body.scrollTop;
	} else if (e.pageX || e.pageY) 	{
		MOUSEX = e.pageX;
		MOUSEY = e.pageY;
	}
	// well, this is the only place we're using it, so let's just 
	// update the error div pos here. (only the main one...)
	var statwin = null;
	if(statwin = document.getElementById('statusDisplayContainer')) {
		statwin.style.top = (MOUSEY-6)+'px';
		statwin.style.left = (MOUSEX-6)+'px';
	}
}
//onmousemove stacker
if(typeof document.onmousemove == 'function') {
	oldmousemove = document.onmousemove;
	document.onmousemove = function(e) {
		oldmousemove(e);
		updateMousePos(e);
	}
} else {
	document.onmousemove = updateMousePos;
}



// DIRECTLY INLINE, RUNS WHEN FILE IS INCLUDED...

// we're going to be using an AJAXTransport, so let's make it global
var REG_AJAX = null;

// cheesy onload adder -- should be standardized.
if(typeof onload == 'function') {
	var oldonloadRA = onload;
	onload = function() {
		oldonloadRA();
		createStatusDisplayWindow();
		REG_AJAX = new AJAXTransport('/simages/scripts/ajax/reg_ajax.cgi',errorStatusDisplay);
	}
} else {
	onload = function() {
		createStatusDisplayWindow();
		REG_AJAX = new AJAXTransport('/simages/scripts/ajax/reg_ajax.cgi',errorStatusDisplay);
	}
}
