/**
 * (c) InfoProjects bv. - www.infoprojects.nl
 *
 * $Revision: 1.21 $
 * $Date: 2010/12/14 14:12:19 $
 * $Author: jacob $
 */

(function($){
  var newWindowLinkList = new Array();
  $.AddNewWindowLink = function(selector, width, height) {
      var newLink = new Object();
      newLink["selector"] = selector;
      newLink["width"] = width;
      newLink["height"] = height;
      newWindowLinkList[newWindowLinkList.length] = newLink;
    };

  $.ProcessLinkWithProperties = function(link) {
    var attributes = link["attributes"];
    attributes += ',width='+link["width"];
    attributes += ',height='+link["height"];

    var selector = link["selector"];
    if ($(selector).size() == 0 && !selector.contains("#")) {
      selector = "#"+selector;
    }
    $(selector).not(".processed").addClass("processed").click(function() {
      return !window.open($(this).attr("href"), 'popup', attributes);
    });
  };

  $.ProcessPage = function(options) {
      settings = $.extend({
          external: ".externLink",
          newWindowList: newWindowLinkList,
          printButton: ".print",
          newWindowDefaultWidth: "500",
          newWindowDefaultHeight: "200",
          newWindowDefaultPopupAttributes: "status=yes,toolbar=yes,menubar=yes,location=no,scrollbars=yes,resizable=yes"
        }, options||{});
      // Process alle externe links.
      $(settings.external).not(".processed").addClass("processed").click(function() {
        window.open($(this).attr("href"));
        return false;
      });
      // Process alle links die een hoogte en breedte hebben.
      for (var i = 0; i < settings.newWindowList.length; i++) {
        if (settings.newWindowList[i]["width"] == "") {
          settings.newWindowList[i]["width"] = settings.newWindowDefaultWidth;
        }
        if (settings.newWindowList[i]["height"] == "") {
          settings.newWindowList[i]["height"] = settings.newWindowDefaultHeight;
        }
        settings.newWindowList[i]["attributes"] = settings.newWindowDefaultPopupAttributes;
        $.ProcessLinkWithProperties(settings.newWindowList[i]);
      }

      $(settings.printButton).not(".processed").addClass("processed").click(function() {
        print();
        return false;
      });
    };
})(jQuery);

var ProcessPageOptions = null;

$(function() {
  $.ProcessPage(ProcessPageOptions);
  _pageContent.ScrollToBookMark();
  try { initStijlset(); } catch(e) {}
  try { initPagetype(); } catch(e) {}
});

var _pageContent = new pageContent();

function pageContent() {
    // New window default settings
    this.newWindowDefaultWidth = '500';
    this.newWindowDefaultHeight = '200';
    this.newWindowDefaultPopupAttributes = 'status=yes,toolbar=yes,menubar=yes,location=no,scrollbars=yes,resizable=yes';
    this.newWindowLinkList = new Array();
  }

pageContent.prototype.ScrollToBookMark = function() {
    if ($("#"+$("#goto_bookmark").attr("name")).length) {
      $("#"+$("#goto_bookmark").attr("name")).get(0).scrollIntoView(true);
    }
  };

pageContent.prototype.FindNewWindowLink = function(linkId) {
    for (var i = 0; i < this.newWindowLinkList.length; i++) {
      if (this.newWindowLinkList[i][0] == linkId) {
        return this.newWindowLinkList[i];
      }
    }
    return null;
  };

pageContent.prototype.AddNewWindowLink = function(linkId, width, heigth) {
    this.newWindowLinkList[this.newWindowLinkList.length] = { selector: linkId, width: width, height: heigth};
    return this.newWindowLinkList.length - 1;
  };

pageContent.prototype.ProcessExternalLinks = function() {
    if (!document.getElementsByTagName) {
      return;
    }
    var anchors = document.getElementsByTagName('a');
    for(var i=0; i<anchors.length; i++) {
      if(anchors[i].className == 'externLink') {
        anchors[i].onclick = function() {
          return !window.open(this.href);
        };
      }
      if(anchors[i].className == 'print') {
        anchors[i].onclick = function() {
          print();
        };
      }
      if (anchors[i].id.indexOf('link') == 0) {
        if (this.FindNewWindowLink(anchors[i].id) != null) {
          anchors[i].onclick = function() {
            return openLinkInNewWindow(this);
          };
        }
      }
    }
  };

function openLinkInNewWindow(anchor) {
  var link = _pageContent.FindNewWindowLink(anchor.id);
  var attributes = _pageContent.newWindowDefaultPopupAttributes;

  if (link[1] != '')
    attributes += ',width='+link[1];
  else
    attributes += ',width='+_pageContent.newWindowDefaultWidth;

  if (link[2] != '')
    attributes += ',height='+link[2];
  else
    attributes += ',height='+_pageContent.newWindowDefaultHeight;

  window.open(anchor.href, 'popup', attributes);
  return false;
}


function parseDigitString(s) {
  while (s.length>1 && s.substring(0,1)=="0") {
    s=s.substring(1,s.length);
  }
  return (isNaN(parseInt(s,10))?0:parseInt(s,10));
}

function putDigitString(num,digits) {
  num = "" + num;
  while (num.length<digits) {
    num="0" + num;
  }
  return (num);
}

function openWindow(url,attr) {
  popup = window.open(url,"popup",attr);
  return false;
}

function openPopup(url,name,attr) {
  popup = window.open(url,name,attr);
  return false;
}

// --- MOUSE EVENT PROPERTIES ---

var curMouseY = 0;
function getMouseWinTop(e) {
  var posy = 0;
  if (!e) var e = window.event;
  if (e.pageY)
  {
    posy = e.pageY;
  }
  else if (e.clientY)
  {
    posy = e.clientY + document.body.scrollTop;
  }
  curMouseY = posy;
  return posy;
}

function updateXY(e) {
  getMouseWinLeft(e);
  getMouseWinTop(e);
}

var curMouseX = 0;
function getMouseWinLeft(e) {
  var posx = 0;
  if (!e) var e = window.event;
  if (e.pageX)
  {
    posx = e.pageX;
  }
  else if (e.clientX)
  {
    posx = e.clientX + document.body.scrollLeft;
  }
  curMouseX = posx;
  return posx;
}


// -- Datum veld check --
function fieldDateCheck(input, nextEltId, length) {
  try {
    removeCharsFromDateField(input);

    if (input.value.length == length) {
      var elt = document.getElementById(nextEltId);
      if(elt) {
        elt.focus();
      }
    }
  }
  catch (e)
  { }
}

// -- Datum veld opschoner, verwijderd alle niet cijfers --
function removeCharsFromDateField(input) {
  try {
    var pos = getCaretPosition(input);
    var regex = new RegExp("[^0-9]", "g");
    input.value = input.value.replace(regex, "");
    setCaretPosition(input, input.value.length <= pos ? pos : input.value.length);
  }
  catch (e)
  { }
}

function hideSelectBoxes(bool) {
  bool?$('select').hide():$('select').show();
}

function showCalendar(curDate, fieldId) {
  // prototype js needed!!!
  hideSelectBoxes(true);
  var url = '/aspx/get.aspx?xdl=/views/global/xdl/calendar&fieldName=' + fieldId + '&calendar_date=' + curDate;
  $('#'+fieldId + '_calendar').load(url);
}

function selectCurrentDate(date, fieldId) {
  $('#'+fieldId + '_d').attr("value", date.substring(6,8));
  $('#'+fieldId + '_m').attr("value", date.substring(4,6));
  $('#'+fieldId + '_y').attr("value", date.substring(0,4));
  closeCalendar(fieldId);
}

function selectCurrentDay(id) {
  $('#'+id).toggleClassName('selected');
}

function closeCalendar(fieldId) {
  hideSelectBoxes(false);
  $("#"+fieldId + '_calendar').html('');
}

function getCaretPosition (ctrl) {
  var CaretPos = 0;
  // IE Support
  if (document.selection) {
    ctrl.focus ();
    var Sel = document.selection.createRange ();
    Sel.moveStart ('character', -ctrl.value.length);
    CaretPos = Sel.text.length;
  }
  // Firefox support
  else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
    CaretPos = ctrl.selectionStart;
    return (CaretPos);
  }
}

function setCaretPosition(ctrl, pos) {
  if (ctrl.setSelectionRange) {
    ctrl.focus();
    ctrl.setSelectionRange(pos,pos);
  }
  else if (ctrl.createTextRange) {
    var range = ctrl.createTextRange();
    range.collapse(true);
    range.moveEnd('character', pos);
    range.moveStart('character', pos);
    range.select();
  }
}

