﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("VictoryFellowshipChurch.Site");

VictoryFellowshipChurch.Site.dedicationControl = function(element) {
    VictoryFellowshipChurch.Site.dedicationControl.initializeBase(this, [element]);

    this._newItemLink = null;
    this._repeater = null;
    this._submitButton = null;
    
    this._form = null;
    this._loading = null;
    this._response = null;

    this._firstNameTextBox = null;
    this._lastNameTextBox = null;
    this._emailTextBox = null;
    this._address1TextBox = null;
    this._address2TextBox = null;
    this._cityTextBox = null;
    this._stateDropDownList = null;
    this._zipTextBox = null;
    this._amountTextBox = null;
    this._cardTypeDropDownList = null;
    this._cardNumberTextBox = null;
    this._cardExpDateTextBox = null;

    this._itemIndex = 0;

    this._dedicationItems = null;
    this._savedIndex = false;

    this._rowArray = new Array();
}

VictoryFellowshipChurch.Site.dedicationControl.prototype = {
    initialize: function() {
        VictoryFellowshipChurch.Site.dedicationControl.callBaseMethod(this, 'initialize');

        // Add custom initialization here
        this._initControls();

        $addHandlers(this._newItemLink,
            {
                click: this._newItemLink_Click
            }, this);

        $addHandlers(this._submitButton,
            {
                click: this._submitButton_Click
            }, this);

        VictoryFellowshipChurch.WebServices.DedicationType.GetDedicationType(this._SucceededCallback, this._FailedCallback, this);

    },
    dispose: function() {
        //Add custom dispose actions here
        VictoryFellowshipChurch.Site.dedicationControl.callBaseMethod(this, 'dispose');
    },

    _initControls: function() {
        this._newItemLink = $get("newItem");
        this._repeater = $get("repeaterDiv");
        this._submitButton = $get("SubmitButton");

        this._firstNameTextBox = $get("pagetemplate1_firstNameTextBox");
        this._lastNameTextBox = $get("pagetemplate1_lastNameTextBox");
        this._emailTextBox = $get("pagetemplate1_emailTextBox");
        this._address1TextBox = $get("pagetemplate1_address1TextBox");
        this._address2TextBox = $get("pagetemplate1_address2TextBox");
        this._cityTextBox = $get("pagetemplate1_cityTextBox");
        this._stateDropDownList = $get("pagetemplate1_stateDropDownList");
        this._zipTextBox = $get("pagetemplate1_zipTextBox");
        this._amountTextBox = $get("pagetemplate1_amountTextBox");
        this._cardTypeDropDownList = $get("pagetemplate1_paymentTypeDropDownList");
        this._cardNumberTextBox = $get("pagetemplate1_cardNumberTextBox");
        this._cardExpDateTextBox = $get("pagetemplate1_cardExpDateTextBox");

        this._form = $get("form");
        this._loading = $get("loading");
        this._response = $get("response");
    },

    _newItemLink_Click: function(e) {
        this._addNewRow();
    },

    _submitButton_Click: function(e) {
        this._initControls();

        if (this._firstNameTextBox.value != "" &&
            this._lastNameTextBox.value != "" &&
            this._emailTextBox.value != "" &&
            this._amountTextBox.value != "") {

            this._loading.style.visibility = "visible";
            this._loading.style.display = "block";

            var donationObject = new VictoryFellowshipChurch.Objects.Donation();
            donationObject.FirstName = this._firstNameTextBox.value;
            donationObject.LastName = this._lastNameTextBox.value;
            donationObject.Email = this._emailTextBox.value;
            donationObject.Address1 = this._address1TextBox.value;
            donationObject.Address2 = this._address2TextBox.value;
            donationObject.City = this._cityTextBox.value;
            donationObject.State = this._stateDropDownList.value;

            if (this._zipTextBox.value != "") {
                donationObject.Zip = parseInt(this._zipTextBox.value);
            }

            donationObject.Amount = this._amountTextBox.value;
            donationObject.CardType_ID = this._cardTypeDropDownList.value;
            donationObject.CardNumber = this._cardNumberTextBox.value;

            if (this._cardExpDateTextBox.value != "") {
                donationObject.CardExpDate = this._cardExpDateTextBox.value;
            }

            Array.clear(this._rowArray);

            for (var i = 0; i < this._itemIndex; i++) {
                var dedicationObject = new VictoryFellowshipChurch.Objects.Dedication();
                dedicationObject.DedicationType_ID = $get("dedication_" + i).value;
                dedicationObject.Message = $get("message_" + i).value;

                Array.add(this._rowArray, dedicationObject);
            }

            VictoryFellowshipChurch.WebServices.Donation.Donation_Save(0, donationObject, this._SucceededCallback, this._FailedCallback, this);
        }
    },

    _addNewRow: function() {
        var previousTextArea = $get("message_" + (parseInt(this._itemIndex) - 1));

        var itemArray = new Array();

        for (var i = 0; i < this._itemIndex; i++) {
            var item = new VictoryFellowshipChurch.Objects.Dedication();
            item.DedicationType_ID = $get("dedication_" + i).value;
            item.Message = $get("message_" + i).value;

            Array.add(itemArray, item);
        }

        if (previousTextArea == null || previousTextArea.value != "") {
            var sb = new Sys.StringBuilder();
            sb.append("<table class=\"formTable\" id=\"item_" + this._itemIndex + "\"><tr>");
            sb.append("<th>Dedication Type: </th>");
            sb.append("<td><select id=\"dedication_" + this._itemIndex + "\">");

            for (var i = 0; i < this._dedicationItems.length; i++) {
                sb.append("<option value=\"" + this._dedicationItems[i].ID + "\">" + this._dedicationItems[i].Name + "</option>");
            }

            sb.append("</select></td></tr>");
            sb.append("<tr><th>Dedication Message: </th><td><textarea id=\"message_" + this._itemIndex + "\" rows=\"2\"></textarea></td>");
            //sb.append("<a id=\"deleteLink_" + this._itemIndex + "\"></a>");
            sb.append("</table>");

            this._repeater.innerHTML = this._repeater.innerHTML + sb.toString();

            for (var i = 0; i < this._itemIndex; i++) {
                var dedicationDropDown = $get("dedication_" + i);
                var dedicationTextArea = $get("message_" + i);

                for (var j = 0; j < dedicationDropDown.options.length; j++) {
                    if (itemArray[i].DedicationType_ID == dedicationDropDown.options[j].value) {
                        dedicationDropDown.options[j].selected = true;
                        break;
                    }
                }

                dedicationTextArea.value = itemArray[i].Message;

                /*$addHandlers($get("deleteLink_" + this._itemIndex),
                {
                click: this._DeleteRow
                }, this);*/
            }

            this._itemIndex = this._itemIndex + 1;
        }
    },

    _SucceededCallback: function(result, userContext, methodName) {
        switch (methodName) {
            case "GetDedicationType":
                userContext._dedicationItems = result
                break;
            case "Donation_Save":
                var savingDedication = false;
                userContext._savedIndex = result;

                for (var i = 0; i < userContext._rowArray.length; i++) {
                    var dedicationObject = userContext._rowArray[i];
                    dedicationObject.Donation_ID = userContext._savedIndex;

                    VictoryFellowshipChurch.WebServices.Dedication.Dedication_Save(0, dedicationObject, userContext._SucceededCallback, userContext._FailedCallback, userContext);

                    savingDedication = true;
                }

                if (!savingDedication) {
                    userContext._Success(userContext);
                }

                break;
            case "Dedication_Save":
                userContext._Success(userContext);
                break;
            case "Send":

                break;
        }

    },

    _FailedCallback: function(error, userContext, methodName) {
        if (error !== null) {
            alert("An error occurred: " + error.get_message());
        }
    },

    _Success: function(userContext) {
        var confMail = new VictoryFellowshipChurch.Objects.Message();
        confMail.To = userContext._emailTextBox.value;
        confMail.From = "info@victoryfellowship.org";
        confMail.BCC = "webapps@bytesofknowledge.com";
        confMail.Body = "Your online donation has been received. We thank you for your partnership in the gospel and ask you to join us in expecting an abundance of fruit in Jesus name.";
        confMail.Subject = "Your online donation has been received.";

        VictoryFellowshipChurch.WebServices.SendEmail.Send(confMail, userContext._SucceededCallback, userContext._FailedCallback, userContext);

        var respMail = new VictoryFellowshipChurch.Objects.Message();
        respMail.To = "info@victoryfellowship.org";
        respMail.From = "info@victoryfellowship.org";
        respMail.BCC = "webapps@bytesofknowledge.com";
        respMail.Body = "A new online donation has been received.";
        respMail.Subject = "A new online donation has been received.";

        VictoryFellowshipChurch.WebServices.SendEmail.Send(respMail, userContext._SucceededCallback, userContext._FailedCallback, userContext);

        userContext._form.style.visibility = "hidden";
        userContext._form.style.display = "none";

        userContext._response.style.visibility = "visible";
        userContext._response.style.display = "block";

        userContext._loading.style.visibilty = "hidden";
        userContext._loading.style.display = "none";
    }
}

VictoryFellowshipChurch.Site.dedicationControl.registerClass('VictoryFellowshipChurch.Site.dedicationControl', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

