// JavaScript used by searcharrest.asp. Originally, this was in-line code.

	function SortResults(sField, sDir) {
		document.frmAction.txtSortField.value = sField;
		document.frmAction.txtSortDir.value = sDir;
		document.frmAction.submit();
	}

	function GoToPage(iPage) {
		document.frmAction.page.value = iPage;
		document.frmAction.submit();
	}
		
	function SearchArrest(){
	// Purpose of this function is data validation for the form. If false is
	// returned then form not submitted.
		var iCount = 0;
		var iDateCount = 0;
		var bValidBegin = false;
		var bValidEnd = false;
		var sBeginYear = "";
		var sBeginMonth = "";
		var sBeginDay = "";
		var sEndYear = "";
		var sEndMonth = "";
		var sEndDay = "";
		var dBeginDate = "";
		var dEndDate = "";
		var nDateDiff = 0;
		var regSplit = /[\/ ]/;					// Regular expression.
		var regHack = /^[a-zA-Z0-9%*\[\]_ ]*$/;	// Regular expression.
		var regStr =  /^[a-zA-Z0-9 ]*$/;		// Regular expression.
		var regNum = /^[ 0-9]+$/;				// Regular expression.
		var sTemp = "";

		// Identify browser and pick appropriate document object model (DOM).
		if (navigator.appName == "Microsoft Internet Explorer") {
			var sUCRPrefix = frmMain.cboUCRPrefix.value;
			var sUCR = frmMain.cboUCR.value;
			var sBegin = frmMain.txtDateBegin.value;
			var sEnd = frmMain.txtDateEnd.value;
			}
		else {
			var sUCRPrefix = document.frmMain.cboUCRPrefix.value;
			var sUCR = document.frmMain.cboUCR.value;
			var sBegin = document.frmMain.txtDateBegin.value;
			var sEnd = document.frmMain.txtDateEnd.value;
		}

		if (sUCRPrefix != '') {
			iCount = iCount + 1;
		}
		if (sUCR != '') {
			iCount = iCount + 1;
		}
		
		if (sBegin != '') {
			// Do a basic date validation test. Note using VBScript CheckDate function only works in IE.
			if (dateTest(sBegin)) {
				iDateCount = iDateCount + 1;
				// Use regular expression to split entered date into 3 strings broken by the slashes.
				// This creates a string array and each of the 3 new strings are identified by an index.
				sTemp = sBegin.split(regSplit);
				sBeginMonth = sTemp[0];
				sBeginDay = sTemp[1];
				sBeginYear = sTemp[2];
				dBeginDate = Date.UTC(sBeginYear, sBeginMonth, sBeginDay, 0, 0, 0, 0);
				}
			else {
				alert("Begin date " + sBegin + " is invalid.");
				return false;
				}
			}

		if (sEnd != '') {
			// Do a basic date validation test. Note using VBScript CheckDate function only works in IE.
			if (dateTest(sEnd)) {
				iDateCount = iDateCount + 1;
				// Use regular expression to split entered date into 3 strings broken by the slashes.
				// This creates a string array and each of the 3 new strings are identified by an index.
				sTemp = sEnd.split(regSplit);
				sEndMonth = sTemp[0];
				sEndDay = sTemp[1];
				sEndYear = sTemp[2];
				dEndDate = Date.UTC(sEndYear, sEndMonth, sEndDay, 23, 59, 59, 999);
				}
			else {
				alert("End date " + sEnd + " is invalid.");
				return false;
				}
			}

			// If no end date provided use current date.
			if (iDateCount == 1 && sEnd == '') {
				dEndDate = new Date;
				dEndDate = dEndDate.valueOf();
				}
		// Date.UTC converts date to milliseconds. Convert to days.
		nDateDiff = ((dEndDate - dBeginDate) / 1000 / 60 / 60 / 24);
		// The form is not filled out at all.	
		if (iCount == 0 && iDateCount == 0) {
			alert("At least one criteria must be entered before starting a search (street direction, street type, or any of the check boxes do not count). If a date range is your sole criteria for a search it must be for a month or less.");
			return false;
		}
		
		// If date range is filled out the beginning date can't be less than ending date.
		if (nDateDiff < 0) {
			alert("The beginning date [" + sBegin + "] cannot be less than the end date [" + sEnd + "].");
			return false;
		}
		
		// Only a date range is filled out on the form. It must be no more than a month's worth.
		else if (iCount == 0 && iDateCount >= 1 && sBegin != '') {
			if (Math.round(nDateDiff) > 31) {
				alert("If a date range is your sole criteria for a search it must be for a month or less.");
				return false;
			}
		}
		// Only the end date is filled out on the form. We need a begin date.
		else if (iDateCount == 1 && sBegin == '') {
			alert("If a date range is selected you must enter a begin date.");
			return false;			
		}
		
		return true;
	}
	
	function PopDetail(iKey){
		// Updated for pop-up display of offense data 4/8/05.
		var sURL = '';
		sURL = 'displayoffense.asp?source=searcharrest.asp&key=' + iKey;
		winLetterpop = window.open(sURL, 'SearchResultsArrest', 'address, scrollbars, resizable, dependent, screenX=0, screenY=0,height=500, width=700');	
	}
	
	function DisplayRecords(sDateBegin, sDateEnd, sUCRPrefix, sUCRCode, sOrderBy, iPageCount, sSortField, sSortDir){
	//function DisplayRecords(iPageCount){
		// If over 100 pages (2000 records) do not allow user to display records. Large recordsets croak plus connection speed is an issue with dial-up users.
		if (iPageCount <= 100) {
			var sURL = '';
			sURL = 'displaycharge.asp?DtBegin=' + sDateBegin + '&DtEnd=' + sDateEnd + '&UCRPfx=' + sUCRPrefix + '&UCR=' + sUCRCode + '&sOrderBy=' + sOrderBy + '&sSortField=' + sSortField + '&sSortDir=' + sSortDir;
			winLetterpop = window.open(sURL, 'SearchResultsArrest', 'scrollbars, resizable, dependent, screenX=0, screenY=0,height=480, width=640');
		}
		else {
			alert("Printing is limited to 2,000 records. Please narrow your search if you wish to print records.");
		}
	}
	
	function getHelp(iHelpID) {
	// This function supplies help.
			if (iHelpID == 1) {
				newWindow = window.open("DateHelp.htm", "newWindow", "width=370,height=340,left=200,top=200");
			}
			if (iHelpID == 6) {
				newWindow = window.open("NWHelp.htm", "newWindow", "width=300,height=185,left=200,top=200");
			}
	}	function findLivePageWidth() {
	// This function added to make pop-up hypertext work.		if (window.innerWidth != null)			return window.innerWidth;		if (document.body.clientWidth != null)			return document.body.clientWidth;		return (null);	}		function popUp(evt,objectID){
	// This function added to make pop-up hypertext work.		if (isDHTML) { // Makes sure this is a DHTML browser			var livePageWidth = findLivePageWidth();			//alert(livePageWidth);			domStyle = findDOM(objectID,1);			dom = findDOM(objectID,0);			state = domStyle.visibility;			if (dom.offsetWidth) elemWidth = dom.offsetWidth;			else { if (dom.clip.width)	elemWidth = dom.clip.width; }			if (state == "visible" || state == "show")  { domStyle.visibility = "hidden"; }			else {				if (evt.pageY) { //Calculates the position for Navigator 4 					topVal = evt.pageY + 4;					leftVal = evt.pageX - (elemWidth / 2); 				}				else { 					if (evt.y) { // Calculates the position for IE4
						topVal = evt.y + 4 + document.body.scrollTop;
						leftVal = evt.x - (elemWidth / 2) + document.body.scrollLeft;
					}
				}
			/*If the element goes off the page to the left, this moves it back */
				if(leftVal < 2) { leftVal = 2; }
				else { 
					if ((leftVal + elemWidth) > livePageWidth) { leftVal = leftVal - (elemWidth / 2); }
				}
			domStyle.top = topVal; // Positions the element from the top
				domStyle.left = leftVal; // Positions the element from the left
				domStyle.visibility = "visible"; // Makes the element visable 
			}
		}
	}
	
	function ClearOptions()
	{
		// Compatibility issue.  IE and Navigator have different syntaxes.
		if (navigator.appName == "Microsoft Internet Explorer")
			{
			frmMain.cboUCRPrefix.selectedIndex = 0;
			frmMain.cboUCR.selectedIndex = 0;
			document.frmMain.txtDateBegin.value = '';
			frmMain.txtDateEnd.value = '';	
			}
		else
			{
			document.frmMain.cboUCRPrefix.selectedIndex = 0;
			document.frmMain.cboUCR.selectedIndex = 0;
			document.frmMain.txtDateBegin.value = '';
			document.frmMain.txtDateEnd.value = '';	
			}
	}
		
	// For popup calendars.
	function CalPopupFrom(sLnk) {
		var sDate;
		var iLeftPos;
		var iTopPos;
		var bBadDate;
		
		if (navigator.appName == "Microsoft Internet Explorer") {
			if (frmMain.txtDateBegin.value == "") {
				sDate = "";
				bBadDate = false;
			}
			else {
				sDate = frmMain.txtDateBegin.value;
				if (CheckDate(sDate) == false) {
					bBadDate = true;
					}
				else {
					bBadDate = false;
				}
			}
		}
		else {
			if (document.frmMain.txtDateBegin.value == "") {
				sDate = "";
				bBadDate = false;
			}
			else {
				sDate = document.frmMain.txtDateBegin.value;
				// Use JavaScript dateTest() function because Netscape can't read ASP function 
				// embedded in JavaScript.
				if (dateTest(sDate) == false) {
					bBadDate = true;
					}
				else {
					bBadDate = false;
				}
			}
		}
		
		if (bBadDate == false) {
			iLeftPos = (screen.width - 160) / 2;
			iTopPos = (screen.height - 185) / 2;
		
			sLnk = sLnk + '?field=txtDateBegin&form=frmMain&date=' + sDate + '&url=' + window.location; 
			window.open(sLnk,"calArrest","height=195,width=160,scrollbars=no,left=" + iLeftPos + ",top=" + iTopPos); 
			}
		else {
			alert(sDate + ' is an invalid date.');
		}
	}
		
	function CalPopupTo(sLnk) {
		var sDate;
		var iLeftPos;
		var iTopPos;
		var bBadDate;

		if (navigator.appName == "Microsoft Internet Explorer") {
			if (frmMain.txtDateEnd.value == "") {
				sDate = "";
				bBadDate = false;
			}
			else {
				sDate = frmMain.txtDateEnd.value;
				if (CheckDate(sDate) == false) {
					bBadDate = true;
					}
				else {
					bBadDate = false;
				}
			}
		}
		else {
			if (document.frmMain.txtDateEnd.value == "") {
				sDate = "";
				bBadDate = false;
			}
			else {
				sDate = document.frmMain.txtDateEnd.value;
				// Use JavaScript dateTest() function because Netscape can't read ASP function 
				// embedded in JavaScript.
				if (dateTest(sDate) == false) {
					bBadDate = true;
					}
				else {
					bBadDate = false;
				}
			}
		}
		
		if (bBadDate == false) {
			iLeftPos = (screen.width - 160) / 2;
			iTopPos = (screen.height - 185) / 2;

			sLnk = sLnk + '?field=txtDateEnd&form=frmMain&date=' + sDate + '&url=' + window.location;
			window.open(sLnk,"calArrest","height=195,width=160,scrollbars=no,left=" + iLeftPos + ",top=" + iTopPos); 
		} 
		else {
			alert(sDate + ' is an invalid date.');
		}
	}
	
	function dateTest(sDate) {
		var regDate = /^[01]?\d\/[0123]?\d\/[12]\d{3}/	// Regular expression.
		var regSplit = /[\/ ]/;							// Regular expression.
		var sDateArray = "";

		// Split date into an array. Index 0 = month, index 1 = day, index 2 = year.
		sDateArray = sDate.split(regSplit);
		
		// Test to make sure date in mm/dd/yyyy format using regular expression.
		if (!regDate.test(sDate)) {
			return false;
		}
		// Test for valid month.
		if (sDateArray[0] > 12) {
			return false;
		}
		// First test for valid number of days in month.
		if ((sDateArray[0] == '04' || sDateArray[0] == '4' || sDateArray[0] == '06' || sDateArray[0] == '6' || sDateArray[0] == '09' || sDateArray[0] == '9' || sDateArray[0] == '11') && sDateArray[1] > 30) {
			return false;
		}
		// Second test for valid number of days in month.
		if ((sDateArray[0] == '01' || sDateArray[0] == '1' || sDateArray[0] == '03' || sDateArray[0] == '3' || sDateArray[0] == '05' || sDateArray[0] == '5' || sDateArray[0] == '07'  || sDateArray[0] == '7'  || sDateArray[0] == '08'  || sDateArray[0] == '8' || sDateArray[0] == '10'  || sDateArray[0] == '12') && sDateArray[1] > 31) {
			return false;
		}
		// Third test for valid number of days in month (non leap years).
		if ((sDateArray[0] == '02' || sDateArray[0] == '2') && sDateArray[2] % 4 > 0 && sDateArray[1] > 28) {
			return false;
		}
		// Fourth test for valid number of days in month (leap years).
		if ((sDateArray[0] == '02' || sDateArray[0] == '2') && sDateArray[2] % 4 == 0 && sDateArray[1] > 29) {
			return false;
		}		
	return true;
	}

  