window['${/parameter/@bp_seq}${/parameter/@layout_code}_SAVE_LAYOUT_DYNAMIC_CLICK']=function(nextStep){
	window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
	var root_ds=$('${/parameter/@bp_seq}${/parameter/@layout_code}_virtual_ds');
	window['${/parameter/@layout_code}_ignore_required_before_save']();
    if (root_ds.validate()) {
        var winid = '${/parameter/@winid}';
        var root_record = root_ds.getCurrentRecord();
        root_ds.setSubmitParameter('layout_code', '${/parameter/@layout_code}');
        function ON_LAYOUT_DYNAMIC_BEFORE_SAVE(){
        	root_ds.un('beforesubmit',ON_LAYOUT_DYNAMIC_BEFORE_SAVE);
        	var flag = false;
        	for (var name in root_record.data) {
                if (root_record.data[name].data[0].ds && root_record.data[name].data[0].ds instanceof $L.DataSet) {
                    var current_record = root_record.data[name].data[0].ds.getCurrentRecord();
                    flag = window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_before_submit'](root_record.data[name].data[0].ds, current_record);
                }
            }
            if(!flag){
            	UN_SUBMIT_REF_FUNCTION();
            }
			window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
        	return flag;
        }
        
        function UN_SUBMIT_REF_FUNCTION(){
			root_ds.un('submitsuccess', ON_LAYOUT_DYNAMIC_INNER_SUBMITSUCCESS);
	        root_ds.un('submitfailed', ON_LAYOUT_DYNAMIC_INNER_SUBMITFAILED);
	    	root_ds.un('submiterror', ON_LAYOUT_DYNAMIC_INNER_SUBMITERROR);
		}
	
		function ON_LAYOUT_DYNAMIC_INNER_SUBMITSUCCESS(ds, res) {
	        UN_SUBMIT_REF_FUNCTION();
	        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_submitsuccess'](ds,root_record,res);
	        if (typeof(nextStep) == 'function') {
	            nextStep(ds);
	        }
	    }
	    function ON_LAYOUT_DYNAMIC_INNER_SUBMITFAILED(){
	    	UN_SUBMIT_REF_FUNCTION();
	    	window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
	    	
	    }
	    function ON_LAYOUT_DYNAMIC_INNER_SUBMITERROR(){
	    	UN_SUBMIT_REF_FUNCTION();
	    	window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
	    	
	    }
	    root_ds.on('beforesubmit',ON_LAYOUT_DYNAMIC_BEFORE_SAVE);
	    root_ds.on('submitsuccess', ON_LAYOUT_DYNAMIC_INNER_SUBMITSUCCESS);
	    root_ds.on('submitfailed', ON_LAYOUT_DYNAMIC_INNER_SUBMITFAILED);
	    root_ds.on('submiterror', ON_LAYOUT_DYNAMIC_INNER_SUBMITERROR);

    	root_ds.submit();
    }else{
    	window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
    }
};