﻿var xmlhttp;

function setShipingZone(value) {
    xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
  		alert ("Browser does not support HTTP Request");
	  	return;
  	}
	var url="price.php";
	url=url+"?id="+value;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=updateShipping;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function updateShipping() {
    resText = xmlhttp.responseText;
    var priceInfo = resText.split('|');
    document.getElementById('price_zone_info').innerHTML = 'Na zvolené mesto <b>'+priceInfo[3]+'</b> pripadá dopravná zóna <b>'+priceInfo[1]+'</b> v hodnote <b>'+priceInfo[2]+' &euro;</b>';
    document.getElementById('price_SHIPPING').value = priceInfo[2];
    document.getElementById('cityName').value = priceInfo[3];
    recountPrice();
}

function recountPrice() {
    defaultPrice = document.getElementById("price_VP").value;
    shippingPrice = document.getElementById("price_SHIPPING").value;
    actualCredit = document.getElementById("ActualCredit").value;
    
        orderPrice = (parseInt(defaultPrice) + parseInt(shippingPrice));
        taxPrice = parseFloat((orderPrice/1.19)*0.19);
        orderTaxPrice = parseFloat(orderPrice-taxPrice);
            if(shippingPrice != '' && defaultPrice != '') {
                if(defaultPrice == -1) {
                    document.getElementById("info_price_box").style.display = "none";
                    document.getElementById("info_deal").style.display = "block";
                } else {
                    if(actualCredit < orderPrice) {
                        document.getElementById("kreditInfoBox").style.display = "block";
                        document.getElementById("kredit").innerHTML = actualCredit;
                        document.getElementById("needed").innerHTML = orderPrice;
                        document.getElementById("orderNow").style.display = "none";
                    } else {
                        document.getElementById("kreditInfoBox").style.display = "none";
                        document.getElementById("orderNow").style.display = "block";
                    }
                    document.getElementById("info_price_box").style.display = "block";
                    document.getElementById("info_deal").style.display = "none";
                }
                document.getElementById("price_WOTAX").value = orderTaxPrice.toFixed(2);
                document.getElementById("price_TAX").value = taxPrice.toFixed(2);
                document.getElementById("price_WTAX").value = orderPrice;
            }
        
    
    // OrderSum/1.19*0.19
}

//districts
function change_district(value) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
  		alert ("Browser does not support HTTP Request");
	  	return;
  	}
	var url="selectbox.php";
	url=url+"?id="+value;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function change_districtRecount(value) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
  		alert ("Browser does not support HTTP Request");
	  	return;
  	}
	var url="selectbox.php";
	url=url+"?id="+value;
	url=url+"&priceRecount=true&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function setServicePrice(value) {
    xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
  		alert ("Browser does not support HTTP Request");
	  	return;
  	}
	var url="price.php";
	url=url+"?service="+value;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=setServPrice;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);    
}

function setServPrice() {
    document.getElementById("price_VP").value=xmlhttp.responseText;
    recountPrice();
}

function stateChanged() {
	if (xmlhttp.readyState==4) {
		document.getElementById("counties").innerHTML=xmlhttp.responseText;
	}
}

//counties
function change_county(value) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
  		alert ("Browser does not support HTTP Request");
	  	return;
  	}
	var url="selectbox.php";
	url=url+"?county="+value;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChangedCities;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function change_countyRecount(value) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
  		alert ("Browser does not support HTTP Request");
	  	return;
  	}
	var url="selectbox.php";
	url=url+"?county="+value;
	url=url+"&priceRecount=true&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChangedCities;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function stateChangedCities() {
	if (xmlhttp.readyState==4) {
		document.getElementById("cities").innerHTML=xmlhttp.responseText;
	}
}

function stateChangedCitiesRecount() {
	if (xmlhttp.readyState==4) {
		document.getElementById("cities").innerHTML=xmlhttp.responseText;
	}
}

function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
  		return new XMLHttpRequest();
 	}
	if (window.ActiveXObject) {
  		// code for IE6, IE5
  		return new ActiveXObject("Microsoft.XMLHTTP");
  	}
	return null;
}
