//BASE FUNCTIONS
function precisionAdjust(val,precision) {
	//Rounds the given value to the given number of decimal places.
	var multi=Math.pow(10,precision)
	return Math.round(val*multi)/multi
}
function addPercent(val,percent) {
	//Adds the given percentage onto the given value and returns.
	var multi=percent/100
	multi+=1.0
	return val*multi
}
function addObjPercent(obj,percent) {
	//Adds the given percentage onto the object's value.
	obj.value=addPercent(obj.value,percent)
}
function addObjPercentWithPrecision(obj,percent,precision) {
	//Adds the given percentage onto the object's value, and rounds
	// to given precision and then stores back.
	obj.value=precisionAdjust(addPercent(obj.value,percent),precision);
}
function runObjCalc(obj) {
	//Performs the calculation found in the object and stores it back
	// into the object
	obj.value=eval(obj.value);
}
var spawnedSub
function spawnSmall(url) {
	if (!window.spawnedSub) {
		//Create a new window
		spawnedSub=window.open(url,"spawnedSub","width=295,height=295,toolbar=0,status=0,location=0,menubar=0,scrollbars=0,resizable=0");
	} else {
		//Window already exists
		if (!spawnedSub.closed) {
			//Bring into focus
			spawnedSub.focus();
		} else {
			//Reopen
			spawnedSub=window.open(url,"spawnedSub","width=295,height=295,toolbar=0,status=0,location=0,menubar=0,scrollbars=0,resizable=0");	
		}
	}
}

//Object showing/hiding functions
function hideObjsOfClass(objs,classname) {
	if (!objs) return;
	if (!classname) return;
	for (var i=0;i<objs.length;i++) {
		if (objs[i].className.substring(0,classname.length)==classname) {
			if (objs[i].className.substring(objs[i].className.length-4)!="hide") {
				objs[i].className=objs[i].className+" hide";
			}
		}
	}
}
function showObjsOfClass(objs,classname) {
	if (!objs) return;
	if (!classname) return;
	for (var i=0;i<objs.length;i++) {
		if (objs[i].className.substring(0,classname.length)==classname) {
			if (objs[i].className.substring(objs[i].className.length-4)=="hide") {
				objs[i].className=objs[i].className.substring(0,objs[i].className.length-5);
			}
		}
	}
}
function showFirstObjOfClass(objs,classname) {
	if (!objs) return;
	if (!classname) return;
	for (var i=0;i<objs.length;i++) {
		if (objs[i].className.substring(0,classname.length)==classname) {
			if (objs[i].className.substring(objs[i].className.length-4)=="hide") {
				objs[i].className=objs[i].className.substring(0,objs[i].className.length-5);
				return; //Found one - done
			}
		}
	}
}
function toggleFirstObjOfClass(objs,classname) {
	if (!objs) return;
	if (!classname) return;
	for (var i=0;i<objs.length;i++) {
		if (objs[i].className.substring(0,classname.length)==classname) {
			if (objs[i].className.substring(objs[i].className.length-5)==" hide") {
				objs[i].className=objs[i].className.substring(0,objs[i].className.length-5);
			} else {
				objs[i].className=objs[i].className+" hide";
			}
			return; //Found one - done
		}
	}
}

function showHideObj(obj) {
	if (obj.className.substring(obj.className.length-4)=="hide") {
		obj.className=obj.className.substring(0,obj.className.length-5);
	} else {
		obj.className=obj.className+" hide";
	}
}

function showObj(obj) {
	//if (!obj) return;
	if (obj.className.substring(obj.className.length-4)=="hide") {
		obj.className=obj.className.substring(0,obj.className.length-5);
		return true;
	}
	return false;
}
function hideObj(obj) {
	if (obj.className.substring(obj.className.length-4)!="hide") {
		obj.className=obj.className+" hide";
		return true;
	}
	return false;
}

//BRC based on Nifty Corners
function BRCCheck(){
if(!document.getElementById || !document.createElement)
    return(false);
var b=navigator.userAgent.toLowerCase();
if(b.indexOf("msie 5")>0 && b.indexOf("opera")==-1)
    return(false);
return(true);
}

function Rounded(selector,bk,color,size){
var i;
var v=getElementsBySelector(selector);
var l=v.length;
for(i=0;i<l;i++){
    AddTop(v[i],bk,color,size);
    AddBottom(v[i],bk,color,size);
    }
}

function RoundedTop(selector,bk,color,size){
var i;
var v=getElementsBySelector(selector);
for(i=0;i<v.length;i++)
    AddTop(v[i],bk,color,size);
}

function RoundedBottom(selector,bk,color,size){
var i;
var v=getElementsBySelector(selector);
for(i=0;i<v.length;i++)
    AddBottom(v[i],bk,color,size);
}

function RoundedBottomR(selector,bk,color,size){
var i;
var v=getElementsBySelector(selector);
for(i=0;i<v.length;i++)
    AddBottomRight(v[i],bk,color,size);
}

function AddTop(el,bk,color,size){
var i;
var d=document.createElement("b");
var lim=4;
if(size) {
if(size=="small") {lim=2}
if(size=="large") {lim=6}
}
d.className="rtop";
d.style.backgroundColor=bk;
for(i=1;i<=lim;i++){
    var x=document.createElement("b");
    x.className="r" + i + lim;
    x.style.backgroundColor=color;
    d.appendChild(x);
    }
el.insertBefore(d,el.firstChild);
}

function AddBottom(el,bk,color,size){
var i;
var d=document.createElement("b");
var lim=4;
if(size) {
if(size=="small") {lim=2}
if(size=="large") {lim=6}
}
d.className="rbottom";
d.style.backgroundColor=bk;
for(i=lim;i>0;i--){
    var x=document.createElement("b");
    x.className="r" + i + lim;
    x.style.backgroundColor=color;
    d.appendChild(x);
    }
el.appendChild(d,el.firstChild);
}

function AddBottomRight(el,bk,color,size) {
var i;
var d=document.createElement("b");
var lim=4;
if(size) {
if(size=="small") {lim=2}
if(size=="large") {lim=6}
}
d.className="rbottom";
d.style.backgroundColor=bk;
for(i=lim;i>0;i--){
    var x=document.createElement("b");
    x.className="r" + i + lim + "r";
    x.style.backgroundColor=color;
    d.appendChild(x);
    }
el.appendChild(d,el.firstChild);
}


function getElementsBySelector(selector){
var i;
var s=[];
var selid="";
var selclass="";
var tag=selector;
var objlist=[];
if(selector.indexOf(" ")>0){  //descendant selector like "tag#id tag"
    s=selector.split(" ");
    var fs=s[0].split("#");
    if(fs.length==1) return(objlist);
    return(document.getElementById(fs[1]).getElementsByTagName(s[1]));
    }
if(selector.indexOf("#")>0){ //id selector like "tag#id"
    s=selector.split("#");
    tag=s[0];
    selid=s[1];
    }
if(selid!=""){
    objlist.push(document.getElementById(selid));
    return(objlist);
    }
if(selector.indexOf(".")>0){  //class selector like "tag.class"
    s=selector.split(".");
    tag=s[0];
    selclass=s[1];
    }
var v=document.getElementsByTagName(tag);  // tag selector like "tag"
if(selclass=="")
    return(v);
for(i=0;i<v.length;i++){
    if(v[i].className==selclass){
        objlist.push(v[i]);
        }
    }
return(objlist);
}
// LightBox::created Chris Campbell, adapted Simon de Haan, Gnabgib

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
var closeButton = 'pics/close.gif';


//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/

function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', getBrowserInfo, false);
Event.observe(window, 'unload', Event.unloadCache, false);

var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(ctrl) {
		this.content = ctrl.rel;
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayLightbox("block");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	displayLightbox: function(display){
		$('overlay').style.display = display;
		$(this.content).style.display = display;
		if(display != 'none') this.actions();		
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		lbActions = document.getElementsByClassName('lbAction');

		for(i = 0; i < lbActions.length; i++) {
			Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
			lbActions[i].onclick = function(){return false;};
		}

	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		
		this.displayLightbox("none");
	}
}

/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightbox active
function initialize(){
	addLightboxMarkup();
	lbox = document.getElementsByClassName('lbOn');
	for(i = 0; i < lbox.length; i++) {
		valid = new lightbox(lbox[i]);
	}
}

// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Lightbox is the centered square that the content is put into.
function addLightboxMarkup() {

	bod 				= document.getElementsByTagName('body')[0];

	overlay 			= document.createElement('div');
	overlay.id			= 'overlay';

	bod.appendChild(overlay);
}

var rsa=new rsa(67);
rsa.encryptionKey("10001",
"303b3948f8e95fdf1d39cf78cc2564401d66736d964d065279bcbf2d722d7e3c7aefe0b498b6cbc5dccb1009abb0f3692c4c1d391694df280ace6094592f2921");
function authSend(frm) {
	if (frm.loginuser.value=="") {
		alert("Please enter a username.");
		return false;
	}
	if (frm.loginpwd.value=="") {
		alert("Please enter a password.");
		return false;
	}
	//New!  Encrypt the submitted value.
	frm.loginpwd.value=rsa.encrypt(frm.loginpwd.value);
	return true;
}

function trim(str) {
	while (str.substring(0,1)==" ") {
		str=str.substring(1, str.length);
	}
	while (str.substring(str.length-1, str.length)==" ") {
		str = str.substring(0,str.length-1);
	}
	return str;
}


function stripe(otable,evenColor) {
	var even=false;
	if (!otable) return;
	var tbody=otable.getElementsByTagName("tbody");
	//Your allowed >1 body, but we only use one
	var trs=tbody[0].getElementsByTagName("tr");
	for (var i=0;i<trs.length;i++) {
		//var cls=trs[i].getAttributeNode("class");
		var cls=trs[i].className;
		if (cls==null) cls=false;
		if (!cls && even) trs[i].style.backgroundColor=evenColor;
		even=!even;
	}
}


