/**
 * get document element identified by ID
 *
 * @ param string e "element id"
 */ 
function getElement(e){
	if(document.all) return document.all[e];
	else return document.getElementById(e);
}


/**
 * add item to shopping cart by getting ammount of pieces and redirectiong page
 *
 * @param int id "article Id"
 */
function AddCartItem(id,url){
	var error = false;

	var ks = getElement("aci"+id).value;
	if(ks=="") error = "Nebyl zadán počet kusů zboží.";
	if(ks<1) error = "Počet kusů zboží přidávaných do košíku musí být nejméně 1.";

	if(error!=false){
		alert(error);
	}else{
		url += "&pridat_zbozi="+id+"&pocet="+ks;
		window.parent.window.location.href = url;
	}
}


/**
 * display javascript confirm dialogue. if confirmed, go to specified URL, else return false
 *
 * @param string question
 * @param string URL
 */
function ask(question,URL){
	if(confirm(question)==true){
		window.parent.window.location.href = URL;
	}else{
		return false;
	}
}


/**
 * 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("#");
			getElement("cena_doprava").innerHTML = data_array[0];
			getElement("cena_platba").innerHTML = data_array[1];
			getElement("cena_celkem").innerHTML = data_array[2];
			getElement("cena_dph").innerHTML = data_array[3];
			getElement("cena_celkem_dph").innerHTML = data_array[4];
		}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_client_data()
 *
 * show file label, fill it with temporary content and call XMLHttpRequest
 *
 * @param int Id "file Id"
 */
function get_prices(cena_celkem,cena_dph,cena_celkem_dph){
	getElement("cena_doprava").innerHTML = "<em>načítám...</em>";
	getElement("cena_platba").innerHTML = "<em>načítám...</em>";
	getElement("cena_celkem").innerHTML = "<em>načítám...</em>";
	getElement("cena_dph").innerHTML = "<em>načítám...</em>";
	getElement("cena_celkem_dph").innerHTML = "<em>načítám...</em>";

	var doprava = getElement("doprava").value;
	var platba = getElement("platba").value;
	createRequest("inc/get_prices.inc.php?doprava="+doprava+"&platba="+platba+"&cena_celkem="+cena_celkem+"&cena_dph="+cena_dph+"&cena_celkem_dph="+cena_celkem_dph);
}


/**
 * 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);
}

