function isNumeric(key) {
    return (key >= 48 && key <= 57);
}

function numInput(field, e, val) {
    var key;

    if (navigator.appName == 'Netscape') {
        key = e.which;
        evnt = e;
        if (isNumeric(key) || key < 32)
            return true;
        if (val != null) {
            for (i = 0; i < val.length; i++) {
                if (val[i] == key)
                    return true;
            }
        }
        return false;
    }
    else {
        key = window.event.keyCode;
        window.event.returnValue = true;
        if (isNumeric(key))
            return true;
        if (val != null) {
            for (i = 0; i < val.length; i++) {
                if (val[i] == key)
                    return true;
            }
        }
        window.event.returnValue = false;
        return false;
    }
}

function CheckNumericAdv(field, e, onlyNumeric) {
    if (!onlyNumeric) {
        var a = new Array(2);
        a[0] = 46;
        a[1] = 44;
        return numInput(field, e, a);
    }
    else return numInput(field, e);
    /*
    var key;
    if (navigator.appName == 'Netscape') {
    key = e.which;
    if ((key > 47 && key < 58) || (!onlyNumeric && (key == 46 || key == 44)))
    return true;
    else
    return false;
    }
    else {
    key = window.event.keyCode;
    if ((key > 47 && key < 58) || (!onlyNumeric && (key == 46 || key == 44)))
    window.event.returnValue = true
    else
    window.event.returnValue = false;
    }
    */
}


function CheckNumeric(field, e, onlyNumeric) {
    var key;

    if (navigator.appName == 'Netscape') {
        key = e.which;
        if (key == 8 || key == 13 || key == 0)
            return true;
    }
    return CheckNumericAdv(field, e, onlyNumeric);

}
function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-,";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function isNumber(value) {
   return typeof value === 'number' && 
   isFinite(value);
}

function unformat_znesek(znesekStr) {
    var temp = "";

    for (var i = 0; i < znesekStr.length; i++) {
        if (znesekStr.charAt(i) == CultureGroupSeparator) {
        }
        else if (znesekStr.charAt(i) == CultureDecimalSeparator)
            temp = temp + ".";
        else
            temp = temp + znesekStr.charAt(i);
    }
    return temp;
}

function AlertAndFocus(field, msg) {
    alert(msg);
    field.focus();
    // če kličemo na onchange in uporabimo TAB / kliknemo drugo polje se field ne bi fokusiral, zato spodnja vrstica
    var tempFF = function SetFocusAfterTimeOut() { field.focus(); };
    setTimeout(tempFF, 5);
}

function IsNapacenZnesek(znesek, str_err_message) {

    var control = znesek;
    if(  control != null && control.type == "text")
    {inp = znesek.value;}
    else    
    {inp = znesek.innerHTML;}
    
    pos = -1
    num = inp.length
    for (i = inp.length - 1; i >= 0; i--) {
        
        if (inp.charAt(i) == CultureDecimalSeparator) {
            if (num != inp.length) //|| i == 0 )
            {
                AlertAndFocus(znesek, str_err_message);
                return true;
            }
            num = i;
        }
        if (inp.charAt(i) == CultureGroupSeparator) {
            if (((num - i - 1) % 3) != 0 || i == 0 || i == (inp.length - 1)) {
                AlertAndFocus(znesek, str_err_message);
                return true;
            }
            num = i;
        }
    }
    return false;
}

function trim_number(s) {
    while (s.substring(0, 1) == '0') {
        s = s.substring(1, s.length);
    }
    while (s.substring(s.length - 1, s.length) == ' ') {
        s = s.substring(0, s.length - 1);
    }

    if (s.substring(0, 1) == CultureDecimalSeparator) s = '0' + s;

    return s;
}

function format_znesek(name, dec, str_err_message) {
    var control = name;
    if(  control != null && control.type == "text")
    {inp = name.value;}
    else    
    {inp = name.innerHTML;}

    if (IsNapacenZnesek(name, str_err_message))
        return;
    
    if (inp != "") {
        vejica = 0;
        if (inp == "") return;
        outt = "";
        for (i = 0; i < inp.length; i++) {
            znak = inp.charAt(i);
            if (znak == CultureGroupSeparator) continue;
            else if (znak == CultureDecimalSeparator) {
                if (dec == 0) break;
                if (vejica > 0) break;
                vejica = 1;
                outt = outt + znak;
            }
            else if (znak < "0" || znak > "9") break;
            else outt = outt + znak;
        }
        out = "";
        outt = trim_number(outt);

        if (dec) {
            nule = "000000000";
            out = CultureDecimalSeparator;
            pos1 = outt.indexOf(CultureDecimalSeparator);
            if (pos1 != -1) {
                pos2 = outt.length;
                if (pos2 - pos1 - 1 > dec) pos2 = pos1 + dec + 1;
                out = out + outt.substring(pos1 + 1, pos2);
                outt = outt.substring(0, pos1);
            }
            out = out + nule.substring(0, 3 - out.length);
        }

        for (i = outt.length; i > 3; i -= 3) out = CultureGroupSeparator + outt.substring(i - 3, i) + out;
        out = outt.substring(0, i) + out;

        zero = CultureDecimalSeparator + "00";
        if (out == zero)
            out = "0" + CultureDecimalSeparator + "00";
            
        name.innerHTML = out;
    }

}

function ValidateNumeric() 
{ 
    var keyCode = window.event.keyCode; 
    if (keyCode > 57 || keyCode < 48) 
        window.event.returnValue = false; 
} 

// calculate the ASCII code of the given character
function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}

function checkNumber(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);

  /* Check if the keyed in character is a number
     do you want alphabetic UPPERCASE only ?
     or lower case only just check their respective
     codes and replace the 48 and 57 */

  if (cCode < 48 || cCode > 57 ) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
}

function novo_okno_pomoc(naslov)
{
  window.open(naslov,'pomoc','resizable=yes,scrollbars=yes,width=600,height=420,menubar=no,toolbar=no,location=no,directories=no,status=yes');
}

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/number/fmt-money [v1.0]
function fmtMoney(n, c, d, t){ //v1.0
    var m = (c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".",
        /(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
    return (x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(\d{3})(?=\d)/g,
        "$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : "");
}

function FormatMoney(input)
{
    input.value = fmtMoney(MoneyToFloat2(input.value));
    return;
}

function MoneyToFloat2(input)
{
  var strMoney, intMoneyLength, intIndex, strInteger;

  strMoney = new String(input);
  intIndex = 0;
  while (strMoney.substring(intIndex, intIndex+1) == '0') {intIndex++};

  intMoneyLength = strMoney.length;
  strMoney = strMoney.substring(intIndex, intMoneyLength);

  strInteger = new String("");
  intFracIndex = intMoneyLength;
  for (intIndex=0; intIndex<intMoneyLength; intIndex++)
  {
    if (strMoney.substring(intIndex,intIndex+1) == ".")
    {
      continue
    };
    if (strMoney.substring(intIndex,intIndex+1) == ",")
    {
      intFracIndex = intIndex + 1;
      break;
    };
    strInteger += strMoney.substring(intIndex,intIndex+1);
  }
  if (strInteger.length == 0) 
  {
    strInteger = "0";
  }
  
  strFrac = new String("");
  for (intIndex = intFracIndex; ((intIndex < intMoneyLength) && (intIndex < intIndex + 2)); intIndex++)
  {
    strFrac += strMoney.substring(intIndex, intIndex+1);
  }
  if (strFrac.length == 0) 
  {
    strFrac = "0";
  }

  if (isNaN(strInteger) || isNaN(strFrac))
  {
    return 0;
  }
  else
  {
    return strInteger + "." + strFrac;
  }	
}

function MoneyToInteger(input)
{
  var strMoney, intMoneyLength, intIndex, strInteger;

  strMoney = new String(input);
  intIndex = 0;
  while (strMoney.substring(intIndex, intIndex+1) == '0') {intIndex++};

  intMoneyLength = strMoney.length;
  strMoney = strMoney.substring(intIndex, intMoneyLength);

  strInteger = new String("");
  intFracIndex = intMoneyLength;
  for (intIndex=0; intIndex<intMoneyLength; intIndex++)
  {
    if (strMoney.substring(intIndex,intIndex+1) == ".")
    {
      continue
    };
    if (strMoney.substring(intIndex,intIndex+1) == ",")
    {
      intFracIndex = intIndex + 1;
      break;
    };
    strInteger += strMoney.substring(intIndex,intIndex+1);
  }
  
  strFrac = new String("");
  for (intIndex = intFracIndex; intIndex < intMoneyLength; intIndex++)
  {
    strFrac += strMoney.substring(intIndex, intIndex+1);
  }
  intFrac = parseInt(strFrac);

  if (strInteger.length == 0) {return 0};
  if (isNaN(strInteger))
  {
    return 0;
  }
  else
  {
    if (intFrac > 0)
      return parseInt(strInteger) + 1;
    else
      return parseInt(strInteger);
  }	
}



