function frmCheckType() {
    if (document.forms.platnosci_payment.pay_type.value === "") {
        alert("Wybierz sposób płatności");
        return false;
    }
    return true;
}

function add_donation(orderId, foundationId) {

	var donationTxt = document.getElementById(foundationId + '_donation').value.replace(",", ".")
	var donation = parseFloat(donationTxt);
	
	donation = Math.round(donation*Math.pow(10,2))/Math.pow(10,2);

	if (isNaN(donation) || donation <= 0) {
		alert("Kwota datku musi być poprawną liczbą dodatnią,\nz dokładnością do dwóch miejsc po przecinku.");
        return;
	}
	
	if (donation > 0) {
		var payTypeElem = document.getElementById("payTypeChbox");
		if (payTypeElem) {
    		payTypeElem.style.visibility = "visible";
    	}
    	//document.getElementById("pay_button").disabled = false;
   		//document.getElementById("finalize_button").disabled = true;   	
	}
	
	var donationValues = donationTxt.split(".");
	url = "/shop/order/place_order/add_donation/" + orderId + "/" + foundationId + "/" + donationValues[0];
	if (donationValues.length > 1) {
		url += "/";
		url += donationValues[1];
	}
		
	loadHtmlIntoDiv(url, document.getElementById("donations_list"));
}

function remove_donation(orderId, foundationId, donationPrice, grossValueWithDonations) {
	if (donationPrice == grossValueWithDonations) {
		var payTypeElem = document.getElementById("payTypeChbox");
		if (payTypeElem) {
    		payTypeElem.style.visibility = "hidden";
    	}
    	//document.getElementById("pay_button").disabled = true;
   		//document.getElementById("finalize_button").disabled = false;   	
    }
	
	loadHtmlIntoDiv(
        "/shop/order/place_order/remove_donation/" + orderId + "/" + foundationId,
        document.getElementById("donations_list"));
}

function order() {
	console.log("order");
	document.getElementById('order_form').submit();
}

function pay() {
	if (frmCheckType()) {
		console.log("pay");
		document.getElementById('platnosci_payment').submit();
	}
}

function finalize(orderId) {
	window.location = "/shop/order/finalize_order/" + orderId;
}

function print_transfer_form() {
    var fromAccount = document.forms.transferform.fromAccount;
    if (document.forms.transferform.paymentOrTransfer[1].checked && fromAccount.value.length != 26) {
        //if (!validateIBAN(document.forms.transferform.fromAccount.value)) {
        alert("Niepoprawny numer konta zleceniodawny.");
        return;
        //}
    }
    document.forms.transferform.submit();
}

function update_from_account_field() {
    if (document.forms.transferform.paymentOrTransfer[0].checked) {
        document.getElementById("fromAccountField").style.visibility = "hidden";
    }
    else {
        document.getElementById("fromAccountField").style.visibility = "visible";
    }
}

function registerUser(address_id){
	window.open("/profile/register/"+address_id,"Załóż konto", "status=0,toolbar=0, resizable=yes, width=1000, height=640, scrollbars=1");
}

function saveUserDefaultData(order_id){
	window.open("/profile/data_edit/"+order_id,"Zapisz dane jako domyślne", "status=0,toolbar=0, resizable=yes, width=1000, height=640, scrollbars=1");
}

