if(typeof google=="undefined"){alert("This feature requires the Google maps API")
}google.load("maps","2");var DeliveryEstimator=function(A){this.init(A)};DeliveryEstimator.prototype.init=function(A){var C=this;
var B={originsId:"de_origins",zipId:"de_zipText",submitId:"de_submit",resultsId:"de_results",zipServiceUrl:"/InterActRV/Supplements/ZipData/ZipData.asmx/GetInfo",loadingText:"Calculating amount...",origins:[],dealerInfo:{name:"",phone:"",icon:""}};
if(A&&typeof (A)!="string"){jQuery.extend(B,A)}this.Settings=B;this.Html=["","",""];
this.Status=0;this.Wait=null;this.Origin=this.originSelect(this.Settings.originsId);
this.DestZip=$("#"+this.Settings.zipId);this.FormSubmit=$("#"+this.Settings.submitId);
this.Results=$("#"+this.Settings.resultsId);this.Origins=this.Settings.origins;this.Dealer=this.Settings.dealerInfo;
this.DestZip[0].value=" Enter zip code";this.DestZip[0].onkeydown=function(D){if(C.IsEnter(D)){C.Estimate();
return false}};this.DestZip[0].onfocus=function(D){if(this.value==" Enter zip code"){this.value=""
}};this.DestZip[0].onblur=function(D){if(this.value==""){this.value=" Enter zip code"
}};if(this.DestZip[0].captureEvents){this.DestZip[0].captureEvents(Event.KEYPRESS)
}this.FormSubmit.bind("click",function(){C.Estimate();return false});window.onunload=GUnload
};DeliveryEstimator.prototype.originSelect=function(C){var A=this;var B=document.getElementById(C);
B.selection=function(){for(var E=0;E<this.options.length;E++){if(this.options[E].selected){var F=this.options[E].value;
for(var D=0;D<A.Origins.length;D++){if(A.Origins[D].zip=F){return A.Origins[D]}}}}return null
};return B};DeliveryEstimator.prototype.Timeout=function(){this.Results.html("The estimate could not be completed, because the Google mapping service did not respond.");
this.Wait=null};DeliveryEstimator.prototype.Estimate=function(){var B=this;if(this.Wait!=null){B.Results.html("Waiting for a previous request.").show();
return }this.Results.hide();this.Status=0;this.Html=["","",""];var C=escape(this.DestZip[0].value);
if(C.length==6){C=C.substr(0,3)+" "+C.substr(3,3)}var A=this.Origin.selection();if(A==null){B.Results.html("A shipping origin must be selected in order to calculate the estimated delivery cost.").show();
return }this.Results.html(this.Settings.loadingText).show();this.Wait=setTimeout(function(){B.Timeout()
},6000);this.GetZipInfo(A.zip,true);this.GetZipInfo(C,false);var D=new GDirections();
D.load(A.zip.replace("%20","")+" to "+C.replace("%20",""));GEvent.addListener(D,"load",function(){var E=Math.floor((this.getDistance().meters/1000/1.61));
var F=(E*(A.cpm));B.Html[2]="Miles: "+B.FormatNumber(E)+"<br/>Estimate: "+B.FormatCurrency(F);
B.Report(3)});GEvent.addListener(D,"error",function(){var E="The estimate could not be completed, because the Google mapping service returned an error. ("+this.getStatus().code+")";
switch(this.getStatus().code){case 602:E="No corresponding geographic location could be found for the specified address. This may be due to the fact that the address is relatively new, or it may be incorrect.";
break;case 603:E="The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons. ";
break;case 604:E="Google could not compute directions between the points mentioned in the query. This is usually because there is no route available between the two points, or because we do not have data for routing in that region.";
break}B.Html[2]=E;B.Report(3)})};DeliveryEstimator.prototype.GetZipInfo=function(zip,isOrigin){var base=this;
var params={zipcode:zip};var action=(isOrigin)?1:2;var label=(isOrigin)?"Origin: ":"Destination: ";
$.ajax({global:true,url:base.Settings.zipServiceUrl,type:"POST",timeout:0,contentType:"application/x-www-form-urlencoded",processData:true,async:true,data:params,dataType:"xml",success:function(data,status){var response=eval("("+data.documentElement.firstChild.nodeValue+")");
if(response.errorNum>0){base.Html[action-1]=response.result}else{base.Html[action-1]=label+response.zips[0].city+", "+response.zips[0].stateName
}base.Report(action)},error:function(data,status){base.Html[action-1]="Error <!--"+data.responseText+" -->";
base.Report(action)}})};DeliveryEstimator.prototype.Report=function(A){this.Status=this.Status+A;
if(this.Status>=6){clearTimeout(this.Wait);this.Wait=null;this.ShowResults()}};DeliveryEstimator.prototype.ShowResults=function(){this.Results.html(this.Html[0]+"<br/>"+this.Html[1]+"<br/>"+this.Html[2]).show()
};DeliveryEstimator.prototype.IsEnter=function(A){A=A||window.event;if(A.keyCode==13||A.which==13){return true
}return false};DeliveryEstimator.prototype.FormatCurrency=function(A){A=A.toString().replace(/\$|\%|\,/g,"");
if(isNaN(A)){A="0"}sign=(A==(A=Math.abs(A)));A=Math.floor(A*100+0.50000000001);cents=A%100;
A=Math.floor(A/100).toString();if(cents<10){cents="0"+cents}for(var B=0;B<Math.floor((A.length-(1+B))/3);
B++){A=A.substring(0,A.length-(4*B+3))+","+A.substring(A.length-(4*B+3))}return(((sign)?"":"-")+"$"+A+"."+cents)
};DeliveryEstimator.prototype.FormatNumber=function(A){A=A.toString().replace(/\$|\%|\,/g,"");
if(isNaN(A)){A="0"}sign=(A==(A=Math.abs(A)));A=Math.floor(A*100+0.50000000001);cents=A%100;
A=Math.floor(A/100).toString();if(cents<10){cents="0"+cents}for(var B=0;B<Math.floor((A.length-(1+B))/3);
B++){A=A.substring(0,A.length-(4*B+3))+","+A.substring(A.length-(4*B+3))}return(((sign)?"":"-")+A)
}
