$(document).ready(function () {

    $("#news").newsticker();
    $('ul.nav li ul').removeClass('ddmenu');
    $("#testimonial").addClass('hide');
    $("#newsbox").removeClass('hide');

    $("a.fancybox").fancybox({
        'titlePosition': 'over'
    });


    $("a.capabilitiesFancybox").fancybox({
        'autoDimensions': false,
        'autoScale': false,
        'width': 500,
        'height': 'auto'
    });




    $('ul.nav').superfish();




    //Email box blur//


    var inputValue;
    var errorFlag;

    $('#softwareSignup form input.textInput').focus(function () {

        if ($(this).hasClass("switched")) {

            $(this).attr("value", "");

        } else {

            inputValue = $(this).val();

            $(this).attr("value", "");
            $(this).removeClass("nonFocus");

            if ($(this).hasClass("error")) {
                $(this).removeClass("error");
                errorFlag = true;
            }

        }

    });

    $('#softwareSignup form input.textInput').blur(function () {

        if ($(this).val() !== "" || $(this).hasClass("switched")) {

            $(this).removeClass("switched");
            $(this).addClass("switched");

        } else {

            $(this).val(inputValue);
            $(this).addClass("nonFocus");

            if (errorFlag) {
                $(this).addClass("error");
            }

        }

    });



    /* attach a submit handler to the form */
    $("#softwareSignup").submit(function (event) {

       
        
        var valid = true;

        /* stop form from submitting normally */
        event.preventDefault();

        if (!isValidEmailAddress($("#emailAddress").val())) {

            valid = false;

            $("#emailAddress").addClass("error");
            $(this).removeClass("switched");

        }


        var $inputs = $('#softwareSignup :input.textInput');

        $inputs.each(function () {

            if (!$(this).hasClass("switched") || $(this).hasClass("error")) {

                $(this).addClass("error");
                $(this).removeClass("switched");
                valid = false;

            }

        });


        if (valid) {

            

            /* get some values from elements on the page: */

            var dataString = $("#softwareSignup form").serialize();





            $.ajax({
                type: "POST",
                url: "../SoftwareSignup.aspx",
                async: false,
                data: dataString,
                success: function (html) {

                    //This means the form isn't fine
                    if (html.indexOf("Success=True") === -1) {

                        thankYouText = "<h2>Error</h2> <p class='ptop bold'>Something went wrong, please email <a href='mailto:sales@xpect-software.com'>sales@xpect-software.com</a> to register your interest</p>";

                        $("#sofwareSignup").fadeOut().remove();
                        $("#responseText").append(thankYouText);
                        $("#responseText").fadeIn();


                    } else {

                        thankYouText = "<h2>Success</h2> <p class='ptop bold'>Thanks, we'll be in touch.</p>";

                        $("#sofwareSignup").fadeOut().remove();
                        $("#responseText").append(thankYouText);
                        $("#responseText").fadeIn();
                    }

                }
            });

            return false;

        }




    });




});



function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([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);
    return pattern.test(emailAddress);
};

