function getSpeedyDelivery() {
	var free_delivery = document.getElementById('delivery[0][free_delivery]');
	var weight = document.getElementById('delivery[0][weight]');
	var volume = document.getElementById('delivery[0][volume]');
	var type = document.getElementById('delivery[0][type]');
	
	if (free_delivery) {
		free_delivery = free_delivery.value;
	} else free_delivery = 0;
	
	if (weight) {
		weight = weight.value;
	} else weight = 0;
	
	if (!weight || free_delivery=='1') {
		var e = document.getElementById('SpeedyPrice');
		if (e) e.innerHTML = '0.00';
		var basket_total = document.getElementById('basket_total');
		var grand_total = document.getElementById('grand_total');
			
		if (basket_total) {
			var fmt_grand_total = isNumber('999,999.99', Number(basket_total.value));
			if (fmt_grand_total) {
				if (grand_total) grand_total.innerHTML = fmt_grand_total[0];
			}
		}
		return;
	}
	
	if (volume) {
		volume = volume.value;
		if (!volume) volume = 0;
	} else volume = 0;

	if (type) {
		type = type.options[type.selectedIndex].value;
	} else type = 3;

	var	amount = document.getElementById('delivery[0][leasing_amount]').value;

	Ajax.loadXMLDoc(
		setURLQuery('index.php',
			'page', 'speedy', 
			'weight', weight,
			'volume', volume,
			'type', type,
			'price', amount,
			'FCID', flowControl.ID
		),
		function (xml) {
			var price = Ajax.getData(xml,'SpeedyPrice');
			if (!price) return;
				price = Number(price) * DELIVERY_DISCOUNT;
			var paymentMethod = document.getElementById('delivery[0][payment_method]');
			if (paymentMethod) paymentMethod = paymentMethod.options[paymentMethod.selectedIndex].value;
			var	amount = document.getElementById('delivery[0][leasing_amount]').value;
			var	fix_hour = document.getElementById('delivery[0][delivery_fix_hour]').checked;
			var weight = document.getElementById('delivery[0][weight]').value;

			var delivery_price = Math.round((paymentMethod == 'в брой при доставка (наложен платеж)' ? (Number(price) + Number(amount) * 0.007) : Number(price)) * 100)/100; 
				delivery_price += (paymentMethod == 'разсрочено плащане' ? 1.8 : 0.0);
				delivery_price += 
					fix_hour ? (
						weight < 5 ? 
							2.40 : (
								weight <= 20 ? 
									6.00 : (
										weight <= 100 ? 
											12.00 : 
											36
									)
							) 
						)
						: 0.00;
				delivery_price = Math.round(delivery_price * 100)/100;

			var fmt_price = isNumber('999,999.99', delivery_price);
			if (fmt_price) delivery_price = fmt_price[0];

			var e = document.getElementById('SpeedyPrice');
			if (e) e.innerHTML = delivery_price;

			var e = document.getElementById('delivery[0][price]');
			if (e) e.value = fmt_price[1];
			
			var basket_total = document.getElementById('basket_total');
			var grand_total = document.getElementById('grand_total');
			
			if (basket_total) {
				var fmt_grand_total = isNumber('999,999.99', Number(basket_total.value) + fmt_price[1]);
				if (fmt_grand_total) {
					if (grand_total) grand_total.innerHTML = fmt_grand_total[0];
				}
			}
				
		}
	);

}

/* ============================================================================================================ */
/*													AJAX														*/
/* ============================================================================================================ */

var Ajax = {
	async : true,
	url : null,
	
	loadXMLDoc : function (url, func, post) {
		if (window.XMLHttpRequest) {
			request = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if (request) {
			this.url = url;
			url += (url.indexOf('?') < 0 ? '?' : '&') +'rnd='+Math.random();
			request.onreadystatechange = this.processReqChange;
			
			if (typeof(func) != 'undefined') {
				this.processResponce = func;
			}
			
			if (typeof(post) != 'undefined') {
				request.open("POST", url, this.async);
				request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
				request.send(post);
			} else {
				request.open("GET", url, this.async);
				request.send(null);
			}
		}  else alert('Can\'t create HTTP Request Object!');
	},
	
	getData : function (xml, element) {
		var e = xml.getElementsByTagName(element);
		if (e.length > 0 && e[0].hasChildNodes()) return e[0].firstChild.data;
	},
	
	processReqChange : function () {
		if (request.readyState == 4) {
			if (request.status == 200) {
				if (request.responseXML) {
					if (Ajax.async) Ajax.processResponce(request.responseXML);
				} else if (request.responseText) {
					Ajax.processResponce(null);
					//alert('AJAX: Text response received!');
				}
			} else {
				alert("There was a problem retrieving the XML data:\n" + request.statusText);
			}
		}
	},
	
	processResponce : function (xml) {
		alert('OOPS: processResponce() have to be revritten');
	}
}
