﻿Type.registerNamespace('Independer.Finance.Hypotheek.Controls');

// The class constructor
Independer.Finance.Hypotheek.Controls.ICMControl = function (state) {
  this._state = state;
  this.numOfYears = 1;
  this.currentLastMonth = 12;
  this.monthOptions = $get(this._state.controls.adviesMaandDropdown).options;
  this.monthOptionArray = [this.monthOptions[0],
            this.monthOptions[1], this.monthOptions[2], this.monthOptions[3], this.monthOptions[4],
            this.monthOptions[5], this.monthOptions[6], this.monthOptions[7], this.monthOptions[8],
            this.monthOptions[9], this.monthOptions[10], this.monthOptions[11], this.monthOptions[12]];

  this.ShowHideShadow($get(this._state.controls.omschrijvingTextBox));
  this.ShowHideShadow($get(this._state.controls.toelichtingTextBox));

  this.FillMaandDropdown();
  if (document.URL.indexOf('check-uw-hypotheekadviseur.aspx', 0) != -1) {
    var kantorBox = $get("KantorenTBody");
    kantorBox.className = "ResultaatBody";
  }
  this.setVisible($get("KantorenTFoot"), false);
  this.setVisible($get("KantorenTFootTop"), false);
  this.setVisible($get("zoekAdviseurBox"), false);
  this.setVisible($get(this._state.controls.adviseurToevoegen), false);
  if ($get(this._state.controls.KantoorId).value != "")
    this.selectKantoor($get(this._state.controls.KantoorId).value);

  //handles situation when kantoor is preselected on advieskantoor.aspx
  if ($get("hdnMode").innerHTML.match("ICMSelectedOffice") != null) {
    $get(this._state.controls.zoekOpKantoorNaamTextBox).value = $get("hdnName").innerHTML;
    $get("hdnName").innerHTML = "";
    var tempId = $get("hdnId").innerHTML.replace(new RegExp("\\n", "g"), "")
    this.selectKantoor(tempId);
    this.setVisible($get("selectedOfficeName"), true);
    $get("KantoorResultaatTabel").style.display = "none";
    $get("kantoorToevoegenDiv").style.display = "none";

  }
  if ($get("hdnMode").innerHTML.match("Extranet") != null) {
    $get("adviesJaarMaandDiv").style.display = "none";
    $get("kantoorToevoegenDiv").style.display = "none";
    $get("adviseurToevoegenDiv").style.display = "none";
    $get("divAddKantoor").style.display = "none";
  }

}

// All the methods of the class
Independer.Finance.Hypotheek.Controls.ICMControl.prototype = {
  ShowHideShadow: function (textbox) {
    if (textbox == null) return;
    if (textbox.value == '')
      this.nextElement(textbox).style.visibility = 'visible';
    else
      this.nextElement(textbox).style.visibility = 'hidden';
  },

  addOption: function (dropdown, text, value) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    dropdown.options.add(optn);
  },

  FillMaandDropdown: function () {
    var today = new Date();
    var startDate = new Date(today.getFullYear() - this.numOfYears, today.getMonth() , 1);
    var endDate = new Date(today.getFullYear(), today.getMonth() + 1, 1);
    var adviesJaar = $get(this._state.controls.adviesJaarDropdown).value;
    var startAdviesDate = new Date(adviesJaar, 0, 1);
    var endAdviesDate = new Date(adviesJaar, 11, 31);

    var firstmMonth = 1;
    var lastMonth = 12;
    if (startAdviesDate <= startDate)
      firstmMonth = startDate.getMonth()+1;
    if (endAdviesDate >= endDate)
      lastMonth = endDate.getMonth()+1;

    var maandDropdown = $get(this._state.controls.adviesMaandDropdown);

    maandDropdown.options.length = 0;
    for (var i = firstmMonth; i <= lastMonth; i++)
      maandDropdown.options.add(this.monthOptionArray[i]);
    this.currentLastMonth = lastMonth;
  },

  nextElement: function (element) {
    element = element.nextSibling;
    while (element.nodeType != 1)
      element = element.nextSibling;
    return element;
  },
  previousElement: function (element) {
    element = element.previousSibling;
    while (element.nodeType != 1)
      element = element.previousSibling;
    return element;
  },

  show: function (e) { e.className = e.className.replace(/hidden/g, "").replace(/  /g, " "); },
  hide: function (e) { e.className = e.className + " hidden"; },
  setVisible: function (e, visible) { if (visible) this.show(e); else this.hide(e); },

  checkText: function () {
    var kantoorNaam = $get(this._state.controls.zoekOpKantoorNaamTextBox);
    if (kantoorNaam.value.replace(/\s/g, "") == "") {
      _icmcontrol.setVisible($get("SearchStringError"), true);
      return false;
    }
    else {
      _icmcontrol.setVisible($get("SearchStringError"), false);
      return true;
    }

  },
  disableSearchResults: function () {
    var tbody = $get("KantorenTBody");
    _icmcontrol.ClearRows(tbody);
  },
  zoekKantoorDelayed: function () {
    if (this.zoekKantoorTimer) {
      clearTimeout(this.zoekKantoorTimer);
    }
    this.zoekKantoorTimer = setTimeout("_icmcontrol.zoekKantoor();", 500);
  },

  zoekKantoor: function () {
    if (this.zoekKantoorTimer) clearTimeout(this.zoekKantoorTime);
    var kantoorNaam = $get(this._state.controls.zoekOpKantoorNaamTextBox);
    $get(this._state.controls.zoekOpKantoorNaamTextBox)
    if (_icmcontrol.checkText()) {


      Independer.Finance.Hypotheek.CodeLib.ServiceProxy.ZoekKantoren(kantoorNaam.value,
            function (arg) {
              _icmcontrol.setVisible($get("KantorenTFoot"), arg.MoreItems);
              _icmcontrol.setVisible($get("KantorenTFootTop"), arg.MoreItems);

              var tbody = $get("KantorenTBody");
              _icmcontrol.ClearRows(tbody);
              var selectedKantoorId = $get(_icmcontrol._state.controls.KantoorId).value;
              if (selectedKantoorId == "") selectedKantoorId = -1;
              var inList = false;
              if (arg.Kantoren.length == 0) {
                _icmcontrol.setVisible($get("SearchError"), true);
                var messageText = "Het kantoor [invoer] wordt niet herkend. Controleer op spelfouten a.u.b. Klik vervolgens opnieuw op de button 'zoek kantoor'. ";
                $get("SearchError").innerHTML = messageText.replace("[invoer]", '"' + kantoorNaam.value + '"');
              }
              else { _icmcontrol.setVisible($get("SearchError"), false); }
              if (arg.Kantoren.length > 10) {
                _icmcontrol.setVisible($get("divAddKantoor"), true);

              }
              else {
                _icmcontrol.setVisible($get("divAddKantoor"), false);

              }
              if ($get("hdnMode").innerHTML.match("Extranet") != null) {
                for (var i = 0; i < arg.Kantoren.length; i++) {
                  var k = arg.Kantoren[i];
                  _icmcontrol.AddRow(tbody, "<span class='Cell NaamKantoorCell'><input id='kantoor" + k.Id + "' type='radio' name='kantoor' " + (k.Id == selectedKantoorId ? "checked='true'" : "") + " value='" + k.Id + "' onchange='_icmcontrol.selectkantoorid(this.value);' onclick='_icmcontrol.selectkantoorid(this.value);' /><label for='kantoor" + k.Id + "'>" + k.Naam + "</label></span><span class='Cell PlaatsCell'><label>" + k.Plaats + "</label></span><span class='Cell StraatCell'>" + k.Straat + "</span><span class='Cell HuisnummerCell'>" + k.Huisnummer + "</span><br>");
                  if (k.Id == selectedKantoorId) inList = true;
                }
                if (!arg.MoreItems) {
                  _icmcontrol.AddRow(tbody, "<span class='Cell NieuwKantoorCell'><input id='nieuwKantoorRadioButton' type='radio' name='kantoor' " + (selectedKantoorId == 0 ? "checked='true'" : "") + " value='0' onchange='_icmcontrol.selectkantoorid(0);' onclick='_icmcontrol.selectkantoorid(0);' />Kantoor niet in lijst, kantoor toevoegen</span><br>");
                  if (selectedKantoorId == 0) inList = true;
                }
              }
              else {
                for (var i = 0; i < arg.Kantoren.length; i++) {
                  var k = arg.Kantoren[i];
                  _icmcontrol.AddRow(tbody, "<span class='Cell NaamKantoorCell'><input id='kantoor" + k.Id + "' type='radio' name='kantoor' " + (k.Id == selectedKantoorId ? "checked='true'" : "") + " value='" + k.Id + "' onchange='_icmcontrol.selectKantoor(this.value);' onclick='_icmcontrol.selectKantoor(this.value);' /><label for='kantoor" + k.Id + "'>" + k.Naam + "</label></span><span class='Cell PlaatsCell'><label>" + k.Plaats + "</label></span><span class='Cell StraatCell'>" + k.Straat + "</span><span class='Cell HuisnummerCell'>" + k.Huisnummer + "</span><br>");
                  if (k.Id == selectedKantoorId) inList = true;
                }
                if (!arg.MoreItems) {
                  _icmcontrol.AddRow(tbody, "<span class='Cell NieuwKantoorCell'><input id='nieuwKantoorRadioButton' type='radio' name='kantoor' " + (selectedKantoorId == 0 ? "checked='true'" : "") + " value='0' onchange='_icmcontrol.selectKantoor(0);' onclick='_icmcontrol.selectKantoor(0);' />Kantoor niet in lijst, kantoor toevoegen</span><br>");
                  if (selectedKantoorId == 0) inList = true;
                }

                if (inList)
                  _icmcontrol.selectKantoor(selectedKantoorId);
                else {
                  _icmcontrol.selectKantoor("");
                }
              }
            },
            function (e) {
              alert('Zoeken van kantoren is mislukt. Probeer het later nog eens.');
            });
    }
  },

  RaiseEvent: function (element, eventname) {
    if (element.dispatchEvent) {
      var e = document.createEvent('HTMLEvents');
      e.initEvent(eventname.toLowerCase(), false, false);
      element.dispatchEvent(e);
    }
    else if (element.fireEvent) {
      element.fireEvent('on' + eventname);
    }
  },

  ClearRows: function (body) {
    while (body.firstChild)
      body.removeChild(body.firstChild);
  },

  AddRow: function (body, rowHtml) {
    var row = document.createElement("span");
    //row.setAttribute("class", "ResultaatRij");
    row.innerHTML = rowHtml;
    body.appendChild(row);
    row.className = "ResultaatRij";
    body.appendChild(document.createElement("br"));
  },

  selectkantoorid: function (id) {
    document.getElementById("selectedIdKantoor").value = id;
    $get(this._state.controls.KantoorId).value = id;
    this.RaiseEvent($get(this._state.controls.KantoorId), "Change");
  },

  selectKantoor: function (id) {
    $get(this._state.controls.KantoorId).value = id;
    if (id == "") id = -1;
    this.setVisible($get("zoekAdviseurBox"), id > 0);
    this.RaiseEvent($get(this._state.controls.KantoorId), "Change");

    if (id >= 0 && $get("hdnId").innerHTML == "") {
      _icmcontrol.setVisible($get("KantorenTFoot"), false);
      _icmcontrol.setVisible($get("KantorenTFootTop"), false);
    }

    if (id > 0) {
      Independer.Finance.Hypotheek.CodeLib.ServiceProxy.ZoekAdviseurs(id,
                function (arg) {
                  var tbody = $get("AdviseursTBody");
                  _icmcontrol.ClearRows(tbody);
                  var selectedAdviseurId = $get(_icmcontrol._state.controls.AdviseurId).value;
                  var inList = selectedAdviseurId == 0;
                  for (var i = 0; i < arg.Adviseurs.length; i++) {
                    var a = arg.Adviseurs[i];
                    _icmcontrol.AddRow(tbody, "<span class='Cell NaamAdviseurCell'><input type='radio' name='adviseur' " + (a.Id == selectedAdviseurId ? "checked='true'" : "") + " value='" + a.Id + "' onchange='_icmcontrol.selectAdviseur(this.value)' onclick='_icmcontrol.selectAdviseur(this.value)' />" + a.Naam + "</span>");
                    if (a.Id == selectedAdviseurId) inList = true;
                  }
                  _icmcontrol.AddRow(tbody, "<span class='Cell'><input id='nieuwAdviseurRadioButton' type='radio' name='adviseur' " + (selectedAdviseurId == "0" ? "checked='true'" : "") + " value='0' onchange='_icmcontrol.selectAdviseur(0)' onclick='_icmcontrol.selectAdviseur(0)' />Adviseur niet in lijst, adviseur toevoegen</span>");
                  if (!inList) $get(_icmcontrol._state.controls.AdviseurId).value = "";
                },
                function (e) {
                  alert('Zoeken van adviseurs is mislukt. Probeer het later nog eens.');
                });
    }
    else {
      this.ClearRows($get("AdviseursTBody"));
      $get(this._state.controls.AdviseurId).value = "";
    }
  },

  selectAdviseur: function (id) {
    //this.setVisible($get(this._state.controls.adviseurNaamInvoerControl), id == 0);
    $get(this._state.controls.AdviseurId).value = id;
    this.RaiseEvent($get(this._state.controls.AdviseurId), "Change");
  }
}

// Add IDisposable support
Independer.Finance.Hypotheek.Controls.ICMControl.registerClass("Independer.Finance.Hypotheek.Controls.ICMControl", null, Sys.IDisposable);

// Notify the application that the script has completed.
if (typeof (Sys) !== "undefined")
    Sys.Application.notifyScriptLoaded();
        

