/**
Helpsite for enuGene v0.9.x  (c) Copyright 2007-2009 by Bogdan Vojska
File: Helpsite javascript
*/

function hlink (relpath, flenme) {
	var suff = '.html'
	var hpath = relpath + flenme + suff;
	window.open(hpath,'_self');
}

function hover (obj) {
	(obj.className == '' || obj.className == 'navlnk') ? obj.className = 'navlnk_hover' : obj.className = 'navlnk';
}

function changeSrcMinMax(obj) {
	var arrObj = obj.src.split('_');
	var lastElm = arrObj[arrObj.length - 1];
	var arrLastElm = lastElm.split('.');
	var fleSuff = arrLastElm[arrLastElm.length - 1];
	var imgSize = arrLastElm[0];
	
	if (imgSize == 'min') {
		arrObj[arrObj.length - 1] = 'max.' + fleSuff;
		obj.src = arrObj.join('_');
	}
	if (imgSize == 'max') {
		arrObj[arrObj.length - 1] = 'min.' + fleSuff;
		obj.src = arrObj.join('_');
	}
}

function clsMgenSite(name) {
	this.name = name;
	this.basePath = {
		href : document.location.href,
		pathname : document.location.pathname,
		submitPath : ''
	};

	this.submit = function (formId, strTask, submitPath) {
		if (this.onSubmit) this.onSubmit();
		var form = this.DOM.getObject( formId );
		form.target = '_self';
		form.method = 'post';
		Site.basePath.submitPath = submitPath;

		var sec_word = this.DOM.getObject('sec_word');
		if (sec_word) {
			secword = document.createElement( 'input' );
			secword.style.display='none';
			secword.name = 'sec_word';
			secword.value = sec_word.content;
			form.appendChild( secword );
		}
		
		// append the task control for the server
		var newInput = document.createElement( 'input' );
		newInput.style.display='none';
		newInput.id='task';
		newInput.name='task';
		newInput.value = strTask;
		form.appendChild( newInput );
	
		// append the taskOptions control for the server
		newInput = document.createElement( 'input' );
		newInput.style.display='none';
		newInput.id='taskOptions';
		newInput.name='taskOptions';
		newInput.value = this.taskOptions.get();
		form.appendChild( newInput );
		
		// append the tab controls section states values
		if (this.controls && this.controls.tabs) {
			newInput = document.createElement( 'input' );
			newInput.style.display='none';
			newInput.id='sectionStates';
			newInput.name='sectionStates';
			newInput.value = this.controls.states.wrap(this.controls.states.controls);;
			form.appendChild( newInput );
		}
		
		if (Site.basePath.submitPath) {
			var arrTask = strTask.split('.');
			var search = '';
			arrTask.length == 1 ? search = '?task=' + arrTask[0] : search = '?task=' + arrTask[0] + '&command=' + arrTask[1];
			form.action = Site.basePath.submitPath + search;
			form.submit();
			Site.taskOptions.clear( );
		}
	};
	
	this.parseAsyncResponse = {
		innerHTML : '',
		
		stdDialog : function (response) {
			var arrResponse = response.split('-::-');
			this.innerHTML = arrResponse[2];
			Site.dialog.show( 'mgen_stdDialog', arrResponse[0], 310, 170, arrResponse[2], '' );
			Site.taskOptions.set( 'formId',  arrResponse[1] );
			document.body.style.cursor = "auto";
		},
		
		createUserAuth : function (response) {
			var arrResponse = response.split('-::-');
			var username = arrResponse[0];
			var password = arrResponse[1];
			var rawpassword = arrResponse[2];
			var leaseStartdate = arrResponse[3];
			var title = arrResponse[4];
			var content = arrResponse[5];
			
			Site.DOM.getObject('ctl_editUser_username').value = username;
			Site.DOM.getObject('ctl_editUser_password').value = password;
			Site.DOM.getObject('ctl_editUser_rawpassword').value = rawpassword;
			Site.DOM.getObject('ctl_editUser_leasestart').value = leaseStartdate;
			
			Site.dialog.show( 'mgen_stdDialog', title, 310, 170, content, '' );
			document.body.style.cursor = "auto";
		},
		
		getTargetMenuSize : function (response) {
			var targetObj = Site.DOM.getObject('ctl_editMenuItem_order');
			if (!targetObj) return '';
			
			Site.controls.selectBox.setTargetId('ctl_editMenuItem_order');
			var cntlSlct = Site.DOM.getObject( 'ctl_editMenuItem_order' );
			if (cntlSlct) cntlSlct.value = '';
			Site.controls.selectBox.getOptions( response );
		},
		
		loadContentThemeFiles : function (response) {
			// test if the target exists
			var targetObj = Site.DOM.getObject( 'ctl_contentFiles_removeFiles' );
			if (!targetObj) return '';
			// clear any current control value
			var cntlSlct = Site.DOM.getObject( 'ctl_contentFiles_removeFiles' );
			if (cntlSlct) cntlSlct.value = '';
			// load the new optionlist
			Site.controls.selectBox.setTargetId('ctl_contentFiles_removeFiles');
			if (!response) {
				Site.controls.selectBox.clearOptions();
			} else {
				Site.controls.selectBox.getOptions( response );
			}
		},
		
		loadContentindexChapterThemes : function (response) {
			if (!response) return '';
			// test target exists and clear any current control value
			var targetObj = Site.DOM.getObject( 'ctl_contentTheme_theme' );
			if (!targetObj) return '';
			targetObj.value = '';
			// load the new optionlist
			Site.controls.selectBox.setTargetId('ctl_contentTheme_theme');
			Site.controls.selectBox.getOptions( response );
		}
	};
	
	this.location = {
		set : function (href) {
			// anywhere within the href 'noSubmit' set? 
			// - leave the location as it is!
			// This is the place to screw up any menu released 
			// javascript actions executed without submit.
			var regex = /(noSubmit)/;
			if (href.search(regex) !== -1) {
				return 'noSubmit';
			}
			// no link provided by menu? - just do 
			// all other actions caused by the mouse down, 
			// but don't send any request.
			if (href) window.location = href;
		},
		
		windowOpen : function (link) {
			window.open ('http://' + link,'_self');
		}
	};
	
	this.controls = {
		append : function (id, type, value, display, targetId) {
			// append the task control or any other control for the server
			if (Site.DOM.getObject( id )) return;
			var newInput = document.createElement( type );
			if (display == 'hidden') {
				newInput.style.visibility='hidden';
			} else {
				var valid = 'none,block,inline'
				if (valid.search( display ) != -1) newInput.style.display = display;
			}
			newInput.id = id;
			newInput.name = id;
			if (type == 'input' || type == 'textarea')
				newInput.value = value;
			else
				newInput.innerHTML = value;
			if (targetId == '') {
				document.body.appendChild( newInput );
			} else {
				var obj = Site.DOM.getObject( targetId );
				if (obj) obj.appendChild( newInput );
			}
		},
		
		selectBox : {
			targetId : '',
			objTarget : '',
			arrOptions : '',
			
		    setTargetId : function(id) {
		         this.targetId = id;
		    },
		    
		    // remove existing options
		    clearOptions : function() {
				for (var i=this.objTarget.options.length - 1; i > -1; i--) {
					this.objTarget.options[i] = null;
					var opt = this.objTarget.options;
					opt = '';
				}
		    },
		    
		    // place the new ones
		    setOptions : function() {
				for (var i=0; i < this.arrOptions.length; i++) {
					this.objTarget.options[i] = new Option( this.arrOptions[i].text, this.arrOptions[i].value );
				}
		    },
		    
		    //responseInnerHTML=arrayOtions; 
			getOptions : function(response) {
				this.targetId ? (this.objTarget = (Site.DOM.getObject( this.targetId )) || '') : this.objTarget = '';
				if (this.objTarget && response) {
					this.clearOptions();
					this.arrOptions = eval('[' + response + ']');
					this.setOptions();
				}
				this.targetId = '';
				this.objTarget = '';
				document.body.style.cursor = "auto";
			}
		}
	};

	this.taskOptions = {
		options : {},

		clear : function () {
			this.options = new Object( );
		},

		// get one Option from option string by key
		getOption : function (key) {
			return this.options[key];
		},
		
		remove : function (key) {
			delete this.options[key];
		},
		
		// set all options at once
		set : function (key, value) {
			if (value) value = value.replace( "|", '.', 'g' );
			this.options[key] = value;
		},
		
		// return the complete option string
		get : function () {
			var x = '';
			var strReturn = '';
			for (x in this.options) {
				strReturn += x + '=' + this.options[x] + '|';
			}
			strReturn = strReturn.substring( 0, strReturn.length - 1 );
			return strReturn;
		}
	};

	this.DOM = {
		getObject : function (id) {
			var obj = null;
			if (document.all)
				obj = document.all[id];
			if (document.getElementById (id))
				obj = document.getElementById (id);
			return obj;
		},
		
		appendRcsIdentifierControls : function (form, arrOption) {
			// Append the 'rcsId' hidden input control providing the 
			// activated rcs id to the server.
			var rcsId = arrOption[2];
			var ctlRcsId = document.createElement('input');
			ctlRcsId.type = 'hidden';
			ctlRcsId.value = rcsId;
			ctlRcsId.name ='rcsId';
			form.appendChild( ctlRcsId );
			
			var objTablename = Site.DOM.getObject('tablename');
			if (objTablename) {
				var tablename = document.createElement( 'input' );
				tablename.style.display = 'none';
				tablename.name = 'tablename';
				tablename.value = objTablename.value;
				form.appendChild( tablename );
			} else {
				// this is for rescue only
				document.cookie = 'tablename=' + arrOption[3] + '_' + arrOption[4];
			}
		},
		
		appendSecWord : function (form) {
			// append the token for submit security
			var sec_word = Site.DOM.getObject('sec_word');
			if (sec_word) {
				secword = document.createElement( 'input' );
				secword.style.display='none';
				secword.name = 'sec_word';
				secword.value = sec_word.content;
				form.appendChild( secword );
			}
		},
		
		isIe : function () {
			if (document.all) 
				return true; 
			else 
				return false;
		}
	};
	
	this.menu = {
		visible : function (id) {
			var obj = Site.DOM.getObject(id);
			display = obj.style.display;
			if ((display == 'none') || (display == '')) {
				obj.style.display = 'block';
			} else {
				obj.style.display = 'none'; 
			}
		},
		
		item : {
			setState : function (obj) {
				var obj = Site.DOM.getObject( obj.id );
				if (!obj) return false;
				var className = obj.className;
				var arrClassName = className.split('_');
				// don't highlight the current active item
				if (arrClassName[1] == 'current') return;
				// if highlightened the normal state is saved as 
				// obj property - if the prop exists, the obj is 
				// high. Get the saved normal classname from there and 
				// clear the value.
				if (obj.savedClassName) {
					obj.className = obj.savedClassName;
					obj.savedClassName = null;
				} else {
					obj.savedClassName = obj.className;
					obj.className = obj.className + '_hover';
				}
			}
		}
	};

	this.event = {
		parent : this,
		menu : {
			mouseDown : function (obj) {
				if (obj) document.cookie='menuId=' + obj.id;
			}
		},
		item : {
			parent : this,
			mouseOver : function (obj) {
				Site.menu.item.setState(obj);
			},
			mouseOut : function (obj) {
				Site.menu.item.setState(obj);
			},
			// this opens the menus
			mouseDown : function (obj,hassub) {
				if (obj) document.cookie='itemId=' + obj.id;
				if (hassub) this.parent.menu.visible( hassub );
			},
			// this executes the links and js actions
			mouseUp : function (obj, action, actionType) {
				if (obj) document.cookie='itemId=' + obj.id;
				if (action) {
					eval(action);
				}
			}
		},
		
		sct : function (obj, option) {
			switch (obj.id) {
			case 'ctl_editMenuItem_contentkey' :
				// target is 'ctl_editMenuItem_command'
				var objTarget = Site.DOM.getObject(option);
				// If some content is selected, set the command 
				// control to the key of the target content template.
				if (objTarget && obj.value != '-select-' && obj.value != 'command') {
					objTarget.value = obj.value;
				}
				break;
			case 'ctl_editMenuItem_task' :
				// target is 'ctl_editMenuItem_command'
				var objTarget = Site.DOM.getObject(option);
				// If some content is selected, set the command 
				// control to the key of the target content template.
				if (objTarget && obj.value == 'public_link') {
					objTarget.value = 'link';
				}
				break;
			case 'ctl_contentTheme_select' :
				switch (option) {
				case 'getThemes' :
					if (!obj.value) return;
					Site.taskOptions.set('chapter', obj.value);
					AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.loadContentindexChapterThemes', 'root_content.get_contentindex_chapterThemes', true );
					break;
				default :
					var objChapter = Site.DOM.getObject('ctl_contentTheme_chapter');
					var objContent = Site.DOM.getObject('ctl_contentTheme_path');
					// If some content is selected, set the command 
					// control to the key of the target content template.
					if (objChapter && obj.value != '-select-') {
						objChapter.value = obj.value;
						objContent.value = obj.value;
					}
					if (objChapter && obj.value == '-select-') {
						objChapter.value = '';
					}
					if (objChapter && obj.value == 'create') {
						objChapter.value = 'NewChapter';
					}
					break;
				}
			case 'ctl_contentFiles_theme' :
				switch (option) {
				case 'appendFiles' :
					// publish selected theme's rcsId
					var objTarget = Site.DOM.getObject('rcsId');
					if (!objTarget) return;
					objTarget.value = obj.value;
					break;
				case 'removeFiles' :
					if (!obj.value) return;
					var objRcsId = Site.DOM.getObject('rcsId');
					objRcsId.value = obj.value;
					Site.taskOptions.set('rcsId', obj.value);
					// fill the file select box with the theme files
					AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.loadContentThemeFiles', 'root_content.get_theme_ContentFiles', true );
					break;
				break;
				}
			case 'ctl_contentTheme_theme' :
				switch (option) {
				case 'setRcsId' :
					if (!obj.value) return;
					var objRcsId = Site.DOM.getObject('rcsId');
					objRcsId.value = obj.value;
					break;
				}
			}
		},
		
		cmd : function (task, formId, option) {
			var opt = '';
			switch (task) {
			case 'member_login_submit' :
				Site.submit(formId, 'noAuth_log.member_login_submit', './index.php' );
				break;
			case 'member_ureg_submit' :
				Site.submit(formId, 'noAuth_registration.member_userReg_submit', './index.php' );
				break;
			case 'autoTable_delete_checked' :
				Site.submit(formId, option + '.autoTable_delete_checked', './index.php' );
				break;
				
			case 'editUser_update' :
				Site.submit(formId, 'root_user.editUser_update', './index.php' );
				break;
			case 'editUser_delete_open' :
				Site.taskOptions.set('formId', formId);
				AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.stdDialog', 'root_user.dialog_editUser_delete', true );
				break;
			case 'editUser_delete_cancel' :
				Site.dialog.hide( );
				break;
			case 'editUser_delete' :
				var formId = Site.taskOptions.getOption('formId');
				Site.dialog.hide( );
				Site.submit(formId, 'root_user.editUser_delete', './index.php' );
				break;
			case 'editUser_authenticate_open' :
				AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.createUserAuth', 'root_user.dialog_editUser_authenticate', true );
				break;
			case 'editUser_authenticate_close' :
				Site.dialog.hide( );
				break;
			case 'editUser_back' :
				Site.submit(formId, 'history_back', './index.php' );
				break;
				
			case 'editQueryDef_insert' :
				Site.submit(formId, 'root_queryDef.editQueryDef_insert', './index.php' );
				break;
			case 'editQueryDef_update' :
				Site.submit(formId, 'root_queryDef.editQueryDef_update', './index.php' );
				break;
			case 'editQueryDef_delete_open' :
				Site.taskOptions.set('formId', formId);
				AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.stdDialog', 'root_queryDef.dialog_editQueryDef_delete', true );
				break;
			case 'editQueryDef_delete_cancel' :
				Site.dialog.hide( );
				break;
			case 'editQueryDef_delete' :
				var formId = Site.taskOptions.getOption('formId');
				Site.dialog.hide( );
				Site.submit(formId, 'root_queryDef.editQueryDef_delete', './index.php' );
				break;
			case 'editQueryDef_back' :
				Site.submit(formId, 'history_back', './index.php' );
				break;
			case 'editMenuItem_back' :
				Site.submit(formId, 'history_back', './index.php' );
				break;
			case 'editMenuItem_insert' :
				Site.submit(formId, 'root_menu.editMenuItem_insert', './index.php' );
				break;
			case 'editMenuItem_update' :
				Site.submit(formId, 'root_menu.editMenuItem_update', './index.php' );
				break;
			case 'editMenuItem_delete_cancel' :
				Site.dialog.hide( );
				break;
			case 'editMenuItem_delete' :
				var formId = Site.taskOptions.getOption('formId');
				Site.dialog.hide( );
				Site.submit(formId, 'root_menu.editMenuItem_delete', './index.php' );
				break;
			case 'menuLanguage_insert' :
				Site.submit(formId, 'root_menu.menuLanguage_insert', './index.php' );
				break;
			case 'menuLanguage_update' :
				Site.submit(formId, 'root_menu.menuLanguage_update', './index.php' );
				break;
				
			case 'contentTheme_insert' :
				Site.submit(formId, 'root_content.contentTheme_insert', './index.php' );
				break;
			case 'contentTheme_update' :
				Site.submit(formId, 'root_content.contentTheme_update', './index.php' );
				break;
			case 'contentTheme_move' :
				Site.submit(formId, 'root_content.contentTheme_move', './index.php' );
				break;
			case 'contentTheme_remove' :
				// get the cached formId
				var formId = Site.taskOptions.getOption('formId');
				Site.dialog.hide( );
				Site.submit(formId, 'root_content.contentTheme_remove', './index.php' );
				break;
			case 'contentTheme_removeDialog_open' :
				Site.taskOptions.set('formId', formId);
				AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.stdDialog', 'root_content.dialog_contentTheme_remove', true );
				break;
				
			case 'contentFiles_append' :
				Site.submit(formId, 'root_content.contentFiles_append', './index.php' );
				break;
			case 'contentFiles_remove' :
				// get the cached formId
				var formId = Site.taskOptions.getOption('formId');
				Site.submit(formId, 'root_content.contentFiles_remove', './index.php' );
				break;
			case 'contentFiles_removeDialog_open' :
				// cache the form id
				Site.taskOptions.set('formId', formId);
				AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.stdDialog', 'root_content.dialog_contentFiles_remove', true );
				break;
			case 'contentFiles_recycle' :
				Site.submit(formId, 'root_content.contentFiles_recycle', './index.php' );
				break;
				
			case 'download_insert' :
				Site.submit(formId, 'root_downloadManager.download_insert', './index.php' );
				break;
			case 'download_update' :
				Site.submit(formId, 'root_downloadManager.download_update', './index.php' );
				break;
				
			case 'contentTrash_delete' :
				Site.submit(formId, 'root_content.contentTrash_delete', './index.php' );
				break;
				
			case 'stdDialog_close' :
				Site.dialog.hide( );
				break;
				
			case 'history_back' :
				Site.submit(formId, 'history_back', './index.php' );
				break;
				
			case 'usrDownload_send' :
				Site.submit(formId, 'public_download.usrDownload_send', './index.php' );
				break;
				
			case 'noSubmit' :
				break;
				
			default :
			}
		},
		
		row_switch_highlight : function (obj) {
			if (this.parent.activeRow) {
				this.parent.activeRow.className = 'tblRow';
				obj.className = 'tblRow';
			}
			// this.parent is the tableFrame
			this.parent.activeRow = obj;
			obj.className = 'tblRowHigh';
			var rowIndex = Site.DOM.getObject( 'mgen_menumngr_tableFrame_rowIndex' );
			if (rowIndex) rowIndex.innerHTML = obj.id.split('_')[2];
		},
		
		menuMngr_filter_mainmenu : function(obj) {
			// append the token for submit security
			var mainmenuId = obj.id.split('_')[2];
			var formId = 'frm_mainmenu_' + mainmenuId;
			var form = Site.DOM.getObject( formId );
			if (mainmenuId) {
				ctlMmId = document.createElement( 'input' );
				ctlMmId.style.display='none';
				ctlMmId.name = 'ctl_menumngr_mainmenuId';
				ctlMmId.value = mainmenuId;
				form.appendChild( ctlMmId );
			}
			Site.submit(formId, 'root_menu.open_table_menus', './index.php' );
		},
		
		tablerow_edit : function (obj, submitPath) {
			formId = obj.parentNode.id;
			form = obj.parentNode;
			var arrOption = formId.split('_');
			if (!arrOption) {
				return false;
			}

			// Append the 'rcsId' and tablename hidden input controls 
			// providing the activated rcs id to the server. 
			Site.DOM.appendRcsIdentifierControls( form, arrOption);
			
			// append the token for submit security
			Site.DOM.appendSecWord( form );

			obj.parentNode.method = 'post';
			obj.parentNode.action = submitPath;
			obj.parentNode.submit();
		},
		
		tablerow_delete : function (obj, submitPath) {
			formId = obj.parentNode.id;
			form = obj.parentNode;
			var arrOption = formId.split('_');
			if (!arrOption) {
				return false;
			}

			// Append the 'rcsId' and tablename hidden input controls 
			// providing the activated rcs id to the server. 
			Site.DOM.appendRcsIdentifierControls( form, arrOption);
			
			// append the token for submit security
			Site.DOM.appendSecWord( form );
			Site.taskOptions.set('formId', formId);
			AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.stdDialog', 'root_menu.dialog_editMenuItem_delete', true );
		},
		
		open_form_editMenuItem_update : function () {
			var ctl = '';
			var activeRow = Site.DOM.getObject( 'mgen_menumngr_tableFrame_rowIndex');
			var formId = 'frm_item_' + activeRow.innerHTML;
			var form = Site.DOM.getObject( formId );
			if (activeRow && form) {
				ctl = document.createElement( 'input' );
				ctl.style.display='none';
				ctl.name = 'rcsId';
				ctl.value = activeRow.innerHTML;
				form.appendChild( ctl );
			}
			Site.submit(formId, 'root_menu.open_form_editMenuItem_update', './index.php' );
		},
		
		/**
		 * This is a editMenuItem function detecting the 
		 * amount of items within a menu
		 */
		getTargetMenuSize : function (ctlParentItem) {
			// is in mainmenu
			if (ctlParentItem.value == '0') {
				var mainId = Site.DOM.getObject('ctl_editMenuItem_mainmenu').value;
				if (mainId) ctlParentItem.value = mainId;
				Site.taskOptions.set('parentId', mainId);
			} else {
				// value is the id of the selected item to be the parent of the new item
				Site.taskOptions.set('parentId', ctlParentItem.value);
			}
			AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.getTargetMenuSize', 'root_menu.getTargetMenuSize', true );
		}
	};

}

function clsMgenAutoTable() {
	
	this.getObject = function (id) {
		var obj = null;
		if (document.all)
			obj = document.all[id];
		if (document.getElementById (id))
			obj = document.getElementById (id);
		return obj;
	};
	
	this.submit = function (formId,task,submitPath) {
		Site.submit(formId, task, submitPath );
	};
	
	this.sql = {
		pageParams : '',
		parent : this,
		
		get_selected_searchFields : function (obj_tbl_search_field) {
			var options = obj_tbl_search_field.options;
			var count = 0;
			obj_tbl_search_field.index_selected_searchFields = [];
			for (x in options) {
				if (x == 'length' || x == 'indexSelected') continue;
				if (options[x].selected) {
					obj_tbl_search_field.index_selected_searchFields[count] = options[x].value;
					++count;
				}
			}
		},
		
		collect : function ( limitoffset ) {
			var obj_tbl_search_field = this.parent.getObject('tbl_search_field');
			this.get_selected_searchFields( obj_tbl_search_field );
			return {
				resultFields : eval('[' + this.parent.getObject('tbl_search_resultFields').value + ']'),
				tablename : this.parent.getObject('tbl_search_tablename').value,
				submitPath : this.parent.getObject('tbl_search_submitPath').value,
				input : this.parent.getObject('tbl_search_input').value,
				like : this.parent.getObject('tbl_search_like').value,
				selected_searchFields : obj_tbl_search_field.index_selected_searchFields,
				operator : this.parent.getObject('tbl_search_operator').value,
				order : this.parent.getObject('tbl_search_order').value,
				desc : this.parent.getObject('tbl_search_desc').value,
				limitrows : this.parent.getObject('tbl_search_limitrows').value,
				// if action is GO search - offset is always zero
				limitoffset : limitoffset
			};
		},
		
		create : function (objParams, limitoffset) {
			var strReturn = '';
			var like = '';
			var input = '';
			var limit = '';
			var desc = '';
			
			var sqlSelect = '';
			var sqlWhere = '';
			var sqlSourceFields = '';
			var sqlOrder = '';
			var sqlDesc = '';
			var sqlLimit = '';
			
			objParams.tablename ? this.tablename = objParams.tablename : this.tablename = '';
			
			// source fields
			objParams.resultFields ? sqlSourceFields = objParams.resultFields.join('`,`') : sqlSourceFields = '*';
			if (this.tablename) {
				sqlSelect = 'SELECT SQL_CALC_FOUND_ROWS `' + sqlSourceFields + '` FROM ' + this.tablename + ' ';
			} else {
				sqlSelect = 'SELECT SQL_CALC_FOUND_ROWS `' + sqlSourceFields + '` |tablename| '
			}
			strReturn += sqlSelect;
			
			// compose compare pattern
			switch (objParams.like) {
			case 'like' :
				objParams.input ? input = " LIKE '%" + objParams.input + "%'" : input = "''";
				break;
			case 'equal' :
				objParams.input ? input = "=" + objParams.input : input = "''";
				break;
			case 'smaller' :
				objParams.input ? input = "<" + objParams.input : input = "''";
				break;
			case 'larger' :
				objParams.input ? input = "<" + objParams.input : input = "''";
				break;
			}
			
			// where
			if (objParams.selected_searchFields.length && objParams.input) {
				var sqlWhere = 'WHERE ';
				var flds = objParams.selected_searchFields;
				for (x in flds) {
					sqlWhere += "`" + flds[x] + "`" + input;
					if (x < flds.length - 1) sqlWhere += ' ' + objParams.operator + ' ';
				}
			}
			strReturn += sqlWhere;
			
			// ordering
			objParams.desc == 'descending' ? desc = ' DESC ' : desc = '';
			objParams.order ? sqlOrder += ' ORDER BY ' + objParams.order + desc : sqlOrder = '';
			sqlDesc ? this.sqlDesc = sqlDesc : this.sqlDesc = false;
			strReturn += sqlOrder;
			
			// limiting
			if ((objParams.limitoffset == '0' || objParams.limitoffset) && objParams.limitrows) {
				sqlLimit = ' LIMIT ' + limitoffset + ',' + objParams.limitrows;
			}
			
			if (!sqlLimit && objParams.limitrows) sqlLimit = ' LIMIT 0,' + objParams.limitrows;
			// restrict selection to 500 rows
			if (!objParams.limitrows) {
				sqlLimit = ' LIMIT 0,500';
				if (this.parent.getObject('tbl_search_limitrows'))
					this.parent.getObject('tbl_search_limitrows').value = '500';
			}
			strReturn += sqlLimit;
			return strReturn;
		},
		
		page : function () {
			strReturn += this.sqlSelect;
		}
	};
	
	this.event = {
		parent : this,
		htmlTable_rowIndex : '',
		cmd : function (task, formId, option) {
			var submitPath = '';
			switch (task) {
			case 'search_autoTable' :
				var query = '';
				// gather all values from sqlMaker
				var objParams = this.parent.sql.collect( '0' );
				// create the query string and put it into the submit control 
				if (query = this.parent.sql.create( objParams, '0' )) {
					this.parent.getObject('tbl_search_query').value = query;
				}
				// get the query row limit from the hidden control and 
				// set it to zero, because all new search creates the 
				// query starting with the first row.
				var obj_ctl_limitoffset = this.parent.getObject('tbl_search_limitoffset');
				if (obj_ctl_limitoffset) obj_ctl_limitoffset.value = '0';

				// get the submit task from the hidden control
				submitPath = this.parent.getObject('tbl_search_submitPath').value;
				if (submitPath) {
					frm_sqlMaker = this.parent.getObject('frm_sqlMaker');
					// append the html table checkbox checked row index
					var rowIndex = document.createElement('input');
					rowIndex.type = 'hidden';
					rowIndex.value = this.htmlTable_rowIndex;
					rowIndex.name ='tbl_autoTable_checkedRows';
					frm_sqlMaker.appendChild( rowIndex );
					frm_sqlMaker.action = submitPath;
					frm_sqlMaker.submit();
				}
				break;
			default :
			}
		},
		
		submit_checked : function (obj, submitPath) {
			var formId = obj.parentNode.id;
			var form = obj.parentNode;

			if (!this.htmlTable_rowIndex) return false;
			
			// append the token for submit security
			var sec_word = this.parent.getObject('sec_word');
			if (sec_word) {
				secword = document.createElement( 'input' );
				secword.style.display='none';
				secword.name = 'sec_word';
				secword.value = sec_word.content;
				form.appendChild( secword );
			}
			
			// append the tablename
			var objTablename = this.parent.getObject('tablename');
			if (objTablename) {
				tablename = document.createElement( 'input' );
				tablename.style.display='none';
				tablename.name = 'tablename';
				tablename.value = objTablename.value;
				form.appendChild( tablename );
			}
			
			if (submitPath) {
				// append the html table checked row index
				var rowIndex = document.createElement('input');
				rowIndex.type = 'hidden';
				rowIndex.value = this.htmlTable_rowIndex;
				rowIndex.name ='tbl_autoTable_checkedRows';
				form.appendChild( rowIndex );
				form.method = 'post';
				form.action = submitPath;
				form.submit();
			}

		},
		
		row_switch_highlight : function (obj) {
			if (this.parent.activeRow) {
				this.parent.activeRow.className = 'tblRow';
				obj.className = 'tblRow';
			}
			this.parent.activeRow = obj;
			obj.className = 'tblRowHigh';
		},
		
		open_sqlMaker : function (obj) {
			var pulldown_area = this.parent.getObject('sqlMaker_pulldown_area');
			if (!obj.state) obj.state = 'closed';
			if (obj.state == 'opened') {
				obj.state = 'closed';
				if (obj.childNodes.length) obj.childNodes[0].alt = 'open';
				pulldown_area.style.display = 'none';
				obj.childNodes[0].style.display = 'block';
				obj.childNodes[1].style.display = 'none';
			} else {
				obj.state = 'opened';
				if (obj.childNodes.length) obj.childNodes[0].alt = 'close';
				pulldown_area.style.display = 'block';
				obj.childNodes[0].style.display = 'none';
				obj.childNodes[1].style.display = 'block';
			}
		},
		
		page : function (obj, strLimit, submitPath) {
			var form = this.parent.getObject('frm_paginator')
			if (form) {
				// append the token for submit security
				var sec_word = this.parent.getObject('sec_word');
				if (sec_word) {
					secword = document.createElement( 'input' );
					secword.style.display='none';
					secword.name = 'sec_word';
					secword.value = sec_word.content;
					form.appendChild( secword );
					
					this.parent.getObject('paginator_sqlLimit').value = strLimit;
					form.action = submitPath + '&sqlLimit=' + strLimit;
					form.submit();
				}
			}
		},
		
		/**
		 * The php constructor creates a hidden div element and saves 
		 * all checkbox id's in there. This index is used here to 
		 * read all checked checkboxes id into a table prop as 
		 * char divided string list.
		 * 
		 * If the table obj has no checkboxes set there is no row 
		 * index div element written to the table frame. In such
		 * case the table 'checkedRows' prop is left empty here.
		 * 
		 * @param strHtmlTable - name of the html table container (not db table)
		 */
		set_checked : function (strHtmlTable) {
			var newArrCheckedRows = [];
			var chkBx = '';
			// table not found ? - leave
			var objTbl = this.parent.getObject(strHtmlTable)
			if (!objTbl) return false;
			if (!objTbl.checkedRows) objTbl.checkedRows = '';
			// Leave the 'checkedRows' prop empty if no checkbox index was created
			var objRowIndex = this.parent.getObject( strHtmlTable + '_rowIndex' );			
			if (!objRowIndex) return false;
			var arrRowIndex = objRowIndex.innerHTML.split('|');
			var count = 0;
			// get all checkboxes one after the other and 
			// save their row id to the table prop 'checkedRows'
			for (var i=0; i<arrRowIndex.length; ++i) {
				chkBx = this.parent.getObject('chkbx_select_' + arrRowIndex[i]);
				if (chkBx.checked == true) {
					newArrCheckedRows[count] = arrRowIndex[i];
					++count;
				}
			}
			if (newArrCheckedRows.length > 0) {
				objTbl.checkedRows = newArrCheckedRows.join('|');
				this.htmlTable_rowIndex = objTbl.checkedRows;
			}
		},
		
		set_all_rows_checked : function (obj,strHtmlTable) {
			var newArrCheckedRows = [];
			var chkBx = '';
			// table not found ? - leave
			var objTbl = this.parent.getObject(strHtmlTable)
			if (!objTbl) return false;
			if (!objTbl.checkedRows) objTbl.checkedRows = '';
			// Leave the 'checkedRows' prop empty if no checkbox index was created
			var objRowIndex = this.parent.getObject( strHtmlTable + '_rowIndex' );
			if (!objRowIndex) return false;
			var arrRowIndex = objRowIndex.innerHTML.split('|');
			var count = 0;
			// get all checkboxes one after the other and 
			// save their row id to the table prop 'checkedRows'
			for (i=0; i<arrRowIndex.length; ++i) {
				chkBx = this.parent.getObject('chkbx_select_' + arrRowIndex[i]);
				if (obj.checked) {
					chkBx.checked = true;
					newArrCheckedRows[count] = arrRowIndex[i];
				} else {
					chkBx.checked = false;
				}
				++count;
			}
			if (newArrCheckedRows.length > 0) {
				objTbl.checkedRows = newArrCheckedRows.join('|');
				this.htmlTable_rowIndex = objTbl.checkedRows;
			}
				
		},
		
		tablerow_edit : function (obj, submitPath) {
			formId = obj.parentNode.id;
			form = obj.parentNode;
			var arrOption = formId.split('_');

			

			// Append the 'rcsId' hidden input control providing the 
			// activated rcs id to the server. 
			var ctlRcsId = document.createElement('input');
			ctlRcsId.type = 'hidden';
			ctlRcsId.value = arrOption[2];
			ctlRcsId.name ='rcsId';
			form.appendChild( ctlRcsId );
			
			var objTablename = this.parent.getObject('tablename');
			if (objTablename) {
				tablename = document.createElement( 'input' );
				tablename.style.display='none';
				tablename.name = 'tablename';
				tablename.value = objTablename.value;
				form.appendChild( tablename );
			} else {
				// this is for rescue only
				document.cookie = 'tablename=' + arrOption[3] + '_' + arrOption[4];
			}
			
			// append the token for submit security
			var sec_word = this.parent.getObject('sec_word');
			if (sec_word) {
				secword = document.createElement( 'input' );
				secword.style.display='none';
				secword.name = 'sec_word';
				secword.value = sec_word.content;
				form.appendChild( secword );
			}

			obj.parentNode.method = 'post';
			obj.parentNode.action = submitPath;
			obj.parentNode.submit();
		}
	}; // auto Table
}

// //////////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////////////

function clsMgenDrag(parent) {
	this.drag = '';
	this.parent = parent;
	this.posX = 0;
	this.posY = 0;
	this.startX = 0;
	this.startY = 0;
	this.mgen_event = null;
	this.object = '';

	/**
	 * Is associated to the mousedown eventhandler of the object to move, which
	 * is the given object.
	 * 
	 * The drag obj is implemented as a part of the dialog header bar.
	 * 
	 * After deactivating it is set to null.
	 * 
	 * @param string
	 */
	this.activate = function(obj) {
		var me = obj.drag;
		document.onmousemove = me.move;
		me.object = obj.parentNode;
		me.startX = me.posX - me.object.offsetLeft;
		me.startY = me.posY - me.object.offsetTop;
	};

	this.deactivate = function(mgen_event) {
		if (document.all) 
			var me = this;
		else
			var me = this.drag;
		me.object.style.left = (me.posX - me.startX) + "px";
		me.object.style.top = (me.posY - me.startY) + "px";
		me.object = null;
		document.onmousemove = '';
	};

	/**
	 * @param object
	 */
	this.move = function(mgen_event) {
		if (document.all) 
			var me = this;
		else
			var me = this.drag;
		me.mgen_event = mgen_event;
		me.posX = document.all ? window.event.clientX + document.documentElement.scrollLeft : mgen_event.pageX;
		me.posY = document.all ? window.event.clientY + document.documentElement.scrollTop : mgen_event.pageY;
		if (!me.object)
			return;
		if (me.object != null) {
			me.object.style.left = (me.posX - me.startX) + "px";
			me.object.style.top = (me.posY - me.startY) + "px";
		}
	};
}

// //////////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////////////

/* class dialog 15.4.2009 v1.0 copyright Bogdan Vojska */
/**
 * If the dialogs are not existing as a body element, they are 
 * constructed by the show method.
 * Each new activated dialog is assigned to the document body by the 
 * id given to the show method element.
 * The drag event object is implemented into the headbar.
 */
function clsMgenDialog (parent) {
	this.parent = parent;
	this.id = '';
	this.title = '';
	this.width = 0;
	this.height = 0;
	this.style = '';
	this.content = '';
	this.contentType = '';
	this.objDialogBox = '';
	this.objTitle = '';
	this.objHeadbar = '';
	this.objContentArea = '';
	this.objCloseButton = '';
	this.objFrame = '';

	this.onHide = '';
	this.onShow = '';
	
	this.lister = function (arg) {

		var list = '';
		var x;
		var elem = document.all[arg.id].drag;
//		alert(elem);
		for (x in elem) {
			list += 'Element: ' + x + '  ->->->  ' + elem[x] + '<br />';
		}
		document.open();
		document.write(list);
		document.close();
	};

	this.box = {
		parent : '',

		closeButton : function() {
			var newDiv = document.createElement('div');
			var newSpan = document.createElement('span');
			var newTextNode = document.createTextNode('X');
			newDiv.id = this.parent.id + '_closeButton';
			newDiv.style.position = 'absolute';
			newDiv.style.top = '4px';
			newDiv.style.left = Math.floor(this.parent.width - 20) + 'px';
			newDiv.style.height = '14px';
			newDiv.style.width = '14px';
			newDiv.style.borderTop = '1px solid #777';
			newDiv.style.borderRight = '1px solid #ccc';
			newDiv.style.borderBottom = '1px solid #eee';
			newDiv.style.borderLeft = '1px solid #777';
			newDiv.style.backgroundColor = '#aaa';
			newDiv.thisDialog = this.parent;
			newSpan.style.position = 'absolute';
			newSpan.style.width = '14px';
			newSpan.style.paddingTop = '0px';
			newSpan.style.fontSize = '13px';
			newSpan.style.lineHeight = '13px';
			newSpan.style.textAlign = 'center';
			newSpan.appendChild(newTextNode);
			newDiv.appendChild(newSpan);
			if (document.all) {
				newDiv.style.cursor = 'hand';
				newDiv.onclick = new Function("this.thisDialog.hide( )");
			} else {
				newDiv.style.cursor = 'pointer';
				newDiv.onclick = function() {
					this.thisDialog.hide();
				};
			}
			return newDiv;
		},

		title : function() {
			var newSpan = document.createElement('span');
			var newTextNode = document.createTextNode(this.parent.title);
			newSpan.id = this.parent.id + '_windowHead_title';
			newSpan.style.position = 'absolute';
			newSpan.style.width = Math.floor(this.parent.width - 25) + 'px';
			newSpan.style.marginLeft = '5px';
			newSpan.style.top = '5px';
			newSpan.appendChild(newTextNode);
			return newSpan;
		},

		headbar : function() {
			var newDiv = document.createElement('div');
			newDiv.id = this.parent.id + '_windowHead';
			newDiv.style.height = '20px';
			newDiv.style.width = (this.parent.width - 4) + 'px';
			newDiv.style.borderTop = '2px solid #eee';
			newDiv.style.borderRight = '2px solid #777';
			newDiv.style.borderBottom = '2px solid #777';
			newDiv.style.borderLeft = '2px solid #ccc';
			newDiv.style.backgroundColor = '#aaa';
			return newDiv;
		},
		
		addDragEventHandler : function ( objHeadbar ) {
			if (document.all) {
				objHeadbar.onmousemove = new Function("document.all[this.id].drag.move()");
				objHeadbar.onmouseup = new Function("document.all[this.id].drag.deactivate()");
				objHeadbar.onmousedown = new Function("document.all[this.id].drag.activate(this)");

//				objHeadbar.onmousedown = new Function("document.all[this.id].lister(this)");
//				objHeadbar.onmousedown = new Function("document.all[this.id].drag.activate(this)");

			} else {
				objHeadbar.onmousedown = function() {this.drag.activate(this);};

				objHeadbar.addEventListener("mousemove", objHeadbar.drag.move, false);
				objHeadbar.addEventListener("mouseup", objHeadbar.drag.deactivate, false);
			}
		},

		frame : function() {
			var frame = document.createElement('div');
			frame.id = this.parent.id;
			frame.style.position = 'fixed';
			frame.style.top = '100px';
			frame.style.left = '20px';
			frame.style.height = this.parent.height + 'px';
			frame.style.width = this.parent.width + 'px';
			frame.style.borderTop = '1px solid #fff';
			frame.style.borderBottom = '1px solid #555';
			frame.style.borderLeft = '1px solid #666';
			frame.style.borderRight = '1px solid #eee';
			frame.style.backgroundColor = '#ddd';
			frame.style.zIndex = 100;
			frame.style.display = 'none';
			return frame;
		},

		contentArea : function() {
			var contentArea = document.createElement('div');
			contentArea.id = this.parent.id + '_controlArea';
			contentArea.style.margin = '5px auto';
			contentArea.style.textAlign = 'left';
			contentArea.style.width = (this.parent.width - 10) + 'px';
			contentArea.style.height = (this.parent.height - 35) + 'px';
			contentArea.style.top = '35px';
			contentArea.innerHTML = this.parent.content || '';
			return contentArea;
		}
	};

	this.create = function(id, title) {
		document.body[id] = {};
		this.id = id;
		this.title = title;
		
		var headbar = this.box.headbar();
		headbar.appendChild(this.objTitle = this.box.title());
		headbar.appendChild(this.objCloseButton = this.box.closeButton());
		headbar.drag = new clsMgenDrag( this );
		headbar.lister = this.lister;
		this.box.addDragEventHandler( headbar );

		var dialogBox = this.objFrame = this.box.frame();
		dialogBox.appendChild(this.objHeadbar = headbar);
		dialogBox.appendChild(this.objContentArea = this.box.contentArea());

		document.body.appendChild(dialogBox);

		return dialogBox;
	};
	
	this.size = function(width, height) {
		this.objFrame.style.height = height + 'px';
		this.objFrame.style.width = width + 'px';
		this.objHeadbar.style.height = '20px';
		this.objHeadbar.style.width = (width - 4) + 'px';
		this.objContentArea.style.width = (width - 10) + 'px';
		this.objContentArea.style.height = (height - 35) + 'px';
		this.objCloseButton.style.left = (width - 24) + 'px';
	};

	this.show = function(id, title, width, height, content, onShow) {
		this.id = id || 'mgen_stdDialog';
		this.box.parent = this;
		this.width = width || 0;
		this.height = height || 0;
		
		if (!document.body[id])
			this.objDialogBox = this.create(id, title);
		this.size(width, height);
		this.objTitle.innerHTML = title || '';
		if (content) {
			this.content = content;
			this.objContentArea.innerHTML = content;
		}
		if (onShow)
			eval(onShow);
		this.objDialogBox.style.display = 'block';
	};
	
	this.hide = function () {
		this.objCloseButton.thisDialog.objDialogBox.style.display = "none";
		if (this.onHide)
			this.onHide();
	};

} 
// end clsMgenDialog

// //////////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////////////

function clsMgenAsyncTrans () {
	this.receivedData= '';
	this.task = 'doAsyncTrans';
	this.command = '';
	this.format = 'ajax';
	this.method = 'POST';
	this.async = true;
	this.url = '';
	this.taskOptions = '';
	this.connection = '';
	this.isIeReq = function() {
		var ie = (window.ActiveXObject) ? true : false;
		return ie
	};

	this.onLoaded = '';

	this.getConnection = function() {
		var connection = '';
		if (this.isIeReq()) {
			try {
				connection = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				connection = new ActiveXObject("Microsoft.XMLHTTP");
			}
			;
		} else {
			connection = new XMLHttpRequest();
		}
		if (connection.overrideMimeType)
			connection.overrideMimeType('text/xml');
		return connection;
	};

	/*
	 * Requesting data with changing the cursor icon
	 */
	this.requestData = function(url, func, command, wait) {
		var arrCom = '';
		if (wait)
			document.body.style.cursor = "wait";
		if ((arrCom = command.split(".")).length > 1) {
			this.task = arrCom[0];
			command = arrCom[1];

		}
		this.command = command;
		this.taskOptions = Site.taskOptions.get();
		this.url = url;
		this.onLoaded = func;
		this.send();
	};

	this.receive = function(httpRequest, strFunction) {
		if (httpRequest.readyState == 4) {
			if (httpRequest.status == 200) {
				if (strFunction && strFunction !== undefined)
					this.onLoaded = eval(strFunction + '(httpRequest.responseText);'); // process
			}
		}
		return null;
	};

	this.send = function() {
		var strSend = '';
		var httpRequest = this.getConnection();
		// append the token for submit security
		var sec_word = Site.DOM.getObject('sec_word');
		if (!httpRequest) {
			alert('asyncTrans: No XMLHTTP connection.');
			return false;
		}
		if (!this.command || !this.task)
			return false;
		strSend += 'task=' + this.task + '.' + this.command;
		strSend += this.format ? '&' + 'format=' + this.format : '';
		strSend += this.taskOptions ? '&' + 'taskOptions=' + this.taskOptions : '';
		strSend += sec_word ? '&' + 'sec_word=' + sec_word.content : '';
		httpRequest.open(this.method, this.url, this.async);
		httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8');
		httpRequest.onreadystatechange = function() {
			AsyncTrans.receive(httpRequest, AsyncTrans.onLoaded);
		};
		httpRequest.send(strSend);
		Site.taskOptions.clear();
		return true;
	};
} // end obj mgen_objAsyncTrans

Site = new clsMgenSite( 'Site');
AutoTable = new clsMgenAutoTable();
Site.dialog = new clsMgenDialog( Site );
AsyncTrans = new clsMgenAsyncTrans();
//window.onload = function (){Site.dialog.create('mgen_stdDialog', 310, 200, '','');}
