
//***********************************************	
//***************** FUNCTIONS *******************
//***********************************************

//*******************START UP FUNCTIONS *********

// start up
function startUp() {


	setTool(document.frmMain.tool.value);
	var sPanel = document.frmMain.panel.value;
	var sSearchPanel = document.frmMain.searchpanel.value;
	
	
	
	if(sPanel != "SEARCH"){
		document.frmMain.searchpanel.value = "";
		showPanel(sPanel);
	}else{
		document.frmMain.panel.value = "";
		if((sSearchPanel == "CONDO") || (sSearchPanel == "SUBDIVISION")){
			setSearchPanel(sSearchPanel);
		}else{
			document.frmMain.searchpanel.value = "";
		}
	}
	setZoomBoxSettings();
	//populateRange();
	if(document.frmMain.showfloat.value == "1"){
		showOverview();
	}
	if(document.frmMain.helpfile.value != ""){
		displayHelp();
	}
	
	
}


//*******************KEY DOWN FUNCTIONS *******

//print key down
function printKeyDown(event){
	if(event.keyCode == 13){
		window.event.keyCode = 0;
		doLayout();
	}
}

//search key down
function searchKeyDown(event){
	if(event.keyCode == 13){
		window.event.keyCode = 0;
		searchPanel =  document.frmMain.searchpanelvalue;
		doSearch(searchPanel);
	}
}

//*******************PANEL FUNCTIONS *********

//toggle panel
function togglePanel(sPanel){
	if(sPanel == document.frmMain.panel.value){
		hidePanel();
		document.frmMain.panel.value = "";
	} else {
		showPanel(sPanel);
	}
}

//hide panel
function hidePanel(){
	sOldPanel = document.frmMain.panel.value;
	if(sOldPanel != ""){
		theOldImg = eval("document.T_"+sOldPanel);
		theOldImg.src = "images/t_select_off.gif";
		hideLayer(sOldPanel);
		if(sOldPanel == "SEARCH"){
			searchPanel =  document.frmMain.searchpanel.value;
			if(searchPanel != ""){
				hideLayer("SEARCH_"+searchPanel);
			}
		}
	}
}

//show panel
function showPanel(sPanel){
	if(sPanel != ""){
		hidePanel();
		theImg = eval("document.T_"+sPanel);
		theImg.src = "images/t_select_on.gif";
		showLayer(sPanel);
		document.frmMain.panel.value = sPanel;
		if(sPanel == "SEARCH"){
			searchPanel =  document.frmMain.searchpanel.value;
			if(searchPanel != ""){
				setSearchPanel(searchPanel);
				//showLayer("SEARCH_"+searchPanel);
			}
		}
	}
}

//toggleSearchPanel
function toggleSearchPanel(sSearchPanel){

	if(sSearchPanel == document.frmMain.searchpanel.value){
		sSearchPanel = "";
		setSearchPanel(sSearchPanel);
	}else{
		if((sSearchPanel == "CONDO")||(sSearchPanel == "SUBDIVISION")){
			document.frmMain.searchpanel.value = sSearchPanel;
			setCmd("POPULATESEARCH");
		}else{
			setSearchPanel(sSearchPanel);
			if(sSearchPanel == "OWNER"){
				document.frmMain.txtFirstName.focus();
			}
			if(sSearchPanel == "ADDRESS"){
				document.frmMain.txtStNum.focus();
			}
			if(sSearchPanel == "FOLIO"){
				document.frmMain.txtFolio.focus();
			}
			if(sSearchPanel == "TRS"){
				document.frmMain.ddlTownship.focus();
			}
		}
	}
}

//set search panel
function setSearchPanel(sSearchPanel){
	document.frmMain.searchpanel.value = sSearchPanel;
	showSearchPanel();
}

//show search panel
function showSearchPanel(){
	hideLayer("SEARCH_OWNER");
	theOldImg = eval("document.T_SEARCH_OWNER");
	theOldImg.src = "images/t_select_off.gif";

	hideLayer("SEARCH_ADDRESS");
	theOldImg = eval("document.T_SEARCH_ADDRESS");
	theOldImg.src = "images/t_select_off.gif";
	
	hideLayer("SEARCH_FOLIO");
	theOldImg = eval("document.T_SEARCH_FOLIO");
	theOldImg.src = "images/t_select_off.gif";
	
	hideLayer("SEARCH_SUBDIVISION");
	theOldImg = eval("document.T_SEARCH_SUBDIVISION");
	theOldImg.src = "images/t_select_off.gif";
	
	hideLayer("SEARCH_CONDO");
	theOldImg = eval("document.T_SEARCH_CONDO");
	theOldImg.src = "images/t_select_off.gif";
	
	hideLayer("SEARCH_TRS");
	theOldImg = eval("document.T_SEARCH_TRS");
	theOldImg.src = "images/t_select_off.gif";
	
	searchPanel =  document.frmMain.searchpanel.value;
	if(searchPanel != ""){
		showLayer("SEARCH_"+searchPanel);
		theImg = eval("document.T_SEARCH_"+searchPanel);
		theImg.src = "images/t_select_on.gif";
	}
}


//************** SEARCH FUNCTIONS *********

//do search
function doSearch(sType){
	
	theForm = document.frmMain;
	sType = theForm.searchpanel.value;
	theForm.selid.value = "";
	theForm.subdivision.value = "";
	theForm.condo.value = "";
	theForm.trs.value = "";
	theForm.queryexpr.value = "";
	theForm.querysort.value = "";
	theForm.querystart.value = "0";
	
	switch (sType){
	
		case "OWNER" :
			sLastName = theForm.txtLastName.value;
			sFirstName = theForm.txtFirstName.value;
			if(sLastName == ""){
				alert("Please enter Owner's Last Name.");
			}else{
				//sOwner = sLastName.toUpperCase();
				
				Criteria2str = "LEFT(Name1,len('" + sLastName.toUpperCase() + "')) = '" + sLastName.toUpperCase() + "'";
				
				if(sFirstName != ""){
					//sOwner = sOwner + ", " + sFirstName.toUpperCase();
					
				  Criteria2str = "(" + Criteria2str + " and substring(Name1,PATINDEX ('%,%',Name1)+2, LEN(Name1)-(PATINDEX ('%,%',Name1)+2)) LIKE '%" + sFirstName.toUpperCase() + "%')";

					
					
				}
				

				
				theForm.queryexpr.value = Criteria2str;
				//theForm.queryexpr.value = "NAME1 LIKE '" + sOwner + "%'";
				theForm.querysort.value = "NAME1"
				setCmd("SEARCH");
			}
		break
		
		case "ADDRESS" :
			sNum = theForm.txtStNum.value;
			sName = theForm.txtStName.value;
			sDirection = theForm.ddlStDirection.value;
			sType = theForm.ddlStType.value; 
			sExpr = "";
			if(sName == ""){
				alert("Please enter a Street Name.");
			}else{
			
			
				sExpr = "((ltrim(rtrim(ISNULL(PLSTNA_T,'')))) Like '" + sName.toUpperCase() + "%')"
				//sExpr = "((ltrim(rtrim(ISNULL(PLSTNA_T,''))) + ' ' + ltrim(rtrim(ISNULL(PLSTTY_T,''))) + ' ' + ltrim(rtrim(ISNULL(PLORDN_T,'')))) Like '" + sName.toUpperCase() + "%')"
				//sExpr = "PLSTNA_T LIKE '" + sName.toUpperCase() + "%'";
				
				if(sNum != ""){
					sExpr = sExpr + " AND ltrim(rtrim(ISNULL(PLNUM_T,''))) Like '" + sNum + "%'";
				}
				
				
				if(sDirection != ""){
					sExpr = sExpr + " AND ltrim(rtrim(ISNULL(PLORDN_T,''))) = '" + sDirection.toUpperCase() + "'";
				}
				
				if(sType != ""){
					sExpr = sExpr + " AND ltrim(rtrim(ISNULL(PLSTTY_T,''))) = '" + sType.toUpperCase() + "'";
				}
				
				
				
				theForm.queryexpr.value = sExpr;
				theForm.querysort.value = "PLSTNA_T,PLSTTY_T,PLORDN_T,PLNUM_T"
				setCmd("SEARCH");
			}
		break
		
		case "FOLIO" :
			sFolio = theForm.txtFolio.value;
			if(sFolio == ""){
				alert("Please enter a Folio Number.");
			}else{
				theForm.queryexpr.value = "FLN_TXT LIKE '%" + sFolio.toUpperCase() + "%'";
				theForm.querysort.value = "FLN_NUM"
				setCmd("SEARCH");
			}
		break
				
		case "SUBDIVISION" :
			sSub = theForm.ddlSubdivision.options[theForm.ddlSubdivision.options.selectedIndex].value;
			if(sSub == ""){
				alert("Please specify a Subdivision name.");
			}else{
				theForm.subdivision.value = sSub;
				toggleSearchPanel('SUBDIVISION');
				setCmd("ZOOMSUBDIVISION");
			}
		break
		
		case "CONDO" :
			sCondo = theForm.ddlCondo.options[theForm.ddlCondo.options.selectedIndex].value;
			if(sCondo == ""){
				alert("Please select a condominium.");
			}else{
				
				theForm.condo.value = sCondo;
				toggleSearchPanel('CONDO');
				setCmd("ZOOMCONDO");
				
			}
		break
		
		case "TRS" :
			sT = theForm.ddlTownship.options[theForm.ddlTownship.options.selectedIndex].value;
			sR = theForm.ddlRange.options[theForm.ddlRange.options.selectedIndex].value;
			sS = theForm.ddlSection.options[theForm.ddlSection.options.selectedIndex].value;
			if(sS.length == 1){
				sS = "0"+sS;
			}
			if(sT+sR+sS.length == 0){
				alert("Please specify a Township-Range-Section.");
			} else {
				theForm.trs.value = sT + sR + sS;
				setCmd("ZOOMTRS");
			}		
		break
	
	}
	
}

// show parcel details
function showParcelDetails(sFolio){
	var sFolioLen = sFolio.length;
	if(sFolioLen < 11){
		for(i=1; i<12-sFolioLen; i++){
			sFolio = "0" + sFolio;
		}
	}
	theURL = "http://www.collierappraiser.com/webmap/Getdata.asp?Map=No&FolioNum=" + sFolio  ;
	if (typeof parcelWin == 'undefined'){
		parcelWin = window.open(theURL, "Details", "width=1000,height=600,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=30,top=0");
		parcelWin.opener = self;
	} else {
		if (parcelWin.closed){
			parcelWin = window.open(theURL, "Details", "width=1000,height=600,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=30,top=0");
			parcelWin.opener = self;
		} else {
			parcelWin = window.open(theURL, "Details", "width=1000,height=600,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=30,top=0");			
			parcelWin.focus();
		}
	}
}



var CondoWin = '';
function showCondoSales(sCondo,sSales,sCondoName) {
	theURL = "CondoSales.asp?CONDO=" + sCondo + "&SALES=" + sSales + "&CONDONAME=" + sCondoName;
	if (!CondoWin.closed && CondoWin.location) {
		CondoWin.location.href = theURL ;
	}
	else {
		CondoWin = window.open(theURL, "CondoSales", "width=250,height=400,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=30,top=0");
		if (!CondoWin.opener) CondoWin.opener = self;
	}
	if (window.focus) {CondoWin.focus()}
	return false;
}




//zoom parcel
function zoomParcel(sFolio){
	theForm = document.frmMain;
	theForm.selid.value = sFolio;
	setCmd("ZOOMPARCEL");
	showParcelDetails(sFolio);
}

//zoom selected
function zoomSelected(){
	theForm = document.frmMain;
	id = theForm.selid.value;
	if(id.length == 0){
		alert("Please select a parcel before using the Zoom Selected button.");
	} else {
		setCmd("ZOOMSELECTED");
	}	
}

//show more
function showMore(startrec){
	theForm = document.frmMain;
	theForm.querystart.value = startrec;
	setCmd("SEARCH");
	
}


//populate range
function populateRange(){
	var selTRS = document.frmMain.trs.value;
	var selRange = "";
	//if(selTRS.length >= 4){
	//	selRange = selTRS.substring(2,4);
	//}
	document.frmMain.ddlRange.options.length = 0;
	srcLen = document.frmMain.ddlRange.options.length;
	document.frmMain.ddlRange.options[srcLen]=new Option("Select",srcLen+1);
	document.frmMain.ddlRange.options[srcLen].value="";
	var min = 25;
	var max = 34;
	twn = document.frmMain.ddlTownship.options[document.frmMain.ddlTownship.selectedIndex].value;
	if(twn == "46"){
		min = 28;
		max = 30;
	}
	if(twn == "47"){
		min = 27;
		max = 30;
	}
	if(twn == "48"){
		max = 30;
	}
	if(twn == "52"){
		min = 26;
	}
	if(twn == "53"){
		min = 26;
	}
	for(i=min; i<max+1; i++){
		srcLen = document.frmMain.ddlRange.options.length;
		document.frmMain.ddlRange.options[srcLen]=new Option(i,srcLen+1);
		document.frmMain.ddlRange.options[srcLen].value=i;
		if(i == parseInt(selRange)){
			document.frmMain.ddlRange.options.selectedIndex=srcLen;
		}
	}
}

//*******************TOOL FUNCTIONS *********

//set tool
function setTool(sTool){
	//MEASURE
	if(sTool == "MEASURE"){
		showLayer("PanelMeasure");
	}else{
		hideLayer("PanelMeasure");
	}
	resetMeasureTool();
	setToolImage(sTool);
	document.frmMain.tool.value = sTool;
}

//set tool image
function setToolImage(sTool){
	sOldTool = document.frmMain.tool.value;
	theOldImg = eval("document.B_"+sOldTool);
	theOldImg.src = "images/b_"+sOldTool+"_off.gif";
	theNewImg = eval("document.B_"+sTool);
	theNewImg.src = "images/b_"+sTool+"_on.gif";
}

//set cmd
function setCmd(sCmd){
	document.frmMain.cmd.value = sCmd;
	//document.getElementById("imgMap").click();
	document.frmMain.submit();
	showLayer("Splash");
}

//****************** PRINT FUNCTIONS ***********

// do layout
function doLayout(){
	theForm = document.frmMain;
	title = escape(theForm.txtTitle.value);
	paper = theForm.ddlPaper.options[theForm.ddlPaper.options.selectedIndex].value;
	orient = theForm.ddlOrientation.options[theForm.ddlOrientation.options.selectedIndex].value;
	selid = theForm.selid.value;
	pid = theForm.pid.value;	
	subdivision = theForm.subdivision.value;
	trs = theForm.trs.value;
	minX = document.frmMain.minX.value;
	minY = document.frmMain.minY.value;
	maxX = document.frmMain.maxX.value;
	maxY = document.frmMain.maxY.value;
	layerlist = buildLayerList();
	aerials = theForm.ddlAerials.options[theForm.ddlAerials.options.selectedIndex].value;
	sales = theForm.ddlSales.options[theForm.ddlSales.options.selectedIndex].value;
	theURL = "mapprint.aspx?title="+title+"&orient="+orient+"&paper="+paper;
	theURL = theURL + "&minX="+minX+"&minY="+minY+"&maxX="+maxX+"&maxY="+maxY+"&layerlist="+layerlist+"&aerials="+aerials+"&sales="+sales;
	theURL = theURL + "&selid="+selid+"&pid="+pid+"&subdivision="+subdivision+"&trs="+trs;
	printWin = window.open(theURL, "Print", "width=900,height=620,toolbar=0,menubar=1,scrollbars=1,resizable=1,left=20,top=0");
	theForm.txtTitle.value = "";
}

//build layer list
function buildLayerList(){
	var i = 0;
	var endoflist = 0;
	var layerlist = "";
	do{
		theChkBox = eval("document.frmMain.chkLayers_"+i);
		if(theChkBox != null){
			if(theChkBox.checked){
				layerlist = layerlist + "1";
			} else {
				layerlist = layerlist + "0";
			}
		} else {
			endoflist = 1;
		}
		i = i + 1;
	}
	while(endoflist == 0)
	return layerlist;
}


//****************** OVERVIEW WIN FUNCTIONS ***********

//toggle overview
function toggleOverview(){
	if(document.frmMain.showfloat.value == "1"){
		closeOverview();
		document.frmMain.showfloat.value = "";
	}else{
		showOverview();
		document.frmMain.showfloat.value = "1";
	}
}

//close overview
function closeOverview(){
	hideLayer("Overview");
	hideLayer("OverviewExt");
}

//show panel float
function showOverview(){
	showLayer("Overview");
	showLayer("OverviewExt");
}


//****************** HELP FUNCTIONS ************


//show intro
function showIntro(){
	introURL = "intro.htm";
	if (typeof introWin == 'undefined'){
		introWin = window.open(introURL, "Introduction", "width=500,height=400,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=300,top=0");
		introWin.opener = self;
	} else {
		if (introWin.closed){
			introWin = window.open(introURL, "Introduction", "width=500,height=400,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=300,top=0");
			introWin.opener = self;
		} else {
			introWin = window.open(introURL, "introduction", "width=500,height=400,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=300,top=0");			
			introWin.focus();
		}
	}
}

//show help
function showHelp(sFile){
	document.frmMain.helpfile.value = sFile;
	displayHelp();
}

//hide help
function hideHelp(){
	document.frmMain.helpfile.value = "";
	hideLayer("SideHelp");
}

//display help
function displayHelp(){
	sFile = document.frmMain.helpfile.value;
	document.getElementById("SideHelpFrame").src = sFile;
	showLayer("SideHelp");
}


