// from old /includes/common.js

var isDOM=document.getElementById?true:false
var isOpera=isOpera5=window.opera && isDOM
var isOpera6=isOpera && window.print
var isOpera7=isOpera && document.readyState
var isMSIE=isIE=document.all && document.all.item && !isOpera
var isStrict=document.compatMode=='CSS1Compat'
var isNN=isNC=navigator.appName=="Netscape"
var isNN4=isNC4=isNN && !isDOM
var isMozilla=isNN6=isNN && isDOM
var statusCode = ""; 
var delayedLoadLayers=new Object();
var delayedLoadLayersOnLoad=new Object();
var delayedLoadXmlHttp=new Object();
var currentTabs=new Object();

if( (document.compatMode)&& (document.compatMode == 'CSS1Compat') &&(document.documentElement)) {
  window.readScroll = document.documentElement;
} else if(document.body) {
  window.readScroll = document.body;
}
else if( document.documentElement )
{
	 window.readScroll = document.documentElement;
}


function getWindowScrollLeft()
{
	if( window.pageXOffset > 0 ) { return window.pageXOffset; }
	else { return window.readScroll.scrollLeft || 0; }
}

function getWindowScrollTop()
{
	if( window.pageYOffset > 0 ) { return window.pageYOffset; }
	else { return window.readScroll.scrollTop || 0; }
}

function Point( x ,y ) { this.x=x; this.y=y; }

Pnt=Point.prototype
function getRect(elem )
{
// return absolute position of elem
// (Left, Top, Right, Bottom) in page
	var i;
	if (typeof(elem.myRect)!="undefined") return elem.myRect;
	var myRect = Array(elem.offsetLeft, elem.offsetTop,
	elem.offsetLeft + elem.offsetWidth-1,
	elem.offsetTop + elem.offsetHeight-1)
	if (!elem.offsetParent)
	{
		var xAdjust = (window.readScroll.clientLeft||0);
		var yAdjust = (window.readScroll.clientTop||0);
		for (i=0;i<4;i++) myRect[i] += (i%2)?yAdjust:xAdjust;
		return myRect;
	}
	newRect = getRect(elem.offsetParent)
	for (i=0;i<4;i++) myRect[i] = myRect[i] + newRect[i % 2];
	return myRect;
}

function HTMLLayer( name ) {
	if( typeof(name)=='string' )
	{
		this.name=name;
		this.getObj(name);
	}
	else
	{
		this.obj = name;
		this.style = this.obj.style?this.obj.style:this.obj;
	}
}

HTMLL=HTMLLayer.prototype

HTMLL.focus=function() { this.obj.focus(); }
HTMLL.getObj=function(name)
{
	this.name=name;
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
  }
  else if (document.all)
  {
		this.obj = document.all[name];
  }
  else if (document.layers)
  {
		this.obj = getObjNN4(document,name);
  }
  if( this.obj==null) { alert(name); }
  else
  {
		this.style = this.obj.style?this.obj.style:this.obj;
	}
  return;
}
HTMLL.getValue=function() { return this.obj.value; }
HTMLL.setValue=function( val) { return this.obj.value=val; }
HTMLL.getLeft=function() { return getRect(this.obj)[0]; }
HTMLL.getWidth=function() {
	var rect=getRect(this.obj);
  return rect[2]-rect[0];
}
HTMLL.getHeight=function() {
	var rect=getRect(this.obj);
  return rect[3]-rect[1];
}

HTMLL.getTop=function() { return getRect(this.obj)[1]; }

HTMLL.getObjNN4=function (obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}
HTMLL.write=function(text)
{
	if (document.getElementById || document.all)
	{
		this.obj.innerHTML = '';
		this.obj.innerHTML = text;
	}
	else if (document.layers)
	{
		text2 = '<P CLASS="testclass">' + text + '</P>';
		this.obj.document.open();
		this.obj.document.write(text2);
		this.obj.document.close();
	}
}
function getMouseClickPosition( e,relTo )
{
 	var out=new Point(0,0);
  if ( !e )
  {
  	var target = new HTMLLayer(event.target || event.srcElement);
  	out.x = window.event.x + getWindowScrollLeft()+target.getLeft()- relTo.getLeft();
  	out.y = window.event.y + getWindowScrollTop()+target.getTop()- relTo.getTop();
  }
 	else if ( e.clientX || e.clientY )
	{
	  out.x = e.clientX + getWindowScrollLeft() - relTo.getLeft();
		out.y = e.clientY + getWindowScrollTop()- relTo.getTop();
  }
	else if ( e.pageX || e.pageY )
	{
    out.x = e.pageX - relTo.getLeft();
	  out.y = e.pageY - relTo.getTop();
	}

  return out;
};
function getObj(name)
{
  if (document.getElementById) {
  	this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
  } else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
  } else if (document.layers) {
		this.obj = getObjNN4(document,name);
		this.style = this.obj;
  }
  return this.obj;
}
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) curtop += obj.y;
	return curtop;
}

  function GetXmlHttpObject() { 
		http_request = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				 http_request.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject) { // IE
			try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e) {
				try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
				catch (e) { }
			}
  }
  return http_request;
}

function queryStringToArray( queryString )
{
	var out=Array();
	
	var mappings=queryString.split("&");
	
	for( var i=0;i<mappings.length;i++)
	{
		var mapping=mappings[i].split("=");
		out[URLDecode(mapping[0]==null?"":mapping[0])]=URLDecode(mapping[1]==null?"":mapping[1]);
	}
	return out;
};
function URLEncode( plaintext )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	plaintext=''+plaintext;
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode( encoded )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
  return  plaintext;
};

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function htmlentities( s ){

    var div = document.createElement('div');
    var text = document.createTextNode(s);
    div.appendChild(text);
    return div.innerHTML;
}
var hexbase="0123456789ABCDEF";

// take a decimal integer, return 2-digit hex string
function DecToHex(number) { return hexbase.charAt((number>> 4)& 0xf)+ hexbase.charAt(number& 0xf); }

// take a hex string, return decimal integer
function HexToDec(str) { return parseInt(str.toUpperCase(), 16); }

function colorFromHexCode( col )
{
	if( col.substr(0,1)=="#") { col=col.substr(1); }
	col=col.toUpperCase();
	return new RAColor( HexToDec(col.substr(0,2)),HexToDec(col.substr(2,2)),HexToDec(col.substr(4,2)) );
}
function RAColor( red, green, blue)
{
	this.r=red;
	this.g=green;
	this.b=blue;
}

RACol=RAColor.prototype;
RACol.setFromColorString=function(str)
	{

	  if( str.substr(0,1)=="#")
	  {
	    str=str.substr(1);
	  }
		this.r=HexToDec(str.substr(0,2));
		this.g=HexToDec(str.substr(2,2));
		this.b=HexToDec(str.substr(4,2));
	}
RACol.getHex=function()
{
	return "#"+DecToHex(this.r)+DecToHex(this.g)+DecToHex(this.b);
}
RACol.getColor= function(changeTo,cur,end )
{
	r=Math.round( this.red+ ((changeTo.red-this.red)*cur/end) );
	g=Math.round( this.green+ ((changeTo.green-this.green)*cur/end) );
	b=Math.round( this.blue+ ((changeTo.blue-this.blue)*cur/end) );
	return "#"+DecToHex(r)+DecToHex(g)+DecToHex(b)
}
RACol.makeDarker=function ( amount)
{
	this.r=Math.max(0,Math.floor(this.r*(1-amount)));
	this.g=Math.max(0,Math.floor(this.g*(1-amount)));
	this.b=Math.max(0,Math.floor(this.b*(1-amount)));
}

 var colors=new Object();
 var colorsNotify=new Object();
  //colors['colorselect1']=new RAColor("#000000");
  function colorSelect(e,name,rgb) {
    var posx = 0;
	  var posy = 0;
	  if (!e) var e = window.event;
	  if (e.pageX || e.pageY) {
  		posx = e.pageX;
	  	posy = e.pageY;
	  }
	  else if (e.clientX || e.clientY) {
		  posx = e.clientX + document.body.scrollLeft;
		  posy = e.clientY + document.body.scrollTop;
	  }
	  getObj(name+rgb);
	  posx-= findPosX(this.obj);
	  if(posx<0) { posx=0; }
	  else if( posx>255) { posx=255; }
    getObj(name+rgb+"a");
    this.style.backgroundPosition=posx+"px 0px";
    (colors[name])[rgb]=posx;
    getObj(name);
    this.obj.value=colors[name].getColorString().substr(1);
    getObj(name+"col");
    this.style.backgroundColor=colors[name].getColorString();
    if( colorsNotify[name]!='' )
    {
    	eval( colorsNotify[name]+"('"+colors[name].getColorString().substr(1)+"')");
    }
  }
  RACol.getColorString=function()
	{
		return "#"+DecToHex(this.r)+DecToHex(this.g)+DecToHex(this.b);
	}
  function setColorField(name,color)
  {
    getObj(name);
    colors[name].setFromColorString(color);
    this.obj.value=colors[name].getColorString().substr(1);
    getObj(name+"col");
    this.style.backgroundColor=colors[name].getColorString();
    getObj(name+"ra");
    this.style.backgroundPosition=colors[name].r+"px 0px";
    getObj(name+"ga");
    this.style.backgroundPosition=colors[name].g+"px 0px";
    getObj(name+"ba");
    this.style.backgroundPosition=colors[name].b+"px 0px";
  }
  function updateColorField(name)
  {
  	var l=new HTMLLayer(name);
    
    colors[name].setFromColorString(this.obj.value);
    l.obj.value=colors[name].getColorString().substr(1);
    getObj(name+"col");
    this.style.backgroundColor=colors[name].getColorString();
    
    l=new HTMLLayer(name+"ra");
    l.style.backgroundPosition=colors[name].r+"px 0px";
    l=new HTMLLayer(name+"ga");
    l.style.backgroundPosition=colors[name].g+"px 0px";
    l=new HTMLLayer(name+"ba");
    l.style.backgroundPosition=colors[name].b+"px 0px";
    
  }
  function colorField( name, value,notify )
  {
  	colors[name]=colorFromHexCode(value);
  	colorsNotify[name]=notify;
    document.write("<input type='hidden' id="+name+" name="+name+" value='"+value+"'><table border=0 cellpadding=0 cellspacing=0><tr><td>");
    document.write("<div id="+name+"col align=center valign=middle style='width:80px;height:35px;border:1px solid black;background:#000000;'><div></td>");
    document.write("<td><table border=0 cellpadding=0 cellspacing=0><tr><td id="+name+"ra width=260 height=4 style='background-repeat:no-repeat;width:260;height:4;background-image:url(arrow.gif);background-position:0px 0px;'></td></tr><tr><td align=center><table border=0 cellpadding=0 cellspacing=0><tr><td id="+name+"r align=center onclick=\"colorSelect(event,'"+name+"','r');\"><img src=redcolorscale.gif width=256 height=8></td></tr></table></td></tr><tr><td id="+name+"ga width=260 height=4 style='background-repeat:no-repeat;width:260;height:4;background-image:url(arrow.gif);background-position:0px 0px;'></td></tr><tr><td align=center><table border=0 cellpadding=0 cellspacing=0><tr><td id="+name+"g align=center onclick=\"colorSelect(event,'"+name+"','g');\"><img src=greencolorscale.gif width=256 height=8></td></tr></table></td></tr><tr><td id="+name+"ba width=260 height=4 style='background-repeat:no-repeat;width:260;height:4;background-image:url(arrow.gif);background-position:0px 0px;'></td></tr><tr><td align=center><table border=0 cellpadding=0 cellspacing=0><tr><td id="+name+"b align=center onclick=\"colorSelect(event,'"+name+"','b');\"><img src=bluecolorscale.gif width=256 height=8></td></tr></table></td></tr></table></td></tr></table>");
    getObj(name);
    if( colors[name] )
    {
    	this.obj.value=colors[name].getHex().substr(1);
    	updateColorField(name);
    }
    
  }

