

function lookupAddressKey (_e, c)
{
    if ( _e.keyCode==13 )
    {
        lookupAddress (c);
        return false;
    }
    return true;
}




function lookupAddress (c)
{
var geocoder = new GClientGeocoder();
geocoder.getLatLng(
    $ ('searchGoogleMaps' ).value,
    function(point) {
      if (!point) {
        alert($ ('searchGoogleMaps' ).value + " not found on Google Maps.");
      } else {
        map.setCenter(point, 13);
        map.removeOverlay ( marker );
        marker = new GMarker(point,  {draggable:true});
        GEvent.addListener ( marker, "dragend", function () {  setSelected ( marker.getPoint () ); } );
        map.addOverlay(marker);
        setSelected ( marker.getPoint () ); 
        
        if (c )$ ( locationId ).value = $ ('searchGoogleMaps' ).value;
      }
    }
  );


}

var marker;
var map;

function createMarker ( lat, lon )
{
    map.removeOverlay ( marker );
    marker = new GMarker(new GLatLng(lat, lon),  {draggable:true} );
    map.addOverlay (marker);
    
    GEvent.addListener ( marker, "dragend", function () {  setSelected ( marker.getPoint () ); } );
    setSelected ( marker.getPoint () ); 
    
    map.setCenter(new GLatLng(lat,lon), 12);
}


function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    
    GEvent.addListener ( map, "click" , 
        function (o,p) { 
        

        map.removeOverlay ( marker );
        marker = new GMarker(p,  {draggable:true} );
        


        map.addOverlay(marker);
        
        GEvent.addListener ( marker, "dragend", function () {  setSelected ( marker.getPoint () ); } );

        setSelected ( marker.getPoint () ); 
        
        } 
    );
   
    
    map.setCenter(new GLatLng(37.4419, -40.1419), 2);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    
    marker = new GMarker(new GLatLng(37.4419, -40.1419),  {draggable:true} );
    map.addOverlay(marker);
    GEvent.addListener ( marker, "dragend", function () {  setSelected ( marker.getPoint () ); } );
    
    
    

  }
}


function locationChange ( _this )
{
    if ( _this [ _this.selectedIndex ].value=='ADD' ) { document.location.href='EditLocation.aspx'; return; }
    if ( _this [ _this.selectedIndex ].value!='' ) document.location.href='surfforecast.aspx?' + _this [ _this.selectedIndex ].value;
}


function locationChangeFB ( _this, i, n )
{
    if ( _this [ _this.selectedIndex ].value=='ADD' ) { document.location.href='FBEditLocation.aspx?I=' + i; return; }
    if ( _this [ _this.selectedIndex ].value!='' ) document.location.href='fbsurfforecast.aspx?F=' + _this [ _this.selectedIndex ].value +
    '&I=' + i + '&N=' + n;
}


function showLocation ()
{
    fadeIn ( 'changeLocation', 0.2, 'block' );
}

function hideLocation ()
{
    fadeOut ( 'changeLocation', 0.2 );
}

function setSelected ( point )
{        

    $ ( 'lat' ).innerHTML = Math.round ( point.y * 1000 ) / 1000;
    $ ( 'lon' ).innerHTML = Math.round ( point.x * 1000 ) / 1000;
    
    $ ( latv ).value = point.y;
    $ ( lonv ).value = point.x;
    $ ( xv ).value = getXValue ( point.x );
    $ ( yv ).value = getYValue ( point.y );
    
    // if this isn't the information entered into the form then crash out on submit
    $ ( spamFilter ).value='31337'; 
    
    
    //alert ( getXValue ( point.x ) );
    //alert ( getYValue ( point.y ) );
        
    if ( $ ( 'selector' ).style.display=='block' )
    {
        fadeOut ( 'selector', 0.2 );
        setTimeout ( "fadeIn ( 'selector', 0.3, 'block' )", 250 );
        setTimeout (" $ ( locationId ).focus ()", 600 );    

    } 
    else 
    {

        fadeOut ( 'selectorInstructions', 0.1 );
        setTimeout ( "$('selectorInstructions').style.display='none'; fadeIn ( 'selector' , 0.3, 'block' )", 120 );
        setTimeout (" $ ( locationId ).focus ()", 470 );
    }
    
    
}











// GENERIC SHIZZLE

// Magic numbers
var x_len=288;
var y_len=157;
var latStep=0.99363057324840764;
var lonStep=1.2456597222222223;

function getXValue(lon) {

	if(lon>0) {
		return Math.floor(lon/lonStep);
	}

	

	if(lon<=0) {
		return Math.floor(lon/lonStep+x_len-1);
	}

}

function getYValue(lat) {

	if(lat>0) {
		return Math.floor(((lat/latStep)-y_len/2)*-1);
	}

	if(lat<=0) {
		return Math.floor(((lat/latStep)*-1)+y_len/2);
	}
}




 
function fadeIn ( id, speed, display )
{
    if ( document.all ) {
        $(id).style.display=display;
        return;
    }
    
    if ( $(id).style.display=='none' || $(id).style.display=='' )
    {
        YAHOO.util.Dom.setStyle ( id, 'opacity',0);
        $(id).style.display=display;
    }

    anim = new YAHOO.util.Anim(id, { opacity: { to: 1 } }, speed, YAHOO.util.Easing.easeOut );
    anim.animate ();

}



function fadeOut ( id, speed )
{
    if ( speed == null ) speed = 0.2;
    if ( document.all ) { $(id).style.display='none'; return; }
    anim = new YAHOO.util.Anim(id, { opacity: { to: 0 } }, speed, YAHOO.util.Easing.easeOut );
    anim.animate ();

}






function displayError ( formNode , validators )
{
    var errorHTML = "";

    for(var i=0;i<validators.length;i++){
        errorHTML += "<li>" + validators[i].message + "</li>";
    }
     
    YAHOO.util.Dom.setStyle ( 'errorDisplay','display','block' );
    YAHOO.util.Dom.get ('errorDisplay').innerHTML = "The form could not be submitted -<br/><ul>" + errorHTML + "</ul>"; 	  
}

function displayErrorAdditional ( message )
{

    YAHOO.util.Dom.setStyle ( 'errorDisplay','display','block' );
    if ( YAHOO.util.Dom.get ('errorDisplay').innerHTML.indexOf ( message ) == -1 ) 
    YAHOO.util.Dom.get ('errorDisplay').innerHTML = "<ul><li>" + message + "</li></ul>"; 
}
