var report = {
	appEnvironment: null,
	init: function(appEnvironment) {
        report.appEnvironment = appEnvironment;
        report.bindButtons();
	},
    bindButtons: function() {
        jQ('#rxgrab-main-tab-report-content .execute-report').unbind('click.execute').bind('click.execute', report.execute);
    },
	execute: function() {
		if(report.validate_fields(jQ(this).data('rx-container-id'))){
			var params_string='';
			if(jQ(this).data('paramsids')!='') {
				var paramsIds = jQ(this).data('paramsids').split('|'),
                    params = jQ(this).data('params').split('|');
				for(var i=0;i<paramsIds.length;i++) {
                    if((jQ('#'+paramsIds[i]).attr('type')=='checkbox' && jQ('#'+paramsIds[i]).prop('checked')) || jQ('#'+paramsIds[i]).attr('type')!='checkbox') {
                        params_string += '/'+params[i]+'/'+(jQ('#'+paramsIds[i]).val()!='' ? jQ('#'+paramsIds[i]).val().replace(/\//g, '-') : 'null');
                    }
				}				
			}
			window.open((report.appEnvironment === 'dev' ? '/app_dev.php/' : '/')+jQ(this).data('url')+params_string, '_blank');
		}
	},
	validate_fields: function(containerId) {
		
		var errors = '';
		
		jQ(containerId+' .required').each(function(){
			if(jQ(this).val()==''){
				errors += jQ(this).data('ref-label') + ' should not be blank.' + '\n';
			}
		});
		
		if(errors=='') {
			return true;
		} else {
			alert(errors);
			return false;
		}
	}
};
