/*
 * util.js
 * (c) Jeroen Peters
 *
 * Diverse functies
 */
 
    
function padL(str, padChar, length) {
  result = new String(str);
  while (result.length < length) {
    result = padChar + result;
  }
  return result;
}

function getTimeZone(iOffsetMin) {
  iOffsetMin = iOffsetMin * -1;
  if (iOffsetMin != 0) {
    var sTzHour = new String(parseInt(iOffsetMin /  60)); 
    var sTzMin  = new String(iOffsetMin % 60);
    var sSign = '+';

    if (iOffsetMin < 0) {
      sSign = '-';
      sTzHour = sTzHour * -1;
    }
      
    result = ' ' + sSign + padL(sTzHour, '0', 2) + padL(sTzMin, '0', 2);  
  } else 
    result = '';
    
  return result;
}
function getDocumentDate(sDocument, aMonth) {
  var dModDate = new Date(sDocument.lastModified);
  
  var result = new JDate( dModDate.getDate(), aMonth[dModDate.getMonth()], dModDate.getFullYear(),
    dModDate.getHours() + ':' + padL(dModDate.getMinutes(), '0', 2), 
    getTimeZone(dModDate.getTimezoneOffset()) );
  
  return result;
}

function lastModified(sDocument) {
  var aMonth = new Array('januari', 'februari', 'maart', 'april', 'mei', 'juni',
    'juli', 'augustus', 'september', 'oktober', 'november', 'december');

  var result = getDocumentDate(sDocument, aMonth);
  return result.getDateString();
}

function lastModified_en(sDocument) {
  var aMonth = new Array('January', 'February', 'March', 'April', 'May', 'June',
    'July', 'Augustus', 'September', 'October', 'November', 'December');

  var result = getDocumentDate(sDocument, aMonth);
  return result.getDateString_en();
}

function JDate(sDay, sMonth, sYear, sTime, sTz) {
  this.sDay = new String(sDay);
  this.sMonth = new String(sMonth);
  this.sYear = new String(sYear);
  this.sTime = new String(sTime);
  this.sTz = new String(sTz);

  this.getDateString = getDateString;
  this.getDateString_en = getDateString_en;
  this.illegalDate = illegalDate;
}

function getDateString() {
  if (this.illegalDate())
    result = 'Onbekend';
  else
    result = this.sDay + ' ' + this.sMonth + ' ' + this.sYear + ', ' + this.sTime;
    
  return result;
}

function getDateString_en() {
  if (this.illegalDate())
    result = 'Unknown';
  else
    result = this.sMonth + ' ' + this.sDay + ', ' + this.sYear + ', ' + this.sTime + ' (GMT' + this.sTz + ')';
  
  return result;
}

function illegalDate() {
  result =  (this.sDay.search('NaN') != -1) & (this.sMonth.search('NaN') != -1) || 
    (this.sYear.search('NaN') != -1) || (this.sTime.search('NaN') != -1) || (this.sTz.search('NaN') != -1);
    
  return result;
}