﻿function friendValidation(source, arguments) {

    var normalColor = "#000000";
    
    var yourEmail = document.getElementById('txtYourEmail');
    var friendEmail = document.getElementById('txtFriendEmail');
    var labelYourEmail = document.getElementById('lbYourEmail');
    var labelFriendEmail = document.getElementById('lbFriendEmail');
    var yourName = document.getElementById('txtYourName');
    var friendName = document.getElementById('txtFriendName');
    var labelYourName = document.getElementById('lbYourName');
    var labelFriendName = document.getElementById('lbFriendName');

    yourEmail.style.borderColor = normalColor;
    labelYourEmail.style.color = normalColor;
    friendEmail.style.borderColor = normalColor;
    labelFriendEmail.style.color = normalColor;
    yourName.style.borderColor = normalColor;
    friendName.style.borderColor = normalColor;
    labelYourName.style.color = normalColor;
    labelFriendName.style.color = normalColor;
    
    var regexName = /[^A-Za-z0-9]/;
    var regexEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

    var colorError = "#ff0000";
    var errorMsgDiv = document.getElementById('errors_div');
    var isValid = true;
    var errorMsg = "";

    if (yourEmail.value == "") {
        isValid = false;
        errorMsg += "<li>Email Address is a required field.</li>";
        yourEmail.style.borderColor = colorError;
        labelYourEmail.style.color = colorError;
    }

    if (friendEmail.value == "") {
        isValid = false;
        errorMsg += "<li>Friend's Email Address is a required field.</li>";
        friendEmail.style.borderColor = colorError;
        labelFriendEmail.style.color = colorError;
    }

    if (yourEmail.value != "" && !yourEmail.value.match(regexEmail)) {
        isValid = false;
        errorMsg += "<li>Please enter a valid e-mail address.</li>";
        yourEmail.style.borderColor = colorError;
        labelYourEmail.style.color = colorError;
    }

    if (friendEmail.value != "" && !friendEmail.value.match(regexEmail)) {
        isValid = false;
        errorMsg += "<li>Please enter a valid e-mail address for your friend.</li>";
        friendEmail.style.borderColor = colorError;
        labelFriendEmail.style.color = colorError;
    }
    
    if (yourName.value == "") {
        isValid = false;
        errorMsg += "<li>Your Name is a required field.</li>";
        yourName.style.borderColor = colorError;
        labelYourName.style.color = colorError;
    }

    if (friendName.value == "") {
        isValid = false;
        errorMsg += "<li>Friend's Name is a required field.</li>";
        friendName.style.borderColor = colorError;
        labelFriendName.style.color = colorError;
    }

    if (isValid == false) {
        errorMsgDiv.innerHTML = "<ul>" + errorMsg + "</ul>";
        arguments.IsValid = false;
    }
    else {
        arguments.IsValid = true;
    }
}