@set @DEBUG = 0
if (typeof(makeDayOfWeek) == "undefined"){
    function wdPack(jday, eday){
        this.Jday = jday;   // JP
        this.Eday = eday;   // EN
    }
    function makeDayOfWeek(){
        var i = 0;
        this[i++] = new wdPack( "日", "Sun" ) ; // 0
        this[i++] = new wdPack( "月", "Mon" ) ; // 1
        this[i++] = new wdPack( "火", "Tue" ) ; // 2
        this[i++] = new wdPack( "水", "Wed" ) ; // 3
        this[i++] = new wdPack( "木", "Thu" ) ; // 4
        this[i++] = new wdPack( "金", "Fri" ) ; // 5
        this[i++] = new wdPack( "土", "Sat" ) ; // 6
        this.length = i;
    }
}
var maxCities = 2; // max number of sound files
function makeCityJ(){
    var i = 0;
    this[i++] = "クライストチャーチ";
    this[i++] = "東京";
    this[i++] = "英語表示";
    this[i] = "";
    this.length = i;
}
function makeCityE(){
    var i = 0;
    this[i++] = "Christchurch";
    this[i++] = "Tokyo";
    this[i++] = "JAPANESE";
    this[i] = "";
    this.length = i;
}
function makeTIDS(){
    var i = 0;
    this[i++] = "idTime1";
    this[i++] = "idTime2";
    this[i++] = "SUMMAR";
    this[i] = "";
    this.length = i;
}
function todayColor(){
    var t   = new Date();
    var day = t.getDay();
    var BC  = new makeBackColor();
    var TC  = new makeTextColor();
    with(oBody){    // defined <body id="oBody" ....
        runtimeStyle["background"] = BC[day];
        runtimeStyle["color"] = TC[day];
    }
}
function fnDetermTodayMsg(){
}
var JPL = new makeCityJ();
var NZL = new makeCityE();
function makeMSGS(){
    var oMsg = new Array();
    var oTemp = fnGetProVerb();
    oMsg[0] = oTemp.EPV;
    oMsg[1] = oTemp.JPV;
        oTemp = fnGetProVerb();
    oMsg[2] = oTemp.EPV;
    oMsg[3] = oTemp.JPV;
    var i = 0;
    this[i] = "<MARQUEE scrolldelay='220'>" + oMsg[i++] + "</MARQUEE>";
    this[i] = "<MARQUEE scrolldelay='220'>" + oMsg[i++] + "</MARQUEE>";
    this[i] = "<MARQUEE scrolldelay='220'>" + oMsg[i++] + "</MARQUEE>";
    this[i] = "<MARQUEE scrolldelay='220'>" + oMsg[i++] + "</MARQUEE>";
    this.length = i;
}
function makeHEADS(){
    var i = 0;
    this[i++] = "都市";
    this[i++] = "時間";
    this[i++] = "備考";
    this[i++] = "City";
    this[i++] = "Times";
    this[i++] = "Remarks";
    this[i] = "";
    this.length = i;
}
function makeHeaders(){
    var i = 0;
    this[i++] = "TROW1";
    this[i++] = "TROW2";
    this[i++] = "TROW3";
    this[i++] = "TDIFF";
    this[i++] = "CITY";
    this[i++] = "TIMES";
    this[i++] = "REM";
    this[i++] = "idTime1T";
    this[i++] = "idTime2T";
    this[i++] = "idTime1";
    this[i++] = "idTime2";
    this[i++] = "SUMMAR";
    this.length = i;
}
function makeCAPS(){
    var i = 0;
    this[i++] = JPL[0];
    this[i++] = JPL[1];
    this[i++] = NZL[0];
    this[i++] = NZL[1];
    this[i] = "";
    this.length = i;
}
function makeRmks(){
    var i = 0;
    this[i++] = "夏季時間実施中";
    this[i++] = "Daylight Saving Times";
    this[i++] = "冬季時間実施中";
    this[i++] = "Non-Daylight Saving Times";
    this[i] = "";
    this.length = i;
}

var TIDS        = new makeTIDS();
var strMsgD     = new makeMSGS();
var strCaps     = new makeCAPS();
var strRmks     = new makeRmks();
var strHeads    = new makeHEADS();
var HEADS       = new makeHeaders();
var DaysOfWeek  = new makeDayOfWeek();

var TKYSTD      =  -9;
var CHSTD1      = -12;  // Winter Time
var CHSTD2      = -13;  // Summar Time
var OHOUR       =  60;  // 1 Hour
var MARCH       =   2;  // as March
var OCTOR       =   9;  // as October
var CMARCH      =   3;  // Mrach (3)
var COCTOBER    =  10;  // October (3)
var OWEEK       =   7;  // One Week Period
var SUNDAY      =   0;  // Sunday (0, 1, 2....6:Sat)
var WINCHANGE   =   3;  // Summer --> Winter Change Week (3rd Sunday in March)
var SUMCHANGE   =   1;  // Winter --> Summer Change Week (1st Sunday in October)
var TWO_OCLOCK  =   2;  // Time Changing Time
var NOT_THE_DAY =   0;  // To be used for Sum_Win_Flag on the changing day
var SUMMAR_TIME =   1;  // To be used for Sum_Win_Flag on the changing day
var WINTER_TIME =   2;  // To be used for Sum_Win_Flag on the changing day
var tz_nz;
var tz_jp;
var time_diff;
var nz, jp;
var bl;
var sl;
var ul;
var summarFlag   = false;
var Sum_Win_Flag = NOT_THE_DAY; // Set the default flag: 0

function getFirstSunday(thisYear, thisMonth){
    // thisYear: xxxx (eg. 2003)
    // thisMonth: 0 - 11 (Jan. - Dec.) eg. 2 as March
    var d, dt;

    for (dt=1; dt<=OWEEK; dt++){    // Examine 1st day to 7th day to determine 1st Sunday
        d = new Date(thisYear, thisMonth, dt);
        if (d.getDay() == SUNDAY) return(dt);   // Matched, return the Day
    }
}
function getNthDayOfMonth(thisYear, thisMonth, thisDay, theNth){
    @if (@DEBUG == 1) alert("getNthDayOfMonth>> IN"); @end
    // thisYear: xxxx (eg. 2003)
    // thisMonth: 0 - 11 (Jan. - Dec.) eg. 2 as March
    // thisday:   0 - 6 (Sun - Sat)
    // theNth:    1 - 5;

    if ((1 > theNth) || (theNth > 5)) theNth = 1;
    var lastDay = 31;
    switch(thisMonth){
        case  3:    // April
        case  5:    // June
        case  8:    // September
        case 10:    // November
            lastDay = 30;
            break;
        case  1:    // Feburary
            lastDay = (((thisYear % 4 == 0) && (thisYear % 100 != 0)) || (thisYear % 400 == 0)) ? 29 : 28;
            break;
        default:
            break;
    }
    var d, dt, Nth = 0;
    var saveDay;

    for (dt=1; dt<=lastDay; dt++){
        d = new Date(thisYear, thisMonth, dt);
        if (d.getDay() == thisDay){
            if (Nth == 0) saveDay = dt; //  Save as Default Day(first day)
            if (++Nth == theNth){
                return(dt); // Matched, return the Day
            }
        }
    }
    return(saveDay);
}

function InitZone(){
    var t, mon, day, dt, hour, year, x, strMsg;
    tz_jp   = TKYSTD * OHOUR;   // -540 (constant)
    t       = new Date();
    year    = t.getFullYear();  // yyyy
    mon     = t.getMonth() + 1; // 1 - 12
    dt      = t.getDate();      // 1 - 31
    day     = t.getDay();       // 0:Sun - 6:Sat
    hour    = t.getHours();     // 0 - 23
    if ((CMARCH < mon) && (mon < COCTOBER)){        // Winter Time Zone: April - September
        tz_nz = CHSTD1 * OHOUR;
    } else if ((CMARCH > mon) || (mon > COCTOBER)){ // Summar Time Zone: November - February
        tz_nz = CHSTD2 * OHOUR;
    } else if (mon == CMARCH){                      // If March: Summar --> Winter
        WINSTART = getNthDayOfMonth(year, MARCH, SUNDAY, WINCHANGE);    // Determine Date-Change Day
        if (dt > WINSTART){
            tz_nz = CHSTD1 * OHOUR;                 // Already Winter Time(after Nth Sunday in March)
        } else if ((day == SUNDAY) && (dt == WINSTART)){    // Nth Sunday: The day the time to be changed
            tz_nz = (hour < TWO_OCLOCK) ? CHSTD2 * OHOUR : CHSTD1 * OHOUR;  // at 2:00 o'clock -1 hour
            if (hour < TWO_OCLOCK) Sum_Win_Flag = WINTER_TIME;  // Today is the changing day (Summar --> Winter) !!
        } else {
            tz_nz = CHSTD2 * OHOUR;                 // Still Summar Time
        }
    } else {                                        // October: Winter ---> Summar
        SUMSTART = getNthDayOfMonth(year, OCTOR, SUNDAY, SUMCHANGE);    // Determine Date-Change Day
        if (dt > SUMSTART){
            tz_nz = CHSTD2 * OHOUR;                 // Already Summar Time(after Nth Sunday in October)
        } else if ((day == SUNDAY) && (dt == SUMSTART)){    // Nth Sunday
            tz_nz = (hour < TWO_OCLOCK) ? CHSTD1 * OHOUR : CHSTD2 * OHOUR;  // at 2:00 o'clock +1 hour
            if (hour < TWO_OCLOCK) Sum_Win_Flag = SUMMAR_TIME;  // Today is the changing day (Winter --> Summer) !!
        } else {
            tz_nz = CHSTD1 * OHOUR;                 // Still Winter Time
        }
    }
    summarFlag = (tz_nz == (CHSTD2 * OHOUR)) ? true : false;
    time_diff  = (tz_jp - tz_nz)/OHOUR + ":00";
    x = F[ "SUMMAR" ];
    if (x != null){
        if (summarFlag){
            strMsg = (sl == "ja") ? strRmks[0] : strRmks[1];
        } else {
            strMsg = (sl == "ja") ? strRmks[2] : strRmks[3];
        }
        x.innerText = strMsg;
    }
    x = F["NZST"];
    if (x != null){
        x.innerText = (summarFlag) ? "13:00" : "12:00";
    }
}

function InitFont(){
    with(navigator){
        bl = browserLanguage;
        sl = systemLanguage;
        ul = userLanguage;
    }
    if (sl == "ja"){
        for (i=0, j=0; i<F.length; i++){
            if (F[i].className == "NZ"){
                F[i].className = "JP";
                F[i].innerText = JPL[j++];
            } else if (F[i].className == "NTIME"){
                F[i].className = "JTIME";
            }
        }
    } else {
        for (i=0, j=0; i<F.length; i++){
            if (F[i].className == "JP"){
                F[i].className = "NZ";
                F[i].innerText = NZL[j++];
            } else if (F[i].className == "JTIME"){
                F[i].className = "NTIME";
            }
        }
    }
}

function ChangeFont(){
   var x, i, j, k, l;
    var jpFlag = false;
    for (i=0, j=0, k=0, l=0; i<F.length; i++){
        if (F[i].className == "JP"){
            F[i].className = "NZ";
            F[i].innerText = NZL[j++];
        }
        else if (F[i].className == "NZ"){
            F[i].className = "JP";
            F[i].innerText = JPL[j++];
        }
        else if (F[i].className == "JTIME"){
            F[i].className = "NTIME";
            with(F){
                idTime1.size = 24;
                idTime2.size = 24;
            }
        }
        else if (F[i].className == "NTIME"){
            F[i].className = "JTIME";
            with(F){
                idTime1.size = 30;
                idTime2.size = 30;
            }
        }
        else if (F[i].className == "CAPJ"){
            F[i].className = "CAPN";
            F[i].innerText = strHeads[k + 3];
            k ++;
        }
        else if (F[i].className == "CAPN"){
            F[i].className = "CAPJ";
            F[i].innerText = strHeads[k + 0];
            k ++;
        }
        else if (F[i].className == "JPS"){
            F[i].className = "NZS";
            F[i].innerText = (summarFlag)? strRmks[l+1] : strRmks[l+3];
            l ++;
        }
        else if (F[i].className == "NZS"){
            F[i].className = "JPS";
            F[i].innerText = (summarFlag)? strRmks[l+0] : strRmks[l+2];
            l ++;
        }
    }
}

function getIndex( idName ){
    var i;
    for (i=0; i<TIDS.length; i++){
        if (idName == TIDS[i]) return(i);
    }
    return(0);
}

function showHeader(flag){
    var x;
    var i = 0;
    var max = (flag) ? 9 : HEADS.length;
    for (i=0; i<max; i++){
        x = F[ HEADS[i] ]; if (x != null) x.style.display = (flag) ? "" : "none";
    }
}

function showTimes( idName ){
    var e = F[idName];
    var i = getIndex(idName);
    if (e != null) e.style.display = "";
    e = F[idName + "T"];
    if (e != null) e.innerHTML = (e.className == "JP") ? strMsgD[i] : strMsgD[i+2];
    if (idName == "idTime1"){
        e = F[ "SUMMAR" ];
        if (e != null){
            if (e.className == "JPS"){
                e.innerText = (summarFlag) ? strRmks[0] : strRmks[2];
            } else {
                e.innerText = (summarFlag) ? strRmks[1] : strRmks[3];
            }
            e.style.display = "";
        }
    }
}

function hideTimes( idName ){
    var e = F[idName];
    var i = getIndex(idName);
    if (e != null) e.style.display = "none";
    e = F[idName + "T"];
    if (e != null) e.innerHTML = (e.className == "JP") ? strCaps[i] : strCaps[i+2];
    if (idName == "idTime1"){
       e = F[ "SUMMAR" ];
       if (e != null) e.style.display = "none";
    }
}

function update_watch(){
    var now = new Date();
    if (Sum_Win_Flag != NOT_THE_DAY){       // != 0
        if (now.getHours() >= TWO_OCLOCK){  // if 02:00 AM and later
            if (Sum_Win_Flag == SUMMAR_TIME){
                tz_nz = CHSTD2 * OHOUR; // + 1 hour
                summarFlag = true;
            } else {
                tz_nz = CHSTD1 * OHOUR; // -1 hour
            }
            if (summarFlag){
                var strMsg = (sl == "ja") ? strRmks[0] : strRmks[1];
            } else {
                var strMsg = (sl == "ja") ? strRmks[2] : strRmks[3];
            }
            with(F){
                SUMMAR.innerText = strMsg;
                NZST.innerText = (summarFlag) ? "13:00" : "12:00";
                TDIFFV.value = (tz_jp - tz_nz)/OHOUR + ":00";
            }
            Sum_Win_Flag = NOT_THE_DAY;     // Clear flag value to the normal condition
        }
    }
    nz  = nowat(now, tz_nz);
//  uk  = nowat(now, tz_uk);
    jp  = nowat(now, tz_jp);
    with(F){
        idTime1.value = nz;
        idTime2.value = jp;
    }
    setTimeout('update_watch()', 999);
}

function nowat(now, cz){
    var hour, min, sec, day, mont, year, wday;
    var t = new Date();
    with(t){
        setTime(now.getTime() + (now.getTimezoneOffset() - cz) * 60 * 1000);
        year = getFullYear();   // YYYY
        mont = getMonth()+1;    // 1-12
        day  = getDate();       // 1-31
        wday = getDay();        // 0-6(Sun - Sat)
        hour = getHours();      // 0-23
        min  = getMinutes();    // 0-59
        sec  = getSeconds();    // 0-59
    }
    mont = (mont < 10) ? "0" + mont : mont;
    day  = (day  < 10) ? "0" + day  : day;
    hour = (hour < 10) ? "0" + hour : hour;
    min  = (min  < 10) ? "0" + min  : min;
    sec  = (sec  < 10) ? "0" + sec  : sec;

    return(year + "/" + mont + "/" + day + "(" + DaysOfWeek[wday].Eday + ")-" + hour + ":" + min + ":" + sec); // yyyy/mm/dd(ddd)-hh:mm:ss
}
