﻿function NewEmail(Department) {
    ResetEmailForm();
    document.getElementById('txtEmailTo').value = Department;
    showPopup('popEmail', true);
}

function ResetEmailForm() {
    document.getElementById('txtEmailTo').value = "";
    document.getElementById('txtEmailFrom').value = "";
    document.getElementById('txtEmailSubject').value = "";
    document.getElementById('txtEmailBody').value = "";
    document.getElementById('chkCopyMyself').checked = false;
}

function OnSendEmailSuccess(result) {
    if (result == "OK") {
        alert('Your email has been sent. You should be contacted shortly.');
        ResetEmailForm();
        showPopup('popWait', false);
    } else {
        OnSendEmailFailed();
    }
}

function OnSendEmailFailed(result) {
    alert('We are unable to send your request at this time.');
    showPopup('popWait', false);
    showPopup('popEmail', true);
}

function SendEmail() {
    showPopup('popEmail', false);
    showPopup('popWait', true);
    var ErrorText = "";
    var Department = document.getElementById('txtEmailTo').value;
    var txtEmailFrom = document.getElementById('txtEmailFrom').value;
    var txtEmailSubject = document.getElementById('txtEmailSubject').value;
    var txtEmailBody = document.getElementById('txtEmailBody').value;
    var chkCopyMyself = document.getElementById('chkCopyMyself').checked;

    if (!txtEmailFrom.match(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
        ErrorText += (ErrorText == "" ? "" : "\n") + "Please provide a return email address.";
    if (txtEmailSubject.trim().length < 4)
        ErrorText += (ErrorText == "" ? "" : "\n") + "Please specify a subject.";
    if (txtEmailBody.trim().length < 10)
        ErrorText += (ErrorText == "" ? "" : "\n") + "Please write a message.";

    if (ErrorText != "") {
        alert(ErrorText);
        showPopup('popWait', false);
        showPopup('popEmail', true);
    } else {
        var webRequest = Sys.Net.WebServiceProxy.invoke("UniversalPropertyService.asmx", "SendEmailRequest", false, { "Department": Department, "FromEmail": txtEmailFrom, "Subject": txtEmailSubject, "Body": txtEmailBody, "CopyFrom": chkCopyMyself }, OnSendEmailSuccess, OnSendEmailFailed, null, 360000);
    }
}
