﻿var tt;
    function optInPopupOfferOpen() {
        $("#optInPopupOffer").dialog("open");
        $("#txtOptInPopupName").blur();
        $("#btnOptInPopup").attr("disabled", false);
    }

$(document).ready(function () {

    $("div.elqOptInDialog").dialog({
        autoOpen: false,
        closeOnEscape: true,
        modal: true,
        draggable: false,
        resizable: false,
        width: 800,
        height: 550,
        show: "slide",
        hide: "slide",
        title: "WELCOME!"
    });

    $("#optInPopupOffer").dialog({
        dialogClass: "no_titlebar",
        autoOpen: false,
        closeOnEscape: true,
        modal: true,
        draggable: false,
        resizable: false,
        width: 600,
        show: "slide",
        hide: "slide"
    });

    $("#optInPopupOffer").find(".closeDialog").click(function () {
        $("#optInPopupOffer").dialog("close");
        return false;
    });

    $("#optInPopupAck").dialog({
        dialogClass: "no_titlebar",
        autoOpen: false,
        closeOnEscape: true,
        modal: true,
        draggable: false,
        resizable: false,
        width: 600,
        show: "slide",
        hide: "slide"
    });

    $("#optInPopupAck").find(".closeDialog").click(function () {
        $("#optInPopupAck").dialog("close");
        return false;
    });

    $("#btnOptIn").click(function () {

        $(this).attr("disabled", true);

        var name = $.trim($("#txtName").val());
        var email = $.trim($("#txtEmail").val());

        name = (name == "Enter Your First Name" ? "" : name);
        email = (email == "Enter Your Email Address" ? "" : email);

        if (name == "" || email == "" || !email.isEmail()) {
            if (name == "") { $("#txtName").addClass("error"); } else { $("#txtName").removeClass("error"); }
            if (email == "" || !email.isEmail()) { $("#txtEmail").addClass("error"); } else { $("#txtEmail").removeClass("error"); }
            $(this).attr("disabled", false);
        }
        else {
            $("#txtName").removeClass("error");
            $("#txtEmail").removeClass("error");
            var optInRequest = new OptInRequest(
                $("#txtName").val(),
                $("#txtEmail").val()
            ).send();
        }

        return false;
    });
	
	
    $("#btnOptInPopup").click(function () {

        $(this).attr("disabled", true);

        var name = $.trim($("#txtOptInPopupName").val());
        var email = $.trim($("#txtOptInPopupEmail").val());

        name = (name == "Enter Your First Name" ? "" : name);
        email = (email == "Enter Your Email Address" ? "" : email);

        if (name == "" || email == "" || !email.isEmail()) {
            if (name == "") { $("#txtOptInPopupName").addClass("error"); } else { $("#txtOptInPopupName").removeClass("error"); }
            if (email == "" || !email.isEmail()) { $("#txtOptInPopupEmail").addClass("error"); } else { $("#txtOptInPopupEmail").removeClass("error"); }
            $(this).attr("disabled", false);
        }
        else {
            $("#txtOptInPopupName").removeClass("error");
            $("#txtOptInPopupEmail").removeClass("error");
            var optInRequest = new OptInRequest(
                $("#txtOptInPopupName").val(),
                $("#txtOptInPopupEmail").val()
            ).send(true);
        }

        return false;
    });
    
    if ($("#displayOptInPopup").val() == "True") {
        tt = setTimeout("optInPopupOfferOpen()", 4000);
    }

});

function OptInRequest(name, email) {
    this.name = name;
    this.email = email;
}
OptInRequest.prototype.send = function (sendFromPopup) {

    $.ajax({
        url: "/webservices/ElqService.svc/OptInRequest",
        type: "POST",
        contentType: "application/json",
        processData: true,
        data: JSON.stringify({ name: this.name, email: this.email }),
        dataType: "json",
        success: (sendFromPopup ? this.sendFromPopupSuccess : this.sendSuccess),
        error: this.sendError
    });
}

OptInRequest.prototype.sendSuccess = function (data, textStatus, xmlHttpRequest) {
    $("div.elqOptInDialog").dialog("open");
    $("#btnOptIn").attr("disabled", false);
}
OptInRequest.prototype.sendFromPopupSuccess = function (data, textStatus, xmlHttpRequest) {
    $("#btnOptInPopup").attr("disabled", false);
    $("#optInPopupOffer").dialog("close");
    $("#optInPopupAck").dialog("open");
}
OptInRequest.prototype.sendError = function (xmlHttpRequest, textStatus, errorThrown) {
    alert("status: {0}; statusText: {1}".format(xmlHttpRequest.status, xmlHttpRequest.statusText));
    $("#btnOptIn").attr("disabled", false);
    $("#btnOptInPopup").attr("disabled", false);
    $("#optInPopupOffer").dialog("close");
}

