if (document.all) document.getElementById = document.all;
var dhtml = (document.getElementById || document.all) && navigator.userAgent.indexOf('Opera') == -1 ? true : false;

var LastTimerId = 0;

function getE(objectID) {
  return document.getElementById(objectID);
}


/**
 * Zmena typu inputu
 *
 * @author MP
 * @deprecated Type nelze menit v IE > 7. Je treba pouzit parentNode.replaceChild nebo neco jineho
 *  pridani udalosti pres http://www.intellisoft.cz/Clanky/Jak-dynamicky-pridat-HTML-prvku-obsluhu-udalosti.aspx
 */
function check_heslo(){
    element = document.getElementById("customer_pass");

    if ( element.value == "") {
        element.value = "Heslo";
        element.type = "text";

    }
    else if ( element.value == "Heslo" ) {
        element.type = "password";
        element.value = "";
    }
}


function switch_sort(param, order) {
  document.getElementById('param_'+param+'_order').value = order;

  document.getElementById('paramico_'+param+'_up').src = "/look/paramicon-up.gif";
  document.getElementById('paramico_'+param+'_same').src = "/look/paramicon-same.gif";
  document.getElementById('paramico_'+param+'_down').src = "/look/paramicon-down.gif";

  document.getElementById('paramico_'+param+'_'+order).src = "/look/paramicon-"+order+"-a.gif";
}

function getCookie( name ) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;

    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
      return null;
    }

    if ( start == -1 ) return null;

    var end = document.cookie.indexOf( ';', len );

    if ( end == -1 ) end = document.cookie.length;

    return unescape( document.cookie.substring( len, end ) );
}

function nastav_cookie(){

    if (getCookie('ukaz_parametr')) {
        document.cookie = "ukaz_parametr=";
        nastav_div();
    }
    else {
        var expDate = new Date();
        expDate.setTime( expDate.getTime() + 365*24*60*60*1000 );
        document.cookie = "ukaz_parametr=ano;"+ "expires=" + expDate.toGMTString();
        nastav_div();
    }
}

function nastav_div(){

    if (getCookie('ukaz_parametr')) {
        document.getElementById('box-parametru').style.display="block";
        document.getElementById('param-ico').className="close-box";
        document.getElementById('odkaz').innerHTML = "<a href=\"javascript: nastav_cookie();\" class=\"a_right\">sbalit parametry</a>";
    }
    else {
        document.getElementById('box-parametru').style.display="none";
        document.getElementById('param-ico').className="open-box";
        document.getElementById('odkaz').innerHTML = "<a href=\"javascript: nastav_cookie();\" class=\"a_right\">rozbalit parametry</a>";
    }
}



function priceToStr(intCislo) {
  strCislo = intCislo.toString().replace(".",",")
  carka = strCislo.length-1 - strCislo.toString().search(",")

  if (carka==0) {
    strCislo = strCislo+',00';
  } else if (carka == 1 && intCislo!="0") {
    strCislo = strCislo+'0';
  }
  strLength = strCislo.length;
  if (strLength > 6) { // cislo vetsi nez tisic
    tisice = strCislo.substr(0, strLength-6);
  	ostatni = strCislo.substr(strLength-6);
  	strCislo = tisice+' '+ostatni;
  }

  return strCislo;
}

function element_show(elmId) {
  objElm = document.getElementById(elmId);
  if (objElm) { objElm.style.display = 'block'; }
}

function element_hide(elmId) {
//  window.clearTimeout(LastTimerId);

  objElm = document.getElementById(elmId);
  if (objElm) { objElm.style.display = 'none'; }
}

function timed_element_show(elmId) {
  LastTimerId = window.setTimeout("element_show('"+elmId+"')",500);
}

function search_test() {
  if (((document.form_search.search_kod.value.length == 0) && (document.form_search.search_text.value.length == 0)) || ((document.form_search.search_kod.value == "Kód...") && (document.form_search.search_text.value == "Hledané zboží..."))) {
	alert('Zadejte prosím Hledané zboží nebo Kód!');
	return false;
  }

  var x = document.form_search.search_kod.value.length;
  document.form_search.search_kod.value = document.form_search.search_kod.value.replace("*","");
  document.form_search.search_kod.value = document.form_search.search_kod.value.replace("?","");
  document.form_search.search_kod.value = document.form_search.search_kod.value.replace("%","");

  if (document.form_search.search_kod.value.length < x) {
	alert('Hledání nepodporuje hvězdičkovou konvenci. \r\nAutomaticky jsou však vyhledány položky s kódem začínajícím zadaným textem.');
  }

  if ((document.form_search.search_kod.value.length < 3) && (document.form_search.search_kod.value.length > 0)) {
	alert('Do pole Kód zadejte prosím nejméně tři znaky!');
	return false;
  }

  var y = document.form_search.search_text.value.length;
  document.form_search.search_text.value = document.form_search.search_text.value.replace("*","");
  document.form_search.search_text.value = document.form_search.search_text.value.replace("?","");
  document.form_search.search_text.value = document.form_search.search_text.value.replace("%","");
  if (document.form_search.search_text.value.length < y) {
  	alert('Hledání nepodporuje hvězdičkovou konvenci. \r\nAutomaticky jsou však vyhledány položky s názvem začínajícím zadaným textem.');
  }

  if ((document.form_search.search_text.value.length < 3) && (document.form_search.search_text.value.length > 0)) {
  	alert('Do pole Hledané zboží zadejte prosím nejméně tři znaky!');
  	return false;
  }

  if ((document.form_search.search_kod.value.length > 0) || (document.form_search.search_text.value.length > 0)) {
	return true;
  }
}

function fill_span(e) {
  var titul = "";
  var dic = "";
  var ico = "";

  if (e=='zak_jmeno' || e=='zak_titul' || e=='zak_prijmeni') {
    if (getE('zak_titul').value!="") { titul = getE('zak_titul').value + " "; }
    getE('szak_jmeno').innerHTML = titul + getE('zak_jmeno').value + " " + getE('zak_prijmeni').value;
  } else if (e=='zak_ico' || e=='zak_dic') {
    if (getE('zak_dic').value!="") { dic = " DIČ: " + getE('zak_dic').value; }
    if (getE('zak_ico').value!="") { ico = "IČ: " + getE('zak_ico').value; }
    getE('szak_icodic').innerHTML = ico + dic;
  } else if (e=='zak_do_jmeno' || e=='zak_do_ulice' || e=='zak_do_mesto' || e=='zak_do_psc') {
    getE('szak_dorucovaci_adresa').innerHTML = getE('zak_do_jmeno').value + ", " + getE('zak_do_ulice').value + ", " + getE('zak_do_psc').value + " " + getE('zak_do_mesto').value;
  } else {
    getE('s' + e).innerHTML =  getE(e).value;
  }
}

/* ----------------- */

function emailControl(f) {
  if ((f=='' || f=='Váš email..') ||(f.indexOf('@') < 1 || f.indexOf('@') != f.lastIndexOf('@') || f.lastIndexOf('.') < f.lastIndexOf('@')+2  || f.lastIndexOf('.') > (f.length-3) || f.lastIndexOf('.') < (f.length-4)) || f.indexOf(" ") >= 0  ||  f.indexOf("#") >= 0  || f.indexOf("/") >=0 || f.indexOf(":") >=0 || f.indexOf(";") >=0 || f.indexOf("(") >=0 || f.indexOf(")") >=0 || f.indexOf("[") >=0 || f.indexOf("]") >=0 || f.indexOf("'") >=0)	{
	return false;
  }
  return true;
}

function ValidateEmail(theinput) {
  s = theinput.valueů
  if (s.search) {
	if (s.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)) {
  	  return false;
	} else {
  	  return true;
	}
  }
  if (s.indexOf) {
	at_character=s.indexOf('@')
	if (at_character<=0 || at_character+4>s.length) {
	  return false
	}
  }
  if (s.length<6) {
	return false
  } else {
	return true
  }
}

function numbersonly(myfield, e, dec) {
var key;
var keychar;

if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);

if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;
else if ((("0123456789").indexOf(keychar) > -1))
return true;
else if (dec && (keychar == "."))
{
myfield.form.elements[dec].focus();
return false;
}
else
return false;
}

function showFirm(objChb, whatKind) {
  if (getE(objChb).checked)	{
    getE(whatKind).style.display = "block";
  } else {
    getE(whatKind).style.display = "none";
  }
}

function showAdr(objChb, whatKind) {
  if (getE(objChb).checked)	{
    getE(whatKind).style.display = "none";
  } else {
    getE(whatKind).style.display = "block";
  }
}

function add_favorite()
{
  var addUrl = 'http://www.nej-ceny.cz/';
  var addTitle = 'Nejlepší ceny na internetu - NEJCENY.cz';

  if (document.all && !window.opera)
  {
    window.external.AddFavorite(addUrl,addTitle);
    return false;
  }
  else if (window.opera && window.print)
  {
    return true;
  }
  else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function'))
  {
    if (window.confirm('Přidat do oblíbených?'))
    {
      window.sidebar.addPanel(addTitle,addUrl,'');
      return false;
    }
  }
  window.alert('Pro přidání do oblíbených stikněte CTRL+D (CTRL+T v Opeře).');
  return false;
}

function show_hidelayers(element, action) {
	var obj;
	obj = document.getElementById(element);
    if (obj.style)
    {
    	obj=obj.style;
    	v=obj.display;
    	v=(action=='show')?'block':(action=='hide')?'none':v;
   		obj.display=v;
    }
}

curFold = 'Desc';

function swap_fold(foldName) {
  show_hidelayers('t' + curFold, 'hide');

  show_hidelayers('t' + foldName, 'show');

  curFold = foldName;
}

/**
 * Pridani vyhledavaciho modulu do Firefoxu
 */
function addSearchEngine() {
  if ((typeof window.sidebar == "object") &&
      (typeof window.sidebar.addSearchEngine == "function")) {

    window.sidebar.addSearchEngine(
      "http://www.nej-ceny.cz/pluginy/nejceny.src",
      "http://www.nej-ceny.cz/pluginy/nejceny.gif",
      "Vyhledávání v internetovém obchodu NEJCENY.cz",
      "NEJCENY.cz",
      "Shop");

  } else
    alert("Váš prohlížeč nepodporuje tuto funkci. Zkuste Mozillu (Firefox).");

}

function new_window(url, width, height, scroll) {
  if (scroll == 0) {
	scrollbar = 'no';
  } else {
	scrollbar = 'yes';
  }

  var top = (screen.availHeight-height)/2;
  var left = (screen.availWidth-width)/2;

  goatwin = window.open(url, '', "status=0,height="+height+",width="+width+",top="+top+",left="+left+",location=0,resizeable=0,fullscreen=0, scrollbars="+scrollbar,true);

  return goatwin;
}

function number_format(number, length, sep, th_sep) {

  number = Math.round( number * Math.pow(10, length) ) / Math.pow(10, length);
  str_number = number+"";
  arr_int = str_number.split(" ");
  if(!arr_int[0]) arr_int[0] = "0";
  if(!arr_int[1]) arr_int[1] = "";
  if(arr_int[1].length < length){
    nachkomma = arr_int[1];
    for(i=arr_int[1].length+1; i <= length; i++){  nachkomma += "0";  }
    arr_int[1] = nachkomma;
  }
  if(th_sep != "" && arr_int[0].length > 3){
    Begriff = arr_int[0];
    arr_int[0] = "";
    for(j = 3; j < Begriff.length ; j+=3){
      Extrakt = Begriff.slice(Begriff.length - j, Begriff.length - j + 3);
      arr_int[0] = th_sep + Extrakt +  arr_int[0] + "";
    }
    str_first = Begriff.substr(0, (Begriff.length % 3 == 0)?3:(Begriff.length % 3));
    arr_int[0] = str_first + arr_int[0];
  }
  return arr_int[0]+sep+arr_int[1];
}

function dostupnost(vyr_id) {
  new_window('/scripts/dostupnost.php?vyr_id='+vyr_id, 400, 400, 1);
}

/**
 * Formatuje cislo
 *
 * @param mixed num Castka
 * @param string prefix NP
 * @param string postfix NP
 * @param integer count_d NP pocet desetinnych
 * @param string desetine NP oddelovac
 * @param string tisice NP oddelovac
 * @return string zformatovany vystup
 *
 * NP neni povinny
 */
function format_currency(num, prefix, postfix, count_d, desetine, tisice) {
    if (typeof(prefix)   == 'undefined') {prefix    = '';}
    else if (prefix != '') {prefix = prefix+'&nbsp;'}
    if (typeof(postfix)  == 'undefined') {postfix   = 'Kč';}
    if (typeof(count_d)  == 'undefined') {count_d   = 0;}
    if (typeof(desetine) == 'undefined') {desetine  = ',';}
    if (typeof(tisice)   == 'undefined') {tisice    = '&nbsp;';}

    num = num.toFixed(count_d) + '';
    x = num.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? desetine + x[1] : '';

    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + tisice + '$2');
    }

    return prefix + x1 + x2 + '&nbsp;' + postfix;
}

/**
 * Varianty PRO - nacteni dalsiho selectu
 */
function variants_pro_loads(actual_select) {
    var varianty    = '';
    var cislo       = 0;
    var id          = 0;
    var name_new    = '';

    cislo = parseInt(actual_select.name.substring(8,9));
    id =  $("input[name='item']").val();
    for (i=cislo; i > -1; i--) {
        varianty += $('#variants' + i).val();
        if (i != 0) {
            varianty += ',';
        }
    }

    name_new = 'variants'+(cislo+1);
    $.get('/pages/page/detail/varianty_ajax.php?id='+id+'&varianty='+varianty, function(data){
        $('#'+name_new).attr('disabled',false).html(data);

    });
    return true;
}

/**
 * Varianty PRO - po posledni zmene presmeruje na
 */
function variants_pro_go2url(actual_select) {
    var varianty = '';
    cislo = parseInt(actual_select.name.substring(8,9));
    id =  $("input[name='item']").val();
    for (i=cislo; i > -1; i--) {
        varianty += $('#variants' + i).val();
        if (i != 0) {
            varianty += ',';
        }
    }

    name_new = 'variants'+(cislo+1);
    $.get('/pages/page/detail/varianty_ajax.php?id='+id+'&varianty='+varianty+'&end=1', function(data){
        if (data) {
            location.href = data;
        }
    });
    return true;
}

/**
 * Ajaxovy vypis kategorii
 */
function category_ajax_select(vlastnik) {
    id_kat = vlastnik.value;
    if (id_kat == -1) {
        return true;
    }
    
    do {
        mazat = false;
        k=$(vlastnik).parent().parent();
        // alert($(k)[0].tagName);
    
        l=$(k).next();
        // alert(l.html());
        if(l.html()) {
            $(l).remove();
            mazat = true;
        }
    }
    while(mazat==true);
    
    $.get('/pages/panel/category_filtr_ajax/category_filtr_ajax.php?id_kat='+id_kat, function(data){
        if (data.substring(0,7) == 'http://') {
            location.href = data;   
        } else {
            //$('#category_ajax').append(data);
            $(vlastnik).parent().parent().parent().append(data);
        }   
    });
    return true;
}


