	var sponsors = new Array();	//globals
	var currZip = 02134;
	var currLat = 0;
	var currLng = 0;
	var allCallsFinished = true;
	
			function sponsorlookup(){
			var ssponsor = $('sponsorlook').value;
			var cosp = -1;
					for( var i=0; i< sponsorlist.cosponsor.length; i++ )
					{
						if( ssponsor.toLowerCase() == sponsorlist.cosponsor[i].last_name.toLowerCase() )
						{
							cosp = i;
							break;
						}
					}
				if(cosp >=0)
					alert( "Representative " + sponsorlist.cosponsor[cosp].first_name + " " + sponsorlist.cosponsor[cosp].last_name + " of " + sponsorlist.cosponsor[cosp].state + " has co-sponsored HR 676.");
				else
					alert( "Could not find any cosponsors with a last name of " + ssponsor );
			}
			
			function districtsponsor( state, district ){
			var issponsor = false;
			//cycle through known sponsors and see if any match the state and district
			for( var i=0; i< sponsorlist.cosponsor.length; i++ )
			{
				if( sponsorlist.cosponsor[i].state == state && sponsorlist.cosponsor[i].district == district )
				{
					issponsor = true;
					break;
				}
			}
				return issponsor;
			}

			function getSearchBy()
			{
				return (document.getElementById("search_by").value);
				//hiding this!
				//var sb = document.getElementById("search_by");
					//return( sb.options[sb.selectedIndex].value );	
			}
			
			function processSearch()
			{
				var sb = getSearchBy();
				if( sb == "zip" )
					processZip();
				else if (sb == "lastname" )
					processLastName();
				else if (sb == "citystate" )
					processState();	//processCityState();

			}
			
			function setMessage( message )
			{
				$('statusDiv').innerHTML = message;	
			}
			
			function processZip() {
				setMessage("Looking up legislators. Please wait...");				
				var zzip = getZip();
				//check here to see if state is one of the places sunlight doesn't support
				if( ! unsupportedSunlightZips(zzip) )
				{
					currZip = zzip;
					//setCoords( zzip );
					repObjectsFromZip( zzip );
				}
			}
			
			function getZip() {
					var zzip = document.getElementById('searchFor').value;
					zzip = zzip.substr(0,5);
					return zzip;
			}
			
			function processLastName()
			{
				setMessage("Looking up legislators. Please wait...");				
				var lastname = document.getElementById('searchFor').value;
				repObjectsFromLastNames( lastname );
			}
			
			function processCityState() {
				setMessage("Parsing city. Please wait...");				
				var citState = getCityState();
				if( citState == "Error" )
					alert( "Error parsing city and state." );
				else
				{
				//check here to see if state is one of the places sunlight doesn't support
				if( ! unsupportedSunlightStates(citState[1] ) )
				{
					//encode spaces before sending.
					citState[0].replace(/\s+/,'%20' );
					cityStateToReps( citState[0], citState[1] );
				}
				}
			}
			
			function processState() {
				setMessage("Parsing state. Please wait...");				
				var citState = getState();
				if( citState == "Error" )
					alert( "Error parsing city and state." );
				else
				{
				//check here to see if state is one of the places sunlight doesn't support
				if( ! unsupportedSunlightStates(citState ) )
				{
					repObjectsFromState( citState );
				}
				}
			}
			
			function getState() {
					var cs = document.getElementById('searchFor').value;
					//now clean
					if( cs.length < 2 )
						return "Error";
					cs = trim(cs);
					cs =cs.substr(0,2).toUpperCase();
					return ( cs );
			}

			
			function getCityState() {
					var cs = document.getElementById('searchFor').value;
					var citStateTemp = cs.split(",");
					//now clean
					if( citStateTemp.length < 2 )
						return "Error";
					citStateTemp[0] = trim(citStateTemp[0]);
					citStateTemp[1] = trim(citStateTemp[1]);
					if(citStateTemp[0].length < 2 || citStateTemp[1].length < 2)
						return "Error";
					citStateTemp[1] =citStateTemp[1].substr(0,2).toUpperCase();
					return (new Array( citStateTemp[0], citStateTemp[1] ));
			}

	//filter out delegates, not supported by sunlight.
	//hack - this only works because their last names are distinct
	function unsupportedSunlightStates( state )
	{
					switch( state )
				{
					case "AS":
						repObjectsFromLastName("Faleomavaega");
						return true;
						break;
					case "VI":
						repObjectsFromLastName("Christensen");
						return true;
						break;
					case "PR":
						repObjectsFromLastName("Fortuno");
						return true;
						break;
					case "DC":
						repObjectsFromLastName("Norton");
						return true;
						break;
					case "GU":
						repObjectsFromLastName("Bordallo");
						return true;
						break;
					default:
						return false;
				}
	}

	
	
	//filter out delegates, not supported by sunlight.
	//hack - this only works because their last names are distinct
	function unsupportedSunlightZips( theZip )
	{
		var ft = theZip.substr(0,3);	//can tell state from first 3 digits
					switch( theZip )
				{
					case "96799":
						repObjectsFromLastName("Faleomavaega");
						return true;
						break;
					case "00801": 
					case "00802": 
					case "00803": 
					case "00804": 
					case "00820": 
					case "00821": 
					case "00822": 
					case "00823": 
					case "00824": 
					case "00830": 
					case "00831": 
					case "00840": 
					case "00841": 
					case "00850": 
					case "00851":
						repObjectsFromLastName("Christensen");
						return true;
						break;
					default:
					if( ft == "969" )	//Guam
					{
						repObjectsFromLastName("Bordallo");
						return true;
					}
					// Puerto Rico - ok to do this way since we already filtered VI
					else if( ft == "006" || ft == "007" || ft == "008" || ft == "009" )
					{
						repObjectsFromLastName("Fortuno");
						return true;
					}
					//DC
					else if( ft == "200" || ft == "201" || ft == "202" || ft == "203" || ft == "204" || ft == "205"  )
					{
						repObjectsFromLastName("Norton");
						return true;
					}
					else
						return false;
				}
	}

	//set our global coordinate values from an Ajax call
			function setCoords( zzip )
			{
			currLat = 0;
			currLng = 0;
			
			var url = "wraps/getZipCoordsWrapper.php?zip=" + zzip + "&call=district";
			
			//grab coords via ajax
			new Ajax.Request(url,{method:'get',
				onComplete:function(req){
					var coords = req.responseText;
				if( !isNaN(parseInt(coords.substr(0,2))))	//did we get an error code
				{
					//some default coords
					currLat = 38.971667;
					currLng = -95.235;
					setMessage("Unable to find coordinates for zip code: " + zzip + ". Try another zip code." );
					//suppress message - probably has already been seen in zip error
					//alert( "Unable to find coordinates for zip code: " + zzip + "." );
				}
				else
				{
				var latlng = eval( '(' + coords + ')' );
				currLat = latlng.latitude;
				currLng = latlng.longitude;
				//setMessage('(' + coords + ')');
				//setMessage("Coordinates found.");
				}
				}
			});		//end coordinate grabbing
			
			}



	//set our global coordinate values from an Ajax call
			function setSomeCoords( sstate )
			{
			currLat = 0;
			currLng = 0;
			
			var url = "wraps/getCoordsFromStateWrapper.php?state=" + sstate;
			
			//grab coords via ajax
			new Ajax.Request(url,{method:'get',
				onComplete:function(req){
					var coords = req.responseText;
				if( !isNaN(parseInt(coords.substr(0,2))))	//did we get an error code
				{
					//some default coords
					currLat = 38.971667;
					currLng = -95.235;
					setMessage("Unable to find coordinates." );
				}
				else
				{
				var latlng = eval( '(' + coords + ')' );
				currLat = latlng.latitude;
				currLng = latlng.longitude;
				}
				}
			});		//end coordinate grabbing
			
			}


	//set our global coordinate to nothing specific
			function setNoCoords( )
			{
					//some default coords
					currLat = 38.971667;
					currLng = -95.235;			
			}

				//grab zip and then set the coords
				//moving everything in here - asynchronous was killing coords
			function cityStateToReps( city, state )
			{
			
			var url = "wraps/getZipFromCityWrapper.php?city=" + city + "&state=" + state;
			//alert( url );
			//grab coords via ajax
			new Ajax.Request(url,{method:'get',
				onComplete:function(req){
					var zipReq = req.responseText;
				if( !isNaN(parseInt(zipReq.substr(0,2))))	//did we get an error code
				alert( "Unable to find zip code for " + city + ", " + state + "." );
				else
				{
				var jsonZips = eval( '(' + zipReq + ')' );
				setMessage("Zip code found.");
				//alert( "Zipreq: " + jsonZips.zips[0] );
			currZip = jsonZips.zips[0];
			currLat = 0;
			currLng = 0;
			
			var url = "wraps/getZipCoordsWrapper.php?zip=" + currZip + "&call=district";
			
					//grab coords via ajax
					new Ajax.Request(url,{method:'get',
						onComplete:function(req){
							var coords = req.responseText;
						if( !isNaN(parseInt(coords.substr(0,2))))	//did we get an error code
						alert( "Unable to find coordinates for zip code: " + currZip + "." );
						else
						{
						var latlng = eval( '(' + coords + ')' );
						currLat = latlng.latitude;
						currLng = latlng.longitude;
						setMessage("Coordinates found.");
						repObjectsFromCityState( city, state );
						}
						}
					});		//end coordinate grabbing

				}
				}
			});		//end zip code finding
			
			}
			
			//return an object with reps attached to a zip code
			function repObjectsFromZip(zzip){
			allCallsFinished = false;
			sponsors = new Array();
			var i;
			var j;

			setCoords( zzip );
			var url = "wraps/getRepsFromZipWrapper.php?zip=" + zzip;
			//get reps from zip
			new Ajax.Request(url,{method:'get',
				onComplete:function(req){
					//grab the returned data
					var zipReps = "" + req.responseText;
					 if(!isNaN( parseInt(zipReps.substr(0,2))))	//did we get an error code
							alert( "Unable to find a representative for zip code: " + zzip + ". Try a different zip code." );
						else
						{	
							setMessage("Representatives found");
							//setMessage("R: " + zipReps);
					var resp = eval( '(' + zipReps + ')' );
					var repList = resp.response;
					var url;
					var repsGottenBack = 0;

					 /****/
					 //08603 is good for testing fake districts with no reps
					 //97132, 21206 - good testers for multiple reps in one zip code
					 if(repList.legislators.length != 0 )
					 {
						 for( i = 0; i < repList.legislators.length; i++ )
						 {

							var theRep = repList.legislators[i].legislator;
							//document.getElementById("map_sidebar").innerHTML = "rep: " + theRep;
							if( theRep.title == "Rep" )
							{
							//setMessage("Representative info found.");
							//theRep.zip = currZip;
							theRep = injectStaggeredCoords( theRep, currLat, currLng, repsGottenBack );
							sponsors.push(sunToSponsor(theRep));
							//alert( sponsors[0].lastname );
							repsGottenBack++;
							}
						}
							//alert( "reptitle: " + theRep.legislator.title);
							setMessage("rep: " + theRep);

							allCallsFinished = true; //not certain, but very likely at this point
							//setMessage("Calling Mapping");
							addJsonSponsors();
							//Finished looping and call-backs should be done at this point
						}
					//setMessage("reps: " + repsGottenBack);

					}	//end for cycling through districts
					}	//if( distlist.districts.length != 0 )
					/****/
				}
			);
		}

			
			//return an object with reps attached to a zip code
			function repObjectsFromLastNames(lastname){
			allCallsFinished = false;
			sponsors = new Array();
			var i;
			var j;

			setNoCoords();
			var url = "wraps/getRepsFromLastNameWrapper.php?lastname=" + lastname;
			//get reps from last name
			new Ajax.Request(url,{method:'get',
				onComplete:function(req){
					//grab the returned data
					var namedReps = "" + req.responseText;
					 if(!isNaN( parseInt(namedReps.substr(0,2))))	//did we get an error code
							alert( "Unable to find a representative for last name: " + lastname + ". Try a different zip code." );
						else
						{	
							setMessage("Representatives found");
							//setMessage("R: " + namedReps);
					var resp = eval( '(' + namedReps + ')' );
					var repList = resp.response;
					var url;
					var repsGottenBack = 0;

					 if(repList.legislators.length != 0 )
					 {
						 for( i = 0; i < repList.legislators.length; i++ )
						 {

							var theRep = repList.legislators[i].legislator;
							if( theRep.title == "Rep" )
							{
							//get some coordinates
							var url2 = "wraps/getCoordsFromStateWrapper.php?state=" + theRep.state;
						
						//grab coords via ajax
						new Ajax.Request(url2,{method:'get',
							onComplete:function(req){
								var coords = req.responseText;
							if( !isNaN(parseInt(coords.substr(0,2))))	//did we get an error code
							{
								//some default coords
								currLat = 38.971667;
								currLng = -95.235;
								setMessage("Unable to find coordinates." );
								alert("Error: " + coords );
							}
							else
							{
							var latlng = eval( '(' + coords + ')' );
							currLat = latlng.latitude;
							currLng = latlng.longitude;
							//setSomeCoords( theRep.state);
							//alert("No Error: " + coords );
							}
							}
						});		//end coordinate grabbing
			
							theRep = injectStaggeredCoords( theRep, currLat, currLng, repsGottenBack );
							sponsors.push(sunToSponsor(theRep));
							repsGottenBack++;

							}
						}

							//Finished looping and call-backs should be done at this point
						}

					}	//end for cycling through districts
							allCallsFinished = true; //not certain, but very likely at this point
							addJsonSponsors();
					}	//if( distlist.districts.length != 0 )
					/****/
				}
			);
		}

			
			//return an object with reps attached to a state
			function repObjectsFromState(sstate){
			allCallsFinished = false;
			sponsors = new Array();
			var i;
			var j;
		/*** SO HOW DO WE DO THIS??? ***/
			//setSomeCoords( sstate );
										//get some coordinates
							var url2 = "wraps/getCoordsFromStateWrapper.php?state=" + sstate;
						
						//grab coords via ajax
						new Ajax.Request(url2,{method:'get',
							onComplete:function(req){
								var coords = req.responseText;
							if( !isNaN(parseInt(coords.substr(0,2))))	//did we get an error code
							{
								//some default coords
								currLat = 38.971667;
								currLng = -95.235;
								setMessage("Unable to find coordinates." );
								//alert("Error: " + coords );
							}
							else
							{
							var latlng = eval( '(' + coords + ')' );
							currLat = latlng.latitude;
							currLng = latlng.longitude;
							}
							}
						});		//end coordinate grabbing

			var url = "wraps/getRepsFromStateWrapper.php?state=" + sstate;
			//get reps from state
			new Ajax.Request(url,{method:'get',
				onComplete:function(req){
					//grab the returned data
					var stateReps = "" + req.responseText;
					 if(!isNaN( parseInt(stateReps.substr(0,2))))	//did we get an error code
							alert( "Unable to find a representative for state: " + sstate + ". Try a different zip code." );
						else
						{	
							setMessage("Representatives found");
					var resp = eval( '(' + stateReps + ')' );
					var repList = resp.response;
					var url;
					var repsGottenBack = 0;

					 /****/
					 //08603 is good for testing fake districts with no reps
					 //97132, 21206 - good testers for multiple reps in one zip code
					 if(repList.legislators.length != 0 )
					 {
						 for( i = 0; i < repList.legislators.length; i++ )
						 {

							var theRep = repList.legislators[i].legislator;
							//document.getElementById("map_sidebar").innerHTML = "rep: " + theRep;
							if( theRep.title == "Rep" )
							{
							//setMessage("Representative info found.");
							//theRep.zip = currZip;
							theRep = injectStaggeredCoords( theRep, currLat, currLng, repsGottenBack );
							sponsors.push(sunToSponsor(theRep));
							//alert( sponsors[0].lastname );
							repsGottenBack++;
							}
						}
							//alert( "reptitle: " + theRep.legislator.title);
							setMessage("rep: " + theRep);

							allCallsFinished = true; //not certain, but very likely at this point
							//setMessage("Calling Mapping");
							addJsonSponsors();
							//Finished looping and call-backs should be done at this point
						}
					//setMessage("reps: " + repsGottenBack);

					}	//end for cycling through districts
					}	//if( distlist.districts.length != 0 )
					/****/
				}
			);
		}

					
		//return an object with reps attached to a city, state
		function repObjectsFromCityState(city, state){
			allCallsFinished = false;
			var repsPutIn = 0;
			var repsGottenBack = 0;
			sponsors = new Array();
			var i;
			var j;

			
			var url = "wraps/getRepFromStateWrapper.php?refiner=" + city + "&state=" + state + "&call=city";
			//get city state from zip
			new Ajax.Request(url,{method:'get',
				onComplete:function(req){
					//grab the returned data
					var reps = "" + req.responseText;
					 if(!isNaN( parseInt(reps.substr(0,2))))	//did we get an error code
							alert( "Error finding representatives for: " + city + ", " + state + ". Please try again." );
						else
						{	
							//alert( reps );
							setMessage("Representatives found");
					var repslist = eval( '(' + reps + ')' );
					var url;

					 /****/

					 if(repslist.entity_ids.length != 0 )
					 {
						 repsPutIn = repslist.entity_ids.length;
						 for( i = 0; i < repslist.entity_ids.length; i++ )
						 {
							var url = "wraps/getPersonInfoWrapper.php?id="+repslist.entity_ids[i];

						//get info on rep	
						 new Ajax.Request(url,{method:'get',
							onComplete:function(req){
								var rep = req.responseText;
							if( !isNaN(parseInt(rep.substr(0,2))))	//did we get an error code
							alert( "Unable to find information on a representative for " + city + ", " + state );
							
							var theRep = eval( '(' + rep + ')' );
							//setMessage("Representative info found: " + (repsGottenBack+1));
							setMessage("Getting info for " + repsPutIn + " Representatives.");
							theRep.zip = currZip;
							theRep = injectStaggeredCoords( theRep, currLat, currLng, repsGottenBack );
							sponsors.push(sunToSponsor(theRep));
							//alert( sponsors[0].lastname );

							repsGottenBack++;

						if( repsGottenBack >= repsPutIn )
						{
							allCallsFinished = true; //not certain, but very likely at this point
							//setMessage("Calling Mapping");
							addJsonSponsors();
							}//Finished looping and non-zip call-backs should be done at this point
							}
						});	
					}	//end for cycling through reps
					}	//if( distlist.districts.length != 0 )
					/****/
				}
			}
			});
		}

					
		//return an object with reps attached to a city, state
		function repObjectsFromLastName(lastname){
			allCallsFinished = false;
			var repsPutIn = 0;
			var repsGottenBack = 0;
			sponsors = new Array();
			var i;
			var j;

			var url = "wraps/getRepsFromLastNameWrapper.php?lastname=" + lastname;
			//get city state from zip
			new Ajax.Request(url,{method:'get',
				onComplete:function(req){
					//grab the returned data
					var reps = "" + req.responseText;
					 if(!isNaN( parseInt(reps.substr(0,2))))	//did we get an error code
							alert( "Error finding representatives with last name: " + lastname + ". Please try again." );
						else
						{	
							//alert( reps );
							setMessage("Representatives found");
					var repslist = eval( '(' + reps + ')' );

					 if(repslist.length != 0 )
					 {
						 repsPutIn = repslist.length;
						 for( i = 0; i < repslist.length; i++ )
						 {

						//get info on rep	
							//setMessage("Representative info found: " + (repsGottenBack+1));
							setMessage("Getting info for " + repsPutIn + " Representatives.");

							sponsors.push(repslist[i]);

							repsGottenBack++;

						if( repsGottenBack >= repsPutIn )
						{
							allCallsFinished = true; //not certain, but very likely at this point
							//setMessage("Calling Mapping");
							addJsonSponsors();
						}//Finished looping and non-zip call-backs should be done at this point
					}	//end for cycling through reps
					}	//if( distlist.districts.length != 0 )
					/****/
				}
			}
			});
		}
		
		
	function Sponsor() 
	{
   this.id = ""; 
   this.lastname = ""; //added
   this.firstname = ""; //added
   this.name = "";
   this.party = "";
   this.state = "";
   this.district = "";
   this.title = "";
   this.website = "";
   this.email = "";
   this.phone = "";
   this.fax = "";
   this.notes = "";
   this.support = "";
   this.zip = "";
   this.lat = "";
   this.lng = "";
   this.photo = ""; //added
   this.gender = ""; //added
	}
		function sunToSponsor( aRep )
		{
			spons = new Sponsor();
		   spons.id = aRep.govtrack_id;	//entity_id; 
		   spons.lastname = aRep.lastname; 
		   spons.firstname = aRep.firstname;
		   spons.name = aRep.firstname + " " + aRep.lastname;
		   spons.party = aRep.party;
		   spons.state = aRep.state;	//state_abbreviation;
		   spons.district = aRep.district;
		   spons.title = aRep.title;
		   spons.gender = aRep.gender;
		   spons.website = aRep.URL;
		   if( aRep.email )
			   spons.email = aRep.email;
		   else
			   spons.email = aRep.webform;
		   spons.phone = aRep.phone;
		   spons.fax = "";
		   spons.notes = "";
		   if( aRep.lastname == "Conyers" )
			   spons.support = "sponsor";
		   else if( districtsponsor( aRep.state, aRep.district ))
			   	spons.support = "co-sponsor";
			   else
				   spons.support = "none";
		   spons.zip = aRep.zip;	//currZip;
		   spons.lat = aRep.lat;	//currLat;
		   spons.lng = aRep.lng;	//currLng;
		   spons.photo = "http://kyhealthcare.org/maps/images/111/" + aRep.bioguide_id + ".jpg";	//aRep.photo;	//add it in because we can!	//took it out because we had to!	//and back in again!
		   return spons;
		}
		
		//attach staggered coordinates into a sunlight rep object
		function injectStaggeredCoords( rep, lat, lng, i )
		{
			//alert( "i = " + i );
			//stagger icons so they don't overlay each other
			var offset = 0.1;
			var offsetMult = (Math.floor(i/4) + 1);	//multiply offsets when there are tons of reps
			offset *= (1.0 * offsetMult );
			switch( i % 4 )
			{
			case 1:
				rep.lat = parseFloat(lat) - offset;
				rep.lng = parseFloat(lng) - offset;
				break;
			case 2:
				rep.lat = parseFloat(lat) - offset;
				rep.lng = parseFloat(lng) + offset;
				break;
			case 3:
				rep.lat = parseFloat(lat) + offset;
				rep.lng = parseFloat(lng) - offset;
			break;
			default:
				rep.lat = parseFloat(lat) + offset;
				rep.lng = parseFloat(lng) + offset;
			}	
			return rep;
		}
