		var refreshArea = null, _lpURL = "", _lpIdClient = "", __idUser = null, __idProject = null;
		var _showedTableProject = null, _showedTreeImage = null, _lastIDClient = 0, __rootPath = '';
		var _prjCustomCode = '', _prjName = '', _prjEditedBy = '', _prjActions = '', _prjDblClick = null;
		
		function goHome()
		{
			location.href = __rootPath + 'cwc/default.asp';
		}
		
		function delUser(idUser, userName, msg1, path)
		{
			if (confirm(msg1 + ' ' + userName + " ?"))
			{
				AjaxRequest.get(
					{
					'url': path + 'cwc/blogic/Users/del.asp',
					'idUser': idUser,
					'timeout': 10000,
					'onSuccess': listUsers,
					'onError': function ()
						{
							alert(msgError);
						}
					}
				);
			}
			void(0);
		}
		
		function openInsertUserWindow(path)
		{
			refreshArea = listUsers;
			__rootPath  = path;
			openInnerWindow(path + 'cwc/blogic/Users/usersDataForm.asp?action=addNew', 380, 470, true);
		}
		
		function openEditUserWindow(idUser, path)
		{
			refreshArea = listUsers;
			__rootPath  = path;
			openInnerWindow(path + 'cwc/blogic/Users/usersDataForm.asp?action=edit&idUser=' + escape(idUser), 380, 470, true);
		}
		
		function listUsers()
		{
			AjaxRequest.get(
				{
				'url': __rootPath + 'cwc/blogic/Users/list.asp',
				'timeout': 10000,
				'onSuccess': function(req)
					{
						getById('innerContent').innerHTML = req.responseText;
					},
				'onComplete': hideWaiting,
				'onLoading': showWaiting,
				'onError': function ()
					{
						alert(msgError);
					}
				}
			);
		}
		
		function listClients()
		{
			AjaxRequest.get(
				{
				'url': __rootPath + 'cwc/blogic/Clients/list.asp',
				'timeout': 10000,
				'onSuccess': function(req)
					{
						var innerContent = getById('innerContent');
						if (innerContent)
						{
							innerContent.innerHTML = req.responseText;
						}
					},
				'onComplete': hideWaiting,
				'onLoading': showWaiting,
				'onError': function ()
					{
						alert(msgError);
					}
				}
			);
		}

		function listContracts()
		{
			AjaxRequest.get(
				{
				'url': __rootPath + 'cwc/blogic/Contracts/list.asp',
				'timeout': 10000,
				'onSuccess': function(req)
					{
						getById('innerContent').innerHTML = req.responseText;
					},
				'onComplete': hideWaiting,
				'onLoading': showWaiting,
				'onError': function ()
					{
						alert(msgError);
					}
				}
			);
		}
		
		function listCommercialProjects()
		{
			openInnerWindow(__rootPath + 'cwc/blogic/CommercialProjects/fullListProjects.asp', 780, 510, true, null, null, false);
		}

		function delClient(idClient, clientName, msg1, msg2, path)
		{
			
			if (confirm(msg1 + " " + clientName + "'?\r\n" + msg2))
			{
				AjaxRequest.get(
					{
					'url': path + 'cwc/blogic/Clients/del.asp',
					'idClient':  idClient,
					'timeout': 10000,
					'onSuccess': listClients,
					'onComplete': hideWaiting,
					'onLoading': showWaiting,
					'onError': function ()
						{
							alert(msgError);
						}
					}
				);
			}
		}

		function openInsertClientWindow(path)
		{
			refreshArea = listClients;
			__rootPath  = path;
			openInnerWindow(path + 'cwc/blogic/Clients/clientsDataForm.asp?action=addNew', 380, 250, true);
		}

		function openEditClientWindow(idClient, p)
		{
			refreshArea = listClients;
			__rootPath  = p;
			openInnerWindow(p + 'cwc/blogic/Clients/clientsDataForm.asp?action=edit&idClient=' + escape(idClient), 380, 250, true);
		}
		
		function delContract(idContract, contractName, msg1, p)
		{
			if (confirm(msg1 + ' ' + contractName + "'?"))
			{
				__rootPath  = p;
				AjaxRequest.get(
					{
					'url': p + 'cwc/blogic/Contracts/del.asp',
					'idContract': idContract,
					'timeout': 10000,
					'onSuccess': listContracts,
					'onComplete': hideWaiting,
					'onLoading': showWaiting,
					'onError': function ()
						{
							alert(msgError);
						}
					}
				);
			}
		}

		function openInsertContractWindow(path)
		{
			__rootPath = path;
			openInnerWindow('cwc/blogic/Contracts/contractsDataForm.asp?action=addNew', 380, 350, true);
		}

		function openEditContractWindow(idContract, p)
		{
			refreshArea = listContracts;
			__rootPath  = p;
			openInnerWindow(p + 'cwc/blogic/Contracts/contractsDataForm.asp?action=edit&idContract=' + escape(idContract), 380, 350, true);
		}
		
		function listContractsByClient(url, idClient)
		{
			var o = getById('contractsArea');
			if ((idClient == null) || (idClient == "") || (isNaN(idClient)))
			{
				o.innerHTML = '';
			}
			else
			{
				_lpURL      = url;
				_lpIdClient = idClient;
	
				AjaxRequest.get(
					{
					'url': url + idClient,
					'timeout': 10000,
					'onSuccess': function(req)
						{
							getById('contractsArea').innerHTML = req.responseText;
							listContractsByClient(_lpURL, _lpIdClient);
						},
					'onComplete': hideWaiting,
					'onLoading': showWaiting,
					'onError': function ()
						{
							alert(msgError);
						}
					}
				);
			}
			return true;
		}
		
		function selectRow(row, tableName)
		{
			if (row == null)
			{
				return false;
			}
			
			var _table = getById(tableName);
			if (_table == null)
			{
				return false;
			}
			
			for (var i = 0; i < _table.rows.length; i++)
			{
				if (_table.rows[i].rowIndex != row.rowIndex)
				{
					_table.rows[i].bgColor = '';
				}
			}

			row.bgColor = '#E2ECF5';
			return true;
		}

		function selectUserRow(row, p)
		{
			if (!selectRow(row, 'usersTable'))
			{
				return;
			}

			__idUser   = row.id.split('id_')[1];
			__rootPath = p;
			
			listUserAccessibleClients();
		}
		
		function listUserAccessibleClients()
		{
			var _div = getById('clientList');
			if (_div == null)
			{
				return;
			}
			
			_div.style.display = '';
			
			AjaxRequest.get(
				{
				'url': __rootPath + 'cwc/blogic/Clients/listClientsByUser.asp',
				'parameters':
					{
						'idUser': __idUser
					},
				'timeout': 10000,
				'onSuccess': function(req)
					{
						getById('clientList').innerHTML = req.responseText;
					},
				'onComplete': hideWaiting,
				'onLoading': showWaiting,
				'onError': function ()
					{
						alert(msgError);
					}
				}
			);
		}
		
		function delUserClientPermission(idUser, idClient, nameClient, msg1, p)
		{
			if (confirm(msg1 + ' ' + nameClient + "'?"))
			{
				__idUser   = idUser;
				__rootPath = p;
				
				AjaxRequest.get(
					{
					'url': p + 'cwc/blogic/Clients/delUserClientPermission.asp?idClient=' + idClient + '&idUser=' + idUser,
					'timeout': 10000,
					'onSuccess': listUserAccessibleClients,
					'onError': function ()
						{
							alert(msgError);
						}
					}
				);
			}
			void(0);
		}
		
		function openAddUserClientPermissionWindow(idUser, t1, t2, t3, p)
		{
			var _table = getById('permissionUserClientTable');
			if (_table == null)
			{
				return;
			}
			
			__rootPath = p;
			
			var _btn = getById('insertUserClientPermission');
			_btn.disabled = true;
			
			var _tr = _table.insertRow(_table.rows.length);
			_tr.id = 'insertingNewUserClientPermission';

			var _tdName = _tr.insertCell(0);
			_tdName.innerHTML = '<select id="selectNewPermissionClient" onchange="javascript:processNewPermissionChange(this, ' + idUser + ', \'' + p + '\');" style="width: 100%"><option class="selectItem" value="" selected="selected">-- ' + t1 + ' --</option><option value=""></option><option class="cancelItem" value="cancel">' + t2 + '</option><option value="">&nbsp;</option></select>'
			
			var _tdManager = _tr.insertCell(1);
			_tdManager.innerHTML = '<table border="0" cellspacing="0" cellpadding="0" style="width: 100%"><tr><td style="width: 1px; border: none; padding: 0px"><input type="checkbox" id="newPermissionaManager" /></td><td style="border: none; padding: 0px"><label for="newPermissionaManager">' + t3 + '</label></td></tr></table>';
			
			var _tdAction = _tr.insertCell(2);
			_tdAction.innerHTML = '&nbsp;';
			
			fillSelectNewClient(idUser);
		}
		
		function fillSelectNewClient(idUser)
		{
			if (idUser == null)
			{
				return;
			}
			
			var _select = getById('selectNewPermissionClient');
			if (_select == null)
			{
				return;
			}
			
			AjaxRequest.get(
				{
				'url': __rootPath + 'cwc/blogic/Clients/listAvailableNewClientsByUser.asp?idUser=' + idUser,
				'timeout': 10000,
				'onComplete': hideWaiting,
				'onLoading': showWaiting,
				'onError': function ()
					{
						alert(msgError);
					},
				'onSuccess': function(req)
					{
						var ie = isIE();
						var _target = getById('selectNewPermissionClient');
						if (!_target)
						{
							return;
						}
			
						var responseText = req.responseText;
						
						if (responseText != 'accessDenied' && responseText != 'accessFailed' && responseText != 'emptyArea')
						{
							var _arr = responseText.split('|#|');
							_arr.pop();
				
							if (_arr.length % 2 != 0)
							{
								return;
							}
				
							for (var i = 0; i < _arr.length; i += 2)
							{
								var op = document.createElement('option');
								op.value = _arr[i + 1];
								op.text = _arr[i];
					
								if (ie)
								{
									_target.add(op, _target.options.length);
								}
								else
								{
									_target.add(op, _target.options[_target.options.length]);
								}
							}
							
							delete arr;
						}
						else 
						{
							if (responseText == 'emptyArea')
							{
								while (_target.options.length > 0)
								{
									_target.remove(0);
								}
							}
							else
							{
								alert('Acesso negado.');
								var h = goHome;
								if (h != null)
								{
									h();
								}
							}
						}
						
						delete _target;
					}
				}
			);
		}
		
		function processNewPermissionChange(_select, idUser, p)
		{
			if (_select == null) return;
			
			if (_select.options[_select.selectedIndex].value == "")
			{
				_select.selectedIndex = 0;
				return;
			}

			var _checkBox = getById('newPermissionaManager');
			if (_checkBox == null)
			{
				_select.selectedIndex = 0;
				return;
			}
						
			var _btn = getById('insertUserClientPermission');
			_btn.disabled = false;
			
			if (_select.options[_select.selectedIndex].value == "cancel")
			{
				var _table = getById('permissionUserClientTable');
				var _tr = getById('insertingNewUserClientPermission');
				if (_table == null || _tr == null) return;
				_table.deleteRow(_tr.rowIndex);
			}
			else			
			{
				var _tr = getById('insertingNewUserClientPermission');
				var idClient = _select.options[_select.selectedIndex].value;
				var textClient = _select.options[_select.selectedIndex].text;
				_tr.id = 'tr_client_' + idClient;

				__idUser   = idUser;
				__rootPath = p;

				AjaxRequest.get(
					{
					'url': p + 'cwc/blogic/Clients/addUserClientPermissions.asp?idUser=' + idUser + '&idClient=' + idClient + '&managePermission=' + _checkBox.checked,
					'timeout': 10000,
					'onSuccess': listUserAccessibleClients,
					'onError': function ()
						{
							alert(msgError);
						}
					}
				);
			}
		}
		
		function insertNewProject(idClient, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, uName, p)
		{
			var _table = getById('tableProject' + idClient);
			if (_table == null)
			{
				return;
			}
			
			__rootPath = p;
			
			var _trTitle = _table.insertRow(_table.rows.length);
			_trTitle.id = 'newTitleRow' + idClient;
			
			var _tdTitle1 = _trTitle.insertCell(0);
			_tdTitle1.style.fontWeight = 'bold';
			_tdTitle1.style.height = '20px';
			_tdTitle1.innerHTML	 = t9;

			var _tdTitle2 = _trTitle.insertCell(1);
			_tdTitle2.style.fontWeight = 'bold';
			_tdTitle2.style.height = '20px';
			_tdTitle2.innerHTML	 = t1;
			
			var _tdTitle3 = _trTitle.insertCell(2);
			_tdTitle3.style.fontWeight = 'bold';
			_tdTitle3.innerHTML = t2;
			
			var _tdTitle4 = _trTitle.insertCell(3);
			_tdTitle4.style.fontWeight = 'bold';
			_tdTitle4.innerHTML = t3;

			var _tr = _table.insertRow(_table.rows.length);
			_tr.id = 'newProjectRow' + idClient;
			
			var _tdProjectCustomCode = _tr.insertCell(0);
			_tdProjectCustomCode.innerHTML = '<input id="newProjectCustomCode' + idClient + '" class="textBox" type="text" style="width: 50px;" value="" />';

			var _tdProjectName = _tr.insertCell(1);
			_tdProjectName.innerHTML = '<input id="newProjectName' + idClient + '" class="textBox" type="text" style="width: 306px; " value="" />';
			
			var _trLastChange = _tr.insertCell(2);
			_trLastChange.innerHTML = '<div style="border: 1px solid #999999; padding: 2px; background-color: #FAFAFA; margin-right: 2px;">' + uName + '</div>';

			var _trAction = _tr.insertCell(3);
			_trAction.innerHTML = '<select onchange="javascript:projectActionChanged(this, ' + idClient + ', \'' + t10 + '\', \'' + t8 + '\');" style="width: 110px;" id="newProjectAction' + idClient + '"><option value="" style="text-align: center; width: 110px">- ' + t5 + ' -</option><option style="width: 110px" value=""></option><option style="width: 110px" value="finish" class="statusBom">' + t6 + '</option><option style="width: 110px" value="cancel" class="cancelItem">' + t7 + '</option></select>';
			_trAction.width = 110;
			
			var _btn = getById('insertProject' + idClient);
			_btn.focus();

			var _tf = getById('newProjectCustomCode' + idClient);
			if (_tf)
			{
				_tf.focus();
			}
			
			_btn.disabled = true;
		}
		
		
		function editProject(rowProject, idClient, idProject, p, t1, t2, t3, uName)
		{
			if (rowProject == null)
			{
				return;
			}
			
			__rootPath  = p;
			
			_prjDblClick          = rowProject.ondblclick;
			rowProject.ondblclick = null;
			
			var _btn = getById('insertProject' + idClient);
			_btn.disabled = true;
			
			_prjCustomCode = rowProject.cells[0].innerHTML;
			_prjName       = rowProject.cells[1].innerHTML;
			_prjEditedBy   = rowProject.cells[2].innerHTML;
			_prjActions    = rowProject.cells[3].innerHTML; 

			rowProject.cells[0].innerHTML = '<input id="editCustomCode' + idProject + '" class="textBox" type="text" style="width: 48px; " value="' + _prjCustomCode + '" />';
			rowProject.cells[1].innerHTML = '<input id="editProjectName' + idProject + '" class="textBox" type="text" style="width: 304px; " value="' + _prjName + '" />';
			rowProject.cells[2].innerHTML = '<div style="border: 1px solid #999999; padding: 2px; background-color: #FAFAFA; margin-right: 2px">' + uName + '</div>';
			rowProject.cells[3].innerHTML = '<select onchange="javascript:projectEditActionChanged(this, ' + idClient + ', ' + idProject + ');" style="width: 110px;" id="newProjectAction' + idClient + '"><option value="" style="text-align: center; width: 110px">- ' + t1 + ' -</option><option style="width: 110px" value=""></option><option style="width: 110px" value="finish" class="statusBom">' + t2 + '</option><option style="width: 110px" value="cancel" class="cancelItem">' + t3 + '</option></select>';
			
			var _tf = getById('editCustomCode' + idProject);
			if (_tf)
			{
				_tf.focus();
			}
		}
		
		function projectEditActionChanged(actionSelect, idClient, idProject)
		{
			if (actionSelect == null || idClient == null || idProject == null)
			{
				return;
			}
			
			var _tr = getById('rowProject' + idProject);
			if (_tr == null)
			{
				return;
			}
			
			switch(actionSelect.options[actionSelect.selectedIndex].value)
			{
				case '':
				{
					actionSelect.selectedIndex = 0;
					break;
				}

				case 'cancel':
				{
					_tr.cells[0].innerHTML = _prjCustomCode;
					_tr.cells[1].innerHTML = _prjName;
					_tr.cells[2].innerHTML = _prjEditedBy;
					_tr.cells[3].innerHTML = _prjActions;
					
					_prjCustomCode = '';
					_prjName       = '';
					_prjEditedBy   = '';
					_prjActions    = '';
					
					var rowProject = getById('rowProject' + idProject);
					if (rowProject)
					{
						rowProject.ondblclick = _prjDblClick;
					}
					_prjDblClick = null;

					var _btn = getById('insertProject' + idClient);
					if (_btn)
					{
						_btn.disabled = false;
					}

					break;
				}

				default:
				{
					var newCode = getById('editCustomCode' + idProject);
					var newName = getById('editProjectName' + idProject);

					if (newName == null || newCode == null)
					{
						actionSelect.selectedIndex = 0;
						break;
					}
					
					__idProject = idProject;
					
					AjaxRequest.get(
						{
						'url': __rootPath + 'cwc/blogic/CommercialProjects/updateCommercialProject.asp',
						'parameters':
							{
								'idProject': idProject,
								'nameProject': newName.value,
								'customCode': newCode.value
							},
						'timeout': 10000,
						'onSuccess': function()
							{
								if (__idProject)
								{					
									location.href = __rootPath + 'cwc/blogic/CommercialProjects/fullListProjects.asp?idProject=' + __idProject;
								}
								else
								{
									location.reload();
								}
								
								__idProject = null;
							},
						'onComplete': hideWaiting,
						'onLoading': showWaiting,
						'onError': function ()
							{
								alert(msgError);
							}
						}
					);
				}
			}
		}

		
		function projectActionChanged(selectAction, idClient, msg1, msg2)
		{
			if (selectAction == null)
			{
				return;
			}
			
			if (selectAction.options[selectAction.selectedIndex].value == '')
			{
				selectAction.selectedIndex = 0;
				return;
			}
			
			var _btn = getById('insertProject' + idClient);
			_btn.disabled = false;
			
			if (selectAction.options[selectAction.selectedIndex].value == 'finish')
			{
				var _customCode  = getById('newProjectCustomCode' + idClient);
				var _nameProject = getById('newProjectName' + idClient);
				if (_nameProject == null || _customCode == null)
				{
					return;
				}
				
				if (_customCode.value == '') 
				{
					alert(msg1);
					selectAction.selectedIndex = 0;
					_customCode.focus();
					_btn.disabled = true;
					return;
				}

				if (_nameProject.value == '')
				{
					alert(msg2);
					selectAction.selectedIndex = 0;
					_nameProject.focus();
					_btn.disabled = true;
					return;
				}
				
				AjaxRequest.get(
					{
					'url': __rootPath + 'cwc/blogic/CommercialProjects/addNewCommercialProject.asp',
					'parameters':
						{
							'idClient': idClient,
							'nameProject': _nameProject.value,
							'customCode': _customCode.value
						},
					'timeout': 10000,
					'onSuccess': function(req)
						{
							var tIdProject = req.responseText.split('idProject=')[1];
							location.href = __rootPath + 'cwc/blogic/CommercialProjects/fullListProjects.asp?idProject=' + tIdProject;
						},
					'onComplete': hideWaiting,
					'onLoading': showWaiting,
					'onError': function (req)
						{
							alert(msgError);
						}
					}
				);
			}
			else
			{
				var _table   = getById('tableProject' + idClient);
				var _tr      = getById('newProjectRow' + idClient);
				var _trTitle = getById('newTitleRow' + idClient);
				
				if (_table == null || _tr == null || _trTitle == null)
				{
					return;
				}

				_table.deleteRow(_tr.rowIndex);
				_table.deleteRow(_tr.rowIndex);
				
			}
		}
		
		function delProject(idProject, nameProject, msg1, path)
		{
			if (confirm(msg1 + ' ' + nameProject + "'?"))
			{
				AjaxRequest.get(
					{
					'url': path + 'cwc/blogic/CommercialProjects/del.asp?idProject=' + idProject,
					'timeout': 10000,
					'onSuccess': function()
						{
							location.reload();
						},
					'onComplete': hideWaiting,
					'onLoading': showWaiting,
					'onError': function ()
						{
							alert(msgError);
						}
					}
				);
			}
		}
		
		function showProjects(idClient, p)
		{
			_lastIDClient = idClient;
			doShowProjects(p);
		}

		function doShowProjects(p)
		{
			if (!_lastIDClient)
			{
				return;
			}

			var _table = getById('projects' + _lastIDClient);
			if (_table == null)
			{
				_lastIDClient = 0;
				return;
			}
			
			var _img = getById('plus' + _lastIDClient);
			
			if (_img == null)
			{
				_lastIDClient = 0;
				return;
			}
			
			var tempShowed = _showedTableProject;
			
			if (_showedTableProject != null)
			{
				_showedTableProject.style.display = 'none';
				_showedTableProject = null;
				_showedTreeImage.src = p + 'images/tPlus.gif';
				_showedTreeImage = null;
				_lastIDClient = 0;
			}
			
			if (tempShowed != _table)
			{
				_table.style.display = '';
				_showedTableProject = _table;
				_showedTreeImage = _img;
				_img.src = p + 'images/tMinus.gif';
			}
		}
		
		function openProjectProcessArea(id_project, p)
		{
			
			_showedTableProject = null;
			__rootPath  = p;
			refreshArea = listCommercialProjects;
			openInnerWindow(p + 'cwc/blogic/CommercialProjects/ProjectProcesses/processDataForm.asp?idProject=' + id_project, 750, 510, true);
		}
		
		function selectProjectRow(row, idProject)
		{
			if (!selectRow(row, 'tableProject' + idProject))
			{
				return;
			}
		}
		
		function checkAndListContracts(p)
		{
			__rootPath = p;
			listContracts();
			void(0);
		}
		
		function checkAndListUsers(p)
		{
			__rootPath = p;
			listUsers();
			void(0);
		}
		
		function checkAndListClients(p)
		{
			__rootPath = p;
			listClients();
			void(0);
		}
		
		function checkAndListCommercialProjects(p)
		{
			__rootPath = p;
			listCommercialProjects();
			void(0);
		}

		function openLostPassword(path)
		{
			openInnerWindow(path + 'cwc/retrievePassword.asp', 380, 210, true);
		}
		
		function changeClientPermission(checkBox, idClient, idUser, path)
		{
			if (checkBox == null) return;
			
			AjaxRequest.get(
				{
				'url': path + 'cwc/blogic/Clients/updateUserClientPermissions.asp',
				'parameters': 
					{
						'idUser': idUser,
						'idClient': idClient,
						'allowed': checkBox.checked
					},
				'timeout': 10000,
				'onError': function ()
					{
						alert(msgError);
					}
				}
			);
			
			return true;
		}
		
