﻿function getClassName(){
    var returnVal = "Not Found";
    $.each(classes, function() {
        if (this.Url == window.location.pathname) {
            returnVal = this.Name;
            return;
        }
    });
    return returnVal;
}

//Contact form
(function($) {
    //start the plugin call 
    $.fn.contactForm = function(options) {
        //set default options
        var defaults = {
            subject: 'Website contact',
            recievedMsg: 'Thank you for your information, we will contact you as soon as possible.',
            notRecievedMsg: 'Sorry but your message could not be sent, try again later'
        };
        //call in the default otions
        var options = $.extend(defaults, options);

        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][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
            return pattern.test(emailAddress);
        }

        //act upon the element that is passed in 
        return this.each(function(options) {
            var submitButton = $(this).find('input[type=button]');
            submitButton.click(function() {
                var form = {
                    firstName: $.trim($('#contact-firstname').val()),
                    lastName: $.trim($('#contact-lastname').val()),
                    email: $.trim($('#contact-email').val()),
					phone: $.trim($('#contact-phone').val()),
                    comments: $.trim($('#contact-comments').val()),
                    subject: $.trim($('#contact-subject').val())
                }
                var err = '';
                if (form.firstName == '') {
                    err += '\n - First name is required';
                }
                if (form.lastName == '') {
                    err += '\n - Last name is required';
                }
                if (form.email == '') {
                    err += '\n - Email is required';
                } else if (isValidEmailAddress(form.email) == false) {
                    err += '\n - Email is invalid';
                }
				if (form.phone == '') {
                    err += '\n - telephone number is required';
                }
                if (form.comments == '') {
                    err += '\n - Comment: please let us know how we can help you';
                }

                if (err != '') {
                    alert('Oops:' + err);
                    return
                }

                $.ajax({
                    type: "POST",
                    url: '/sendmail.php',
                    cache: false,
                    timeout: 5000,
                    data: ({ FirstName: form.firstName, LastName: form.lastName, Email: form.email, Phone: form.phone, Subject: form.subject, Comments: form.comments }),
                    beforeSend: function() {
                        submitButton.val('Sending...');
                        submitButton.attr('disabled', 'disabled');
                    },
                    success: function(data, status, request) {
                        this.reset();
                        $('#contact-form')
                           .after('<div id="contact-form-submitted">' + defaults.recievedMsg + '</div>')
                           .hide();
                    },
                    error: function(request, status, error) {
                        if (console) { 
                            console.log(request)
                        }
                        this.reset();
                        $('#contact-form')
                           .after('<div id="contact-form-error">' + defaults.notRecievedMsg + '</div>');
                        $('#contact-form-error').dblclick(function() {
                            $('#contact-form-error').html(request.status + " - " + request.statusText + '<br>' + request.responseText);
                        });
                    },
                    reset: function() {
                        submitButton.val('Submit');
                        submitButton.removeAttr('disabled');
                    }
                });
            });
        });
    }
    //end the plugin call 
})(jQuery);

$(document).ready(function() {

    //Set selected top menu
    $("#menu a[href='" + window.location.pathname.toLowerCase() + "']").addClass('active');
    //for subclasses
    if(window.location.pathname.toLowerCase().indexOf('classes/') != -1){
        $("#menu a[href='/classes/']").addClass('active');
    }
    //for pass
    if(window.location.pathname.toLowerCase().indexOf('membership/') != -1){
        $("#menu a[href='/membership/']").addClass('active');
    }

    //hookup facebox
    $('a[rel*=facebox]').facebox();

}); //end $(document).ready

