/* author: FUN Tuning */
// Kontrola, zda se stránka nenachází v rámu
if (top != self)
  /* Pokud ano, načte se do nejvyššího okna (top) */
  top.location.href = self.location.href;

// Otevře nové okno s fotografií (použito v novinkách)  
function FOTO(URL, Name) {
 var NewWin =  window.open(URL, Name, "toolbar=no,width=680,height=500,resizable=1");
}
  
/* funkce, která nepovolí stisknout ENTER při vypisování poznámky */ 
function IsEnter(e) {
  if(!e){
    return (event.keyCode != 13);
  }
  else
  {
    return (e.which != 13);
  }
}

// Kontrola správnosti zadaného e-mailu
function zkontroluj_email(adresa)
{
  re = /^[^.]+(\.[^.]+)*@([^.]+[.])+[a-z]{2,3}$/;
  return adresa.search(re) == 0;
}

// Funkce pro vytvoření nového okna
function NEWwin (url, name, props)
{
  window.open (url, name, props);
}

function showhide(what) {
	var w, text;
	w=what.parentNode;
	if (w.className=="closed") {w.className="open";what.src="./styles/img/navigace_minus.gif";} else {w.className="closed";what.src="./styles/img/navigace_plus.gif";}
	return false;
}


//   This provides convenience methods for working with cookies
//   document: The document object that the cookie is to be associated with
//   name:     Required name
//   path:     Optional - the path to be specified for the cookie
//   domain:   Optional - the domain to be specified for the cookie
//
function Cookie(document, name, path, domain)
{
    // All the predefined properties of this object begin with '$'
    // to distinguish them from other properties which are the values to
    // be stored in the cookie.
    this.$document = document;
    this.$name = name;
    if (path) this.$path = path; else this.$path = null;
    if (domain) this.$domain = domain; else this.$domain = null;
}

// This function is the store() method of the Cookie object.
Cookie.prototype.store = function () {
    // First, loop through the properties of the Cookie object and
    // put together the value of the cookie. Since cookies use the
    // equals sign and semicolons as separators, we'll use colons
    // and ampersands for the individual state variables we store
    // within a single cookie value. Note that we escape the value
    // of each state variable, in case it contains punctuation or other
    // illegal characters.
    var cookieval = "";
    for(var prop in this) {
        // Ignore properties with names that begin with '$' and also methods.
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
            continue;
        if (cookieval != "") cookieval += '&';
        cookieval += prop + ':' + escape(this[prop]);
    }

    // Now that we have the value of the cookie, put together the
    // complete cookie string, which includes the name and the various
    // attributes specified when the Cookie object was created.
    var cookie = this.$name + '=' + cookieval;
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;

    // Now store the cookie in document specified during cookie creation
    this.$document.cookie = cookie;
}

// This function is the load() method of the Cookie object.
Cookie.prototype.load = function() {
    // First, get a list of all cookies that pertain to this document.
    // We do this by reading the magic Document.cookie property.
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;

    // Now extract just the named cookie from that list.
    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false;   // Cookie not defined for this page.
    start += this.$name.length + 1;  // Skip name and equals sign.
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);

    // Now that we've extracted the value of the named cookie, we've
    // got to break that value down into individual state variable
    // names and values. The name/value pairs are separated from each
    // other by ampersands, and the individual names and values are
    // separated from each other by colons. We use the split method
    // to parse everything.
    var a = cookieval.split('&');    // Break it into array of name/value pairs.
    for(var i=0; i < a.length; i++)  // Break each pair into an array.
        a[i] = a[i].split(':');

    for(var i = 0; i < a.length; i++) {
        this[a[i][0]] = unescape(a[i][1]);
    }

    return true;
}

// This function is the remove() method of the Cookie object.
Cookie.prototype.remove = function() {
    var cookie;
    cookie = this.$name + '=';
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

    this.$document.cookie = cookie;
}


function WriteCookie (Name, Value)
{
  // Name je jediný povinný parametr
  if (Name=='') return;
  
  // Spojení názvu a hodnoty cookie
  var Cookie = Name + '=' + escape (Value);
  
  // Nakonec se zapíše výsledný řetězec do vlastnosti cookie
  document.cookie = Cookie;
}

function ReadCookie (Name, DefValue)
{
  // Získá se seznam všech cookies, na které má tato stránka práva
  var Cookies = document.cookie;
  
  // Pokud je výsledek prázdný, vrátí se výchozí hodnota
  if (Cookies == "") return (Defvalue);
  
  // Najde se cookie podle názvu
  var Start = Cookies.indexOf (Name+'=');
  
  // Pokud nebyl nalezen, vrátí se výchozí hodnota
  if (Start == -1) return DefValue;
  
  // Start je pozice, kde se v řetězci nachází začátek hodnoty cookie
  Start += Name.length + 1;
  
  // End je pozice, kde se v řetězci nachází konec hodnoty cookie
  var End = Cookies.indexOf(';', Start);
  if (End == -1) End = Cookies.length;
  
  // Nakonec se z řetěžce vysekne hodnota cookie
  return (unescape (Cookies.substring(Start, End)));
}


 // Přidání zboží do košíku
function addtocart (parent, ID, price, act, lang)
{
 var text_confirm = new Array ("Přejete si přejít do pokladny?", "Do You wish to go to Your shopping cart?");
 if (act == "delete")
 {	
   CARTrows = 0;
   CARTparent = "";
   CARTid = "";
   CARTcount = "";
   CARTtotalprice = 0;
   CARTtotalcount = 0;

   ChangeCartInfo();
 }
 else
 {	   
  CARTrows = ReadCookie('CARTrows', 0);
  CARTparent = ReadCookie('CARTparent', '');
  CARTid = ReadCookie('CARTid', '');
  CARTcount = ReadCookie('CARTcount', '');
  CARTtotalcount = ReadCookie('CARTtotalcount', 0);
  CARTtotalprice = ReadCookie('CARTtotalprice', 0);
  count = eval(document.getElementById('count_'+parent+'_'+ID).value);
  
  aCARTparent = CARTparent.split(";");
  aCARTid = CARTid.split(";");
  aCARTcount = CARTcount.split(";");
  CARTcount = "";
  
  found = false;
  if (CARTrows>0)
  {
    for (i=0;i<CARTrows;i++)
    { 
	  if ((parent == aCARTparent[i]) && (ID == aCARTid[i]))
	  {
	    if (act == "add")
		{
		  aCARTcount[i] = eval(aCARTcount[i]) + count;
		  CARTtotalcount = eval(CARTtotalcount) + count; 
		  CARTtotalprice = eval(CARTtotalprice) + (count * price);
		}  
		else if (act == "refresh")
		{
		  CARTtotalcount = eval(CARTtotalcount) - eval(aCARTcount[i]); 
		  CARTtotalprice = eval(CARTtotalprice) - (eval(aCARTcount[i]) * price);
		  aCARTcount[i] = count;
		  CARTtotalcount = eval(CARTtotalcount) + eval(aCARTcount[i]); 
		  CARTtotalprice = eval(CARTtotalprice) + (eval(aCARTcount[i]) * price);
		}
		found = true;  
	  }
	  CARTcount += aCARTcount[i]+";" 
    }
  }
  if ((!found)&&(act=="add"))
  {
	CARTrows ++;
    CARTparent += parent+";";
    CARTid += ID+";";
    CARTcount += count+";";
	CARTtotalprice = eval(CARTtotalprice) + (count * price);
	CARTtotalcount = eval(CARTtotalcount) + count;
  }
  
  aCARTcount = "";
  aCARTcount = CARTcount.split(";");
 }
  
 WriteCookie('CARTrows', CARTrows);
 WriteCookie('CARTparent', CARTparent);
 WriteCookie('CARTid', CARTid);
 WriteCookie('CARTcount', CARTcount);
 WriteCookie('CARTtotalprice', CARTtotalprice);
 WriteCookie('CARTtotalcount', CARTtotalcount);
 
 ChangeCartInfo();
  
 if (act == "add")
 {
    if (confirm(text_confirm[lang]))
	{
      document.location.href = "index.php?page=cart&lang="+lang;
	}
 }
 else
    document.location.href = "index.php?page=cart&lang="+lang;
}

/* funkce pro kontrolu délky vepisovaného textu do poznámky u objednávky */
function TextLengthValidator_Objednavka (MaxLength){

 /* Pokud je zapsaný text příliš dlouhý, ořízne se metodou substring na příslušnou délku (MaxLength) */
 if (document.order_form.poznamka_zakaznika.value.length > MaxLength)
   document.order_form.poznamka_zakaznika.value =
     document.order_form.poznamka_zakaznika.value.substring (0, MaxLength);

 /* vypočte se a zobrazí počet znaků, které lze ještě napsat */
 document.order_form.edRemaining.value = MaxLength - document.order_form.poznamka_zakaznika.value.length;

 /* zobrazí se počet napsaných znaků */
 document.order_form.edTotal.value = document.order_form.poznamka_zakaznika.value.length;
}

function ChangeCartInfo() 
{
  if (document.all) 
  {
    document.all['top_cart_count'].innerHTML = ReadCookie('CARTtotalcount', 0);
	document.all['top_cart_price'].innerHTML = ReadCookie('CARTtotalprice', 0);
	document.all['top_cart_priceeu'].innerHTML = parseInt(ReadCookie('CARTtotalprice', 0)/KURZ_EU);
  } else if (document.layers) 
  {
    with (document['top_cart_count'].document) {
	  open();
	  write(ReadCookie('CARTtotalcount', 0));
	  close();
	}
    with (document['top_cart_price'].document) {
	  open();
	  write(ReadCookie('CARTtotalprice', 0));
	  close();
	}
    with (document['top_cart_priceeu'].document) {
	  open();
	  write(ReadCookie('CARTtotalprice', 0)/KURZ_EU);
	  close();
	}
  }	
}

// Přednačtení obrázků do cashe (obrázky použité v menu)
function PreLoad ()
{
  ImArray = new Array(PreLoad.arguments.length);
  
  for (var i=0; i<PreLoad.arguments.length; i++){
    ImArray[i] = new Image();
	ImArray[i].src = "./menu_top/" + PreLoad.arguments[i] + ".gif";
  }
}

// Zvýraznění obrázku v e-shopu při události OnMouseOver
function makevisible(cur,which){
strength=(which==0)? 1 : 0.8;
if (cur.style.MozOpacity)
cur.style.MozOpacity=strength;
else if (cur.filters)
cur.filters.alpha.opacity=strength*100;
}
