// datum laatste wijziging
var d_last_changed = new String("23 december 2009");

//init data store
var dsName = "nmbrm";
var dataStore = getDataStore(dsName);

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    //document.cookie= name + "=" + escape(value) +
    document.cookie= name + "=" + value +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/**
 * setDataStore
 * creates a cookie that will hold the datastore
 * name      name of the cookie
 * ds        the datastore (an array)
 */
function setDataStore(name, ds){
  var dsRec = new Array()
  for (index in ds){
   var dsIndex = new String(ds[index]);
   dsIndex = dsIndex.replace(/~/gi, '-')
   dsRec[dsRec.push()] = index +"="+ dsIndex.replace(/,/gi, '~')
   //dsRec[dsRec.push()] = index +"="+ escape(ds[index])
   }
  //convert to text
  dsStr = new String(dsRec)
  dsStr = dsStr.replace(/^/gi, '');
  dsStr = dsStr.replace(/,/gi, '^');
  //alert(dsStr);
  var numyears=4;
  var storeTil=new Date();
  storeTil.setFullYear(numyears+storeTil.getFullYear());
  //alert(storeTil)
  setCookie(name, dsStr, storeTil);
  //setCookie(name, dsStr);
  }

/**
 * getDataStore
 * returns a datastore from the cookie that holds the datastore
 * name      name of the cookie
 */
function getDataStore(name){
  dsStr = n2n(getCookie(name));
  //alert(dsStr)
  ds = new Array()
  if (dsStr!=''){
    dsRec = dsStr.split("^");
    for (i=0;i<dsRec.length; i++){
      var strDsRec = dsRec[i].split("=")
      //ds[strDsRec[0]] = unescape(strDsRec[1])
      strDsRecVal = new String(strDsRec[1]);
      ds[strDsRec[0]] = strDsRecVal.replace(/~/gi, ',')
      //alert(strDsRec[0])
      }
    }
  else{//array is not initialized
    ds["app"] = 'NMBRM';
    }
  return ds;
  }

/**
 * getDSval
 * returns the value of the variable in the given datastore
 * ds        name of the datastore
 * name      name of the variable
 */
function getDSval(ds, name){
   return ds[name];
   }



/**
 * setDSval
 * adds or updates the value of the variable in the given datastore
 * ds        name of the datastore
 * name      name of the variable
 * val       value of the variable
 */
function setDSval(ds, name, val){
   ds[name] = val;
   //alert(ds[name]);
   }


function getCheck(frm, fld){
        s = eval("document.forms."+frm+"."+fld)
        if (s.checked){
        		return s.value;
        		}
        else{
            return 0;
            }
        };

function setSelect(frm, fld, val){
        s = eval("document.forms."+frm+"."+fld+".options")
        for (var i=0; i< s.length; i++){
          if (s[i].value == val) s.selectedIndex = i;
          }
        };

function setCheck(frm, fld, val){
        s = eval("document.forms."+frm+"."+fld)
        if (s.value == val){
       		s.checked = true;
       		}
        else{
          s.checked = false;
          }
        };

function setRadio(frm, fld, val){
	if (val=='true') val = "1";
	if (val=='false') val = "0";
	s = eval("document.forms."+frm+"."+fld)
	for (var i=0; i< s.length; i++){
		if (s[i].value == val) s[i].checked = true;
		}
	};

function getRadio(frm, fld){
	s = eval("document.forms."+frm+"."+fld)
	for (var i=0; i< s.length; i++){
		if (s[i].checked) {
      return s[i].value;
      }
		}
	};

function in2z(inp){
  var out = new Number(0);
  if (isNaN(inp) || inp==''){
    return out;
    }
  else{
    return new Number(inp);
    }
  }

function n2n(inp){
  var out = new String("");
  inp = new String(inp);
  if (inp.toLowerCase() == 'undefined' || inp.toLowerCase() == 'null' || inp == null){
    return out;
    }
  else{
    return inp;
    }
  }

function z2n(inp){
  var out = new String("");
  inp = new String(inp);
  if (inp.toLowerCase() == 'undefined' || inp.toLowerCase() == 'null' || inp == null || inp == '0'){
    return out;
    }
  else{
    return inp;
    }
  }


function afronden(inp, dec){
  var outp
  var inp = new Number(inp);
  if (!isNaN(dec)){
    if (dec == 0) outp = Math.round(inp*1)/1;
    if (dec == 1) outp = Math.round(inp*10)/10;
    if (dec == 2) outp = Math.round(inp*100)/100;
    if (dec == 3) outp = Math.round(inp*1000)/1000;
    }
  else{
    var inpABS = inp;
    if (inp<0) inpABS=inp*-1;
    if (inpABS>=100) outp  = Math.round(inp*1)/1;
    if (inpABS<100)  outp  = Math.round(inp*10)/10;
    if (inpABS<10)   outp  = Math.round(inp*100)/100;
    }
  return outp;  
  }

function calcTotaalDM(){
  var tot_kgN_aan = 0;
  var tot_kgN_af = 0;
  var tot_kgP_aan = 0;
  var tot_kgP_af = 0;
  tot_kgN_aan = parseFloat(in2z(getDSval(dataStore, "RM1dmN"))
                   +in2z(getDSval(dataStore, "RM2dmN"))
                   +in2z(getDSval(dataStore, "RM3dmN"))
                   +in2z(getDSval(dataStore, "RM4dmN"))
                   );
  tot_kgN_af = parseFloat(in2z(getDSval(dataStore, "RM5dmN"))
                   +in2z(getDSval(dataStore, "RM6dmN"))
                   );
      
  setDSval(dataStore, "RM31FN", afronden(tot_kgN_aan - tot_kgN_af));

  tot_kgP_aan = parseFloat(in2z(getDSval(dataStore, "RM1dmP"))
                   +in2z(getDSval(dataStore, "RM2dmP"))
                   +in2z(getDSval(dataStore, "RM3dmP"))
                   +in2z(getDSval(dataStore, "RM4dmP"))
                   );
  tot_kgP_af = parseFloat(in2z(getDSval(dataStore, "RM5dmP"))
                   +in2z(getDSval(dataStore, "RM6dmP"))
                   );
      
  setDSval(dataStore, "RM31GP", afronden(tot_kgP_aan - tot_kgP_af));
  }

function calcTotaalStaldieren(){
  var tot_kgN_aan = 0;
  var tot_kgN_af = 0;
  var tot_kgP_aan = 0;
  var tot_kgP_af = 0;
  if (getDSval(dataStore, "RMsdr")=='1'){
    tot_kgN_aan = parseFloat(in2z(getDSval(dataStore, "RM5AN"))
                   +in2z(getDSval(dataStore, "RM5BN"))
                   +in2z(getDSval(dataStore, "RM5CN"))
                   +in2z(getDSval(dataStore, "RM5GN"))
                   +in2z(getDSval(dataStore, "RM5EN"))
                   );
                   
    tot_kgN_af = parseFloat(in2z(getDSval(dataStore, "RM5DN"))
                   +in2z(getDSval(dataStore, "RM5FN"))
                   +in2z(getDSval(dataStore, "RM5HN"))
                   );
    //store it      
    setDSval(dataStore, "RM5TN", afronden(tot_kgN_aan - tot_kgN_af));

    tot_kgP_aan = parseFloat(in2z(getDSval(dataStore, "RM5AP"))
                   +in2z(getDSval(dataStore, "RM5BP"))
                   +in2z(getDSval(dataStore, "RM5CP"))
                   +in2z(getDSval(dataStore, "RM5GP"))
                   +in2z(getDSval(dataStore, "RM5EP"))
                   );
    tot_kgP_af = parseFloat(in2z(getDSval(dataStore, "RM5DP"))
                   +in2z(getDSval(dataStore, "RM5HP"))
                   );
    //store it      
    setDSval(dataStore, "RM5TP", afronden(tot_kgP_aan - tot_kgP_af));
    }
  else{
    setDSval(dataStore, "RM5TN", 0);
    setDSval(dataStore, "RM5TP", 0);
    }
  //bijwerken 3.1     
  if (z2n(getDSval(dataStore,"RM5TN")) != ''){
    setDSval(dataStore, "RM4dmN", afronden(getDSval(dataStore,"RM5TN")));
    }
  else{
    setDSval(dataStore, "RM4dmN", 0);
    }
  
  if (z2n(getDSval(dataStore,"RM5TP")) != ''){  
    setDSval(dataStore, "RM4dmP", afronden(getDSval(dataStore,"RM5TP")));
    }
  else{
    setDSval(dataStore, "RM4dmP", 0);
    }
 }

function testKey(e){
  var code;
  if (!e){
    e = window.event;
    }
	if (e.keyCode){
    code = new String(e.keyCode);
    }
	else if (e.which){
    code = new String(e.which);
    }

  //alert(code)
  if (e.keyCode){
    if (code == '44'){
      e.keyCode = 46;
      }
    }
  else if (e.which){
    if (code == '44'){
      //geeft wel een js error in Firefox zie
      //https://bugzilla.mozilla.org/show_bug.cgi?id=236791
      alert('Gebruik punt als decimaal teken bij invoer van getallen');
      }
    }
  //alert(code)
  if (code == '13'){
    postForm();
    /*  
    var flds = document.forms[0];
    for (i=0;i<flds.length;i++){
      if (flds[i].id == document.activeElement.id){
        for (j=parseInt(i+1);j<flds.length;j++){
          //alert(flds[j].type)
          if (flds[j].readOnly || flds[j].readOnly == 'readonly' || (flds[j].type!='text' && flds[j].type!='select-one' && flds[j].type!='button')){
            null;
            }
          else{
            //alert(flds[j].id)
            flds[j].focus();
            if( flds[j].type=='button'){
              postForm();
              }

            return false;
            }
          }
        return false;
        }
      }
    */
    }
  }

function testNumber(field, pos){
  if (n2n(new String(pos)) == ''){
    pos = true;
    }
//  if (field.className == 'Fn' && field.value!=''){
    if (isNaN(new Number(field.value))){
      //field.value='';
      alert("Alleen numerieke waarden toegestaan in dit veld");
      field.focus();
      field.select();
      return false;
      }
    else if (new Number(field.value)<0 && pos){
      //field.value='';
      alert("Alleen positieve waarden toegestaan in dit veld");
      field.focus();
      field.select();
      return false;
      }
    else{
      return true;
      }
//    }
//  else{
//    return true;
//    }
  }

function setReadOnlyCSS(){
  /* vooralsnog nog niet nu dus wel */
  var flds = document.forms[0];
  for (i=0;i<flds.length;i++){
    if (flds[i].readOnly || flds[i].readOnly == 'readonly' ){
      flds[i].className = 'Fro';
      flds[i].tabIndex = -1;
      }
    }
  }

function setOnBlurFn(){
  /* vooralsnog nog niet */ 
  var flds = document.forms[0];
  var klas = new String("")
  for (i=0;i<flds.length;i++){
    klas = flds[i].className;
    if (klas=='Fn' ){
      flds[i].onblur = function () {return testNumber(this);};
      }
    if (klas=='FnA' ){
      flds[i].onblur = function () {return testNumber(this, false);};
      }
  
    }
  }

function testAllForN(){
  /* vooralsnog nog niet */ 
  var flds = document.forms[0];
  var klas = new String("")
  var test = true;
  for (i=0;i<flds.length;i++){
    klas = flds[i].className;
    if (klas=='Fn' && test){
      test = testNumber(flds[i]);
      }
    if (klas=='FnA' && test){
      test = testNumber(flds[i], false);
      }
    }
  return test;
  }

function getTextParam(parameters, tekst){
  var theText = new String(tekst);
  var theParams = new String(parameters);
  //alert(theParams)
  var params = theParams.split(",")
  //alert("params"+params.length)
  for (var i=0;i<params.length;i++){
    theText = theText.replace("par"+(i+1), params[i])
    }
  return theText;
  }

