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

// Front Controller for all forms
var url = '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 = $F('email_address');
        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 = $F('to_email_address');
        var from_address = $F('from_email_address');
        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').innerHTML = errorMessage;
    return isValidEmail;

}

function validateContactUs() {
    var validForm = false;

    // Strip away all the script tags first
    document.getElementById('contact_name').value    = $F('contact_name').strip().stripScripts().stripTags();
    document.getElementById('contact_comment').value = $F('contact_comment').strip().stripScripts().stripTags();
    document.getElementById('contact_email_address').value = $F('contact_email_address').strip().stripScripts().stripTags();

    // Check to confirm the stipped down version is not empty
    if (!$F('contact_name').blank() &&
        !$F('contact_email_address').blank() &&
        !$F('contact_comment').blank()) {
            
            // Verify the email address is properly formatted
            if (validateEmailAddress('contactForm')) {
                validForm = true;
            }
    }

    if (!validForm) {
        var defaultContactName = "No Contact Name Entered";
        var defaultEmailAddress = "No Email Address Entered";
        var defaultComment = "No Comment Entered";

        var contactName = $F('contact_name');
        var emailAddress = $F('contact_email_address');
        var comment = $F('contact_comment').truncate(20);

        if (contactName === '') {
            contactName = defaultContactName;
        }

        if (emailAddress === '') {
            emailAddress = defaultEmailAddress;
        }

        if (comment === '') {
            comment = defaultComment;
        }

        var errorMessage = "We apologize for the inconvience, <br/>" +
               "but we were unable to save the information you entered:<br/><br/>" +
               "<span class='h2'>" +
               "Name: " + contactName+ "<br/>" +
               "Email Address: " + emailAddress + "<br/>" +
               "Comment: " + comment + "<br/>" +
               "</span><br/>" +
               "Please confirm the information and resubmit.";
           
       $('result').innerHTML = errorMessage;
    }
   
    return validForm;
}




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').innerHTML = processMessage;

    // Prepare data for ajax call
    var pageId        = $F('page_id');
    var email_address = $F('email_address');
    var pars =  'action_type=registerEmail&page_id=' + pageId + '&email_address=' + email_address;
    new Ajax.Request(url, {method: 'post',parameters: pars,onComplete: showResponse});
}

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').innerHTML = processMessage;

    // Prepare data for ajax call
    var pageId             = $F('page_id');
    var to_email_address   = $F('to_email_address');
    var from_email_address = $F('from_email_address');
    var pars =  'action_type=tellFriend&page_id=' + pageId + '&to_email_address=' + to_email_address + '&from_email_address=' + from_email_address;
    new Ajax.Request(url, {method: 'post',parameters: pars,onComplete: showResponse});
}

function submitContactUsForm() {
    if (!validateContactUs()) {
        showThankYouMessage();
        showThankYouCloseMessage();
        return;
    }

    var contact_name  = $F('contact_name');
    var email_address = $F('contact_email_address');
    var comment       = $F('contact_comment');
    var pars =  'action_type=contactUs&contact_name=' + contact_name + '&email_address=' + email_address + '&comment=' + comment;
    new Ajax.Request(url, {method: 'post',parameters: pars,onComplete: showResponse});
}


function showResponse(originalRequest) {
    $jsonResponse = originalRequest.responseText.evalJSON();
    if ($jsonResponse.responseLocation == 'result') {
        $('result').innerHTML = $jsonResponse.responseText;
        showThankYouMessage();
        showThankYouCloseMessage();
    }
}

function showThankYouCloseMessage() {
    $('resultClose').appear({duration: 0.35 });
}

function showThankYouMessage() {
    $('thankyoulayer').appear({duration: 0.35 });
}

function closeThankYouMessage() {
    $('thankyoulayer').fade({duration: 0.50 });
    $('resultClose').fade({duration: 0.35 });
}


