/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

// Front Controller for all forms
var url = 'http://www.charlottetarantola.com/controller.php';

var processMessage = "Please be patient while we process your request<br/><br/><img src='/images/loading.gif'>";


function validateEmailAddress(formType) {
    
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var isValidEmail = true;
    var errorMessage;
    var defaultEmailMessage = 'No Email Address Entered';

    if (formType === 'regForm') {
        var address = $('#email_address').val();
        if(!reg.test(address)) {
            if (address === '') {
                address = defaultEmailMessage;
            }
            isValidEmail = false;
            errorMessage = "We apologize for the inconvenience, <br/>" +
                           "but we were unable to save the email address you entered:<br/><br/>" +
                           "<span class='h2'>" + address + "</span><br/><br/>" +
                           "Please confirm your email and resubmit.";
        }
    }

     if (formType === 'friendForm') {
        var to_address   = $('#to_email_address').val();
        var from_address = $('#from_email_address').val();
        if(!reg.test(to_address)) {
            if (to_address === '') {
                to_address = defaultEmailMessage;
            }
            isValidEmail = false;
            errorMessage = "We apologize for the inconvenience, <br/>" +
                           "but we were unable to save the email address you entered:<br/><br/>" +
                           "<span class='h2'>To: " + to_address + "</span><br/><br/>" +
                           "Please confirm your email and resubmit.";
        }

        // Only check if the TO address is invalid
        if(!reg.test(from_address) && isValidEmail) {
            if (from_address === '') {
                from_address = defaultEmailMessage;
            }
            isValidEmail = false;
            errorMessage = "We apologize for the inconvenience, <br/>" +
                           "but we were unable to save the email address you entered:<br/><br/>" +
                           "<span class='h2'>From: " + from_address + "</span><br/><br/>" +
                           "Please confirm your email and resubmit.";
        }
    }

    $('#result').html(errorMessage);
    return isValidEmail;

}


function submitEmailRegForm() {

    // Validate email on client side prior to
    // sending it to the server side
    if (!validateEmailAddress('regForm')) {
        showThankYouMessage();
        showThankYouCloseMessage();
        return;
    }

    // If it passes client side message, so a processing
    // message during the ajax call
    showThankYouMessage();
    $('#result').html(processMessage);

    // Prepare data for ajax call
    var pageId        = $('#page_id').val();
    var email_address = $('#email_address').val();
    var pars =  'action_type=registerEmail&page_id=' + pageId + '&email_address=' + email_address + '&callback=?';
    postToRemote(pars);
}

function submitTellAFriendForm() {

    // Validate email on client side prior to
    // sending it to the server side
    if (!validateEmailAddress('friendForm')) {
        showThankYouMessage();
        showThankYouCloseMessage();
        return;
    }

    // If it passes client side message, so a processing
    // message during the ajax call
    showThankYouMessage();
    $('#result').html(processMessage);

    // Prepare data for ajax call
    var pageId             = $('#page_id').val();
    var to_email_address   = $('#to_email_address').val();
    var from_email_address = $('#from_email_address').val();
    var pars =  'action_type=tellFriend&page_id=' + pageId + '&to_email_address=' + to_email_address + '&from_email_address=' + from_email_address + '&callback=?';
    postToRemote(pars);
}

function postToRemote(pars) {
    jQuery.getJSON(url + '?' + pars, function(data) {showResponse(data);});
}


function showResponse(originalRequest) {
    $('#result').html(originalRequest.responseText);
    showThankYouMessage();
    showThankYouCloseMessage();
}

function showThankYouCloseMessage() {
    $('#resultClose').show();
}

function showThankYouMessage() {
    $('#thankyoulayer').show();
}

function closeThankYouMessage() {
    $('#thankyoulayer').fadeOut('slow', function () {this});
    $('#resultClose').fadeOut('slow', function () {this});
}


