//Copyright 2000-2003 Technology Assessment, Inc. All rights reserved. 
//BYB is granted a perpetual license to use and modify this program. 
//This program in whole or part and any derivative works thereof may not
//be sold or licensed without prior approval of Technology Assessment, Inc.

function isNumeric(sValue,iDecimalPlaces)
{
	var string=sValue;
	var i,ch;
	var iDec=iDecimalPlaces;
	var bAllowDecimals,bFoundDecimal=false,bAllowNegs=false;
	
	if (iDecimalPlaces > 0)
		bAllowDecimals = true;
	else
		bAllowDecimals = false;
	if (string.length == 0)
		return false;
	if (iDec < 0)
		{
		bAllowNegs = true;
		iDec *= -1;
		}
	for (i=0;i<string.length;i++)
		{
		ch = string.charAt(i);
		if (bAllowNegs && (ch == '-'))
			continue;
		else if (ch < '0' || ch > '9')
			{
			if (!bAllowDecimals || bFoundDecimal || ch != '.')
			   return false;
			if (ch == '.')
				bFoundDecimal = true;
			}
		else
			{
			if (bFoundDecimal)
				iDec--;
			if (iDec < 0)
				return false;
			}
		}
	return true;
}

//The each character of the mask can be:
//  For numbers:
//      '-' : Negative sign
//      '.' : Decimal point
//      ':' : Colon
//      '9' : Number
//  For alphas and alpanumerics:
//      '&' : Alphanumeric
//      '!' : Alphanumeric, force to uppercase
//      'a' : Alpha
//      'A' : Alpha, force to uppercase
//      'T' : Time, 0-9 and ':'
function Mask(evt,mask)
{
	var iKey,i,chMask,fld;
	var bAllowNeg=false,bNumber=true;
	var iMaskInteger,iMaskDecimals=0,iMaskColons=0;
	var iInteger,iDecimals=0,iColons=0,bFoundNeg=false,bFullField=false;

	if (document.all)
		{
		iKey = evt.keyCode;
		fld = evt.srcElement.value;
		}
	else
		{
		iKey = evt.which;
		fld = evt.target.value;
		}
	if (mask.length == 0)	//no entry allowed in field
		return false;
	if ((iKey == 8) || (iKey == 13))	//backspace or linefeed
		return true;
	if (iKey > 127)	
		return false;
	for (i=0;i<mask.length;i++)
		{
		chMask = mask.charAt(i);
		if ((chMask < '0' || chMask > '9') && (chMask != '-') && (chMask != '.') && (chMask != ':'))
			{
			bNumber = false;
			break;
			}
		}
	if (bNumber)
		{
		if (document.selection.createRange().text.length > 1)
			bFullField = true;		
		if (mask.charAt(0) == '-')
			bAllowNeg = true;
		i = mask.indexOf(".");
		if (i >= 0)
			{
			iMaskInteger = i;
			iMaskDecimals = mask.length - i - 1;
			}
		else
			{
			i = mask.indexOf(":");
			if (i >= 0)
				{
				iMaskInteger = i;
				iMaskColons = mask.length - i - 1;
				}
			else
				iMaskInteger = mask.length;
			}
		if (bAllowNeg)
			iMaskInteger--;
		if (iKey < 48 || iKey > 57)
			if ((iKey != 45) && (iKey != 46) && (iKey != 58))
				return false;
			else if (iKey == 46)		//'.'
			    if ((fld.indexOf(".") >= 0) || (iMaskDecimals <= 0))
					if ((fld.indexOf(".") >= 0) && bFullField)
						fld = "";
					else
						return false;
				else
					return true;
			else if (iKey == 58)		//':'
			    if ((fld.indexOf(":") >= 0) || (iMaskColons <= 0))
					if ((fld.indexOf(":") >= 0) && bFullField)
						fld = "";
					else
						return false;
				else
					return true;
			else
				if (bAllowNeg && ((fld.length == 0) || bFullField))
					{
					fld = "";
					return true;
					}
				else
					return false;
		else
			{
			if (fld.charAt(0) == '-')
				bFoundNeg = true;
			i = fld.indexOf(".");
			if (i >= 0)
				{
				iInteger = i;
				iDecimals = fld.length - i - 1;
				}
			else
				{
				i = fld.indexOf(":");
				if (i >= 0)
					{
					iInteger = i;
					iColons = fld.length - i - 1;
					}
				else
					iInteger = fld.length;
				}
			if (bFoundNeg)
				iInteger--;
			i = fld.indexOf(".");
			if (i >= 0)
				{
				if (iDecimals + 1 > iMaskDecimals)
					if (bFullField)
						fld = "";
					else
						return false;
				}
			else
				{
				i = fld.indexOf(":");
				if (i >= 0)
					{
					if (iColons + 1 > iMaskColons)
						if (bFullField)
							fld = "";
						else
							return false;
					}
				else
					{
					if (iInteger + 1 > iMaskInteger)
						if (bFullField)
							fld = "";
						else
							return false;
					}
				}
			}			
		}
	else
		{
		if (document.selection.createRange().text.length == mask.length)
			fld = "";
		if (fld.length + 1 > mask.length)
			return false;
		chMask = mask.charAt(fld.length);
		if (chMask == 'a')
			if (iKey >= 65 && iKey <= 122)
				return true;
			else
				return false;
		if (chMask == 'A')
			if (iKey >= 65 && iKey <= 122)
				{
				if (iKey >= 91 && iKey <= 122)
					if (document.all)
						evt.keyCode -= 32;
					else
						evt.which -= 32;
				return true;
				}
			else
				return false;
		if (chMask == 'T')
			if ((iKey >= 48 && iKey <= 57) || (iKey == 58))
				return true;
			else
				return false;
		if ((chMask == '!') || (chMask == '&'))
			if ((iKey >= 65 && iKey <= 90) || (iKey >= 97 && iKey <= 122) || 
			    (iKey >= 48 && iKey <= 57))
				{
				if ((chMask == '!') && (iKey >= 97) && (iKey <= 122))
					if (document.all)
						evt.keyCode -= 32;
					else
						evt.which -= 32;
				return true;
				}
			else
				return false;
		}
	return true;
}

function OpenCalendar(sCName,bSundayOnly)
{
	var strDate,strSunday,ret;
	
	if (bSundayOnly)
		strSunday = ";Sun";
	else
		strSunday = "";
	strDate = document.all[sCName].value;
	if (strDate == "")
		{
		d = new Date();
		strDate = d.getMonth()+1 + "/" + d.getDate() + "/" + d.getFullYear();
		}
	ret = window.showModalDialog("calendar.htm", strDate+strSunday, "dialogWidth:18;dialogHeight:20;");
	if (ret != "")
		document.all[sCName].value = ret;
	document.all[sCName].focus();
}

function isTime(sValue)
{
	var i;
	
	if (sValue.length == 0)
		return false;
	i = sValue.indexOf(":");
	if (i >= 0)
		{
		var iArray = sValue.split(":");
		if (iArray[0] > 23)
			return false;
		if (iArray[1] > 59)
			return false;
		}
	else
		return false;
	return true;
}

function CallXML(query)
{
	var s,objXMLReq = new ActiveXObject("Microsoft.XMLHTTP");
	var bDebug=false;
	
	objXMLReq.open("POST",query,false);    
	objXMLReq.send();
	s = objXMLReq.responseText;
	if (s.substr(0,2) == "<!")
		s = s.slice(s.indexOf(">")+1);

//	bDebug = true;
	if (bDebug)
		{
		var iLen=s.length,iStart=0;
		if (iLen > 1300)	iStart = iLen - 1300;
		alert(s.substr(iStart));
		}
	
	if (s.length > 4)
		execScript(s);
}