//==================================================================================
// ============== Variables Declrations Beginnig ===================================
//var theForm = document.frmNavigation;

// p is an abbreviation for "prompt"
var pEntryPrompt = "Please enter a ";
var pDate = "date in the following format mm/dd/yyyy."
var pTime = "time in the following format hh:mm."

var errorStatus = 0;
var errorMonthLength = 1;
var errorLeapYear = 2;
var errorStartDate = 3;
var errorStartTime = 4;
var errorDateOutOfRange = 5;
var errorDateFormat = 6;
var errorHourRange = 7;
var errorMinuteRange = 8;
var errorMonthRange = 9;

var monthsFullName = new Array("","January","February","March","April",
                                  "May","June","July", "August", "September",
                                  "October", "November", "December");

// ============== Variables Declarations Beginnig ==================================
//==================================================================================
// ============== Time Functions Beginnig ==========================================
		
// Cehck if the time is valid 
function isValidTime(hh, mm){
	if (hh > 23) {
		errorStatus = errorHourRange;
		return false;
	} else if (mm > 59) {
		errorStatus = errorMinuteRange;
		return false;
	}
	return true;
}		

function chktime(objName, theField){
	//var theForm = document.frmNavigation;
	var timefield = objName;
	var strTime;

	strTime = timefield.value;
	if (strTime.length < 1) {
	return true;
	}

	if (strTime.length != 5) {
		warnInvalid (objName, theField, "Invalid Time -format is hh:mm.");				
		return false;
	}

	if (!CheckTimeFormat(strTime)) {
		alert("Invalid Time -format is hh:mm.");
		return false;
	}
			
	var sHour = strTime.substring(0, 2);	// hour
	var sMinute = strTime.substring(3, 5);	// hour
			
    if ( !isValidTime( sHour, sMinute ) ){
		if ( errorStatus == errorHourRange ){
		 	alert("Number of hours entered is greater than 23");
		}//if errorStatus
		else if ( errorStatus == errorMinuteRange ){
		 	alert("Number of minutes entered is greater than 59");
		}//else if errorStatus
		return false;
    } else { // if !isValidDate
		return true;
    }
}			

function checktime(objName, theField) {
var timefield = objName;
	if (chktime(objName, theField) == false) {
		timefield.select();
		timefield.focus();
		return false;
	}
	else {
		return true;
	}
}	

// ============== Time Functions Ending ============================================
//==================================================================================
// ============== Date Functions Beginnig ==========================================

// Cehck if the date is valid 
function isValidDate(dd, mm, yyyy){
   
	var theForm = document.frmNavigation;
	var MinDate = theForm.LD_YMD.value;		// Minimum Date
	var MaxDate = theForm.RD_YMD.value;		// Maximum Date
	var InDate = yyyy + mm + dd;

   if ( mm < 1 || mm > 12) {
		errorStatus = errorMonthRange;
		return false;
   } else if (( mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) && dd > 31) {
		errorStatus = errorMonthLength;
		return false;
   } else if (( mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30 ){
		errorStatus = errorMonthLength;
		return false;
   } else if ( mm == 2 && dd > 29 ) {
		errorStatus = errorMonthLength;
		return false;
   } else if (mm == 2 && yyyy % 4 > 0 && dd > 28){
        errorStatus = errorLeapYear;
        return false;
   } else if ( (InDate < MinDate) || (InDate > MaxDate) ) {
        errorStatus = errorDateOutOfRange;
        return false;
   }// if yyyy % 4
   return true;
}//isValidDate

// Check all dates in the array
function chkdate(objName, theField){
	//var theForm = document.frmNavigation;
	var sDate = objName.value;
	var datefield = objName;
	var strDate;

	strDate = datefield.value;
	if (strDate.length < 1) {
	return true;
	}
			
	if (sDate.length != 10) {
		warnInvalid (objName, theField, "Invalid Date -format is mm/dd/yyyy.");				
		return false;
	}
			
	if (!CheckDateFormat(sDate)) {
		alert("Invalid Date -format is mm/dd/yyyy.");
		return false;
	}
			
	sMonth = sDate.substring(0, 2);		// month
	sDay = sDate.substring(3, 5);		// day
	sYear = sDate.substring(6, 10);		// year
			
    if ( !isValidDate( sDay, sMonth, sYear ) ){
		if ( errorStatus == errorMonthRange ){
		 	alert(" A year can not have " + sMonth + " month.");
		} else if ( errorStatus == errorMonthLength ){
		 	alert(monthsFullName[parseInt(sMonth,10)] + " can not have " + sDay + " days.");
		}//if errorStatus
		else if ( errorStatus == errorLeapYear ){
		 	alert(year + " is not a leap year.");
		}//else if errorStatus
		else if ( errorStatus == errorDateOutOfRange ){
		 	alert( "Data is not available for " + monthsFullName[parseInt(sMonth,10)] + " " + parseInt(sDay,10) + ", " + sYear );
		}//else if errorStatus
		return false;
    } else { // if !isValidDate
		return true;
    }
}// checkDate				

function checkdate(objName, theField) {	var datefield = objName;
	if (chkdate(objName, theField) == false) {
		datefield.select();
		datefield.focus();
		return false;
	}
	else {
		return true;
	}
}

// ============== Date Functions Ending ============================================
//==================================================================================
// ============== Miscellaneous Functions Beginnig =================================
		
// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.
function warnInvalid (objName, theField, s) {
	if (theField == "FD") {
		objName.select();
		alert(s);
		objName.focus();
	} else if (theField == "TD") {
		objName.select();
		alert(s);
		objName.focus();
	} else if (theField == "FT") {
		objName.select();
		alert(s);
		objName.focus();
	} else if (theField == "TT") {
		objName.select();
		alert(s);
		objName.focus();
	}
    return false
}

// Returns true if character c is a digit (0 .. 9).
function isDigit(c)
{   return ((c >= "0") && (c <= "9"))
}

function isBackSlash(c)
{   return ((c == "/"))
}

function isColon(c)
{   return ((c == ":"))
}

// Check that current character is number.
function CheckDateFormat(s) {
	if (!isDigit(s.charAt(0))) return false;
	if (!isDigit(s.charAt(1))) return false;
	if (!isBackSlash(s.charAt(2))) return false;
	if (!isDigit(s.charAt(3))) return false;
	if (!isDigit(s.charAt(4))) return false;
	if (!isBackSlash(s.charAt(5))) return false;
	if (!isDigit(s.charAt(6))) return false;
	if (!isDigit(s.charAt(7))) return false;
	if (!isDigit(s.charAt(8))) return false;
	if (!isDigit(s.charAt(9))) return false;
	return true;
}

// Check that current character is number.
function CheckTimeFormat(s) {
	if (!isDigit(s.charAt(0))) return false;
	if (!isDigit(s.charAt(1))) return false;
	if (!isColon(s.charAt(2))) return false;
	if (!isDigit(s.charAt(3))) return false;
	if (!isDigit(s.charAt(4))) return false;
	return true;
}

// Display prompt string s in status bar.
function prompt (s) {
	window.status = s;
}

// Display data entry prompt string s in status bar.
function promptEntry (s) {
	window.status = pEntryPrompt + s;
}

// ============== Miscellaneous Functions Ending ===================================
//==================================================================================
// ============== FORM Beginnig ====================================================

function writeSearchForm() {

var sLD_MDY = document.frmNavigation.LD_MDY.value;
var sRD_MDY = document.frmNavigation.RD_MDY.value;
var sMD_MDY = document.frmNavigation.MD_MDY.value;

var sUDOW = document.frmNavigation.UDOW.value;
var sULD = document.frmNavigation.ULD.value;
var sURD = document.frmNavigation.URD.value;

var sULT = document.frmNavigation.ULT.value;
var sURT = document.frmNavigation.URT.value;
var sUI = document.frmNavigation.UI.value;

var content = "";
//content += "<form name='frmAddr' action='javascript:doAddrSearch()' onsubmit='javascript:doAddrSearch()'>"
content = "<form name='frmAdvSearch'>"

content += "  <TABLE border='0' cellPadding='2' cellSpacing='1' bgColor='#FFFFFF' width='200'>"
content += "  <TR>";
content += "  <TD bgColor='#FFF2D7' align='left'><font class='SearchForm3'>" + "Data available from ";
content += sLD_MDY + " to " + sRD_MDY + "</font></TD>";
content += "  </TR>"

content += "<TR>";
content += "<TD bgColor='#FFF2D7' align='left'><font class='SearchForm3'>"
content += "Unincorporated Miami-Dade County and City of North Miami cases only</font></TD>"
content += "</TR>"
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
content += "<TR bgcolor='#FFF2D7'><TD>"
content +="  <TABLE bgcolor='#FFF2D7' border='0' cellPadding='2' cellSpacing='0' width ='100%'>"

content += "<TR>"
content += "<td align='left'><font class='SearchForm2'>From Date:</font><br><font class='SearchForm0'>(mm/dd/yyyy)</font></td>"
if (sULD == ""){
	content += "<td align='left'><font class='input1'><input class='input1' type=text size='12' maxlength='10' name='FROMDATE' onFocus='promptEntry(pDate)' value=" + sMD_MDY + ">";
	if (!isNav) content += "<a name='cal1' href='javascript:showCalendar()'><IMG NAME='cal1' SRC='images/b_calendar.gif' border='0' alt='Show Calendar' align='absmiddle' height='20' width='20'></a>";
	content += "</font></td>";
} else {
	content += "<td align='left'><font class='input1'><input class='input1' type=text size='12' maxlength='10' name='FROMDATE' onFocus='promptEntry(pDate)' value=" + sULD + ">";
	if (!isNav) content += "<a name='cal1' href='javascript:showCalendar()'><IMG NAME='cal1' SRC='images/b_calendar.gif' border='0' alt='Show Calendar' align='absmiddle' height='20' width='20'></a>";
	content += "</font></td>";
}
content += "</TR>"

content += "<TR>"
content += "<td align='left'><font class='SearchForm2'>To Date:</font><br><font class='SearchForm0'>(mm/dd/yyyy)</font></td>"
if (sURD == "") {
	content += "<td align='left'><font class='input1'><input class='input1' type=text size='12' maxlength='10' name='TODATE' onFocus='promptEntry(pDate)' value=" + sRD_MDY + ">";
	if (!isNav) content += "<a name='cal2' href='javascript:showCalendar()'><IMG NAME='cal2' SRC='images/b_calendar.gif' border='0' alt='Show Calendar' align='absmiddle' height='20' width='20'></a>";
	content += "</font></td>";
} else {
	content += "<td align='left'><font class='input1'><input class='input1' type=text size='12' maxlength='10' name='TODATE' onFocus='promptEntry(pDate)' value=" + sURD + ">";
	if (!isNav) content += "<a name='cal2' href='javascript:showCalendar()'><IMG NAME='cal2' SRC='images/b_calendar.gif' border='0' alt='Show Calendar' align='absmiddle' height='20' width='20'></a>";
	content += "</font></td>";
}
content += "</TR>"
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
content += "<TR>"
content += "<td align='left'><font class='SearchForm2'>From Time:</font><br><font class='SearchForm0'>(hh:mm)</font></td>"
if (sULT == "")
	content += "<td align='left'><font class='input1'><input class='input1' type=text size='6' maxlength='5' name='FROMTIME' onFocus='promptEntry(pTime)' value='00:00'></font></td>"
else
	content += "<td align='left'><font class='input1'><input class='input1' type=text size='6' maxlength='5' name='FROMTIME' onFocus='promptEntry(pTime)' value=" + sULT + "></font></td>"
content += "</TR>"

content += "<TR>"
content += "<td align='left'><font class='SearchForm2'>To Time:</font><br><font class='SearchForm0'>(hh:mm)</font></td>"
if (sURT == "")
	content += "<td align='left'><font class='input1'><input class='input1' type=text size='6' maxlength='5' name='TOTIME' onFocus='promptEntry(pTime)' value='23:59'></font></td>"
else
	content += "<td align='left'><font class='input1'><input class='input1' type=text size='6' maxlength='5' name='TOTIME' onFocus='promptEntry(pTime)' value=" + sURT + "></font></td>"
content += "</TR>"
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
content += "<TR>"
content += "<td align='left'><font class='SearchForm2'>Week Day:</font></td>"

content += "<TD align='left'><font class='select'>"
content += "  <select name='DAYOFWEEK' size='1' class='select'>"

if ((sUDOW == "ALL") || (sUDOW == "")) {
	content += "    <option selected value='ALL'>ALL</option>"
} else {
	content += "    <option value='ALL'>ALL</option>"
}
if (sUDOW == "SAT") {
	content += "    <option selected value='SAT'>SAT</option>"
} else {
	content += "    <option value='SAT'>SAT</option>"
}
if (sUDOW == "MON") {
	content += "    <option selected value='MON'>MON</option>"
} else {
	content += "    <option value='MON'>MON</option>"
}
if (sUDOW == "TUE") {
	content += "    <option selected value='TUE'>TUE</option>"
} else {
	content += "    <option value='TUE'>TUE</option>"
}
if (sUDOW == "WED") {
	content += "    <option selected value='WED'>WED</option>"
} else {
	content += "    <option value='WED'>WED</option>"
}
if (sUDOW == "THU") {
	content += "    <option selected value='THU'>THU</option>"
} else {
	content += "    <option value='THU'>THU</option>"
}
if (sUDOW == "FRI") {
	content += "    <option selected value='FRI'>FRI</option>"
} else {
	content += "    <option value='FRI'>FRI</option>"
}
if (sUDOW == "SUN") {
	content += "    <option selected value='SUN'>SUN</option>"
} else {
	content += "    <option value='SUN'>SUN</option>"
}
    
content += "</select>"
content += "</font></TD>"
content += "</TR>"
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
content += "<TR>"
content += "<td colspan='2' align='left'><font class='SearchForm2'><br>Crime Incident</font></td>"
content += "</TR>"

content += "<TR>"
content += "<td colspan='2' align='left'><font class='select'>"
content += "  <select name='INCIDENT' size='1' class='select'>"

if ((sUI == "ALL") || (sUI == ""))
	content += "    <option selected value='ALL'>ALL Incidents</option>"
else
	content += "    <option value='ALL'>ALL Incidents</option>"

if (sUI == "ALL22")
	content += "    <option selected value='ALL22'>All Auto Theft</option>"
else
	content += "    <option value='ALL22'>All Auto Theft</option>"

if (sUI == "ALL26")
	content += "    <option selected value='ALL26'>All Burglary</option>"
else
	content += "    <option value='ALL26'>All Burglary</option>"

if (sUI == "22")
	content += "    <option selected value='22'>Auto Theft (No-Type)</option>"
else
	content += "    <option value='22'>Auto Theft (No-Type)</option>"

if (sUI == "22A")
	content += "    <option selected value='22A'>Auto Theft Attempt</option>"
else
	content += "    <option value='22A'>Auto Theft Attempt</option>"

if (sUI == "22F")
	content += "    <option selected value='22F'>Auto Theft Foreign Stolen</option>"
else
	content += "    <option value='22F'>Auto Theft Foreign Stolen</option>"

if (sUI == "22S")
	content += "    <option selected value='22S'>Auto Theft Foreign Stolen</option>"
else
	content += "    <option value='22S'>Auto Theft Stolen</option>"

if (sUI == "26")
	content += "    <option selected value='26'>Burglary (No-Type)</option>"
else
	content += "    <option value='26'>Burglary (No-Type)</option>"

if (sUI == "26C")
	content += "    <option selected value='26C'>Burglary Commercial</option>"
else
	content += "    <option value='26C'>Burglary Commercial</option>"

if (sUI == "26R")
	content += "    <option selected value='26R'>Burglary Residencial</option>"
else
	content += "    <option value='26R'>Burglary Residencial</option>"

if (sUI == "26V")
	content += "    <option selected value='26V'>Burglary Vehicle</option>"
else
	content += "    <option value='26V'>Burglary Vehicle</option>"

if (sUI == "29")
	content += "    <option selected value='29'>Robbery</option>"
else
	content += "    <option value='29'>Robbery</option>"

content += "</select>"
content += "</font></TD>"
content += "</TR>"
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------

content += "<TR>"
//content += "<td colspan='2' align='center' ><br><a href='javascript:doAdvSearch();' onmouseover='swap(document.AdvSearchRefreshMap.name,\"RefreshMap_2\"); return true;' onmouseout='swap(document.AdvSearchRefreshMap.name,\"RefreshMap_1\")'><IMG SRC='images/RefreshMap_1.gif' name='AdvSearchRefreshMap'alt='Refresh Map' border='0' width='140' height='20'></a></td>"
content += "<td colspan='2' align='center' ><br><a href='javascript:doAdvSearch();'><IMG SRC='images/search_button.gif' alt='Search' border='0'></a></td>"
content += "</TR>"

content += "</TABLE>"
content += "</TD></TR>"
content += "  </TABLE>";

content += "</form>"

// ============== FORM Ending ======================================================
//==================================================================================

return content;
}

