/*
 * @(#)clock.js
 *
 * Copyright (C) 2005 D.TRIBE, Inc. All rights reserved.
 *
 * THIS SOFTWARE IS THE PROPRIETARY INFORMATION OF D.TRIBE, INC.
 * USE IS SUBJECT TO LICENSE TERMS.
 *
 * @author		Barney Kim
 * @version		$Revision: 1.3 $, $Date: 2005/06/10 02:29:20 $
 */
var MONTH_IN_YEAR=new Array();
MONTH_IN_YEAR[1]="Jan";  MONTH_IN_YEAR[2]="Feb";  MONTH_IN_YEAR[3]="Mar";
MONTH_IN_YEAR[4]="Apr";  MONTH_IN_YEAR[5]="May";  MONTH_IN_YEAR[6]="Jun";
MONTH_IN_YEAR[7]="Jul";  MONTH_IN_YEAR[8]="Aug";  MONTH_IN_YEAR[9]="Sep";
MONTH_IN_YEAR[10]="Oct"; MONTH_IN_YEAR[11]="Nov"; MONTH_IN_YEAR[12]="Dec";

var runID, runID1;

var dspYear,  dspMonth, dspDay;
var dspHours, dspMins,  dspSecs;

var svrYear,  svrMonth, svrDay, svrDiw;
var svrHours, svrMins,  svrSecs;

var othYear,  othMonth, othDay, othDiw;
var othHours, othMins,  othSecs;

/**
 * ÀÌ ÀÚ¹Ù½ºÅ©¸³Æ®¸¦ È£ÃâÇÏ´Â ÆäÀÌÁö¿¡´Â ¾Æ·¡¿Í °°Àº ÆûÀÌ ÀÖ¾î¾ß ÇÑ´Ù.
 *
 * <form name="serverTimeForm">
 * <input type="hidden" name="serverTime" value="200505041213394" />
 * <input type="text" name="displayTime" value="Not Setting"
 *  style="background-color:#ECECEC; border-style:none 0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px" readonly />
 * </form>
 */
function startServerClock() {
	var serverTime = document.serverTimeForm.serverTime.value;
	svrYear  = new Number(serverTime.substring(0, 4));
	svrMonth = new Number(serverTime.substring(4, 6));
	svrDay   = new Number(serverTime.substring(6, 8));
	svrHours = new Number(serverTime.substring(8, 10));
	svrMins  = new Number(serverTime.substring(10,12));
	svrSecs  = new Number(serverTime.substring(12,14));
	svrDiw   = new Number(serverTime.substring(14,15)) - 1;
	_domainClock();
}

function _leapYear(year){
	return ((year%4)==0)?true:false;
}

function _domainClock() {
	var leapYear = _leapYear(svrYear);
	if(svrMins < 0) { svrMins += 60; svrHours--; }
  	if(svrHours<0) { svrHours += 24; svrDay--;	svrDiw--;	if(svrDiw < 0) svrDiw = 6; }
  	if(svrDay<=0) {	svrMonth--;
  		if(svrMonth==1||svrMonth==3||svrMonth==5||svrMonth==7||svrMonth==8||svrMonth==10||svrMonth==12)	svrDay += 31;
 		  else if(svrMonth==4||svrMonth==6||svrMonth==9||svrMonth==11) svrDay += 30;
  		else if(svrMonth==2 && leapYear) svrDay += 29;
  		else if(svrMonth==2 && !leapYear) svrDay += 28;
  		else if(svrMonth<1) { svrYear--; svrMonth = 11;	svrDay = 31; }
  	}
  	if(svrSecs >= 60) { svrSecs -= 60; svrMins++; }
	if(svrMins>=60) { svrMins -= 60; svrHours++; }
  	if(svrHours>=24) { svrHours -= 24; svrDay++; svrDiw++; if(svrDiw > 6) svrDiw = 0; }
    if(svrMonth==1 || svrMonth==3 || svrMonth==5 || svrMonth==7 || svrMonth==8 || svrMonth==10) {
		if(svrDay > 31) { svrDay = 1; svrMonth++; }
	}
	else if(svrMonth==4 || svrMonth==6 || svrMonth==9 || svrMonth== 11){
		if(svrDay>30) { svrDay = 1; svrMonth++; }
	}
	else if(svrMonth==2 && leapYear) {
		if(svrDay>29){ svrDay = 1; svrMonth++; }
	}
	else if(svrMonth==2 && !leapYear) {
		if(svrDay > 28){ svrDay = 1; svrMonth++; }
	}
	else if(svrMonth==12){
		if(svrDay > 31){ svrDay = 1; svrMonth = 0; svrYear++; }
    }

	dspYear  = svrYear;	  dspMonth = svrMonth; dspDay   = svrDay;
	dspHours = svrHours;  dspMins  = svrMins;  dspSecs  = svrSecs;
	dsp=dspMonth + ":" + dspMins + ":" + dspSecs;
	if(runID1 == undefined) {
		document.serverTimeForm.displayTime.value = MONTH_IN_YEAR[dspMonth] + " " + dspDay + ", " + dspYear + " " + dspHours + ":" + dspMins + ":" + dspSecs;
	}
    ++svrSecs;
    clearTimeout(runID);
    runID = setTimeout("_domainClock()", 1000);
}

function startOtherClock(offset) {
	othYear   = svrYear
	othMonth  = svrMonth
	othDay    = svrDay;
	othHours  = svrHours;
	othMins   = svrMins;
	othSecs   = svrSecs;
	othDiw    = svrDiw;
	_interClock(offset);
}

function _interClock(offset) {
	var leapYear = _leapYear(othYear);

	if(offset != undefined) {
		if(offset.charAt(0) == '+')	othHours = othHours + new Number(offset);
		else othHours = othHours + new Number(offset);
	}
	if(othMins < 0) { othMins += 60; othHours--; }
  	if(othHours<0) { othHours += 24; othDay--;	othDiw--;	if(othDiw < 0) othDiw = 6; }
  	if(othDay<=0) {	othMonth--;
  		if(othMonth==1||othMonth==3||othMonth==5||othMonth==7||othMonth==8||othMonth==10||othMonth==12) othDay += 31;
  		else if(othMonth==4||othMonth==6||othMonth==9||othMonth==11) othDay += 30;
  		else if(othMonth==2 && leapYear) othDay += 29;
  		else if(othMonth==2 && !leapYear) othDay += 28;
  		else if(othMonth<1) { othYear--; othMonth = 11; othDay = 31; }
  	}
  	if(othSecs >= 60) { othSecs -= 60; othMins++; }
	if(othMins>=60) { othMins -= 60; othHours++; }
  	if(othHours>=24) { othHours -= 24; othDay++; othDiw++; if(othDiw > 6) othDiw = 0; }
    if(othMonth==1 || othMonth==3 || othMonth==5 || othMonth==7 || othMonth==8 || othMonth==10) {
		if(othDay > 31) { othDay = 1; othMonth++; }
	}
	else if(othMonth==4 || othMonth==6 || othMonth==9 || othMonth== 11){
		if(othDay>30) { othDay = 1; othMonth++; }
	}
	else if(othMonth==2 && leapYear) {
		if(othDay>29){ othDay = 1; othMonth++; }
}
	else if(othMonth==2 && !leapYear) {
		if(othDay > 28){ othDay = 1; othMonth++; }
	}
	else if(othMonth==12){
		if(othDay > 31){ othDay = 1; othMonth = 0; othYear++; }
    }

	dspYear  = othYear;  dspMonth = othMonth; dspDay   = othDay;
	dspHours = othHours; dspMins  = othMins;  dspSecs  = othSecs;
	var dsp=dspMonth + ":" + dspMins + ":" + dspSecs;
	document.serverTimeForm.displayTime.value = MONTH_IN_YEAR[dspMonth] + " " + dspDay + ", " + dspYear + " " + dspHours + ":" + dspMins + ":" + dspSecs;
    ++othSecs;
    clearTimeout(runID1);
    runID1 = setTimeout("_interClock()", 1000);
}
// EOF
