﻿
//Tham so
if (typeof (BackColor) == "undefined")
    BackColor = "white";
if (typeof (ForeColor) == "undefined")
    ForeColor = "black";
if (typeof (TargetDate) == "undefined")
    TargetDate = "08/14/2010 05:30 PM";
if (typeof (TextColor) == "undefined")
    TextColor = "#0000FF"
if (typeof (NumberColor) == "undefined")
    NumberColor = "#FF0000"    
if (typeof (DisplayFormat) == "undefined")
    DisplayFormat = "<center><table border='0' cellspacing='1' cellpadding='4' style='color:#0000FF; font-weight:bold'>"
                    + "<tr>"
                    + "<td align='center'>%%D%% </td>"
					+ "<td> ngày </td>"
                    + "<td align='center'>%%H%% </td>"
                    + "<td> giờ </td>"
                    + "<td align='center'>%%M%% </td>"
                    + "<td> phút </td>"
                    + "<td align='center'>%%S%% </td>"
					+ "<td> giây </td>"
                    + "</tr>"
                    + "</table></center>";

if (typeof (CountActive) == "undefined")
    CountActive = true;
if (typeof (CountStepper) != "number")
    CountStepper = -1;
if (typeof (LeadingZero) == "undefined")
    LeadingZero = true;


CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
    CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper) - 1) * 1000 + 990;

//Function
function calcage(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (LeadingZero && s.length < 2)
    s = "0" + s;
  return s;
}

function CountBack(objCount, secs, FinishMessage) {
  if (secs < 0) {
      document.getElementById(objCount).innerHTML = FinishMessage;
    return;
  }
  DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
  DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
  DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
  DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));

  document.getElementById(objCount).innerHTML = DisplayStr;
  var sec = secs + CountStepper;
  if (CountActive)
    setTimeout("CountBack('" + objCount + "'," + sec + ",'" + FinishMessage + "')", SetTimeOutPeriod);
}


function convertSecond(nowDate, targetDate, CountStepper) {
    var dthen = new Date(targetDate);
    var dnow = new Date(nowDate);
    var ddiff;
    var gsecs;
    if (CountStepper > 0) {
        ddiff = new Date(dnow - dthen);
    }
    else {
        ddiff = new Date(dthen - dnow);
    }
    gsecs = Math.floor(ddiff.valueOf() / 1000);
    return gsecs;
}

//Modify
function CountDownEvent(objCount, nowDate, targetDate, FinishMessage) {
    var gsecs = convertSecond(nowDate, targetDate, CountStepper);
    CountBack(objCount, gsecs, FinishMessage);
}

var fader = new Array();
 
function CountDown(objName,objCount) {
    //var name = document.getElementById(objName);
	//name.innerHTML = "<span style='font-size:20px; color:#0000FF; font-weight:bold'>Đêm Thánh Ca mừng sinh nhật Khúc Cảm Tạ</span>";
	CountDownEvent(objCount, new Date(), "8/14/2010 17:30", "Mừng sinh nhật Khúc Cảm Tạ");	
}


