/**
 * function unit_price()
 *
 * change prices in article details according to selected article unit. also change name of ujit in cart-adding
 * form and value of unit_id element. then get optional discount for selected unit and display it
 */
function unit_price() {
	var prices_array = document.getElementById("jednotka").value.split("#");

	if (document.getElementById("jednotka_nazev")) document.getElementById("jednotka_nazev").innerHTML = prices_array[0];
	if (document.getElementById("jednotka_id")) document.getElementById("jednotka_id").value = prices_array[1];
	if (document.getElementById("cena")) document.getElementById("cena").innerHTML = prices_array[2] + " Kč";
	if (document.getElementById("dph")) document.getElementById("dph").innerHTML = prices_array[3] + " Kč";
	if (document.getElementById("cena_dph")) document.getElementById("cena_dph").innerHTML = prices_array[4] + " Kč";
	if (document.getElementById("celkem_cena_dph")) document.getElementById("celkem_cena_dph").innerHTML = prices_array[5] + " Kč";

	if (prices_array[6]) {
		if (document.getElementById("sleva_text")) document.getElementById("sleva_text").innerHTML = prices_array[6] + "<br /><span>(tato sleva není do výše uvedené ceny započítána)</span>";
		if (document.getElementById("sleva_text")) document.getElementById("sleva_text").style.display = "block";
	} else {
		if (document.getElementById("sleva_text")) document.getElementById("sleva_text").style.display = "none";
	}

	if (document.getElementById("code")) document.getElementById("code").innerHTML = prices_array[7];
	if (document.getElementById("availability")) document.getElementById("availability").innerHTML = prices_array[9] == 0 ? prices_array[8] : "skladem (množství: " + prices_array[9] + ")";
}


/**
 * function katalog_add_to_cart()
 *
 * add article into shopping cart directly from catalogue
 *
 * @param int zbozi_id "article Id"
 * @param int jednotka_id "article unit Id"
 */
function katalog_add_to_cart(zbozi_id,jednotka_id){
	document.getElementById("zbozi_id").value = zbozi_id;
	document.getElementById("jednotka_id").value = jednotka_id;
	document.getElementById("katalog_form").submit();
}


/**
 * function kosik_edit()
 *
 * update ammount and unit of article in cart by adjusting some form variables and submitting the form
 *
 * @param int kosik_id "article Id"
 */
function kosik_edit(kosik_id){
	document.getElementById("kosik_id").value = kosik_id;
	document.getElementById("pocet_jednotek").value = document.getElementById("pocet_jednotek_"+kosik_id).value;
	document.getElementById("jednotka_id").value = document.getElementById("jednotka_id_"+kosik_id).value;
	document.getElementById("akce").value = "upravit";
	document.getElementById("form_akce").submit();
}


/**
 * function kosik_remove()
 *
 * remove one item from shopping cart by adjusting some form variables and submitting the form
 *
 * @param string zbozi_nazev "article name"
 * @param int kosik_id "article Id"
 */
function kosik_remove(zbozi_nazev,kosik_id){
	if(confirm("Odebrat zboží \""+zbozi_nazev+"\" z košíku?")==true){
		document.getElementById("kosik_id").value = kosik_id;
		document.getElementById("akce").value = "smazat";
		document.getElementById("form_akce").submit();
	}
}


/**
 * function kosik_truncate()
 *
 * truncate shopping cart completely
 */
function kosik_truncate(kosik_id){
	if(confirm("Odebrat všechno zboží z košíku?")==true){
		document.getElementById("akce").value = "vyprazdnit";
		document.getElementById("form_akce").submit();
	}
}


/**
 * function createRequest()
 *
 * create new XMLHttpRequest
 *
 * @param string url "requested file url"
 */
function createRequest(url) {
	if (window.XMLHttpRequest) {
		httpRequest = new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}

	httpRequest.open("GET", url, true);
	httpRequest.onreadystatechange = function () { processRequest(); };
	httpRequest.send(null);
}


/**
 * function processRequest()
 *
 * process XMLHttpRequest, write plain text data into file label
 *
 * no params needed, inner function of createRequest
 */
function processRequest() {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			var data_string = httpRequest.responseText;
			var data_array = data_string.split("#");
			document.getElementById("cena_doprava").innerHTML = data_array[0];
			document.getElementById("cena_platba").innerHTML = data_array[1];
			document.getElementById("cena").innerHTML = data_array[2];
			document.getElementById("dph").innerHTML = data_array[3];
			document.getElementById("cena_dph").innerHTML = data_array[4];
			document.getElementById("hidden_cena").value = data_array[2];
			document.getElementById("hidden_dph").value = data_array[3];
			document.getElementById("hidden_cena_dph").value = data_array[4];

			/**
			 * if user can have free transport, alert him about. else hide alert box
			 */
			if (data_array[5] != "") {
				document.getElementById("free_price_alert").innerHTML = "Pokud nakoupíte zboží za dalších <strong>" + data_array[5] + ",- Kč</strong>, bude Vám dopraveno pomocí zvoleného způsobu dopravy zdarma!";
				document.getElementById("free_price_alert").style.display = "block";
			} else {
				document.getElementById("free_price_alert").innerHTML = "";
				document.getElementById("free_price_alert").style.display = "none";
			}

		/**
		 * oops, error
		 */
		} else {
			alert("Při načítání informací se vyskytla chyba.\n\nStiskněte F5 pro znovunačtení stránky a poté\nznovu vyberte způsob dopravy zboží a způsob\nplatby objednávky.");
		}
	}
}


/**
 * function get_prices()
 *
 * get payment and transport values and call XMLHttpRequest
 *
 * @param int Id "file Id"
 */
function get_prices(cena,dph,cena_dph){
	var doprava = document.getElementById("doprava").value;
	var platba = document.getElementById("platba").value;
	createRequest("inc/get_prices.inc.php?doprava=" + doprava + "&platba=" + platba + "&cena=" + cena + "&dph=" + dph + "&cena_dph=" + cena_dph);
}


/**
 * function cart_options_selected()
 *
 * check if transport and payment type is selected. if not, display error and return false
 *
 * no params needed :-)
 */
function cart_options_selected() {
	if (document.getElementById("doprava").value == 0) {
		alert("Vyberte typ dopravy zboží.");
		return false;
	} else if(document.getElementById("platba").value == 0) {
		alert("Vyberte typ platby.");
		return false;
	} else {
		return true;
	}
}


/**
 * function param_detail()
 *
 * open new window with param details, center this window
 *
 * @param string url "window URL"
 */
function param_detail(url) {

	/**
	 * append variable to URL, thi will tell us to display close link in window
	 */
	url = url + "close/";

	/**
	 * count window's position
	 */
	var pos_x = (screen.width - 600) / 2;
	var pos_y = (screen.height - 400) / 2;

	/**
	 * open window and move it to position
	 */
	w = window.open(url, false, "width=600,height=400,toolbar=no,location=no,menubar=no,resizable=no");
	w.moveTo(pos_x, pos_y);
}


/**
 * function get_payment_types()
 *
 * @param int transport_id "desired transport type ID"
 *
 * request available payment types after transport type is chosen, append
 * selectbox options
 */
function get_payment_types(transport_id) {

	/**
	 * create XMLHttpRequest and open connection
	 */
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xhr.open("GET", "/inc/get_payment_types.inc.php?transport_id=" + transport_id, true);

	/**
	 * when connection status changes...
	 */
	xhr.onreadystatechange = function () {

		/**
		 * loading data...
		 */
		if (xhr.readyState==4) {

			/**
			 * reply 200 - OK
			 */
			if (xhr.status == 200) {

				/**
				 * remove all childs from payment type select
				 */
				var s = document.getElementById("platba");
				while (s.hasChildNodes()) s.removeChild(s.lastChild);

				/**
				 * option "chose" should always be visible
				 */
				var o = document.createElement("option");
				o.value = "0";
				o.innerHTML = "--- vyberte ---";
				s.appendChild(o);

				/**
				 * continue only if response is not empty
				 */
				if (xhr.responseText != "") {

					/**
					 * split payment types
					 */
					var payment_types = xhr.responseText.split("#")

					/**
					 * go through array of payment types
					 */
					for (var i in payment_types) {

						/**
						 * payment type must be string
						 */
						if (typeof(payment_types[i]) == "string") {

							/**
							 * split payment type ID and name
							 */
							var pt = payment_types[i].split("|");

							/**
							 * create and append option
							 */
							var o = document.createElement("option");
							o.value = pt[0];
							o.innerHTML = pt[1];
							s.appendChild(o);
						}
					}
				}

			/**
			 * any aother reply status than 200 means error, alert it
			 */
			} else {
				alert("Při načítání způsobů plateb se vyskytla chyba.\n\nKontaktujte prosím správce obchodu.");
			}
		}
	};

	xhr.send(null);
}

