
var SdCommon = {};		// public  global identifiers
var $_Common = {};		// private global identifiers

$_Common.cache = null;	// {};		// do not reload responses from same url

$_Common.debug = false;
$_Common.proxyPrefix = 'missing-proxy-prefix';

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.require = function (module) // module="public global identifier" hash
{
	if (!module)
		SdCommon.error("required module is missing");
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.warn = function (msg, link)
{
	if (!$_Common.debug) return;
	if (link)
	{
		SdCommon.lnk(" * * * ERROR: " + msg, link);
	}
	else
	{
		SdCommon.log(" * * * ERROR: " + msg);
	}
	alert(msg + link); 
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.error = function (msg, link)
{
	if (!$_Common.debug) return;
	SdCommon.warn(msg, link);
	throw msg;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.log = function (msg)
{
	// <div id="sirsi-dynix-log">
	//   <hr>
	//   <b>SirsiDynix Log:</b>
	//   <ol id = "sirsi-dynix-log-list">
	//      : ... attach new nodes here ...
	//   </ol>
	//   <hr>
	// </div>

	var  listNode = document.getElementById("sirsi-dynix-log-list");
	if (!listNode) listNode = $_Common.createLogListNode();
	if (!listNode) return;

	var itemNode = document.createElement("li");
	var textNode = document.createTextNode(msg);

	itemNode.appendChild(textNode);
	listNode.appendChild(itemNode);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.lnk = function (label, url)
{
	var  listNode = document.getElementById("sirsi-dynix-log-list");
	if (!listNode) listNode = $_Common.createLogListNode();
	if (!listNode) return;

	var   itemNode = document.createElement("li");
	var anchorNode = document.createElement("a");
	var  labelNode = document.createTextNode(label);
	var   textNode = document.createTextNode(url);

	anchorNode.setAttribute("href"  ,    url );
	anchorNode.setAttribute("target","_blank");
	anchorNode.appendChild(textNode);

	itemNode.appendChild( labelNode);
	itemNode.appendChild(anchorNode);
	listNode.appendChild(  itemNode);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

$_Common.createLogListNode = function ()
{
	var node = document.getElementById("sirsi-dynix-log");
	if (node != null)
	{
		// use the "log" node from the page file
		
		// which might be:
		//
		//  <div id="sirsi-dynix-log" style=display:none></div>
		//
		// if so, fix the style to be visible ...

		var style = node.getAttribute("style");
		if (style)  node.style.display="block";
	}
	else
	{
		// create a new "log" node

		var node = document.createElement("div");
		node.id = "sirsi-dynix-log";

		var elements = document.getElementsByTagName("body");
		var body     = ( (elements && elements.length) ? elements[0] : null );

		if (!body) {
			var noBodyMsg = "missing '<body>' tag in html";
			alert(noBodyMsg);
			throw noBodyMsg;
		}

		var first = body.firstChild;
		if (first)
		{
			body.insertBefore(node, first);
		}
		else
		{
			body.appendChild(node);
		}
	}

	// <div id="sirsi-dynix-log">
	//   <hr>
	//   <b>SirsiDynix Log:</b>
	//   <ol id = "sirsi-dynix-log-list">
	//      :
	//   </ol>
	//   <hr>
	// </div>

	var hiBr = document.createElement ("br");
	var hiHr = document.createElement ("hr");
	var loBr = document.createElement ("br");
	var bold = document.createElement ("b");
	var text = document.createTextNode("SirsiDynix Log:");
	var list = document.createElement ("ol");
	var loHr = document.createElement ("hr");

	list.id = "sirsi-dynix-log-list";

	node.appendChild(hiBr);
	node.appendChild(hiHr);
	node.appendChild(loBr);
	bold.appendChild(text);
	node.appendChild(bold);
	node.appendChild(list);
	node.appendChild(loHr);

	return list;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.trim = function (text) {
	return text ? text.replace(/^\s+|\s+$/g,"") : null;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.setProxifyPrefix = function (prefix)
{
    $_Common.proxyPrefix = prefix;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.proxify = function (url)
{
        var proxy_url = "/uhtbin/" + $_Common.proxyPrefix + "?" + url; // "/cgi-bin/ptp.sh?" + url; or "/cgi-bin/ptp.exe?" + url;   // "/cgi-bin/passThruProxy.pl?" + url;
        return proxy_url;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.load_script = function (url, task)
{
	var ajaxlet = new SdEcStats.Ajaxlet((task ? task : "UNKNOWN"), "jsf" ,(url ? url : "UNKNOWN"));
	SdEcStats.topOfRequestingData(ajaxlet);
	SdEcStats.addNote(ajaxlet, "javascript");

	var script = document.createElement("script");

	script.type = "text/javascript";
	script.src = url;
	script.charset = "utf-8";

//	SdCommon.lnk("loading: ", script.src);

	Event.observe( script, 'load',
		function (event)
		{
			SdEcStats.endOfRequestingData(ajaxlet);
//			SdEcStats.topOfProcessingData(ajaxlet);
//			SdEcStats.endOfProcessingData(ajaxlet);
		}
	);

	var head = document.getElementsByTagName('head')[0];
	head.appendChild(script);

}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.mapToText = function (map)
{
	if (!map) return "no map";
	var text = "{";
	var name;
	for (name in map)
	{
		var value = map[name];
		if (typeof value === 'function') continue;
		if (text.length > 1) text = text + ", ";
		text = text + name + " : " + value;
	}
	text = text + "}";
	return text;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.filterArrayOfNodesByAttribute = function (array, attributeName, operator, attributeValue)
{
	if (!operator      ) operator       = null;
	if (!attributeValue) attributeValue = null;

	var list  = new Array();
	var count = 0;
	
	var length = (array ? array.length : 0);
	for (var k = 0; k < length; k++)
	{
		var element = array[k];

		var  elementValue = element.getAttribute(attributeName);
		if (!elementValue) elementValue = null;

		if (operator == "==")
		{
			if (elementValue == attributeValue)
			{
				list[count++] = element;
			}
		}
		else if (operator == "!=")
		{
			if (elementValue != attributeValue)
			{
				list[count++] = element;
			}
		}
		else if (operator != null)
		{
			throw("unknown operator '" + operator + "'");
		}
		else if (elementValue == undefined)
		{
			// null operator matches any attribute that EXISTS
		}
		else
		{
			list[count++] = element;
		}
	}

	return list;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.addLoadEvent = function (func)
{
	try {
		// special case: window.onload was mangled by prototype.js
		//		 ... so do things the prototype.js way ...

		Event.observe(window, 'load', func);
		return;

	} catch (bogus) {

		// 'usual' case: prototype.js not included
	}


	// code from the book 'DOM Scripting" by Jeromy Keith
	// as found in various places on the internet -- just
	// google for 'addLoadEvent" ...


	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		var oldonload = window.onload;
		window.onload = function() {
			oldonload();
			func();
		};
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdCommon.addLoadEvent(SdEcTimer.IncDoneOnLoadEvent);

