/**
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('_');
		obj.style.zIndex = 100;
	}
	if (imgSize == 'max') {
		arrObj[arrObj.length - 1] = 'min.' + fleSuff;
		obj.src = arrObj.join('_');
		obj.style.zIndex = 0;
	}
}

function clsMgenSite(name) {
	this.name = name;
	this.table = {
		activeTableRowId : null,
		objActiveTableRow : null,
		objTableRow : null,
		objRcsEditButton : null
	};
	
	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] + '.' + 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 );
		},
		
		load_editMenuItem_controls : function (response) {
			document.body.style.cursor = "auto";
			if (!response) return '';
			var targetObj = Site.DOM.getObject( 'controlframe' );
			if (!targetObj) return '';
			targetObj.innerHTML = 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 (objTarget == null) break;
				objTarget.value = obj.value;
				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', 'contentManager.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', 'contentManager.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 'login_submit' :
				Site.submit(formId, 'logging.login_submit', './index.php' );
				break;
			case 'member_ureg_submit' :
				Site.submit(formId, '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, 'userManager.editUser_update', './index.php' );
				break;
			case 'editUser_delete_open' :
				Site.taskOptions.set('formId', formId);
				AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.stdDialog', 'userManager.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, 'userManager.editUser_delete', './index.php' );
				break;
			case 'editUser_authenticate_open' :
				AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.createUserAuth', 'userManager.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, 'autoTableQueryManager.editQueryDef_insert', './index.php' );
				break;
			case 'editQueryDef_update' :
				Site.submit(formId, 'autoTableQueryManager.editQueryDef_update', './index.php' );
				break;
			case 'editQueryDef_delete_open' :
				Site.taskOptions.set('formId', formId);
				AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.stdDialog', 'autoTableQueryManager.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, 'autoTableQueryManager.editQueryDef_delete', './index.php' );
				break;
			case 'editQueryDef_back' :
				Site.submit(formId, 'history_back', './index.php' );
				break;
			case 'editMainmenu_back' :
				Site.submit(formId, 'menuManager.open_table_mainmenus', './index.php' );
				break;
			case 'editMainmenu_insert' :
				Site.submit(formId, 'menuManager.editMainmenu_insert', './index.php' );
				break;
			case 'editMainmenu_update' :
				Site.submit(formId, 'menuManager.editMainmenu_update', './index.php' );
				break;
			case 'editMenuItem_back' :
				Site.submit(formId, 'menuManager.open_table_menus', './index.php' );
				break;
			case 'createMenuItem_back' :
				Site.submit(formId, 'menuManager.open_table_menus', './index.php' );
				break;
			case 'createMenuItem_insert' :
				Site.submit(formId, 'menuManager.editMenuItem_insert', './index.php' );
				break;
			case 'editMenuItem_update' :
				Site.submit(formId, 'menuManager.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, 'menuManager.editMenuItem_delete', './index.php' );
				break;
			case 'editMenuItem_freeContent_open' :
				if (document.forms[formId] == null) break;
				var ctlRcsId = document.forms[formId].elements.rcsId;
				if (ctlRcsId == null) break;
				Site.taskOptions.set('formId', formId);
				Site.taskOptions.set('rcsId', ctlRcsId.value);
				AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.stdDialog', 'menuManager.dialog_editMenuItem_freeContent', true );
				break;
			case 'editMenuItem_freeContent' :
				Site.dialog.hide( );
				var formId = Site.taskOptions.getOption('formId');
				if (document.forms[formId] == null) break;
				var ctlRcsId = document.forms[formId].elements.rcsId;
				if (ctlRcsId == null) break;
				Site.taskOptions.set('formId', formId);
				Site.taskOptions.set('rcsId', ctlRcsId.value);
				Site.submit(formId, 'menuManager.editMenuItem_freeContent', './index.php' );
				break;
			case 'editMenuItem_freeContent_cancel' :
				Site.dialog.hide( );
				break;
			case 'menuLanguage_insert' :
				Site.submit(formId, 'menuManager.menuLanguage_insert', './index.php' );
				break;
			case 'menuLanguage_update' :
				Site.submit(formId, 'menuManager.menuLanguage_update', './index.php' );
				break;
				
			case 'contentTheme_insert' :
				Site.submit(formId, 'contentManager.contentTheme_insert', './index.php' );
				break;
			case 'contentTheme_update' :
				Site.submit(formId, 'contentManager.contentTheme_update', './index.php' );
				break;
			case 'contentTheme_move' :
				Site.submit(formId, 'contentManager.contentTheme_move', './index.php' );
				break;
			case 'contentTheme_remove' :
				// get the cached formId
				var formId = Site.taskOptions.getOption('formId');
				Site.dialog.hide( );
				Site.submit(formId, 'contentManager.contentTheme_remove', './index.php' );
				break;
			case 'contentTheme_removeDialog_open' :
				Site.taskOptions.set('formId', formId);
				AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.stdDialog', 'contentManager.dialog_contentTheme_remove', true );
				break;
				
			case 'contentFiles_append' :
				Site.submit(formId, 'contentManager.contentFiles_append', './index.php' );
				break;
			case 'contentFiles_remove' :
				// get the cached formId
				var formId = Site.taskOptions.getOption('formId');
				Site.submit(formId, 'contentManager.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', 'contentManager.dialog_contentFiles_remove', true );
				break;
			case 'contentFiles_recycle' :
				Site.submit(formId, 'contentManager.contentFiles_recycle', './index.php' );
				break;
			case 'contentChapter_remove' :
				Site.submit(formId, 'contentManager.contentChapter_remove', './index.php' );
				break;
				
			case 'download_insert' :
				Site.submit(formId, 'downloadManager.download_insert', './index.php' );
				break;
			case 'download_update' :
				Site.submit(formId, 'downloadManager.download_update', './index.php' );
				break;
				
			case 'contentTrash_delete' :
				Site.submit(formId, 'contentManager.contentTrash_delete', './index.php' );
				break;
				
			case 'stdDialog_close' :
				Site.dialog.hide( );
				break;
				
			case 'history_back' :
				Site.submit(formId, 'history_back', './index.php' );
				break;
				
			case 'download_send' :
				Site.submit(formId, 'download.download_send', './index.php' );
				break;
				
			case 'noSubmit' :
				break;
				
			default :
			}
		},

		row_switch_highlight : function (obj) {
			
			var ctlActiveRow = Site.DOM.getObject( 'activeTableRow' );
			
			if (ctlActiveRow.value) this.parent.activeRow = document.getElementById(ctlActiveRow.value);
						
			if (this.parent.activeRow) {
				this.parent.activeRow.className = 'tblRow';
				obj.className = 'tblRow';
			}
			
			// this.parent is the tableFrame
			this.parent.activeRow = obj;
			obj.className = 'tblRowHigh';
			
			// Set the table activeTableRow field for the active row
			var ctlActiveRow = Site.DOM.getObject( 'activeTableRow' );
			if (ctlActiveRow != null) {
				ctlActiveRow.value = obj.id;
			}
		},
		
		tablerow_edit : function (obj, submitPath) {
			formId = obj.id;
			form = obj;
			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 );

			form.method = 'post';
			form.action = submitPath;
		},
		
		tablerow_delete : function (obj, submitPath) {
			formId = obj.id;
			form = obj;
			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', 'menuManager.dialog_editMenuItem_delete', true );
		},

		/**
		 * This changes the set of controls to the item type according 
		 * needs.
		 */
		load_editMenuItem_controls : function (obj) {
			var form = obj.form;
			var formId = form.id;
//			Site.DOM.appendSecWord( form );
			
			Site.taskOptions.set( 'rcsId', form.rcsId.value );
			Site.taskOptions.set( 'tablename', form.tablename.value );
			Site.taskOptions.set( 'itemtype', form.ctl_editMenuItem_itemtype.value );

			AsyncTrans.requestData( './index.php', 'Site.parseAsyncResponse.load_editMenuItem_controls', 'menuManager.load_editMenuItem_controls', true );
		},
		
		open_form_editMenuItem_update : function () {
			var ctl = '';
			var activeRow = Site.DOM.getObject( 'activeTableRow');
			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, 'menuManager.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', 'menuManager.getTargetMenuSize', true );
		}
	};

}

function clsMgenAutoTable() {
	
	this.utils = {
		serialize_multiSelect : function (objControl) {
			var strReturn = '';
			var arrOptions = [];
			var options = objControl.options;
			var count = 0;
			for (var x=0; x<options.length; ++x) {
				if (options[x].selected === true) {
					arrOptions[count] = options[x].value;
					++count;
				}
			}
			return arrOptions.join(',');
		},

		frm_appendInputControl : function (objForm, name, value) {
			var newElem = document.createElement( 'input' );
			newElem.style.display = 'none';
			newElem.name = name;
			newElem.value = value;
			objForm.appendChild( newElem );
		}
	};
	
	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 = {
		parent : this,
	
		/**
		 * Write the form specific local 'mgenat' field values 
		 * to the according controls of the table submit form.
		 * 
		 * If no according control exists there, append the 
		 * source control to the table submit form.
		 */
		update_submitForm : function (form) {
			var sourceForm = form;
			var targetForm = document.forms["frm_autoTableSubmit"];
			var elems = sourceForm.elements;
			var strSelectFields = '';
			for (var x=0; x < elems.length; ++x) {
				if (elems[x].name.split('_')[0] == 'mgenat') {
					if (elems[x].type == 'select-multiple') {
						strSelectFields = this.parent.utils.serialize_multiSelect(elems[x]);
					}
					// control exists in target form?
					if (!targetForm.elements[elems[x].name]) {
						if (elems[x].type == 'select-multiple') {
							// write as input type
							this.parent.utils.frm_appendInputControl( targetForm, elems[x].name.replace("[]",''), strSelectFields );
						} else {
							elems[x].style.display = 'none';
							targetForm.appendChild(elems[x]);
						}
					} else {
						targetForm.elements[elems[x].name].value = elems[x].value;
					}
				}	
			}
			return true;
		},
		
		// Appends the security word provided by the server
		append_secWord : function (form) {
			if (!form) return false;
			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 );
			}
		},
		
		send : function (formId, action) {
			var form = document.forms["frm_autoTableSubmit"];
			this.append_secWord( document.forms["frm_autoTableSubmit"] );
			this.update_submitForm( document.forms[formId] );
			form.method = 'post';
			form.action = action;
			form.submit( );
		}
	};
	
	this.sql = {
		parent : this,
		
		get_selected_searchFields : function (obj_mgenat_field) {
			var options = obj_mgenat_field.options;
			var count = 0;
			obj_mgenat_field.index_selected_searchFields = [];
			for (x in options) {
				if (x == 'length' || x == 'indexSelected') continue;
				if (options[x].selected) {
					obj_mgenat_field.index_selected_searchFields[count] = options[x].value;
					++count;
				}
			}
		},

		page : function () {
			strReturn += this.sqlSelect;
		}
	};
	
	this.event = {
		parent : this,
		checkedRows : '',
		cmd : function (task, formId, option) {
			var action = '';
			switch (task) {
			case 'page_autoTable' :
				var form = document.forms[formId];
				// get the submit task from the hidden control
				var action = form.elements['mgenat_submitPath'].value;
				if (form) {
					form.elements['mgenat_limitoffset'].value  = option.split(',')[0];
					form.elements['mgenat_limitrows'].value  = option.split(',')[1];
					this.parent.submit.send( formId, action + '&sqlLimit=' + option );
				}
				break;
			case 'search_autoTable' :
				var form = document.forms[formId];
				form.elements['mgenat_limitoffset'].value = '0';
				// get the submit task from the hidden control
				var action = option;
				if (action) {
					this.parent.submit.send(formId, action);
				}
				break;
			case 'submit_checked' :
				// formId is the target form to copy seleted rows to. - frm_autoTableSubmit
				var form = document.forms[formId];
				var action = option;

				this.get_checked();
					
				// If no rows selected or no primary key exists the server will manage this.
				this.parent.utils.frm_appendInputControl( form, 'mgenat_checkedRows', this.checkedRows );
	
				if (action) {
					// append the html table checked row index
					form.method = 'post';
					form.action = action;
					form.submit();
				}
				break;
			case 'all_autoTable' :
				var form = this.parent.getObject( formId );
				form.elements['mgenat_field'].value = '';
				var action = form.elements['mgenat_submitPath'].value = option;
				if (action) {
					form.method = 'post';
					form.action = action;
					form.submit();
				}
				break;
			case 'trash_checked' :
				var form = this.parent.getObject( formId );
				var action = option;
				if (action) {
					form.method = 'post';
					form.action = action;
					form.submit();
				}
				break;
			case 'cancel_trash' :
				var action = option;
				if (action) {
					form.method = 'post';
					form.action = action;
					form.submit();
				}
				break;
			default :
			}
		},
		
		row_switch_highlight : function (obj) {
			
			var ctlActiveRow = Site.DOM.getObject( 'activeTableRow' );
			
			if (ctlActiveRow.value) this.parent.activeRow = document.getElementById(ctlActiveRow.value);
						
			if (this.parent.activeRow!= null && this.parent.activeRow) {
				this.parent.activeRow.className = 'tblRow';
				obj.className = 'tblRow';
			}
			
			// this.parent is the tableFrame
			this.parent.activeRow = obj;
			obj.className = 'tblRowHigh';
			
			// Set the table activeTableRow field for the active row
			if (ctlActiveRow != null) {
				ctlActiveRow.value = obj.id;
			}
			
		},
		
		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';
			}
		},

		/**
		 * 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 as char divided list into a table prop .
		 * 
		 * @param strHtmlTable - name of the html table container (not db table)
		 */

		get_checked : function () {
				// Get the table object row index list
				var arrRowIndex = document.getElementsByName('chkbxRow');
				
				// If no primary index is defined for the table,  no 
				// row checkboxes are created because no individual 
				// addressing is available in such case.
				if (arrRowIndex.length == 0) {
					this.checkedRows = '';
					return false;
				}
	
				// Get all checkboxes one after the other and 
				// collect their row ids to the checkedRows if 
				// they are checked.
				var count = 0;
				var newArrCheckedRows = [];
				var arrRowId = null;
				for (var i=0; i<arrRowIndex.length; i++) {
					chkBx = arrRowIndex[i];
					if (chkBx.checked === true) {
						arrRowId = chkBx.id.split('_');
						newArrCheckedRows[count] = arrRowId[arrRowId.length-1];
						++count;
					}
				}
				
				if (newArrCheckedRows.length > 0) {
					this.checkedRows = newArrCheckedRows.join('|');
				} else {
					this.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;
			// Get the table object row index list
			var arrRowIndex = document.getElementsByName('chkbxRow');
			// If no primary index is defined for the table,  no 
			// row checkboxes are created because no individual 
			// addressing is available in such case.
			if (arrRowIndex.length == 0) {
				this.checkedRows = '';
				return false;
			}
			// Get all checkboxes one after the other and 
			// save their row id to the table checkedRows collection
			for (i=0; i<arrRowIndex.length; ++i) {
				chkBx = arrRowIndex[i];
				// If the table delete checkbox is on select all else deselect all.
				if (obj.checked == true) {
					chkBx.checked = true;
					newArrCheckedRows[i] = chkBx.id;
				} else {
					chkBx.checked = false;
				}
			}
			if (newArrCheckedRows.length > 0) {
				this.checkedRows = newArrCheckedRows.join('|');
			}
				
		},
		
		tablerow_edit : function (obj, action) {
			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];
			}
/*
			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 = action;
			obj.parentNode.submit();
		}
	}; // event
} // clsAutoTable

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

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();

