﻿(function($) {
    // Create a plugin with the name 'watermark'
    $.fn.watermark = function(settings) {
        // Defaults
        var config = {
            watermarkClass: 'watermark',
            defaultText: 'type here...'
        };

        // merge the passed in settings with our default config
        if (settings) $.extend(config, settings);

        // Loop through the elements in the selector that we call the plug-in on
        this.each(function() {
            // Apply defaults to the box
            $(this).addClass(config.watermarkClass).val(config.defaultText);

            // Apply our focus and blur events
            // When we click on the field, we expect it to clear the field and remove the watermark
            $(this).focus(function() {
                $(this).filter(function() {
                    // Check to see if we have a blank field or the default text
                    return $(this).val() === "" || $(this).val() === config.defaultText;
                }).val("").removeClass(config.watermarkClass);
            });

            // When we click off of the field, we expect it to replace the watermark,
            // unless we have entered text
            $(this).blur(function() {
                $(this).filter(function() {
                    // Check to see if the field is blank
                    return $(this).val() === "";
                }).addClass(config.watermarkClass).val(config.defaultText);
            });
        });
    };
})(jQuery);


$(document).ready(function () {
    $("#sub-name").watermark({ defaultText: 'Name' });
    $("#sub-email").watermark({ defaultText: 'Email Address' });
    $("#sub-location").change(function () { var c = ($(this).val() == "-1") ? "#666" : "#111"; $(this).css({ color: c }); });
    $("#sub-subscribe").bind("click", subscribe);


    $("#contact").hover(function () { $("#contact ul").slideDown("fast"); }, function () { $("#contact ul").slideUp("fast"); });
});

function subscribe() {

    var name = $.trim($("#sub-name").val());
    var email = $.trim($("#sub-email").val());
    var location = $.trim($("#sub-location").val());

    if (name.length == 0 || name == "Name" || email.length == 0 || email == "Email Address" || location == -1) {
        alert("Please enter your name, email address and choose a location");
        return false;
    }
    $("#subscribe input, #subscribe select").attr("disabled", "disabled");
    $("#sub-subscribe").hide();
    $("#subscribe-result").text("Please Wait...");
    $("#subscribe-result").show();

    $.get("newsletter.axd", { "name": escape(name), "email": escape(email), "location": location }, subscribeResult);

}

function subscribeResult(data) {
  

    var r = $("#subscribe-result");
    if (data == "0")
    {
        r.css({color:"green"});
        r.html("Thanks, you have been subscribed");
        }
    else {
        r.css({color:"red"});
        r.html("Sorry, you were not subscribed.<br/>Please check your email address is entered correctly");
    }
    $("#subscribe input,#subscribe select").removeAttr("disabled");
    $("#sub-subscribe").show(); 
    
    
}

