// JavaScript Document

var uaddr="",lat=0,lon=0,stype=1;
var geocoder=null;

function dogo(s, loc)
{
//	alert(loc);
  if ( loc ) {
  set_cookie("where", loc);
  }
	  if(s) stype=s;
      document.yp.k.value="";
      var f=document.fstandard;
      if(stype==2) {    // distance search
            f=document.fdistance;
            // geocode the address
            var addr=f.address.value;
            if(addr.length) addr+=" ";
            addr+=f.where.value;
            if(addr!=uaddr) {
                  uaddr=addr;
                  if(!geocoder) geocoder = new GClientGeocoder();
                  geocoder.getLocations(addr,geoCallback);
                  return false;
            }
            // set the radius
            var rad=f.miles.options[f.miles.selectedIndex].value;
            document.yp.k.value="rad:"+rad+";uaddr:"+uaddr+";lat:"+lat+";lon:"+lon;
      } else if(stype==3) {   // reverse lookup
            f=document.freverse;
            if(f.phone.value.length) f.what.value=f.phone.value;
            document.yp.k.value="rev:1";
      }
	  // validate location
	var stateNames={"alabama":"AL","alaska":"AK","arizona":"AZ","arkansas":"AR","california":"CA","colorado":"CO","connecticut":"CT","delaware":"DE","florida":"FL","georgia":"GA","hawaii":"HI","idaho":"ID","illinois":"IL","indiana":"IN","iowa":"IA","kansas":"KS","kentucky":"KY","louisiana":"LA","maine":"ME","maryland":"MD","massachusetts":"MA","michigan":"MI","minnesota":"MN","mississippi":"MS","missouri":"MO","montana":"MT","nebraska":"NE","nevada":"NV","ohio":"OH","oklahoma":"OK","oregon":"OR","pennsylvania":"PA","tennessee":"TN","texas":"TX","utah":"UT","vermont":"VT","virginia":"VA","washington":"WA","wisconsin":"WI","wyoming":"WY","al":"AL","ak":"AK","az":"AZ","ar":"AR","ca":"CA","co":"CO","ct":"CT","de":"DE","fl":"FL","ga":"GA","hi":"HI","id":"ID","il":"IL","in":"IN","ia":"IA","ks":"KS","ky":"KY","la":"LA","me":"ME","md":"MD","ma":"MA","mi":"MI","mn":"MN","ms":"MS","mo":"MO","mt":"MT","ne":"NE","nv":"NV","nh":"NH","nj":"NJ","nm":"NM","ny":"NY","nc":"NC","nd":"ND","oh":"OH","ok":"OK","or":"OR","pa":"PA","ri":"RI","sc":"SC","sd":"SD","tn":"TN","tx":"TX","ut":"UT","vt":"VT","va":"VA","wa":"WA","wv":"WV","wi":"WI","wy":"WY","dc":"DC"};
	var stateNames2={"new-hampshire":"NH","new-jersey":"NJ","new-mexico":"NM","new-york":"NY","north-carolina":"NC","north-dakota":"ND","rhode-island":"RI","south-carolina":"SC","south-dakota":"SD","west-virginia":"WV"};
	var where=f.where.value;
	if(where.length<5) { alert("Please enter a city name or zip code in the Location field"); return false; }
	var w=where.split(",");
	var wu=w[0].replace(/[\\\/\. ]/g,"-").replace(/[^a-zA-Z0-9-]/g,"").replace(/-+/g,"-").replace(/^-/g,"").replace(/-$/g,"");
	if(w.length>1) {
		var sn=w[1].replace(/[\\\/\. ]/g,"-").replace(/[^a-zA-Z0-9-]/g,"").replace(/-+/g,"-").replace(/^-/g,"").replace(/-$/g,"");
		if(stateNames[sn.toLowerCase()]) sn=stateNames[sn.toLowerCase()];
		else if(stateNames2[sn.toLowerCase()]) sn=stateNames2[sn.toLowerCase()];
		wu+=","+sn;
	}
	else {
		if((stateNames[wu.toLowerCase()] || stateNames2[wu.toLowerCase()]) && wu.toLowerCase()!="new-york") { alert("Please enter a city name or zip code in the Location field"); return false; }
		var i=wu.lastIndexOf("-");
		if(i>0) {
			var sn=wu.substr(i+1).toLowerCase();
			if(stateNames[sn]) wu=wu.substr(0,i)+","+stateNames[sn];
			else {
				i=wu.substr(0,i).lastIndexOf("-");
				if(i>0) {
					var sn=wu.substr(i+1).toLowerCase();
					if(stateNames2[sn]) wu=wu.substr(0,i)+","+stateNames2[sn];
	}	}	}	}
 location.href="http://search.namesandnumbers.com/"+f.where.value+"/"+f.what.value;
 //document.yp.go.value=f.what.value;
      //document.yp.pa.value=f.where.value;
      //document.yp.submit();
      return false;
}
 
function geoCallback(g)
{
      if(!g || g.Status.code!=200) {      // not found
            alert("Please enter a city name or zip code in the Location field.");
            return;
      }
      var p=g.Placemark[0];
      lat=Math.floor(p.Point.coordinates[1]*1000000+0.5);
      lon=Math.floor(p.Point.coordinates[0]*1000000+0.5);
      dogo();
}
function getQueryParameter ( parameterName ) {
  var queryString = window.top.location.search.substring(1);
  var parameterName = parameterName + "=";
  if ( queryString.length > 0 ) {
    begin = queryString.indexOf ( parameterName );
    if ( begin != -1 ) {
      begin += parameterName.length;
      end = queryString.indexOf ( "&" , begin );
        if ( end == -1 ) {
        end = queryString.length
      }
      return unescape ( queryString.substring ( begin, end ) );
    }
  }
  return "";
}

function initForm(oForm, element_name, init_txt) {
//alert(typeof(oForm));
//alert(init_txt);
frmElement = oForm.elements[element_name];
frmElement.value = init_txt;

}

function initWhere() {
var whereVal=getQueryParameter('where');
var whereLoc=get_cookie("where")
if (whereVal.length>0) {
	initForm(document.forms['fstandard'], 'where', whereVal);
	initForm(document.forms['fdistance'], 'where', whereVal);
	initForm(document.forms['freverse'], 'where', whereVal);
} else {
	initForm(document.forms['fstandard'], 'where', whereLoc);
	initForm(document.forms['fdistance'], 'where', whereLoc);
	initForm(document.forms['freverse'], 'where', whereLoc);
}
}
function addLoadEvent(func) {   
   var oldonload = window.onload;   
   if (typeof window.onload != 'function') {   
     window.onload = func;   
   } else {   
     window.onload = function() {   
       if (oldonload) {   
         oldonload();   
       }   
       func();   
     }   
   }   
 }
 
addLoadEvent(initWhere);

function set_cookie ( name, value, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );
  var myDate = new Date();
  newDate = myDate.getMonth() + "/" +myDate.getDate() + "/" + (myDate.getYear()+1);
  //cookie_string += "; expires=" + newDate;
  cookie_string += ";expires=Fri, 31-Dec-2023 00:00:00 GMT;path=/"
  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
//alert(cookie_string);
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return "";
}