Ext.ns('Ext.pv.security');
Ext.pv.security.TermsAndConditionsDialog = Ext.extend(Ext.Window, {
	id : 'termsAndConditionsDialogId',
	frame : true,
    layout : 'border',
    border : false,
    width : 800,
    height : 550,
    modal : true,
    
    // Localized strings
    title : 'Terms of Use',
    agreeText : 'I agree',
    disagreeText : 'I disagree',
	okButtonText : 'OK',
	cancelButtonText : 'Cancel',
    
	direction : 'ltr',
    userId : 0,
    contextPath : null,
    
    requireAgreement : true,
	agreeTermsAndConditionsCallback : function() {},
	disagreeTermsAndConditionsCallback : function() {},

	/**
	 * initComponent
	 * 
	 * @protected
	 */
	initComponent : function() {

		if (this.direction=='rtl') {
			this.style = 'direction:rtl';
		}
		
		this.agreed = false;
		
		this.agreementPanel = new Ext.Panel({
			region : 'south',
			bodyStyle : 'valign: middle; padding-left: 10px; background: #e2e2e2; ',
			border : false,
			autoHeight: true,
			items : [new Ext.form.RadioGroup({
				id : 'radioGroupId',
	    		width : 150,
	    		cls : 'grey-x-panel-body',
	            items: [{
	            	id: 'agreeRadioId',
	            	boxLabel: this.agreeText, 
	            	name: 'agree', 
	            	inputValue: 'true',
	            	value: 'one',
	            	width: 75,
	            	handler: this.onRadioSelect
	            },{
	            	id: 'disagreeRadioId',
	            	boxLabel: this.disagreeText, 
	            	name: 'agree', 
	            	inputValue: 'false',
	            	value: 'two',
	            	width: 75,
	            	handler: this.onRadioSelect
	            }]
	        })
	        ]
		});
		
		this.items = new Array();
		this.items[this.items.length] = new Ext.Panel({
			region : 'center',
			layout : 'fit',
			autoScroll : true,
			bodyStyle : 'padding: 5px;',
			autoLoad : {
				url : this.contextPath + '/security/termsAndConditions.html',
				//scripts : true,
				noCache : true
			}
		});
		if (this.requireAgreement) {
			this.items[this.items.length] = this.agreementPanel;
		}
      	
		this.buttons = new Array();
		this.buttons[this.buttons.length] = new Ext.Button({
			id : 'okButtonId',
			text : this.okButtonText,
			disabled : (this.requireAgreement)?true:false,
			handler : this.onOk,
			scope : this
	    });
		if (this.requireAgreement) {
			this.buttons[this.buttons.length] = new Ext.Button({
				text : this.cancelButtonText,
				handler : this.onCancel,
				scope : this
		    });
		}
		
		this.on(
	    	'close',
	    	function() {
	    		this.onClose();
	    	},
	    	this
	    );

		// super
		Ext.pv.security.TermsAndConditionsDialog.superclass.initComponent.call(this);
	},
	
	onRadioSelect : function() {
		var okButton = Ext.getCmp('okButtonId');
		okButton.enable();
	},
	
	onOk : function() {
		if (this.requireAgreement) {
			var agreeRadioEl = Ext.getCmp('agreeRadioId');
			if (agreeRadioEl.getGroupValue()=='true') {
				this.agreed = true;
				this.acceptTermsAndConditions(this.userId);
			} else {
				this.close();
			}
		} else {
			this.close();
		}
	},
	
	onCancel : function() {
		this.close();
	},
	
	onClose : function() {
		if (this.requireAgreement) {
			if (!this.agreed) {
				this.disagreeTermsAndConditionsCallback();
			}
		}
	},
	
	acceptTermsAndConditions : function(userId) {
		var thisDialog = this;
		Ext.Ajax.request( {
			url : this.contextPath + '/security/acceptTermsAndConditions.do',
			method : 'POST',
			success : function(result, request) {

				var response = null;
				cmd = 'response = ' + result.responseText + ';';
				eval(cmd);
				if (response!=null && response.success!=null) {
					thisDialog.agreeTermsAndConditionsCallback();
					thisDialog.close();
				}

			},
			params : {
				userId : userId
			}
		});
	}
	
    
});
Ext.reg('TermsAndConditionsDialog',Ext.pv.security.TermsAndConditionsDialog);
