(function(a){a.fn.forms=function(b){var c="#"+b.State.FormName,d=a(window);a.ajaxSetup({type:"POST",url:"CoreService.asmx/FormEvents",contentType:"application/json; charset=utf-8",dataType:"json",error:function(a){alert(a.status+" "+a.statusText)}});function h(){var d=a(c);a('<div id="'+b.State.FormName+'-loader">').css({position:"absolute",width:"200px",height:"100px",background:"white url(images/ajax-loader2.gif) no-repeat scroll center center",border:"solid 1px black",font:"bold 14px/14px Arial",textAlign:"center",padding:"15px 0px 0px 0px",left:Math.round(d.width()/2-200/2),top:Math.round(d.height()/2-100/2)}).html("Please Wait...").appendTo(c+":last")}function g(){a(c+"-loader").css("display","none")}function e(g){var c=new stringBuilder;c.append('<div id="');c.append(b.State.FormName);c.append('-popup" class="form-popup-wrap">');c.append('<div class="form-popup-title">');c.append('<div class="form-popup-title-text">');c.append(b.Title);c.append("</div>");c.append('<div class="form-popup-title-close"><a href="#">Close</a></div>');c.append("</div>");c.append('<div class="form-popup-content">');c.append(g);c.append("</div>");c.append("</div>");a(c.toString()).appendTo("form");!b.IsCoupon&&a('<div class="form-popup-overlay" />').appendTo("body").css({left:"0px",top:"0px",width:"100%",height:a(document).height(),opacity:.5});a(".form-popup-title-close a").click(function(d){d.preventDefault();a(".form-popup-wrap").remove();a(".form-popup-overlay").remove();var b=location.href,c=b.indexOf("/confirm");if(c!=-1){b=b.substring(0,c);location.href=b}});var e=a(".form-popup-wrap");if(b.IsCouponPrint){a('<div class="form-popup-overlay" />').appendTo("body").css({left:"0px",top:"0px",width:"100%",height:a(document).height()});e.css({left:"0px",top:"0px"});return}if(b.IsCoupon&&b.DivOffsetLeft>0&&b.DivOffsetTop>0)e.css({left:b.DivOffsetLeft,top:b.DivOffsetTop});else e.css({left:Math.round(d.width()/2-e.width()/2),top:e.height()>d.height()?0:Math.round(d.height()/2+d.scrollTop()-e.height()/2)});!b.IsCoupon&&d.resize(function(){a('<div class="form-popup-overlay" />').css("height",a(document).height());e.css({left:Math.round(d.width()/2-e.width()/2),top:e.height()>d.height()?0:Math.round(d.height()/2+d.scrollTop()-e.height()/2)})});!b.IsConfirm&&f()}function f(){var f=false;a.forms_hookup_requiredFields(c,b.RequiredFields,b.DisplayTemplate);b.DisplayTemplate==="Label Field"&&a.forms_hookup_labelFields(c);if(b.Makes!=null){var e=a('select[name="Make"]');if(e.length>0){a.each(b.Makes,function(){var a=document.createElement("OPTION");a.text=this;a.value=this;if(b.SelectedMake==this)a.selected=true;e[0].options.add(a)});var d=a('select[name="Model"]');if(b.SelectedMake!=""){b.State.MakeName=b.SelectedMake;b.State.EventName="makeChange";a.ajax({data:"{ 'state': "+a.toJSON(b.State)+" }",success:function(c){d.html('<option value="">&lt;Select a Model&gt;</option>');a.each(c.d.Html.split(","),function(){var a=document.createElement("OPTION");a.text=this;a.value=this;if(b.SelectedModel==this)a.selected=true;d[0].options.add(a)})}})}e.change(function(e){var c=e.target;b.State.MakeName=c.value;b.State.EventName="makeChange";a.ajax({data:"{ 'state': "+a.toJSON(b.State)+" }",success:function(b){d.html('<option value="">&lt;Select a Make&gt;</option>');a.each(b.d.Html.split(","),function(){var a=document.createElement("OPTION");a.text=this;a.value=this;d[0].options.add(a)})}})})}}a(c+" .submit").click(function(d){d.preventDefault();if(f)return;if(a.forms_validateForm(c,b.RequiredFields)){h();f=true;a.each(b.State.Sections,function(d,b){a.each(b.Elements,function(e,d){if(d.Value===""||d.Value===null){var b=a(c+' :input[name="'+d.Name+'"]');d.Value=b.attr("title")===b.val()?"":b.val();if(b[0].type==="radio"||b[0].type==="checkbox"){b=a(c+' :input[name="'+d.Name+'"]:checked');if(b.length>1){d.Value="";b.each(function(a){if(a>0)d.Value+=", ";d.Value+=this.value})}else d.Value=b.val()}}})});b.State.EventName="send";var e=a.toJSON(b.State);a.ajax({data:"{ 'state': "+a.toJSON(b.State)+" }",success:function(h){var f=h.d;if(f.EventName==="success"){var e=b.IsCoupon?"confirm-coupon":"confirm-"+b.State.FormName,d=location.href;if(d.indexOf("vehicleScroll")!=-1)d=d.replace("vehicleScroll",e);else if(d.indexOf("confirm")===-1)if(d.indexOf("?s=")!=-1)d=d+"&"+e;else d=d.endsWith("/")?d+"default.aspx/"+e:d+"/"+e;a.manualSubmit(d)}else{g();a(c+":first").before('<div class="response">'+f.Html+"</div>")}}})}})}if(b.FromLink){(b.State.EventName==="show"||b.IsConfirm)&&e(b.State.Html);a("a[id^=Forms-"+b.State.FormName+"]").click(function(c){c.preventDefault();c.stopPropagation();var d=this;b.State.EventName="show";a.each(d.id.split("~"),function(d,c){var a=c.split(":");switch(a[0]){case"make":b.SelectedMake=a[1];break;case"model":b.SelectedModel=a[1];break;case"vid":b.State.VehicleID=a[1];break;case"inv":b.State.InventoryID=a[1];break;case"els":b.State.Elements=a[1]}});a.ajax({data:"{ 'state': "+a.toJSON(b.State)+" }",success:function(a){b.State.InventoryID=a.d.InventoryID;b.State.Sections=a.d.Sections;e(a.d.Html)}})})}else if(b.IsCoupon)e(b.CouponBody);else!b.IsConfirm&&f();return this}})(jQuery)
