/**
 * Vseobecne, podporne funkcie pre ostatne objekty.
 *
 * dependencies: CrossBrowser
 */
var CommonFunctions = new function() {
  /**
   * Prida paramater do URI
   *
   * @param isURI povodne URI
   * @param isParamName nazov pridavaneho parametra
   * @param isParamValue hodnota pridavaneho parametra
   * @return URI s pridanym parametrom
   */
  this.AddParamToURI = function(isURI, isParamName, isParamValue) {
    var lsParts = isURI.match(/^([^#?]*)(\?.*?(\&)?)?(#.*)?$/);
    var lsParam = isParamName + '=' + encodeURIComponent(isParamValue);

    if (this.VarToStr(lsParts[2]) == '') {
      lsParam = '?' + lsParam;
    } else if(this.VarToStr(lsParts[3]) == '') {
      lsParam = '&' + lsParam;
    }

    return   this.VarToStr(lsParts[1])
           + this.VarToStr(lsParts[2])
           + lsParam
           + this.VarToStr(lsParts[4]);
  }

  /**
   * Najde nadradeny element specifikovany nazvom elementu a/alebo
   * nazvom triedy
   *
   * @param isParentTagName nazov hladaneho elementu alebo null ak
   * nehladame podla nazvu elementu
   * @param isClassName nazov triedy (hodnota atributu class)
   * hladaneho elementu, alebo null ak nehladame podla nazvu triedy
   * @param null ak nenasiel pozadovany element alebo pozadovany element
   */
  this.FindParent = function(irElement, isParentTagName, isClassName) {
    while(   (irElement != null)
          && (   (   (isParentTagName != null)
                  && (irElement.tagName.toLowerCase() != isParentTagName)
                 )
              || (   (isClassName != null)
                  && (irElement.className != isClassName)
                 )
              )
         )
      irElement = irElement.parentNode;
    return irElement;
  }

  /**
   * Testuje ci sa jedna odkaz v ramci dokumentu (kotva)
   *
   * @param irAnchor element odkazu
   * @return true ak sa jedna o odkaz v ramci dokumentu
   */
  this.isNamedAnchor = function(irAnchor) {
    return ((irAnchor.name != null) && (irAnchor.name != ''));
  }

 /**
   * Testuje ci sa jedna o odkaz v ramci dokumentu (kotva)
   *
   * @param irAnchor element odkazu
   * @return true ak sa jedna o odkaz v ramci dokumentu
   */
  this.isLocalAnchor = function(irAnchor) {
    if((this.VarToStr(irAnchor.href) == '') || this.isNamedAnchor(irAnchor))
      return true;
    else{
      return (irAnchor.href.replace(/^([^#]+)(#.*)?$/,'$1') == document.URL.replace(/^([^#]+)(#.*)?$/,'$1'));
    }
  }

  /**
   * Konvertuje premennu na string
   *
   * @param irVar vstupna premenna
   * @return textova reprezentacia hodnoty premennej
   */
  this.VarToStr = function (irVar) {
    if (CrossBrowser.IsUndefined(irVar))
      lsResult = '';
    else if (irVar == null)
      lsResult = '';
    else
      lsResult = irVar;
    return lsResult;
  }
}
