//convert seconds to timeparts in an array
function secToTimeArray(seconds) {
  var timeArray = new Array();
  timeArray[0] = 0;timeArray[1] = 0;timeArray[2] = 0;timeArray[3] = 0;
  // 0=Days, 1=Hours, 2=Minutes, 3=Seconds

  if (seconds > 0) {
    timeArray[0] = Math.floor((seconds/((60*60)*24)));
    timeArray[1] = Math.floor((seconds/(60*60)) - (timeArray[0]*24));
    timeArray[2] = Math.floor((seconds/60) - ((timeArray[1]*60) + (timeArray[0]*(60*24))));
    timeArray[3] = seconds % 60;
  }

  return timeArray;
};

function timeArrayToSecs(timeArray) {
  var secs;
  // 0=Days, 1=Hours, 2=Minutes, 3=Seconds
  secs = (timeArray[0]*60*60*24);
  secs = secs + (timeArray[1]*60*60);
  secs = secs + (timeArray[2]*60);
  secs = secs + timeArray[3];
  return secs;
}


//seconds to format [1:12:04]
function secToTime(seconds) {
  if (seconds>0) {
    return ("[" + secToTime2(seconds, ':') + "]");
  } else {
    return ""
  }
}

//seconds to delimited format
function secToTime2(seconds, delimitor) {
  var timeArray = new secToTimeArray(seconds);
  var returnString = '';

  if (timeArray[1]>0) returnString = (timeArray[1]) + delimitor;
  returnString = returnString + (((timeArray[2]<10)&&(timeArray[1]>0))?"0":"") + timeArray[2] + delimitor;
  returnString = returnString + (((timeArray[3]<10)&&(timeArray[2]>0))?"0":"") + timeArray[3];

  return returnString;
}

//seconds to dateparts
function TimeStringTillNow(seconds, lines) {
  var timeArray = new secToTimeArray(seconds);
  var returnString = '';

  if (timeArray[0]>0) returnString = timeArray[0] + (timeArray[0]>1?' days':' day') + (lines?'<BR>\n':', ');
  if (timeArray[1]>0) returnString = returnString + timeArray[1] + (timeArray[1]>1?' hours':' uur') + (lines?'<BR>\n':', ');
  if (timeArray[2]>0) returnString = returnString + timeArray[2] + (timeArray[2]>1?' minutes':' minuten') + (lines?'<BR>\n':' en ');
  if (timeArray[3]>0) returnString = returnString + timeArray[3] + (timeArray[3]>1?' seconds':' seconden');

  return returnString;
}


// Add seconds to a date
function addSeconds(beginDate, length) {
  var msecs;
  if (length < 0) {
    msecs = 5 * 60 * 1000;
  } else {
    msecs = length * 1000;
  }
  return new Date(beginDate.getTime() + msecs);
};



// current time is before the end date
function beforeEnd(endDate) {
  var currentDate = new Date();

  if (currentDate < endDate) {
    return true;
  }
  return false;
}


function nowUntilDate_ToTimeArray(UntilDate) {
  var first;
  var second;
  if (beforeEnd(UntilDate)) { // Untildate is later then current date, countdown
    first = new Date();
    second = new Date(UntilDate);
  } else {
    first = new Date(UntilDate);
    second = new Date();
  }

  var timeArray = new Array();
  timeArray[0] = 0;timeArray[1] = 0;timeArray[2] = 0;timeArray[3] = 0;
  // 0=Days, 1=Hours, 2=Minutes, 3=Seconds
  //document.write(first + "<br>");
  //document.write(second + "<br>");
  var days = (first - second) / 1000 / 60 / 60 / 24;
  var daysRound = Math.floor(days);
  var hours = (first - second) / 1000 / 60 / 60 - (24 * daysRound);
  var hoursRound = Math.floor(hours);
  var minutes = (first - second) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
  var minutesRound = Math.floor(minutes);
  var seconds = (first - second) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
  var secondsRound = Math.round(seconds);

  timeArray[0] = daysRound;
  timeArray[1] = hoursRound;
  timeArray[2] = minutesRound;
  timeArray[3] = secondsRound;
  return timeArray;
}



