function trim(st)
{
	if(st.length > 0)
	{
		re = / +$/g; 
		newval = st.replace(re,"");
		re = /^ +/g;
		newvala = newval.replace(re,"");
		return newvala;
	}
	return "";
}

function IsNumeric(value)
{
	return value.match(/^\d*$/);
}
	
function maskInput(e)
{
	var key;
	var keychar;

	if (window.event)
	{
		key = window.event.keyCode;
	}
	else if (e)
	{
		key = e.which;
	}
	else
	{
		return true;
	}

	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) ||
		(key==9) || (key==13) || (key==27) )
	{
		return true;
	}
	// numbers
	else if ((("0123456789.,").indexOf(keychar) > -1))
	{
		return true;
	}
	
	return false;
}

function stripNumber(val)
{
	return val.toString().replace(/\,/g,"");
}

function decorateNumber(val)
{
	val = stripNumber(val);
	
	if (isNaN(val) || val == "Infinity") return "";
	
	var acc = "";
	 
	var i = val.indexOf(".");
	if (i > -1) 
	{
		acc = val.substring(i);
	}
	else i = val.length;
	
	while (i > 3)
	{
		acc = "," + val.substring(i - 3,i) + acc;
		i -= 3;
	}
			
	if (i > 0)
	{
		acc = val.substring(0, i) + acc;
	}
	
	return acc;
}

function ToNum(str)
{
	return Number(stripNumber(str));
}

function IsDate(mMonth, mDay, mYear)
{
	mMonth = parseInt(mMonth);
	mDay = parseInt(mDay);
	mYear = parseInt(mYear);
	var mLeap = false;
	if ( mYear%4 == 0 )
		mLeap = true;
	if ( mMonth == 1 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 2 )
	{
		if ( mLeap )
		{
			if ( mDay < 1 || mDay > 29 )
				return false;
			else
				return true;
		}
		else
		{
			if ( mDay < 1 || mDay > 28 )
				return false;
			else
				return true;
		}
	}
	else if ( mMonth == 3 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 4 )
	{
		if ( mDay < 1 || mDay > 30 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 5 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 6 )
	{
		if ( mDay < 1 || mDay > 30 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 7 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 8 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 9 )
	{
		if ( mDay < 1 || mDay > 30 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 10 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 11 )
	{
		if ( mDay < 1 || mDay > 30 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 12 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else
	{
		return false;
	}
}

function popup(url, name, width, height)
{
	if (arguments.length == 5)
	{
		styles = arguments[4];
	}
	else
	{
		styles = "toolbar=0,scrollbars=1,resizable=1";
	}
	
	var w = window.open(url, name, 'width='+width+',height='+height+','+styles);
	w.focus();
}

function go(url)
{
	document.location.href = url;
}

function copyFrom(selector, url, titleField)
{
	var idx = selector.selectedIndex;
	
	var title = document.getElementById(titleField);
	
	if (idx > 0)
	{
		var srcName = selector.options[idx].text;
		var instance = selector.value;
		
		if (confirm("Are you sure you want to copy the form data from '" + srcName + 
					"'?\r\nAny data you have entered into this form will be overwritten."))
		{	
			var loc = url + "&src_inst=" + instance;
			
			if (title && title.value != undefined)
			{
				loc += "&title=" + escape(title.value);
			}
			
			window.location.href = loc;
		}
	}
}

function toggleNDAControl(toggle, controlID)
{
	var control = document.getElementById(controlID);
	
	if (toggle.checked)
	{
		control.value = "";
		control.disabled = true;
	}
	else
	{
		control.disabled = false;
	}
}

function toggleNDAForm(toggle, formID, formInstance)
{
	if (toggle.checked)
	{
		if (confirm("Are you sure you want to mark this form as having no information available?\r\n" +
					"Any data you have entered into this form will be deleted."))
		{
			var loc = "form_empty.php?empty=Y&form_id=" + formID + "&form_instance=" + formInstance;
			window.location.href = loc;
		}
	}
	else
	{
		var loc = "form_empty.php?empty=N&form_id=" + formID + "&form_instance=" + formInstance;
		window.location.href = loc;
	}
}

function httpRequest(url)
{
	var ua = navigator.userAgent.toLowerCase();
   	if (!window.ActiveXObject)
   	{
    	request = new XMLHttpRequest();
   	}
   	else if (ua.indexOf('msie 5') == -1)
   	{
     	request = new ActiveXObject("Msxml2.XMLHTTP");
   	}
   	else
   	{
   		request = new ActiveXObject("Microsoft.XMLHTTP");
   	}

	// the request variable holds an XMLHttpRequest 
	// object instance
	request.open("GET", url, false);
	request.send("");

	var result = "";

	if ( request.status == 200 )
	{
		result = request.responseText;
	}
	else
	{
		result = "<span style='color:red;font-weight:bold'>Error retrieving data</span>";
	}
	
	return result;	
}

function todayLong()
{
	var monthNames = new Array(
	"January","February","March","April","May","June","July",
	"August","September","October","November","December");

	var now = new Date();

	var today = now.getDate() + " " + monthNames[now.getMonth()] + " " + now.getFullYear();
	
	return today;
}
