﻿// JScript File

function DoSearchAddress()
{
    var housenum = document.getElementById("AddressNumber");
    var street = document.getElementById("AddressStreet");
    
    if(housenum.value == "")
    {
        alert("Need to put in a house number.");
    }
    else if(street.value == "")
    {
        alert("Need to put in a street.");
    }
    else
    {
        setTimeout("MyCallback('EventArg=addresssearch&housenum=" + housenum.value + "&street=" + street.value + "');", 50)
    }
}

function DisplaySearchResults(searchresults)
{
    var display = "";
    if(searchresults != "")
    {
        display += "<table>\n";
        
        //Need to parse out the string here, getting x, y, match_addr
        var resultsarray = searchresults.split(",");
        var index = 0;
        while(index < resultsarray.length)
        {
            //Getting x variable here
            var x = resultsarray[index];
            //Getting y variable here
            index += 1;
            var y = resultsarray[index];
            //Getting the match_addr here
            index += 1;
            var addr = resultsarray[index];
            
            //Now we are ready to add to the address and zoom to info
            display += '<tr style="background-color: #CCCCCC; font-size: small; cursor: pointer;" onMouseOver="this.style.backgroundColor=\'#FFF000\';" onMouseOut="this.style.backgroundColor=\'#CCCCCC\';" onClick="ZoomToAddress(' + x + ',' + y + ');">';
            display += '<td>' + addr + '</td></tr>\n';
            
            index += 1;
        }
        
        display += "</table>";
    }
    else
    {
        display = "No records found.";
    }
    
    var searchresulttable = document.getElementById("AddressSearchResultPanel_Contents");
    //alert(searchresulttable.innerHTML);
    if(searchresulttable) 
    {
        searchresulttable.innerHTML = display;
    }
    
    var addresssearchresults = document.getElementById("AddressSearchResultPanel");
    if(addresssearchresults) 
    {
        showFloatingPanel("AddressSearchResultPanel");
    }
}

function CloseSearchResults()
{
    var addresssearchresults = document.getElementById("AddressSearchResultPanel");
    if(addresssearchresults)
    {
        hideFloatingPanel('AddressSearchResultPanel');
    }
}

function ZoomToAddress(x, y)
{
    setTimeout("MyCallback('EventArg=zoom&x=" + x + "&y=" + y + "');", 50);
    hideFloatingPanel('AddressSearchResultPanel');
}

function PrintMap()
{
    setTimeout("MyCallback('EventArg=print');", 50);
}

function OpenMapPrintWindow(printContent)
{
    var regexp = /xxrnxx/gi;
    
    var windowOptions = "menubar=yes,toolbar=yes,width=700,height=600,top=100,left=200,scrollbars=yes,resizable=yes";
	var printWin = window.open("", "_blank", windowOptions);
	
	if (printWin !== null)
	{
	    // restore line breaks (encoded as xxrnxx at server)
		printContent = printContent.replace(regexp, "\n");
		
		// "unescape" the content
		printContent = decode(printContent);
		
		printWin.document.write(printContent);

		printWin.document.close();
		printWin.focus();
	}
	else
	{
	    alert("Print window can't open.  Make sure you have any popup-blockers disabled for this site.");
	}
}

var conversionMap = {
	"amp" : "&",
	"lt" : "<",
	"gt" : ">",
	"apos" : "'",
	"quot" : '"'
};

function decode(entityString) {
	return entityString.replace(/&(\w+);/g, function(m,g) {
		return conversionMap[g]||m;
	});
}

function CookieHandler() {

    this.setPermanentCookie = function(name, value) {
    
        this.setCookie(name, value, -1);
    
    }

    this.setCookie = function (name, value, seconds) {
    
        if (typeof(seconds) != 'undefined') {
        
        	if(seconds == -1)
            {
                seconds = 31536000;  // one year
            }
        
	        var date = new Date();
	        date.setTime(date.getTime() + (seconds*1000));
	        var expires = "; expires=" + date.toGMTString();
        }
        else {
	        var expires = "";
        }
 
        document.cookie = name+"="+value+expires+"; path=/";
    }
 
    this.getCookie = function (name) {
 
        name = name + "=";
        var carray = document.cookie.split(';');
 
        for(var i=0;i < carray.length;i++) {
	        var c = carray[i];
	        while (c.charAt(0)==' ') c = c.substring(1,c.length);
	        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
        }
 
        return null;
    }
 
    this.deleteCookie = function (name) {
        this.setCookie(name, "", -1);
    }
 
}

function ShowSite()
{
    var ch = new CookieHandler();
    ch.setPermanentCookie("disclaimerviewed", "true");
    
    var whiteout = document.getElementById("WhiteOut");
    whiteout.style.display = "";
    hideFloatingPanel("Disclaimer");   
    map.refresh();
}

function RedirectSite()
{
    var ch = new CookieHandler();
    ch.setPermanentCookie("disclaimerviewed", "false");
    
    window.location = "http://www.fremontne.gov";
}
