﻿//Funkce, ktera nastavi text uvnitr bloku identifikovaneho promennou Element
function ChangeText(Element, NewStr) {
	if (document.all) {
		document.all[Element].innerHTML = NewStr;
	} else
		document.getElementById(Element).innerHTML = NewStr;
};

function formularValidation(validateType, testString) { // test false ... vrací
	// -1 ; test ok ...
	// vraci 1 popripade vek
	// ziskany z RC ; treti
	// hodnota je nepovinná
	// a je to 0/1 - pole
	// může být
	// prázdné(nepovinné) /
	// nesmí být prázdné ..
	// default
	if (arguments.length > 2) {
		if (arguments[2] == 0 && testString.length == 0)
			return 1;
	}
	if (arguments.length > 3) {
		var_getFullYear = arguments[3].FullYear;
		var_getMonth = arguments[3].Month;
		var_getDate = arguments[3].Day;
	} else {
		today = new Date();
		var_getFullYear = today.getFullYear()
		var_getMonth = today.getMonth();
		var_getDate = today.getDate();
	}
	;
	// else if (testString.length == 0) return 1; // pro defaultní stav
	// nepovinné
	if (validateType == 1)
		validateSpec = /^\d{1,100}$/; // cisla
	if (validateType == 2)
		validateSpec = /^.{2,100}$/; // string
	if (validateType == 3)
		validateSpec = /^[a-zA-ZěščřžýáíéúůňťĚŠČŘŽÝÁÍÉÚŮŤŇ]{1,100}$/; // pismena
	if (validateType == 4)
		validateSpec = /^([\+|00]\d{2,3})?[ ]?\d{9,12}$/; // phone
	if (validateType == 5)
		validateSpec = /^.+@.+\..{2,4}$/; // email ...
	// cosi@cosi.dva az
	// tri
	// znaky
	if (validateType == 6)
		validateSpec = /^\d{3}[ ]?\d{2}$/; // psc ...
	// 5cisel nebo
	// 3cisla mezera
	// 2cisla
	if (validateType == 7)
		validateSpec = /^(http:\/\/)?(.+\.)*.{2,3}$/; // url
	if (validateType == 8)
		validateSpec = /^\d{5}$/; // psc2 ...
	// pouze 5cisel
	if (validateType == 9)
		validateSpec = /^.{1,100}[ ]*(\d{1,6}[a-zA-Z]?[ ]*((\/)[ ]*\d{1,9}[a-zA-Z]?)?)?[ ]*$/; // ulice
	// nebo
	// ulice
	// cislo
	// nebo
	// ulice
	// cislo/cislo
	if (validateType == 11) { // rodne
		// cislo
		// nebo
		// datum
		// narozeni
		// (YYMMDD)
		// ...
		// bere max vek 99let
		validateSpec = /^[0-9]{6}$/;
		day = parseInt(testString.substr(4, 2), 10);
		month = parseInt(testString.substr(2, 2), 10);
		year = parseInt(testString.substr(0, 2), 10);
		if (day > 31 || day < 1)
			return -1;
		if (validateSpec.test(testString) == true) {
			// today=new Date();
			if (month > 12 || month < 1)
				return -1;
			// if (today.getYear() < 100)
			// age = 100 + today.getYear() -
			// year;
			// else age = today.getYear() -
			// year;
			age = var_getFullYear - year;
			// if (age > 100) age = age -
			// 100 * Math.floor(age / 100);
			age = age - 100 * Math.floor(age / 100);
			if ((var_getMonth + 1) < month)
				age = age - 1;
			if ((var_getMonth + 1) == month && var_getDate < day)
				age = age - 1;
			return age;
		} else {
			validateSpec = /^[0-9]{9,10}$/;
			if (validateSpec.test(testString) == true) {
				rc = parseInt(testString);
				if (rc % 11 > 0) {
					if (testString.length == 10) {
						test = parseInt(testString.substr(0, 9), 10) % 11;
						if (test == 10)
							test = 0;
						if (testString.substr(9, 1) != test)
							return -1;
					} else
						return -1;
				}
				;
				// today=new Date();
				if (month > 50)
					month = month - 50;
				if (month > 20 && year > 2 && testString.length == 10)
					month = month - 20;
				if (month > 12 || month < 1)
					return -1;
				age = var_getFullYear - (year + 1900);
				if (age > 99 && year <= (var_getFullYear - 2000) && testString.length == 10)
					age = age - 100;
				if ((var_getMonth + 1) < month)
					age = age - 1;
				if ((var_getMonth + 1) == month && var_getDate < day)
					age = age - 1;
				if (age >= 0 && age < 120)
					return age;
				else
					return -1;
			} else
				return -1;
		}
		;
	}
	;
	if (validateSpec.test(testString) == true)
		return 1;
	else
		return -1;
}

function set_input_error(element, class_to_add) {
	classes = element.className;
	if (classes.indexOf(class_to_add) < 0) {
		element.className = element.className + ' ' + class_to_add;
	}
}

function unset_input_error(element, class_to_delete) {
	classes = element.className;
	class_position = classes.indexOf(class_to_delete);
	if (class_position != -1) {
		classes = classes.substring(0, (class_position - 1)) + classes.substring((class_position + class_to_delete.length), classes.length);
		element.className = classes;
	}
	;
}

function error_message() {
	alert(bad_input);
}

function check_complete_form_obednatel(error_class) {
	valid = 1;
	if (formularValidation(2, document.getElementById('lastName').value) != 1) {
		valid = 0;
		set_input_error(document.getElementById('lastName'), error_class);
	} else
		unset_input_error(document.getElementById('lastName'), error_class);

	if (formularValidation(2, document.getElementById('firstName').value) != 1) {
		valid = 0;
		set_input_error(document.getElementById('firstName'), error_class);
	} else
		unset_input_error(document.getElementById('firstName'), error_class);

	if (formularValidation(9, document.getElementById('street').value) != 1) {
		valid = 0;
		set_input_error(document.getElementById('street'), error_class);
	} else
		unset_input_error(document.getElementById('street'), error_class);

	if (formularValidation(9, document.getElementById('city').value) != 1) {
		valid = 0;
		set_input_error(document.getElementById('city'), error_class);
	} else
		unset_input_error(document.getElementById('city'), error_class);

	if (formularValidation(6, document.getElementById('zip').value) != 1) {
		valid = 0;
		set_input_error(document.getElementById('zip'), error_class);
	} else
		unset_input_error(document.getElementById('zip'), error_class);

	if (formularValidation(4, document.getElementById('phoneOffice').value, 0) != 1) {
		valid = 0;
		set_input_error(document.getElementById('phoneOffice'), error_class);
	} else
		unset_input_error(document.getElementById('phoneOffice'), error_class);

	if (formularValidation(4, document.getElementById('phonePersonal').value, 0) != 1) {
		valid = 0;
		set_input_error(document.getElementById('phonePersonal'), error_class);
	} else
		unset_input_error(document.getElementById('phonePersonal'), error_class);

	if (document.getElementById('phonePersonal').value == "" && document.getElementById('phoneOffice').value == "") {
		valid = 0;
		set_input_error(document.getElementById('phoneOffice'), error_class);
		set_input_error(document.getElementById('phonePersonal'), error_class);
	}
	;

	if (formularValidation(5, document.getElementById('email').value) != 1) {
		valid = 0;
		set_input_error(document.getElementById('email'), error_class);
	} else
		unset_input_error(document.getElementById('email'), error_class);

	if (formularValidation(11, document.getElementById('persIdentification').value, 0) < 0) {
		valid = 0;
		set_input_error(document.getElementById('persIdentification'), error_class);
	} else
		unset_input_error(document.getElementById('persIdentification'), error_class);
	if (valid == 1)
		return 1;
	else {
		error_message();
		return 0;
	}
	;
}

function checkInputField(element, type) {
	error_class = 'error_in_set';
	if (arguments.length > 2)
		mandatory = arguments[2];
	else
		mandatory = 1;

	if (formularValidation(type, element.value, mandatory) == -1)
		set_input_error(element, error_class);
	else
		unset_input_error(element, error_class);
}

function check_RC(element, row, error_class) { // row - cislo radku
	var participants_idetify;
	if (parseInt(row) == -1) {
		participants_idetify = '';
	} else {
		participants_idetify = 'participants' + row + '.orderParticipant.';
	}
	set_value = element.value;
	if (arguments.length > 3)
		age = formularValidation(11, set_value, 1, arguments[3]);
	else
		age = formularValidation(11, set_value);
	if (age != -1) {
		unset_input_error(element, error_class);
		if (document.getElementById(participants_idetify + 'personTypeString').value == 'D' || document.getElementById(participants_idetify + 'personTypeString').value == 'H' || (document.getElementById(participants_idetify + 'personTypeString').value == 'K' && document.getElementById(participants_idetify + 'personTypeString').disabled == false)) {
			var ret_value = 1;
			// nastaveni veku
			document.getElementById(participants_idetify + 'age').value = age;

			// kontrola zda se jedna o zenu ci muze dle RC a zda je nastaveno
			// odpovidajici osloveni
			if (set_value.length > 6 && document.getElementById(participants_idetify + 'personTypeString').value != 'K') {
				first_month_num = parseInt(set_value.substr(2, 1));
				if ((first_month_num > 4 && document.getElementById(participants_idetify + 'personTypeString').value == 'D') || (first_month_num < 4 && document.getElementById(participants_idetify + 'personTypeString').value == 'H')) {
					unset_input_error(element, error_class);
					unset_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
					ret_value = 1;
				} else {
					set_input_error(element, error_class);
					set_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
					return 0;
				}
				;
			}
			;
			// kontrola, ze dospeli musi byt starsi 18ti let
			if ((age < 18 && document.getElementById(participants_idetify + 'personTypeString').value != 'K') || (age >= 18  && document.getElementById(participants_idetify + 'personTypeString').value == 'K') ) {
				set_input_error(element, error_class);
				ret_value = 0;
			}
			;
			return ret_value;
		}
		;
		if (document.getElementById(participants_idetify + 'personTypeString').value == 'K' && document.getElementById(participants_idetify + 'personTypeString').disabled == true) {
			// kontrola zda vek ditete odpovida jeho rodnemu cislu
			if (document.getElementById(participants_idetify + 'age').value != age) {
				set_input_error(element, error_class);
				return 0;
			} else {
				unset_input_error(element, error_class);
				return 1;
			}
			;
		}
		;
		if (document.getElementById(participants_idetify + 'personTypeString').value == 'B') {
			// kotrola, zda vek ditete neni vetsi nez 2 a nastaveni
			// tohoto veku
			if (age > 2) {
				set_input_error(element, error_class);
				return 0;
			} else {
				document.getElementById(participants_idetify + 'age').value = age;
				unset_input_error(element, error_class);
				return 1;
			}
		}
	} else {
		set_input_error(element, error_class);
		return 0;
	}
}

function check_RC_Sym(element, row, error_class) { // row - cislo radku  ... paty argument je 0 / 1 a ozamuje zda se jedna o kontrolu v kombinaci (1) ... default stav je 0
        if (arguments.length > 4) combination = arguments[4];
      	   else combination = 0;
	var participants_idetify;
	if (parseInt(row) == -1) {
		participants_idetify = '';
	} else if (element.id.indexOf('.infant.') > 0) {
		participants_idetify = 'participants' + row + '.orderParticipant.infant.';
	} else {
		participants_idetify = 'participants' + row + '.orderParticipant.';
	}
	set_value = element.value;
	if (arguments.length > 3)
		age = formularValidation(11, set_value, 1, arguments[3]);
	else
		age = formularValidation(11, set_value);
	if (age != -1) {
		unset_input_error(element, error_class);
		if (document.getElementById(participants_idetify + 'symType').value == 'ADT') {
			var ret_value = 1;
			// nastaveni veku
			document.getElementById(participants_idetify + 'age').value = age;

			// kontrola zda se jedna o zenu ci muze dle RC a zda je nastaveno
			// odpovidajici osloveni
			if (set_value.length > 6) {
				first_month_num = parseInt(set_value.substr(2, 1));
				if ((first_month_num > 4 && document.getElementById(participants_idetify + 'personTypeString').value == 'D') || (first_month_num < 4 && document.getElementById(participants_idetify + 'personTypeString').value == 'H')) {
					unset_input_error(element, error_class);
					unset_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
					ret_value = 1;
				} else {
					set_input_error(element, error_class);
					set_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
					return 0;
				}
			}

			// kotrola, zda vek dospeleho neni mensi nez 24 nebo vetsi/rovno nez 60 a
			// nastaveni tohoto veku
			if (age < 24 || age >= 60) {
				set_input_error(element, error_class);
				ret_value = 0;
			}
			return ret_value;
		}

		if (document.getElementById(participants_idetify + 'symType').value == 'YTH') {
			// kotrola, zda vek mladeznika neni mensi nez 12 nebo vetsi/rovno nez 24 popripade doli hranice 18 (pokud se jedna o kombinace) a
			// nastaveni tohoto veku
			if (((age < 12 || age >= 24) && combination == 0) || ((age < 18 || age >= 24) && combination == 1)) {
				set_input_error(element, error_class);
				return 0;
			} else {
                                if (age < 25 && age >= 18) {
              			   // kontrola zda se jedna o zenu ci muze dle RC a zda je nastaveno
              			   // odpovidajici osloveni
              			   if (set_value.length > 6) {
              				first_month_num = parseInt(set_value.substr(2, 1));
              				if ((first_month_num > 4 && document.getElementById(participants_idetify + 'personTypeString').value == 'D') || (first_month_num < 4 && document.getElementById(participants_idetify + 'personTypeString').value == 'H')) {
              					unset_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
				                document.getElementById(participants_idetify + 'age').value = age;
				                unset_input_error(element, error_class);
				                return 1;
              				} else {
              					set_input_error(element, error_class);
              					set_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
              					return 0;
              				}
              			      } else {
		                             document.getElementById(participants_idetify + 'age').value = age;
				             unset_input_error(element, error_class);
				             unset_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
				             return 1;
			                     }

                                   } else {
                                          if ((document.getElementById(participants_idetify + 'personTypeString').value == 'D' || document.getElementById(participants_idetify + 'personTypeString').value == 'H')) {
              				     set_input_error(element, error_class);
              				     set_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
          				     return 0;
                                             } else {
                                                    document.getElementById(participants_idetify + 'age').value = age;
              				            unset_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
				                    unset_input_error(element, error_class);
				                    return 1;
                                                    }
                                          }

			}
		}

		if (document.getElementById(participants_idetify + 'symType').value == 'CHD') {
			// kotrola, zda vek ditete neni mensi nez 2 nebo vetsi/rovno nez 12 a
			// nastaveni tohoto veku
			if (age < 2 || age >= 12) {
				set_input_error(element, error_class);
				return 0;
			} else {
				if (combination == 0) document.getElementById(participants_idetify + 'age').value = age;
				unset_input_error(element, error_class);
				return 1;
			}
		}

		if (document.getElementById(participants_idetify + 'symType').value == 'INF') {
			// kotrola, zda vek batolete neni mensi nebo rovno nez 2 a nastaveni tohoto
			// veku
			if (age >= 2) {
				set_input_error(element, error_class);
				return 0;
			} else {
				if (combination == 0) document.getElementById(participants_idetify + 'age').value = age;
				unset_input_error(element, error_class);
				return 1;
			}
		}

		if (document.getElementById(participants_idetify + 'symType').value == 'YCD') {
                        // kotrola, zda vek seniora neni mensi nez 60 a nastaveni tohoto veku
			if (age < 60) {
				set_input_error(element, error_class);
				return 0;
			        }

			// kontrola zda se jedna o zenu ci muze dle RC a zda je nastaveno odpovidajici osloveni
			if (set_value.length > 6) {
				first_month_num = parseInt(set_value.substr(2, 1));
				if ((first_month_num > 4 && document.getElementById(participants_idetify + 'personTypeString').value == 'D') || (first_month_num < 4 && document.getElementById(participants_idetify + 'personTypeString').value == 'H')) {
					unset_input_error(element, error_class);
					unset_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
					document.getElementById(participants_idetify + 'age').value = age;
					return 1;
				} else {
					set_input_error(element, error_class);
					set_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
					return 0;
				}
			    } else {
		                   document.getElementById(participants_idetify + 'age').value = age;
				   unset_input_error(element, error_class);
				   unset_input_error(document.getElementById(participants_idetify + 'personTypeString'), error_class);
				   return 1;
			           }
		}

	} else {
		set_input_error(element, error_class);
		return 0;
	}
}

function check_complete_form(element_name, rows, error_class, date_to) {
	valid = 1;
	if (rows == 0 || row == -1) {
		if ((formularValidation(2, document.getElementById('lastName').value) != 1) || (document.getElementById('lastName').value == document.getElementById('firstName').value)) {
			valid = 0;
			set_input_error(document.getElementById('lastName'), error_class);
		} else
			unset_input_error(document.getElementById('lastName'), error_class);
		if ((formularValidation(2, document.getElementById('firstName').value) != 1) || (document.getElementById('lastName').value == document.getElementById('firstName').value)) {
			valid = 0;
			set_input_error(document.getElementById('firstName'), error_class);
		} else
			unset_input_error(document.getElementById('firstName'), error_class);
		if (check_RC(document.getElementById('persIdentification'), '', error_class, date_to) != 1)
			valid = 0;
	} else {
		for ( var row = 0; row < rows; row++) {
			if (document.getElementById('roomCodes' + row)) {
				if (document.getElementById('roomCodes' + row).value == '') {
					valid = 0;
					set_input_error(document.getElementById('roomCodes' + row), error_class);
				} else
					unset_input_error(document.getElementById('roomCodes' + row), error_class);
			}
			;
			if ((formularValidation(2, document.getElementById('participants' + row + '.orderParticipant.lastName').value) != 1) || (document.getElementById('participants' + row + '.orderParticipant.lastName').value == document.getElementById('participants' + row + '.orderParticipant.firstName').value)) {
				valid = 0;
				set_input_error(document.getElementById('participants' + row + '.orderParticipant.lastName'), error_class);
			} else
				unset_input_error(document.getElementById('participants' + row + '.orderParticipant.lastName'), error_class);
			if ((formularValidation(2, document.getElementById('participants' + row + '.orderParticipant.firstName').value) != 1) || (document.getElementById('participants' + row + '.orderParticipant.lastName').value == document.getElementById('participants' + row + '.orderParticipant.firstName').value)) {
				valid = 0;
				set_input_error(document.getElementById('participants' + row + '.orderParticipant.firstName'), error_class);
			} else
				unset_input_error(document.getElementById('participants' + row + '.orderParticipant.firstName'), error_class);
			if (check_RC(document.getElementById('participants' + row + '.orderParticipant.persIdentification'), row, error_class, date_to) != 1)
				valid = 0;
		}
		;
	}
	;
	if (document.getElementById('roomCodes0')) {
		for ( var room in rooms)
			if (rooms[room].full_beds == 0)
				valid = 0;
	}
	;
	if (valid == 1)
		return 1;
	else {
		error_message();
		return 0;
	}
	;
}

function check_complete_form_Sym(element_name, rows, error_class, date_to) {
        // paty argument je 0 / 1 a ozamuje zda se jedna o kontrolu v kombinaci (1) ... default stav je 0
        if (arguments.length > 5) combination = arguments[5];
      	   else combination = 0;

	valid = 1;
	if (document.getElementById('roomCodes0')) {
	   for ( var irooms in rooms ) {
               if (rooms[irooms].full_beds == 0) valid = 0;
               };
           };
	for ( var row = 0; row < rows; row++) {
		if (!document.getElementById('participants' + row + '.orderParticipant.symType'))
			continue;

			if (document.getElementById('roomCodes' + row)) {
				if (document.getElementById('roomCodes' + row).value == '') {
					valid = 0;
					set_input_error(document.getElementById('roomCodes' + row), error_class);
				} else
					unset_input_error(document.getElementById('roomCodes' + row), error_class);
			}
		
		if ((formularValidation(2, document.getElementById('participants' + row + '.orderParticipant.lastName').value) != 1) || (document.getElementById('participants' + row + '.orderParticipant.lastName').value == document.getElementById('participants' + row + '.orderParticipant.firstName').value)) {
			valid = 0;
			set_input_error(document.getElementById('participants' + row + '.orderParticipant.lastName'), error_class);
		} else
			unset_input_error(document.getElementById('participants' + row + '.orderParticipant.lastName'), error_class);
		if ((formularValidation(2, document.getElementById('participants' + row + '.orderParticipant.firstName').value) != 1) || (document.getElementById('participants' + row + '.orderParticipant.lastName').value == document.getElementById('participants' + row + '.orderParticipant.firstName').value)) {
			valid = 0;
			set_input_error(document.getElementById('participants' + row + '.orderParticipant.firstName'), error_class);
		} else
			unset_input_error(document.getElementById('participants' + row + '.orderParticipant.firstName'), error_class);
		if (check_RC_Sym(document.getElementById('participants' + row + '.orderParticipant.persIdentification'), row, error_class, date_to) != 1,combination)
			valid = 0;

		if (document.getElementById('participants' + row + '.orderParticipant.symType').value == 'INF') {
			if ((formularValidation(2, document.getElementById('participants' + row + '.orderParticipant.infant.lastName').value) != 1) || (document.getElementById('participants' + row + '.orderParticipant.infant.lastName').value == document.getElementById('participants' + row + '.orderParticipant.infant.firstName').value)) {
				valid = 0;
				set_input_error(document.getElementById('participants' + row + '.orderParticipant.infant.lastName'), error_class);
			} else
				unset_input_error(document.getElementById('participants' + row + '.orderParticipant.infant.lastName'), error_class);
			if ((formularValidation(2, document.getElementById('participants' + row + '.orderParticipant.infant.firstName').value) != 1) || (document.getElementById('participants' + row + '.orderParticipant.infant.lastName').value == document.getElementById('participants' + row + '.orderParticipant.infant.firstName').value)) {
				valid = 0;
				set_input_error(document.getElementById('participants' + row + '.orderParticipant.infant.firstName'), error_class);
			} else
				unset_input_error(document.getElementById('participants' + row + '.orderParticipant.infant.firstName'), error_class);
			if (check_RC_Sym(document.getElementById('participants' + row + '.orderParticipant.infant.persIdentification'), row, error_class, date_to) != 1,combination)
				valid = 0;
		}
	}
	if (valid == 1)
		return 1;
	else {
		error_message();
		return 0;
	}
}

function check_gender(element, row, error_class) {
	var participants_idetify;
	if (parseInt(row) == -1)
		participants_idetify = '';
	else
		participants_idetify = 'participants' + row + '.orderParticipant.';
	persIdentification_value = document.getElementById(participants_idetify + 'persIdentification').value;
	age = formularValidation(11, persIdentification_value);
	if (age != -1) {
		if (persIdentification_value.length > 6) {
			first_month_num = parseInt(persIdentification_value.substr(2, 1));
			if ((first_month_num > 4 && element.value == 'D') || (first_month_num < 4 && element.value == 'H')) {
				unset_input_error(element, error_class);
				unset_input_error(document.getElementById(participants_idetify + 'persIdentification'), error_class);
			} else {
				set_input_error(element, error_class);
				classes = document.getElementById(participants_idetify + 'persIdentification').className;
				if (classes.indexOf(error_class) < 0)
					document.getElementById(participants_idetify + 'persIdentification').className = document.getElementById(participants_idetify + 'persIdentification').className + ' ' + error_class;
			}
			;
		}
		;
	} else
		unset_input_error(element, error_class);
}

/* function check_complete_form(element_name, rows, error_class, date_to) {
	valid = 1;
	if (rows == 0 || row == -1) {
		if (formularValidation(2, document.getElementById('lastName').value) != 1) {
			valid = 0;
			set_input_error(document.getElementById('lastName'), error_class);
		} else
			unset_input_error(document.getElementById('lastName'), error_class);
		if (formularValidation(2, document.getElementById('firstName').value) != 1) {
			valid = 0;
			set_input_error(document.getElementById('firstName'), error_class);
		} else
			unset_input_error(document.getElementById('firstName'), error_class);
		if (check_RC(document.getElementById('persIdentification'), '', error_class, date_to) != 1)
			valid = 0;
	} else {
		for ( var row = 0; row < rows; row++) {
			if (document.getElementById('roomCodes' + row)) {
				if (document.getElementById('roomCodes' + row).value == '') {
					valid = 0;
					set_input_error(document.getElementById('roomCodes' + row), error_class);
				} else
					unset_input_error(document.getElementById('roomCodes' + row), error_class);
			}
			;
			if (formularValidation(2, document.getElementById('participants' + row + '.orderParticipant.lastName').value) != 1) {
				valid = 0;
				set_input_error(document.getElementById('participants' + row + '.orderParticipant.lastName'), error_class);
			} else
				unset_input_error(document.getElementById('participants' + row + '.orderParticipant.lastName'), error_class);
			if (formularValidation(2, document.getElementById('participants' + row + '.orderParticipant.firstName').value) != 1) {
				valid = 0;
				set_input_error(document.getElementById('participants' + row + '.orderParticipant.firstName'), error_class);
			} else
				unset_input_error(document.getElementById('participants' + row + '.orderParticipant.firstName'), error_class);
			if (check_RC(document.getElementById('participants' + row + '.orderParticipant.persIdentification'), row, error_class, date_to) != 1)
				valid = 0;
		}
		;
	}
	;
	if (document.getElementById('participants0.orderParticipant.personRoom')) {
		for ( var room in rooms)
			if (rooms[room].adult == 0)
				valid = 0;
	}
	;
	if (valid == 1)
		return 1;
	else {
		error_message();
		return 0;
	}
	;
} */

// ///////////////////////////////
// funkce pro doplnkove sluzby //
// ///////////////////////////////

function fill_services() {
	/*                         */
	/*  zde zmenit id elementu */
	/*                         */
	service_group_id = document.getElementById('sluzby_typy').value;
	sluzby_complete = 'sluzby_complete';

	document.getElementById(sluzby_complete).options.length = 0;
	var position = document.getElementById(sluzby_complete).options.length;
	document.getElementById(sluzby_complete).options.length = document.getElementById(sluzby_complete).options.length + 1;
	document.getElementById(sluzby_complete).options[position].text = vybrat_sluzbu;
	document.getElementById(sluzby_complete).options[position].value = '';
	for ( var service in services) {
		if (services[service].service_group_id == service_group_id || service_group_id == '0') {
			position = document.getElementById(sluzby_complete).options.length;
			document.getElementById(sluzby_complete).options.length = document.getElementById(sluzby_complete).options.length + 1;
			document.getElementById(sluzby_complete).options[position].text = services[service].name;
			document.getElementById(sluzby_complete).options[position].value = services[service].id;
		}
		;
	}
	;
	document.getElementById(sluzby_complete).options[0].defaultSelected = true;
	document.getElementById(sluzby_complete).options[0].selected = true;
}

function enable_disable_select(element) {
	// druhy parametr je -2.. nastavi default hodnotu / -1 .. necha aktualni
	// option / 1 .. nastavi pozadavanou option na selected .. option jsou
	// cislovany vzestupne od 0 podle toho, jak jsou uvedeny v HTML
	// treti parametr urcuje, zda se select povoluje - 1 nebo zakazuje - 0 ...
	// pokud neni urcen, prepina se
	if (arguments.length > 1) {
		if (arguments[1] > -1)
			element.options[ds].selected = true;
		if (arguments[1] == -2)
			for (ds = 0; ds < element.options.length; ds++) {
				if (element.options[ds].defaultSelected == true)
					element.options[ds].selected = true;
			}
		;
	}
	;
	if (arguments.length > 2) {
		if (arguments[2] == 1)
			element.disabled = false;
		else
			element.disabled = true;
	} else {
		if (element.disabled == true)
			element.disabled = false;
		else
			element.disabled = true;
	}
	;
}

function povolit_zakazat(select_status) {
	// select_status urcuje, ktere selecty maji byt vybrany (0 - doporucene
	// sluzby, 1- vsechny sluzby)
	/*                         */
	/* zde zmenit id elementu */
	/*                         */
	document.getElementById("lastForm").value = select_status;
	if (select_status == 0 && document.getElementById('sluzby_doporucene').disabled == true) {
		enable_disable_select(document.getElementById('sluzby_doporucene'), -2);
		enable_disable_select(document.getElementById('sluzby_typy'), -2);
		enable_disable_select(document.getElementById('sluzby_complete'), -2);
	}
	;
	if (select_status == 1 && document.getElementById('sluzby_complete').disabled == true) {
		enable_disable_select(document.getElementById('sluzby_doporucene'), -2);
		enable_disable_select(document.getElementById('sluzby_typy'), -2);
		enable_disable_select(document.getElementById('sluzby_complete'), -2);
		fill_services();
	}
	;
}

function check_uncheck(element) {
	/*                         */
	/* zde zmenit id elementu */
	/*                         */
	form_id = 'services';
	all_checkbox_id = 'all';
	if (element.id == all_checkbox_id) {
		for ( var ca = 0; ca < document.getElementById(form_id).elements.length; ca++) {
			if (document.getElementById(form_id).elements[ca].type == 'checkbox' && document.getElementById(form_id).elements[ca].disabled == false)
				document.getElementById(form_id).elements[ca].checked = document.getElementById(all_checkbox_id).checked;
		}
		;
	} else {
		var are_all_checked = true;
		for ( var ca = 0; ca < document.getElementById(form_id).elements.length; ca++) {
			if (document.getElementById(form_id).elements[ca].type == 'checkbox' && document.getElementById(form_id).elements[ca].id != all_checkbox_id) {
				if (document.getElementById(form_id).elements[ca].checked == false) {
					are_all_checked = false;
					break;
				}
			}
		}
		document.getElementById(all_checkbox_id).checked = are_all_checked;
	}
}

// ////////////////////////////////
// / funkce pouze pro apartmany ///
// ////////////////////////////////

function fill_room_select(element, i, error_class) {
	var element2id = 'participants' + i + '.orderParticipant.personTypeString';
	var element2 = document.getElementById(element2id);

	if (element.value != '' && element2.value != 'B') {
		rooms[element.value].full_beds = parseInt(rooms[element.value].full_beds, 10) - 1;
	}
	;

	element.options.length = 0;
	element.options.length = 1;
	var position = 0;
	element.options[position].text = vybrat_text;
	element.options[position].value = '';
	var item = 0;
	for ( var frs in rooms) {
		item++;
		if (element.value == rooms[frs].id) {
			position = element.options.length;
			element.options.length = element.options.length + 1;
			element.options[position].text = item;
			element.options[position].value = rooms[frs].id;
			element.options[position].selected = true;
		} else if (rooms[frs].full_beds < rooms[frs].beds || element2.value == 'B') {
			position = element.options.length;
			element.options.length = element.options.length + 1;
			element.options[position].value = rooms[frs].id;
			element.options[position].text = item;
		}
		;
	}
	;
}

function rooms_actualization(element, i, error_class) {
        if (element.parentNode.parentNode.lastChild.firstChild == null) infant = element.parentNode.parentNode.lastChild.previousSibling.firstChild.id.indexOf('.infant.');
           else infant = element.parentNode.parentNode.lastChild.firstChild.id.indexOf('.infant.');
	if (parseInt(i) == -1) {
		participants_idetify = '';
	} else if (infant > 0) {
		participants_idetify = 'participants' + i + '.orderParticipant.infant.personTypeString';
	} else {
		participants_idetify = 'participants' + i + '.orderParticipant.personTypeString';
	}
	element2 = document.getElementById(participants_idetify);
	if (element.value != '' && element2.value != 'B') {
		rooms[element.value].full_beds = parseInt(rooms[element.value].full_beds, 10) + 1;
		if (element2.value != 'K')
			parseInt(rooms[element.value].adult, 10) + 1;
		count_selected_beds++;
	}
	;
	if (element.value == '')
		set_input_error(element, error_class);
	else
		unset_input_error(element, error_class);
}

function enable_disable_checkboxes(checkbox_action, form_name) {
	// checkbox_action ... 0 - zakazuje , 1 - povoluje
	if (arguments.length > 3) {
		element_id = arguments[2];
		element_type = arguments[3]; // 0- prislusne checkboxy budou
		// vynechany , 1- prislusne checkboxy
		// budou zakazany
	} else
		element_type = 1;
	for ( var ca = 0; ca < document.getElementById(form_name).elements.length; ca++) {
		if (document.getElementById(form_name).elements[ca].type == 'checkbox') {
			if (arguments.length > 3) {
				found = 0;
				if (document.getElementById(form_name).elements[ca].parentNode.id == element_id)
					found = 1;
				if (document.getElementById(form_name).elements[ca].parentNode.parentNode.id == element_id)
					found = 1;
				if (document.getElementById(form_name).elements[ca].parentNode.parentNode.parentNode.id == element_id)
					found = 1;
				if (document.getElementById(form_name).elements[ca].parentNode.parentNode.parentNode.parentNode.id == element_id)
					found = 1;
				if (document.getElementById(form_name).elements[ca].parentNode.parentNode.parentNode.parentNode.parentNode.id == element_id)
					found = 1;
				if ((found == 0 && element_type == 0) || (found == 1 && element_type == 1)) {
					if (checkbox_action == 0)
						document.getElementById(form_name).elements[ca].disabled = true;
					else
						document.getElementById(form_name).elements[ca].disabled = false;
				}
				;
			} else {
				if (checkbox_action == 0)
					document.getElementById(form_name).elements[ca].disabled = true;
				else
					document.getElementById(form_name).elements[ca].disabled = false;
			}
			;
		}
		;
	}
	;
}

// funkce zkotroluje zda je zaskrtnuty nejaky checkbox ve formulari s ID
// (formID) ... pokud ano, vraci 1, pokud ne, vraci 0
function check_checked_checkboxes(formID) {
	var found = 0;
	for ( var ca = 0; ca < document.getElementById(formID).elements.length; ca++) {
		if (document.getElementById(formID).elements[ca].type == 'checkbox' && document.getElementById(formID).elements[ca].checked == true)
			found = 1;
	}
	;
	return found;
}

function update_checkboxes(form_name, element_id) {
	all_empty = 1;
	for ( var ca = 0; ca < document.getElementById(form_name).elements.length; ca++) {
		if (document.getElementById(form_name).elements[ca].type == 'checkbox') {
			if (document.getElementById(form_name).elements[ca].checked == true)
				all_empty = 0;
		}
		;
	}
	;
	if (all_empty == 0)
		enable_disable_checkboxes(0, form_name, element_id, 0);
	else
		enable_disable_checkboxes(1, form_name);
}

function order_apartmans(start_url, element_id, form_name) {
	end_url = '';
	for ( var ca = 0; ca < document.getElementById(form_name).elements.length; ca++) {
		if (document.getElementById(form_name).elements[ca].type == 'checkbox') {
			found = 0;
			if (document.getElementById(form_name).elements[ca].parentNode.id == element_id)
				found = 1;
			if (document.getElementById(form_name).elements[ca].parentNode.parentNode.id == element_id)
				found = 1;
			if (document.getElementById(form_name).elements[ca].parentNode.parentNode.parentNode.id == element_id)
				found = 1;
			if (document.getElementById(form_name).elements[ca].parentNode.parentNode.parentNode.parentNode.id == element_id)
				found = 1;
			if (document.getElementById(form_name).elements[ca].parentNode.parentNode.parentNode.parentNode.parentNode.id == element_id)
				found = 1;
			if (found == 1 && document.getElementById(form_name).elements[ca].checked == true)
				end_url = end_url + '&unit=' + document.getElementById(form_name).elements[ca].value;
		}
		;
	}
	;
	if (end_url != '') {
		location.href = start_url + end_url;
	}
	;
}

function showMap(url) {
	var left = screen.width / 2 - 225;
	var top = screen.height / 2 - 200;
	var mapWindow = window.open(url, '_blank', 'width=450,height=400,left=' + left + ',top=' + top + 'location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no');
	mapWindow.focus();
}
function showGoogleMap(url) {
	var left = screen.width / 2 - 350;
	var top = screen.height / 2 - 237;
	var mapWindow = window.open(url, '_blank', 'width=700,height=475,left=' + left + ',top=' + top + ',toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0');
	mapWindow.focus();
}

function showDetail(url) {
	var left = screen.width / 2 - 390;
	var top = screen.height / 2 - 237;
	var detailWindow = window.open(url, '_blank', 'width=777,,height=475,left=' + left + ',location=no,menubar=no,resizable=no,scrollbars=yes,status=no,titlebar=no,toolbar=no');
	detailWindow.focus();
}

// ///////////////////////////
// / funkce pouze pro auta ///
// ///////////////////////////

function show_pick_drop_detail(detail_id, element_id, open_time_text, input_array) {
	if (detail_id != '')
		ChangeText(element_id, input_array[detail_id].address + '<br>' + open_time_text + '<br>' + input_array[detail_id].open_times);
	else
		ChangeText(element_id, '');
};

function generate_pick_drop_detail_page(input_array) {
	var myWin = window.open('', 'detail_page', 'scrollbars=yes,resizable=no,width=650,height=330');
	myWin.document.write('<html><head>\n');
	myWin.document.write('<meta http-equiv="content-type" content="text/html; charset=UTF-8" />\n');
	myWin.document.write('<meta http-equiv="Content-Script-Type" content="text/javascript" />\n');
	myWin.document.write('<link rel="stylesheet" href="../css/style.css" type="text/css" />\n');
	myWin.document.write('<script type="text/JavaScript" language="javascript" src="../scripts/controls.js"></script>\n');
	myWin.document.write('</head>\n');
	myWin.document.write('<body>\n');
	myWin.document.write('<table class="car_pick_drop_detail_page">\n');
	for ( var car_location in input_array) {
		myWin.document.write('<tr>\n');
		myWin.document.write('   <td class="name">\n');
		myWin.document.write('   ' + input_array[car_location].location_name + '\n');
		myWin.document.write('   </td>\n');
		myWin.document.write('   <td>\n');
		myWin.document.write('   ' + input_array[car_location].address + '\n');
		myWin.document.write('   </td>\n');
		myWin.document.write('   <td class="time">\n');
		myWin.document.write('   ' + input_array[car_location].open_times + '\n');
		myWin.document.write('   </td>\n');
		myWin.document.write('</tr>\n');
	}
	;
	myWin.document.write("</table>\n");
	myWin.document.write("</body></html>");
	myWin.document.close();
};

function fill_pick_drop_select(element, data_array) {
	element.disabled = false;
	var element_id;
	element.options.length = 0;
	var position = 0;
	if (arguments.length < 2)
		return -1;
	if (arguments.length == 3) {
		sup_id = arguments[2].substr(0, 2);
		for ( var date_record in data_array) {
			if (data_array[date_record].id.substr(0, 2) == sup_id) {
				position = element.options.length;
				element.options.length = element.options.length + 1;
				element.options[position].text = data_array[date_record].location_name;
				element.options[position].value = data_array[date_record].id;
			}
			;
		}
		;
		show_pick_drop_detail(element.value, 'drop_off', opening_hours, locationsTo);
	} else {
		for ( var date_record in data_array) {
			if (arguments.length > 3) {
				// 4 argument - data_array2 ... srovnavaci
				var data_array2 = arguments[3];
				for ( var date_record_2 in data_array2) {
					if (data_array2[date_record_2].id.substr(0, 2) == data_array[date_record].id.substr(0, 2)) {
						position = element.options.length;
						element.options.length = element.options.length + 1;
						element.options[position].text = data_array[date_record].location_name;
						element.options[position].value = data_array[date_record].id;
						break;
					}
					;
				}
				;
			} else {
				position = element.options.length;
				element.options.length = element.options.length + 1;
				element.options[position].text = data_array[date_record].location_name;
				element.options[position].value = data_array[date_record].id;
			}
			;
		}
		;
	}
	;
};

function start_init_pick_drop() {
	fill_pick_drop_select(document.getElementById('from'), locationsFrom, '', locationsTo);
	show_pick_drop_detail(document.getElementById('from').value, 'pick_up', opening_hours, locationsFrom);
	fill_pick_drop_select(document.getElementById('to'), locationsTo, document.getElementById('from').value);
	show_pick_drop_detail(document.getElementById('to').value, 'drop_off', opening_hours, locationsTo);
};

// ///////////////////////////
// / funkce pouze pro menu ///
// ///////////////////////////

addEvent(window, "load", initMenu);

function addEvent(obj, evType, fn) {
	/*
	 * adds an eventListener for browsers which support it Written by Scott
	 * Andrew: nice one, Scott
	 */
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on" + evType, fn);
		return r;
	} else {
		return;
	}
}

var menuType; // click,rolover
menuType = "rolover";

function initMenu() {
	var elmTree;
	if (document.getElementById) {
		elmTree = document.getElementById("mainMenu");
		if (elmTree == null) {
			return;
		}
	} else {
		return;
	}
	searchSubmenus(elmTree);
}

function searchSubmenus(fromNode) {
	var levelItems;
	for ( var i = 0; i < fromNode.childNodes.length; i++) {
		var child = fromNode.childNodes[i];
		var subUl;
		var parA;
		for ( var j = 0; j < child.childNodes.length; j++) {
			var subChild = child.childNodes[j];
			if (subChild.nodeName == "UL") {
				searchSubmenus(subChild);
				subChild.style.display = "none";
				subUl = subChild;
			} else if (subChild.nodeName == "A") {
				parA = subChild;
			}
		}
		if (subUl) {
			aktivate(parA, subUl);
			parA = null;
			subUl = null;
		}
	}
}

function aktivate(a, ul) {
	a.className = "submenuSwitch_off";
	if (menuType == 'click') {
		a.onclick = function() {
			if (ul.style.display == "none") {
				// pokusďż˝me se najďż˝t otevrenďż˝ poloďż˝ky ve stejnďż˝
				// ďż˝rovni
				var tmp = a.parentNode.parentNode.childNodes // vďż˝echny LI
				// naďż˝ďż˝
				// ďż˝rovne
				for ( var i = 0; i < tmp.length; i++) {
					var sub = tmp[i];
					if (sub.nodeName == "LI") {
						for ( var j = 0; j < sub.childNodes.length; j++) {
							var subUl = sub.childNodes[j];
							if (subUl.nodeName == "UL") {
								if (subUl.style.display == "inline") {
									subUl.style.display = "none";
								}
							}
							if (subUl.nodeName == "A" && subUl.className == "submenuSwitch_on") {
								subUl.className = "submenuSwitch_off";
							}
						}
					}
				}
				ul.style.display = "block";
				a.className = "submenuSwitch_on";
			} else {
				ul.style.display = "none";
				a.className = "submenuSwitch_off";
			}
			return false;
		}
	} else if (menuType == 'rolover') {
		var closeTimeout;
		a.onmouseover = function() {
			clearTimeout(a.closeTimeout);
			ul.style.display = "inline";
			a.className = "submenuSwitch_on";
		}
		a.onmouseout = function() {
			a.closeTimeout = setTimeout(a.closeSub, 200);
		}
		a.closeSub = function() {
			ul.style.display = "none";
			a.className = "submenuSwitch_off";
		}
		ul.onmouseover = function() {
			clearTimeout(a.closeTimeout);
		}
		ul.onmouseout = function() {
			a.closeTimeout = setTimeout(a.closeSub, 200);
		}
	}
}

// /////////////////////////
// / funkce pro zamlzeni ///
// /////////////////////////

function hide_show_screen() {
	var mlha = document.getElementById("mlha").style;
	var wait = document.getElementById("waiting").style;
	if (mlha.display == 'block') {
		mlha.display = 'none';
		wait.display = 'none';
	} else {
		wait.left = (document.documentElement.clientWidth / 2 - 100) + 'px';
		wait.top = (document.documentElement.clientHeight / 2 - 50) + 'px';
		mlha.display = 'block';
		wait.display = 'block';
	}
}

///////////////////////////
/// funkce pro pridani / odber classy z prislusneho radku tabulky
///////////////////////////

function bolding(element, class_to_add_delete){
	classes = element.parentNode.parentNode.className;
	class_position = classes.indexOf(class_to_add_delete);
	if (class_position > -1) {
		classes = classes.substring(0, (class_position - 1)) + classes.substring((class_position + class_to_add_delete.length), classes.length);
		element.parentNode.parentNode.className = classes;
            } else element.parentNode.parentNode.className = element.parentNode.parentNode.className + ' ' + class_to_add_delete;
       }
       
// /////////////////////////
// / funkce pro odmlzeni ///
// /////////////////////////
 
function show_screen() {
 document.getElementById("mlha").style.display = 'none';
 document.getElementById("waiting").style.display = 'none'
}



function compare_date(date1) { //kdyz je date1 > date2 pak +pocet rozdilnych dnu; kdyz je date1 = date2 pak 0; kdyz je date1 < date2 pak -pocet rozdilnych dnu
// jestlize neni 2 datum zadano, bere se aktualni datum
//     var date1 = '2008-10-16 00:00:00';
//     var date2 = '2008-11-16 00:00:00';
//  treti parametr je nepovinny a pta se, zda je dany den v tydnu v rozsahu, pricemz to nesmi byt posledni den daneho rozsahu .... pokud ano, pripocitava se k poctu dni hodnota 10 000
//  nedele 0, pondeli 1, ....
    msPerDay = 24 * 60 * 60 * 1000 ; // Pocet milisekund za den

    sys_date_1 = new Date();
    sys_date_1.setFullYear(date1.substr(0, 4));
    sys_date_1.setMonth(parseInt(date1.substr(5, 2)) - 1);
    sys_date_1.setDate(date1.substr(8, 2));
    sys_date_1.setHours(0);
    sys_date_1.setMinutes(0);
    sys_date_1.setSeconds(0);
    sys_date_1.setMilliseconds(0);

    sys_date_2 = new Date();
    if (arguments.length > 1) {
       date2 = arguments[1];
       sys_date_2.setFullYear(date2.substr(0, 4));
       sys_date_2.setMonth(parseInt(date2.substr(5, 2)) - 1);
       sys_date_2.setDate(date2.substr(8, 2));
       };
    sys_date_2.setHours(0);
    sys_date_2.setMinutes(0);
    sys_date_2.setSeconds(0);
    sys_date_2.setMilliseconds(0);

    days = (sys_date_1 - sys_date_2)/msPerDay;

    if (arguments.length > 2) {
       day = arguments[2];
       if (day > -1 && day < 7) {
          day_2 = sys_date_1.getDay();
          day_1 = sys_date_2.getDay();
          if ((day_1 == day) || (days > 6) || (day_1 < day && day_2 > day) || (day_1 < day && day_2 < day_1) || (day_1 > day && day_2 > day)) {
	          days = days + 10000;
          }
       }
    }
    return days;
};

function validate_possible_date(date1, date2) {
// funkce, ktera slouzi pro vypocet kontroly pouzitelnosti vybraneho datumu
//    Min_days = 1;
//    Max_days = 1000;
//    Over_day = 2;
    var days = compare_date(date1, date2, Over_day);
    if (days > 9000) {
       days = days - 10000;
       var dayOver = 1;
       } else dayOver = 0;
        if ((( days >= Min_days && (Min_days != 1 || Over_day ==-1)) || (( dayOver == 1 ) && (days > 0))) && ( days < Max_days )) return 1;
       else return 0;
}

function update_catalog() {
    //
    var item_value = '2008-09-01 00:00:00';
    var first_value = '2008-09-01 00:00:00';
    for (var item=0; item < (document.forms[0].elements.length - 1); item++) {
        if (document.forms[0].elements[item].value > item_value) {
           item_value = document.forms[0].elements[item].value;
           if (document.forms[0].elements[item].checked == true) first_value = item_value;
           } else {
                  item_value = '2108-09-01 00:00:00';
                  if (validate_possible_date(document.forms[0].elements[item].value, first_value) == 1) {
                     document.forms[0].elements[item].disabled = false;
                     } else {
                            document.forms[0].elements[item].checked = false;
                            document.forms[0].elements[item].disabled = true;
                            };
                  };
        };

};

////////////////////////
// Obsluha chyb ajaxu //
////////////////////////

function initialSettings(){
	dwr.engine.setErrorHandler(exceptionHandler);
}

function exceptionHandler(message, ex){
	alert('Nastala chyb při kominukaci, prosím zkuste to později. Děkujeme');
}

function sessionExpired(){
	document.location.href = redirect + '/session-expired.jsp';
}

