//UTILITY =========================== DOM ROUTINES =============================================

function handleErr(msg,url,l){
	alert("Page error.\n\nError: " + msg + "\nURL: " + url + "\nLine: " + l + "\n\nClick OK to continue.\n\n");
	return false;
}
onerror=handleErr;

function addLoadEvent(func){
	var oldld=window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
		if (oldld){
			oldld();
		}
			func();
		};
	}
}

function rN(n){if(typeof(n)==='string'){n=gID(n);} if(n.parentNode!==null){n.parentNode.removeChild(n);}}
function cUL(arr,func){
	var t=jCR(['ul']);
	for(var i in arr){
		func(t,i,arr);
	}
	return t;
}
function dSP(n,x){n.className=(x===1?'hide_me': x);}
function dP(p){if(p) {delete p;}};  // delete the property - clean-up function
function gID(txt){return document.getElementById(txt) || false;}
function aIH(a,str){a.innerHTML=str; return a;}
function cEL(n,i,c){var e=document.createElement(n); if(i){e.id=i;} if(c){e.className=c;} return e;}
function aTR(n,v){var a=document.createAttribute(n); a.value=v; return a;}
function aC(){var x=arguments.length; for(var i=0;i<x;i++){arguments[i][0].appendChild(arguments[i][1]);} return arguments[0][0];}
function nCR(n){return cEL(n.tagName, n.id, n.className);}
function tCR(s){return document.createTextNode(s);}
function jID(s){var a=s.split('#'); var b=a[0].split('.'); return {tagName: b[0], className: b[1], id: a[1]};}
function nE(n,arr) {
	if (typeof nE.control == 'undefined' ) { // we only want to check once for what type of event model is to be used
		nE.control = n.addEventListener ? 1 : 0;
		nE.str='omd,omo,omt'; // this is to allow abbrevieated forms of events to be passed.
		nE.arr=[['onmousedown','onmouseover','onmouseout'],['mousedown','mouseover','mouseout']];
	}
	var x=arr[0].slice(1);
	var event_type='';
	var e_type=nE.str.search(x);
	if(typeof(arr[1])==='string') { //if we are using the DOM 1 method, we need to substitute the "on" use for events
//		if(e_type>=0) {event_type=nE.arr[1][e_type/4];}
		event_type=e_type!=-1 ?  nE.arr[0][e_type/4] : x; // if the event is in the array - use the array full name for the event, otherwise use the name for the event passed to the function
		n[event_type]=function(){eval(arr[1]);};
	} else {
		event_type=e_type!=-1 ?  nE.arr[nE.control][e_type/4] : x; // if the event is in the array - use the array full name for the event, otherwise use the name for the event passed to the function
		// events are named differently for ie and others - this conforms the event to the browser.
		nE.control===1 ? n.addEventListener(event_type,arr[1],true) : n.attachEvent(event_type,arr[1]);
	}
}
function get_evt_idNode(e) {  // passed an event and returns the first node, target or parent(s), that have an id attribute
	var el = e.target || e.srcElement;  // ie or other
	while (!el.id)	{  
		el = el.parentNode;
	}
	return el;
}
	


function getEvent(e, el) {
	if (!e) {
		e = el ? window.event : el.document.parentWindow.event;
	}
	if (!e.srcElement) {  // .target for firefox || .srcElement for ie
		var el = e.target;
		while (el != null && el.nodeType != 1)
			el = el.parentNode;
		e.srcElement = el;
	}
	if (typeof e.offsetX == "undefined") {
		e.offsetX = e.layerX;
		e.offsetY = e.layerY;
	}
	return e;
}
function jCR(j){
	if(j.nodeType){return j;}
	if(typeof(j)==='string'){return (j.slice(0,1)==='~'|| j.slice(0,1)==='^') ? j : tCR(j);}  // if this is a string and not an array
	if(j[0])
	{
		if(j[0].charAt(0)==='@'){return aTR(j[0].slice(1), j[1]);}

		var e=nCR(jID(j[0]));
		for (var i=1;i<j.length;i++){
			if(typeof(j[i][0])==='string' && j[i][0].charAt(0)==='^'){  // ie is screwey on setting events via attributes, so we capture the item and set teh event via javascript.  Becuase it is in an i loop, ie againg makes it difficult and we need to indirectly set the event so that the i is currently interpreted as opposed to the last set value
				nE(e,j[i]);
			} else if(typeof(j[i][0])==='string' && j[i][0].charAt(0)==='~'){
				aIH(e,j[i][0].slice(1));
			} else {
				var c = jCR(j[i]);
			(c && (c.nodeType===2)) ? e.setAttributeNode(c) : e.appendChild(c);
			}
		}
		return e;
	}
	return null;
}
var ajax={

	req:  function(str,url,req_no){
		//	if(str==='x') {return;}
		//	alert(str);
		//	return;
		this.x = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
		var x=this.x;
		x.open("POST",url,true);
		x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		x.setRequestHeader('Connection','close');
		x.onreadystatechange=function(){ajax.stateChanged(req_no);};
		x.setRequestHeader('Content-length', str.length);
		x.send(str);
		this.req_no=req_no;
		},

	stateChanged: function (req_no){
		if (this.x.readyState===4 || this.x.readyState==="complete") this.ajax_received();
	},

	ajax_received: function(){
		var txt = this.x.responseText;
		if (typeof this.req_no=='function'){
			this.req_no(txt);
		} else {
		//	if(response[0] !== 0) {
		//		gID('Message').value = 'there was an error';
		//	} else {
		if(this.req_no===4){
		alert(txt);
	}
		if(this.req_no===5){
		admin_show_mat(txt);
	}
		if(this.req_no===6){
		admin_show_col(txt);
	}
		if(this.req_no===7){
		alert('colors have been set and value is ' + this.req_no + ' and text is ' + txt);
	}

		}
	}
};
