// (c) Jean Luc Biellmann - Groupe Ressources - 2004/2010

//network.http.max-connections-per-server

var _FormViewCtrl = {
	cn : '', // class name
	tbl : '', // SQL tablename
	mode : '', // ls, edit, ...
	lastEditTpl : '', // memo for edit mode
	updEditTpl : false,
	toLs : false,
	statusId : 'formViewCtrlStatus',
	_Status : new Status('formViewCtrlStatus'),
	init : function (cn,tbl) {
		// load main template at startup
		if (!$('formViewCtrl'))
			_FormViewCtrl.json('c=FormViewCtrl&f=init');
		// update form values with current cn and tbl
		if (_FormViewCtrl.cn!=cn) {
			_FormViewCtrl.cn = cn;
			// needed for CSV and ODT exports...
			$('formViewCtrl__c').value = cn;
			_FormViewCtrl.tbl = tbl;
		}	
		_FormViewCtrl.updEditTpl = true;
		_FormViewCtrl.ls(''); // default action...	
	},
	updSubMenu : function () {
		// submenu icons are displayed according to current mode
		$('formViewCtrl').select('.submenu').each(function (obj) {
			if (obj.hasClassName('isadmin') && usr['acl']!='admin') 
				obj.hide();
			else
				obj.hasClassName(_FormViewCtrl.mode) ? obj.show() : obj.hide();
		});
	},
	setMode : function (mode) {
		_FormViewCtrl.mode = mode;
		_FormViewCtrl.updSubMenu();
		// autoload edit template in edit mode
		if (mode=='edit' && _FormViewCtrl.updEditTpl) {
			_FormViewCtrl.json('c='+_FormViewCtrl.cn+'&f=loadTpl&tplpath='+_FormViewCtrl.mode);
			_FormViewCtrl.resetTpl();
			_FormViewCtrl.updEditTpl = false;
		}
	},
	ls : function (action) {
		_FormViewCtrl.setMode('ls');
////console.log('c='+_FormViewCtrl.cn+'&f=ls&'+action);
		_FormViewCtrl.json('c='+_FormViewCtrl.cn+'&f=ls&'+action);
	},
	resetTpl : function () {
		$('divViewDataBody').update(_FormViewData.tpl[_FormViewCtrl.cn]);
		$$('#formViewData input[type=text]').each(function (input) {
			if (input.hasClassName('autocompletion')) 
				new AutoCompletion(input, $('autocompletepanel'));
		});			
		$$('#formViewData input,#formViewData select,#formViewData textarea').each(function (element) {
			Element.observe(element, 'focus', function (e) { $('autocompletepanel').hide() });
		});
		_FormViewCtrl.lastEditTpl = _FormViewCtrl.cn;
	},
	edit : function (action) {
		_FormViewCtrl.setMode('edit');
		_FormViewCtrl.json('c='+_FormViewCtrl.cn+'&f=edit&'+action);	
	},
	add : function () {
		// we need a request to get a session edit key
		_FormViewCtrl.edit('add=1');
	},
	last : function () {
		_FormViewCtrl.edit('last=1');
	},
	copy : function () {
		if (_FormViewCtrl.mode=='edit')
			if ($(_FormViewCtrl.tbl+'__id')) {
				$(_FormViewCtrl.tbl+'__id').value = '';
				_FormViewCtrl._Status.info('Fiche actuelle copiée !');
			}
	},
	jump : function (offset) {
		switch (_FormViewCtrl.mode) {
			case 'ls': // previous or next page
				_FormViewCtrl.ls('offset='+offset);	
				break;
			case 'edit': // previous or next file
				if ($(_FormViewCtrl.tbl+'__id')) {
					var id = parseInt($(_FormViewCtrl.tbl+'__id').value);
					if (id>0)
						_FormViewCtrl.edit('id='+id+'&offset='+offset);
				}
				break;
		}
	},
	rec : function () {
		this.toLs = false;
		if (_FormViewCtrl.mode=='edit')
			_FormViewCtrl.json('c='+_FormViewCtrl.cn+'&f=rec');
	},
	rec2 : function () {
		this.toLs = true;
		if (_FormViewCtrl.mode=='edit')
			_FormViewCtrl.json('c='+_FormViewCtrl.cn+'&f=rec');
	},
	resetSearch : function () {
		$('formViewCtrl__kws').value = '';
		_FormViewCtrl.search();
	},
	search : function () {
		if (_FormViewCtrl.mode=='edit' && !confirm('Abandonner l\'édition courante (vous perdrez les modifications non enregistrées !!!) et relancer la recherche ?'))
			return false;
		var kws = String($('formViewCtrl__kws').value).replace(/(^\s*|\s*$)/, '');		
		$('formViewCtrl__kws').value = kws.escapeHTML();			
		_FormViewCtrl.ls('formViewCtrl[kws]='+kws.escapeHTML())
	},
	del : function () {
		if (_FormViewCtrl.mode=='edit')
			if ($(_FormViewCtrl.tbl+'__id') && confirm('ATTENTION : la fiche sera défivement effacée ! Continuer ?'))
				_FormViewCtrl.json('c='+_FormViewCtrl.cn+'&f=del&&id='+$(_FormViewCtrl.tbl+'__id').value);
	},
	json : function (args) {
		_JSON.onSuccess = (function(json) {

			$('autocompletepanel').hide();
			
			if (json.reset=='1') {
				$('formViewCtrl').reset();
				$('formViewData').reset();
			}

			if (json.loadTpl)
				_FormViewData.tpl[_FormViewCtrl.cn] = json.tplDatas;

			if (json.search==1 && json.rows) 
				_FormViewCtrl.mode = 'ls';
				
			// edit mode
			if (_FormViewCtrl.mode=='edit') {
				if (json.editKey)
					$('formViewData__editKey').value = json.editKey;
				if (json.add || json.del || !$(_FormViewCtrl.tbl+'__id'))
					_FormViewCtrl.resetTpl();
				if (json.add)
					_FormViewCtrl._Status.info('Ajout d\'une nouvelle fiche !');
				if (json.row) {
					$('divViewDataBody').style.overflow='auto';
					_FormViewData.lastRowId[_FormViewCtrl.cn] = json.row['id'];
					_FormCtrl.fill(_FormViewCtrl.tbl,json.row);
					_FormViewAction.postView('formViewData');
				}
				if (json.last) {
					$(_FormViewCtrl.tbl+'__id').value='';
					_FormViewCtrl._Status.info('Dernière fiche copiée !');
				}
				if (json.isadmin=='1')
					if ($$('.givemefocusplease').length)
						$$('.givemefocusplease')[0].focus();
			}
	
			// ls mode
			if (_FormViewCtrl.mode=='ls') {
				_FormViewData.reset();
				if (json.rows && json.rows.length) {
					$('formViewCtrl__kws').value = json.page.kws.escapeHTML();
					$('divViewDataBody').scrollTop = 0;
					$('divViewDataBody').style.overflow='hidden';
					var _TableLs = new TableLs(json.tbl,json.rows,'divViewDataBody');
					// special addons for URL, mails, ...
					_FormViewAction.postView('formViewData');
					// force to resize design table according to current viewport
					//_FormViewData.resize();
					// fix slots
					_TableLs.slotEdit = function (e) {
						if (_TableLs.hover!=null)
							_FormViewData.lsRowEdit(_TableLs.hover);
					};
					// fix events
					_TableLs.addEvents();
					// fix last row position
					_TableLs.setLastRowId(_FormViewData.getLastRowId());
					$('formViewCtrl__pager').update('Page '+json.page['cur']+'/'+json.page['max']);
					$('formViewCtrl__count').update(json.page['count']+' fiche'+(json.page['count']>0 ? 's' : ''));
				} else {
					$('formViewCtrl__pager').update('Page 0');
					$('formViewCtrl__count').update('0 fiche');
				}
			}
			
			if (json.rec && this.toLs)
				this.ls();	

			if (json.updCSV) {
				_FormViewCtrl._Status.info('Mise à jour effectuée !');
				window.location.reload();
			}
				
		}).bind(this);
		_JSON.send('m=formview&'+args,{
			formId:($('formViewData') ? 'formViewData' : ''),
			statusId:'formViewCtrlStatus'
		});
	},
	updShowRoom : function () {
		if (confirm('Confirmer la mise à jour de la boutique ?')) 
			_FormViewCtrl.json('c=ShowRoomDB&f=updCSV');
	},
	csv : function () {
		document.location = 'index.php?m=formview&c=ShowRoomDB&f=exportCSV';
	},
	chkShowRoom : function () {
		_FormViewCtrl.json('c=ShowRoomDB&f=chkDB');
	}
};


