﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("BandSite");

BandSite.ContactUs = function(element) {
    BandSite.ContactUs.initializeBase(this, [element]);
}

BandSite.ContactUs.prototype = {
    initialize: function() {
        BandSite.ContactUs.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        var that = this;

        this._sendButton.add_buttonClicked(
            function(sender, e) {
                var name = that._nameInput.get_value();
                var email = that._emailInput.get_value();
                var subject = that._subjectInput.get_value();
                var message = that._messageInput.get_value();
                var code = that._captchaInput.get_value();
                
                if (!name) {
                    that._messageLabel.flashMessage("Name must be entered");
                } else if (! email.match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)) {
                    that._messageLabel.flashMessage("Email address is invalid");
                } else if (!subject) {
                    that._messageLabel.flashMessage("Subject must be entered");
                } else if (!message) {
                    that._messageLabel.flashMessage("Message must be entered");
                } else if (!code) {
                    that._messageLabel.flashMessage("Code must be entered");
                } else {
                                
                    VisitorService.SendMessage(
                        that._bandID,
                        name,
                        email,
                        subject,
                        message,
                        code,
                        function(result) {
                            that._panelSet.showPanel('ThanksPanel');
                        },
                        function(error) {
                            if (error.get_message() == "Code is incorrect") {
                                that._messageLabel.flashMessage("Code is incorrect");
                            } else {
                                webServiceError(error);
                            }
                        }
                        
                    );
                
                }                
            }
        );        
        
        this.generateCaptchaImage();

    },
    dispose: function() {        
        //Add custom dispose actions here
        BandSite.ContactUs.callBaseMethod(this, 'dispose');
    },
    
    generateCaptchaImage: function() {
        var that = this;
        VisitorService.GenerateContactUsCaptchaImage(
            this._bandID,
            function(result) {
                that._captchaImage.refreshImage();
            },
            webServiceError            
        );
    },
    
    resetSection: function() {
        this._nameInput.set_value('');
        this._emailInput.set_value('');
        this._subjectInput.set_value('');
        this._messageInput.set_value('');    
        this._captchaInput.set_value('');    
    
        this._panelSet.showPanel("FormPanel");
    }
    
}
BandSite.ContactUs.createProperty("bandID");
BandSite.ContactUs.createProperty('panelSet');
BandSite.ContactUs.createProperty('nameInput');
BandSite.ContactUs.createProperty('emailInput');
BandSite.ContactUs.createProperty('subjectInput');
BandSite.ContactUs.createProperty('messageInput');
BandSite.ContactUs.createProperty('captchaInput');
BandSite.ContactUs.createProperty('captchaImage');
BandSite.ContactUs.createProperty('sendButton');
BandSite.ContactUs.createProperty('messageLabel');
BandSite.ContactUs.registerClass('BandSite.ContactUs', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
