
    /*
    * local cache functions
    * The KBOCache is used to cache enterprises that were COMPLETELY fetched
    * from KBO
    */
    function KBOCache() {
      this.ENT = new Object();
      this.BU = new Object();
      this.last;
      this.lastBU;


      /**
      * Add voegt een enterprise of een business unit aan de cache toe
      * de functie checkt eerst over welk van de twee objecten het gaat en voegt
      * deze dan aan de juiste cache toe
      */
      this.Add = function(obj) {
        if(obj.type == 1) {
          this.ENT[new String(obj.Entnumber)] = obj;
          this.last = new String(obj.Entnumber);
        } else if(obj.type == 2) {
          this.BU[new String(obj.Entnumber)] = obj;
          this.lastBU = obj.Entnumber;}
          else if (obj.type == undefined){
            this.ENT[new String(obj.Entnumber)]=obj;
            this.last = new String(obj.Entnumber);
          }
      }

      this.Get = function(num) {
        num = new String(num);
        if(this.isCached(num)) {
          if(this.ENT[num] != undefined) {
            this.last = num;
            return this.ENT[num];
          } else if(this.BU[num] != undefined) {
            this.lastBU = num;
            return this.BU[num];
          }

        }
        else
          return undefined;
      }

      this.getCurrent = function() {
        return this.ENT[this.last];
      }

      this.getCurrentBU = function() {
        return this.BU[this.lastBU];
      }


      this.isCached = function(num) {
        num = new String(num);
        return (this.ENT[num]!=undefined) || (this.BU[num]!=undefined);
      }
    }
    var KBOCACHE = new KBOCache();

    /*
    *  PersonsCache
    */

    function personCache() {
      this.persons = new Object();
      this.current;

      this.Add = function(p) {
        this.persons[p.pepersonnumber] = p;
        this.current = p.pepersonnumber;
      }

      this.Get = function(nr) {
        this.current = nr;
        return this.persons[nr];
      }

      this.IsCached = function(nr) {
        return this.persons[nr] != undefined;
      }

      this.GetCurrent = function() {
        return this.persons[this.current];
      }
    }
    var PERSONCACHE = new personCache();

    /*
    * CARD CACHE
    */

    function cardCache()
    {
      this.cards=new Object();
      this.current;

      this.Add=function(c){
          this.cards[c.cardNumber]=c;
          this.current=c.cardNumber;
      };

      this.Get=function(nr){
        this.current = nr;
        return this.cards[nr];
      };

      this.IsCached=function(nr){
        return this.cards[nr] != undefined;
      };

      this.getCurrent=function(){
        return this.cards[this.current];
      };
    }

    var CARDCACHE = new cardCache();


    /*
    * AJAX Functions
    */
    function getJSON(action,paramsObj,cb) {
      var zinneke="De gegevens worden opgehaald";
      var url;
      switch(action) {
        case "001":
          url = "direct.ws2_ajax.search_enterprise";
          break;
        case "002":
          url = "direct.ws2_ajax.get_enterprise_cards";
          break;
        case "004":
          url = "direct.ws2_ajax.get_businessunit";
          break;
        case "005":
          url = "direct.ws2_ajax.search_person";
          break;
        case "003":
          url = "direct.ws2_ajax.get_person";
          break;
        case "102":
          url="direct.ws2_ajax.SEARCH_AT_ENTERPRISE";
          break;
        case "104":
          url="direct.ws2_ajax.GET_AT_READCARD";
          break;
        case "107":
          url="direct.ws2_ajax.GET_AT_LISTCARDSTATUS";
          break;
        case "108":
          url="direct.ws2_ajax.GET_AT_LISTATTRACTIONS";
          break;
        case "103":
          url="direct.ws2_ajax.get_at_person";
          break;
        case "159":
          url="direct.ws2_ajax.ADD_AT_PERSON";
          zinneke="de gegevens worden verwerkt";
          break;
        case "156":
          url="direct.ws2_ajax.add_at_enterprise";
          zinneke="de gegevens worden verwerkt";
          break;
        case "158":
          url="direct.ws2_ajax.DEL_AT_ENTERPRISE";
          zinneke="de gegevens worden verwerkt";
          break;
        case "157":
          url="direct.ws2_ajax.UPDATE_AT_ENTERPRISE";
          zinneke="de gegevens worden verwerkt";
          break;
        case "161":
          url="direct.ws2_ajax.DEL_AT_PERSON";
          zinneke="de gegevens worden verwerkt";
          break;
        case "160":
          url="direct.ws2_ajax.UPDATE_AT_PERSON";
          zinneke="de gegevens worden verwerkt";
          break;
        case "101":
          url="direct.ws2_ajax.SEARCH_AT_CARD";
          break;
        case "151":
          url="direct.ws2_ajax.ADD_CARD";
          zinneke="de gegevens worden verwerkt";
          break;
      }
      showBusyModal(zinneke);
      AjaxCall(url,paramsObj,function(txt){getJSONCB(txt,cb);},"post",false);
    }

    function getJSONCB(txt,cb) {
      function checkStatus() {
        if(obj.Status.Code != "KOE00001" &&
           obj.Status.Code != "KMOCACHED" &&
           obj.Status.Code != 0 && //this is an exception for the old version of the wsperson webservice that are still used
           obj.Status.Code != 1 &&
           obj.Status.Code != "KBCA_WRD_001" &&
           obj.Status.Description != "BISREG_R55" &&
           obj.Status.Code != "RRNP.000" &&
           obj.Status.Code != "AAH00000" &&
           obj.Status.Code != "AAH00114" && //het persoonsnummer is niet gevonden (mogelijkheid tot aanmaken)
           obj.Status.Code != "AAH00119" && //de onderneming is niet gevonden
           obj.Status.Code != "AAH00110") {
          YahooAlert(obj.Status.Code,obj.Status.Description);
          return false
        } else
          return true;
      }
      var obj = eval('(' + txt + ')');
      hideBusyModal();
      if(checkStatus())
        cb(obj);
    }

    function searchEnterprise(P_DENOMINATION,
                              P_ADR_STREETCODE,
                              P_ADR_NUMBER,
                              P_ADR_POSTCODE,
                              P_ADR_NISCODE,
                              P_ADR_COUNTRYCODE,
                              P_ENTITYSTATUS,
                              P_ENTITYTPE,
                              P_JURIDICALFORM,
                              callBack) {
      var obj = new Object();
      obj["P_DENOMINATION"] = P_DENOMINATION;
      obj["P_ADR_STREETCODE"] = P_ADR_STREETCODE;
      obj["P_ADR_NUMBER"] = P_ADR_NUMBER;
      obj["P_ADR_POSTCODE"] = P_ADR_POSTCODE;
      obj["P_ADR_NISCODE"] = P_ADR_NISCODE;
      obj["P_ADR_COUNTRYCODE"] = P_ADR_COUNTRYCODE;
      obj["P_ENTITYSTATUS"] = P_ENTITYSTATUS;
      obj["P_ENTITYTPE"] = P_ENTITYTPE;
      obj["P_JURIDICALFORM"] = P_JURIDICALFORM;
      getJSON("001",obj,function(o){searchEnterpriseCB(o,callBack);});
    }

    function searchEnterpriseCB(obj,cb) {
      if(cb != undefined)
        cb(obj);
    }

    function search_at_enterprise(atnum,callBack)
    {
      if (KBOCACHE.isCached(atnum))
      {
        callBack(KBOCACHE.Get(atnum),callBack);
      }
      else
      {
        var obj = new Object();
        obj["p_atnumber"]=atnum;
        getJSON("102",obj,function(o){searchATEnterpriseCB(o,callBack);});
      }
    }

    function searchATEnterpriseCB(o,callBack)
    {
     try{
     KBOCACHE.Add(o.atEnterprise);
     }catch(e){};
     if (callBack != undefined)
      callBack(o);
    }

    function getAtPerson(atnum,callBack)
    {
      if (PERSONCACHE.IsCached(atnum))
      {
        callBack(PERSONCACHE.Get(atnum),callBack);
      }
      else
      {
        var obj=new Object();
        obj["p_atNumber"]=atnum;
        getJSON("103",obj,function(o){searchAtPersonCB(o,callBack);});
      }
    }

    function searchAtPersonCB (o,callBack)
    {
      try{
      PERSONCACHE.Add(o.AtPerson);
      }catch(e){};
      if (callBack!=undefined)
        callBack(o);
    }

    function search_cardNumber (cn,callBack)
    {
        if (CARDCACHE.IsCached(cn))
        {
          callBack(CARDCACHE.Get(cn),callBack);
        }
        else
        {
          var obj= new Object();
          obj["p_cardnumber"]=cn;
          getJSON("104",obj,function(o){searchCardNumberCB(o,callBack);});
        }
    }

    function searchCardNumberCB(o,callback)
    {
      try{CARDCACHE.Add(o.card)}
      catch(e){};
      if (callback != undefined)
      {
       callback(o);
      }
    }

    function getCardHistory (cn,callBack)
    {
      var obj=new Object();
      obj["p_cardnumber"]=cn;
      getJSON("107",obj,function(o){getCardHistoryCB(o,callBack);});
    }

    function getCardHistoryCB(o,callBack)
    {
      if (callBack != undefined)
      {
        callBack(o);
      }
    }

    function getFairAttractionHistory (cn,callBack)
    {
         var obj=new Object();
         obj["p_cardnumber"]=cn;
         getJSON("108",obj,function(o){getFairAttractionHistoryCB(o,callBack,1,"Y");});
    }

    function getFairAttractionHistoryCB(o,cb,p,m)
    {
      if (cb!=undefined)
      {
        cb(o,p,m);
      }
    }

    function getEnterprise(num,cb) {
      if(KBOCACHE.isCached(num))
        cb(KBOCACHE.Get(num),cb);
      else {
        var obj = new Object();
        obj["p_number"] = num;
        getJSON("002",obj,function(o){getEnterpriseCB(o,cb);});
      }
    }

    function getEnterpriseCB(obj,cb) {
      if(obj.Entities[0].Enterprise) {
        KBOCACHE.Add(obj.Entities[0].Enterprise);
        if(cb!=undefined)
          cb(obj);
      }
      else
        throw "No enterprise found";
    }

    function getBU(num,cb) {
      if(KBOCACHE.isCached(num))
        cb(KBOCACHE.Get(num),cb);
      else {
        var obj = new Object();
        obj["p_number"] = num;
        getJSON("004",obj,function(o){getBUCB(o,cb);});
      }
    }

    function getBUCB(obj,cb) {
      if(obj.Entities[0].BusinessUnit) {
        KBOCACHE.Add(obj.Entities[0].BusinessUnit);
        if(cb!=undefined)
          cb(obj.Entities[0].BusinessUnit);
      } else
        throw "No business unit found";
    }

    function searchPerson(p_niscode,
                          p_firstName,
                          p_lastName,
                          p_middleName,
                          p_birthdateDay,
                          p_birthdateMonth,
                          p_birthdateYear,
                          p_genderCode,
                          p_tolerance,
                          cb) {
       var obj = new Object();
       obj["p_niscode"] = p_niscode;
       obj["p_firstName"] = p_firstName;
       obj["p_lastName"] = p_lastName;
       obj["p_middleName"] = p_middleName;
       obj["p_birthdateDay"] = p_birthdateDay;
       obj["p_birthdateMonth"] = p_birthdateMonth;
       obj["p_birthdateYear"] = p_birthdateYear;
       obj["p_genderCode"] = p_genderCode;
       obj["p_tolerance"] = p_tolerance;
       getJSON("005",obj,function(o){searchPersonCB(o,cb);});
    }

    function searchPersonCB(o,cb) {
      if(cb!=undefined)
        cb(o);
    }

    function getPerson(nr,cb) {
      if(PERSONCACHE.IsCached(nr))
        cb(PERSONCACHE.Get(nr));
      else {
        var obj = new Object();
        obj["p_number"] = nr;
        getJSON("003",obj,function(o){getPersonCB(o,cb);});
       }
    }

    function getPersonCB(o,cb) {
      PERSONCACHE.Add(o.Person);
      if(cb!=undefined)
        cb(o.Person);
    }

    //opslaan van de Atperson in KBO
    function saveAtPerson(vnaam,naam,geslacht,gebDat,gebpla,gebla,nat,straat,straatcode,gemeente,land,niscode,huisnr,postcode,busnr,cBack)
    {
      var obj = new Object();
      obj["p_firstName"]=vnaam;
      obj["p_lastName"]=naam;
      obj["p_gender"]=geslacht;
      obj["p_birthDate"]=gebDat;
      obj["p_birthLocation"]=gebpla;
      obj["p_birthCountry"]=gebla;
      obj["p_nationality"]=nat;
      obj["p_streetname"]=straat;
      obj["p_streetcode"]=straatcode;
      obj["p_municipality"]=gemeente;
      obj["p_countryCode"]=land;
      obj["p_nisCode"]=niscode;
      obj["p_houseNumber"]=huisnr;
      obj["p_postcode"]=postcode;
      obj["p_postBox"]=busnr;
      getJSON("159",obj,function(o){saveAtPersonCB(o,cBack);});
    }

    function saveAtPersonCB(o,cb)
    {
      if (cb!=undefined)
      {
        cb(o);
      }
    }

    function saveAtEnt(denom,entType,straat,straatcode,gemeente,land,niscode,huisnr,postcode,busnr,cBack)
    {
      var obj = new Object();
      obj["p_denomination"]=denom;
      obj["p_enterpriseType"]=entType;
      obj["p_streetname"]=straat;
      obj["p_streetcode"]=straatcode;
      obj["p_municipality"]=gemeente;
      obj["p_countryCode"]=land;
      obj["p_nisCode"]=niscode;
      obj["p_houseNumber"]=huisnr;
      obj["p_postcode"]=postcode;
      obj["p_postBox"]=busnr;
      getJSON("156",obj,function(o){saveAtEntCB(o,cBack);});

    }

    function saveAtEntCB(o,cb)
    {
      if (cb!=undefined)
      {
        cb(o);
      }

    }

    function delAtEnt(p_entNr,CB)
    {
      var obj=new Object();
      obj["p_entnumber"]=p_entNr;
      getJSON("158",obj,function(o){DeleteAtEntCB(o,CB);});
    }

    function DeleteAtEntCB (o,cb)
    {
      if (cb!=undefined)
      {
        cb(o);
      }
    }

    function updateAtEnt(p_entNr,type,denom,streetcode,street,huisnr,bus,niscode,pc,munici,land,phone,fax,mail,cb)
    {
      var obj=new Object();
      obj["p_at_ent_number"]=p_entNr;
      obj["p_enterpriseType"]=type;
      obj["p_denomination"]=denom;
      obj["p_streetcode"]=streetcode;
      obj["p_streetname"]=street;
      obj["p_housenumber"]=huisnr;
      obj["p_postbox"]=bus;
      obj["p_niscode"]=niscode;
      obj["p_postcode"]=pc;
      obj["p_municipality"]=munici;
      obj["p_countrycode"]=land;
      getJSON("157",obj,function(o){UpdateAtEntCB(o,cb);});
    }

    function UpdateAtEntCB(o,cb)
    {
      if (cb!=undefined)
      {
        cb(o);
      }
    }

    function DeleteAtPerson(persnr,cb)
    {
      var obj=new Object();
      obj["p_personnumber"]=persnr;
      getJSON("161",obj,function(o){DeleteAtPersonCB(o,cb)});
    }

    function DeleteAtPersonCB(o,cb)
    {
      if (cb!=undefined)
      {
        cb(o);
      }
    }

    function updateAtPerson(nr,vnaam,naam,geslacht,gebdat,gebpla,gebland,nat,straat,straatcode,gemeente,land,niscode,huisnr,postcode,busnr,cb)
    {
      var obj=new Object();
      obj["p_personNumber"]=nr;
      obj["p_firstName"]=vnaam;
      obj["p_lastName"]=naam;
      obj["p_gender"]=geslacht;
      obj["p_birthDate"]=gebdat;
      obj["p_birthLocation"]=gebpla;
      obj["p_birthCountry"]=gebland;
      obj["p_nationality"]=nat;
      obj["p_streetname"]=straat;
      obj["p_streetcode"]=straatcode;
      obj["p_houseNumber"]=huisnr;
      obj["p_postBox"]=busnr;
      obj["p_postCode"]=postcode;
      obj["p_nisCode"]=niscode;
      obj["p_municipality"]=gemeente;
      obj["p_countryCode"]=land;
      getJSON("160",obj,function(o){UpdateAtPersonCB(o,cb)});
    }

    function UpdateAtPersonCB(o,cb)
    {
      if (cb!=undefined)
      {
        cb(o);
      }
    }

    //zoeken naar kaartjes
    function searchcards(ondnr,atOndnr,rrn,Atrrn,cb)
    {
      var obj=new Object;
      obj["p_CBE_Enterprise_number"]=ondnr;
      obj["p_AT_Enterprise_number"]=atOndnr;
      obj["p_Person_number"]=rrn;
      obj["p_AT_Person_number"]=Atrrn;
      getJSON("101",obj,function(o){searchcardsCB(o,cb)});
    }

    function searchcardsCB(o,cb)
    {
      if (cb!=undefined)
      {
        cb(o);
      }
    }

    function AddNewCardKBO(NrCard,NrContinuator,NrPredecessor,NrEnt,NrAtEnt,AtEntType,AtDenomation,NrPerson,FirstName,LastName,NrAtPerson,AtFirstname,AtLastname,TypeCard,Location,OldCard,Application,PrintStatus,NrFO,FODescription,Product,FairAttraction,cb)
    {
       var obj=new Object;
       obj["p_cardnumber"]=NrCard;
       obj["p_numberContinuator"]=NrContinuator;
       obj["p_numberPredecessor"]=NrPredecessor;
       obj["p_ENTnumber"]=NrEnt;
       obj["p_atEnt_number"]=NrAtEnt;
       obj["p_atEnt_type"]=AtEntType;
       obj["p_atDenomination"]=AtDenomation;
       obj["p_person_number"]=NrPerson;
       obj["p_firstName"]=FirstName;
       obj["p_lastName"]=LastName;
       obj["p_person_ATnumber"]=NrAtPerson;
       obj["p_ATfirstName"]=AtFirstname;
       obj["p_ATlastName"]=AtLastname;
       obj["p_CardType"]=TypeCard;
       obj["p_location"]=Location;
       obj["p_oldCard"]=OldCard;
       obj["p_application"]=Application;
       obj["p_printStatus"]=PrintStatus;
       obj["p_FO_Number"]=NrFO;
       obj["p_fo_description"]=FODescription;
       obj["p_product"]=Product;
       obj["p_fairAttraction"]=FairAttraction;
       getJSON("151",obj,function(o){AddNewCardKBOCB(o,cb)});

    }

    function AddNewCardKBOCB(o,cb)
    {
      if (cb!=undefined)
      {
        cb(o);
      }
    }

    


    function getMainAddress(CD) {
      for(var i=0;i<CD.Addresses.length;i++) {
        if(CD.Addresses[i].KBOUsage.Type == "001") {
          return CD.Addresses[i];
        }
      }
      return null;
    }

    function getMainDenomination(CD) {
     try{
      for(var i=0;i<CD.Denominations.length;i++) {
        if(CD.Denominations[i].code == "001") {
          return CD.Denominations[i];
        }
      }
      }
      catch(e){return null};
      return null;
    }

    function getBUAddress(CD) {
      for(var i=0;i<CD.Addresses.length;i++) {
        if(CD.Addresses[i].KBOUsage.Type == "002") {
          return CD.Addresses[i];
        }
      }
      return null;
    }

    function getBUDenom(CD) {
      for(var i=0;i<CD.Denominations.length;i++) {
        if(CD.Denominations[i].code == "003" ||
           CD.Denominations[i].code == "") {
          return CD.Denominations[i];
        }
      }
      return null;
    }

    function getCreator(ent) {
      for(var i=0;i<ent.EntityCommonData.Functions.length;i++) {
        if(ent.EntityCommonData.Functions[i].type = "00001") {
          return ent.EntityCommonData.Functions[i];
        }
      }
      return null;
    }
    
