﻿$(document).ready(function () {

    //alert('hello');

    //Tabs
    $('.tab').hide();
    $('.tab:first').show();
    $('#tab-controls li').stop().click(function () {
        switch_tabs($(this));
    });

    function switch_tabs(obj) {

        if ($(obj).hasClass("selected")) {

            //Already active, do nothing

        } else {

            $('.tab').hide();
            $('#tab-controls li').removeClass('selected');

            var tabID = obj.attr("rel");
            $('#' + tabID).fadeIn();
            obj.addClass('selected');

        }

    }


    //Accordion
    function initAccordion() {

        var eventControl = $('#schedule .control');
        var eventDetails = $('#schedule .eventDetails');

        //$('#schedule li .event:last').css('margin', '0px');
        eventDetails.hide();

        eventControl.click(function () {

            if ($(this).hasClass('titleExpand')) {

                $(this).removeClass('titleExpand');
                $(this).addClass('titleCollapse');
                $(this).next().stop(true, true).slideDown();

            } else {

                $(this).removeClass('titleCollapse');
                $(this).addClass('titleExpand');
                $(this).next().stop(true, true).slideUp();

            }

        });

        var expandAll = $('#schedule .expandAll');
        var collapseAll = $('#schedule .collapseAll');

        expandAll.click(function () {

            eventDetails.stop(true, true).slideDown();
            $('.titleExpand').removeClass('titleExpand');
            $('.control').addClass('titleCollapse');

        });

        collapseAll.click(function () {

            eventDetails.stop(true, true).slideUp();
            $('.control').addClass('titleExpand');
            $('.titleCollapse').removeClass('titleCollapse');

        });

    }

    //Init. Accordion function
    initAccordion();

    $('#success-message').hide();

    $("#submit-contact").click(function () {

        var name = $("input#name").val();
        var phone = $("input#phone").val();
        var email = $("input#email").val();
        var subject = $("#subject:selected").val();
        var message = $("textarea#message").val();

        spamProtect = $('#validate-user').val();

        if (spamProtect == 6) {

            if (name == "") {
                alert('Please enter your name!');
                return false;
            }

            if (phone == "") {
                alert('Please enter your phone number!');
                return false;
            }

            function isValidEmailAddress(emailAddress) {
                var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
                return pattern.test(emailAddress);
            };

            var userEmail = $('#email').val();

            if (email == "") {
                alert('Please enter a valid email address!');
                return false;
            }

            if (!isValidEmailAddress(userEmail)) {
                alert('Please enter a valid email address!');
                return false;
            }

            if (message == "") {
                alert('Please leave me a message if your going to get in contact');
                return false;
            }

            var dataString = 'name=' + name + '&email=' + email + '&phone=' + phone + '&message=' + message;
            //alert(dataString);

            $.ajax({
                type: "POST",
                url: "bin/formProcess.php",
                data: dataString,
                success: function () {
                    $('#contactPatti').remove();
                    $('#success-message').fadeIn();
                }
            });
            return false;

        } else {
            alert('Sorry but you did not pass the spam protection test, please enter the answer to the math problem above the drop down to submit this form!');
            return false;

        }

    });

});
