// <![CDATA[
function findPos  (obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function in_array(needle,haystack) {
	var bool = false;
	for (var i=0; i<haystack.length; i++) {
		if (haystack[i]==needle) {
			bool=true;
		}
	}
	return bool;
}

// this is needed by selector and region selector to get events 
function createMethodReference(object, methodName) {
	return function (event) {
		object[methodName].call(object, event || window.event);
	};
}

// clean up an XML/HTML node by removing all its children nodes
function removeAllChildren(element)
{
        while(element.hasChildNodes())
          element.removeChild(element.firstChild);
}

// should we even attempt to support NN4 since it shouldn't be able to 
function getObj(name) {
	if (document.getElementById) {	// (Mozilla, Explorer 5+, Opera 5+, Konqueror, Safari, iCab, Ice, OmniWeb 4.5)
		return document.getElementById(name);
	} else if (document.all) {	// (Explorer 4+, Opera 6+, iCab, Ice, Omniweb 4.2-) fuckos!
		return document.all[name];
	} else {
		alert("looks like your javascript is not standards compliant.");
	}
	// Netscape 4, Ice, Escape, Omniweb 4.2-  || don't think we need this
	// else if (document.layers) {  
	//	this = document.layers[name];
	//	this.style = document.layers[name];
	//	return this;
	//}
}

function getX(obj) {
	var curleft = 0;
	while (obj.offsetParent) {
		curleft += obj.offsetLeft
		obj = obj.offsetParent;
	}
	return curleft;
}

function getY(obj) {
	var curtop = 0;
	while (obj.offsetParent) {
		curtop += obj.offsetTop
		obj = obj.offsetParent;
	}
	return curtop;
}

// threadsafe asynchronous XMLHTTPRequest code
function makeRequest ( url, callback, callbackdata, method, postdata ){
	// we use a javascript feature here called "inner functions"
	// using these means the local variables retain their values after the outer function
	// has returned. this is useful for thread safety, so
	// reassigning the onreadystatechange function doesn't stomp over earlier requests.
	
	function bindCallback(){
		if (ajaxRequest.readyState == 0) {
			// uninitialized
		} 
		if (ajaxRequest.readyState == 1) {
			// loading
		} 
		if (ajaxRequest.readyState == 2) {
			// loaded
		} 
		if (ajaxRequest.readyState == 3) {
			// interactive
		} 
		if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
				if (ajaxCallback){
  			  if(ajaxRequest.responseXML != null) { // added to accomodate HTML requests as well as XML requests
  					ajaxCallback( ajaxCallbackData, ajaxRequest.responseXML);
  				}else{
  					ajaxCallback( ajaxCallbackData, ajaxRequest.responseText);
  				}
				} else {
					alert('no callback defined');
				}
			} else {
				alert("There was a problem retrieving the xml data:\n" + 
					ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + 
					ajaxRequest.responseText);
			}
		}
	}
	
	// use a local variable to hold our request and callback until the inner function is called...
	var ajaxRequest = null;
	var ajaxCallbackData = callbackdata;
	var ajaxCallback = callback;

	// bind our callback then hit the server...


	if (window.ActiveXObject) {
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		// moz et al
		ajaxRequest = new XMLHttpRequest();
	} else {
		alert('Your browser is not capable to run ActiveXscripts.')
	}
	ajaxRequest.onreadystatechange = bindCallback;
	if ( method == "get" || method == "delete") {
		ajaxRequest.open(method, url , true);
		ajaxRequest.send(null);
	} else {
		ajaxRequest.open(method, url , true);
		//ajaxRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		ajaxRequest.setRequestHeader("Content-Type","text/xml");
		ajaxRequest.send( postdata );
	}
}

function checkErrorXML (data, xmlResource) {
	//////////////
	var errors = xmlResource.getElementsByTagName("error");
	if (errors.length > 0 ) {
		var txt = "THERE WERE SOME ERRORS: \n";
		for (var i=0; i< errors.length; i++) {
			txt += "error " + (i +1) +  ': ' + unescape( errors[i].firstChild.nodeValue ) + "\n";
		}   
		alert( txt );
	}
}

function clog(str) {
  if(typeof(console.log) == "function") {
		console.log(str);
	}else{
		alert(str);
	}
}

// ]]>

