function refreshData(page) {
	showWindowLoading();
	currentState['resultPage'] = page;
	if (param) {
		currentState['param'] = param;
	}
	else {
		currentState['param'] = false;
	}
	eval(currentPage + 'GetData()');
}

function rewriteList() {
	showWindowLoading();
	currentState['page'] = 'list';
	currentState['form'] = 'add';
	currentState['action'] = 'list';
	if (currentState['list'] !== false) {
		document.getElementById(idPre + 'content').innerHTML = currentState['list'];
		hideWindowLoading();
	}
	else {
		calendarGetData();
	}
}

function dateDisplay(milis, dateOnly, dateOnlyIfNoTime) {
	var curVal = new Date();
	curVal.setTime(milis);
	var dateToDisp = curVal.getDate() + '.' + (curVal.getMonth() + 1) + '.' + curVal.getFullYear() + ' ';
	if ((dateOnly == null || dateOnly == false) && (curVal.getMilliseconds() == 555 || curVal.getMilliseconds() == 999)) {
		var minutes = curVal.getMinutes();
		if (minutes < 10) {
			minutes = '0' + minutes.toString();
		}
		dateToDisp += curVal.getHours() + ':' + minutes;
		if (curVal.getMilliseconds() == 999) {
			var seconds = curVal.getSeconds();
			if (seconds < 10) {
				seconds = '0' + seconds.toString();
			}
			dateToDisp += ':' + seconds;
		}
	}
	else if ((dateOnly == null || dateOnly == false) && (dateOnlyIfNoTime == null || dateOnlyIfNoTime == false) && curVal.getMilliseconds() == 222) {
		dateToDisp += dic['calendar']['allDay'];
	}
	return dateToDisp;
}

/* Forms */
function addForm(formType) {
	showLoadingDisable('windowForm');
	currentWorkItem = false;
	currentForm['action'] = 'add';
	eval(formType + 'InitForm();');
	drawForm();
	if (eval('typeof(' + formType + 'DefaultForm)') == 'function') {
		eval(formType + 'DefaultForm();');
	}
	defaultFormState();
	showWindow('windowForm');
}

function saveFromForm() {
	var curAction = 'save_' + currentForm['action'];
	if (currentForm['action'] == 'edit') {
		curAction += '&id=' + currentWorkItem;
	}
	var saveValues = '';
	for (formElement in formElements) {
		saveValues += formElement + '=' + document.getElementById(formPre + formElement).value + '&';
	}
	saveValues = saveValues.substr(0, saveValues.length - 1);
	xhttp.open('POST', 'data/' + currentForm['name'] + '.php?action=' + curAction, false);
	xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	xhttp.send(saveValues);
	var resultSave = eval(xhttp.responseText);
	if (resultSave === true) {
		hideWindow('windowForm');
		enableDiv('windowForm');
		showMessage('ok', 'add_' + currentForm['name']);
		refreshData(currentState['resultPage']);
	}
	else {
		showMessage('failed', 'add_' + currentForm['name']);
		hideWindowLoading();
	}
}

function drawForm() {
	var output = '';
	var inner;
	var icon;
	if (currentForm['window'] == true) {
		output += '<table><tr><td>';
	}
	output += '<form method="post" title="' + currentForm['name'] + '" id="' + idPre + 'mainform" action="" onsubmit="';
	if (currentForm['action'] == 'login') {
		output += 'return loginUser();'
	}
	else {
		output+= 'return validateSubmit();';
	}
	output += '" onreset="resetForm(); return false;">';
	output += '<p id="' + idPre + 'mainform-error"></p>';
	for (formElement in formElements) {
		icon = '';
		switch (formElements[formElement]['type']) {
			case 'date':
				/* Should have been changed */
				inner = '<input type="hidden" name="' + formElement + '" id="' + formPre + formElement + '" />'
					+ '<input type="text" id="' + formPre + formElement + '_display" '
					//+ 'onfocus="if(document.getElementById(idPre + \'window\').style.visibility == \'hidden\') { currentFormElement = \'' + formElement + '\'; showCalendar(); }" '
					+ 'class="format" disabled="disabled" />';
				icon = '<img src="icons/calendar.png" '
					+ 'onclick=currentFormElement = \'' + formElement + '\'; showCalendar();" '
					+ 'class="icon" alt="Calendar icon" />';
			break;
			case 'dateTime':
				inner = '<input type="hidden" name="' + formElement + '" id="' + formPre + formElement + '" />'
					+ '<input type="text" id="' + formPre + formElement + '_display" '
					//+ 'onfocus="if (document.getElementById(idPre + \'window\').style.visibility == \'hidden\') { currentFormElement = \'' + formElement + '\'; showCalendar(); }" '
					+ 'class="format" disabled="disabled" />';
				icon = '<img id="' + formPre + formElement + '-calendarIcon" src="icons/calendar.png" '
					+ 'onclick="currentFormElement = \'' + formElement + '\'; showCalendar();" '
					+ 'class="icon" alt="Calendar icon" ';
					if (formElements[formElement]['part'] == 'to') {
						icon += 'style="visibility: hidden;" '
					}
				icon += '/>'
					+ '<img id="' + formPre + formElement + '-clockIcon" src="icons/clock.png" '
					+ 'onclick="currentFormElement = \'' + formElement + '\'; showClock();" '
					+ 'class="icon" alt="clock icon" ';
				if (formElements[formElement]['part'] == 'to') {
					icon += 'style="visibility: hidden;" '
				}
				icon += '/>';if (formElements[formElement]['part'] == 'to') {
					icon += '<img id="' + formPre + formElement + '-switchIcon" src="icons/disabled.png" '
						+ 'onclick="currentFormElement = \'' + formElement + '\'; enableDisableTo();" '
						+ 'class="icon" alt="switch to" />';
				}
			break;
			case 'select':
				inner = '<select name="actionType" id="' + formPre + 'actionType" class="format">'
					+ '<option value="1">Závod</option>'
					+ '<option value="2">Sjezd</option>'
					+ '<option value="3">Jiná</option>'
					+ '</select>';
			break;
			case 'check':
				inner = '<input type="hidden" name="' + formElement + '" id="' + formPre + formElement + '" />'
					 + '<img src="" alt="" id="' + formPre + formElement + '_display" '
					 + 'onclick="currentFormElement = \'' + formElement + '\'; checkIt();" class="icon" />';
			break;
			case 'textarea':
				inner = '<textarea name="' + formElement + '" id="' + formPre + formElement + '" rows="6" cols="40" style="margin-bottom: 10px; float: left;" onfocus="currentFormElement = \'' + formElement + '\'; ';
				if (formElements[formElement]['noValidation'] != true) {
					inner += 'oldValue = \'\';" onblur="validate();" onkeydown="oldValue = validateOldValue();"  onkeyup="validateNewValue();" ';
				}
				else {
					inner += '" ';
				}
				inner += '></textarea>';
			break;
			case 'hidden':
				inner = '<input type="hidden" name="' + formElement + '" id="' + formPre + formElement + '" />';
			break;
			case 'password':
				inner = '<input type="password" name="' + formElement + '" id="' + formPre + formElement + '" onfocus="currentFormElement = \'' + formElement + '\'; '
					+ 'oldValue = \'\';" onblur="validate();" onkeydown="oldValue = validateOldValue();"  onkeyup="validateNewValue();" '
					+ 'class="format" />';
			break;
			case 'text':
			default:
				inner = '<input type="text" name="' + formElement + '" id="' + formPre + formElement + '" onfocus="currentFormElement = \'' + formElement + '\'; ';
				if (formElements[formElement]['noValidation'] != true) {
					inner += 'oldValue = \'\';" onblur="validate();" onkeydown="oldValue = validateOldValue();"  onkeyup="validateNewValue();" ';
				}
				else {
					inner += '" ';
				}
				if (formElements[formElement]['maxlength']) {
					inner += 'maxlength="' + formElements[formElement]['maxlength'] + '" ';
				}
				inner += 'class="format" />';
			break;
		}
		if (formElements[formElement]['type'] == 'hidden') {
			output += '<div>' + inner + '</div>';
		}
		else {
			output += '<p id="' + formElement + '_line">'
				+ '<label id="' + formPre + formElement + '_label" for="' + formPre + formElement + '">'
				+ dic['forms'][currentForm['name']][formElement] + '</label>'
				+ inner + '<span id="' + formElement + '_icon">' + icon + '</span>&nbsp;<span id="' + formElement + '_error"></span></p>'
				+ '<div class="clear" style="width: 100%;"></div>';
		}
	}
	output += '<p><label>&nbsp;</label>';
	output += '<input type="submit" name="submit" id="' + formPre + 'submit" value="' + dic['forms'][currentForm['name']]['submit_' + currentForm['action']] + '" class="submitDisabled" />';
	if (currentForm['reset'] == true) {
		output += '&nbsp;<input type="reset" name="reset" value="Původní" class="reset" />';
	}
	output += '</p>'
		+ '<div class="clear" style="width: 100%;"></div>'
		+ '</form>';
	if (currentForm['window'] == true) {
		output += '</td></tr></table>';
	}
	document.getElementById(idPre + currentForm['name'] + 'Form').innerHTML = output;
}

function resetForm () {
	for (formElement in formElements) {
		document.getElementById(formPre + formElement).value = formElements[formElement]['resetValue'];
	}
	formElement = '';
	defaultFormState();
}

function checkIt() {
	var checkVal = document.getElementById(formPre + currentFormElement).value;
	if (checkVal == 1) {
		document.getElementById(formPre + currentFormElement + '_display').src = disabledImg.src;
		document.getElementById(formPre + currentFormElement + '_display').alt = 'disabled';
		document.getElementById(formPre + currentFormElement).value = 0;
	}
	else {
		document.getElementById(formPre + currentFormElement + '_display').src = enabledImg.src;
		document.getElementById(formPre + currentFormElement + '_display').alt = 'enabled';
		document.getElementById(formPre + currentFormElement).value = 1;
	}
}

function enableDisableTo() {
	var val = document.getElementById(formPre + currentFormElement).value;
	if (val == '0' || val == '') {
		var fromElement = formElements[currentFormElement]['fromElement'];
		val = parseInt(document.getElementById(formPre + fromElement).value) + 86400000;
		document.getElementById(formPre + currentFormElement).value = val;
		document.getElementById(formPre + currentFormElement + '_display').value = dateDisplay(val);
		document.getElementById(formPre + currentFormElement + '-calendarIcon').style.removeProperty('visibility');
		document.getElementById(formPre + currentFormElement + '-clockIcon').style.removeProperty('visibility');
		document.getElementById(formPre + currentFormElement + '-switchIcon').src = enabledImg.src;
	}
	else {
		document.getElementById(formPre + currentFormElement).value = '';
		document.getElementById(formPre + currentFormElement + '_display').value = '';
		document.getElementById(formPre + currentFormElement + '-calendarIcon').style.setProperty('visibility', 'hidden', '');
		document.getElementById(formPre + currentFormElement + '-clockIcon').style.setProperty('visibility', 'hidden', '');
		document.getElementById(formPre + currentFormElement + '-switchIcon').src = disabledImg.src;
	}
}

function pageGetData() {
	getContentDataSynch();
	document.getElementById(idPre + 'content').innerHTML = contentResponse;
	hideWindowLoading();
}

function getDetailData(id) {
	showLoadingDisable('windowDetail');
	initWindowDetail();
	currentState['action'] = 'detail';
	currentState['id'] = id;
	getContentDataSynch();
	document.getElementById(idPre + 'windowDetailContent').innerHTML = contentResponse;
	fitWindowDetail();
	showWindow('windowDetail');
	hideWindowLoading();
}

/* Calendar */
function calendarPage() {
	currentState['resultPage'] = 1;
	currentState['page'] = 'list';
	currentState['form'] = 'add';
	calendarGetData();
}

function calendarGetData() {
	currentState['action'] = 'list';
	currentState['data'] = 'calendar';
	pageGetData();
}

/* Articles */
function articlesPage() {
	currentState['resultPage'] = 1;
	currentState['page'] = 'list';
	currentState['form'] = 'add';
	articlesGetData();
}

function articlesGetData() {
	currentState['action'] = 'list';
	currentState['data'] = 'articles';
	pageGetData();
}

/* Book */
function bookPage() {
	currentState['resultPage'] = 1;
	currentState['page'] = 'list';
	currentState['form'] = 'add';
	bookGetData();
}

function bookGetData() {
	currentState['action'] = 'list';
	currentState['data'] = 'book';
	pageGetData();
}

function bookInitForm() {
	currentForm['name'] = 'book';
	currentForm['window'] = true;
	currentForm['reset'] = false;
	initWindowForm('book', 400, 250, 'Formulář pro přidání příspěvku');
	formElements = new Array(2);
	formElements['name'] = new Array(7);
	formElements['message'] = new Array(6);
	
	formElements['name']['type'] = 'text';
	formElements['name']['maxlenght'] = 100;
	formElements['name']['optional'] = false;
	formElements['name']['noValidation'] = false;
	formElements['name']['onChangeOnly'] = true;
	formElements['name']['isValid'] = false;
	formElements['name']['resetValue'] = '';
	
	formElements['message']['type'] = 'textarea';
	formElements['message']['optional'] = false;
	formElements['message']['noValidation'] = false;
	formElements['message']['onChangeOnly'] = true;
	formElements['message']['isValid'] = false;
	formElements['message']['resetValue'] = '';
}

/* Contacts */
function contactsPage() {
	currentState['page'] = 'detail';
	currentState['form'] = 'add';
	contactsGetData();
}

function contactsGetData() {
	currentState['action'] = 'list';
	currentState['data'] = 'contacts';
	pageGetData();
}

/* Rozpis */
function rozpisPage() {
	currentState['page'] = 'detail';
	currentState['form'] = 'edit';
	rozpisGetData();
}

function rozpisGetData() {
	currentState['action'] = 'detail';
	currentState['data'] = 'rozpis';
	pageGetData();
}

/* Payment */
function paymentPage() {
	currentState['page'] = 'detail';
	currentState['form'] = 'edit';
	paymentGetData();
}

function paymentGetData() {
	currentState['action'] = 'detail';
	currentState['data'] = 'payment';
	pageGetData();
}

/* History */
function historyPage() {
	currentState['page'] = 'detail';
	currentState['form'] = 'edit';
	historyGetData();
}

function historyGetData() {
	currentState['action'] = 'detail';
	currentState['data'] = 'history';
	pageGetData();
}

/* Fotogalery */
function fotogaleryPage() {
	currentState['resultPage'] = 1;
	currentState['page'] = 'list';
	currentState['form'] = 'add';
	fotogaleryGetData();
}

function fotogaleryGetData() {
	currentState['action'] = 'list';
	currentState['data'] = 'fotogalery';
	pageGetData();
}


