﻿var rattikin = window.rattikin || {};

rattikin.contactUs = (function() {
            
    function submitContactForm() {
        if (validate()) {
            $('#contact-status').html('Sending...').css('color', 'red').show();
            wsRattikin.submitContactForm($('#tbName').val(), $('#tbEmail').val(), $('#tbPhone').val(), $('#taQuestion').val(), submitSuccess, submitFailure);
        }
        return false;
    }
    
    function validate() {
        var error = '';
        
        if ($('#tbName').val().length === 0) { error += 'Please enter your name.<br />'; $('#tbName').focus(); }
        if ($('#tbEmail').val().length === 0) { 
            if (error === '') { $('#tbEmail').focus(); }
            error += 'Please enter your email address.<br />';     
        }
        else if (!validateEmail($('#tbEmail').val())) {
            if (error === '') { $('#tbEmail').focus(); }
            error += 'Pleae enter a valid email address.<br />'; 
        }
        if ($('#tbPhone').val().length === 0) { 
            if (error === '') { $('#tbPhone').focus(); }
            error += 'Please enter your phone number.<br />'; 
        }
        if (($('#taQuestion').val() === '') || ($('#taQuestion').val() === 'Please type your question here.')) {
            if (error === '') { $('#taQuestion').val(''); $('#taQuestion').focus(); }
            error += 'Please enter your question or comment.'; 
        }
        
        if (error !== '') {
            $('#contact-status').html(error).css('color', 'red').show();
            return false;
        }
        else {
            return true;
        }
    }
    
    function validateEmail(email) {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return filter.test(email);
    }
    
    function submitSuccess(result) {
        if (result) {
            $('#contact-form').hide();
            $('#contact-thanks').show();
            $('#contact-form :input:not(:button)').each(function(i, el) {
                $(el).val('');
            });
            $('#contact-status').html('').hide();
        }
    }
    
    function submitFailure(error) {
        $('#contact-status').html('An error occured. Please try again.').css('color', 'red').show();
    }
    
    return {
        init: function() {
            $('#btnContactSubmit').click(submitContactForm);
            $('#taQuestion').blur(function() { 
                if ($('#taQuestion').val() === '') {
                    $('#taQuestion').val('Please type your question here.').css('color', '#666666');
                }
            });
            $('#taQuestion').focus(function() { 
                if ($('#taQuestion').val() === 'Please type your question here.') {
                    $('#taQuestion').val('').css('color', 'black');
                }
            });
        }
    };
    
})();

$(function() {
    rattikin.contactUs.init();
});
