// -----------------------------------------------
//	document.layers				NS 4                          
// document.all 					IE 4+                            
// window.opera 					Opera of some version            
// document.getElementById 	IE5+ OR NS6+/ Firefox
//  document.getElementById 	NS6+ OR Firefox
//  && !document.all                                                       
// ------------------------------------------------

var opera = window.opera ? true : false;
var ie4	 = document.all ? true : false;
var ie5	 = document.getElementById ? true : false;
var ie6 	 = false;
var ie7 	 = false;
var ns4 	 = document.layers ? true : false;
var ns5 	 = ie5;
var ff	 = document.getElementById && !document.all;
var safari = navigator.userAgent.indexOf("AppleWebKit") !=-1;


if (document.getElementById || document.getElementById && !document.all)
{
	if (navigator.appVersion.indexOf("MSIE")!=-1)
	{
		temp=navigator.appVersion.split("MSIE")
		version=parseFloat(temp[1])
		ie7 = version > 6.99;
		ie6 = version < 7 && version > 5.5;
		ie5 = version < 5.6;
		ie4 = version < 5;
	}	
	else
	{
		ie4 = ie5 = ie6 = false;
		ff = navigator.userAgent.indexOf("Firefox")!=-1 ? true : false;
		ns5 = !ff;
	}	
}

if (false)
{
	alert(
		""
		+"opera: "+ typeof(opera)+" -- "+ opera +"\n"
		+"ie5: "+ typeof(ie5)+" -- "+ ie5 +"\n"
		+"ie6: "+ typeof(ie6)+" -- "+ ie6 +"\n"
		+"ns4: "+ typeof(ns4)+" -- "+ ns4 +"\n"
		+"ns5: "+ typeof(ns5)+" -- "+ ns5 +"\n"
		+"ff: "+ typeof(ff)+" -- "+ ff +"\n"
	);
}
	


function obj_browser( )
{
	this.window = new Object();
	this.getWinSize();
	
	switch ( true )
	{
		case (ie4 || ie5 || ie6 || ie7) : this.make = 'ie'; break;
		case (ff) : this.make = 'firefox'; break;
		case (ns4 ||ns5): this.make='ns'; break;
		case (safari) : this.make = 'safari'; break;
		case (opera) : this.make = 'opera'; break;
	}
	
	this.win32 = navigator.platform.indexOf('Win') !=-1;
	this.os = navigator.platform;
	
}

obj_browser.prototype.getWinSize = function()
{
	// ------------------------------------------------------------------
	// http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
	// ------------------------------------------------------------------
	if( typeof( window.innerWidth ) == 'number' ) 
	{

		this.window['width'] = window.innerWidth;
		this.window['height'] = window.innerHeight;
	} 
	else if( document.documentElement 
		&& ( document.documentElement.clientWidth 
		|| document.documentElement.clientHeight ) ) 
	{
		this.window['width'] = document.documentElement.clientWidth;
		this.window['height'] = document.documentElement.clientHeight;
	} 
	else if( document.body 
		&& ( document.body.clientWidth || document.body.clientHeight )) 
	{

		this.window['width'] = document.body.clientWidth;
		this.window['height'] = document.body.clientHeight;
	}
}


function popapp(url, title , w, h)
{
	if(undef(w)) w = "400";
	if(undef(h)) h = "600";
	var arg = "toolbar=yes";
	arg = arg + ",resizable=yes";
	arg = arg + ",titlebar=0";
	arg = arg + ",location=0";
	arg = arg + ",directories=0";
	arg = arg + ",status=0";
	arg = arg + ",menubar=0";
	arg = arg + ",width=" + w;
	arg = arg + ",height=" + h;
	arg = arg + ",maximize=0";
	 
	window.open(url,title, arg);
}


/* ---------------------------[ event handlers ]------------------------*/

function enterpressed(e)
{
	var code;
	//if (!e) var e = window.event;
	var e = window.event;
	
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	
	return code == 13
}


function enterfires(e,what)
{
	if (!e) var e = window.event;
	var code;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;	
	
	if (code == 13) what.click();
	
	
}

function _addEvent(obj, evType, func)
{ 
	if (obj.addEventListener)
	{ 
		obj.addEventListener(evType, func, false); 
		return true; 
	} 
	else if (obj.attachEvent)
	{ 
		var r = obj.attachEvent("on"+evType, func); 
		return r; 
	} 
	return false; 
}


/* ---------------------------[ checking and type ]------------------------*/
function isAlien(a){ return isObject(a) && typeof(a.constructor) != 'function';}
function isArray(a){ return isObject(a) && a.constructor == Array; }
function isBoolean(a){ return typeof(a) == 'boolean'; }
function isFunction(a){ return typeof(a) == 'function'; }
function isNull(a){ return a === null; }
function isInt(a){ return typeof(a) == 'number'; }
function isObject(a){ return (a && typeof(a) == 'object') || isFunction(a); }
function isString(a){ return typeof(a) == 'string'; }
function undef(a){ return typeof(a) == 'undefined'; }
function isFormElement(a){ return a.constructor=='[HTMLInputElement]'; }

function empty(a,loosly)
{
	if (undef(a)) return true;
	if (isNull(a)) return true;
	if (isInt(a)) return a < 1 ;
	if (!undef(loosly) && a.match(/^[0-9]+$/)) return parseInt(a,10) == 0;
	if(isBoolean(a)) return !a;
	return count(a) < 1;
}
function count(a) 
{
	if (undef(a)) return 0;
	var c = 0;
	for (var i in a ) c++;
	return c;
}

/* ---------------------------[ page ]------------------------*/


function setpage()
{
	browser = new obj_browser();
	browser.getWinSize();
	var w = document.getElementById('page');
	var h = document.getElementById('header');
	var b = document.getElementById('toolbar');
	var f = document.getElementById('footer');
	var c = document.getElementById('content');
	var n = document.getElementById('nav'); 
	
		
	if (PAGE == 'fixed')
	{
		n.style.width = (NAV - NAVPADDING - NAVBORDER)+'px';
		var t = parseInt(NAV) + parseInt(CONTENT);
		c.style.width = (CONTENT - CONTPADDING - CONTENTBORDER)+'px';
				
		var nw = Math.round((browser.window.width - t) / 2); 		
		h.style.marginLeft= f.style.marginLeft = w.style.marginLeft = nw +'px';
		h.style.width= f.style.width = w.style.width = t +'px';
		
		b.style.marginLeft = h.style.marginLeft;
		b.style.width = h.style.width;
		if (!undef(PAGEPADDING) && PAGEPADDING > 0)
		{
			 w.style.width = (parseInt(ww) +  PAGEPADDING +PAGEBORDER) + 'px';
			 w.style.marginLeft = (nw- (PAGEPADDING/2)) +'px';
		}		
		return;
	}
	

	if (NAV.indexOf('%') != -1)
	{
		var nv = NAV.split('%');
		nv = nv[0];		
		var nw = (browser.window.width / 100 ) * nv;
	}
	else 
	{
		var nv = NAV.split('px');
		nv = nv[0];
		var nw = nv;
	}
	
	nw = nw - NAVPADDING;
	n.style.width = nw + 'px';
	c.style.width = ((browser.window.width - nw )-CONTPADDING)+'px';
	
	
	// scrollbars
	if (c.offsetHeight > browser.window.height)
	{
		c.style.width = (parseInt(c.style.width) -15) +'px';
	}	
	
	var ww = nw + (parseInt(c.style.width)) + CONTPADDING +'px';
	b.style.width = h.style.width= f.style.width = w.style.width = ww;
	
	if (!undef(PAGEPADDING) && PAGEPADDING > 0)
	{
		 w.style.width = (parseInt(ww) +  PAGEPADDING) + 'px';
	}
}


function releaseerror(frmfield)
{
	frmfield.id ="";
}



function addnav( content )
{
	var d = getId('nav');
	if (empty(d)) return;
	
	var e = document.createElement('DIV');
	e.id = 'anonymous';
	e.innerHTML = content;
	d.appendChild(e);
}


function getId( id )
{
	return (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? document.layers[id]: null;
}

function getLayer(id) {
	var els='';
	var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? document.layers[id]: null;
	if (el) els = (el.style)? el.style: el;
	return els;
}

function fadeout(id, hideafter, opa)
{
	var l = document.getElementById(id);
	if (empty(opa)) opa = 90;
	
	l.style.filter="alpha(opacity="+opa+")";
	l.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+opa+")";
	l.style.mozOpacity='0.'+opa;
	l.style.opacity='0.'+opa;

	var fadetime = !empty(FADEOUTTIME) ? FADEOUTTIME : 100;
	
	if (opa > 0)
	{
		opa = parseInt(opa) - 10;
		active_timeout = setTimeout(
			"fadeout('messagebox','"+hideafter+"','"+opa+"');"
			,fadetime
		);	
		return;
	}

	if (!empty(hideafter))
	{
		l.style.visibility="hidden";
		l.style.display="none";
	}
	
	if (!empty(active_timeout)) clearTimeout(active_timeout);
}


function fadein(id, fadeoutafter, opa)
{
	var l = document.getElementById(id);
	if (empty(opa)) opa = 10;
	
	l.style.filter="alpha(opacity="+opa+")";
	l.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+opa+")";
	l.style.mozOpacity= opa == 100 ? 100 : '0.'+opa;
	l.style.opacity= opa == 100 ? 100 : '0.'+opa;
	
	var fadetime = !empty(FADEINTIME) ? FADEINTIME : 100;
	
	if (opa < 100 )
	{
		opa = parseInt(opa) + 10;
		active_timeout = setTimeout(
			"fadein('messagebox','"+fadeoutafter+"','"+opa+"');"
			,fadetime
		);
		return;
	}
	
	if (!empty(fadeoutafter))
	{
		//active_timeout = setTimeout("fadeout('messagebox',1);",FADEWAIT);
		return;
	}
	if (typeof(active_timeout) !== 'undefined' ) clearTimeout(active_timeout);
}


/* ---------------------------[ forms ]------------------------*/

function checkall(frm, _name)
{
	if (undef(frm) || undef(frm.elements) ) return;

	for (var i =0; i < frm.elements.length; i++)
	{
		if ( frm.elements[i].type == 'checkbox' )
		{
			if (frm.elements[i].name.indexOf(_name)!=-1)
			{
				frm.elements[i].checked = true;
			}
		}
	}
}

function checknone(frm, _name)
{
	if (undef(frm) || undef(frm.elements) ) return;

	for (var i =0; i < frm.elements.length; i++)
	{
		if ( frm.elements[i].type == 'checkbox' )
		{
			if (frm.elements[i].name.indexOf(_name)!=-1)
			{
				frm.elements[i].checked = false;
			}
		}
	}
}

function checkonly(frm, _name)
{
	if (undef(frm) || undef(frm.elements) ) return;

	for (var i =0; i < frm.elements.length; i++)
	{
		if ( frm.elements[i].type == 'checkbox' )
		{
			if (frm.elements[i].name == _name)
			{
				frm.elements[i].checked = true;
			}
			else
			{
				frm.elements[i].checked = false;
			}
		}
	}
}


function togglebilling()
{
	var frm = document.domainreg;
	if (undef(frm) || undef(frm.elements) ) return;
	for (var i =0; i < frm.elements.length; i++)
	{
		if (frm.elements[i].name.match(/^billing/))
		{
			frm.elements[i].disabled = frm.billasowner.checked;
		}
	}
}

/* ---------------------------[ interaction ]------------------------*/


function clicktrigger(_item,_data,_scope)
{
	var f = document.main;
	if ( typeof(a) != "undefined" )
	{
		trigger(_item,_data,_scope);
		return;
	}
	
	var f = document.createElement("FORM");
	f.name="main";
	f.method="post";
	
	var iq = document.createElement("INPUT");
	iq.name = "q";
	iq.type="hidden";
	var idata = document.createElement("INPUT");
	idata.name = "data";
	idata.type="hidden";
	var iscope = document.createElement("INPUT");
	iscope.name = "scope";
	iscope.type="hidden";
	var itrigger = document.createElement("INPUT");
	itrigger.name = "trigger";
	itrigger.type="hidden";
	f.appendChild(iq);
	f.appendChild(idata);
	f.appendChild(iscope);
	f.appendChild(itrigger);
	document.body.appendChild(f);
	
	trigger(_item,_data,_scope);
}

function select2trigger(obj)
{
	var opt = obj.options[obj.selectedIndex].value;			
	if (opt.length < 1) return;
	var d = opt.split(";");
	if (d.length < 3 ) return;
	
	trigger(d[0],d[1],d[2]);
}


function directtrigger(url, _item,_data,_scope)
{
	document.main.q.value = _data;
	document.main.scope.value = _scope;
	document.main.forcescope.value = _scope;
	trigger( _item, _data, _scope, url );
}

function returnLoc()
{
	if ( location.href.indexOf("#") != -1 )
	{
		var l = location.href.split("#");
		return  l[1];
	}
	return false;
}

function trigger( _item, _data, _scope, url )
{
	lhref = location.href.replace("?new=1","");
	loc = returnLoc();
	if (typeof(url) != "undefined")
	{
		lhref = url;
		loc = _data;
	}
	
	document.main.q.value = loc 
		? loc 
		: document.main.q.value;	
	var datavalue =  typeof(_scope) != "undefined"  
		?  (_data.match(":"+_scope+"$") ? _data : _data + ":" + _scope) 
		: _data ;
	document.main.data.value = datavalue;
	
	document.main.scope.value = typeof(_scope) != undefined
		? (document.main.forcescope.value !="" 
			? document.main.forcescope.value : _scope)
		: document.main.scope.value;

	document.main.trigger.value = _item.toLowerCase();
	document.main.action= loc 
		? lhref 
		: lhref + "#" + document.main.q.value; 
	document.main.action= 
		document.main.action.replace(/#+/,"#");
	


	document.main.submit();				
}

function pulltrigger( _item, _data, _scope )
{
	trigger( _item, _data, _scope );
}

function reload()
{
	document.location.href=document.location.href+"?new=1";
}

function dosearch(_q , _scope)
{
	document.main.q.value = _q;
	document.main.data.value = _q;
	document.main.scope.value = _scope;
	
	document.main.submit();
}
function witedebug( txt )
{
	witefield("debug", txt);
}

function witefield( field, txt )
{
	d = document.getElementById(field);
	d.innerHTML = txt;
}		



/*
function URLEncode( string )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = (
		"0123456789" +					// Numeric
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
		"abcdefghijklmnopqrstuvwxyz" +
		"-_.!~*'()"					// RFC2396 Mark characters
	);
	var HEX = "0123456789ABCDEF";

	var plaintext = string;
	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;
	return false;
};

function URLDecode( string )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   
   var encoded = string;
   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;
};



var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function base64_encode(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}



function base64_decode(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

*/

