﻿// ******************************************************
// *****************CALLBACK FUNCTIONS ******************
// ******************************************************



// -------------------------------------------------------
// CALLBACK: MAP COORDINATES

var mapminx = 0;
var mapminy = 0;
var mapmaxx = 0;
var mapmaxy = 0;
var mapscale = "";


//get map extent
function getMapExtent(){
    var message = "GetMapExtent";
    var context = "MapExtent";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true); 
}


//update map extent
function updateMapExtent(extentParams){
    var ext = extentParams.split(":");
    mapminx = parseFloat(ext[0]);
    mapminy = parseFloat(ext[1]);
    mapmaxx = parseFloat(ext[2]);
    mapmaxy = parseFloat(ext[3]);
    mapscale = ext[4];
    setStatusbarScale(mapscale);
}

function ZoomtoFullExtent()
{
    ZoomFullExtent();
    return false;
}
//update map coordinates
function updateMapCoordinates(x,y){
    if((mapminx != 0) && (mapminy != 0) && (mapmaxx != 0) && (mapmaxy != 0)){
        mapW = map.tileWidth;
        mapH = map.tileHeight;
        var box = calcElementPosition(map.divId);
        var pixelX = Math.abs(mapmaxx-mapminx) / mapW;
	    var deltaX = x - box.left;
	    curX = Math.round(mapminx + pixelX * deltaX);
	    var pixelY = Math.abs(mapmaxy-mapminy) / mapH;
	    var deltaY = y - box.top;
	    curY = Math.round(mapmaxy - pixelY * deltaY);
    	    
        coordsdiv = document.getElementById("Status_Coordinates");
        if(coordsdiv != null){
            coordsdiv.innerHTML = "X: "+ curX + ", Y: " + curY;
        }
    }
}


// -------------------------------------------------------
// CALLBACK: ZOOM ACTIVE LAYER, PREVIOUS EXT, NEXT EXT

//set active layer
function setActiveLayer(){
    var lyr = document.frmMain.ddlActiveLayer.options[document.frmMain.ddlActiveLayer.options.selectedIndex].value;
    var message = "SetActiveLayer:"+lyr;    
    var context = "SetActiveLayer";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true); 
}

//check active layer
function checkActiveLayer(nodeID){
    c = document.getElementById(nodeID + "_CheckBox");
    if(c != null){
        c.checked = true;
    }
    
}


//zoom to active layer
function zoomToActiveLayer(){
    var lyr = document.frmMain.ddlActiveLayer.options[document.frmMain.ddlActiveLayer.options.selectedIndex].value;
    var message = "ZoomToActiveLayer:"+lyr;    
    var context = "ZoomToActiveLayer";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true); 
}

//zoom previous extent
function zoomPreviousExtent(){
    var message = "ZoomPreviousExtent";    
    var context = "ZoomPreviousExtent";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true); 
}

//zoom next extent
function zoomNextExtent(){
    var message = "ZoomNextExtent";    
    var context = "ZoomNextExtent";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true); 
}


// -------------------------------------------------------
// CALLBACK: GRAPHICS

//map draw text
function MapDrawText(){
    map = Maps['Map1'];
    MapPoint(map.controlName, "MapDrawText", false);
    map.divObject.onmousedown = MapDrawTextClick;
    map.divObject.onmouseup = "";
}

// map draw text click
function MapDrawTextClick(e) {
    // get font size and color
    var text = document.frmMain.Panel_GraphicText_txtGraphicText.value;
    var size = document.frmMain.Panel_GraphicText_txtGraphicSize.value;
    var color = document.frmMain.Panel_GraphicText_ddlGraphicColor.options[document.frmMain.Panel_GraphicText_ddlGraphicColor.options.selectedIndex].value; 
     var s = parseInt(size);
    if((text == "") || (s < 5) || (s > 100)){
        alert("Please enter valid text between 5 points and 100 points.");
    }else{
	    //get click coordinates
        getXY(e);
	    var box = calcElementPosition(map.containerDivId);
	    zleft = mouseX - box.left;
	    ztop = mouseY - box.top;
	    var message = "MapDrawText:" + text + ":" + size + ":" + color + ":" + zleft + "&" + ztop;
        var context = "MapDrawText";
        WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true);     
	
	    map.mode = map.tempMode;
	    map.actionType = map.tempAction;
	    map.cursor = map.tempCursor;
	    return false;
	}
}


//map erase drawing
function MapEraseDrawing(){
    map = Maps['Map1'];
    MapDragRectangle(map.controlName, "MapEraseDrawing", false);
    map.divObject.onmouseup = MapEraseDrawingUp;

}

// map erase drawing up5
function MapEraseDrawingUp(e) {
    //get click coordinates
    getXY(e);
	var box = calcElementPosition(map.containerDivId);

//	zl = zleft - box.left;
//	zt = ztop - box.top;
//	zr = zright - box.left;
//	zb = zbottom - box.top;
	
	rl = x1 - box.left;
	rt = y1 - box.top;
	rr = x2 - box.left;
	rb = y2 - box.top;
	
	if(rr < rl){
	    zl = rr;
	    zr = rl;
	}else{
	    zl = rl;
	    zr = rr;
	}
	if(rb < rt){
	    zt = rb;
	    zb = rt;
	}else{
	    zt = rt;
	    zb = rb;
	}
	
	var message = "MapEraseDrawing:"+zl + "&" + zb + "&" + zr + "&" + zt;
    var context = "MapEraseDrawing";
	WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true);     
	
	map.mode = map.tempMode;
	map.actionType = map.tempAction;
	map.cursor = map.tempCursor;
	return false;
}


// -------------------------------------------------------
// CALLBACK: BUFFER

//do buffer
function doBuffer(){
    lyr = document.frmMain.Panel_Buffer_ddlTargetLayer.options[document.frmMain.Panel_Buffer_ddlTargetLayer.options.selectedIndex].value;
    dist = document.frmMain.Panel_Buffer_txtBufferDistance.value;
    if(dist == ""){
        alert("Please enter a valid buffer distance.");
    }else{
        d = parseInt(dist);
        var message = "BufferFeatures:" + lyr + ":" + d;
        var context = "BufferFeatures";
        setTab("Search_Results");
        WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true);
        var pResults = document.getElementById('Panel_Results');
	    if(pResults != null){
	        pResults.innerHTML = "<img src='images/ajax_loading.gif' align='middle'/> Getting Information. . .";
	    }
	    setTab("Search_Results");
    }     
}


// -------------------------------------------------------
// CALLBACK: PRINT

//get printable map
function getPrintableMap(){
    var paper = document.frmMain.ddlPaper.options[document.frmMain.ddlPaper.options.selectedIndex].value;
    if(document.frmMain.rblOrientation_0.checked){
        orient = "L";
    }else{
        orient = "P";
    }
    var message = "GetPrintableMap" + ":" + paper + ":" + orient;
    var context = "PrintableMap";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true); 
}


//show printable map
function showPrintableMap(){
    var paper = document.frmMain.ddlPaper.options[document.frmMain.ddlPaper.options.selectedIndex].value;
    if(document.frmMain.rblOrientation_0.checked){
        orient = "L";
    }else{
        orient = "P";
    }
    var title = document.frmMain.txtTitle.value;
    var subtitle = document.frmMain.txtSubtitle.value;
    var results = "";
    if(document.frmMain.chkResults.checked){
        results = "Y";
    }
    r = document.getElementById("Panel_Results");
    if(r != null){
        content = r.innerHTML;
    }
    theURL = "Print.aspx?title=" + escape(title) + "&subtitle=" + escape(subtitle) + "&paper=" + paper + "&orient=" + orient+"&results="+results;
    printWin = window.open(theURL, "Print", "width=800,height=600,toolbar=0,menubar=1,scrollbars=1,resizable=1,left=0,top=0");
}



// -------------------------------------------------------
// CALLBACK: IDENTIFY

// map identify
//function MapIdentify() {
//    //map = Maps['Map1'];
//    var map = $find('Map1');
//    MapPoint(map.controlName, "MapIdentify", false);
//    map.divObject.onmousedown = MapIdentifyClick;
//    map.divObject.onmouseup = "";
//}
function MapIdentify(divid) {
    var map = $find('Map1');    
    MapPoint(divid,"MapIdentify",false,"");
    //map.divObject.onmousedown = MapIdentifyClick;    
    map.add_mouseDown(MapIdentifyClick);	
    //map.add_mouseUp(MouseUpFunction);
    //map.divObject.onmouseup = "";
   }

// map identify all
function MapIdentifyAll() {
    map = Maps['Map1'];
    MapPoint(map.controlName, "MapIdentifyAll", false);
    map.divObject.onmousedown = MapIdentifyClick;
    map.divObject.onmouseup = "";
}


// map select rectangle
function MapSelectRectangle() {
    map = Maps['Map1'];
    MapDragRectangle(map.controlName, "MapSelectRectangle", false);
    map.divObject.onmouseup = MapSelectRectangleUp;
}


// Event handler for Identify
//Commented out the function
function MapIdentifyClick(e) {        
    //get click coordinates
    getXY(e);
	var box = calcElementPosition('MapControlDiv_Map1');
	zleft = mouseX - box.left;
	ztop = mouseY - box.top;
	var searchtype = "SelectFeatures:Identify";
	var searchvalue = zleft + "&" + ztop;
	var map = $find('Map1');
	map.mode = map.tempMode;
	map.actionType = map.tempAction;
	map.cursor = map.tempCursor;
	identiyPostback(searchvalue);
	return false;
}
//Call Server side function

function MapIdentifyNew(searchvalue){
 //  var txtresult = $get("X_CLICK");
    var txtresult = $get("X_CLICK");
    PageMethods.LocateIdentify(searchvalue,OnCallSumComplete,OnCallSumError,txtresult);
    return true;
}

function OnCallSumComplete(result,txtresult,methodName)

{ var txtresult = $get("X_CLICK");
  var txtresult2 = $get("XCLICK");
   //Show the result in txtresult
   txtresult.value = result;
   txtresult2.value = result;
}
function OnCallSumError(error,userContext,methodName)
{
 if(error !== null) 
{
   alert(error.get_message());
}
}

// Event handler for Select
function MapSelectRectangleUp(e) {
    // get active layer
    var lyr = document.frmMain.ddlActiveLayer.options[document.frmMain.ddlActiveLayer.options.selectedIndex].value;
        
    //get click coordinates
    getXY(e);
	var box = calcElementPosition(map.containerDivId);
//	zl = zleft - box.left;
//	zt = ztop - box.top;
//	zr = zright - box.left;
//	zb = zbottom - box.top;
	
	rl = x1 - box.left;
	rt = y1 - box.top;
	rr = x2 - box.left;
	rb = y2 - box.top;
	
	if(rr < rl){
	    zl = rr;
	    zr = rl;
	}else{
	    zl = rl;
	    zr = rr;
	}
	if(rb < rt){
	    zt = rb;
	    zb = rt;
	}else{
	    zt = rt;
	    zb = rb;
	}
	
	var searchtype = "SelectFeatures:Select Rectangle:" + lyr;
    var searchvalue = zl + "&" + zb + "&" + zr + "&" + zt;
	
	map.mode = map.tempMode;
	map.actionType = map.tempAction;
	map.cursor = map.tempCursor;
	SelectRectangle(searchvalue);
	return false;
}


//--------------------------------------------------------
// CALLBACK: VALIDATION

// validate numeric
function validateNumeric(controlname){
	txtbox = document.getElementById(controlname);
	if(txtbox != null){
		sCheck = "0123456789";
		sVal = txtbox.value;
		sValOut = sVal;
		for(i=0;i<sVal.length;i++){
			sChar = sVal.charAt(i);
			if(sCheck.indexOf(sChar) < 0){
				sValOut = sValOut.replace(sChar, "");
			}
		}
		txtbox.value = sValOut;
	}
}

// -------------------------------------------------------
// CALLBACK: SEARCH

//do search
function doSearch(searchtype, searchvalue){
    var message = searchtype + ":" + searchvalue;
    var context = searchtype;
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true);  
    
}

//show search results
function showSearchResults(results){
    var pResults = document.getElementById('Panel_Results');
	if(pResults != null){
	 pResults.innerHTML = results;
	}
}

//clear search results
function clearSearchResults(){
    var message = "ClearSearchResults";
    var context = "ClearSearchResults";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true);  
    var pResults = document.getElementById('Panel_Results');
	if(pResults != null){
	 pResults.innerHTML = "";
	}
}

//page search results
function pageSearchResults(startrec){
    var message = "PageSearchResults:" + startrec;
    var context = "PageSearchResults";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true);  
    var pResults = document.getElementById('Panel_Results');
	if(pResults != null){
	 pResults.innerHTML = "<img src='images/ajax_loading.gif' align='middle'/> Getting Information. . .";
	}
}

//sort search results
function sortSearchResults(sortfield){
    var message = "SortSearchResults:" + sortfield;
    var context = "SortSearchResults";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true);  
    var pResults = document.getElementById('Panel_Results');
	if(pResults != null){
	 pResults.innerHTML = "<img src='images/ajax_loading.gif' align='middle'/> Getting Information. . .";
	}
}

//export search results
function exportSearchResults(type){
    var theURL = "ExportResults.aspx?type=" + type;
     exportWin = window.open(theURL, "Export", "width=800,height=600,toolbar=0,menubar=1,scrollbars=1,resizable=1,left=0,top=0"); 
}

//zoom to extent
function zoomToExtent(x1,y1,x2,y2){
    var message = "ZoomToExtent:" + x1 + ":" + y1 + ":" + x2 + ":" + y2;
    var context = "ZoomToExtent";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true);  
}


//==========================================

//do search addr
function doSearchAddr(){
    num = document.frmMain.txtNumAddr.value;
    dir = document.frmMain.ddlDirAddr.options[document.frmMain.ddlDirAddr.options.selectedIndex].value;
    name = document.frmMain.txtNameAddr.value;
    type = document.frmMain.ddlTypeAddr.options[document.frmMain.ddlTypeAddr.options.selectedIndex].value;
    suff = document.frmMain.ddlSuffAddr.options[document.frmMain.ddlSuffAddr.options.selectedIndex].value;
    zip = document.frmMain.txtZipAddr.value;
    if((num == "") || (name == "")){
        alert("Please enter a valid address.");
    }else{
        var searchtype = "Locate:Address:Streets";
        var searchvalue = num + " " + dir + " " + name + " " + type + " " + suff + "&" + zip;
        doSearch(searchtype,searchvalue);
    }
}

//do search int
function doSearchInt(){
    dir1 = document.frmMain.ddlDirInt1.options[document.frmMain.ddlDirInt1.options.selectedIndex].value;
    name1 = document.frmMain.txtNameInt1.value;
    type1 = document.frmMain.ddlTypeInt1.options[document.frmMain.ddlTypeInt1.options.selectedIndex].value;
    suff1 = document.frmMain.ddlSuffInt1.options[document.frmMain.ddlSuffInt1.options.selectedIndex].value;
    dir2 = document.frmMain.ddlDirInt2.options[document.frmMain.ddlDirInt2.options.selectedIndex].value;
    name2 = document.frmMain.txtNameInt2.value;
    type2 = document.frmMain.ddlTypeInt2.options[document.frmMain.ddlTypeInt2.options.selectedIndex].value;
    suff2 = document.frmMain.ddlSuffInt2.options[document.frmMain.ddlSuffInt2.options.selectedIndex].value;
    zip = document.frmMain.txtZipInt.value;
    if((name1 == "") || (name2 == "")){
        alert("Please enter a valid intersection.");
    }else{
        var searchtype = "Locate:Intersection:Streets";
        var searchvalue = dir1 + " " + name1 + " " + type1 + " " + suff1 + "@" + dir2 + " " + name2 + " " + type2 + " " + suff2 + "&" + zip;
        //alert(searchvalue);
        doSearch(searchtype,searchvalue);
    }
}

//do search street
function doSearchStreet(){
    dir = document.frmMain.ddlDirSt.options[document.frmMain.ddlDirSt.options.selectedIndex].value;
    name = document.frmMain.txtNameSt.value;
    type = document.frmMain.ddlTypeSt.options[document.frmMain.ddlTypeSt.options.selectedIndex].value;
    suff = document.frmMain.ddlSuffSt.options[document.frmMain.ddlSuffSt.options.selectedIndex].value;
    if(name == ""){
        alert("Please enter a valid street.");
    }else{
        var searchtype = "SearchFeatures:Street:Streets";
        var searchvalue = dir + "&" + name + "&" + type + "&" + suff;
        doSearch(searchtype,searchvalue);
    } 
}

//do search owner
function doSearchOwner(){
    lname = document.frmMain.txtLastName.value;
    fname = document.frmMain.txtFirstName.value;
    if(lname == ""){
        alert("Please enter a valid Last Name.");
    }else{
        var searchtype = "SearchFeatures:Owner:Parcels";
        var searchvalue = fname + "&" + lname;
        doSearch(searchtype,searchvalue);
    }   
}


//do search folio
function doSearchFolio(){
    folio = document.frmMain.txtFolio.value;
    if(folio == ""){
        alert("Please enter a valid Folio Number.");
    }else{
        var searchtype = "SearchFeatures:Folio:Parcels";
        var searchvalue = folio;
        doSearch(searchtype,searchvalue);
    }
}


//do search trs
function doSearchTRS(){
    trs = document.frmMain.txtTRS.value;
    if(trs == ""){
        alert("Please enter a valid Township-Range-Section (TTRRSS).");
    }else{
        var searchtype = "SearchFeatures:Township Range Section:Township Range Section";
        var searchvalue = trs;
        doSearch(searchtype,searchvalue);
    }   
}


//do search mun
function doSearchMun(){
    mun = document.frmMain.ddlMun.options[document.frmMain.ddlMun.options.selectedIndex].value;
    if(mun == ""){
        alert("Please select a valid Municipality.");
    }else{
        var searchtype = "SearchFeatures:Municipality:Municipalities";
        var searchvalue = mun;
        doSearch(searchtype,searchvalue);
    }  
}


//do search comm
function doSearchComm(){
    comm = document.frmMain.ddlComm.options[document.frmMain.ddlComm.options.selectedIndex].value;
    if(comm == ""){
        alert("Please select a valid Commission District.");
    }else{
        var searchtype = "SearchFeatures:Commission District:Commission Districts";
        var searchvalue = comm;
        doSearch(searchtype,searchvalue);
    }
}


//do search permit
function doSearchPermit(){
    permit = document.frmMain.txtPermit.value;
    if(permit == ""){
        alert("Please enter a valid Permit Number.");
    }else{
        var searchtype = "SearchFeatures:Permit:Building Permits";
        var searchvalue = permit;
        doSearch(searchtype,searchvalue);
    }
}

//Added for Selection tool
function performPostBack(aTargetElement, aArg)
{
    var postBack = new Sys.WebForms.PostBackAction();
    postBack.set_target(aTargetElement);
    postBack.set_eventArgument(aArg);
    postBack.performAction();
}
//overview map
function OverviewMap() {
    var ovMapPanel = document.getElementById("Panel_Overview");
    if (ovMapPanel!=null) { 
           toggleFloatingPanelVisibility('Panel_Overview');
    } else 
        alert("Overview is not available"); 
   
}

//function OverviewMap() {
//    var mag = $get("Panel_Overview");
//    if (mag != null) {
//        toggleFloatingPanelVisibility('Panel_Overview');
//    } else
//        alert("Magnifier is not available");

//}

//Magnifier tool
function toggleMagnifier() {
    var mag = document.getElementById("Magnifier1");
    if (mag!=null) { 
            toggleFloatingPanelVisibility('Magnifier1'); 
    } else 
        alert("Magnifier is not available"); 
    
}
//Zoom to previous extent
function zoomPreviousMapExtent()
{
  ZoomToPreviousExtent();
  return false;
}

//Print map
function PrintMap() {
printPostback();
return false;
//    var ovMapPanel = document.getElementById("Panel_Print");
//    if (ovMapPanel!=null) { 
//           showFloatingPanel('Panel_Print');
//    } else 
//        alert("Print Map function is not available"); 
   
}
//Update print map URL
function UpdateMapURL(url)
{
   window.open("Print.aspx?URL=" + url );
   return false;
}

// Event handler for Identify
//Commented out the function
function MapPrintClick() {        
    printPostback();
	return false;
}

//show help
function showHelp(helpTopic){
    theURL = "helpneighborhood.aspx";
    helpWin = window.open(theURL, "Help", "width=500,height=400,toolbar=0,menubar=1,scrollbars=1,resizable=1,left=300,top=0");
}

//MishraM - modified for printmap 
function printMap()
{
  printPostback();
  return false;
}

function showCIISPrintMapReport()
{
     url = "Print.aspx?Application=CIIS" 
   window.open(url,"Print","");
}

//Google street view

function initializeGoogleMap() {
    //map = Maps['Map1'];
    var map = $find('Map1');
    MapPoint(map.controlName, "Google", false);
    map.divObject.onmousedown = MapGoogleClick;
    map.divObject.onmouseup = "";
}


// Event handler for Identify
//Commented out the function
function MapGoogleClick(e) {        
    //get click coordinates
    var map = $find('Map1');
    getXY(e);
	var box = calcElementPosition(map.containerDivId);
	zleft = mouseX - box.left;
	ztop = mouseY - box.top;
	var searchvalue = zleft + "&" + ztop;
	map.mode = map.tempMode;
	map.actionType = map.tempAction;
	map.cursor = map.tempCursor;
	GooglePostback(searchvalue);
	return false;
}
function initializelatlon(lat,lon) 
{
//alert(lat,lon);
   var fenwayPark = new GLatLng(lat,lon);
   var fenwayPOV = {yaw:370.64659986187695,pitch:-20};      
   var panoClient = new GStreetviewClient();  
   panoClient.getNearestPanoramaLatLng(fenwayPark, function(point2) {
       if (point2) 
       {
              var mag = document.getElementById("GoogleMapFloatingPanel");
              if (mag!=null) 
              { 
                 showFloatingPanel('GoogleMapFloatingPanel'); 
              } else 
              {
                  alert("google Map is not available"); 
              }
               new GStreetviewPanorama(document.getElementById("pano"),{latlng:point2,yaw:bearing(point2,fenwayPark)});
        } else 
        {
                 alert("Streetview returns no location")
        }
  });          
}

 var degreesPerRadian = 180.0 / Math.PI;
 var radiansPerDegree = Math.PI / 180.0;
 // Returns the bearing in degrees between two points.
 // North = 0, East = 90, South = 180, West = 270.
function bearing( from, to ) {
 // See T. Vincenty, Survey Review, 23, No 176, p 88-93,1975.
 // Convert to radians.
        var lat1 = from.latRadians();
        var lon1 = from.lngRadians();
        var lat2 = to.latRadians();
        var lon2 = to.lngRadians();
        // Compute the angle.
        var angle = - Math.atan2( Math.sin( lon1 - lon2 ) * Math.cos( lat2 ), Math.cos( lat1 ) * Math.sin( lat2 ) - Math.sin( lat1 ) * Math.cos( lat2 ) * Math.cos( lon1 - lon2 ) );
        if ( angle < 0.0 ) angle  += Math.PI * 2.0;
        // And convert result to degrees.
        angle = angle * degreesPerRadian;
        angle = angle.toFixed(1);
        return angle;
    }
    
    
    function extResponse()
      {  
         if (document.getElementById("panel_layers_out") != null )
        {   
          var panel = document.getElementById("panel_layers_out"); 
          panel.style.display = "inline"; 
        }
         
      }
      
      function extResponseHidden()
      {     
         if (document.getElementById("panel_layers_out") != null )
        {   
          var panel = document.getElementById("panel_layers_out"); 
          panel.style.display = "none"; 
        }
      }   