﻿//searches for element in current document
function get_element_client_id(element_tag,element_id)
{
    return get_element_client_id_ex(document,element_tag,element_id);
}

//searches for element in specified document
//When you are try to get some element in parent window or in some iFrame use this method
//use this method if search is in other document than current. 
function get_element_client_id_ex(content_document,element_tag,element_id)
{
    var allInputs = content_document.getElementsByTagName(element_tag);
	for (var i=0; i <= allInputs.length - 1; i++)
	{
		if (allInputs[i].id.indexOf(element_id) != -1)
			return allInputs[i].id;
	}
}


//navigate to the page, but stays in the same window or tab
function go_to_page(url)
{
    window.open(url,'_self');    
}


function IsNullOrUndefined(obj)
{
    if(typeof(obj)==typeof(undefined))
        return true;
    if(obj == null)
        return true;
    
    return false;
}


//trims left and right space characters
function trimString(str) {
    str = this != window ? this : str;
    return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


//removes all space shars in the input parameter
function trim_everywhere(string) {
    if (string == null || string == undefined) {
        alert('Input parameter is null!');
        return;
    }

    var reg_exp = /\s+/gi;
    return string.replace(reg_exp, '');
}


function string_replace(value, replaceThis, replaceWithThis) {

    if (value == null || value == '')
        return value

    while (value.indexOf(replaceThis) > -1) {
        value = value.replace(replaceThis, replaceWithThis);
    }

    return value;
        
}
     function get_scrollbar_width () 
     {
        var inner = document.createElement('p');
        inner.style.width = '100%';
        inner.style.height = '200px';

        var outer = document.createElement('div');
        outer.style.position = 'absolute';
        outer.style.top = '0px';

        outer.style.left = '0px';
        outer.style.visibility = 'hidden';
        outer.style.width = '200px';
        outer.style.height = '150px';
        outer.style.overflow = 'hidden';
        outer.appendChild (inner);

        document.body.appendChild (outer);
        var w1 = inner.offsetWidth;
        outer.style.overflow = 'scroll';
        var w2 = inner.offsetWidth;
        if (w1 == w2) w2 = outer.clientWidth;

        document.body.removeChild (outer);

        return (w1 - w2);
    }

    
   function element_has_vertical_scrollbar(elem_id) 
   { 
        var  elem = document.getElementById(elem_id); 
    
        if(elem == null)
            alert(elem+' is not found!');
        
        if (elem.clientHeight < elem.scrollHeight) 
            return true;
        else 
            return false;
  }


  function centerElementOnScreen(element) {


      var scrollTop = document.body.scrollTop;
      var scrollLeft = document.body.scrollLeft;

      var viewPortHeight = document.body.clientHeight;
      var viewPortWidth = document.body.clientWidth;

      if (document.compatMode == "CSS1Compat") {
          viewPortHeight = document.documentElement.clientHeight;
          viewPortWidth = document.documentElement.clientWidth;

          scrollTop = document.documentElement.scrollTop;
          scrollLeft = document.documentElement.scrollLeft;
      }

      var topOffset = Math.ceil(viewPortHeight / 2 - element.offsetHeight / 2);
      var leftOffset = Math.ceil(viewPortWidth / 2 - element.offsetWidth / 2);

      var top = scrollTop + topOffset - 40;
      var left = scrollLeft + leftOffset - 70;

      element.style.position = "absolute";
      element.style.top = top + "px";
      element.style.left = left + "px";
  }



  //error handlingot ne raboti na safari i opera, ne e poddrzan od niv
  //krstev: error_handler treba da bide vo aspx, en vi liib za da moze da se koristi
  // pusti ja ovaa linija kod, ega dodeka programiram, mi smeta
  //window.onerror = error_handler;

  function error_handler(error_message, error_url, error_line) {

      try {

          if (typeof (error_message) != 'undefined' && error_message != null)
              alert('Java script error handler: ' + error_message);

          log_client_error_to_server(error_message, error_url, error_line);


      }
      catch (e) {
          alert('Java script error handler: there is a error while executing method from java script error handler!Error message:  ' + e.message);
      }
  }
  function log_client_error_to_server(error_message, error_url, error_line) {
      try {
          do_ajax_callback('clienterrorlogger.aspx', '[error_message=' + error_message + '] [error_url= ' + error_url + '] [error_line=' + error_line + ']', null);
      }
      catch (ex) {
          alert(ex);
      }
  }





  /*
  The second parameter is the number of decimal places that the number should have. 
  If the number contains more decimal places than required it will be rounded to the nearest number with that number of decimal places. 
  If it has fewer decimal places than specified zeroes will be added to the end.
  The third parameter is the thousands separator. 
  The fourth parameter is the decimal point. Either a period or comma is normal here.
  The fifth and sixth parameters are used for monetary values and one or other of them will contain the currency symbol when required. 
  If your location uses a currency symbol hard against the left of numbers then place that symbol by itself in the fifth parameter eg. '$'. 
  If you normally have a space after the currency symbol then add it after the symbol in this '$ '. 
  If your currency symbol comes after the amount instead of before then place it in the sixth parameter instead of the fifth parameter.
  The seventh and eighth parameters define the symbols to place around the number when the value is negative. 
  The usual values for these parameters would be '-','' but you may have a situation where you want to use '(',')' or even '',' CR'. 
 
  */

  function formatNumber(number, decimalPlaces, thousandsSep, decimalPoint, frontCurrencySymbol, backCurrencySymbol, negativeSign1, negativeSign2) {
      var x = Math.round(number * Math.pow(10, decimalPlaces));
      if (x >= 0)
          negativeSign1 = negativeSign2 = '';
      var y = ('' + Math.abs(x)).split('');
      var z = y.length - decimalPlaces;
      if (z < 0)
          z--;
      for (var i = z; i < 0; i++)
          y.unshift('0');
      if (z < 0) z = 1;
      y.splice(z, 0, decimalPoint);
      if (y[0] == decimalPoint) y.unshift('0');
      while (z > 3) {
          z -= 3; y.splice(z, 0, thousandsSep);
      }
      var r = frontCurrencySymbol + negativeSign1 + y.join('') + negativeSign2 + backCurrencySymbol;
      return r;
  }


  function getPageSizeWithScroll() {

      if (window.innerHeight && window.scrollMaxY) // Firefox 
      {
          pageWidth = window.innerWidth + window.scrollMaxX;
          pageHeight = window.innerHeight + window.scrollMaxY;
      }
      else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
      {
          pageWidth = document.body.scrollWidth;
          pageHeight = document.body.scrollHeight;
      }
      else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
      { pageWidth = document.body.offsetWidth + document.body.offsetLeft; pageHeight = document.body.offsetHeight + document.body.offsetTop; }

      arrayPageSizeWithScroll = new Array(pageWidth, pageHeight);
      //alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
      return arrayPageSizeWithScroll;


  }
