
/* javascript */
/*********************
//* jQuery Multi Level CSS Menu #2- By Dynamic Drive: http://www.dynamicdrive.com/
//* Last update: Nov 7th, 08': Limit # of queued animations to minmize animation stuttering
//* Menu avaiable at DD CSS Library: http://www.dynamicdrive.com/style/
*********************/

//Specify full URL to down and right arrow images (23 is padding-right to add to top level LIs with drop downs):
var arrowimages={down:['downarrowclass', 'down.gif', 23], right:['rightarrowclass', 'right.gif']}

var jqueryslidemenu={

animateduration: {over: 200, out: 100}, //duration of slide in/ out animation, in milliseconds

buildmenu:function(menuid, arrowsvar){
	jQuery(document).ready(function($){
		var $mainmenu=$("#"+menuid+">ul")
		var $headers=$mainmenu.find("ul").parent()
		$headers.each(function(i){
			var $curobj=$(this)
			var $subul=$(this).find('ul:eq(0)')
			this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
			this.istopheader=$curobj.parents("ul").length==1? true : false
			$subul.css({top:this.istopheader? this._dimensions.h+"px" : 0})

			$curobj.hover(
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					this._offsets={left:$(this).offset().left, top:$(this).offset().top}
					var menuleft=this.istopheader? 0 : this._dimensions.w
					menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
					if ($targetul.queue().length<=1) //if 1 or less queued animations
						$targetul.css({left:menuleft+"px", width:this._dimensions.subulw+'px'}).slideDown(jqueryslidemenu.animateduration.over)
				},
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					$targetul.slideUp(jqueryslidemenu.animateduration.out)
				}
			) //end hover
		}) //end $headers.each()
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
	}) //end document.ready
}
}

//build menu with ID="mySlideTopMenuItem" on page:
jqueryslidemenu.buildmenu("Nav2", arrowimages)



/*
 * Tato funkce se aktualne nepouziva - jejim cilem bylo ulozit do cookies IDecka vsech nadrazenych UL v menu kategorii.
function setParentCookies(idEndMenuElement){
    alert("Funkce na ulozeni do cookies vsech UL predku.");
    var parentEls = $("#endMenuElemement" + idEndMenuElement).parents("ul");
  
    for (i = 0; i < parentEls.length; i++) {
      var idParentUl = parentEls[i].id;
      if(idParentUl != null && idParentUl != '') {
        //alert(idParentUl);
        idParentUl = idParentUl.substring(11);
        //alert(idParentUl);
        setCookie(("expandedCategoryMenu" + idCategory), idCategory, 2070, 01, 01,'/', null, null);
      }
    }
    alert('Vse probehlo bez chyby JS.');
}
*/  
  
function createMaderczMenu() {
  //nasledujici zverstvo da mezi repasy a kategorii lcd(ma id=1410) hnusnou caru
  if(idForMaderLine && idForMaderLine != null) {
	  var ulIdForMaderLine = "#ulForExpand" + idForMaderLine;
	  $(ulIdForMaderLine).parent().prepend('<div class\=\"txtCenter\"><img alt=\"oddělení kategorií\" src=\"/img/menuSeparator.png\"/></div>');
  }

  //nasledujici sracka by mela nahradit odkaz na konfigurator XLS. Pozn.: V jQuery se selektor na element s ul elementem, ktery vevnitr musi obsahovat DIV s vnitrnim textem '...'
  $("ul div:contains('Konfigurátor počítačů')").replaceWith('<div><a href=\"/configurator\">Konfigurátor počítačů</a></div>');
}  

/*
 * 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";
	      }
      }
    } 
}

/**
 * Tato funkce rozbali pomoci jQuery vsechny kategorie menu i s rodicema a nastavi spravny vzhled sipek
 */
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();
}

