﻿var AccessWizard_DownloadDemo_UserInfo = Class.create({
    initialize: function(containerDiv, clientID) {
        this.popup = new Popup(containerDiv);
        this.clientID = clientID;

        this.cboVersion = this.getChild("cboVersion");
        this.txtName = this.getChild("txtName");
        this.txtEmail = this.getChild("txtEmail");
        this.txtCompany = this.getChild("txtCompany");
        this.txtPhone = this.getChild("txtPhone");
        this.chkDontContact = this.getChild("chkDontContact");

        this.contact_form = this.getChild("contact_form");
        this.email_sent = this.getChild("email_sent");
        this.pnlError = this.getChild("pnlError");

        this.getChild("btnSubmit").observe("click", this.submit.bindAsEventListener(this));
        this.getChild("btnReset").observe("click", this.reset.bindAsEventListener(this));
        this.getChild("btnClose").observe("click", this.close.bindAsEventListener(this));
    },

    show: function(event) {
        this.pnlError.hide();
        this.popup.show(event);
    },

    submit: function(event) {
        this.pnlError.hide();

        var response = Controls_Products_AccessWizard_DownloadDemo_UserInfo.ApplyForDemo(
            this.txtName.value,
            this.txtEmail.value,
            this.txtCompany.value,
            this.txtPhone.value,
            this.cboVersion.value,
            this.chkDontContact.checked);

        if (response == null) {
            this.pnlError.update("<p>The server didn't respond. Please try again later</p>");
            this.pnlError.show();
            return;
        }

        if (response.error != null) {
            this.pnlError.update("<p>" + response.error.Message + "</p>");
            this.pnlError.show();
            return;
        }

        this.contact_form.hide();
        this.email_sent.show();
    },

    reset: function(event) {
        this.txtName.value = "";
        this.txtEmail.value = "";
        this.txtCompany.value = "";
        this.txtPhone.value = "";
        this.chkDontContact.checked = false;
        this.popup.hide();
    },

    close: function(event) {
        this.popup.hide();
    },

    getChild: function(childID) {
        var fullID = this.clientID + "_" + childID;
        return $(fullID);
    }
});