// JavaScript used by searchoffense.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 SearchOffense(){
	// 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 sMD = frmMain.cboMagisterialDistrict.value;
			var sOffStat = frmMain.cboOffenseStatus.value;
			var sDis = frmMain.cboDisposition.value;
			var sSA = frmMain.txtSA.value;
			var sSRA = frmMain.txtSRA.value;
			var sBegin = frmMain.txtDateBegin.value;
			var sEnd = frmMain.txtDateEnd.value;
			// Added for public web.
			var sBlock = frmMain.txtBlockNumber.value;
			var sStreet = frmMain.txtStreetName.value;
			var sWatch = frmMain.cboWatch.value;
			var sDirection = frmMain.cboDirection.value;
			var sType = frmMain.cboStreetType.value;
			//var sPrimary = frmMain.chkPrimaryOffense.value;
			}
		else {
			var sUCRPrefix = document.frmMain.cboUCRPrefix.value;
			var sUCR = document.frmMain.cboUCR.value;
			var sMD = document.frmMain.cboMagisterialDistrict.value;
			var sOffStat = document.frmMain.cboOffenseStatus.value;
			var sDis = document.frmMain.cboDisposition.value;
			var sSA = document.frmMain.txtSA.value;
			var sSRA = document.frmMain.txtSRA.value;
			var sBegin = document.frmMain.txtDateBegin.value;
			var sEnd = document.frmMain.txtDateEnd.value;
			// Added for public web.
			var sBlock = document.frmMain.txtBlockNumber.value;
			var sStreet = document.frmMain.txtStreetName.value;
			var sWatch = document.frmMain.cboWatch.value;
			var sDirection = document.frmMain.cboDirection.value;
			var sType = document.frmMain.cboStreetType.value;
			//var sPrimary = frmMain.chkPrimaryOffense.value;
		}

		if (sUCRPrefix != '') {
			iCount = iCount + 1;
		}
		if (sUCR != '') {
			iCount = iCount + 1;
		}
		if (sBlock != '') {
			iCount = iCount + 1;
		}
		if (sStreet != '') {
			iCount = iCount + 1;
		}
		if (sOffStat != '') {
			iCount = iCount + 1;
		}
		if (sDis != '') {
			iCount = iCount + 1;
		}
		if (sMD != '') {
			iCount = iCount + 1;
		}
		if (sSRA != '') {
			iCount = iCount + 1;
		}
		if (sSA != '') {
			iCount = iCount + 1;
		}
		if (sWatch != '') {
			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;			
		}

		// Make sure invalid characters like ";" aren't entered in search text boxes.
		if (!regHack.test(sStreet) && sStreet != '') {
			alert("The street name '" + sStreet + "' is not properly formatted.");
			return false;
		}

		if (!regStr.test(sType) && sType != '') {
			alert("The street type '" + sType + "' is not properly formatted.");
			return false;
		}
		
		if (!regStr.test(sDirection) && sDirection != ''){
			alert("The street direction '" + sDirection + "' is not properly formatted.");
			return false;
		}
		
		if (!regNum.test(sBlock) && sBlock != '') {
			alert("The street block number '" + sBlock + "' is not properly formatted.");
			return false;
		}

		if (!regNum.test(sSA) && sSA != '') {
			alert("The police service area '" + sSA + "' is not properly formatted.");
			return false;
		}

		if (!regNum.test(sSRA) && sSRA != '') {
			alert("The small reporting area '" + sSRA + "' is not properly formatted.");
			return false;
		}	
		return true;
	}
	
	function PopDetail(sICRkey){
		
		var sURL = '';
		//sURL = 'detailicr.asp?icrkey=' + sICRkey
		sURL = 'displayoffense.asp?key=' + sICRkey + '&source=searchoffense.asp';
		winLetterpop = window.open(sURL, 'SearchResultsOffense', 'scrollbars, resizable, dependent, screenX=0, screenY=0,height=500, width=700');	
	}
	
	function DisplayRecords(sDateBegin, sDateEnd, sPrimaryOffense, sUCRPrefix, sUCRCode, sMagisterialDistrict, sOffenseStatus, sDisposition, sSA, sSRA, sStreetName, sBlockNumber, sStreetDirection, sStreetType, sStreetOnly, sIntersectionOnly, sWatch, sVictimInjured, sVandalized, sStolen, sRecovered, sLost, sOrderBy, iPageCount, sSortField, sSortDir){
		// 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 = 'displayoffense.asp?DtBegin=' + sDateBegin+ '&DtEnd=' + sDateEnd + '&POffense=' + sPrimaryOffense + '&UCRPfx=' + sUCRPrefix + '&UCR=' + sUCRCode + '&MD=' + sMagisterialDistrict + '&OStatus=' + sOffenseStatus + '&Disp=' + sDisposition + '&SA=' + sSA + '&SRA=' + sSRA + '&StName=' + sStreetName + '&BlkNbr=' + sBlockNumber + '&Dir=' + sStreetDirection + '&StType=' + sStreetType + '&StOnly=' + sStreetOnly + '&IntOnly=' + sIntersectionOnly + '&Watch=' + sWatch + '&Victim=' + sVictimInjured + '&Vandal=' + sVandalized + '&Stolen=' + sStolen + '&Recover=' + sRecovered + '&Lost=' + sLost + '&sOrderBy=' + sOrderBy + '&sSortField=' + sSortField + '&sSortDir=' + sSortDir + '&source=searchoffense.asp';
			winLetterpop = window.open(sURL, 'SearchResultsOffense', '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 == 2) {
				newWindow = window.open("CFSBlockHelp.htm", "newWindow", "width=250,height=170,left=200,top=200");
			}			if (iHelpID == 3) {
				newWindow = window.open("BlockHelp.htm", "newWindow", "width=250,height=170,left=200,top=200");
			}
			if (iHelpID == 4) {
				newWindow = window.open("StreetHelp.htm", "newWindow", "width=400,height=290,left=200,top=200");
			}			if (iHelpID == 5) {
				newWindow = window.open("StreetTypeHelp.htm", "newWindow", "width=240,height=140,left=200,top=200");
			}
			if (iHelpID == 6) {
				newWindow = window.open("NWHelp.htm", "newWindow", "width=300,height=185,left=200,top=200");
			}
			if (iHelpID == 7) {
				newWindow = window.open("SAHelp.htm", "newWindow", "width=300,height=100,left=200,top=200");
			}
			if (iHelpID == 8) {
				newWindow = window.open("SRAHelp.htm", "newWindow", "width=300,height=120,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;
			frmMain.cboMagisterialDistrict.selectedIndex = 0;
			frmMain.cboOffenseStatus.selectedIndex = 0;
			frmMain.cboDisposition.selectedIndex = 0;
			frmMain.txtSA.value = '';
			frmMain.txtSRA.value = '';
			frmMain.txtDateBegin.value = '';
			frmMain.txtDateEnd.value = '';	
			frmMain.txtStreetName.value = '';
			frmMain.txtBlockNumber.value = '';
			frmMain.cboStreetType.selectedIndex = 0;
			frmMain.cboDirection.selectedIndex = 0;
			frmMain.cboWatch.value = '';
			frmMain.chkPrimaryOffense.checked = false;
			frmMain.chkStreetOnly.checked = false;
			frmMain.chkIntersectionOnly.checked = false;
			frmMain.chkVictimInjured.checked = false;
			frmMain.chkVandalized.checked = false;
			frmMain.chkStolen.checked = false;
			frmMain.chkRecovered.checked = false;
			frmMain.chkLost.checked = false;
			}
		else
			{
			document.frmMain.cboUCRPrefix.selectedIndex = 0;
			document.frmMain.cboUCR.selectedIndex = 0;
			document.frmMain.cboMagisterialDistrict.selectedIndex = 0;
			document.frmMain.cboOffenseStatus.selectedIndex = 0;
			document.frmMain.cboDisposition.selectedIndex = 0;
			document.frmMain.txtSA.value = '';
			document.frmMain.txtSRA.value = '';
			document.frmMain.txtDateBegin.value = '';
			document.frmMain.txtDateEnd.value = '';	
			document.frmMain.txtStreetName.value = '';
			document.frmMain.txtBlockNumber.value = '';
			document.frmMain.cboStreetType.selectedIndex = 0;
			document.frmMain.cboDirection.selectedIndex = 0;
			document.frmMain.cboWatch.value = '';
			document.frmMain.chkPrimaryOffense.checked = false;
			document.frmMain.chkStreetOnly.checked = false;
			document.frmMain.chkIntersectionOnly.checked = false;
			document.frmMain.chkVictimInjured.checked = false;
			document.frmMain.chkVandalized.checked = false;
			document.frmMain.chkStolen.checked = false;
			document.frmMain.chkRecovered.checked = false;
			document.frmMain.chkLost.checked = false;
			}
	}
		
	function streetOnlyClicked() {
	// Treat street only filter check boxes as radio buttons. If you check one uncheck the other, if necessary.
		if (navigator.appName == "Microsoft Internet Explorer") {
			if (frmMain.chkIntersectionOnly.checked == true) {
				frmMain.chkIntersectionOnly.checked = false;
			}
			else {
				frmMain.chkIntersectionOnly.checked = false;
			}
		}
		else {
			if (document.frmMain.chkIntersectionOnly.checked == true) {
				document.frmMain.chkIntersectionOnly.checked = false;
			}
			else {
				document.frmMain.chkIntersectionOnly.checked = false;
			}
		}
	}
	
	function intersectionOnlyClicked() {
	// Treat intersection only filter check boxes as radio buttons. If you check one uncheck the other, if necessary.
		if (navigator.appName == "Microsoft Internet Explorer") {
			if 	(frmMain.chkStreetOnly.checked == true) {
				frmMain.chkStreetOnly.checked = false;
			}
			else {
				frmMain.chkStreetOnly.checked = false;
			}
		}
		else {
			if 	(document.frmMain.chkStreetOnly.checked == true) {
				document.frmMain.chkStreetOnly.checked = false;
			}
			else {
				document.frmMain.chkStreetOnly.checked = false;
			}
		}
	}
	
	// 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,"calOffense","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,"calOffense","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;
	}
