/**
 * Builds a Popup window used to submit Error 
 *
 * @author Interface
 * @created 2009-12-05
 * @version 1.0
 * 
 *
 * @param custom_labels - customized instance of Ron.Properties, creates new instance if one is not provided 
 *
 * @known_issues
 *		-
 *
*/
Ext.namespace("Ron");

Ron.ErrorDialog = Ext.extend(Ext.Window, {
	// Constructor Defaults, can be overridden by user's config object
	id: 'error-dialog-popup',	
	
	initComponent: function() {
                
        var errorCode = Ext.DomQuery.selectValue('Code', this.errorResponse);
        var message = Ext.DomQuery.selectValue('Message', this.errorResponse);
        var time = Ext.DomQuery.selectValue('Time', this.errorResponse);        
        
	    var submitErrorForm = new Ext.FormPanel({
	    	id: 'error-form',
	    	url : '',
			frame : false,
			bodyStyle : 'padding:5px 5px 0',
			autoScroll:true,
			hideMode:'offsets',	
			items : [{
				xtype : 'fieldset',
				id : 'error-info-fieldset',	
				title: custom_labels.ErrorDialog_errorInfoFieldsetTitle,
				labelWidth : 100,
				collapsible : false,
				collapsed : false,
				autoHeight : true,				
				defaultType : 'textfield',
				hideMode:'offsets',
				width:435,
				defaults: {
					width : 300
				},
				items : [{
					fieldLabel : custom_labels.ErrorDialog_errorCode,
					name : 'errorCode',
					id: 'error-info-errorCode',
					value: errorCode,
					readOnly:true,
					cls:'x-form-text-without-border'
				},{
					fieldLabel : custom_labels.ErrorDialog_message,
					name : 'message',
					id: 'error-info-message',					
					value: custom_labels.ErrorDialog_message_text,
					readOnly:true,
					cls:'x-form-text-without-border'
				},{
					id:'error-info-time',
					hidden:true,
					hideLabel:true,
					value:time
				}]
			},{
				xtype : 'fieldset',
				id : 'send-info-form-fieldset',				
				title: custom_labels.ErrorDialog_sendErrorInfoFieldsetTitle,
				labelWidth : 100,
				collapsible : false,
				collapsed : false,
				autoHeight : true,
				hidden:true,
				width:435,
				defaultType : 'textfield',
				defaults: {
					width : 300
				},
				items : [{
					fieldLabel : custom_labels.ErrorDialog_description,
					xtype:'textarea',
					name : 'description',
					id: 'send-info-form-description',
					allowBlank : false
				}]
			}]	    	
	    });
	    
		Ext.apply(this, {
			title: custom_labels.ErrorDialog_pageTitle,			
			closable: true,		
			autoScroll:true,
			width: 468,
			//height: 288,
			height: 190,
			layout:'fit',
			resizable: false,
			modal: true,
			items: [ submitErrorForm ],
			buttons:[{
					text: custom_labels.ErrorDialog_reportBtn,
					id:'error-dialog-popup-report-btn',
					handler: function(){
						this.hide();
						Ext.getCmp('error-dialog-popup').setHeight(295);
						Ext.getCmp('send-info-form-fieldset').show();
						Ext.getCmp('error-dialog-popup-submit-btn').show();
					}
				},{
					text: custom_labels.ErrorDialog_submitBtn,
					id:'error-dialog-popup-submit-btn',
					hidden:true,
					handler:this.submitErrorForm
				},{
					text: custom_labels.ErrorDialog_cancelBtn,
					id:'error-dialog-popup-cancel-btn',
					handler: function(){					
						Ext.getCmp('error-dialog-popup').close();
					}
				}]
		});
		
		//Call parent (required)
		Ron.ErrorDialog.superclass.initComponent.apply( this, arguments );
	},
	
	submitErrorForm: function(){
		var errFrm = Ext.getCmp('error-form').getForm();
		if(errFrm.isValid()){
			//Do XHR call to server to send email.
			var requestParams = {};
			requestParams.onAction = 'ERROR-DETAILS';
			requestParams.Feature = window.lookup_codes.ERROR_PARAM_FEATURE_VALUE;		
			requestParams.ErrorCode = Ext.getCmp('error-info-errorCode').getValue();
			requestParams.ErrorMessage = custom_labels.ht.get(Ext.getCmp('error-info-errorCode').getValue());
			requestParams.ErrorTime = Ext.getCmp('error-info-time').getValue();
			requestParams.UserComment = Ext.getCmp('send-info-form-description').getValue();
			
			//XHR call to enter response in DB
			handleXHRCalls(custom_labels.HANDLER_URL, requestParams);			
			
		} else {
		    Ext.Msg.alert('Error', custom_labels.validationFailure);
		    return false;
		}		
	},
	
	//Override other inherited methods
	onRender: function() {
		//Call parent (required)
		Ron.ErrorDialog.superclass.onRender.apply( this, arguments );
	}
		
});
