
function getStawka(state){
	if(document.getElementById("export").checked == true){
	$('fromEX').style.display = "inline";
	$('toEX').style.display = "inline";
	$('from').style.display = "none";
	$('to').style.display = "none";
	$('from_idEX').value = $('fromEX').selectedIndex;
    country = $('fromEX').options[$('fromEX').selectedIndex].value;
    city = $('fromEX').options[$('fromEX').selectedIndex].text;
	plik = '../../destinationsEX.php';
	} else {
	$('fromEX').style.display = "none";
	$('toEX').style.display = "none";
	$('from').style.display = "inline";
	$('to').style.display = "inline";
	$('from_id').value = $('from').selectedIndex;
    country = $('from').options[$('from').selectedIndex].value;
    city = $('from').options[$('from').selectedIndex].text;	
	plik = '../../destinations.php';
	}
    if ($('hidden_15')) {
        $('hidden_15').value = city;
    }
    var myAjax = new Ajax(plik, {
        onRequest: function(){
            $('indicator').style.display = "inline"
        },
        onComplete: function(response){
            stawka = response;
            if ($('hidden_13')) {
                $('hidden_13').value = stawka
            }
            updateKoszt()
        },
        onFailure: function(){
            $('indicator').style.display = "none"
        }
    });
    myAjax.options.data = "country=" + country + "&state=1&city=" + city;
    myAjax.request();
}

function updateKoszt(){
    waga = $('waga').value / 1000;
    if (isNaN(waga) || waga < 1) {
        waga = 1;
    }
    objetosc = $('objetosc').value.replace(/,/gi, '.');
    if (objetosc < 1) {
        objetosc = 1;
    }
    if (waga >= objetosc) {
        w_m = waga;
    }
    else {
        w_m = objetosc;
    }
    stawka_updated = stawka * w_m;
    stawka_updated = Math.round(stawka_updated);
    $('stawka_parse').innerHTML = stawka_updated;

    $('indicator').style.display = "none";
    validator();
}

function validator(errors){
    error = "-----";
    stawka_error = 0;
    stop = 0;
	wagaaa = $('waga').value;

    if (isNaN(wagaaa) || wagaaa == "" || wagaaa == "0") {
       
	    if (errors == 1) {
            $('waga').value = "wpisz";
            $('waga').style.color = "red"
        }
        stop = 1;
        stawka_error = 1
    }
    if (isNaN(objetosc) || objetosc == "" || objetosc == "0") {
        if (errors == 1) {
            $('objetosc').value = "wpisz";
            $('objetosc').style.color = "red"
        }
        stop = 1;
        stawka_error = 1
    }
    if (stawka_error == 1) {
        $('stawka_parse').innerHTML = error
    }
    if (stawka_error == 1 && $('koszt_parse')) {
        $('koszt_parse').innerHTML = error
    }
    if (stop == 1) {
        return false;
    }
    if (stop == 1) {
        return false;
    }
}

//validator();
function init(){
    getStawka(1);
   
    //koszt_pln();
}

