﻿/************************************** Splendid *************************************
* Creation Date:    25th March 2009
* Created By:       Steve
* Edited ----------------------------------------------------------------------------
*		By:	    On:
* Description -----------------------------------------------------------------------
*       This file contains the functions for sending a page to a friend
*       Requires jQuery to be included...
*
*       sendMsg()           // Controls the email sending.
*       validateForm()      // Validates the form and displays errors if there are any.
*       checkEmail()        // Does a basic check that the email could be valid  
*       checkAllEmail()     // Loops through a comma delimited list of emails to check they're all valid
*       
**************************************************************************************/

/********************************** Global Variables *********************************/

var fromEmail = '';
var toEmail = '';
var name = '';
var msg = '';
var copy = '';

/************************************* Functions *************************************/

/****
* Controls the email sending.
****/
function sendMsg() {
    var success = true;

    if (!validateForm())
        return;

    $.get('/emailFriend.aspx', {
        to: escape(toEmail),
        from: escape(fromEmail),
        name: escape(name),
        msg: escape(msg),
        page: escape(document.title),
        copy: escape(copy),
        url: escape(location.href),
        content: escape($('#content .openingContent').html()),
        js: 'true'
    }, function(data) {
        setTopOffset();
        $(popupID).fadeOut('fast', function() {
            $(popupID).remove();
            $('body').append($(data));
            $(popupID).css({
                left: $(window).width() / 2 - ($(popupID).width() / 2) + 'px',
                top: ((($(window).height() / 2) + topOffset) - ($(popupID).height() / 2)) + 'px',
                position: "absolute"
            }).fadeIn('fast');
        });
    });
}

/****
 * Validates the form and displays errors if there are any.
 ****/
function validateForm() {
    var success = true;
        
    fromEmail = $('#FromEmail').val();
    toEmail = $('#ToEmail').val();
    name = $('#Name').val();
    msg = $('#Msg').val();
    copy = $('#Copy').attr('checked') ? 1 : 0;

    // Validate the form input fields
    if (toEmail == '') {
    $('#ToErr').slideDown('slow');
        success = false;
    } else {
        if (checkAllEmail(toEmail)) {
            $('#ToErr').slideUp('slow');
        } else {
            $('#ToErr').slideDown('slow');
            success = false;
        }
    }

    if (fromEmail == '') {
        $('#FromErr').slideDown('slow');
        success = false;
    } else {
        if (checkEmail(fromEmail)) {
            $('#FromErr').slideUp('slow');
        } else {
            $('#FromErr').slideDown('slow');
            success = false;
        }
    }

    if (name == '') {
        $('#NameErr').slideDown('slow');
        success = false;
    } else {
        $('#NameErr').slideUp('slow');
    }

    if (msg.length > 1000) {
        $('#MsgErr').slideDown('slow');
        success = false;
    } else {
        $('#MsgErr').slideUp('slow');
    }

    return success;
}

/****
* Does a basic check that the email could be valid
****/
function checkEmail(email) {
    var reg = new RegExp("[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1,}?[0-9a-zA-Z]");
    return email.match(reg);
}

/****
 * Loops through a comma delimited list of emails to check they're all valid
 ****/
function checkAllEmail(email) {
    if (email.indexOf(',') > -1) {
        var emails = email.split(',');
        for (var i = 0; i < emails.length; i++) {
            if (!checkEmail(emails[i])) {
                return false;
            }
        }
        return true;
    } else {
        return checkEmail(email);
    }
}
