// JavaScript Document
//global variables that can be used by ALL the function son this page.
var inputs;
var divs = new Array();
var imgFalse = http_localhost + '/images/checkbox.jpg';
var imgTrue = http_localhost + '/images/checkbox_check.jpg';
var imgFalse_disabled = http_localhost + '/images/checkbox_disabled.jpg';
var imgTrue_disabled = http_localhost + '/images/checkbox_check_disabled.jpg';

function newImage(arg) {
	rslt = new Image();
	rslt.src = arg;
	return rslt;
}

imgF = newImage(imgFalse);
imgT = newImage(imgTrue);
imgFD = newImage(imgFalse_disabled);
imgTD = newImage(imgTrue_disabled);

// this function runs when the page is loaded, put all your other onload stuff
// in here too.
function init() {
	replaceChecks();
	inp = document.getElementsByTagName('input');
	for (i = 0; i < inp.length; i++) {
		if (inp[i].type == "text" || inp[i].type == "password") {
			inp[i].onfocus = new Function('focus()');
			if (!(inp[i].name == "ic" || inp[i].name == "login_name")){
				inp[i].onblur = new Function('blur()');
			}
		}
	}

	inp = document.getElementsByTagName('textarea');
	for (i = 0; i < inp.length; i++) {
		inp[i].onfocus = new Function('focus()');
		inp[i].onblur = new Function('blur()');
	}
	
	document.getElementById("left_form_search").setAttribute('onSubmit', 'return checkFormSearch();'); //kontrola na fultext hledani
}

function replaceChecks() {
	bool_group = false;
	// get all the input fields on the page
	inputs = document.getElementsByTagName('input');
	div = document.getElementsByTagName('div');

	j = 0;
	for ( var i = 0; i < div.length; i++) {
		if ((div[i].id).substr(0, 6) == 'uroven')
			divs[j++] = div[i];
	}

	// cycle trough the input fields
	for ( var i = 0; i < inputs.length; i++) {

		// check if the input is a checkbox
		if (inputs[i].getAttribute('type') == 'checkbox') {

			// create a new image
			var img = document.createElement('img');

			// check if the checkbox is checked
			if (inputs[i].checked) {
				if (inputs[i].disabled)
					img.src = imgTrue_disabled;
				else
					img.src = imgTrue;
			} else {
				if (inputs[i].disabled)
					img.src = imgFalse_disabled;
				else
					img.src = imgFalse;
			}
			img.align = "top";

			// set image ID and onclick action
			img.id = 'checkImage' + i;
			// set image
			img.onclick = new Function('checkChange(' + i + ')');
			// place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);

			// hide the checkbox
			inputs[i].style.display = 'none';

			if ((inputs[i].id).substr(0, 5) == 'group' && inputs[i].checked) // groupovani
			// po
			// radku
			// klepnu
			// na
			// jeden
			// ostatni
			// se z
			// neaktivni
			{
				bool_group = true;
			}
		}
	}

	// byl odeslan formular a je treba osetrit radkovani...
	if (bool_group)
		for ( var i = 0; i < inputs.length; i++) {
			if (inputs[i].getAttribute('type') != 'checkbox')
				continue;
			if ((inputs[i].id).substr(0, 5) == 'group' && inputs[i].checked) // groupovani
			// po
			// radku
			// klepnu
			// na
			// jeden
			// ostatni
			// se z
			// neaktivni
			{
				selected_id = (inputs[i].id).substr(5, ((inputs[i].id)
						.lastIndexOf("_") - 5));
				for ( var j = 0; j < inputs.length; j++) {
					if (inputs[j].getAttribute('type') != 'checkbox')
						continue;
					if ((inputs[j].id).substr(0, 5) != 'group')
						continue;

					number = (inputs[j].id).substr(5, ((inputs[j].id)
							.lastIndexOf("_") - 5));

					if (number == selected_id) {
						if (inputs[j].id != inputs[i].id) {
							inputs[j].disabled = true;
							document.getElementById('checkImage' + j).src = imgFalse_disabled;
						}
					}
				}
			}
		}
}

// change the checkbox status and the replacement image
function checkChange(i) {
	if ((inputs[i].id).substr(0, 8) == 'checkbox') {
		if (inputs[i].checked && !inputs[i].disabled) {
			souhlas = confirm("Pokud zrušíte tuto kategorii zruší se výběr i v podkategoriích této kategorie. Chete přesto tuto kategorii odstranit?");
			if (souhlas) {
				num = (inputs[i].id).substr(8);
				for ( var j = 0; j < inputs.length; j++) {
					num1 = (inputs[j].id).substr(8, num.length);
					if (num1 == num) {

						if (inputs[j].checked) {
							if (((inputs[j].id).substr(8)).length == 6) {
								document.getElementById("count_2_kat").value = (parseInt(document
										.getElementById("count_2_kat").value)) - 1;
								var urls = "../action/insert_firm/set_count_cat.php?action=minus";
								var rq = new RAYNET.HTTPRequest();
								rq.setMethod("post");
								rq.setFormat("txt");
								rq.setMode("async");
								rq.send(urls, window, "_callBack");

								if ((parseInt(document
										.getElementById("count_2_kat").value)) > 0) {
									price = parseInt(document
											.getElementById("counter").value);
									
									new_price = (parseInt(price))- parseInt(price_kategorie);
									if (new_price < 0)
										new_price = 0;
									document.getElementById("counter").value = new_price; 
								}
							}
							var urls = "../action/insert_firm/unset_session.php?id_parent="
									+ inputs[j].value;
							var rq = new RAYNET.HTTPRequest();
							rq.setMethod("post");
							rq.setFormat("txt");
							rq.setMode("async");
							rq.send(urls, window, "_callBack");
							inputs[j].checked = '';
							document.getElementById('checkImage' + j).src = imgFalse;
						}
					}
				}
				
				if (num.length == 6)
				{
					is_check = false;
					elm = null;
					temp = num.substr(0,3);
					for ( var j = 0; j < inputs.length; j++) 
					{
						if (inputs[j].id == 'checkbox'+temp)
							elm = j;
						if (inputs[j].id.substr(8,3) == temp && (inputs[j].id.substr(8)).length == 6)
						{
							//2uriven projiti...
							if (inputs[j].checked)
							{
								is_check = true;
								break;
							}
						}
					}
					
					if (!is_check)
					{
						var urls = "../action/insert_firm/unset_session.php?id_parent="
							+ inputs[elm].value;
						var rq = new RAYNET.HTTPRequest();
						rq.setMethod("post");
						rq.setFormat("txt");
						rq.setMode("async");
						rq.send(urls, window, "_callBack");
						inputs[elm].checked = false;
						document.getElementById('checkImage' + elm).src = imgFalse;
					}
				}
				
			}
		} else if (!inputs[i].checked) {
			// for(var j=0; j < divs.length; j++) {
			// divs[j].style.display = 'none';
			// }
			// document.getElementById('uroven'+inputs[i].value).style.display =
			// 'block';
			num = (inputs[i].id).substr(8);
			if (num.length == 3) {
				menuClick(num, true);
			} else if (num.length == 6) {
				inputs[i].checked = 'checked';
				document.getElementById('checkImage' + i).src = imgTrue;
				for ( var j = 0; j < inputs.length; j++) {
					if (inputs[j] == document.getElementById("checkbox"
							+ (num.substr(0, 3)))) {
						if (!inputs[j].checked) {
							var urls = "../action/insert_firm/set_session.php?id="
									+ inputs[j].value;
							var rq = new RAYNET.HTTPRequest();
							rq.setMethod("post");
							rq.setFormat("txt");
							rq.setMode("async");
							rq.send(urls, window, "_callBack");
						}
						document.getElementById("checkbox" + (num.substr(0, 3))).checked = true;
						document.getElementById("checkImage" + j).src = imgTrue;
					}
				}

				document.getElementById("count_2_kat").value = (parseInt(document.getElementById("count_2_kat").value)) + 1;
				var urls = "../action/insert_firm/set_count_cat.php?action=plus";
				var rq = new RAYNET.HTTPRequest();
				rq.setMethod("post");
				rq.setFormat("txt");
				rq.setMode("async");
				rq.send(urls, window, "_callBack");

				if ((parseInt(document.getElementById("count_2_kat").value)) > 1) {
					price = parseInt(document.getElementById("counter").value);
					new_price = (parseInt(price))+ parseInt(price_kategorie);
					if (new_price < 0)
						new_price = 0;
					document.getElementById("counter").value = new_price;
				}
				GB_showCenter("Výběr podkategorií",
						"../content/insert_firmy/podkategorie.php?id="
								+ inputs[i].value, 500, 800);
				var urls = "../action/insert_firm/set_session.php?id="
					+ inputs[i].value;
				var rq = new RAYNET.HTTPRequest();
				rq.setMethod("post");
				rq.setFormat("txt");
				rq.setMode("async");
				rq.send(urls, window, "_callBack");
			}
		}
	} else if ((inputs[i].id).substr(0, 5) == 'group') // groupovani po radku
	// klepnu na jeden
	// ostatni se z
	// neaktivni
	{
		selected_id = (inputs[i].id).substr(5,
				((inputs[i].id).lastIndexOf("_") - 5));
		for ( var j = 0; j < inputs.length; j++) {
			if (inputs[j].getAttribute('type') != 'checkbox')
				continue;
			if ((inputs[j].id).substr(0, 5) != 'group')
				continue;

			number = (inputs[j].id).substr(5,
					((inputs[j].id).lastIndexOf("_") - 5));

			if (number == selected_id) {

				if (!inputs[i].checked) {
					if (inputs[j].id != inputs[i].id) {
						inputs[j].disabled = true;
						inputs[j].checked = false;
						document.getElementById('checkImage' + j).src = imgFalse_disabled;
					}
				} else {
					if (inputs[j].id != inputs[i].id) {
						inputs[j].disabled = false;
						document.getElementById('checkImage' + j).src = imgFalse;
					}
				}
			}
		}
		if (!inputs[i].checked) {
			inputs[i].checked = true;
			inputs[i].disabled = false;
			document.getElementById('checkImage' + i).src = imgTrue;
		} else {
			inputs[i].checked = false;
			document.getElementById('checkImage' + i).src = imgFalse;
		}
	} else if (!inputs[i].disabled) {
		if (inputs[i].id == "reg_to_finders") {
			type_reg_to_finders = reg_to_finders(type_reg_to_finders,
					price_reg_to_finders);
		}
		if (inputs[i].checked) {
			inputs[i].checked = '';
			document.getElementById('checkImage' + i).src = imgFalse;
		} else {
			inputs[i].checked = 'checked';
			document.getElementById('checkImage' + i).src = imgTrue;
		}
	}
}

function _callBack(txt, status) {
	if (status != 200 || txt != "OK") {
		alert("Chyba");
		// this.location.reload(true);
	}
}
