function FareCalc() {window.FareCalc=this;this.map=null;this.geocoder=null;this.directions=null;this.thisForm=null;this.startPoint=null;this.endPoint=null;this.fareRates=null;this.basePoint=null;this.baseLat=45.456187;this.baseLon=-122.83543;this.load=function(){if(google.maps.BrowserIsCompatible()){map=new google.maps.Map2($('faresMap'));map.setCenter(new google.maps.LatLng(45.523875,-122.670399),10);map.addControl(new GSmallMapControl());geocoder=new google.maps.ClientGeocoder();directions=new google.maps.Directions(map);}};this.findFare=function(thisForm){FareCalc.thisForm=thisForm;FareCalc.startAddr=thisForm.start_addr.value;FareCalc.startCity=thisForm.start_city.value;FareCalc.startState=thisForm.start_state.value;FareCalc.endAddr=thisForm.end_addr.value;FareCalc.endCity=thisForm.end_city.value;FareCalc.endState=thisForm.end_state.value;FareCalc.fareRates=null;FareCalc.fareDistance=null;FareCalc.farePrice=null;FareCalc.fareDistanceInternal=null;FareCalc.farePriceInternal=null;FareCalc.getRates();return false;};this.getRates=function(){new Ajax.Request('fares_man.php',{method:'post',parameters:{action:'getRates',state:FareCalc.thisForm.start_state.value,city:FareCalc.thisForm.start_city.value},onSuccess:function(transport){FareCalc.fareRates=transport.responseText.evalJSON(true);FareCalc.calculateFare(FareCalc.thisForm);},onFailure:function(){alert('Sorry, an error has occurred. Please try re-wording your request.');}});};this.recordFare=function(){new Ajax.Request('fares_man.php',{method:'post',parameters:{action:'recordEstimate',startAddr:FareCalc.startAddr,startCity:FareCalc.startCity,startState:FareCalc.startState,endAddr:FareCalc.endAddr,endCity:FareCalc.endCity,endState:FareCalc.endState,fareDistance:FareCalc.fareDistance,farePrice:FareCalc.farePrice,fareDistanceTravel:FareCalc.fareDistanceInternal,farePriceTravel:FareCalc.farePriceInternal,fareRateBase:FareCalc.fareRates.rateBase,fareRatePerMile:FareCalc.fareRates.ratePerMile,fareRatePerMileTravel:FareCalc.fareRates.ratePerMileInternal},onSuccess:function(transport){},onFailure:function(){alert('Sorry, an error has occurred. Please try re-wording your request.');}});};this.calculateFare=function(){var startAddr='';if(FareCalc.startAddr)startAddr+=FareCalc.startAddr+', ';if(FareCalc.startCity)startAddr+=FareCalc.startCity+', ';if(FareCalc.startState)startAddr+=FareCalc.startState+', ';if(startAddr.length>2)startAddr=startAddr.substr(0,startAddr.length-2);var endAddr='';if(FareCalc.endAddr)endAddr+=FareCalc.endAddr+', ';if(FareCalc.endCity)endAddr+=FareCalc.endCity+', ';if(FareCalc.endState)endAddr+=FareCalc.endState+', ';if(endAddr.length>2)endAddr=endAddr.substr(0,endAddr.length-2);show_elm('sumStats');show_elm('startLatLng');show_elm('endLatLng');show_elm('faresDisclaimer');FareCalc.basePoint=new GLatLng(FareCalc.baseLat,FareCalc.baseLon);if(geocoder){geocoder.getLatLng(startAddr,function(point){if(!point){FareCalc.startPoint=null;$('sumStats').update('<strong>Fare Estimate:</strong><br /> <em>An error has occurred,<br /> please try again</em>');$('startLatLng').update('<strong>Pickup:</strong><br /> <em>Sorry, we couldn\'t find<br /> the pickup address</em>');} else{FareCalc.startPoint=point;map.setCenter(point,10);var marker=new google.maps.Marker(point,{title:startAddr});map.addOverlay(marker);$('startLatLng').update('<strong>Pickup:</strong><br /> Lat: '+point.lat()+'&deg;<br /> Lon: '+point.lng()+'&deg;');} geocoder.getLatLng(endAddr,function(point){if(!point){FareCalc.endPoint=null;$('sumStats').update('<strong>Fare Estimate:</strong><br /> <em>An error has occurred,<br /> please try again</em>');$('endLatLng').update('<strong>Destination:</strong><br /> <em>Sorry, we couldn\'t find<br /> the destination address</em>');} else{FareCalc.endPoint=point;var marker=new google.maps.Marker(point,{title:endAddr});map.addOverlay(marker);$('endLatLng').update('<strong>Destination:</strong><br /> Lat: '+point.lat()+'&deg;<br /> Lon: '+point.lng()+'&deg;');} if(FareCalc.startPoint&&FareCalc.endPoint) {var waypoints=new Array(FareCalc.basePoint,FareCalc.startPoint);directions.loadFromWaypoints(waypoints);GEvent.clearListeners(directions,'load');GEvent.addListener(directions,'load',function(){distance=directions.getDistance();FareCalc.fareDistanceInternal=distance.meters/1609.344;FareCalc.fareCostPickup=FareCalc.fareRates.ratePerMileInternal*distance.meters/1609.344;var waypoints=new Array(FareCalc.basePoint,FareCalc.endPoint);directions.loadFromWaypoints(waypoints);GEvent.clearListeners(directions,'load');GEvent.addListener(directions,'load',function(){distance=directions.getDistance();FareCalc.fareDistanceInternal+=distance.meters/1609.344;FareCalc.fareDistanceInternal=FareCalc.fareDistanceInternal.toFixed(2);FareCalc.fareCostPickup+=FareCalc.fareRates.ratePerMileInternal*distance.meters/1609.344;var waypoints=new Array(FareCalc.startPoint,FareCalc.endPoint);directions.loadFromWaypoints(waypoints);GEvent.clearListeners(directions,'load');GEvent.addListener(directions,'load',function() {distance=directions.getDistance();if(distance.meters>FareCalc.fareRates.longDistanceCutoff*1609.344||FareCalc.fareDistanceInternal>FareCalc.fareRates.longDistanceCutoff*1609.344) {FareCalc.farePrice=0;var farePriceOut='Please call for an estimate';} else {FareCalc.farePrice=Number(FareCalc.fareRates.rateBase) +(FareCalc.fareRates.ratePerMile*distance.meters/1609.344);FareCalc.farePrice=parseInt(FareCalc.farePrice*100)/100;FareCalc.farePrice+=FareCalc.fareCostPickup;FareCalc.farePrice=Math.round(FareCalc.farePrice).toFixed(2);var farePriceOut='Cost: $ '+FareCalc.farePrice;} FareCalc.fareDistance=(distance.meters/1609.344).toFixed(2);$('sumStats').update('<strong>Fare Estimate:</strong><br /> ' +farePriceOut +'<br /> Distance: '+FareCalc.fareDistance+' mi');FareCalc.recordFare();});});});} else {$('sumStats').update('<strong>Fare Estimate:</strong><br /> <em>An error has occurred,<br /> please try again</em>');}});});}};google.load("maps","2");google.setOnLoadCallback(this.load);} new FareCalc();