// ***********************************************************************************************************************************
// Set up general variables so we know what browser and version, and the computer type the user is using
// as well as general use Layer functions
// Version 1.2.0

var pageloaded = 0, onloadlist = "";
var InNetscape = 0, InNS4 = 0, InNS6 = 0;
var InIE = 0, InOldIE = 0;
var InOpera = 0, DOMCompatible = 0;
var BrowserVersion;
var BrowserVerMajor = parseInt(navigator.appVersion), BrowserVerMinor = parseFloat(navigator.appVersion);
var useragent = navigator.userAgent.toLowerCase();

InNetscape = ((useragent.indexOf('mozilla') != -1) && (useragent.indexOf('spoofer') == -1) && (useragent.indexOf('compatible') == -1) &&
	(useragent.indexOf('opera') == -1) && (useragent.indexOf('webtv') == -1)) ? 1 : 0;
InOpera = (useragent.indexOf('opera') != -1) ? 1 : 0;
InIE = (useragent.indexOf('msie') != -1) ? 1 : 0;
if (InNetscape) {
	BrowserVersion = BrowserVerMajor;
	InNS4 = (BrowserVerMajor==4);
	InNS6 = (BrowserVerMajor>=5);
	if (InNS6) {
		if (useragent.indexOf('netscape/7') != -1)
			BrowserVersion = 7
		else
			BrowserVersion = 6;
		DOMCompatible = 1;
	}
} else if (InIE) {
	if (BrowserVerMajor < 4) {
		BrowserVersion = BrowserVerMajor;
		InIE = 0;
		InOldIE = 1;
	} else if (BrowserVerMajor==4) {
		if (useragent.indexOf('msie 6.0') != -1)
			BrowserVersion = 6;
		else if (useragent.indexOf('msie 5.0') != -1)
			BrowserVersion = 5;
		else
			BrowserVersion = 4;
	}
	if (BrowserVersion>4)
		DOMCompatible = 1;
} else {
	BrowserVersion = BrowserVerMajor;
	if (InOpera)
		DOMCompatible = 1;
}

var InMacOS, InUnix, InWindows;
var useros = navigator.platform.toLowerCase();
InMacOS = (useros.indexOf("mac") != -1) ? 1 : 0;
InWindows = (useros.indexOf("win") != -1) ? 1 : 0;
InUnix = ((useros.indexOf("unix") != -1) || (useros.indexOf("solaris") != -1) || (useros.indexOf("linux") != -1) || (useros.indexOf("sun") != -1) ||
	(useros.indexOf("hp") != -1) || (useros.indexOf("bsd") != -1)) ? 1 : 0;

// DisplayBrowserAndOSData();

function DisplayBrowserAndOSData() {
	alert('useragent = ' + useragent + '\n' +
		'InNetscape = ' + InNetscape + '\n' +
		'InNS4 = ' + InNS4 + '\n' +
		'InNS6 = ' + InNS6 + '\n' +
		'InIE = ' + InIE + '\n' +
		'InOldIE = ' + InOldIE + '\n' +
		'InOpera = ' + InOpera + '\n' +
		'DOMCompatible = ' + DOMCompatible + '\n' +
		'BrowserVersion = ' + BrowserVersion + '\n' +
		'BrowserVerMajor = ' + BrowserVerMajor + '\n' +
		'BrowserVerMinor = ' + BrowserVerMinor + '\n' +
		'\nuseros = ' + useros + '\n' +
		'InMacOS = ' + InMacOS + '\n' +
		'InWindows = ' + InWindows + '\n' +
		'InUnix = ' + InUnix + '\n');
}

// ***********************************************************************************************************************************
// Functions

var RandSeed=0;
// Creates the random seed from the system time
function Randomize() {
  var now = new Date();
  RandSeed = now.getTime() % 0xffffffff;
}

// Gets a random number from 0 to n-1
function Random(n) {
  RandSeed = (0x015a4e35 * RandSeed + 1) % 0x7fffffff;
  return (RandSeed >> 16) % n;
}

function OnLoadMainFunction() {
	if (onloadlist!="")
		eval(onloadlist);
	pageloaded = 1;
}

// Simplifies a popup confirmation
function AreYouSure(popuptext, someurl) {
	if (confirm(popuptext))
		window.location = someurl;
}

// For a standard popup with a scrollbar and no menu
function DisplayMiniWindow(windowpage, popupwidth, popupheight) {
	window.open(windowpage, '', 'width=' + popupwidth + ',height=' + popupheight +
		',location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes');
}

// For a popup without a scrollbar
function DisplayMiniWindow2(windowpage, popupwidth, popupheight) {
	window.open(windowpage, '', 'width=' + popupwidth + ',height=' + popupheight +
		',location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no');
}

// For a popup with a scrollbar, going to a specific window
function DisplayMiniWindow3(windowpage, targetwindow, popupwidth, popupheight) {
	window.open(windowpage, targetwindow, 'width=' + popupwidth + ',height=' + popupheight +
		',location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes');
}

// For a popup without a scrollbar, going to a specific window
function DisplayMiniWindow4(windowpage, targetwindow, popupwidth, popupheight) {
	window.open(windowpage, targetwindow, 'width=' + popupwidth + ',height=' + popupheight +
		',location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no');
}

// Displays a message in a popup, and goes to a new location
function DisplayMessageAndGo(displaymessage, newlocation) {
	DisplayMiniWindow2('/popup-message.asp?message=' + escape(displaymessage), 400, 250);
	window.location = newlocation;
}

function PreloadImagesPart2(imagetoload) {
	var tempimage = new Image;
	tempimage.src = imagetoload;
}

// Preload images for mouseover and other events
function PreloadImages(imagelist) {
	var imagearray = imagelist.split(",");
	for (var q = 0; q < imagearray.length; q++) {
		PreloadImagesPart2(imagearray[q]);
	}
}

// ---------------------------------------------------------------------------
// The following are currently Netscape 4.x, Netscape 6.x, and IE4+ compatible


// ---------------------------------------------------------------------------
// Deprecated Functions
function MeMSOStyleChange(objectname, styletochange, newvalue) {
	MeMSOObjectStyleChange(objectname, styletochange, newvalue);
}

function MeMSOLayerMove(objectname, newx, newy) {
	MeMSOObjectMove(objectname, newx, newy);
}

function MeMSOLayerUpdate(objectname, newcontent) {
	MeMSOObjectUpdate(objectname, newcontent);
}

function MeMSOLayerSetVisible(objectname, turniton) {
	MeMSOObjectSetVisible(objectname, turniton)
}
// End Deprecated Functions
// ---------------------------------------------------------------------------

// Add a standard event handler
function MeMSOAddEvent(eventname, eventfunction) {
	if (!InIE && !DOMCompatible && !InNS6 && !InNS4)
		return 0;
	if (InNS6)
		document.addEventListener(eventname, eventfunction, false);
	switch (eventname) {
	case 'mousemove':
		if (InNS4)
			document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = eventfunction;
		break;
	case 'mousedown':
		if (InNS4)
			document.captureEvents(Event.MOUSEDOWN);
		document.onmousedown = eventfunction;
	case 'mouseup':
		if (InNS4)
			document.captureEvents(Event.MOUSEUP);
		document.onmouseup = eventfunction;
	}
	return 1;
}

function MeMSOObjectStyleChange(objectname, styletochange, newvalue) {
	var stylename = '', q, flag, curval;
	if (DOMCompatible || InNS4) {
		if (styletochange=='background-image')
			newvalue = "url('" + newvalue + "')";
		for (q=0; q<styletochange.length; q++) {
			curval = styletochange.substring(q, q+1);
			if (curval=='-') {
				flag = 1;
			} else {
				if (flag) {
					stylename += curval.toUpperCase();
					flag = 0;
				} else {
					stylename += curval;
				}
			}
		}
		if (!InNS4) {
			eval('document.getElementById(objectname).style.' + stylename + ' = newvalue;');
		} else {
			eval('document.ids.' + objectname + '.' + stylename + ' = newvalue;');
		}
	} else if (InIE) {
		switch (styletochange) {
		case 'background-color':
			stylename = 'bgColor';
			break;
		case 'background-image':
			stylename = 'bgImage';
			break;
		default:
			stylename = styletochange;
		}
		eval('document.all.' + objectname + '.' + stylename + ' = newvalue;');
	}
}

function MeMSOObjectMove(objectname, newx, newy) {
	var ourpointer;
	if (InIE || DOMCompatible) {
		ourpointer = (DOMCompatible) ? document.getElementById(objectname) : eval('document.all.' + objectname);
		ourpointer.style.left = newx;
		ourpointer.style.top = newy;
	} else if (InNS4) {
		ourpointer = eval('document.' + objectname);
		ourpointer.left = newx;
		ourpointer.top = newy;
	}
}

function MeMSOObjectUpdate(objectname, newcontent) {
	var ourpointer;
	if (InNS6 || InIE) {
		ourpointer = (DOMCompatible) ? document.getElementById(objectname) : eval('document.all.' + objectname);
		ourpointer.innerHTML = newcontent;
	} else if (DOMCompatible) {
		ourpointer = document.getElementById(objectname);
		ourpointer.firstChild.nodeValue = newcontent;
	} else if (InNS4) {
		ourpointer = eval('document.' + objectname);
		ourpointer.document.open();
		ourpointer.document.write(newcontent);
		ourpointer.document.close();
	}
}

function MeMSOObjectSetVisible(objectname, turniton) {
	var ourpointer;
  if (InNS6 || InIE || DOMCompatible) {
		ourpointer = (DOMCompatible) ? document.getElementById(objectname) : eval('document.all.' + objectname);
    ourpointer.style.visibility = (turniton) ? 'visible' : 'hidden';
  } else if (InNS4) {
		ourpointer = eval("document." + objectname);
    ourpointer.visibility = (turniton) ? 'show' : 'hide';
  }
}

// Returns: An object having .x and .y properties which are the coordinates of the object
function MeMSOObjectGetPosition(objectname) {
	// This function will return an Object with x and y properties
	var coordinates = new Object();
	var x=0, y=0;

	// Logic to find position
	if (InIE) {
		x = MeMSO_IEGetPageOffsetLeft(document.all[objectname]);
		y = MeMSO_IEGetPageOffsetTop(document.all[objectname]);
	} else if (DOMCompatible) {
		x = document.getElementById(objectname).offsetLeft;
		y = document.getElementById(objectname).offsetTop;
	} else if (InNetscape) {
		x = document.layers[objectname].top;
		y = document.layers[objectname].left;
		/*
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==objectname) {
				found=1;
				break;
			}
		}
		if (!found) {
			x = 0;
			y = 0;
		} else {
			x = document.anchors[i].x;
			y = document.anchors[i].y;
		}
		*/
	}
	coordinates.x = x;
	coordinates.y = y;
	return coordinates;
}

// Returns: An object having .x and .y properties which are the coordinates of the object, relative to the window
function MeMSOObjectGetScreenPosition(objectname) {
	var coordinates = MeMSOObjectGetPosition(anchorname);
	var x = 0;
	var y = 0;
	if (InIE) {
		x = coordinates.x - document.body.scrollLeft + window.screenLeft;
		y = coordinates.y - document.body.scrollTop + window.screenTop;
	} else if (DOMCompatible) {
		if (isNaN(window.screenX)) {
			x = coordinates.x - document.body.scrollLeft + window.screenLeft;
			y = coordinates.y - document.body.scrollTop + window.screenTop;
		} else {
			x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset;
			y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset;
		}
	}	else if (InNetscape) {
		x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset;
		y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset;
	}
	coordinates.x = x;
	coordinates.y = y;
	return coordinates;
}

// Returns: An object having .width and .height properties which are the size of the object
function MeMSOObjectGetSize(objectname) {
	var coordinates = new Object();
	var curwidth = 0, curheight = 0;
	if (InIE) {
		curwidth = eval(objectname + ".offsetWidth");
		curheight = eval(objectname + ".offsetHeight");
	} else if (DOMCompatible) {
		curwidth = parseInt(document.defaultView.getComputedStyle(document.getElementById(objectname), "").getPropertyValue("width"));
		curheight = parseInt(document.defaultView.getComputedStyle(document.getElementById(objectname), "").getPropertyValue("height"));
	} else if (InNS4) {
		curwidth = document.layers[objName].clip.width;
		curheight = document.layers[objName].clip.height;
	}
	coordinates.width = curwidth;
	coordinates.height = curheight;
	return coordinates;
}

// ---------------------------------------------------------------------------
// Internal Use Functions

// Functions for old IE to get position of an object
function MeMSO_IEGetPageOffsetLeft(curobject) {
	var curpos = curobject.offsetLeft;
	while ((curobject = curobject.offsetParent)!=null) {
		curpos += curobject.offsetLeft;
	}
	return curpos;
}

function MeMSO_IEGetPageOffsetTop(curobject) {
	var curpos = curobject.offsetTop;
	while ((curobject = curobject.offsetParent)!=null) {
		curpos += curobject.offsetTop;
	}
	return curpos;
}

// ---------------------------------------------------------------------------
// Administration Functions

function AdminAreYouSure(itemname, someurl) {
	if (confirm('Are you sure you want to permanently delete ' + itemname + '?'))
		window.location = someurl;
}

function ASPUpload_ShowProgress(CurrentBarHREF, FormName) {
	strAppVersion = navigator.appVersion;
	if (eval('document.' + FormName + '.FILE1.value') != "") {
		if (InIE && BrowserVerMajor>=4) {
			winstyle = "dialogWidth=375px; dialogHeight: 180px; center:yes";
			window.showModelessDialog(CurrentBarHREF + '&b=IE', null, winstyle);
		} else {
			window.open(CurrentBarHREF + '&b=NN', '', 'width=370,height=165', true);
		}
	}
	return true;
}

// ---------------------------------------------------------------------------
// Advanced Editor Functions

// Deprecated
function AdvancedTextUpdate(formname, elementname, updatetype) {
	AdvancedEditor_TextUpdate(formname, elementname, updatetype);
}

function AdvancedEditor_TextUpdate(formname, elementname, updatetype) {
	var cursel=document.selection.createRange()
	var somestring='', addbefore='', addafter='', addper='';
	var parentelement=cursel.parentElement();
	if (parentelement.name==elementname && parentelement.form.id==formname) {
		if (updatetype<256) {
			switch (updatetype) {
			case 0: // Bold
				addbefore = '<b>';
				addafter = '</b>';
				break;
			case 1: // Italic
				addbefore = '<i>';
				addafter = '</i>';
				break;
			case 2: // Underline
				addbefore = '<u>';
				addafter = '</u>';
				break;
			case 8: // Smaller
				addbefore = '<small>';
				addafter = '</small>';
				break;
			case 9: // Bigger
				addbefore = '<big>';
				addafter = '</big>';
				break;
			case 16:// Make Red
				addbefore = '<span style="color: #FF0000;">';
				addafter = '</span>';
				break;
			case 32:// Link
				if (cursel.text.charAt(cursel.text.length-1)==' ') {
					addbefore = '<a href="' + cursel.text.substring(0, cursel.text.length-1) + '">';
				} else {
					addbefore = '<a href="' + cursel.text + '">';
				}
				addafter = '</a>';
				break;
			case 33:// New Window Link
				if (cursel.text.charAt(cursel.text.length-1)==' ') {
					addbefore = '<a href="' + cursel.text.substring(0, cursel.text.length-1) + '" target="_blank">';
				} else {
					addbefore = '<a href="' + cursel.text + '" target="_blank">';
				}
				addafter = '</a>';
				break;
			case 64:// Indent
				addbefore = '<table border="0" cellspacing="0" cellpadding="0" width="100%"><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td width="100%">';
				addafter = '</td></tr></table>';
				break;
			case 96:// Bullet
				addbefore = '<img src="/images/bullet.gif" width="8" height="10" alt="" /> ';
				addafter = '';
				break;
			}
			if (cursel.text.charAt(cursel.text.length-1)==' ') {
				cursel.text = addbefore + cursel.text.substring(0, cursel.text.length-1) + addafter + ' ';
			} else {
				cursel.text = addbefore + cursel.text + addafter;
			}
		} else {
			switch (updatetype) {
			case 256:// Bullet
				addbefore = '<ul><li>';
				addafter = '</ul>';
				addper = '<li>';
				break;
			case 257:// Numbered List
				addbefore = '<ol type="1"><li>';
				addafter = '</ol>';
				addper = '<li>';
				break;
			}
			somestring = cursel.text;
			somestring = somestring.replace(/\r?\n/g, '\r\n' + addper);
			if (somestring.charAt(somestring.length-1)==' ') {
				cursel.text = addbefore + somestring.substring(0, cursel.text.length-1) + addafter + ' ';
			} else {
				cursel.text = addbefore + somestring + addafter;
			}
		}
	}
}

// Deprecated
function AdvancedPreview(elementname, nlconvert) {
	AdvancedEditor_Preview(elementname, nlconvert);
}

function AdvancedEditor_Preview(elementname, nlconvert) {
	var curtext = eval(elementname + ".value");
	var win = window.open(', ', '', 'width=600,height=400,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes');
	if (nlconvert)
		curtext = curtext.replace(/\r?\n/g, '<br />');
	curtext = '<html><head><link rel="stylesheet" type="text/css" href="/general.css" /></head><body class="PopupPreviewWindow">' +
		'<span class="MainText"><b>HTML Preview</b> (<small><a href="javascript: window.close();">close this window</a></small>)</span><br />' +
		'<table border="0" cellspacing="0" cellpadding="0" width="100%">' +
		'<tr><td bgcolor="#000000"><img src="/images/dot.gif" width="1" height="1" alt="" /></td></tr></table><br />' +
		'<span class="MainText">' + curtext + '<br /><br /></span></body></html>';
	win.document.write(curtext);
}

function AdvancedEditor_SelectUpdate(formname, elementname, dropdownname) {
	var curvalue = eval('document.' + formname + '.' + dropdownname + '.value');
	if (curvalue!='') {
		AdvancedEditor_TextUpdate(formname, elementname, parseInt(curvalue));
	}
	eval('document.' + formname + '.' + dropdownname + '.selectedIndex = 0;');
}

function AdvancedEditor_PopulateDropdown(formname, elementname) {
	var curstring = '', curidregion = formname + '_' + elementname + '_dd_div';
	curstring = '<select name="' + formname + '_' + elementname + '_dropdown" style="font-size: 8pt;" ' +
		'onchange="AdvancedEditor_SelectUpdate(\'' + formname + '\', \'' + elementname + '\', \'' + formname + '_' + elementname + '_dropdown\');">' +
		'<option value="" selected="selected">select to alter highlighted text</option>' +
		'<option value="">----------------------</option>';
	curstring +=
		'<option value="0">Bold</option>' +
		'<option value="1">Italic</option>' +
		'<option value="2">Underline</option>' +
		'<option value="8">Smaller Text</option>' +
		'<option value="9">Bigger Text</option>' +
		'<option value="16">Red Text</option>' +
		'<option value="">----------------------</option>' +
		'<option value="32">Convert to a Link</option>' +
		'<option value="33">Convert to a Popup Link</option>' +
		'<option value="">----------------------</option>' +
		'<option value="96">Bullets</option>' +
		'<option value="64">Indent</option>';
	curstring += '</select>';
	MeMSOObjectUpdate(curidregion, curstring);
}