/* -----------------------------------------------------------------------------
    @AUTHOR:  Zdenek Benak, zdenek.benak@centrum.cz
    @YEAR:    2006
    @PROJECT: MAppEngine
    @FILE:    inc/m_string.php
    @DESCR:   funkce pro praci s retezci
 * -------------------------------------------------------------------------- */

// ---------------------- prevede nepovolene znaky v URL -----------------------

  function UrlEncode(str)
  {
    var hex_tab = "0123456789ABCDEF", result = "", ascii_tab = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ";
    for (var i=0; i<str.length; i++)
    {
      var chr = str.charAt(i), ord = ascii_tab.indexOf(chr)+32;
      if (ord==32) result += "+";
      else if  (((ord>47)&&(ord<58))||((ord>64)&&(ord<91))||((ord>95)&&(ord<122))||(chr==".")||(chr=="/")) result += chr; else result += "%"+hex_tab.charAt(Math.floor(ord/16))+hex_tab.charAt(ord%16);
    }
    return result;
  }
  
// --------------------------- nacte parametr z URL ----------------------------

  function GetParam(param_name,default_value,search_str)
  {

    if(!search_str) search_str = self.top.document.location.search.substr(1).replace("%20", " ");
    
    params = search_str.split("&");

    for (i=0;i<params.length;i++)
    {
      p = params[i].split("=");
      if (p.length>=2)
      {
        if (p[0]==param_name)
        {
          pstr = p[1];
          for (i=2;i<p.length;i++) pstr += ("="+p[i]);
          return pstr;
        }
      }
    }
    
    return default_value;
    
  }
  
// ----------------------- generovani safe url z textu -------------------------

  function CreateURLIdFromCaption(caption_text)
  {
    caption_text = caption_text.toLowerCase();
    caption_text = RemoveDiacritics(caption_text);
    caption_text = caption_text.replace(new RegExp("[^abcdefghijklmnopqrstuvwxyz0-9]+","g"),"-");
    caption_text = caption_text.replace(new RegExp("[-]+","g"),"-");
    caption_text = caption_text.replace(new RegExp("[-]+$","g"),"");
    caption_text = caption_text.replace(new RegExp("^[-]+","g"),"");
    
    return caption_text;
  }
  
// ------------------------------ str_replace ----------------------------------

  function str_replace(src_str, replace_str, str)
  {

    var arr = str.split(src_str);
    var str = arr.length>0?arr[0]:"";
    for (var i=1; i<arr.length; i++) str += replace_str+arr[i];
    return str;

    //return str.replace(new RegExp(src_str,"g"),replace_str);
  }

// ---------------------------------- trim ------------------------------------

  String.prototype.trim = function()
  {
  	return this.replace(/^\s+|\s+$/g,"");
  }

  String.prototype.ltrim = function()
  {
    return this.replace(/^\s+/,"");
  }

  String.prototype.rtrim = function()
  {
    return this.replace(/\s+$/,"");
  }

// --------------------------- odstrann diakritiky ---------------------------

  function RemoveDiacritics(str)
  {
    var source_chars = "峾";
    var dest_chars   = "acdeeinorstuuyzaaaaccdeeeilllnooorsstuuzz";
    source_chars    += source_chars.toUpperCase();
    dest_chars      += dest_chars.toUpperCase();
    
    for (i=0;i<source_chars.length;i++)
    {
      str = str.replace(new RegExp(source_chars.charAt(i),"g"),dest_chars.charAt(i));
    }

    return str;
  }
  
// ---------------------------------- formtovn cen --------------------------

  var price_precision     = 2;
  var price_suffix        = "";
  var price_decimal_sign  = ",";
  
  function RoundPrice(price)
  {
    if (price_precision>0) return Math.round(10*price_precision*price)/(10*price_precision);
    else                   return Math.round(price);
  }

  function FormatPrice(price)
  {
     var i;
     var zcount;

     price = RoundPrice(price);

     var price_formated   = "";
     var price_str        = price.toString();
     var p_arr            = price_str.split(".");

     for (i=p_arr[0].length-1;i>=0;i--)
     {
       price_formated = p_arr[0].charAt(i)+price_formated;
       if ((p_arr[0].length-i)%3==0 && i>0) price_formated = " " + price_formated;
     }


     if (price_precision>0)
     {
       price_formated += price_decimal_sign;

       if (p_arr.length==2)
       {
          price_formated += p_arr[1].substring(0, price_precision);
          zcount          = price_precision -  p_arr[1].substring(0, price_precision).length
       }
       else
       {
          zcount = price_precision;
       }
       
       for (i=1;i<=zcount;i++)
       {
         price_formated += "0";
       }

     }
     
     price_formated += price_suffix;
     return price_formated;
     
   }


  function GetRandomStr(str_len)
  {
    var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    var str   = "";
    var r;
    var i;

    for (i=0;i<str_len;i++)
    {
      r    = Math.round(Math.random()*(chars.length-1));
      str += chars.charAt(r);
    }
    
    return str;
    
  
  }