// copyright c 2009 by t. j. marlin
// All rights reserved. No part of this script may be reproduced or used in any
// form or by electronic or mechanical means, including information storage and retrieval 
// systems, without the permission in writing from the author, except in normal use of
// browsing the web page of which this is a part. 

mapServer = 'Google';
numZips = 0;
numMaps = 0;
delete zips;
delete maps;
zips = new Array();
maps = new Array();

function zip( zipc, city, state ){
  this.zip = zipc.trim();
  this.city = city.trim();
  this.state = state.trim();
  zips[numZips++] = this;
} // function zip

function map( name, street, zip, lat, lon, field_map ){
  // alert( "in map with name " + name + " street " + street + " zip " + zip );
  this.name = name.trim();
  this.street = street.trim();
  this.zip = zip.trim();
  this.field_map = "";
  this.lat = "";
  this.lon = "";

  if( arguments.length >= 4 ){
	this.lat = lat;
	this.lon = lon;
	if( arguments.length == 6 ){
	    this.field_map = field_map;
	}
  }
  
  maps[numMaps++] = this;
} // function map


// do a binary search on the zip code array to get the entry for city and state.
// returns:
// >= 0 enty found. use return as anindex
// -1. zip not found.
function find_zc ( izc ){
  //alert( "in find_sc with " + izc );
  var low = 0;
  var high = numZips - 1;
  var idx;
  var test_count = 0;
  var tzc = izc.substr( 0, 5 ); // search o only the first 5 chars
  //say( "<p>in find with " + izc + "</p>" );
  while( low <= high ){
    test_count++;

    idx = Math.floor(( high - low ) / 2 ) + low;
	//alert( "test count " + test_count + " idx " + idx + " low " + low + " high " + high ); 
	// say( "idx: " + idx );
    if( zips[ idx ].zip == tzc ){
	  //alert( "find_zc found at " + idx + " with " + test_count + " compares" );
	  return idx;
	}
 
	if( zips[ idx ].zip > tzc ){
	  high = idx - 1;
	} else {
	  low = idx + 1;
	}
  } // while
  //alert( "find_zc not found with " + test_count + " compares" );
  return -1;
 } // find_zc

 
 // the function for building a map page. 
var mapWin;

// 27 oct 09
// newMapIt
// since the maps page is now generated from the server, the maps array in java script
// is no longer built. 
// this function generates the google map page.
// lots of stuff is stolen from mapIt

function newMapIt( name, istreet, icity, zip, lat, lon ){
	//alert( "in newMapIt with " + name );
	re = / /gi; // an re to replace blanks in strings with a plus sign
	var street = istreet.replace( re, "+");
	var city = icity.replace( re, "+");
	var place = name.replace( re, "+");
	var mpage;
	if( place.slice( 0, 5 ) == "ztest" ){
		mpage = "test_page";
	} else {
		mpage = "gmap_page";
	}
	var url = "http://www.ayso11k.org/maps/" 
	//var url = "http://asus/maps/" 
		  + mpage + ".shtml?lat=" 
		  + lat
		  + "&lon=" + lon 
		  + "&st=" + street 
		  + "&city=" + city 
		  + "&zip=" + zip 
		  + "&place=" + place;
	//alert( "url |" + url + "|" );
	mapWin = window.open( url, "mapWin", "location=yes,resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes");
	mapWin.focus();
}
function mapIt( pname ){
  //alert( "in mapIt with " + pname );
  var i, j, name;
  var city = "";
  var lat, lon;
  name = pname.trim();

  for( i = 0; i < numMaps; i++ ){
  	if( maps[ i ].name == name ){
	    //alert( "found " + pname + " at index " + i + " zip code " + maps[i].zip  ); //
		j = find_zc( maps[ i ].zip );
		if( j < 0 ){
		  alert( "zip code " + maps[ i ].zip + " not known. Send mail to webmaster." );
		  break;
		}
		//alert( "found zip " + maps[ i ].zip + " at index " + j + " city " + zips[ j ].city + " state " + zips[ j ].state);
	   // for( j = 0; j < numZips; j++ ){
	//	  if( maps[ i ].zip == zips[ j ].zip ){
    //      city = zips[ j ].city;
	//		break;
	//	  }
	//	} // for( j = 0; j < numZips; j++ ){
	    re = / /gi; // an re to replace blanks in strings with a plus sign
		var street = maps[ i ].street.replace( re, "+");
		city = zips[ j ].city.replace( re, "+");
		lat = maps[ i ].lat;
		lon = maps[ i ].lon;
		var url = "";
		var lMapServer = mapServer;	
		if( mapServer == "Google" ){
		  if( maps[ i ].lat == "" ){
		    alert( "Google maps not available for " + name + " yet. Yahoo will be used." );
			lMapServer = "Yahoo";
		  }
		}
		switch( lMapServer ){
		  case "mapQuest" :
		   // alert( "building map request for mapQuest" );
		   // mapquest interface updated, 9 dec 01.	
           //http://www.mapquest.com/maps/map.adp
		   // ?country=US
		   // &address=2650+Fairview+Rd
		   // &city=&state=
		   // &zipcode=92626
		   // &homesubmit.x=38&homesubmit.y=10
		
	 	   // ampquest interface updated, may 04
		   // http://www.mapquest.com/maps.adp
		   // ?country=US&countryid=250
		   // &address=<number>+<street name>
		   // &city=<city>
		   // &state=<state>
		   // &zipcode=<zipcode>
		   // &search=++Search++
		
    	   url =
		   "http://www.mapquest.com/maps/map.adp?country=US&countryid=250&addtohistory=&searchtab=address&searchtype=address&address=";

		   url += street; 
		   url += "&city=";
		   url += city;
		   url += "&state=";
		   url += zips[ j ].state;
		   url += "&zipcode=";
		   url += maps[ i ].zip;
		   url += "&search=++Search++";
		   break;
	   case "Yahoo" :
	      // the Yahoo map request, 2 sept 05 is
		  // http://maps.yahoo.com/maps_result?addr=16095+mesquite+cir&csz=92708&country=us&new=1&name=&qty=
	      //alert( "building map request for Yahoo" );
	      url = "http://maps.yahoo.com/maps_result?addr=";
		  url += street;
		  url += "&csz=";
		  url += maps[ i ].zip;
		  url += "&country=us&new=1&name=&qty=";
		  break;
	   case "Google" :
	      //alert( "build map request for google" );
		  var place = name.replace( re, "+");
		  var mpage;
		  if( place.slice( 0, 5 ) == "ztest" ){
		     mpage = "test_page";
		  } else {
		     mpage = "gmap_page";
		  }
	      url = "http://www.ayso11k.org/maps/" + mpage + ".shtml?lat=" 
		          + lat
				  + "&lon=" + lon 
				  + "&st=" + street 
				  + "&city=" + city 
				  + "&zip=" + maps[ i ].zip 
				  + "&place=" + place;
		  //alert( "url |" + url + "|" );
		  //openLink( url, 'mapWin' );
		  break;
	 } // switch
	 //if( lMapServer != "Google" ){
	 // alert( "built url: " + url );
	 //if( mapWin ){
	 //  alert( "mapWin defined, close it" );
	 //  mapWin.close();
	 //}
	 mapWin = window.open( url, "mapWin", "location=yes,resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes");
	 //}
     mapWin.focus();
	 break;
	}  // if( maps[i].name == name ){
  } // for( i = 0; i < numMaps; i++ ){
  if( i == numMaps ){
    alert( "Place name " + pname + " not known. Send mail to webmaster." );
  } // if( i == numMaps ){
}  //  function mapIt



function genServerChoice(){
  say( '<p><form name="mapServerChoiceForm">' );
  say( '<b>Choose your preferred map server:</b>' );
  say( '   <input type = "radio" name = "mapServerChoice" onClick = "chooseMapServer( \'mapQuest\' )" > MapQuest ' );
  say( '   <input type = "radio" name = "mapServerChoice" onClick = "chooseMapServer( \'Yahoo\' )" > Yahoo ' );
  say( '   <input type = "radio" name = "mapServerChoice" onClick = "chooseMapServer( \'Google\' )" > Google ' );
  say( '</form></p>' );
  switch( mapServer ){
    case "mapQuest" :
      document.mapServerChoiceForm.mapServerChoice[0].checked = 1;
	  break;
    case "Yahoo" :
      document.mapServerChoiceForm.mapServerChoice[1].checked = 1;  
	  break;
	case "Google" :
	  document.mapServerChoiceForm.mapServerChoice[2].checked = 1;  
	  break;
  }
  say( '<p>For the map server choice to work on your machine, you should clear your memory and disk cache.</p>' );
  say( "<p>MapQuest's page has many errors. " );
  say( 'To make Mapquest work reliably I find that I have to delete the mapquest cookie ');
  say( 'between map requests (webmaster running firefox).</p>' );
} // function genServerChoice{

function chooseMapServer( choice ){
  // alert( "in chooseMapServer with " + choice + "prev value " + mapServer  );
  mapServer = choice;
} // function chooseMapServer( choice ){

function genMapList(){
  var i, j, city;
  say( '<p><table border="0" align="center" cellpadding="4" >' );
  for( i = 0; i < numMaps; i++ ){
    city = "";
	for( j = 0; j < numZips; j++ ){
	  if( maps[ i ].zip.substr( 0, 5 ) == zips[ j ].zip ){
	    city = zips[ j ].city;
		break;
	  }
	} // for( j = 0; j < numZips; j++ ){

  	say( '<tr>' );
	say( '<td><a href=\"javascript: void mapIt(\'' );
	say( maps[i].name );
	say( '\')\">' + maps[i].name + '</a></td>' );
	say( '<td>' + maps[i].street + '</td>' );
	say( '<td>' + city + '</td>' );
	say( '<td>' + maps[i].zip.substr( 0, 5 ) + '</td>' );
	if( maps[i].field_map != "" ){
	  say( '<td>' + '<a href= "http://www.ayso11k.org/maps/' + maps[i].field_map + '">field info</a>' + '</td>' );
	}
	say( '</tr>' );
  } // for( i = 0; i < numMaps; i++ ){
  say( '</table></p>' );
} // function genMapList(){

/*
new zip( "zip", "city", "state" );
new zip( "92708", "Fountain Valley", "ca" );
new zip( "9", "", "CA" );

Must be in asending zip code numeriacal order.
*/

new zip( "90240", "Downey", "CA" );
new zip( "90620", "Buena Park", "CA" );
new zip( "90623", "La Palma", "CA" );
new zip( "90630", "Cypress", "CA" );
new zip( "90631", "La Habra", "CA" );
new zip( "90638", "La Mirada", "CA" );
new zip( "90703", "Cerritos", "CA" );
new zip( "90720", "Los Alamitos", "CA" );
new zip( "90808", "Long Beach", "CA" );
new zip( "90815", "Long Beach", "CA" );
new zip( "91764", "Ontario", "CA" );
new zip( "92069", "San Marcos", "CA" );
new zip( "92078", "San Marcos", "CA" );
new zip( "92374", "Redlands", "CA" );
new zip( "92590", "Temecula", "Ca" );
new zip( "92603", "Irvine", "CA" );
new zip( "92604", "Irvine" , "CA");
new zip( "92606", "Irvine" , "CA");
new zip( "92610", "Foothill Ranch", "CA" );
new zip( "92612", "Irvine", "CA" );
new zip( "92618", "Irvine", "CA" );
new zip( "92620", "Irvine", "CA" );
new zip( "92625", "Corona Del Mar", "CA" );
new zip( "92626", "Costa Mesa", "CA" );
new zip( "92627", "Costa Mesa", "CA" );
new zip( "92630", "Lake Forest", "CA" );
new zip( "92640", "Garden Grove", "CA" );
new zip( "92641", "Garden Grove", "CA" );
new zip( "92643", "Garden Grove", "CA" );
new zip( "92644", "Garden Grove", "CA" );
new zip( "92646", "Huntington Beach", "CA" );
new zip( "92647", "Huntington Beach", "CA" );
new zip( "92648", "Huntington Beach", "CA" );
new zip( "92649", "Huntington Beach", "CA" );
new zip( "92653", "Laugna Hills", "CA" );
new zip( "92655", "Midway City", "CA" );
new zip( "92656", "Aliso Viejo", "CA" );
new zip( "92660", "Newport Beach", "CA" );
new zip( "92663", "Newport Beach", "CA" );
new zip( "92673", "San Clemente", "CA" );
new zip( "92675", "San Juan Capistrano", "CA" );
new zip( "92677", "Laguna Niguel", "CA" );
new zip( "92680", "Tustin", "CA" );
new zip( "92683", "Westminster", "CA" );
new zip( "92688", "Rancho Santa Margarita", "CA" );
new zip( "92691", "Mission Viejo", "CA" );
new zip( "92692", "Mission Viejo", "CA" );
new zip( "92694", "Ladera Ranch", "CA" );
new zip( "92701", "Santa Ana", "CA" );
new zip( "92703", "Santa Ana", "CA" );
new zip( "92704", "Santa Ana", "CA" );
new zip( "92705", "Santa Ana", "CA" );
new zip( "92706", "Santa Ana", "CA" );
new zip( "92707", "Santa Ana", "CA" );
new zip( "92708", "Fountain Valley", "CA" );
new zip( "92710", "Tustin", "CA" );
new zip( "92714", "Irvine" , "CA");
new zip( "92780", "Tustin", "CA" );
new zip( "92782", "Tustin", "CA" );
new zip( "92841", "Garden Grove", "CA" );
new zip( "92843", "Garden Grove", "CA" );
new zip( "92845", "Garden Grove", "CA" );
new zip( "92865", "Orange", "CA" );
new zip( "92868", "Orange", "CA" );
new zip( "92870", "Placentia", "CA" );