﻿function DoLogin() {
    $(dvLoginId).style.display = 'block';
    $(dvAddressBookId).style.display = 'none';
    $(txtEmailId).value = '';
    $(txtPasswordId).value = '';
    $('dvError').innerHTML = '';
}

function showList() {
    $('dvError').innerHTML = '';

    var mailServer;
    if ($(radGMailId).checked) {
        mailServer = 'GMail';
    }
    else if ($(radHotMailId).checked) {
        mailServer = 'HotMail';
    }
    else if ($(radYahooId).checked) {
        mailServer = 'Yahoo';
    }

    new Ajax.Request('http://localhost/aniboom/Pages/Application/GeneralPages/RetrieveAddressBook.aspx?email=' + $(txtEmailId).value + '&password=' + $(txtPasswordId).value + '&mailServer=' + mailServer,
    {
        parameters: '', method: 'get',

        onSuccess: function(transport) {
            if (transport.responseText != "FAILED" && transport.responseText != "") {
                $(dvLoginId).style.display = 'none';
                $(dvAddressBookId).style.display = 'block';
                $(dvAddressBookId).innerHTML = transport.responseText;
            }
            else {
                $('dvError').innerHTML = 'ERROR: Check your e-mail credentials and try again.';
            }
        },

        onFailure: function() {
            $('dvError').innerHTML = 'ERROR: Cannot connect to e-mail server, wait a few minutes and try again.';
        }
    });
}

function sendThisMovieClicked() {
    var textMessage = $(txtMessageId).value;

    if (($(txtMessageId).value.indexOf("<") > -1) || ($(txtMessageId).value.indexOf(">") > -1) || ($(txtMessageId).value.indexOf("&lt;") > -1) || ($(txtMessageId).value.indexOf("&gt;") > -1)) {
        return;
    }

    var typeId;

    if (typeof (playerAnimationID) != "undefined") // player page
    {
        typeId = playerAnimationID;
    }
    else if (typeof (animatedImgUrl) != "undefined") // animachines page
    {
        if (document.URL.indexOf("shapeshifter") > 0) {
            pageTracker._trackPageview("shapeshifter/emailsent/virtual");
        }
        typeId = animatedImgUrl;
    }
    else if (typeof (_user_id) != "undefined")  // zone page
    {
        typeId = _user_id;
    }
    else typeId = 74080;

    if (!validateMailForm($("emailTo").value, $(txtNameId).value)) {
        return;
    }

    new Ajax.Request('http://localhost/aniboom/Pages/Application/GeneralPages/SendMailToFriend.aspx?mailTo=' + $("emailTo").value + '&mailFrom=' + $(txtNameId).value + '&bodyText=' + $(txtMessageId).value + '&linkUrl=' + location.href + '&sendType=' + sendTypeId + '&typeId=' + typeId,
    {
        parameters: '', method: 'post',

        onSuccess: function(transport) {
            alert("Email Sent Successfully");
            if ($("popupAnimatedGif") && $("shapeshifterswf")) {
                $("popupAnimatedGif").style.display = "none";
                $("shapeshifterswf").style.display = "block";
            }
        },

        onFailure: function() {
            $('dvError').innerHTML = 'ERROR: Cannot connect to e-mail server, wait a few minutes and try again.';
        }
    });

    //SENDMAIL
    //hidePopup('popup_adress_book_send_movie');
    hidePopup('popup_send_movie');

    try {
        if ($('popupAnimatedGif') != null) {
            showPopup('popupAnimatedGif');
        }
        else {
            if ($('shapeshifterswf') != null) { toggleSWF('shapeshifterswf'); }
        }
    }
    catch (err) { }
}

function validateMailForm(mailTo, mailFrom) {
    var errMessage = "";

    var isValid = true;
    if (trim(mailTo) == "") {
        errMessage = "ERROR: You need to specify the receivers of the message.";
        isValid = false;
    }
    else if (isValidEmails(mailTo) == false) {
        errMessage = "ERROR: One of the email addresses may not valid.";
        isValid = false;
    }
    else if (trim(mailFrom) == "") {
        errMessage = "ERROR: You need to specify the sender of the message.";
        isValid = false;
    }

    var spnErr = $("spnErrMsg");
    spnErr.innerHTML = errMessage;
    if (!isValid) {
        msgElement = spnErr;
    }
    return isValid;
}

function isValidEmails(mailsAsStrting) {
    var i, isValid = false, mailAddress;
    var arrMails = mailsAsStrting.split(";");
    for (i = 0; i < arrMails.length; i++) {
        mailAddress = arrMails[i];
        mailAddress = trim(mailAddress);
        if (mailAddress != "") {
            isValid = validateMail(mailAddress);
        }
    }
    return isValid;
}

function validateMail(mailAddress) {
    var isValid = true;
    if (mailAddress.search(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/) == -1) {
        isValid = false;
    }
    return isValid;
}

function LTrim(value) {
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}

function RTrim(value) {
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}

function trim(value) {
    return LTrim(RTrim(value));
}