function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function getURLParameter( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

/*function formatString(sInput)
{
    sInput = sInput.Replace("_", " ");
    sInput = sInput.Replace("%26", "&");
    return sInput;
}*/

function getURL(URL, target)
{
	if(getURLParameter('menuType') == "web"){
		document.write("<a href='" + URL + "?menuType=web' target='" + target + "'>");
	}else if(getURLParameter('menuType') == "standard"){
		document.write("<a href='" + URL + "?menuType=standard' target='" + target + "'>");
	}else if(getURLParameter('menuType') == "portable"){
		document.write("<a href='" + URL + "?menuType=portable' target='" + target + "'>");
	}else if(getURLParameter('menuType') == "international"){
		document.write("<a href='" + URL + "?menuType=international' target='" + target + "'>");
	}else{
		document.write("<a href='" + URL + "' target='" + target + "'>");
	}
}

function searchBtnPressed()
{
	var sZipCode = document.getElementsByName("ZipCode")[0].value
	var sState = document.getElementsByName("State")[0].value
	var sCity = document.getElementsByName("City")[0].value
	var sZipCodeMiles = document.getElementsByName("ZipCodeMiles")[0].value	
	if (sZipCode.length == 0 && sState.length == 0 && sCity.length == 0){
		alert('Validation Error: Must enter some search criteria!')
		return;
	}
	if (sZipCode.length > 0){
		window.location = "/StorageLocations/USA/" + sZipCode + "/Search.htm?menuType=search&ZipCodeMiles=" + sZipCodeMiles
	}else{
		if (sState.length == 0 && sCity.length != 0){
			alert('Validation Error: Cannot enter a city without a state!')
			return;
		}else if (sState.length != 0 && sCity.length != 0){
			if (sState.length == 2){
				window.location = "/StorageLocations/USA/" + sState + "/_/" + sCity + "/Search.htm?menuType=search"
			}else{
				window.location = "/StorageLocations/USA/_/" + sState + "/" + sCity + "/Search.htm?menuType=search"
			}
		}else{
			if (sState.length == 2){
				window.location = "/StorageLocations/USA/" + sState + "/_/Search.htm?menuType=search"
			}else{
				window.location = "/StorageLocations/USA/_/" + sState + "/Search.htm?menuType=search"
			}
		}
	}
}

function checkEnter(e){ //e is event object passed from function invocation
	var characterCode //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		document.forms[0].submit() //submit the form
		//searchBtnPressed()
		return false
	}
	else{
		return true
	}
}
  
  var map = null;
  var geocoder = null;
  var zoomLevel = 11;

  function load() {
    if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"));
      //map.setCenter(new GLatLng(37.4419, -122.1419), 1);
      geocoder = new GClientGeocoder();
      map.addControl(new GLargeMapControl());
		  //map.addControl(new GSmallMapControl());
      //map.addControl(new GMapTypeControl());
    }
  }

  function zoomOutMap() {
    if (geocoder) {
      map.zoomOut();
    }
  }
  
  function showAddress(address) {
	  showAddressZoom(address, 11);
  }

	//jss mapa  tocado para poner por defecto lo que quiero
  function showAddressZoom(address, zoom,jj) {
	zoomLevel = zoom;
    if (geocoder) {
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
			try {
				showAddressZoom(address.substring(address.indexOf(',') + 1), zoomLevel,jj);
			}catch(ex){
				showAddress("SPAIN");
			}
          } else {
            map.setCenter(point, zoomLevel);
				//he encontado un punto lo marco
            //map.zoomOut();
            //map.zoomOut();
				creaPunto(point,jj);
            //var marker = new GMarker(point);
            //map.addOverlay(marker);
            //marker.openInfoWindowHtml(address);
				//counter++; //como pongo un punto lo marco en el contador
          }
        }
      );
    }
	  //addLocationMarker("3000 highwoods blvd, raleigh, nc");
  }
  
        // A function to create the marker and set up the event window
      // Dont try to unroll this function. It has to be here for the function closure
      // Each instance of the function preserves the contends of a different instance
      // of the "marker" and "html" variables which will be needed later when the event triggers.    
      function createMarker(point,html) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        return marker;
      }
  
    function addLocationMarker(address, infoWindowText) {
    if (geocoder) {
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
			//alert('test1')
		  } else {
            //map.setCenter(point, 13);
            //var marker = new GMarker(point);
			var marker = createMarker(point,"<div style='width:315px'>" + infoWindowText + "</div>");
            map.addOverlay(marker);
            //marker.openInfoWindowHtml(address);
          }
        }
      );
    }
  }
