// JavaScript Document
  
 //Sidebar stuff
$(document).ready(function() {
    function ClearNewsletter() {
        var $news = $("#sidebar .newsletter");
        $news.find("label").text("Thank you for signing up to the Easy Weddings Newsletter. You should receive your first email soon.");
        $news.find("input, span.error").remove();
        setTimeout(function() { $news.slideUp(500); }, 5000);
    }

    function ShowRegions(control) {
        var active = control.find("ul.state li.active:first a")[0] || control.find("ul.state li:first a")[0];
        $(active).parent().addClass("active");

        var state = active.hash.substring(1);
        control.find("ul.regions li").removeClass("active");
        control.find("ul.regions li." + state).addClass("active");

    }

    $("#tab-regions, #RegionalListings").each(function() {
        var $control = $(this);
        $control.find("ul.state>li a").click(function() {
            $control.find("ul.state>li").removeClass("active");
            $(this).parent().addClass("active");
            ShowRegions($control);
        });
        ShowRegions($control);
    });

    $("#sidebar .email").focus(function() {
        if ($(this).val() == "Email address") $(this).val("");
    });

    $("#sidebar .newsletter #btnSubmitNewsletter").click(function() {
        var email = $("#sidebar .newsletter input.email").val();

        if (/(.*)@(.*)/.test(email)) {
            $.post("/myweb/ajax/sidebar_functions.asp", { action: 1, "email": email }, ClearNewsletter);
            _gaq.push(["_trackEvent", "Sidebar", "Newsletter - Subscribe", $("h1").text()]);
        } else {
            _gaq.push(["_trackEvent", "Sidebar", "Newsletter - Subscribe - Failure", $("h1").text()]);

            if (!$(this).siblings("span.error").length) {
                $(this).siblings("div.clear").after("<span class='error'>Please enter a valid email address.</span>")
            }

            $(this).siblings("span.error").css({
                "color": "red",
                "display": "block",
                "background-color": "#8cd98c"
            }).effect("highlight");
        }
        return false;
    });
    $("#sidebar .newsletter #btnSubmitNewsletter").click(function() {
        var $this = $(this);
        $.post("/myweb/ajax/update_user_details.asp", { EmailNotify: 1, update: 1 }, ClearNewsletter);
        _gaq.push(["_trackEvent", "Sidebar", "Newsletter - Subscribe", $("h1").text()]);
        return false;
    });

    $("#sidebar .directory a").click(function() {
        _gaq.push(["_trackEvent", "Sidebar", "Category - Browse", $(this).text()]);

    });

    $("#sidebar .relatedarticles a").click(function() {
        _gaq.push(["_trackEvent", "Sidebar", "Article - Related Article", $(this).text()]);
    });

    $(".side_nav_container a").click(function() {
        _gaq.push(["_trackEvent", "LeftSidebar", $(this).text()]);
    });

    $("#sidebar .planningtools a").click(function() {
        _gaq.push(["_trackEvent", "Sidebar", "Planning Tools", $(this).text()]);
    });

    $("#sidebar .item .wrap").tabs();

    $("#tab-requestform ul li.control select.requestCategory").change(function() {
        var option = $("#tab-requestform ul li.control select.requestCategory option:selected");
        var addressHeading = $("#sidebarAddressHeading");

        if (option && addressHeading) {
            if (option.attr('toggle') == 'True') {
                addressHeading.html(addressHeading.attr('bridesLocationHeading') + "&nbsp;<span class='required'>*</span>");
            }
            else {
                addressHeading.html(addressHeading.attr('weddingLocationHeading') + "&nbsp;<span class='required'>*</span>");
            }
        }
    });

    //var data = "Geelong Vic 3220|Geelong West Vic 3220|Gerringhap Vic 3212".split("|");
    //$("#tab-requestform ul li.control input.requestSuburb").autocomplete(data);
    //$("#tab-requestform ul li.control input.requestSuburb").autocomplete("http://localhost:51435/wedding/webservices/AutoComplete.asmx/GetCompletionListClassic", { dataType: "xml", datakey: "string", max: 50 });
    //$("#tab-requestform ul li.control input.requestSuburb").autocomplete("http://localhost:51435/inc/classes/get_suburbs.asp", { max: 50 });

    $("#tab-requestform ul li.control input.requestSuburb").autocomplete('/inc/classes/get_suburbs.asp', {
        minChars: 1,
        max: 50,
        dataType: 'json', 
        extraParams: {
            format: 'json' 
        },
        parse: function(data) {
            var parsed = [];
            data = data.users;

            for (var i = 0; i < data.length; i++) {
                parsed[parsed.length] = {
                    data: data[i],
                    value: data[i].address,
                    result: data[i].address
                };
            }

            return parsed;
        },
        formatItem: function(item) {
            return item.address;
        }
    });
});

 function MM_findObj(n, d) { //v4.01
     var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
         d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
     }
     if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
     for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
     if (!x && d.getElementById) x = d.getElementById(n); return x;
 }

 function MM_validateForm() { //v4.0
     var i, p, q, nm, test, num, min, max, errors = '', args = MM_validateForm.arguments;
     for (i = 0; i < (args.length - 2); i += 3) {
         test = args[i + 2]; val = MM_findObj(args[i]);
         if (val) {
             nm = val.name; if ((val = val.value) != "") {
                 if (test.indexOf('isEmail') != -1) {
                     p = val.indexOf('@');
                     if (p < 1 || p == (val.length - 1)) errors += '- ' + nm + ' must contain an e-mail address.\n';
                 } else if (test != 'R') {
                     num = parseFloat(val);
                     if (isNaN(val)) errors += '- ' + nm + ' must contain a number.\n';
                     if (test.indexOf('inRange') != -1) {
                         p = test.indexOf(':');
                         min = test.substring(8, p); max = test.substring(p + 1);
                         if (num < min || max < num) errors += '- ' + nm + ' must contain a number between ' + min + ' and ' + max + '.\n';
                     } 
                 } 
             } else if (test.charAt(0) == 'R') errors += '- ' + nm + ' is required.\n';
         }
     } if (errors) alert('The following error(s) occurred:\n' + errors);
     document.MM_returnValue = (errors == '');
 }
