﻿String.prototype.trim = function ()
{
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
String.prototype.convertQuotes = function()
{
    return this.replace(/"/g,"'");
}

jQuery().ready( function()
{
    $("#ctl00_btn_signUp").click(function(e)
    {
        e.preventDefault();
        var errorMsg = "";
        var firstname = $("#ctl00_txt_firstname").val().trim();
        var surname = $("#ctl00_txt_surname").val().trim();
        var email = $("#ctl00_txt_email").val().trim();
        
        if(!(firstname.length > 0))
        {
            errorMsg += "<li>Please enter your first name</li>"
        }
        if(!(surname.length > 0))
        {
            errorMsg += "<li>Please enter your surname</li>"
        }
        if(!(email.length > 0))
        {
            errorMsg += "<li>Please enter your email address</li>"
        }
        else if(!(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email)))
        {
            errorMsg += "<li>Please enter a valid email address</li>"
        }
        if(errorMsg.length > 0)
        {
            errorMsg = "<ul>" + errorMsg + "</ul>";
            $("#subscribe-message").html(errorMsg);
            return;
        }
        
        $.ajax(
        {
            type: "POST",
            url: "/WebService.asmx/Subscribe",
            data: '{"firstname":"' + firstname + '","surname":"' + surname + '","email":"' + email + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                $(container).append("<p>Sorry, due to an error your comment could not be submitted. Please try again.</p>");
            },
            success: function(msg)
            {
                // Clear textarea
                $("#ctl00_txt_firstname").val("");
                $("#ctl00_txt_surname").val("");
                $("#ctl00_txt_email").val("");
                // Load comments
                if((msg) == "true")
                {
                    $("#subscribe-message").text("Thanks for subscribing.");
                }
                else if(msg == "false")
                {
                    $("#subscribe-message").text("Sorry, there was a problem subscribing you to the mailing list. Please try again.");
                }
                else if(msg == "subscribed")
                {
                    $("#subscribe-message").text("You are already subscribed to the mailing list.");
                }
            }
        });
    });
});