var zipCode = "";

var dataFetch = Class.create();

dataFetch.prototype = 
{
	initialize: function( obj )
	{
		this.obj = obj;
    },
    url: function()
    {
        var urlStr = this.obj.getURL + '?';

        for( var key in this.obj.getParams ) 
        {
            urlStr += key + '='+ this.obj.getParams[key] + '&';
        }

        if( urlStr.endsWith('&') )
        {
           urlStr = urlStr.truncate( urlStr.length-1, '' );
        }

        return urlStr;
    },
    fetch: function()
    {
        new Ajax.Request(this.url() + '&d='+ new Date().getTime(), 
        {
            method: 'get',
            onSuccess: function(transport){ this.obj.targetControl.value = ( transport.responseText ); formSend(); }.bind( this )
        });
    }
};

function getFlashMovie(movieName)
{
	var movie;

	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	movie = (isIE) ? window[movieName] : document[movieName];

	// new to end
	if(movie == null)
	{
		movie = document.getElementById(movieName);
	}
	
	return movie;
}

function formSend()
{
	var hdnAddress = $("hdnAddress");
	var hdnZipCode = $("hdnZipCode");
	var hdnRegion = $("hdnRegion");
	
	var flashMovie = getFlashMovie("ExternalInterfaceExample");
	
	if( flashMovie )
	{
		flashMovie.sendTextToFlash( "Flash/" + hdnRegion.value + ".swf" ); 
	}
	
	mapIt( hdnAddress.value + ", " + hdnZipCode.value );
	
	// reload the weather center
	var ifWeatherCenter = $("ifWeatherCenter");
	if (ifWeatherCenter != null)
	{
		var txtZipCode = $("hdnZipCode");
		ifWeatherCenter.src = "WeatherCenter.aspx?zipCode=" + txtZipCode.value;
	}
}

function getTextFromFlash( str )
{
	var hdnAddress = $("hdnAddress");
	hdnAddress.value = str.substring( 0, str.length - 5 );
	
	var hdnZipCode = $("hdnZipCode");
	hdnZipCode.value = str.substring( str.length - 5, str.length );
	
	zipCode = hdnZipCode.value;
	
	var myDataFetch = new dataFetch(
	{
		getURL: 'GMapProcessor.aspx',
        getParams: 
        {
			zipCode: $("hdnZipCode").value
        },
		targetControl: $('hdnRegion')
    });
    
    myDataFetch.fetch();
    
	return str + " returned from javascript";
}

function getTextFromForm()
{
	var str = $("txtAddressNonFlash").value;
	
	// Set the value of the ASP.NET address textbox
	var txtAddress = $("hdnAddress");
	txtAddress.value = str.substring(0, str.length - 5);
	
	// Set the value of the ASP.NET zipcode textbox
	var txtZipCode = $("hdnZipCode");
	txtZipCode.value = str.substring(str.length - 5, str.length);
	zipCode = txtZipCode.value;
	
	var myDataFetch = new dataFetch(
	{
		getURL: 'GMapProcessor.aspx',
        getParams: 
        {
			zipCode: $("hdnZipCode").value
        },
		targetControl: $('hdnRegion')
    });
    
    myDataFetch.fetch();
	
	// return
	return str + " returned from javascript";
}

function zoomEnd()
{
	$("lookupcontainer").style.display = "none";
	$("mapcontainer").style.visibility = "visible";
}

function MM_callJS(jsStr)
{ //v2.0
	alert("in MM_callJS()");
	return eval(jsStr)
}

// automatically zoom map out
var intervalID = setInterval( MyZoomOut, 1000 );
var flag1 = true;
var flag2 = false;

function MyZoomOut()
{
	try
	{
		var map = document.getElementById( "map" );
		var index = map.innerHTML.indexOf( "Try zooming out" );
		
		if( flag1 && Map.base.getZoom() == 18 )
		{
			Map.base.setZoom( 19 );
			flag1 = false;
		}
		
		if( index > -1 )
		{
			Map.base.zoomOut();
			flag2 = true;
		}
		
		if( flag2 && index <= -1 )
		{
			clearInterval( intervalID );
		}
	}
	catch( ex )
	{
		// the Map variable isn't created
		// till the flash form is submitted
	}
}	