///////////////////////////////////////////////////////////////////////////
//	
//	Filename:		bastools.js
//	
//	Author:			Forrest Tate (ftate@sensiblepc.com)
//	
//	Last Modified:	January 12, 2001
//	
///////////////////////////////////////////////////////////////////////////

/* ****************************************************************** *
 * ******************* Start Browser Functions ********************** *
 * ****************************************************************** */
 function BrowserCheck() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.v = parseInt(navigator.appVersion)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (navigator.userAgent.indexOf('MSIE 4')>0)
	this.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0)
	if (this.ie5) this.v = 5
	this.min = (this.ns||this.ie)
}

// automatically create the "is" object
is = new BrowserCheck()
/* ****************************************************************** *
 * ******************** End Browser Functions *********************** *
 * ****************************************************************** */
 
 /* ****************************************************************** *
 * **************** Start Standard String Functions ***************** *
 * ****************************************************************** */
function LCase(str) {
	var stemp = str;
	if (typeof str == "string")
		return stemp.toLowerCase();
	return str
}
function UCase(str) {
	var stemp = str;
	if (typeof str == "string")
		return stemp.toUpperCase();
	return str
}
function PCase(str) {
	var stemp = "";
	if (typeof str == "string") {
		str = str.toLowerCase();
		for (c = 0; c < str.length; c++) {
			if (str.charAt(c) == " ") {
				if (str.length > c) {
					stemp += " " + UCase(str.charAt(1 + c));
				}
			} 
			else if (!c) stemp += UCase(str.charAt(c));
			else if (str.charAt(c - 1) == " ") stemp += "";
			else stemp += str.charAt(c);
		}
		return stemp;
	}
	return str
}
function LTrim(str) {
	if (typeof str == "string") {
		for (var c = 0; c < str.length; c++)
			if (str.charAt(c) != " ") break;
		
		stemp = str.substring(c,str.length);
		return stemp;
	}
	return str;
}
function RTrim(str) {
	if (typeof str == "string") {
		var c = 0;
		for (var x = str.length - 1; x >= 0 ; x--) {
			if (str.charAt(x) == " ") c++;
			else break;
		}
		stemp = str.substring(0,str.length - c);
		return stemp;
	}
	return str;
}
function Trim(str) {
	if (typeof str == "string") {
		stemp = LTrim(str);
		stemp = RTrim(stemp);
		return stemp;
	}
	return str;
}
function isEmpty(testStr, check) {
	switch (LCase(check)) {
		case "null" :
			if (testStr != null || testStr != "") return false;
		break;
		case "empty" :
			if (testStr != "") return false;
		break;
		case "space" :
			if (!testStr) return true;
			for (var i = 0; i < testStr.length; i++) {
				var thisChar = testStr.charAt(i);
				if (thisChar != " ") return false;
			}
		break;
		default:
			if (testStr != "") return false;
	}
	return true;
}
/* ****************************************************************** *
 * ***************** End Standard String Functions ****************** *
 * ****************************************************************** */
 
//--------------------------------------------------------------------\\
 
/* ****************************************************************** *
 * ***************** Start Dialog Object Functions ****************** *
 * ****************************************************************** */
//Open the Window
function _dlgOpenWindow() {
	this.hwnd = window.open(this.url,this.name,this.features);
	if (this.url.length) this.hwnd.location.href = this.url;
	if (this.html.length) {
		this.hwnd.document.write(this.html);
		this.hwnd.document.close();
	}
	if (this.BGColor) this.hwnd.document.bgColor = this.BGColor;
	this.hwnd.focus();
			
	//Set opener for netscape
    if (this.hwnd.opener == null) {
		this.hwnd.opener = window; 
		this.hwnd.opener.name = "opener";
	} 
	
	//SOS var: disable session timeout warning.
	top._session_timeout_warn = false;
}
function _dlgCloseWindow() {
	if (this.hwnd != null) {
		this.hwnd.close();
	} 
} 
//Focus the Window
function _dlgFocusWindow() {
	if (this.hwnd != null) {
		this.hwnd.focus();
		return true;
	}
	return false;
}
//Reload the Window
function _dlgReloadWindow() {
	if (this.hwnd != null) {
		this.hwnd.location.reload();
	}
}
function Dialog(name, url, html, settings, height, width, vCenter, hCenter, BGColor) {
	//Set vars
	this.url = url;
	this.html = html;
	this.name = name;
	this.height = height;
	this.width = width;
	this.vCenter = vCenter;
	this.hCenter = hCenter;
	this.top = 0;
	this.left = 0;
	this.Open = _dlgOpenWindow;
	this.Close = _dlgCloseWindow;
	this.Focus = _dlgFocusWindow;
	this.OpenUrl = _dlgOpenUrlWindow;
	this.BGColor = BGColor;
	
	//Center it on the screen
	if (this.hCenter) this.left = (((screen.width - 15) - this.width) / 2);
	if (this.vCenter) this.top = (((screen.height - 60) - this.height) / 2);
	
	//Set features		
	if (!settings.length) {
		this.features = "toolbar=false";
		this.features += ",status=false";
		this.features += ",resizable=false";
		this.features += ",scrollbars=false";
		this.features += ",left=" + this.left;
		this.features += ",top=" + this.top;
		this.features += ",height=" + this.height;
		this.features += ",width=" + this.width;
	}
	else {
		this.features = settings
	}
}
/* ****************************************************************** *
 * ****************** End Dialog Object Functions ******************* *
 * ****************************************************************** */

//--------------------------------------------------------------------\\
 
/* ****************************************************************** *
 * ********** Start Functions for working with form fields ********** *
 * ****************************************************************** */
sortitems = 1;  // Automatically sort items within lists? (1 or 0)
function SortE(box, exception)  {
	var temp_opts = new Array();
	var temp = new Object();
	for(var i=0; i<box.options.length; i++)
		temp_opts[i] = box.options[i];
		
	for(var x=0; x<temp_opts.length-1; x++)  {
		for(var y=(x+1); y<temp_opts.length; y++)  {
			if((LCase(temp_opts[x].text) > LCase(temp_opts[y].text)) 
			  && (LCase(temp_opts[x].text) != LCase(exception)))  {
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
				temp = temp_opts[x].value;
				temp_opts[x].value = temp_opts[y].value;
				temp_opts[y].value = temp;
				
	      	}
	   	}
	}
	for(var i=0; i<box.options.length; i++)  {
		box.options[i].value = temp_opts[i].value;
		box.options[i].text = temp_opts[i].text;
   }
}
function SortD(box)  {
	var temp_opts = new Array();
	var temp = new Object();
	for(var i=0; i<box.options.length; i++)
		temp_opts[i] = box.options[i];
		
	for(var x=0; x<temp_opts.length-1; x++)  {
		for(var y=(x+1); y<temp_opts.length; y++)  {
			if(temp_opts[x].text > temp_opts[y].text)  {
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
				temp = temp_opts[x].value;
				temp_opts[x].value = temp_opts[y].value;
				temp_opts[y].value = temp;
      		}
   		}
	}
	for(var i=0; i<box.options.length; i++)  {
		box.options[i].value = temp_opts[i].value;
		box.options[i].text = temp_opts[i].text;
   	}
}

function SortA(box)  {
	var temp_opts = new Array();
	var temp = new Object();
	for(var i=0; i<box.options.length; i++)
		temp_opts[i] = box.options[i];
		
	for(var x=0; x<temp_opts.length-1; x++)  {
		for(var y=(x+1); y<temp_opts.length; y++)  {
			if(temp_opts[x].text < temp_opts[y].text)  {
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
				temp = temp_opts[x].value;
				temp_opts[x].value = temp_opts[y].value;
				temp_opts[y].value = temp;
      		}
   		}
	}
	for(var i=0; i<box.options.length; i++)  {
		box.options[i].value = temp_opts[i].value;
		box.options[i].text = temp_opts[i].text;
   	}
}
function listSwap(fbox,tbox) {
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].selected && fbox.options[i].value != "") {
			var no = new Option();
			no.value = fbox.options[i].value;
			no.text = fbox.options[i].text;
			tbox.options[tbox.options.length] = no;
			fbox.options[i].value = "";
			fbox.options[i].text = "";
	   }
	}
	BumpUp(fbox);
	if (sortitems) SortD(tbox);
}
function BumpUp(box)  {
	for(var i=0; i<box.options.length; i++) {
		if(box.options[i].value == "")  {
			for(var j=i; j<box.options.length-1; j++)  {
				box.options[j].value = box.options[j+1].value;
				box.options[j].text = box.options[j+1].text;
			}
			var ln = i;
			break;
		   	}
		}
		if(ln < box.options.length)  {
			box.options.length -= 1;
			BumpUp(box);
	}
}
/* ****************************************************************** *
 * *********** End Functions for working with form fields *********** *
 * ****************************************************************** */

//--------------------------------------------------------------------\\
 
/* ****************************************************************** *
 * ******************** Start Parsing Functions ********************* *
 * ****************************************************************** */

//Use to escape the '\' unescape char in javascript stings from form fields
function escSlash(str) {
	var stemp = "";
	for (var c = 0; c < str.length; c++)
	  	if (str.charAt(c) == "\\") stemp += "\\\\"
		else stemp += str.charAt(c);
	return stemp;
}
/* *********** chrParse(str, allow) ***********
 *	
 *		str: 	The string/char you want to parse.
 *	  allow:	A string of 'legal' characters (in lower case)
 *				Or one of the preset filters below
 *	Example:
 *		chrParse('xyz 10 f 0% abc', chr_percent)
 *		Returns '100%'
 */
//Preset filters
var chr_int 	= "0123456789";
var chr_alpha 	= "abcdefghijklmnopqrstuvwxyz";
var chr_double 	= chr_int + ".";
var chr_hex 	= chr_int + "abcdef";
var chr_percent = chr_int + "%";
var chr_var		= chr_int + chr_alpha + "_";
var chr_struct	= chr_int + chr_alpha + "_.";
var chr_eng 	= chr_int + chr_alpha + ".:;,?-_!@$%&*-+ ";
var chr_file 	= chr_int + chr_alpha + "/(){}[].\\_-!%: ";
var chr_html	= chr_int + chr_alpha + "_-,.@!#$%^&*(){}+=/; ";

function chrParse(str, allow) {
	var stemp = ""; str.toString();
	for (var c = 0; c < str.length; c++)
	  	if (allow.indexOf(LCase(str.charAt(c))) != -1) 
			stemp += str.charAt(c);
	return stemp;
}

/* ************** htmlTag Object **************
 *
 *		Properties
 *			tag: 	The html tag you will be parsing.
 *						
 *		   Methods	 Syntax:			 Returns:
 *			get:	.get(prop)			the value of that property
 *			set:	.set(prop, val)		the modified tag including the new value
 *	   validate:	.validate(tag)		the validated tag
 *
 * 	  Example: 
 *		var valid_var_name = new htmlTag()
 *		valid_var_name.tag = "<a href='www.javascript.com' target='about:blank'>"
 * 		valid_var_name.get("href") 					returns "www.javascript.com"
 *		valid_var_name.set("newprop", "somevalue")	adds the new property and returns the complete tag
 *		valid_var_name.get("newprop")				returns "somevalue"
 */
function _htmlTag_validate(tag) {
	if (typeof tag == "string") {
		var tmp = "";
		for (var t = 0; t < tag.length; t++) {
			if (tag.charAt(t) == "\"") tmp += "'";
			else tmp += tag.charAt(t);
		} return tmp;
	} return false;
}
function _htmlTag_get(prop) {
	prop = Trim(chrParse(prop, chr_html));
	this.tag = Trim(this.validate(this.tag));
	if (this.tag.length == 0) return false;
	var reg = new RegExp("" + prop + "[\s]*=[\s]*'([^']*)'", "i")
	if (reg.test(this.tag)) {
		var find = reg.exec(this.tag);
		return find[1];
	} return false;
}
function _htmlTag_set(prop,val) {
	prop = chrParse(prop, chr_html);
	val = Trim(chrParse(val, chr_html));
	this.tag = this.validate(this.tag);
	var reg = new RegExp("(<?[^>]*\s*" + prop + ")[\s]*=[\s]*'[^']*'([^'>]*>?)", "i");
	if (reg.test(this.tag)) {
		var find = reg.exec(this.tag);
		this.tag = Trim(find[1]) + "='" + val + "'" + Trim(find[2]);
		return this.tag;
	} else {
		reg.compile("(<?[^>]*)(>?)", "i");
		var find = reg.exec(this.tag);
		this.tag = Trim(find[1]) + " " + prop + "='" + val + "'" + Trim(find[2]);
		return this.tag;
	} return false;
}
function htmlTag() {
	this.tag = "";
	this.get = _htmlTag_get;
	this.set = _htmlTag_set;
	this.validate = _htmlTag_validate;
}
/* ****************************************************************** *
 * ********************* End Parsing Functions ********************** *
 * ****************************************************************** */

//--------------------------------------------------------------------\\
 
/* ****************************************************************** *
 * ********************* Start Math Functions *********************** *
 * ****************************************************************** */
/* **************** toHex(dec) ****************
 *	
 *		dec: 	The decimal to be converted to hexadecimal.
 *
 *	Example:
 *		toHex(255)
 *		Returns 'FF'
 *
 *     Note: You can pass the decimal in or out of quotes.
 *		toHex(255) is the same as this toHex("255")
 */
function toHex(dec) {
	dec = dec.toString();
	dec = chrParse(dec, chr_int);
	if (dec > 255) dec = 255;
	var hexChrs = "0123456789ABCDEF";
	var i = dec % 16;
	var j = (dec - i) / 16;
	rtn = hexChrs.charAt(j);
	rtn += hexChrs.charAt(i);
	if (rtn.length < 2) rtn = (rtn.length) ? rtn += "0" : rtn += "00";
	return rtn;
}
function max(int1, int2) {
	if (typeof int1 == "string") int1 = chrParse(int1, chr_int);
	if (typeof int1 == "string") int2 = chrParse(int2, chr_int);
	if (int1 > int2) return int1; else return int2;
}
function min(int1, int2) {
	if (typeof int1 == "string") int1 = chrParse(int1, chr_int);
	if (typeof int1 == "string") int2 = chrParse(int2, chr_int);
	if (int1 < int2) return int1; else return int2;
}
/* ****************************************************************** *
 * ********************** End Math Functions ************************ *
 * ****************************************************************** */

//--------------------------------------------------------------------\\

/* ****************************************************************** *
 * ******************* Start SOS Roll Functions ********************* *
 * ****************************************************************** */
function SOS_preloadImages() {
    var d=document; if(d.images){ if(!d.SOS_p) d.SOS_p=new Array();
    var i,j=d.SOS_p.length,a=SOS_preloadImages.arguments; 
    for(i=0; i<a.length; i++)
        if (a[i].indexOf("##")!=0) { d.SOS_p[j]=new Image; d.SOS_p[j++].src=a[i];}}
}
function SOS_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=SOS_findObj(n,d.layers[i].document); return x;
}
function SOS_roll(event, grpName) {
  var i,img,nbArr,args=SOS_roll.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = SOS_findObj(args[2])) != null && !img.SOS_init) {
      img.SOS_init = true; img.SOS_up = args[3]; img.SOS_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = SOS_findObj(args[i])) != null) {
        if (!img.SOS_up) img.SOS_up = img.src;
        img.src = img.SOS_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.SOS_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = SOS_findObj(args[i])) != null) {
      if (!img.SOS_up) img.SOS_up = img.src;
      img.src = (img.SOS_dn && args[i+2]) ? args[i+2] : args[i+1];
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.SOS_nbOver.length; i++) {
      img = document.SOS_nbOver[i]; img.src = (img.SOS_dn) ? img.SOS_dn : img.SOS_up; }
  } else if (event == "down") {
    if ((nbArr = document[grpName]) != null)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.SOS_up; img.SOS_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = SOS_findObj(args[i])) != null) {
      if (!img.SOS_up) img.SOS_up = img.src;
      img.src = img.SOS_dn = args[i+1];
      nbArr[nbArr.length] = img;
  } }
}
/* ****************************************************************** *
 * ******************** End SOS Roll Functions ********************** *
 * ****************************************************************** */