dojo.addOnLoad(function() {
    dojo.require("dijit.form.TextBox");
    dojo.require("dijit.form.Button");
	dojo.require("dijit.form.NumberTextBox");
    dojo.require("dojo.parser");
});

function showItemType(itemType, itemLabel) {
    window.open("/shop/cart/type/"+itemType, itemLabel, 
    	"status=0,toolbar=0,resizable=yes,width=640,height=480,scrollbars=1");                                     
}


function add_chapters_to_cart(itemID) {
	form = dijit.byId('product_detailsForm');
	checked = false;
	children = form.getChildren();
	for(i=2; i<children.length; i++) {
		elementValue = children[i].getValue();

		if(elementValue) {
			checked = true;
        }
	}
	if(checked) {
        frm = dojo.byId('product_detailsForm');
        frm.itemID.value = itemID;

        dijit.byId("orderDetailDialog").hide();
        document.getElementById('product_detailsForm').submit();
    }
    else {
		alert("Proszę zaznaczyć co najmniej jeden rozdział");
    }
}

function add_chapters_to_library() {
	form = dijit.byId('product_detailsForm');
	checked = false;
	children = form.getChildren();
	for(i=2; i<children.length; i++) {
		elementValue = children[i].getValue();

		if(elementValue) {
			checked = true;
        }
	}
	if(checked) {
        frm = dojo.byId('product_detailsForm');
        frm.isLibrary.value = "True";

        dijit.byId("orderDetailDialog").hide();
        document.getElementById('product_detailsForm').submit();
    }
    else {
		alert("Proszę zaznaczyć co najmniej jeden rozdział");
    }
}

function add_pages_to_cart(itemID, bookID, from, to)
{	
	if(from === "" || to === "") {
		alert("Strony początkowa i końcowa nie mogą być puste");
		return;
	}
	fromInt = parseInt(from, 10);
	toInt = parseInt(to, 10);

	if(isNaN(fromInt) || isNaN(toInt)){
        alert("Strony początkowa i końcowa muszą być liczbami");
        return;
    }

	if (fromInt < 1) 
	{
		alert("Strona początkowa musi być większa od 0");
		return;
	}
	if (toInt < 1)
    {
        alert("Strona końcowa musi być większa od 0");
        return;
    }
	if(fromInt>toInt){
		alert("Strona końcowa musi być większa od początkowej");
		return;
	}

    loadHtmlWithArgs("/shop/cart/add_pages_to_cart", {
        itemID: itemID,
        bookID: bookID,
        fromInt: fromInt,
        toInt: toInt
    },
    function(warningMessage) {
        if (warningMessage.match("^ERR")) {
            warningMessage = warningMessage.replace("ERR ", "");
            alert(warningMessage);
        }
        else {
            if (warningMessage) {
                alert(warningMessage);
            }
            window.location = "/shop/cart/confirmation?bookID=" + bookID + "&from=" + fromInt + "&to=" + toInt;
            show_cartBlock();
            showStartOfPage();
        }
    });
}


//function add_book_to_cart(bookID)
//{
 //   loadHtml("/shop/cart/add_book_to_cart/" + bookID, function(errorMessage) {
//        if (errorMessage) {
 //           alert(errorMessage);
  //      }
   //     else {
    //        console.log(1);
     //       loadHtmlIntoContentDiv("/shop/cart/confirmation?bookID=" + bookID, "mainArea");
      //      console.log(2);
       //     show_cartBlock();
        //    console.log(3);
         //   showStartOfPage();
          //  console.log(4);
//        }
 //   });
//}


function addProductsAdd() {
	document.getElementById('add_productsForm').submit();
}

function addProductsSkip() {
	window.location.href="/shop/cart/payment_methods";
}


function show_cartBlock() {
    loadHtmlIntoDiv("/shop/cart/block", document.getElementById("cartBlock"));
}

function show_cart() {
    window.location = '/shop/cart/show_cart';
}

function cartDeleteMarkedItems()
{
	loadHtmlWithForm("/shop/cart/delete_marked_items", "cart",
    function(result) {
		window.location.href="/shop/cart/show_cart";
    });
}

function edit_orderSave(frm) {
    if (frm.isValid()) {
        loadHtmlWithForm("/shop/cart/prolong_reservations", frm.name,
        function(errorMessage) {
            // [DCI058] - sprawdzenie rezerwacji
            if (errorMessage) {
                alert(errorMessage);
                window.location.href="/shop/cart/show_cart";
                return;
            }
            loadHtmlWithForm("/shop/cart/recompute_cart", frm.name,
            function(errorMessage) {
                if (errorMessage) {
                    alert(errorMessage);
                    window.location.href="/shop/cart/show_cart";
                    return;
                } 
                else {
                	document.getElementById('cart').submit();
                }
            });
        });
    }
    else {
        alert("Na stronie występują błędy, proszę je poprawić");
    }
}

function edit_deliverySave(frm) {
    if (frm.isValid()) {
    	loadHtmlWithForm("/shop/cart/recompute_cart_with_delivery", frm.name,
    		function(errorMessage) {
            if (errorMessage) {
                alert(errorMessage);
                window.location.href="/shop/cart/payment_methods";
               return;
            }
            dijit.byId('payment_methods').submit();
        });
    }
    else {
        alert("Na stronie sa bledy, prosze poprawic");
    }
}

function edit_deliverySave_zeroPriceOrder(frm) {
    if (frm.isValid()) {
    	loadHtmlWithForm("/shop/cart/recompute_cart_with_delivery", frm.name,
    		function(errorMessage) {
            if (errorMessage) {
                alert(errorMessage);
                window.location.href="/shop/cart/payment_methods";
               return;
            }
            window.location.href="/shop/order/place_order";
        });
    }
    else {
        alert("Na stronie sa bledy, prosze poprawic");
    }
}

function recompute_cart(frm) {
	console.log("recomputeCart");
    if (frm.isValid()) {
        loadHtmlWithForm("/shop/cart/recompute_cart", frm.name,
        function(result) {
        	window.location.href="/shop/cart/show_cart";
        });
    }
    else {
        alert("Na stronie sa bledy, prosze poprawic");
    }
}

function recompute_cartWithDelivery(frm) {
	console.log("recomputeCartWithDelivery");
    document.getElementById('nextButton').disabled = true;
    if (frm.isValid()) {
        loadHtmlWithForm("/shop/cart/recompute_cart_with_delivery", frm.name,
        function(result) {
        	window.location.href="/shop/cart/payment_methods";
        });
    }
    else {
        alert("Na stronie są błędy. Popraw je i spróbuj przeliczyć koszyk ponownie.");
    }
}

function prolong_reservations(frm) {
    loadHtmlWithForm("/shop/cart/prolong_reservations", frm.name,
    function(result) {
        show_cart();
        if (result) {
            alert(result);
        }
    });
}


/*function addUsedToCart(usedProductID) {
    loadHtml("/shop/cart/used?usedProductID=" + usedProductID,
    function(errorMessage) {
        if (errorMessage) {
            alert(errorMessage);
        }
        else {
            loadHtmlIntoContentDiv(
                "/shop/cart/confirmation?usedProductID=" + usedProductID, "mainArea");
            show_cartBlock();
            showStartOfPage();
        }
    });
}*/


function forceLogin() {
    window.location = '/profile/showLoginUserForm/shop/cart/merge_carts';
}

function validateNumber(evt) {
	var theEvent = evt || window.event;
  	var key = theEvent.keyCode || theEvent.which;
  	key = String.fromCharCode( key );
  	var regex = /[0-9]|[\b]/;
  	if( !regex.test(key) ) {
    	theEvent.returnValue = false;
    	theEvent.preventDefault();
  	}
}



