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

function manipulateLinks()
	{
	var lnks = document.getElementsByTagName("a")
	for (var i = 0; i < lnks.length; i++)
		{
		if (lnks[i].getAttribute("rel") == "new")
			{
			lnks[i].onclick = openNewWindow;
			}
		}
	}

function openNewWindow()
	{
	window.open(this.getAttribute("href"));
	return false;
	}

function getPageSize()
	{
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY)
		{	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
		}
	else if (document.body.scrollHeight > document.body.offsetHeight)
		{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
		}
	else
		{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
		}

	var windowWidth, windowHeight;
	if (self.innerHeight)
		{	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
		}
	else if (document.documentElement && document.documentElement.clientHeight)
		{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
		}
	else if (document.body)
		{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
		}	

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
		 {
		 pageHeight = windowHeight;
		 }
	 else
		 { 
		 pageHeight = yScroll;
		 }

	 // for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
		{	
		pageWidth = windowWidth;
		}
	else
		{
		pageWidth = xScroll;
		}
	return {pWidth: pageWidth, pHeight: pageHeight, wWidth: windowWidth, wHeight: windowHeight};//arrayPageSize;
	}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	//arrayPageScroll = new Array('',yScroll) 
	return yScroll;
}

function getThisElement(e)
	{
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType && targ.nodeType == 3) //SafariBug
		targ = targ.parentNode;
	
	//alert(targ.nodeName);
	return targ;
	}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function sendRequest(met,url,callback,parameters)
    {
    var req = createXMLHTTPObject();
    if (!req) return false;

	req.open(met, url, true);

	req.onreadystatechange = function ()
    	    {
	        if (req.readyState != 4) return;
    	    if (req.status != 200 && req.status != 304)
        	    {
	            alert('HTTP error ' + req.status);
    	        return;
        	    }
        	callback(req);
        	}


	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("X-Ajax", "true");
	//req.setRequestHeader("Content-length", parameters.length);
	//req.setRequestHeader("Connection", "close");
	var par = null;
	if (parameters != false) par = parameters;
	req.send(par);
	}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

function createDropdown(el,array,callback)
	{
	var dropdown = document.createElement('div');
	dropdown.className = "dropdown";
	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7)
		{
		var ifrm = document.createElement('iframe');
		//ifrm.className = "ieHack";
		ifrm.style.display = "block";
		ifrm.style.position = "absolute";
		ifrm.style.top = "0";
		ifrm.style.left = "0";
		ifrm.style.zIndex = "-1";
		ifrm.style.filter = "mask()";
		ifrm.style.width = "3000px";
		ifrm.style.height = "3000px";
		dropdown.appendChild(ifrm);
		}
	if (array[0] != false)
		{
		var info = document.createElement('span');
		var itext = document.createTextNode(array[0]);
		info.appendChild(itext);
		dropdown.appendChild(info);
		}

	for (var i = 1; i < array.length; i++)
		{
		var li = document.createElement('a');
		li.id = 'slc_'+(i);
		li.className = "select";
		li.appendChild(document.createTextNode(array[i]));
		li.onclick = function(e) {
							stopBubble(e);
							callback(parseInt(this.id.split('_')[1],10));
							el.nextSibling.style.visibility = "hidden";
							el.parentNode.removeChild(el.nextSibling);
							document.onclick = "";
							};
 		dropdown.appendChild(li);
		}
	el.parentNode.insertBefore(dropdown,el.nextSibling);
	document.onclick = function(e) {
									stopBubble(e);
									callback(false);
									el.nextSibling.style.visibility = "hidden";
									el.parentNode.removeChild(el.nextSibling);
									document.onclick = "";
									}
	}

function stopBubble(e)
	{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	}

// function dropSelect()
// 	{
// 	
// 	this.parentNode.style.visibility = "hidden";
// 	this.parentNode.parentNode.removeChild(this.parentNode);
// 	}

function setWarn(el,string)
	{
	el.innerHTML = string;
	}


function utf8(wide) {
  var c, s;
  var enc = "";
  var i = 0;
  while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  }
  return enc;
}

var hexchars = "0123456789ABCDEF";

function toHex(n) {
  return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}

var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

function encodeURIComponentNew(s) {
  var s = utf8(s);
  var c;
  var enc = "";
  for (var i= 0; i<s.length; i++) {
    if (okURIchars.indexOf(s.charAt(i))==-1)
      enc += "%"+toHex(s.charCodeAt(i));
    else
      enc += s.charAt(i);
  }
  return enc;
}

function buildURL(fld)
{
	if (fld == "") return false;
	var encodedField = "";
	var s = fld;
	if (typeof encodeURIComponent == "function")
	{
		// Use JavaScript built-in function
		// IE 5.5+ and Netscape 6+ and Mozilla
		encodedField = encodeURIComponent(s);
	}
	else 
	{
		// Need to mimic the JavaScript version
		// Netscape 4 and IE 4 and IE 5.0
		encodedField = encodeURIComponentNew(s);
	}
	return encodedField;
}

function loadScripts(scripts)
	{
	this.callback_ = null;
	for (var i in scripts)
		{
		var script = document.createElement('script');
		script.setAttribute('type','text/javascript');
		script.setAttribute('src', scripts[i] + "?rand=" + Math.random());
		document.getElementsByTagName('head')[0].appendChild(script);
		}
	}
	
loadScripts.prototype.setOnLoad = function(check,callback)
	{
	if (typeof callback != "function")
		{
		throw 'loadScripts.setOnLoad missing callback function';
		return false;
		}
	try
		{
		if (typeof eval(check) == "function")
			{
			callback();
			}
		}
	catch(err)
		{
		var self_ = this;
		this.timeout_ = window.setTimeout(function() {
			self_.setOnLoad(check,callback);
			},100);
		}
	}

/**
 * Namespace for ncpa helper functions
 */
function ncpa() {}

/**
 * Function derived from Prototype.js to get a given element's
 * height and width
 * @private
 * @param {Object} element The DOM element that will have height and 
 *                    width will be calculated for it.
 * @return {Object} Object with keys: width, height
 */
ncpa.prototype.getDimensions = function(element) {
  var display = this.getStyle(element, 'display');
  if (display != 'none' && display != null) { // Safari bug
    return {width: element.offsetWidth, height: element.offsetHeight};
  }

  // All *Width and *Height properties give 0 on elements with display none,
  // so enable the element temporarily
  var els = element.style;
  var originalVisibility = els.visibility;
  var originalPosition = els.position;
  var originalDisplay = els.display;
  els.visibility = 'hidden';
  els.position = 'absolute';
  els.display = 'block';
  var originalWidth = element.clientWidth;
  var originalHeight = element.clientHeight;
  els.display = originalDisplay;
  els.position = originalPosition;
  els.visibility = originalVisibility;
  return {width: originalWidth, height: originalHeight};
};

/**
 * Function derived from Prototype.js to get a given element's
 * value that is associated with the passed style
 * @private
 * @param {Object} element The DOM element that will be checked.
 * @param {String} style The style name that will be have it's value returned.
 * @return {Object}
 */
ncpa.prototype.getStyle = function(element, style)
	{
	var found = false;
	style = this.camelize(style);
	var value = element.style[style];
	if (!value)
		{
		if (document.defaultView && document.defaultView.getComputedStyle)
			{
			var css = document.defaultView.getComputedStyle(element, null);
			value = css ? css[style] : null;
			}
		else if (element.currentStyle)
			{
			value = element.currentStyle[style];
			}
		}

	if((value == 'auto') && (style == 'width' || style == 'height') && (this.getStyle(element, 'display') != 'none'))
		{
		if( style == 'width' )
			{
			value = element.offsetWidth;
			}
		else
			{
			value = element.offsetHeight;
			}
		}
//   if (window.opera && ['left', 'top', 'right', 'bottom'].include(style)) {
//     if (this.getStyle_(element, 'position') == 'static') value = 'auto';
//   }
// 	alert(element.id + ":\n" + style + " = " + value);
	return (value == 'auto') ? null : value;
};

/**
 * Function pulled from Prototype.js that will change a hyphened
 * style name into camel case.
 * @private
 * @param {String} element The string that will be parsed and made into camel case
 * @return {String}
 */
ncpa.prototype.camelize = function(element) {
  var parts = element.split('-'), len = parts.length;
  if (len == 1) return parts[0];
  var camelized = element.charAt(0) == '-'
    ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
    : parts[0];

  for (var i = 1; i < len; i++) {
    camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);
  }
  return camelized;
};

ncpa = new ncpa();