getScreenResolution();

function getScreenResolution() {
	screenWidth = document.documentElement.clientWidth;
	screenHeight = document.documentElement.clientHeight;
	if (document.getElementById(idPre + 'left')) {
		fitBody();
	}
	//setTimeout('getScreenResolution()', 2000);
}

function getResponse() {
	if (xhttp.readyState == 4) {
		if (xhttp.status == 200) {
			takeContentData(xhttp.responseText);
		}
		else {
			alert('Not found');
		}
	}
}

function getContentDataSynch(editData) {
	var params;
	var page;
	if (editData == true) {
		params = 'action=edit&id=' + currentWorkItem;
		page = currentForm['name'];
	}
	else {
		page = currentState['data'];
		if (currentState['action'] == 'list')  {
			params = 'action=list&page=' + currentState['resultPage'] + '&results=' + resultsOnPage[currentState['data']];
		}
		else {
			params = 'action=detail&id=' + currentState['id'];
		}
	}
	var url = 'data/' + page + '.php?' + params;
	xhttp.open('GET', url, false);
	xhttp.send(null);
	eval(xhttp.responseText);
}

function takeContentData(response) {
	eval(response);
	switch(currentState['page']) {
		case 'detail':
			eval(currentState['data'] + 'Detail()');
		break;
		default:
			eval(currentState['data'] + 'Data()');
		break;
	}
}

function drawFooter() {
	document.write(copyrightYear);
	var currentYear = new Date();
	var currentYear = currentYear.getFullYear();
	if (copyrightYear < currentYear) {
		document.write('&nbsp;&ndash;&nbsp;' + currentYear);
	}
}

function showWindowLoading() {
	disableDiv('all');
	document.getElementById(idPre + 'windowLoading').style.visibility = 'visible';
}

function hideWindowLoading() {
	setTimeout('document.getElementById(idPre + \'windowLoading\').style.visibility = \'hidden\';  enableDiv(\'all\');', 1000);
}

function initWindow(name, width, height, text) {
	windowWidth = width + 0;
	windowHeight = height + 28;
	var windowMarginLeft = windowWidth / 2;
	var windowMarginTop = windowHeight / 2;
	document.getElementById(idPre + 'window').style.width = windowWidth.toString() + 'px';
	document.getElementById(idPre + 'window').style.height = windowHeight.toString() + 'px';
	document.getElementById(idPre + 'window').style.left = '50%';
	document.getElementById(idPre + 'window').style.top = '50%';
	document.getElementById(idPre + 'window').style.marginLeft = '-' + windowMarginLeft.toString() + 'px';
	document.getElementById(idPre + 'window').style.marginTop = '-' + windowMarginTop.toString() + 'px';

	document.getElementById(idPre + 'windowContent').style.width = width.toString() + 'px';
	document.getElementById(idPre + 'windowContent').style.height = height.toString() + 'px';
	document.getElementById(idPre + 'windowContent').innerHTML = '<div id="' + idPre + name + '"></div>';
	document.getElementById(idPre + 'windowText').style.width = (width - windowIconsWidth).toString() + 'px';
	document.getElementById(idPre + 'windowText').innerHTML = text;
	if (name == 'delete') {
		document.getElementById(idPre + 'windowIcon').innerHTML = '<img src="icons/' + currentPage + '_' + name + '.png" style="width: 16px; height: 16px;" alt="' + name + '" />';
	}
	else {
		document.getElementById(idPre + 'windowIcon').innerHTML = '<img src="icons/' + name + '.png" style="width: 16px; height: 16px;" alt="' + name + '" />';
	}
}

function initWindowForm(name, width, height, text) {
	windowFormWidth = width + 0;
	windowFormHeight = height + 28;
	var windowFormMarginLeft = windowFormWidth / 2;
	var windowFormMarginTop = windowFormHeight / 2;
	windowFormPosX = (screenWidth - windowFormWidth) / 2;
	windowFormPosY = (screenHeight - windowFormHeight) / 2;
	document.getElementById(idPre + 'windowForm').style.width = windowFormWidth.toString() + 'px';
	document.getElementById(idPre + 'windowForm').style.height = windowFormHeight.toString() + 'px';
	document.getElementById(idPre + 'windowForm').style.left = '50%';
	document.getElementById(idPre + 'windowForm').style.top = '50%';
	document.getElementById(idPre + 'windowForm').style.marginLeft = '-' + windowFormMarginLeft.toString() + 'px';
	document.getElementById(idPre + 'windowForm').style.marginTop = '-' + windowFormMarginTop.toString() + 'px';

	document.getElementById(idPre + 'windowFormContent').style.width = width.toString() + 'px';
	document.getElementById(idPre + 'windowFormContent').style.height = height.toString() + 'px';
	document.getElementById(idPre + 'windowFormContent').innerHTML = '<div id="' + idPre + name + 'Form"></div>';
	document.getElementById(idPre + 'windowFormText').style.width = (width - windowIconsWidth).toString() + 'px';
	document.getElementById(idPre + 'windowFormText').innerHTML = text;
	document.getElementById(idPre + 'windowFormIcon').innerHTML = '<img src="icons/' + name + '_' + currentForm['action'] + '.png" style="width: 16px; height: 16px;" alt="' + name + '" />';
}

function fitWindowDetail() {
	windowDetailHeight = screenHeight - 100;
	var height = windowDetailHeight - 25;
	var width = 800;
	if (document.getElementById(idPre + 'photogallery')) {
		var maxGalImageSize = 550;
		var elseGalWidth = 326;
		var elseGalHeight = 50;
		var actualImageSize = height - elseGalHeight;
		if (actualImageSize > maxGalImageSize) {
			actualImageSize = maxGalImageSize;
			height = actualImageSize + elseGalHeight;
			windowDetailHeight = height + 25;
		}
		var actualImageSizeBorders = actualImageSize + 20;
		width = actualImageSize + elseGalWidth;
		var windowDetailMarginLeft = width / 2;
	}
	var heightContent = height - 30;
	heightContent = heightContent.toString() + 'px';
	var windowDetailMarginTop = windowDetailHeight / 2;
	windowDetailPosY = (screenHeight - windowDetailHeight) / 2;
	document.getElementById(idPre + 'windowDetailContent').style.height = height.toString() + 'px';
	document.getElementById(idPre + 'windowDetail').style.height = windowDetailHeight.toString() + 'px';
	document.getElementById(idPre + 'windowDetail').style.marginTop = '-' + windowDetailMarginTop.toString() + 'px';
	document.getElementById(idPre + 'windowDetailText').style.width = (width - windowIconsWidth).toString() + 'px';
	if (document.getElementById(idPre + 'windowDetailContentList')) {
		document.getElementById(idPre + 'windowDetailContentList').style.height = heightContent;
	}
	if (document.getElementById(idPre + 'windowDetailContentText')) {
		document.getElementById(idPre + 'windowDetailContentText').style.height = heightContent;
	}
	if (document.getElementById(idPre + 'photogallery')) {
		document.getElementById(galPre + 'mainImage').style.maxHeight = actualImageSize.toString() + 'px';
		document.getElementById(galPre + 'mainImage').style.maxWidth = actualImageSize.toString() + 'px';
		document.getElementById(galPre + 'mainPhoto').style.height = actualImageSizeBorders.toString() + 'px';
		document.getElementById(galPre + 'mainPhoto').style.width = actualImageSizeBorders.toString() + 'px';
		document.getElementById(idPre + 'windowDetail').style.width = width.toString() + 'px';
		document.getElementById(idPre + 'windowDetail').style.marginLeft = '-' + windowDetailMarginLeft.toString() + 'px';
	}
}

function initWindowDetail(name) {
	if (!name) {
		name = currentState['data'];
	}
	windowDetailWidth = 800;
	var windowDetailMarginLeft = windowDetailWidth / 2;
	windowDetailPosX = (screenWidth - windowDetailWidth) / 2;
	document.getElementById(idPre + 'windowDetail').style.width = windowDetailWidth.toString() + 'px';
	document.getElementById(idPre + 'windowDetail').style.left = '50%';
	document.getElementById(idPre + 'windowDetail').style.top = '50%';
	document.getElementById(idPre + 'windowDetail').style.marginLeft = '-' + windowDetailMarginLeft.toString() + 'px';

	document.getElementById(idPre + 'windowDetailContent').style.width = windowDetailWidth.toString() + 'px';
	document.getElementById(idPre + 'windowDetailText').innerHTML = dic['windowDetail'][name];
	document.getElementById(idPre + 'windowDetailIcon').innerHTML = '<div style="width: 25px;"><img src="icons/' + name + '.png" style="width: 16px; height: 16px;" alt="' + name + '" /><div>';
}

function showWindow(windowType) {
	setTimeout('document.getElementById(idPre + \'' + windowType + '\').style.visibility = \'visible\'', 250);
	hideWindowLoading();
}

function hideWindow(windowType) {
	document.getElementById(idPre + windowType).style.visibility = 'hidden';
	switch(windowType) {
		case 'window':
			if (document.getElementById(idPre + 'windowForm').style.visibility == 'visible') {
				enableDiv('windowForm');
			}
			else if (document.getElementById(idPre + 'windowDetail').style.visibility == 'visible') {
				enableDiv('windowDetail');
			}
			else {
				enableDiv('wrapper');
			}
		break;
		case 'windowForm':
			if (document.getElementById(idPre + 'windowDetail').style.visibility == 'visible') {
				enableDiv('windowDetail');
			}
			else {
				enableDiv('wrapper');
			}
		break;
		case 'windowDetail':
			enableDiv('wrapper');
		break;
		default:
			enableDiv('wrapper');
		break;
	}
}

function showMessage(type, what) {
	showLoadingDisable('window');
	initWindow(type, 300, 100, dic['window'][what]);
	var output;
	output = '<table>'
		+ '<tr><th>' + dic[what][type]
		+ '</th></tr><tr><td><input id="' + idPre + 'windowOkButton" type="button" value="ok" class="noButton" onclick="hideWindow(\'window\');" /></td></tr>'
	document.getElementById(idPre + type).innerHTML = output;
	showWindow('window');
}

function showLoadingDisable(windowType) {
	showWindowLoading();
	switch(windowType) {
		case 'window':
			if (document.getElementById(idPre + 'windowForm').style.visibility == 'visible') {
				disableDiv('windowForm');
			}
			else if (document.getElementById(idPre + 'windowDetail').style.visibility == 'visible') {
				disableDiv('windowDetail');
			}
			else {
				disableDiv('wrapper');
			}
		break;
		case 'windowForm':
			if (document.getElementById(idPre + 'windowDetail').style.visibility == 'visible') {
				disableDiv('windowDetail');
			}
			else {
				disableDiv('wrapper');
			}
		break;
		case 'windowDetail':
			disableDiv('wrapper');
		break;
		default:
			disableDiv('wrapper');
		break;
	}
}

function disableDiv(name) {
	document.getElementById(idPre + name + 'Disable').style.height = screenHeight + 'px';
	setTimeout('document.getElementById(\'' + idPre + name + 'Disable\').style.visibility = \'visible\';', 10);
}

function enableDiv(name) {
	document.getElementById(idPre + name + 'Disable').style.visibility = 'hidden';
}

function fitBody() {
	var minusHeight = 9;
	var minusMarginHeight = 19;
	var iconsWidth = 50;
	document.getElementById(idPre + 'wrapper').style.height = screenHeight + 'px';
	document.getElementById(idPre + 'left').style.height = (screenHeight - elseHeight - minusHeight) + 'px';
	document.getElementById(idPre + 'contentLeft').style.height = (screenHeight - elseHeight - minusMarginHeight) + 'px';
	document.getElementById(idPre + 'contentCenter').style.height = (screenHeight - elseHeight - minusMarginHeight) + 'px';
	if (document.getElementById(idPre + 'allDisable')) {
		document.getElementById(idPre + 'wrapperDisable').style.height = screenHeight + 'px';
		document.getElementById(idPre + 'windowDetailDisable').style.height = screenHeight + 'px';
		document.getElementById(idPre + 'windowFormDisable').style.height = screenHeight + 'px';
		document.getElementById(idPre + 'windowDisable').style.height = screenHeight + 'px';
		document.getElementById(idPre + 'allDisable').style.height = screenHeight + 'px';
		fitWindowDetail();
	}
	var menuItemName;
	var curLeft;
	for (var i = 0; i < menuLists.length; i++) {
		menuItemName = menuLists[i];
		curLeft = document.getElementById(idPre + 'topMenuItem-' + menuItemName).offsetLeft + document.getElementById(idPre + 'wrapper').offsetLeft;
		document.getElementById(idPre + 'topMenuList-' + menuItemName).style.left = '' + curLeft + 'px';
	}
}

function checkElements() {
	if (lockMenuSwitching === true) {
		lockMenuSwitching = false;
	}
	else {
		if (currentMenuList !== null) {
			switchList(currentMenuList);
		}
	}
}

function date(dateFormat, timestamp, withoutMillis) {
	var convDate = new Date();
	var returnString = '';
	var val;
	if (timestamp != null) {
		if (withoutMillis == true) {
			timestamp *= 1000;
		}
		convDate.setTime(timestamp);
	}
	for (var index = 0; index < dateFormat.length; index++) {
		switch (dateFormat.charAt(index)) {
			case 'd':
				val = convDate.getDate();
				if (val < 10) {
					val = '0' + val.toString();
				}
				returnString += val;
			break;
			case 'D':
				returnString += dic['dateNames']['dayShort'][convDate.getDay()];
			break;
			case 'j':
				returnString += convDate.getDate();
			break;
			case 'l':
				returnString += dic['dateNames']['day'][convDate.getDay()];
			break;
			case 'N':
				val = convDate.getDay();
				if (val == 0) {
					returnString += '7';
				}
				else {
					returnString += val;
				}
			break;
			case 'w':
				returnString += convDate.getDay();
			break;
			case 'F':
				returnString += dic['dateNames']['month'][convDate.getMonth()];
			break;
			case 'm':
				val = convDate.getMonth() + 1;
				if (val < 10) {
					returnString += '0';
				}
				returnString += val;
			break;
			case 'M':
				returnString += dic['dateNames']['monthShort'][convDate.getMonth()];
			break;
			case 'n':
				returnString += (convDate.getMonth() + 1);
			break;
			case 'Y':
				returnString += convDate.getFullYear();
			break;
			case 'y':
				val = convDate.getFullYear().toString();
				returnString += val.substr(2);
			break;
			case 'a':
				returnString += (convDate.getHours() < 12) ? dic['dateNames']['time']['am'] : dic['dateNames']['time']['pm'];
			break;
			case 'A':
				val = (convDate.getHours() < 12) ? dic['dateNames']['time']['am'] : dic['dateNames']['time']['pm'];
				returnString += val.toUpperCase();
			break;
			case 'g':
				val = convDate.getHours();
				if (val == 0) {
					val = 12;
				}
				else if (val > 12) {
					val -= 12;
				}
				returnString += val;
			break;
			case 'G':
				returnString += convDate.getHours();
			break;
			case 'h':
				val = convDate.getHours();
				if (val == 0) {
					val = 12;
				}
				else if (val > 12) {
					val -= 12;
				}
				if (val < 10) {
					val = '0' + val.toString();
				}
				returnString += val;
			break;
			case 'H':
				val = convDate.getHours();
				if (val < 10) {
					val = '0' + val.toString();
				}
				returnString += val;
			break;
			case 'i':
				val = convDate.getMinutes();
				if (val < 10) {
					val = '0' + val.toString();
				}
				returnString += val;
			break;
			case 's':
				val = convDate.getSeconds();
				if (val < 10) {
					val = '0' + val.toString();
				}
				returnString += val;
			break;
			case 'u':
				returnString += convDate.getMilliseconds();
			break;
			case 'U':
				val = convDate.getTime().toString();
				val = val.substr(0, val.length - 3);
				returnString += val;
			break;
			default:
				returnString += dateFormat.charAt(index);
			break;
		}
	}
	return returnString;
}

function textProcess(text) {
	text = text.replace(/\r\n/g, '\n');
	text = text.replace(/\&/, '&amp;');
	text = text.replace(/\</, '&lt;');
	text = text.replace(/\>/, '&gt;');
	var pos;
	var pos2;
	var temp = new Array(3);
	var tagLock = false;
	var patt;
	var lists = {'ul': '*', 'ol': '+'};
	do {
		pos = text.search('--');
		if (text.substr(pos + 1).search('--') != -1) {
			text = text.replace('--', '<b>');
			text = text.replace('--', '</b>');
		}
		else {
			pos = -1;
		}
	} while (pos != -1);
	
	do {
		pos = text.search('//');
		if (text.substr(pos + 1).search('//') != -1) {
			text = text.replace('//', '<i>');
			text = text.replace('//', '</i>');
		}
		else {
			pos = -1;
		}
	} while (pos != -1);

	for (type in lists) {
		do {
			if (lists[type] == '*') {
				patt = /(^|\n)\*\s*/;
			}
			else if (lists[type] == '+') {
				patt = /(^|\n)\+\s*/;
			}
			pos = text.search(patt);
			if (pos != -1) {
				pos++;
				pos2 = text.substr(pos + 1).search(/\n|$/);
				if (pos2 != -1) {
					temp[0] = text.substr(0, pos - 1);
					temp[1] = text.substr(pos + 1, pos2);
					temp[2] = text.substr(pos + pos2 + 1);
					text = temp[0];
					if (tagLock == false) {
						text += '</p><' + type + '>';
						tagLock = true;
					}
					text += '<li>' + temp[1] + '</li>';
					if (temp[2].charAt(1) !== lists[type]) {
						text += '</' + type + '><p>';
						tagLock = false;
					}
					text += temp[2];
				}
			}
		} while (pos != -1);
	}
	
	do {
		var type = 'h5';
		pos = text.search('(^|\n):::\s*');
		if (pos != -1) {
			pos2 = text.substr(pos + 1).search(/\n|$/);
				if (pos2 != -1) {
					if (pos != 0) {
						temp[0] = text.substr(0, pos + 1);
						temp[1] = text.substr(pos + 4, pos2 - 3);
					}
					else {
						temp[0] = '';
						temp[1] = text.substr(pos + 3, pos2 - 2);
					}
					temp[1] = temp[1].replace(/^\s+/, '');
					temp[2] = text.substr(pos + pos2 + 1);
					text = temp[0] + '</p><' + type + '>' + temp[1] + '</' + type + '><p>' + temp[2];
				}
		}
	} while (pos != -1);

	do {
		var type = 'h4';
		pos = text.search('(^|\n)::\s*');
		if (pos != -1) {
			pos2 = text.substr(pos + 1).search(/\n|$/);
				if (pos2 != -1) {
					if (pos != 0) {
						temp[0] = text.substr(0, pos + 1);
						temp[1] = text.substr(pos + 3, pos2 - 2);
					}
					else {
						temp[0] = '';
						temp[1] = text.substr(pos + 2, pos2 - 1);
					}
					temp[1] = temp[1].replace(/^\s+/, '');
					temp[2] = text.substr(pos + pos2 + 1);
					text = temp[0] + '</p><' + type + '>' + temp[1] + '</' + type + '><p>' + temp[2];
				}
		}
	} while (pos != -1);
	text = '<p>' + text + '</p>';
	text = text.replace(/\n{2,}/g, '</p><p>');
	text = text.replace(/<p>\s*/g, '<p>');
	text = text.replace(/\s*<\/p>/g, '</p>');
	text = text.replace(/<p>\s*<\/p>/g, '');
	text = text.replace(/\n/g, '<br />');
	return text;
}

function urlencode(text) {
	if (text != '' && text !== null) {
		var characters = {/*'21': /\!/, '23': /\#/g, '24': /\$/g, */'25': /\%/g, '26': /\&/g, '2a': /\*/g,
						'2b': /\+/g, '2f': /(\/)/g, '3d': /\=/g, '3f': /\?/g, '40': /\@/g, '5c': /\\/};
		for (chr in characters) {
			text = text.replace(characters[chr], '%' + chr);
		}
	}
	return text
}

function switchDetailPhotos(next) {
	var prevImg;
	var nextImg;
	if (next === true) {
		if (photoList[0] == photoList.length - 1) {
			photoList[0] = 1;
		}
		else {
			photoList[0]++;
		}
	}
	else {
		if (photoList[0] == 1) {
			photoList[0] = photoList.length - 1;
		}
		else {
			photoList[0]--;
		}
	}
	if (photoList[0] == 1) {
		prevImg = photoList.length - 1;
	}
	else {
		prevImg = photoList[0] - 1;
	}
	if (photoList[0] == photoList.length - 1) {
		nextImg = 1;
	}
	else {
		nextImg = photoList[0] + 1;
	}
	document.getElementById(galPre + 'mainImage').src = photoList[photoList[0]];
	document.getElementById(galPre + 'nextImage').src = photoList[nextImg];
	document.getElementById(galPre + 'prevImage').src = photoList[prevImg];
	document.getElementById(galPre + 'counter').innerHTML = photoList[0];
}




