var CommBox = function(){   
    // define some private variables
    var dialog, showBtn;
    
    // return a public interface
    return {  
        init : function(){
             showBtn = Ext.get('COMMENT_ID');
             // attach to click event
             showBtn.on('click', this.showDialog, this);
						 
						 Ext.get('SEND_COMMENT').on('click', this.sendComment, this );
        },
				
        showDialog : function(){
            if(!dialog){ // lazy initialize the dialog and only create it once
                dialog = new Ext.BasicDialog("comm-dlg", { 
                        autoTabs:true,
                        width:500,
                        height:500,
                        shadow:true,
                        minWidth:500,
                        minHeight:250,
                        proxyDrag: true
                });
                dialog.addKeyListener(27, dialog.hide, dialog);
                dialog.addButton('Close', dialog.hide, dialog);
            }
            dialog.show(showBtn.dom);
        },
				
				sendComment : function() {					
					// Setup waiting image
					Ext.get('MessageWaitingID').dom.style.visibility = "";
					Ext.get('SEND_COMMENT').dom.disabled = true;	
					
					Ext.get('MESSAGE_SUCCESS').dom.style.display = 'none';
					Ext.get('MESSAGE_FAIL').dom.style.display = 'none';
					
					for (var i=0; i < document.CommentForm.MessageReason.length; i++) {
						if (document.CommentForm.MessageReason[i].checked) {
							lReason = "&reason=" + document.CommentForm.MessageReason[i].value;
						}
					}													
					
					Ext.Ajax.request({
						url:"SendMessage.php",
						params: "message="+Ext.get('EMAIL_CONTENT').dom.value+lReason+"&email="+Ext.get('EMAIL_ADDRESS').dom.value,
						callback: this.processAJAX,
						scope:this
					});					
				},
				
				processAJAX : function(option, success, response) {
					// Setup waiting image
					Ext.get('MessageWaitingID').dom.style.visibility = "hidden";
					Ext.get('SEND_COMMENT').dom.disabled = false;										
					
					var lData = response.responseXML.getElementsByTagName('result')
					if( getNodeValue(lData[0],'message') == 'TRUE' ) {
						Ext.get('MESSAGE_SUCCESS').dom.style.display = '';
						Ext.get('EMAIL_CONTENT').dom.value = '';
					} else {
						Ext.get('MESSAGE_FAIL').dom.style.display = '';
					}
				}
    };
}();

Ext.onReady(CommBox.init, CommBox, true);
