﻿
function sendToFriend() {
    if (validateForm())
    {
        var copy = ($F("copyMe") == "on") ? true : false;
        var pars = {"From_nm" : $F("senderName"),
                    "From_email" : $F("senderAddress"),
                    "To_nm" : $F("friendName"),
                    "To_email" : $F("friendAddress"),
                    "Copy" : copy}
        var myAjax = new Ajax.Request(
            "STFService.asmx/SendToFriend",       //url
            {
                method: "POST",
                parameters: pars,
                onSuccess: mailSent,
                onFailure: sendFailed
            });
    }
}

function validateForm() {
    var valid = true;
    var errorString = "";
    $$(".error").each(function(e) {e.hide() });
    
    if ($F("friendName") == "")
    {
        valid = false;
        errorString += "Friend's name is required\n";
        $("fnError").show();
    }
    if ($F("friendAddress") == "")
    {
        valid = false;
        errorString += "Friend's email is required\n";
        $("feError").show();
    }
    else if (!validateEmail($F("friendAddress")))
    {
        valid = false;
        errorString += "Friend's email is invalid\n";
        $("feError").show();
    }
    if ($F("senderName") == "")
    {
        valid = false;
        errorString += "Your name is required\n";
        $("ynError").show();
    }
    if ($F("senderAddress") == "")
    {
        valid = false;
        errorString += "Your email is required\n";
        $("yeError").show();
    }
    else if (!validateEmail($F("senderAddress")))
    {
        valid = false;
        errorString += "Your email is invalid\n";
        $("yeError").show();
    }
    
    if (errorString != "") alert(errorString);
    return valid;
}

function validateEmail( strValue) {
    var objRegExp = /^[a-z]([\w-\.])*@[\w-\.]+(\.[\w-\.])*$/i;
    return objRegExp.test(strValue);
}

function clearFields() {
    $("senderName").value = "";
    $("senderAddress").value = "";
    $("friendName").value = "";
    $("friendAddress").value = "";
    $("copyMe").checked = false;
}

function mailSent(t) {
    $("stfForm").hide();
    $("stfThanks").show();
    clearFields();
}

function sendFailed() {
    $("stfForm").hide();
    $("stfError").show();
}

function openSTF() {
    $("stfError").hide();
    $("stfThanks").hide();
    $("stfForm").show();
    $("stfPanel").show();
}

function closeSTF() {
    $("stfPanel").hide();
}