/*
 * Tato funkce standardne rozbali menu kategorii, pricemz prohodi display u jednotlivych UL.
 */
function expandMenu(idCategory) {
	var item = document.getElementById('ulForExpand' + idCategory);
	$(item).slideDown(); // tohle je volani jQuery pro rozbaleni divu

	var collapsingLink = document.getElementById('collapsingCategoryLink' + idCategory);
	var expandingLink = document.getElementById('expandingCategoryLink' + idCategory);
	collapsingLink.style.display = "inline";
	expandingLink.style.display = "none";
	
	// ulozi zmenu do cookies - nastavi prislusne ID
	setCookie(("expandedCategoryMenu" + idCategory), idCategory, 2070, 01, 01,'/', null, null);
}

/*
 * Tato funkce standardne zabali menu kategorii, pricemz prohodi display u jednotlivych UL.
 */
function collapseMenu(idCategory) {
	var item = document.getElementById('ulForExpand' + idCategory);
	$(item).slideUp(); // tohle je volani jQuery pro zabaleni divu

	// po zabaleni je treba prenastavit odkaz z collapse na expand
	var collapsingLink = document.getElementById('collapsingCategoryLink' + idCategory);
	var expandingLink = document.getElementById('expandingCategoryLink' + idCategory);
	collapsingLink.style.display = "none";
	expandingLink.style.display = "inline";

	// ulozi zmenu do cookies - vymaze prislusne ID
	setCookie(("expandedCategoryMenu" + idCategory), idCategory, 1970, 01, 01,'/', null, null);
}

/*
 * Tato husta funkce rozbali na zaklade pole ID vsechny polozky ktere maji byt rozbalene (informace je z cookies)
 */
function expandCookieColapsedMenu(expandedIds) {
  
    if(expandedIds != null) {
      for (i = 0; i < expandedIds.length; i++) {
        var collapsingLink = document.getElementById('collapsingCategoryLink' + expandedIds[i]);
	    var expandingLink = document.getElementById('expandingCategoryLink' + expandedIds[i]);
	    var ulSubmenu = document.getElementById('ulForExpand' + expandedIds[i]);
	    if(collapsingLink!=null) {
	      collapsingLink.style.display = "inline";
	    }
	    if(expandingLink!=null) {
	      expandingLink.style.display = "none";
	    }
	    if(ulSubmenu!=null) {
	      ulSubmenu.style.display = "inline";
	    }
      }
    } 
}

function expandCookieColapsedMenu2(expandedCategoryMenu) {
  if(expandedCategoryMenu!=null) {
    for (i = 0; i < expandedCategoryMenu.length; i++) {
      //nastavi se spravne konkretni nekoncovy element
      $("#collapsingCategoryLink"+expandedCategoryMenu[i]).css("display", "inline");
      $("#expandingCategoryLink"+expandedCategoryMenu[i]).css("display", "none");
      $("#ulForExpand"+expandedCategoryMenu[i]).css("display", "inline");
      
      //nastavi se vsechny rodicovske UL na otevrene
      $("#collapsingCategoryLink"+expandedCategoryMenu[i]).parents("ul").css("display", "inline");
      $("#endCategoryLink"+expandedCategoryMenu[i]).parents("ul").css("display", "inline");

      //nastavi jako rozbalene prislusne nadrazene sipky apod.      
      $("#collapsingCategoryLink"+expandedCategoryMenu[i]).parents("li").children("span[id*='collapsingCategoryLink']").css("display", "inline");
      $("#collapsingCategoryLink"+expandedCategoryMenu[i]).parents("li").children("span[id*='expandingCategoryLink']").css("display", "none");
      $("#collapsingCategoryLink"+expandedCategoryMenu[i]).parents("li").children("span[id*='ulForExpand']").css("display", "inline");
      $("#endCategoryLink"+expandedCategoryMenu[i]).parents("li").children("span[id*='collapsingCategoryLink']").css("display", "inline");
      $("#endCategoryLink"+expandedCategoryMenu[i]).parents("li").children("span[id*='expandingCategoryLink']").css("display", "none");
      $("#endCategoryLink"+expandedCategoryMenu[i]).parents("li").children("span[id*='ulForExpand']").css("display", "inline");
    }  
  }
}

function setCategoryMenuAsLast(idCategory) {
	// radeji schova rozbalovaci i zabalovaci odkaz
	var collapsingLink = document.getElementById('collapsingCategoryLink' + idCategory);
	var expandingLink = document.getElementById('expandingCategoryLink' + idCategory);
	var lastCategory = document.getElementById('endCategoryLink' + idCategory);
	collapsingLink.style.display = "none";
	expandingLink.style.display = "none";
	lastCategory.style.display = "inline";
}

function setCookieUntilRelationEnd(name, value) {
	var cookie_string = name + "=" + escape(value);
	// cookie_string += "; expires=";
	document.cookie = cookie_string;
}

function setCookie(name, value, exp_y, exp_m, exp_d, path, domain, secure) {
	var cookie_string = name + "=" + escape(value);

	if (exp_y) {
		var expires = new Date(exp_y, exp_m, exp_d);
		cookie_string += "; expires=" + expires.toGMTString();
	} else {
		var expires = new Date();
		expires.setDate(expires.getDate() + 365);
		cookie_string += "; expires=" + expires.toGMTString();
	}

	if (path) {
		cookie_string += "; path=" + escape(path);
	}

	if (domain) {
		cookie_string += "; domain=" + escape(domain);
	}

	if (secure) {
		cookie_string += "; secure";
	}

	document.cookie = cookie_string;
}

function reloadAttributes(id) {

	window.location = "selectCategorySearch?id=" + id;
}

function destroyCartCookieId() {
	setCookie(("cartCookieId"), "nothing", 1970, 01, 01, null, null, null);
	return true;
}

function showLoginDiv() {
	$("#standardLogin").slideDown();
	$("#forgottenPassword").slideUp();
	$("#loginLinkShift").css("display","inline");
	$("#forgottenPasswordLinkShift").css("display","none");
}

function showForgottenPasswordDiv() {
	$("#forgottenPassword").slideDown();
	$("#standardLogin").slideUp();
	$("#loginLinkShift").css("display","none");
	$("#forgottenPasswordLinkShift").css("display","inline");
}

function showChangePasswordDiv() {
	$("#changePassword").slideDown();
	$("#logoutDiv").slideUp();
	$("#changePasswordLinkShift").css("display","inline");
}


//funkce pro jednoduchou validaci formulare na zmenu hesla, aby se zbytecne nezatezoval server
function isChangePasswordFiled(changePasswordForm) {
	if (changePasswordForm.oldPassword.value == "") {
	    alert("Vyplňte staré heslo!");
	    changePasswordForm.oldPassword.focus();
	    return false;
	}
	if (changePasswordForm.newPassword.value == "") {
	    alert("Vyplňte nové heslo!");
	    changePasswordForm.newPassword.focus();
	    return false;
	}
	if (changePasswordForm.newPassword2.value == "") {
	    alert("Vyplňte nové heslo 2!");
	    changePasswordForm.newPassword2.focus();
	    return false;
	}
	if (changePasswordForm.newPassword.value != changePasswordForm.newPassword2.value) {
	    alert("Nové heslo nebylo správně zopakováno!");
	    changePasswordForm.newPassword2.focus();
	    return false;
	}
	return true;
}


function transmitShoppingCart() {
	var itemIds = new Array();
	var itemValues = new Array();
	var inputs = $("input[name='quantity']");

	for (i = 0; i < inputs.length; i++) {
		var oneItem = inputs[i];
		itemIds[i] = oneItem.id.substring(12);
		itemValues[i] = oneItem.value;
	}

	var idTransportation = dwr.util.getValue("selectTransportation");
	var idPayment = dwr.util.getValue("selectPayment");
	var shoppingCart = {
		cartItemIds :itemIds,
		itemQuantities :itemValues,
		idTransportation :idTransportation,
		idPayment :idPayment
	};
	dwr.engine.beginBatch();
	ShoppingCartDwrController.updateShoppingCart(shoppingCart,
			receiveShoppingCart);
	dwr.engine.endBatch();
}

function receiveShoppingCart(shoppingCart) {
	var itemIds = shoppingCart.cartItemIds;
	var itemPrices = shoppingCart.itemPrices;
	var itemsPrice = shoppingCart.itemsPrice
	var ptPrice = shoppingCart.ptPrice;
	var sumPrice = shoppingCart.sum;
	var sumWeight = shoppingCart.sumWeight;
	var possiblePaymentsIds = shoppingCart.possiblePaymentsIds;
	var possiblePaymentsAbbreviations = shoppingCart.possiblePaymentsAbbreviations;
	var selectedPaymentId = shoppingCart.selectedPaymentId;
	var selectedPaymentAbbreviation = shoppingCart.selectedPaymentAbbreviation;

	if (itemPrices != null && itemIds != null) {
		for (i = 0; i < itemIds.length; i++) {
			dwr.util.setValue("itemPrice" + itemIds[i], itemPrices[i]);
		}
	}

	if (itemsPrice != null) {
		dwr.util.setValue("itemsPrice", itemsPrice);
	} else {
		dwr.util.setValue("itemsPrice", noItemsPrice);
	}

	if (ptPrice != null) {
		dwr.util.setValue("ptPrice", ptPrice);
	} else {
		dwr.util.setValue("ptPrice", noPTPrice);
	}

	if (sumPrice != null) {
		dwr.util.setValue("sumPrice", sumPrice);
	} else {
		dwr.util.setValue("sumPrice", noSumPrice);
	}
		
	if (sumWeight != null) {
		dwr.util.setValue("sumWeight", sumWeight);
	} else {
		dwr.util.setValue("sumWeight", noSumWeight);
	}

	if (possiblePaymentsIds != null && possiblePaymentsAbbreviations != null) {
		$("#selectPayment").removeAttr("disabled");
		$("#selectPayment").empty();

		if (selectedPaymentId != null && selectedPaymentAbbreviation != null) {
			$("#selectPayment").append(
					"<option value=\"" + selectedPaymentId + "\"> "
							+ selectedPaymentAbbreviation + " </option>");
		}

		$("#selectPayment").append("<option value=\"0\"> Neuvedeno </option>");

		if (possiblePaymentsIds.length > 0) {
			for (i = 0; i < possiblePaymentsIds.length; i++) {
				$("#selectPayment").append(
						"<option value=\"" + possiblePaymentsIds[i] + "\"> "
								+ possiblePaymentsAbbreviations[i]
								+ " </option>");
			}
		}
	}

}

function validatePurchaseAddressesForm(form) {
	if(!form.agreeWithBusinessConditions.checked){
		dwr.util.setValue("errorCheckConfirm", "Pro vytvoření objednávky musíte souhlasit s obchodními podmínkami.");
		return false;
	} 
	return true;
}

function validatePurchaseBonus(form) {
	if(form.useBonus == null) {
		// input box s bonusem vubec nebyl zadan, je tedy NULL
		return true;
	}
	if(form.useBonus.value == "0") {
		return true;
	}
	if(form.useBonus.value / form.useBonus.value != 1) {
		dwr.util.setValue("purchaseError", "Špatně zadané číslo, zadejte prosím číselnou hodnotu bonusu, který chcete čerpat.");
		return false;
	}
	
	
	// nasedujici nejak zahadne nefunguje, resp. nekdy ano, nekde ne :-(
	/*
	var ubv = form.useBonus.value;
	var mbv = form.maxBonus.value;
	if(mbv<ubv) {
		dwr.util.setValue("purchaseError", "Nelze čerpat bonus větší než máte k dispozici.");
		return false;
	}
	*/
	
	//pokud je vse OK, vrati se true
	return true;
}


function sumbitSearchDwr(){
	var listsize = document.getElementById("listsize");
	var searchValues = new Array;
	
	searchValues[0] = document.getElementById("category");
	for(i=1; i <listsize; i++){
		searchValues[i] = document.getElementById(i);
}
	searchValues[listsize] = document.getElementById("priceFrom");
	searchValues[listsize+1]= document.getElementById("priceTo");
	dwr.engine.beginBatch();
	
	
	dwr.engine.endBatch();
}

function expandCetelemInfo1() {
	$("#cetelemInfo1").slideDown();
}

function expandCetelemInfo2() {
	$("#cetelemInfo2").slideDown();
}
