﻿var CallBackForm = Class.create({
    initialize: function(containerDiv, clientID)
    {
        this.popup = new Popup(containerDiv);
        this.clientID = clientID;

        this.txtName = this.getChild("txtName");
        this.txtPhoneNumber = this.getChild("txtPhoneNumber");
        this.txtCompany = this.getChild("txtCompany");
        this.cboTimeZone = this.getChild("cboTimeZone");
        this.chkMorning = this.getChild("chkMorning");
        this.chkAfternoon = this.getChild("chkAfternoon");
        this.chkEvening = this.getChild("chkEvening");
        this.chkASAP = this.getChild("chkASAP");
        this.txtReason = this.getChild("txtReason");

        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 callTimes = 0;
        if (this.chkMorning.checked)
            callTimes |= 1;
        if (this.chkAfternoon.checked)
            callTimes |= 2;
        if (this.chkEvening.checked)
            callTimes |= 4;
        if (this.chkASAP.checked)
            callTimes |= 8;

        var response = SageKey.Website.Controls_CallBackForm.AskForCallBack(
            this.txtName.value,
            this.txtPhoneNumber.value,
            this.txtCompany.value,
            this.cboTimeZone.value,
            callTimes,
            this.txtReason.value);

        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.txtPhoneNumber.value = "";
        this.txtCompany.value = "";
        this.cboTimeZone.selectedIndex = 4;
        this.chkMorning.checked = false;
        this.chkAfternoon.checked = false;
        this.chkEvening.checked = false;
        this.chkASAP.checked = false;
        this.popup.hide();
    },

    close: function(event)
    {
        this.popup.hide();
    },

    getChild: function(childID)
    {
        var fullID = this.clientID + "_" + childID;
        return $(fullID);
    }
});
