function validate_date(field) {
  var danes = new Date() ;
  var month;
  var day;
  var year;
  var isvalid;
  var comps;
  var len , tmp ;
  var strdate;

  if ( field.value.replace(/ +/, '') == '' )
  { field.value = '' ;
    return true;
  };
  
  if(chkChars(field.value, '0123456789')) {
    if(field.value.length >= 4) {
      year  = danes.getFullYear() ;
      year = year.toString();
      strdate = field.value.substring(0,2) + '.' + field.value.substring(2,4) + '.';
      switch (field.value.length) {
        case 4:
          field.value = strdate + year;
          break;
        case 5:
          field.value = strdate + year.substring(0,3) + field.value.substring(4,5);
          break;
        case 6:
          field.value = strdate + year.substring(0,2) + field.value.substring(4,6);
          break;
        case 8:
          field.value = strdate + field.value.substring(4,8)
          break;
      }
    }
  }
  
  comps = field.value.split( /\D/g ) ;
  len = comps.length ;

  day   = danes.getDate() ;
  month = danes.getMonth()+1 ;
  year  = danes.getFullYear() ;

  tmp = parseInt(comps[0],10) ;
  if ( !isNaN(tmp) ) day = tmp ;
  tmp = parseInt(comps[1],10) ;
  if ( !isNaN(tmp) ) month = tmp ;
  tmp = parseInt(comps[2],10) ;
  if ( !isNaN(tmp) ) year = tmp ;

  if (year < 50)
    year += 2000;
  else if (year < 100)
    year += 1900;

  if ( month >= 1 && month <= 12 && day >= 1 && day <= 31 && year > 1900 && year < 2100 )
  {
    field.value = getdatestring(new Date(year, month - 1, day));
    isvalid = true;
  }
  else
  {
    alert("Pogrešno unesen datum !");
    isvalid = false;
  };
  if ( ! isvalid )
  {
    window.focus();
    field.focus();
  }
  return isvalid;
}

function getdatestring(d) {
  var Day = d.getDate();
  var Month = d.getMonth()+1;
  var Year = d.getFullYear();
  if (Day < 10) Day = "0" + Day;
  if (Month < 10) Month = "0" + Month;

  return Day + "." + Month + "." + Year; 
}

function validate_hour(field){
  var hour;
  var minutes;
  var seconds;
  var isvalid = true;
  var comps;
  var len , tmp ;

  if ( field.value.replace(/ +/, '') == '' )
  { field.value = "00:00";
    return true;
  };
  
  if (field.value.length==4) {
    field.value = field.value.substring(0,2) + ':' + field.value.substring(2,4)
  }

  comps = field.value.split( /\D/g ) ;
  len = comps.length ;

  hour   = 0;
  minutes = 0;
  seconds  = 0;

  tmp = parseInt(comps[0],10);
  if ( !isNaN(tmp) ) hour = tmp;
  tmp = parseInt(comps[1],10);
  if ( !isNaN(tmp) ) minutes = tmp;
  tmp = parseInt(comps[2],10);
  if ( !isNaN(tmp) ) seconds = tmp;

  if ( hour >= 0 && hour <= 23 && minutes >= 0 && minutes <= 59 && seconds >= 0 && seconds <= 59 )
  {
    field.value = gethourstring(new Date(0, 0, 0, hour, minutes, seconds)).substring(0, 5);
  }
  else
  {
    isvalid = false;
  };

  if ( ! isvalid )
  {
    alert("Pogrešno uneseno vrijeme !");
    window.focus();
    field.focus();
  }
  return isvalid;
}

function gethourstring(d) {
  var Hours = d.getHours();
  var Minutes = d.getMinutes();
  var Seconds = d.getSeconds();
  if (Hours < 10) Hours = "0" + Hours;
  if (Minutes < 10) Minutes = "0" + Minutes;
  if (Seconds < 10) Seconds = "0" + Seconds; 

  return Hours + ":" + Minutes + ":" + Seconds;
}

function chkChars(str, checkOK)
{
  var allValid = true;
  var i, j, ch;

  for (i = 0;  i < str.length;  i++) {
    ch = str.charAt(i);
    for (j = 0;  j < checkOK.length;  j++) {
      if (ch == checkOK.charAt(j))
        break;
    }
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
  }
  return allValid;
}

function validate_numeric(objName,minval,maxval,comma,period,hyphen)
{
  var numberfield = objName;
  var msg = chkNumeric(objName,minval,maxval,comma,period,hyphen);
  if (msg != '')
  {
    alert(msg);
    window.focus();
    numberfield.focus();
    return false;
  }
  else
  {
    return true;
  }
}

function chkNumeric(objName,minval,maxval,comma,period,hyphen)
{
  // only allow 0-9 be entered, plus any values passed
  // (can be in any order, and don't have to be comma, period, or hyphen)
  // if all numbers allow commas, periods, hyphens or whatever,
  // just hard code it here and take out the passed parameters
  var checkOK = "-0123456789" + comma + period + hyphen;
  var checkStr = objName;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  var alertsay = '';

  for (i = 0;  i < checkStr.value.length;  i++) {
    ch = checkStr.value.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
  }

  if (!allValid) {
    alertsay = "Dozvoljeni su samo znakovi \""
    alertsay = alertsay + checkOK + "\" u polju \"" + checkStr.title + "\""
    return alertsay;
  }

  // set the minimum and maximum
  var chkVal = allNum.replace( /,/ , "." );
  var prsVal = parseFloat(chkVal);
  if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval)) {
    alertsay = "Unesite vrijednost veču ili  "
    alertsay = alertsay + "jednaku \"" + minval + "\" i manju ili "
    alertsay = alertsay + "jednaku \"" + maxval + "\" u polju \"" + checkStr.title + "\""
    return alertsay;
  }
  return alertsay;
}

function validate_integer(objName, minval, maxval)
{
  var numberfield = objName;
  var msg = chkInteger(objName,minval,maxval)
  if (msg != '')
  {
    alert(msg);
    window.focus();
    numberfield.focus();
    numberfield.select();
    return false;
  }
  else
  {
    return true;
  }
}

function chkInteger(objName,minval,maxval)
{
  // only allow 0-9 be entered, plus any values passed
  // (can be in any order, and don't have to be comma, period, or hyphen)
  // if all numbers allow commas, periods, hyphens or whatever,
  // just hard code it here and take out the passed parameters
  var checkOK = "0123456789"
  var checkStr = objName;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  var alertsay = '';

  for (i = 0;  i < checkStr.value.length;  i++) {
    ch = checkStr.value.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
    if ((ch != ",")||(ch != ".")) allNum += ch;
  }

  if (!allValid) {
    alertsay = "Dozvoljeni su samo znakovi \""
    alertsay = alertsay + checkOK + "\" u polju \"" + checkStr.title + "\""
    return alertsay;
  }

  // set the minimum and maximum
  // ne preverjamo, če sta minval in maxval enaki 0
  var chkVal = allNum.replace( /,/ , "." );
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval) && minval != 0 && maxval != 0) {
    alertsay = "Unesite cjelobrojnu vrijednost veču ili  "
    alertsay = alertsay + "jednaku \"" + minval + "\" i manju ili "
    alertsay = alertsay + "jednaku \"" + maxval + "\" u polju \"" + checkStr.title + "\""
    return alertsay;
  }
  return alertsay;
}

function validate(field, format, required, fieldLabel) {
  var ok = true;
  if(fieldLabel != '') fieldLabel = ': ' + fieldLabel;
  switch (format.toUpperCase()){
    case 'C':
      if (field.value == '' && required=='1') {
        alert('Potreban alfanumerički unos' + fieldLabel + '!');
        field.focus();
        if(field.tagName == 'INPUT') field.select();
        return false;
      }
      break;
    case 'N':
      if (field.value == '' && required=='1') {
        alert('Potreban numerički unos' + fieldLabel + '!');
        field.focus();
        if(field.tagName == 'INPUT') field.select();
        return false;
      }
      ok = validate_numeric(field,-99999999,99999999,',','.','');
      break;
    case 'NP':
      if (field.value == '' && required=='1') {
        alert('Potreban pozitivan numerički unos' + fieldLabel + '!');
        field.focus();
        if(field.tagName == 'INPUT') field.select();
        return false;
      }
      ok = validate_numeric(field,0.000001,99999999,',','.','');
      break;
    case 'Z':
      if (field.value == '' && required=='1') {
        alert('Potreban unos iznosa' + fieldLabel + '!');
        field.focus();
        if(field.tagName == 'INPUT') field.select();
        return false;
      }
      ok = validate_numeric(field,-99999999,99999999,',','.','');
      break;
    case 'ZP':
      if (field.value == '' && required=='1') {
        alert('Potreban unos pozitivnog iznosa' + fieldLabel + '!');
        field.focus();
        if(field.tagName == 'INPUT') field.select();
        return false;
      }
      ok = validate_numeric(field,0.01,99999999,',','.','');
      break;
    case 'I':
      if (field.value == '' && required=='1') {
        alert('Potreban cjelobrojni unos' + fieldLabel + '!');
        field.focus();
        if(field.tagName == 'INPUT') field.select();
        return false;
      }
      ok = validate_integer(field,-99999999,99999999);
      break;
    case 'IP':
      if (field.value == '' && required=='1') {
        alert('Potreban pozitivan cjelobrojni unos' + fieldLabel + '!');
        field.focus();
        if(field.tagName == 'INPUT') field.select();
        return false;
      }
      ok = validate_integer(field,1,99999999);
      break;
    case 'D':
      if (field.value == '' && required=='1') {
        alert('Potreban datumski unos' + fieldLabel + '!');
        field.focus();
        if(field.tagName == 'INPUT') field.select();
        return false;
      }
      ok = validate_date(field);
      break;
  }
  return ok;
}

function validateForm(form, fields) {
  var title;
  var fieldPar, fieldName, format, required;
  for(var i=1; i<arguments.length; i++) {
    fieldPar = arguments[i].split(';');
    fieldName = fieldPar[0];
    format = fieldPar[1];
    required = fieldPar[2];
    if(fieldName != '' && eval('form' + '.' + fieldName) != undefined) {
      if(eval('form' + '.' + fieldName + '.' + 'title') != undefined)
        title=eval('form' + '.' + fieldName + '.' + 'title');
      else
        title=fieldName;
      if(validate(eval('form' + '.' + fieldName), format, required, title) == false) return false;
    }
  }
  return true;
}

function date_complete(field,key) {
  if(key != 106 && key != 107 && key != 109) {
    return false;
  }

  if(!validate_date(field)) {
    return false;
  }

  var tmp;
  var comps;
  if(field.value == '') {
    field.value = getdatestring(new Date());
  }
  comps = field.value.split( /\D/g );
  tmp = parseInt(comps[0],10);
  if ( !isNaN(tmp) ) day = tmp;
  tmp = parseInt(comps[1],10);
  if ( !isNaN(tmp) ) month = tmp;
  tmp = parseInt(comps[2],10);
  if ( !isNaN(tmp) ) year = tmp;

  var danes = new Date();
  switch (key) {
    case 106: // *
      field.value = getdatestring(new Date());
      break;
    case 107: // +
      day   = day + 1;
      field.value = getdatestring(new Date(year, month - 1, day));
      break;
    case 109: // -
      day   = day - 1;
      field.value = getdatestring(new Date(year, month - 1, day));
      break;
  }
  field.onchange();
  return true;
}

function date_add(field,incdec) {

  if(!validate_date(field) || field.value=='') {
    return '';
  }
  
  incdec = parseInt(incdec,10);
  if(isNaN(incdec)) {
    return '';
  }

  var tmp;
  var comps;
  comps = field.value.split( /\D/g );
  tmp = parseInt(comps[0],10);
  if ( !isNaN(tmp) ) day = tmp;
  tmp = parseInt(comps[1],10);
  if ( !isNaN(tmp) ) month = tmp;
  tmp = parseInt(comps[2],10);
  if ( !isNaN(tmp) ) year = tmp;

  return getdatestring(new Date(year, month-1, day+incdec));
}

function fRound(v,places) {
  var ok = true;
  var i, j, ch, vstr;
  
  if(places >= 0) {
    if(!chkChars(v,'0123456789.,')) {
      ok = false;
    } else {
      vstr = v.toString();
      if(vstr.indexOf(',') >= 0) {
        vstr = vstr.replace(/\./g , "");
        vstr = vstr.replace(/,/ , ".");
      }
      v = parseFloat(vstr);
      v = v.toFixed(places);
      v = parseFloat(v);
    } 
  } else {
    ok = false;
  }
  if(ok)
    return v;
  else
    return '';
}

function check_amount(e,id) {
  var ns = (navigator.appName == "Netscape");
  if(ns) var key_index = e.which;
  else var key_index = window.event.keyCode;

  // only allow numbers + backspace + tab
  if((key_index>47 && key_index<58) || key_index==0 || key_index==8 || key_index==9 || key_index==44 || key_index==46) 
    return true;
  else
    return false;
}

//currency format
function format_currency(name,dec) {
  inp=name.value;
  vejica=0;
  sign = ""

  if (inp.substring(0,1) == '-') 
    {
        sign = "-";
        inp = inp.substring(1, inp.length);
    };

  if (inp=="") return;
  outt="";
  for (var i=0; i<inp.length; i++) {
    znak=inp.charAt(i);
    if (znak==".") continue;
    else if (znak==",") {      
      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="";
  if (dec) {
    nule="000000000";
    out=",";
    pos1=outt.indexOf(",");
    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="." + outt.substring(i-3,i) + out;
  out=outt.substring(0,i)+out;

  name.value=sign+out;
}

