﻿function sendToFriendValidation() {
    var IsValid = true;
    if (!isValidEmail(document.getElementById("RecipientEmail").value)) {
        document.getElementById("RecipientEmail").value = "Valid e-mail required";
        document.getElementById("RecipientEmail").style.color = "red";
        IsValid = false;
    }
    else {
        IsValid = true;
    }
    if (!isValidEmail(document.getElementById("YourEmail").value)) {
        document.getElementById("YourEmail").value = "Valid e-mail required";
        document.getElementById("YourEmail").style.color = "red";
        IsValid = false;
    }
    else {
        IsValid = true;
    }

    var FormNames = "RecipientName|RecipientLastName|YourName|YourLastName";
    for (var i = 0; i < FormNames.split('|').length; i++) {
        if (document.getElementById(FormNames.split('|')[i]).value == '') {
            document.getElementById(FormNames.split('|')[i]).value = "*";
            document.getElementById(FormNames.split('|')[i]).style.color = "red";
            IsValid = false;
        }
        else {
            IsValid = true;
        }
    }

    if (IsValid == true) {
        sendWithClick();
    }

    return IsValid;
}

function sendToFreindValidationCaptureEnterKey() {
    document.onkeydown = null;
    document.onkeydown = sendToFriendValidationKeyBoardEvents;
    if (window.addEventListener) {
        window.addEventListener("keydown", sendToFriendValidationKeyBoardEvents, false);
    }
}

function sendToFriendValidationKeyBoardEvents(evt) {
    var keyCode = document.layers ? evt.which : document.all ? event.keyCode : document.getElementById ? evt.keyCode : 0;

    if (keyCode == 13) {
        if (sendToFriendValidation() == true) {
            SendClientData();
        }
        else {
            return false;
        }
    }
}

function sendWithClick() {
    SendClientData();
}

var SendToFriendRequest;

function SendClientData() {
    SendToFriendRequest = new createRequest();

    if (SendToFriendRequest) {
        try {
            var url = "/SCPSendToFriend.ashx?RecipientName=" + document.getElementById("RecipientName").value + "&RecipientEmail=" + document.getElementById("RecipientEmail").value + "&YourName=" + document.getElementById("YourName").value + "&YourEmail=" + document.getElementById("YourEmail").value + "&Referrer=" + location.href + "&Note=" + document.getElementById("note").value;
            SendToFriendRequest.open("GET", url, true);
            SendToFriendRequest.onreadystatechange = manageEmailDataChange;
            SendToFriendRequest.send(null);
            ShowSendToFriend(false);
        }
        catch (e) {
            alert(e);
        }
    }
}

function manageEmailDataChange() {
    try {
        switch (SendToFriendRequest.readyState) {
            case 4:
                if (SendToFriendRequest.responseText) {
                    if (SendToFriendRequest.responseText == 'True') {
                        hideSendToFriend();
                    }
                    break;
                }
        }
    }
    catch (e) {
        alert(e);
    }
}

function createRequest() {
    var request;
    var RequestObjects = new Array('Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP');
    try {
        request = new XMLHttpRequest();
        return request;
    }
    catch (trymicrosoft) {
        try {
            request = new ActiveXObject("Msxml4.XMLHTTP");
            return request;
        }
        catch (othermicrosoft) {
            for (var i = 0; i < RequestObjects.length; i++) {
                try {
                    request = new ActiveXObject(RequestObjects[i]);
                    return request;
                }
                catch (ex) {
                    return false;
                }
            }
        }
    }
    if (!request) {
        alert("Error initializing XMLHttpRequest!");
    }
}