﻿var alert_submission_result; var sms_submission_result;
function alertsInit(objectId, objectTypeId) {
    var profileGuid = getCookie("__REELZ_ACCOUNT_ID");
    if (profileGuid != null && hasEmail == "1") {
        document.getElementById("email_submission").style.display = "none";
        document.getElementById("alert_subscription_msg").style.display = "none";
        document.getElementById("profile_submission").style.display = "block";
        xmlHttp = getXmlHttpObject();
        xmlHttp.onreadystatechange = getAlertCallback;
        var url = "/webservices/alert.asmx/getProfileAlert?profileGuid=" + profileGuid + "&objectTypeId=" + objectTypeId + "&objectId=" + objectId;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    }
    else {
        document.getElementById("email_submission").style.display = "block";
        document.getElementById("profile_submission").style.display = "none";
    }
}
function hideErrorMessages() {
    if (typeof alert_submission_result != 'undefined' && alert_submission_result != null)
        alert_submission_result.style.display = "none";
    if (typeof sms_submission_result != 'undefined' && sms_submission_result != null)
        sms_submission_result.style.display = "none";
}
function setAlertAdded() {
    if (document.getElementById("alert_subscription_msg") != null) {
        document.getElementById("alert_subscription_msg").innerHTML = "Alert has been added to your email list.";
        document.getElementById("alert_subscription_msg").style.display = "block";
    }
    else if (alert_submission_result != null) {
        alert_submission_result.innerHTML = "Alert has been added to your email list.";
        alert_submission_result.style.display = "block";
    }
    if (document.getElementById("email_submission") != null) { document.getElementById("email_submission").style.display = "none"; }
    if (document.getElementById("profile_submission") != null) { document.getElementById("profile_submission").style.display = "block"; }
    if (document.getElementById("add_alert_btn") != null) { document.getElementById("add_alert_btn").style.display = "none"; }
    if (document.getElementById("alert_promo_msg") != null) { document.getElementById("alert_promo_msg").style.display = "none"; }
}
function alertAlreadyAdded() {
    if (document.getElementById("alert_subscription_msg") != null) {
        document.getElementById("alert_subscription_msg").innerHTML = "You are already subscribed to this alert.";
        document.getElementById("alert_subscription_msg").style.display = "block";
    } else if (alert_submission_result != null) {
        alert_submission_result.innerHTML = "You are already subscribed to this alert.";
        alert_submission_result.style.display = "block";
    }
    if (document.getElementById("add_alert_btn") != null) { document.getElementById("add_alert_btn").style.display = "none"; }
    if (document.getElementById("alert_promo_msg") != null) { document.getElementById("alert_promo_msg").style.display = "none"; }
}
function errorAddingAlert() {
    alert_submission_result.innerHTML = "Error while processing your alert request, please try again later.";
    alert_submission_result.style.display = "block";
}
function alertNotAdded() {
    document.getElementById("email_submission").style.display = "none";
    document.getElementById("profile_submission").style.display = "block";
    document.getElementById("add_alert_btn").style.display = "inline";
    document.getElementById("alert_promo_msg").style.display = "block";
    document.getElementById("alert_subscription_msg").style.display = "none";
}
function addAlertCallback() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        cbstring = getReturnValue("string");
        if (cbstring == "failure") {
            errorAddingAlert();
        }
        else if (cbstring == "exists") {
            alertAlreadyAdded();
        } else {
            setAlertAdded();
        }
    }
    else if (xmlHttp.readyState == 4) {
        errorAddingAlert();
    }
}
function getAlertCallback() {
    hideErrorMessages();
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        cbstring = getReturnValue("string");
        if (cbstring == "AlertExists") {
            alertAlreadyAdded();
        } else {
            alertNotAdded();
        }
    } else if (xmlHttp.readyState == 4) {
        alertNotAdded();
    }
}
function getSMSAlertCallback() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        sms_submission_result.innerHTML = "<span>SMS alert submitted! You will begin to receive alerts upon verification.</span>";
        sms_submission_result.style.display = "block";
    }
    else if (xmlHttp.readyState == 4) {
        sms_submission_result.innerHTML = "<span>Error while processing your alert request, please try again later.</span>";
        sms_submission_result.style.display = "block";
    }
}

function getEmailAlertCallback() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        alert_submission_result.innerHTML = "<span>Look for the Confirm Your ReelzChannel alerts email. Click the link in it to activate your alerts.</span>";
        alert_submission_result.style.display = "block";
    }
    else if (xmlHttp.readyState == 4) {
        alert_submission_result.innerHTML = "<span>Error while processing your alert request, please try again later.</span>";
        alert_submission_result.style.display = "block";
    }
}

function submitEmail(emailId, oname, oid, otid, resultObj) {
    hideErrorMessages();
    alert_submission_result = resultObj;
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (emailPattern.test(emailId) == false) {
        alert_submission_result.innerHTML = "<span>Please enter a valid email address.</span>";
        alert_submission_result.style.display = "block";
    }
    else {
        alert_submission_result.innerHTML = "";
        alert_submission_result.style.display = "none";
        xmlHttp = getXmlHttpObject();
        xmlHttp.onreadystatechange = getEmailAlertCallback;
        var url = BASE_URL + "webservices/alert.asmx/addEmailAlert?email=" + emailId + "&objectName=" + oname + "&oid=" + oid + "&otid=" + otid;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
        return;
    }
}

function blogSubmitEmail(emailId, idlist, onameList, resultObj){
    hideErrorMessages();
    alert_submission_result = resultObj;
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (emailPattern.test(emailId) == false) {
        alert_submission_result.innerHTML = "<span>Please enter a valid email address.</span>";
        alert_submission_result.style.display = "block";
    }
    else {
        alert_submission_result.innerHTML = "";
        alert_submission_result.style.display = "none";
        xmlHttp = getXmlHttpObject();
        xmlHttp.onreadystatechange = getEmailAlertCallback;
        var url = BASE_URL + "webservices/alert.asmx/addEmailAlertList?email=" + emailId + "&idList=" + idlist + "&onameList=" + onameList;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
        return;
    }
}

function addUserAlert(objectId, objectTypeId, resultObj) {
    alert_submission_result = resultObj;
    hideErrorMessages();
    var profileGuid = getCookie("__REELZ_ACCOUNT_ID");
    if (profileGuid != null) {
        xmlHttp = getXmlHttpObject();
        xmlHttp.onreadystatechange = addAlertCallback;
        var url = BASE_URL + "webservices/alert.asmx/addProfileAlert?profileGuid=" + profileGuid + "&objectId=" + objectId + "&objectTypeId=" + objectTypeId;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    }
}

function addUserAlertList(idList, resultObj) {
    alert_submission_result = resultObj;
    hideErrorMessages();
    var profileGuid = getCookie("__REELZ_ACCOUNT_ID");
    if (profileGuid != null) {
        xmlHttp = getXmlHttpObject();
        xmlHttp.onreadystatechange = addAlertCallback;
        var url = BASE_URL + "webservices/alert.asmx/addProfileAlertList?profileGuid=" + profileGuid + "&idList=" + idList;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    }
}

function submitSMS(oname, oid, otid, resultObj) {
    hideErrorMessages();
    sms_submission_result = resultObj
    var smsnumber;
    var smscarrier;
    if (document.getElementById("SMScarriers").value == "0") {
        sms_submission_result.innerHTML = "<span>Please select a carrier.</span>";
        sms_submission_result.style.display = "block";
    }
    else {
        smscarrier = document.getElementById("SMScarriers").value;
        smsnumber = document.getElementById("alertSMSField").value;
        var arr = smsnumber.split(/\B|\b/g);
        smsnumber = "";
        for (n in arr) {
            var numberPattern = /\d$/;
            if (numberPattern.test(arr[n])) {
                smsnumber += arr[n];
            }
        }
        if (smsnumber.length != 10) {
            sms_submission_result.innerHTML = "<span>Number must be 10 digits.</span>";
            sms_submission_result.style.display = "block";
        }
        else {
            sms_submission_result.innerHTML = "";
            sms_submission_result.style.display = "none";
            xmlHttp = getXmlHttpObject();
            xmlHttp.onreadystatechange = getSMSAlertCallback;
            var url = BASE_URL + "webservices/alert.asmx/addSMSAlert?carrierId=" + smscarrier + "&number=" + smsnumber + "&objectName=" + oname + "&oid=" + oid + "&otid=" + otid;
            xmlHttp.open("GET", url, true);
            xmlHttp.send(null);
            return;
        }
    }
}
