/*
* JavaScripts for perfectchoicefunerals.com
*
*
* Date: 2008-12-05
* Author: Steve Taylor
*
* Amended: 2009-05-18
* 1. Merged other jscripts into this one so they fall under the global ns.
* 2. Fixed accordion issue.
*
* Amended: 2009-06-15
* 1. Fixed contact form validation
*
* Amended: 2009-06-17
* 1. Updated Contact Form functions to validate new contact form correctly.
*/

var PerfectChoice = window.PerfectChoice || {}; PerfectChoice.Common = (function() {
    return { handleError: function(msg) { alert(msg); }, hideFocus: function() { $("a").click(function(e) { this.blur(); }); }, textResize: (function() {
        var lastSize; function write(name, value, days) {
            var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 86400000)); var expires = "; expires=" + date.toGMTString(); }
            document.cookie = name + "=" + value + expires + "; path=/";
        }; function read(name) {
            name += "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(name) == 0) return c.substring(name.length, c.length); }
            return null;
        }; return { init: function(selector, defaultSize) { lastSize = defaultSize; $(selector).click(function() { write('size', this.className); PerfectChoice.Common.textResize.setTextSize(this.className); return false; }); PerfectChoice.Common.textResize.setTextSize(this.className); }, setTextSize: function(defaultSize) { $("body").removeClass(lastSize); lastSize = read('size') || defaultSize; $("body").addClass(lastSize); } }
    })()}
    })(); PerfectChoice.Search = function() {
        var defaultWatermark = ""; return { init: function(elem, watermark) { defaultWatermark = watermark; $("#" + elem).focus(this.hideWatermark); $("#" + elem).blur(this.showWatermark); }, hideWatermark: function(e) {
            if (this.value === defaultWatermark)
                this.value = ""; this.className = "focus";
        }, showWatermark: function(e) { if (this.value === "") { this.value = defaultWatermark; this.className = "field"; } } }
        } (); PerfectChoice.SearchResults = function() { return { init: function() { $("select.size").change(function() { location.href = $.query.set("PageSize", this.value).set("Page", 1); }); } } } (); PerfectChoice.Details = function() {
            var map, marker, markerText, routeFinder, route, directions; var maxZIndex = 1000; function initialise(mapElem, routeElem, showWidgets) {
                directions = routeElem; map = MMFactory.createViewer(mapElem); map.setMapType(MM_WORLD_BEHYBRID); map.setOption("mousewheel:wheelup", "zoomin"); map.setOption("mousewheel:wheeldown", "zoomout"); if (showWidgets) { map.addWidget(new MMMapTypeWidget()); }
                routeFinder = MMFactory.createRouteRequester(routeLoaded, map);
            }
            function createStepMarker(location, instruction, text, zindex) { var marker = map.createMarker(location, { zIndex: zindex, "text": text }); marker.setInfoBoxContent("<p>" + instruction + "<" + "/p>"); }
            function showRoute(postcodeStart, postcodeEnd) { route = new MMRoute(addLocation(postcodeStart, postcodeEnd)); routeFinder.request(route); }
            function addLocation(postcodeStart, postcodeEnd) { var locations = []; locations.push(new MMLocation(new MMAddress({ postal_code: postcodeStart, country_code: "GB" }))); locations.push(new MMLocation(new MMAddress({ postal_code: postcodeEnd, country_code: "GB" }))); return locations; }
            function routeLoaded() { if (route.error_code) { PerfectChoice.Common.handleError(route.error_code + ": " + route.error_explanation); } else { map.goToPosition(map.getAutoScaleLocation(route.bounds)); displayStages(route); for (var i = 0, l = route.polyLine.length; i < l; ++i) { map.addOverlay(route.polyLine[i]); } } }
            function displayStages(route) {
                var currentStep = 1; var stages = route.stages; var div = document.createElement("div"); div.className = "info"; for (var count = 0; count < stages.length; count++) {
                    var stageSummary = ""; stageSummary += "Driving Distance: <span>" + stages[count].distance.miles + " mile(s)</span>"; stageSummary += "<br />Estimated Travel Time: <span>"; if (stages[count].duration.days > 0) { stageSummary += stages[count].duration.days + " day(s) "; }
                    if (stages[count].duration.hours > 0) { stageSummary += stages[count].duration.hours + " hour(s) "; }
                    if (stages[count].duration.minutes > 0) { stageSummary += stages[count].duration.minutes + " minute(s) "; }
                    var p = document.createElement("p"); p.innerHTML = stageSummary; p.className = "Summary"
                    div.appendChild(p); directions.appendChild(div); var ol = document.createElement("ol"); ol.id = "stage_" + count; ol.className = "directions"; var steps = stages[count].steps; for (var stepCount = 0; stepCount < steps.length; stepCount++) {
                        var text = currentStep; var zindex = maxZIndex - currentStep + 1; if (count == 0 && stepCount == 0) { text = "A"; }
                        if (count == stages.length - 1 && stepCount == steps.length - 1) { text = "B"; zindex = maxZIndex; }
                        var instruction = steps[stepCount].instruction; var roadname = steps[stepCount].road_name; var roadnumber = steps[stepCount].road_number; if (roadname && roadnumber) { instruction += " " + roadname + " (" + roadnumber + ") "; } else if (roadname) { instruction += " " + roadname + " "; } else if (roadnumber) { instruction += " " + roadnumber + " "; }
                        var distance = ""; if (steps[stepCount].distance.miles > 0) { distance += steps[stepCount].distance.miles + " mile(s) "; }
                        if (distance != "") { distance = "" + distance }; var sp = document.createElement("span"); sp.className = "instruction"; sp.innerHTML = instruction; var sp2 = document.createElement("span"); sp2.className = "distance"; sp2.innerHTML = distance; var li = document.createElement("li"); li.appendChild(sp); li.appendChild(sp2); ol.appendChild(li); createStepMarker(steps[stepCount].start_point, instruction, text, zindex); ++currentStep;
                    }
                    directions.appendChild(ol); var a = document.createElement("a"); a.innerHTML = "<< Back to search results"; a.href = "javascript:history.go(-1)"; var p = document.createElement("p"); p.className = "backtoresults"; p.appendChild(a); directions.appendChild(p);
                }
                directions.style.display = "block"; var copyright = ""; if (route.copyright) { copyright += "<span>Copyright</span>: " + route.copyright; }
                if (route.disclaimer) {
                    copyright += " <span>Disclaimer</span>:  <a href=\"" + route.disclaimer + "\">"
+ route.disclaimer + "<" + "/a>";
                }
                var p = document.createElement("p"); p.innerHTML = copyright; p.className = "mapviewer_copyright"
                directions.appendChild(p);
            }
            return { showMap: function(mapElem, routeElem, postcodeStart, postcodeEnd) { initialise(mapElem, routeElem, true); showRoute(postcodeStart, postcodeEnd); } }
        } (); PerfectChoice.UsefulLinks = (function() { return { init: function() { $(".toggle").prev().wrapInner('<a href="#" class="toggleLink"></a>').children().click(function() { $(this).parent().next(".toggle").toggle("slow"); $(this).parent().toggleClass("open"); return false; }).parent().next(".toggle").hide(); $('.toggle.show').show('slow').prev().toggleClass('open'); } } })(); PerfectChoice.Contact = (function() {
            return { init: function(frm, submitButton) {
                $(".form fieldset .info-tooltip").toggleClass("hidden"); $(".form fieldset input, .form fieldset textarea").focus(function() { $(".form .info-tooltip").addClass("hidden"); $(this).parent().children(".info-tooltip").removeClass("hidden"); $(".form fieldset").removeClass("active-fieldset"); $(this).parent("fieldset").addClass("active-fieldset"); }); $(".form fieldset .brochure").toggleClass("hide"); $(".form fieldset .input-brochure").click(function() { $(".form fieldset .brochure").toggleClass("hide"); }); $(".form fieldset .something-else").toggleClass("hide"); $(".form fieldset .input-something-else").click(function() { $(".form fieldset .something-else").toggleClass("hide"); }); $(".form .salutation-other").hide(); $(".form #salutation option[value='other']").click(function() { $(".form .salutation-other").show(); $(".form label[for='salutation']").hide(); $(".form #salutation").hide(); $(".form #salutation-other").rules("add", { required: true }); $(".form #salutation").rules("remove", "required"); }); $(".form .focus").focus(); $("#" + frm).validate({ rules: { fullname: "required", telephone: { required: true, digits: true }, email: { required: true, email: true }, salutation: { required: true }, postaladdress: { required: true }, postcode: { required: true, postcode: true }, dob: { required: true, date: true} }, messages: { email: "Please enter a valid email address" }, onsubmit: false }); $.validator.setDefaults({ debug: false, success: "valid" }); $("#" + submitButton).click(function(event) {
                    PerfectChoice.Contact.validate(frm); if (!$("#" + frm).valid())
                        return false;
                });
            }, validate: function(frm) { $("#" + frm).validate().form() } }
            })(); $(function() { PerfectChoice.Common.hideFocus(); PerfectChoice.Common.textResize.init(".accessibility-features .text-size a", "textsize-normal"); });
