function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent('on'+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}
/* edit these variables to customise the multiselect */
{
var show_toplinks=true;
var counter = 0;
}

/* global variables - do not touch */
{
var isIE=window.attachEvent?true:false;
var selectDefaults=[];
}
function buildMultiselects(){
do{
found=0;
a=document.getElementsByTagName('select');
for(b=0;b<a.length,!found;b++){
var ms=a[b];
if(ms==null)break;
var name=ms.name;
if(name.substring(name.length-2, name.length) == '[]')
	name = name.substring(0,ms.id.length-2);
	
if(ms.multiple && String(ms.className).search('NoCBSwitch') == -1){
	/* common variables */ {
	selectDefaults[name]=[];
	var found=1,disabled=ms.disabled?1:0,width=ms.offsetWidth,height=ms.offsetHeight;
	if(width<120)width=120;
	if(height<60)height=60;
	}
	/* set up wrapper */ {
	var wrapper=document.createElement('div');
	wrapper.style.width=width+'px';
	wrapper.style.height=height+'px';
	wrapper.style.position='relative';
	wrapper.style.border='2px solid #000';
	wrapper.style.borderColor='#333 #ccc #ccc #333';
	wrapper.style.font='10px sans-serif';
	wrapper.style.textAlign='left';
	}
	if(show_toplinks){ /* reset, all, none */
	var toplinksHeader=document.createElement('div');
	toplinksHeader.style.background = '#e0e0e0'
	toplinksHeader.style.textAlign='center';
	 
	toplinksHeader.appendChild(newLink("javascript:"+(disabled?"alert('selection disabled')":"multiselect_selectall('"+name+"','checked');" + (ms.getAttribute('onChange') == null ? "" : ms.getAttribute('onChange')) + ";"),'all'));
	toplinksHeader.appendChild(document.createTextNode('  '));
	toplinksHeader.appendChild(newLink("javascript:"+(disabled?"alert('selection disabled')":"multiselect_selectall('"+name+"','');" + (ms.getAttribute('onChange') == null ? "" : ms.getAttribute('onChange')) + ";"),'none'));
	toplinksHeader.appendChild(document.createTextNode('  '));
	toplinksHeader.appendChild(newLink("javascript:"+(disabled?"alert('selection disabled')":"multiselect_selectall('"+name+"','reset');" + (ms.getAttribute('onChange') == null ? "" : ms.getAttribute('onChange')) + ";"),'reset'));
	 
	wrapper.appendChild(toplinksHeader);
	}
	/* setup multiselect */ {
	newmultiselect=document.createElement('div');
	newmultiselect.style.position='absolute';
	newmultiselect.style.top=show_toplinks?'15px':'0';
	newmultiselect.style.left='0';
	newmultiselect.style.overflow='auto';
	newmultiselect.style.width=(isIE?width-4:width)+'px';
	newmultiselect.style.height=show_toplinks?height-(isIE?19:15)+'px':height-(isIE?4:0)+'px';
	}
	c=ms.getElementsByTagName('option');
	for(d=0;d<c.length;d++){
	var label=document.createElement('label');
	label.style.display='block';
	label.style.border='1px solid #eee';
	label.style.borderWidth='1px 0';
	label.style.font='10px arial';
	label.style.lineHeight='10px';
	label.style.paddingLeft='20px';
	label.htmlFor = '__mscb' + counter
	checkbox=document.createElement('input');
	checkbox.type='checkbox';
	if(c[d].selected){
	checkbox.checked='checked';
	checkbox.defaultChecked=true;
	}
	if(c[d].disabled){
	checkbox.disabled='disabled';
	label.style.color='#666';
	}
	selectDefaults[name][d]=c[d].selected?'checked':'';
	if(disabled)checkbox.disabled='disabled';
	checkbox.value=c[d].value;
	checkbox.style.marginLeft='-16px';
	checkbox.style.marginTop='-2px';
	checkbox.name=ms.name;
	checkbox.id='__mscb' + counter++
	checkbox.setAttribute('onclick', ms.getAttribute('onChange')); // moz version
	checkbox.onclick = ms.getAttribute('onChange'); // ie version
	// escape the label
	var text=c[d].innerHTML;
	text=text.replace(/\&nbsp;?/g,' ');
	text=text.replace(/\&lt;?/g,'<');
	text=text.replace(/\&gt;?/g,'>');
	text=text.replace(/\&amp;?/g,'&');
	text=text.replace(/\&quot;?/g,'"');
	labelText=document.createTextNode(text);
	label.appendChild(checkbox);
	label.appendChild(labelText);
	newmultiselect.appendChild(label);
	}
wrapper.appendChild(newmultiselect);
ms.parentNode.insertBefore(wrapper,ms);
ms.parentNode.removeChild(ms);
}
}
}while(found);
/*document.write(wrapper.innerHTML.replace(/\</g, '&lt;'));*/}
function multiselect_selectall(name,val){
var els=document.getElementsByTagName('input'),found=0;
for(var i=0;i<els.length;++i){
if(els[i].name==name+'[]' || els[i].name==name) els[i].checked = (val == 'reset' ? selectDefaults[name][found++] : val);
}
}
function newLink(href,text){
var e=document.createElement('a');e.href=href;e.appendChild(document.createTextNode(text));
	e.setAttribute('onclick', href); // moz version
	e.onclick = 'function() { ' + href + ' }'; // ie version
return e;
}

var newWindow;
function openloading() {
  newWindow = window.open('loading.htm','newWin','toolbar=no,location=no,scrollbar=no,resizable=no,width=300,height=100')
	return;
}
function closeloading() {
	if (newWindow) {
    newWindow.close()
  }
	return;
}
function CreateWindow(page,width,height, name) {
	if (name == '') { name = 'displayWindow' } 
		if (!(!newWindow || newWindow.closed)) 
			newWindow.close();
		newWindow = window.open(page,name,'toolbar=no,width='+width+',height='+height+',screenX=150,screenY=50,top=50,left=150,directories=no,status=yes,scrollbars=yes,resizable=yes,menubar=no');
		if (newWindow) {
			if (window.screen.availWidth < 640 && window.screen.availHeight < 480)
			{
				myWindow.resizeTo(window.screen.availWidth,window.screen.availHeight );
				myWindow.moveTo(0,0);
			}
			newWindow.focus();
		}
		if (!newWindow.opener)
			newWindow.opener = self;
		return;
	}
function RefreshSideFrames() {
	var main = parent.mainFrame.location.href;
	var list_begin = main.indexOf('listID');
	if (list_begin < 0) {
		var listID = 0;
	}
	else {
		main = main.substring(list_begin+7, main.length)
		var list_end = main.indexOf()
		if (list_end < 0) {
			list_end = main.length;
		}
		var listID = main.substring(0, list_end);
	}
	parent.topFrame.location.href = '/admin/topleft.asp';
	var left = parent.leftFrame.location.href;
	var reg = new RegExp("listid=\\w*&?", "gi");
	left = left.replace(reg, "");
	parent.leftFrame.location.href = parent.leftFrame.location.href;
	return;
}
function PreloadImages() {
	var d=document;
	if(d.images){ if(!d.MU_p) d.MU_p=new Array();
	var i,j=d.MU_p.length,a=PreloadImages.arguments; 
	for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MU_p[j]=new Image; d.MU_p[j++].src=a[i];}}
	return;
 }
if(typeof(listID) == 'undefined')
	var listID = 0;
 function ClearListID() { try { listID = null; } catch(e) { } } 
 function CSSPage() {
	a=document.getElementsByTagName('input');
	for(b=0;b<a.length;b++)
	{
		var ms=a[b];
		if(ms==null)
			break;
		if(ms.type.toLowerCase() == 'text' || ms.type.toLowerCase() == 'password' || ms.type.toLowerCase() == 'file')
			ms.className += ' textfield';
		else if(ms.type.toLowerCase() == 'radio')
			ms.className += ' radiobutton';
		else if(ms.type.toLowerCase() == 'checkbox')
			ms.className += ' checkbox';
	}
	a=document.getElementsByTagName('textarea');
	for(b=0;b<a.length;b++)
	{
		var ms=a[b];
		if(ms==null)
			break;
		ms.className = 'textfield';
	}
 }
function ValidateDate(field, bForceFocus)
{
	var dt = new Date(field.value);
	
	if(isNaN(dt.getTime()))
	{
		//alert('Invalid Date Value.');
		ShowAlertBox('', '', '', 'Error', 'Error', 'Invalid date');
		
		if(bForceFocus == true)
			setTimeout("document.getElementById('" + field.id + "').focus();", 0);
			
		return false;
	}
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 
			string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

String.prototype.trim = function()
{
    return this.replace( /^\s+|\s+$/, "" );
}
    
function getElementsByClassName(oElm, strTagName, strClassName)
{
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    
    for(var i=0; i<arrElements.length; i++)
    {
        oElement = arrElements[i];
        
        if(oRegExp.test(oElement.className))
            arrReturnElements.push(oElement);
    }
    
    return (arrReturnElements);
}
    
function addClassName(elem, className)
{
    removeClassName(elem, className);
    elem.className = (elem.className + ' ' + className).trim();
}

function removeClassName(elem, className)
{
    elem.className = elem.className.replace(className, "").trim();
}

function GetX(obj)
{
	var curleft = 0;

	alert(obj.offsetParent + ' - ' + document.body.scrollTop + ' - ' + document.body.scrollX);
	
	if(obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if(obj.x)
		curleft += obj.x;

	return curleft;
}

function GetY(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;
}

setTimeout(function()
    {
        /*
            note: overwritten because of a FF bug... if/when this gets resolved this setTimeout should be removed
            https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=355573
        */
        window.ValidatorConvert = function(op, dataType, val)
        {

            function GetFullYear(year) {
                var twoDigitCutoffYear = val.cutoffyear % 100;
                var cutoffYearCentury = val.cutoffyear - twoDigitCutoffYear;
                return year > twoDigitCutoffYear ? cutoffYearCentury - 100 + year : cutoffYearCentury + year;
            }

            var num, cleanInput, m, exp;
            if (dataType == "Integer") {
                exp = /^\s*[-\+]?\d+\s*$/;
                if (op.match(exp) == null) {
                    return null;
                }
                num = parseInt(op, 10);
                return isNaN(num) ? null : num;
            } else if (dataType == "Double") {
                exp = new RegExp("^\\s*([-\\+])?(\\d*)\\" + val.decimalchar + "?(\\d*)\\s*$");
                m = op.match(exp);
                if (m == null) {
                    return null;
                }
                if ((!m[2] || m[2].length == 0) && (!m[3] || m[3].length == 0)) {
                    return null;
                }
                cleanInput = (m[1] != null ? m[1] : "") + (m[2].length > 0 ? m[2] : "0") + (m[3].length > 0 ? "." + m[3] : "");
                num = parseFloat(cleanInput);
                return isNaN(num) ? null : num;
            } else if (dataType == "Currency") {
                var hasDigits = val.digits > 0;
                var beginGroupSize, subsequentGroupSize;
                var groupSizeNum = parseInt(val.groupsize, 10);
                if (!isNaN(groupSizeNum) && groupSizeNum > 0) {
                    beginGroupSize = "{1," + groupSizeNum + "}";
                    subsequentGroupSize = "{" + groupSizeNum + "}";
                } else {
                    beginGroupSize = subsequentGroupSize = "+";
                }
                exp = new RegExp("^\\s*([-\\+])?((\\d" + beginGroupSize + "(\\" + val.groupchar + "\\d" + subsequentGroupSize + ")+)|\\d*)" + (hasDigits ? "\\" + val.decimalchar + "?(\\d{0," + val.digits + "})" : "") + "\\s*$");
                m = op.match(exp);
                if (m == null) {
                    return null;
                }
                if (m[2].length == 0 && hasDigits && m[5].length == 0) {
                    return null;
                }
                cleanInput = (m[1] != null ? m[1] : "") + m[2].replace(new RegExp("(\\" + val.groupchar + ")", "g"), "") + (hasDigits && m[5].length > 0 ? "." + m[5] : "");
                num = parseFloat(cleanInput);
                return isNaN(num) ? null : num;
            } else if (dataType == "Date") {
                var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
                m = op.match(yearFirstExp);
                var day, month, year;
                if (m != null && ((m[2] && m[2].length == 4) || val.dateorder == "ymd")) {
                    day = m[6];
                    month = m[5];
                    year = m[2].length == 4 ? m[2] : GetFullYear(parseInt(m[3], 10));
                } else {
                    if (val.dateorder == "ymd") {
                        return null;
                    }
                    var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
                    m = op.match(yearLastExp);
                    if (m == null) {
                        return null;
                    }
                    if (val.dateorder == "mdy") {
                        day = m[3];
                        month = m[1];
                    } else {
                        day = m[1];
                        month = m[3];
                    }
                    year = (m[5] && m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));
                }
                month -= 1;
                var date = new Date(year, month, day);
                if (year < 100) {
                    date.setFullYear(year);
                }
                return typeof date == "object" &&
                    year == date.getFullYear() &&
                    month == date.getMonth() && day == date.getDate() ? date.valueOf() : null;
            } else {
                return op.toString();
            }
        }
    }, 500);