		// This variable defines the Grid row selection color.
		var rowColor = "#BB99CC";
		
		// This variable carries the SPL Local URL.
		var baseURL = "http://" + location.host + "/SPLLocal/";
		
		// This variable carries the SPL Web URL.
		var SPLWebURL = "http://localhost/SPLWeb/";
		
		// Replaces javascript's confirm dialog box with VBScript's yes/no box.
		function window.confirm(str)
		{
			execScript('n = msgbox("'+str+'","4132")', "vbscript");
			return(n == 6);
		}

		// Select an item in the dropdown list on the client-side.
		function SetDropDownList(ddl, item) {
			//alert("Item = " + item + " -- " + ddl.options[3].text);
			ddl.selectedIndex = 0;
			for (i=0; i<ddl.length;i++) {
				if (ddl.options[i].value == item) {
					ddl.selectedIndex = i;
				}
			}
		}

		// Check Report Number
		function CheckRptNumFormat(rptnum) {
			var reNum=/^[A-Z](\-|\.)\d{2}\1?\d{6}$/; //regular _expression defining a report number
			if (rptnum.length > 0 &&  rptnum.search(reNum)==-1) {//if match failed
				alert("Please enter a valid Report Number within the 'Report Number' field.");
				return false;
			}
			else {
				return true;
			}
		}



		function validateSearchForm(frmSearch)
		{
		var rptnum = frmSearch.txtreportnumber.value;
		if(!CheckRptNumFormat(rptnum))
			return false;
		var frmItemsSelected = 0;
		var dateinvalid = false;
		for (var i=0; i<frmSearch.length ; i++) 
			{
				var theElement = frmSearch.elements[i];
				var element_type = theElement.type;
				var element_name = theElement.name;
				var element_value = theElement.value;
				// Check Text boxes ...
				//	alert(element_name + ' ' +  element_value +  ' ' + element_type);
			
				//if(element_type="text" && element_name.indexOf('date'))
				
				if (element_type == "text" && element_value != '')
				{
					frmItemsSelected = frmItemsSelected + 1;
					if(element_name.indexOf('date') != -1)
					{
						if(!dateValid(element_value))
						{
							dateinvalid = true;
							break;
						}
					}
				}
				else if(element_type == "checkbox" && theElement.checked == true)
				{
					frmItemsSelected = frmItemsSelected + 1;
				}
				else if(element_name == "cbosex" && theElement.selectedIndex > 0 )
				{
					frmItemsSelected = frmItemsSelected + 1;
				}
			}
			if(frmItemsSelected == 0)
			{
					alert('Select a search Criteria');
					return false;
			}
			if(dateinvalid == true)
			{
				alert("Enter the date in the correct format (mm/dd/yyyy)");
				theElement.focus();
				return false;
			}
			return true;
		}
		
			function clearForm(frmSearch)
			{
				for (var i=0; i<frmSearch.length ; i++) 
				{
					var theElement = frmSearch.elements[i];
					var element_type = theElement.type;
					var element_name = theElement.name;
					var element_value = theElement.value;
			// Check Text boxes ...
				//	alert(element_name + ' ' +  element_value +  ' ' + element_type);
					if (element_type == "text") 
					{
						theElement.value = '';
					}
					else if(element_type == "checkbox")
					{
						theElement.checked = false;
					}
					else if(element_name == "cboreportcat")
					{
						theElement.selectedIndex = 4;// = false;
					}
					else if(element_name == "cbosex")
					{
						theElement.selectedIndex = 0;
					}
				}
			}
		


		// - Validates an entry against a US zip code.
		function CheckZipCodeFormat(zipCtrl){
			var re5digit=/(^\d{5}$)|(^\d{5}-\d{4}$)/ //regular expression defining a 5 digit number
			if (zipCtrl.value.length > 0 && zipCtrl.value.search(re5digit)==-1) {//if match failed
				alert("Please enter a valid Zip Code. E.g. 01581");;
				zipCtrl.focus();
				return false;
			}
			else {
				//alert("Match");
				return true;
			}
		}
				
		// - Validates an entry against a US phone number.
		function checkPhoneFormat(phoneCtrl){
		var re10digit = /^([0-9]{10}$)|(\d{3}-\d{3}-\d{4}$)|(\(\d{3}\) \d{3}-\d{4}$)/
			if (phoneCtrl.value.length > 0 && phoneCtrl.value.search(re10digit)==-1) {//if match failed
				alert("Please enter a valid 10 digit phone number. E.g. 5081234321");
				phoneCtrl.focus();
			}
			else {
				//alert("Match");
				if (phoneCtrl.value.length > 0)
					phoneCtrl.value = ParseUSNumber(phoneCtrl.value);

			}
		}

		// - This function automatically converts the 10 digit phone number to (xxx) xxx-xxxx.
		function ParseUSNumber(PhoneNumberInitialString)
		{
			var FmtStr="";
			var index = 0;
			var LimitCheck;

			LimitCheck = PhoneNumberInitialString.length;
			while (index != LimitCheck)
			{
				if (isNaN(parseInt(PhoneNumberInitialString.charAt(index))))
				{ }
				else
				{ FmtStr = FmtStr + PhoneNumberInitialString.charAt(index); }
				index = index + 1;
			}
			if (FmtStr.length == 10)
			{
				FmtStr = "(" + FmtStr.substring(0,3) + ") " + FmtStr.substring(3,6) + "-" + FmtStr.substring(6,10);
			}
			else
			{
				FmtStr=PhoneNumberInitialString;
				alert("United States phone numbers must have exactly ten digits.");
			}
			return FmtStr;
		}

		// New Code Added -------------------------
		// Mode: 'sno' - Called from SnoMed Services tab.
		//		 'mac' - Called from Diagnosis tab.
		function AddCode(mode)
		{
			//alert(mode);
			if (mode == 'sno') {
				var tcode = document.getElementById('txtTCode');
			}
			else {
				var tcode = document.getElementById('txtTCode2');
			}
			//alert(tcode.value);
			var snomed = document.getElementById('cboSnoMeds');
			var macrocode = document.getElementById('txtMacro');
			if (tcode.value.length != 0 || snomed.value.length != 0 || macrocode.value.length != 0) {
				var rowid = addRowToTable(tcode,snomed,macrocode,'icd9');
//				alert("Row ID: " + rowid);
				var snospan;
				if (mode == 'sno') {
					var icd9span = rowid + snomed.value;
					macrocode.value = "";
				}
				else {
					var icd9span = rowid + macrocode.value + 'I';
					snospan = rowid + macrocode.value + 'S';
				}
//				alert(icd9span);
//				alert(snospan);
				getTCSMDesc(tcode.value,snomed.value,macrocode.value,snospan,icd9span);
			}
		}
		
		// - Adds a row to the table
		function addRowToTable(tcode,snomed,macrocode,icd9)
		{
			var tbl = document.getElementById('tblTCodesSelected');
			var lastRow = tbl.rows.length;
			// if there's no header row in the table, then iteration = lastRow + 1
			var iteration = lastRow;
			var row = tbl.insertRow(lastRow);
			row.bgColor ='white';
			row.setAttribute('id',lastRow);
			// TCode
			var cellTcode = row.insertCell(0);
			var textTcode = document.createTextNode(tcode.value);
			cellTcode.setAttribute('class','tabledata');
			cellTcode.setAttribute('width','33%');
			cellTcode.appendChild(textTcode);

			// Snomed
			var cellSnomed = row.insertCell(1);
			if (snomed.value) {
				var textSnomed = document.createTextNode(snomed.value);
				cellSnomed.setAttribute('class','tabledata');
				cellSnomed.setAttribute('width','33%');
				cellSnomed.appendChild(textSnomed);
			}
			else {
				//alert('In Macro Span');
				var textSnomed = "<span id='" + lastRow + macrocode.value + "S'></span>";
				cellSnomed.setAttribute('class','tabledata');
				cellSnomed.setAttribute('width','33%');
				cellSnomed.innerHTML = textSnomed;
			}
			

			// ICD9
			var cellICD9 = row.insertCell(2);
			//var textICD9 = document.createTextNode(icd9.value);
			if (snomed.value) {
				var textICD9 = "<span id='" + lastRow + snomed.value + "'></span>";
			}
			else{
				var textICD9 = "<span id='" + lastRow + macrocode.value + "I'></span>";
			}
			cellICD9.setAttribute('class','tabledata');
			cellICD9.setAttribute('width','33%');
			cellICD9.innerHTML = textICD9;
			
			row.onclick=onRowClick;

			return lastRow;

		}
		
		function removeRowFromTable(rowNum)
		{
			var tbl = document.getElementById('tblTCodesSelected');
			//alert(tbl.rows(rowNum).innerHTML);
			// create the XML object
			objXmlDoc = new ActiveXObject("Msxml2.DOMDocument");
			objXmlDoc.loadXML('<?xml version="1.0"' + tbl.rows(rowNum).innerHTML);
			//alert(objXmlDoc.xml);
			//alert(objXmlDoc.getElementsByTagName("TD")[0].firstChild.nodeValue)
			tbl.deleteRow(rowNum);
			for(i=0;i<tbl.rows.length;i++)
			{
				var currentrow = tbl.rows[i];
				currentrow.id = i;
			}

			document.getElementById('hdselectedRow').value = 'x';
		}
		
		function RemoveCode()
		{
			selectedRow = document.getElementById('hdselectedRow').value;
			if(document.getElementById('hdselectedRow').value == 'x')
			{alert('select a row to be deleted');}
			else
			{removeRowFromTable(selectedRow);}
		}
		
		function onRowClick(e,obj)
		{
			var objId;
			if (obj != null) 
			{
				objId = obj.id;
			} 
			else 
			{
				objId = this.id;
			}
			
			if(document.getElementById(objId).bgColor == '#ffffff')
			{
				if(document.getElementById('hdselectedRow').value != 'x')
				{
					document.getElementById(document.getElementById('hdselectedRow').value).bgColor = '#ffffff';
				}
				document.getElementById(objId).bgColor = rowColor;
				
				document.getElementById('hdselectedRow').value = objId;
			}
			else
			{
					document.getElementById(objId).bgColor  = '#ffffff';
					document.getElementById('hdselectedRow').value = 'x';
			}
		}
		
		function BoolNumToString(numval) {
			if (numval == undefined) { return; }
			if (numval) {
				return "Yes";
			}
			else {
				return "No";
			}
		}
		
		function checkLen(x,y)
		{
		if (y.length==x.maxLength)
			{
			var next=x.tabIndex
			if (next<document.getElementById("myForm").length)
				{
				document.getElementById("myForm").elements[next].focus()
				}
			}
		}

		function getAge(dateString,dateType) {
		/*
		function getAge
		parameters: dateString dateType
		returns: boolean

		dateString is a date passed as a string in the following
		formats:

		type 1 : 19970529
		type 2 : 970529
		type 3 : 29/05/1997
		type 4 : 29/05/97
		type 5 : 05/29/1997

		dateType is a numeric integer from 1 to 4, representing
		the type of dateString passed, as defined above.

		Returns string containing the age in years, months and days
		in the format yyy years mm months dd days.
		Returns empty string if dateType is not one of the expected
		values.
		*/
			//alert(dateString);
			
			var now = new Date();
			var today = new Date(now.getYear(),now.getMonth(),now.getDate());

			var yearNow = now.getYear();
			var monthNow = now.getMonth();
			var dateNow = now.getDate();
			
			//alert("Now: " + yearNow + " -- " + monthNow + " -- " + dateNow);

			if (dateType == 1)
				var dob = new Date(dateString.substring(0,4),
									dateString.substring(4,6)-1,
									dateString.substring(6,8));
			else if (dateType == 2)
				var dob = new Date(dateString.substring(0,2),
									dateString.substring(2,4)-1,
									dateString.substring(4,6));
			else if (dateType == 3)
				var dob = new Date(dateString.substring(6,10),
									dateString.substring(3,5)-1,
									dateString.substring(0,2));
			else if (dateType == 4)
				var dob = new Date(dateString.substring(6,8),
									dateString.substring(3,5)-1,
									dateString.substring(0,2));
			else if (dateType == 5)
				var dob = new Date(dateString.substring(6,10),
									dateString.substring(0,2),
									dateString.substring(3,5));
			else
				return '';

			//alert("DOB = " + dob);
			alert("Date String: " + dateString.substring(6,10) + " -- " + dateString.substring(0,2) + " -- " + dateString.substring(3,5));
			
			var yearDob = dob.getYear();
			var monthDob = dob.getMonth();
			var dateDob = dob.getDate();
			//alert("Entered: " + yearDob + " -- " + monthDob + " -- " + dateDob);
			yearAge = yearNow - yearDob;
			//alert("Year Age: " + yearAge)
			if (monthNow >= monthDob)
				var monthAge = monthNow - monthDob;
			else {
				yearAge--;
				var monthAge = 12 + monthNow -monthDob;
			}
			
			if (dateNow >= dateDob)
				var dateAge = dateNow - dateDob;
			else {
				monthAge--;
				var dateAge = 31 + dateNow - dateDob;

				if (monthAge < 0) {
					monthAge = 11;
					yearAge--; 
				}
			}

			return yearAge + ' years ' + monthAge + ' months ' + dateAge + ' days';
			
		}
		
function checkNumeric(objName,minval, maxval,comma,period,hyphen)
{
	var numberfield = objName;
	if (chkNumeric(objName,minval,maxval,comma,period,hyphen) == false)
	{
		numberfield.select();
		numberfield.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function chkNumeric(objName,minval,maxval,comma,period,hyphen)
{
	// only allow 0-9 be entered, plus any values passed
	// (can be in any order, and don't have to be comma, period, or hyphen)
	// if all numbers allow commas, periods, hyphens or whatever,
	// just hard code it here and take out the passed parameters
	var checkOK = "0123456789" + comma + period + hyphen;
	var checkStr = objName;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";

	for (i = 0;  i < checkStr.value.length;  i++)
	{
		ch = checkStr.value.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
			allNum += ch;
	}
	if (!allValid)
	{	
		alertsay = "Please enter only these values \""
		alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
		alert(alertsay);
		return (false);
	}

	// set the minimum and maximum
	var chkVal = allNum;
	var prsVal = parseInt(allNum);
	if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))
	{
		alertsay = "Please enter a value greater than or "
		alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
		alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
		alert(alertsay);
		return (false);
	}
}
		
function calculateage(paramdate)
{
	var yr;
	var mon;
	var day;
	var unit;
	var valdecimal = 0;
	var round;

	var one_day=1000*60*60*24
	var one_month=1000*60*60*24*30
	var one_year=1000*60*60*24*30*12

	arrdate = paramdate.split("/");
	yr = arrdate[2];
	mon = arrdate[0];
	day = arrdate[1];
	unit = "years";
	round = "roundup";		
	//alert(mon);

	today=new Date()
	var pastdate=new Date(yr, mon-1, day);

	var countunit=unit
	var decimals=valdecimal;
	var rounding=round


	finalunit=(countunit=="days")? one_day : (countunit=="months")? one_month : one_year
	decimals=(decimals<=0)? 1 : decimals*10


	if (unit!="years"){
		if (rounding=="rounddown")
		return(Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals)
		else
		return(Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals)
	}
	else{
		yearspast=today.getFullYear()-yr-1
		tail=(today.getMonth()>mon-1 || today.getMonth()==mon-1 && today.getDate()>=day)? 1 : 0
		pastdate.setFullYear(today.getFullYear())
		pastdate2=new Date(today.getFullYear()-1, mon-1, day)
		tail=(tail==1)? tail+Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals : Math.floor((today.getTime()-pastdate2.getTime())/(finalunit)*decimals)/decimals
		return(yearspast+tail)
	}
}

function openReportwindow(url)
{
	window.open(url,"","")
}	