﻿// Public Site E-Mail Dialog
// (c) 2008 MRI

var oEMailTimer = null;
var iEMailTimeout = 60000;
var sButtonEMailClose = '<input  type="button" id="ButtonCloseMail" name="ButtonCloseMail" value="Close" class="dialogbutton" style="width:150px" onclick="return onCloseEmail()" />';
var iEMailReleaseID = 0;
var iEMailGenID = 0;
var iEMailPostID = 0;

function onCloseEmail() {
    HideElement(sDialogParent);
    HideElement(sFixedDialogParent);
    return false;
}

function onCheckEMailField(oEvent,oControl,iIndex) {
    var iKey;
    
    if (window.event) iKey = oEvent.keyCode;
    else if (oEvent.which) iKey = oEvent.which;
    
    if (iKey == 13) oEvent.keyCode = 9;
    
    if ((iKey != 13) && (iKey != 9)) return;

    if (iIndex == 1) {
        if (oControl.value == "") SetEMailImage(1,"ImageAddr1");
        else SetEMailImage(2,"ImageAddr1");
    } else if (iIndex == 2) {
        if (oControl.value == "") SetEMailImage(1,"ImageAddr2");
        else if (IsEMailValid(oControl.value)) SetEMailImage(2,"ImageAddr2");
        else SetEMailImage(1,"ImageAddr2");
    } else if (iIndex == 3) {
        if (oControl.value == "") SetEMailImage(0,"ImageAddr"+iIndex);
        else if (IsEMailValid(oControl.value)) SetEMailImage(2,"ImageAddr"+iIndex);
        else SetEMailImage(1,"ImageAddr"+iIndex);
    } else {
        if (oControl.value == "") SetEMailImage(0,"ImageAddr1");
        else if (IsEMailValid(oControl.value)) SetEMailImage(2,"ImageAddr1");
        else SetEMailImage(1,"ImageAddr1");
    }
}

function SetEMailImages() {
    SetEMailImage(0,"ImageAddr1");
    SetEMailImage(0,"ImageAddr2");
    SetEMailImage(0,"ImageAddr3");
    SetEMailImage(0,"ImageAddr4");
}    

function SetEMailImage(iState, sControl) {
    var oImage;
    
    if (iState == 0) HideElement(sControl);
    else {
        oImage = document.getElementById(sControl);
        
        if (oImage != null) {
            if (iState == 1) oImage.src = "/Images/trash.gif";
            else oImage.src = "/Images/submit.gif";
            ShowElement(sControl);
        }
    }
}

function ValidateEMailtoFriend()  {
    var sControl = "";
    var sMessage = "";
    var sAddrMessage = "Please enter a valid e-mail address";
       
    if (GetValue("EditEMailFromAddr") == "") {
        sControl = "FromAddr";
        sMessage = "Please enter your name.";
        SetEMailImage(1,"ImageAddr1");
    } else if (!IsEMailValid(GetValue("EditEMailAddr1"))) {
        sControl = "EditEMailAddr1";
        sMessage = sAddrMessage;
        SetEMailImage(1,"ImageAddr2");
        SetEMailImage(2,"ImageAddr1");
    } else if ((!IsEMailValid(GetValue("EditEMailAddr2"))) && (GetValue("EditEMailAddr2") != "")) {
        sControl = "EditEMailAddr2";
        sMessage = sAddrMessage;
        SetEMailImage(1,"ImageAddr3");
        SetEMailImage(2,"ImageAddr2");
    } else if ((!IsEMailValid(GetValue("EditEMailAddr3"))) && (GetValue("EditEMailAddr3") != "")) {
        sControl = "EditEMailAddr3";
        sMessage = sAddrMessage;
        SetEMailImage(1,"ImageAddr4");
        SetEMailImage(2,"ImageAddr3");
    }
       
    if (sMessage != "") {
        SetFocus(sControl);
        alert(sMessage);
        return false
    } else {
        if (GetValue("EditEMailAddr1") != "") SetEMailImage(2, "ImageAddr2");
        if (GetValue("EditEMailAddr2") != "") SetEMailImage(2, "ImageAddr3");
        if (GetValue("EditEMailAddr3") != "") SetEMailImage(2, "ImageAddr4");
    }
            
    return true;
}

function onShowEMail(iReleaseID, iGenTypeID, iPostID) {
    var sURL = sSADataURL + "ID=" + GetSessionID() + "&AID=23";
    pProcessContent = onProcessGetEmailDialog;
    iEMailReleaseID = iReleaseID;
    iEMailGenID = iGenTypeID;
    iEMailPostID = iPostID;
    LoadContent(sURL, "", true)
}

function onProcessGetEmailDialog(sContent, sParent) {
    var oDiv = document.getElementById(sDialogParent);
    var iX = iCursorX;
    var iY = iCursorY;
    var iPos = Math.floor(getClientWidth() / 2) - 260;
    var c = 0;

    if (oDiv != null) {
        oDiv.innerHTML = sContent;

        ShowElement(sDialogParent);
        oDiv.style.position = "absolute";

        if (!bIsIE) {
            oDiv.style.top = iY + 10 + "px";
            oDiv.style.left = iPos + "px";
        } else {
            oDiv.style.top = iY + 10;
            oDiv.style.left = iPos;
        }

        SetEMailImages();
        SetFocus("EditEMailFromAddr");
        //if (bIsIE) document.getElementById("EMAILDIALOGHEIGHT").style.height = '278px';
    }
}

function EncodeEMailValues() {
    return GetValue("EditEMailFromAddr") + "~" +
           GetValue("EditEMailFromMsg") + "~" +
           GetValue("EditEMailAddr1") + "~" +
           GetValue("EditEMailAddr2") + "~" +
           GetValue("EditEMailAddr3");
}

function onSendEMailToFriend() {
    var iReleaseID = GetValue("RELEASEID");
    var sRIDs = "";
    
    if (ValidateEMailtoFriend()) {
        HideElement("ButtonSendMail");
        HideElement("ButtonCloseMail");
        SetBusyMessage("SENDINGSTATUS", "Sending E-Mail...");
        oEMailTimer = setTimeout(pEMailTimeoutProc, iEMailTimeout);

        if ((iEMailReleaseID != null) && (iEMailReleaseID != 0)) iReleaseID = iEMailReleaseID;

        if (aCompareReleaseIDs.length > 0) {
            sRIDs = EncodeReleaseList();   
        }

        var sURL = sSADataURL + "ID=" + GetSessionID() + "&AID=24&SID=" + iEMailGenID + sRIDs +
                   "&PID=" + iEMailPostID + "&RID=" + iReleaseID + "&WID=" + dDVDWeekendDate + 
                   "&DATA=" + EncodeEMailValues();

        pProcessContent = onProcessSendMail;
        LoadContent(sURL, "", true)
    }

    return false;
}

function pEMailTimeoutProc() {
    clearTimeout(oEMailTimer);
    oEMailTimer = null;
    //onCloseEmail();
    ShowElement("ButtonCloseMail");
    SetSpanString("SENDINGSTATUS", "<span style='font-size:10pt'>We're sorry. A system problem prevented the e-mail from being sent.</span><br>" + sButtonEMailClose);
}

function onProcessSendMail(sContent, sParent) {
    clearTimeout(oEMailTimer);
    oEMailTimer = null;
    iEMailReleaseID = 0;
    //onCloseEmail();
    ShowElement("ButtonCloseMail");
    SetSpanString("SENDINGSTATUS", "The e-mail was sent<br><br>" + sButtonEMailClose);
}

function onShowEMailAlerts() {
    var sURL = sSADataURL + "ID=" + GetSessionID() + "&AID=26";
    pProcessContent = onProcessGetEmailAlertDialog;
    LoadContent(sURL, "", true)

}

function onProcessGetEmailAlertDialog(sContent, sParent) {
    oDiv = document.getElementById(sFixedDialogParent);

    if (oDiv != null) {
        ShowElement(sFixedDialogParent);
        oDiv.innerHTML = sContent;
        scroll(0, 0);
        SetEMailImages();
        SetFocus("EditEMailFromAddrAlert");
    }
}

function ValidateEMailAlerts() {
    var sControl = "";
    var sMessage = "";
    var sAddrMessage = "Please enter a valid e-mail address";

    if (GetValue("EditEMailFromAddrAlert") == "") {
        sControl = "EditEMailFromAddrAlert";
        sMessage = "Please enter your e-mail address.";
        SetEMailImage(1, "ImageAddr1");
    } else if (!IsEMailValid(GetValue("EditEMailFromAddrAlert"))) {
        sControl = "EditEMailFromAddrAlert";
        sMessage = sAddrMessage;
        SetEMailImage(1, "ImageAddr1");
    } else {
        sMessage = "";
    }
    
//    else if ((!GetChecked("CheckLimited")) && (!GetChecked("CheckWide")) &&
//               (!GetChecked("CheckBest")) && (!GetChecked("CheckUnsubscribe"))) {
//        sControl = "CheckLimited";
//        sMessage = "Please select at least one kind of alert."
//        SetEMailImage(2, "ImageAddr1");
//    } else SetEMailImage(2, "ImageAddr1");

    if (sMessage != "") {
        SetFocus(sControl);
        alert(sMessage);
        return false
    }

    return true;
}

function EncodeEMailAlertValues() {
//    return siif(GetChecked("CheckWide"), "1", "0") + ";" +
//           siif(GetChecked("CheckLimited"), "1", "0") + ";" +
//           siif(GetChecked("CheckBest"), "1", "0") + ";" +
//           siif(GetChecked("CheckUnsubscribe"), "1", "0");
    
    return "1;1;1;" + siif(GetChecked("CheckUnsubscribe"), "1", "0");
}

function onSaveEMailAlerts() {
    if (ValidateEMailAlerts()) {
        HideElement("ButtonSendMail");
        HideElement("ButtonCloseMail");
        SetBusyMessage("SENDINGSTATUS", "Saving Alert Preferences...");
        oEMailTimer = setTimeout(pEMailAlertTimeoutProc, iEMailTimeout);

        var sURL = sSADataURL + "ID=" + GetSessionID() + "&AID=27" +
                    "&EMAIL=" + GetValue("EditEMailFromAddrAlert") + "&ALERTS=" + EncodeEMailAlertValues();

        pProcessContent = onProcessSaveAlerts;
        LoadContent(sURL, "", true)
    }

    return false;
}

function pEMailAlertTimeoutProc() {
    clearTimeout(oEMailTimer);
    oEMailTimer = null;
    //onCloseEmail();
    ShowElement("ButtonCloseMail");
    SetSpanString("SENDINGSTATUS", "<span style='font-size:10pt'>We're sorry. A system problem prevented the alerts from being saved.</span><br>" + sButtonEMailClose);
}

function onProcessSaveAlerts(sContent, sParent) {
    clearTimeout(oEMailTimer);
    oEMailTimer = null;
    ShowElement("ButtonCloseMail");
    SetSpanString("SENDINGSTATUS", "The alerts were saved.<br>" + sButtonEMailClose);
}

function onEMailAlertInputClick() {
    SetClassName("ALERTEMAIL", "alertinput");
    SetValue("ALERTEMAIL", "");
    //SetSpanString("ALERTMESSAGE", "3-4 per week. No spam.");
}

function onAlertControlGo() {
    var sControl = "ALERTEMAIL";
    var sMessage = "";
    var sAddrMessage = "Please enter a<br>valid e-mail address.";

    if (GetValue(sControl) == "") {
        sMessage = "Please enter<br>your e-mail address.";
    } else if (!IsEMailValid(GetValue(sControl))) {
        sMessage = sAddrMessage;
    } else {
        sMessage = "";
    }

    if (sMessage != "") {
        SetFocus(sControl);
        SetSpanString("ALERTMESSAGE", sMessage);
    } else {
        var sURL = sSADataURL + "ID=" + GetSessionID() + "&AID=27" +
                    "&EMAIL=" + GetValue("ALERTEMAIL") + "&ALERTS=1;1;1;0";

        pProcessContent = onProcessSaveAlertControl;
        LoadContent(sURL, "", true)

        SetSpanString("ALERTMESSAGE", "Address saved.<br><i>Thank you!</i>");
        SetClassName("ALERTEMAIL", "alertinputinit");
        SetValue("ALERTEMAIL", "Enter your address here.");
    }
}

function onProcessSaveAlertControl(sContent) {

}

function onAddAddressClick() {
    oSearchAhead.SaveAddressToPackage();
    return false;
}

function onSendPosts() {
    oSearchAhead.ClearPackage();
    SetSpanString("MAILFILMLIST", "");
    SetSpanString("MAILADDRESSES", "");
    ShowElement("ButtonCloseMail");
    ShowElement("ButtonSendMail");
    HideElement("SENDING");

    ShowElement("SENDMAILDIALOG");
    SetFocus("SAMAIL");
    return false;
}

function onSendPostsClose() {
    HideElement("SENDMAILDIALOG");
    return false;
    SetValue("EditEMailFromMsg", "");
}

function ValidateSendPosts() {
    if (GetSpanString("MAILFILMLIST") == "") {
        alert("Please enter at least one film.");
        SetFocus("SAMAIL");
        return false;
    } else if (GetSpanString("MAILADDRESSES") == "") {
        alert("Please enter at least one email address.");
        SetFocus("ADDADDRESS");
        return false;
    } else return true;
}

function onSendPostsSend() {
    if (ValidateSendPosts()) {
        HideElement("ButtonCloseMail");
        HideElement("ButtonSendMail");
        ShowElement("SENDING");
        oSearchAhead.SendPackage();
        SetValue("EditEMailFromMsg", "");
    }
   
    return false;
}

function onRemoveEMailTitle(iRID) {
    oSearchAhead.iAID = 40;
    var sURL = oSearchAhead.GetURL() + "&RID=" + iRID;
    pProcessContent = onProcessRemoveMailTitle;
    LoadContent(sURL, "", true);
}

function onProcessRemoveMailTitle(sContent) {
    SetSpanString("MAILFILMLIST", sContent);
    SetValue("SAMAIL", "");
}

function onRemoveEMailAddr(sAddr) {
    oSearchAhead.iAID = 41;
    var sURL = oSearchAhead.GetURL() + "&ADDR=" + sAddr;
    pProcessContent = onProcessRemoveMailAddr;
    LoadContent(sURL, "", true);
}

function onProcessRemoveMailAddr(sContent) {
    SetSpanString("MAILADDRESSES", sContent);
    SetValue("ADDADDRESS", "");
}
