// Time Format Calculators 
// Copyright (c) 2008, CEPD Inc.
// http://www.cepd.com

var theDate;
            
function ISO8601Z(date) {
    // handles years from 0000 to 9999 only
    // from http://www.cs.tut.fi/~jkorpela/ecmascript-8601.txt

    return ("000" + date.getUTCFullYear()).slice(-4) +
        "-" + ("0" + (date.getUTCMonth() + 1)).slice(-2) +
        "-" + ("0" + date.getUTCDate()).slice(-2) +
        "T" + ("0" + date.getUTCHours()).slice(-2) +
        ":" + ("0" + date.getUTCMinutes()).slice(-2) +
        ":" + ("0" + date.getUTCSeconds()).slice(-2) +
        "Z";
}

function ISO8601Local(date) {
    // handles years from 0000 to 9999 only
    var offset = date.getTimezoneOffset();
    var offsetSign = "-";
    if (offset <= 0) {
        offsetSign = "+";
        offset = -offset;
    }
    var offsetHours = Math.floor(offset / 60);
    var offsetMinutes = offset - offsetHours * 60;
    return ("000" + date.getFullYear()).slice(-4) +
        "-" + ("0" + (date.getMonth() + 1)).slice(-2) +
        "-" + ("0" + date.getDate()).slice(-2) +
        "T" + ("0" + date.getHours()).slice(-2) +
        ":" + ("0" + date.getMinutes()).slice(-2) +
        ":" + ("0" + date.getSeconds()).slice(-2) +
        offsetSign + ("0" + offsetHours).slice(-2) +
        ":" + ("0" + offsetMinutes).slice(-2);
}

function gregdaynumber(year,month,day){

    // computes the day number since 0 January 0 CE (Gregorian)
    // source: http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm
    var y = year;
    var m = month+1;
    if(month < 3) {y = y-1;}
    if(month < 3) {m = m+12;}
    return Math.floor(365.25*y)-Math.floor(y/100)+Math.floor(y/400)+Math.floor(30.6*m)+day-62;
}

function isoweeknumber(fdate){

    // computes the ISO week number of the specified date
    // source: http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm
    var year = fdate.getUTCFullYear();
    var month = fdate.getUTCMonth();
    var date = fdate.getUTCDate();
    var wday = fdate.getUTCDay();
        
    // var isoweek;

    wday=((wday+6)%7)+1; // weekdays will be numbered 1 to 7

    yiso=year;

    d0=gregdaynumber(year,1,0);
    wday0=((d0+4)%7)+1;

    d=gregdaynumber(year,month+1,date);
    wniso=Math.floor((d-d0+wday0+6)/7)-Math.floor((wday0+3)/7);

    // check whether the last few days of December belong to the next year's ISO week

    if((month == 11) && ((date-wday) > 27)){
        wniso=1;
        yiso=yiso+1;
    }

    // check whether the first few days of January belong to the previous year's ISO week

    if((month === 0) && ((wday-date) > 3)){
        d0=gregdaynumber(year-1,1,0);
        wday0=((d0+4)%7)+1;
        wniso=Math.floor((d-d0+wday0+6)/7)-Math.floor((wday0+3)/7);
        yiso=yiso-1;
    }

    if(wniso < 10){return yiso+"-W0"+wniso+"-"+wday;}
    if(wniso > 9) {return yiso+"-W"+wniso+"-"+wday;}
}

function open_clock()
{
    window.open("clock.htm","_blank","toolbar=no, location=yes, directories=no, \n\
      status=no, menubar=no, scrollbars=no, resizable=no,toolbar=no,copyhistory=yes, width=340, height=430");
}
    
function padd2(value) {
    if (value <10){
        return( "0" + value);
    } else {
        return(value);}
}
 
function setAll(form) {
    setDateTime(form); 
    setCurrentD(form);    
    // setLDateTime(form);
    setGDateTime(form);
    setPosix(form);
    setW1096(form);
    setJulianDay(form);  
    setJscript(form);
    setMSW(form);
} 

function getDateTime(form) {
    var y = parseInt(form.Year.value,10);
    var mo = parseInt(form.Month.value,10)-1;
    var d = parseInt(form.Day.value,10);
    var h = parseInt(form.Hour.value,10);
    var mi = parseInt(form.Minute.value,10);
    var s = parseInt(form.Second.value,10);
    h += parseInt(form.TZSignSelect.value,10) * parseInt(form.TZHourSelect.value,10);
    mi += parseInt(form.TZSignSelect.value,10) * parseInt(form.TZHalfSelect.value,10);
               
    theDate.setUTCFullYear(y,mo,d);
    theDate.setUTCHours(h,mi,s);
    setAll(form);
}
                        
function getLDateTime(form) {
    var y = parseInt(form.LYear.value,10);
    var mo = parseInt(form.LMonth.value,10)-1;
    var d = parseInt(form.LDay.value,10);
    var h = parseInt(form.LHour.value,10);
    var mi = parseInt(form.LMinute.value,10);
    var s = parseInt(form.LSecond.value,10);
               
    theDate.setFullYear(y,mo,d);
    theDate.setHours(h,mi,s);
    setAll(form);
}
            
function getGDateTime(form) {
    var y = parseInt(form.GYear.value,10);
    var mo = parseInt(form.GMonth.value,10)-1;
    var d = parseInt(form.GDay.value,10);
    var h = parseInt(form.GHour.value,10);
    var mi = parseInt(form.GMinute.value,10);
    var s = parseInt(form.GSecond.value,10);
               
    theDate.setUTCFullYear(y,mo,d);
    theDate.setUTCHours(h,mi,s);
    setAll(form);
}

function getPosix(form) {
    theDate.setTime(parseInt(form.Posix.value,10)*1000);
    setAll(form);
}
function getJscript(form) {
    theDate.setTime(parseFloat(form.Jscript.value,10));
    setAll(form);
}
function getJulian(form) {
    var jdn = parseFloat(form.JDN.value,10);
    var time = (jdn-2440587.5)*86400000.0;
    theDate.setTime(time);
    setAll(form);
}
function getCurrent(form){
    theDate = new Date();
    setAll(form);
}
function getIsoWF(form) {
    var datetime = form.IsoWF.value;
    datetime=datetime.split("T");
    date=datetime[0].split("-");
    var time=datetime[1].split("Z");
    time=time[0].split(":");
    theDate.setUTCFullYear(date[0],date[1]-1,date[2]);
    theDate.setUTCHours(time[0],time[1],time[2]);
    setAll(form);
}

function setDateTime(form){
    var localDate = new Date();
    localDate.setTime(theDate.getTime());
    var h = localDate.getUTCHours() - parseInt(form.TZSignSelect.value,10) * parseInt(form.TZHourSelect.value,10);
    var m = localDate.getUTCMinutes() - parseInt(form.TZSignSelect.value,10) * parseInt(form.TZHalfSelect.value,10);
    var s = localDate.getUTCSeconds();
    localDate.setUTCHours(h,m,s);
    form.Year.value=localDate.getUTCFullYear();
    form.Month.value=localDate.getUTCMonth() + 1;
    form.Day.value=localDate.getUTCDate();
    form.Hour.value=localDate.getUTCHours();
    form.Minute.value = localDate.getUTCMinutes();
    form.Second.value = localDate.getUTCSeconds();		
}
            
function setLDateTime(form){
    form.LYear.value=theDate.getFullYear();
    form.LMonth.value=theDate.getMonth() + 1;
    form.LDay.value=theDate.getDate();
    form.LHour.value=theDate.getHours();
    form.LMinute.value = theDate.getMinutes();
    form.LSecond.value = theDate.getSeconds();		
}
                        
function setGDateTime(form){
    form.GYear.value=theDate.getUTCFullYear();
    form.GMonth.value=theDate.getUTCMonth() + 1;
    form.GDay.value=theDate.getUTCDate();
    form.GHour.value=theDate.getUTCHours();
    form.GMinute.value = theDate.getUTCMinutes();
    form.GSecond.value = theDate.getUTCSeconds();		
}
            
function setW1096(form){

    form.IsoW.value = isoweeknumber(theDate);
    var temp =ISO8601Z(theDate);
    form.IsoWF.value =  temp ;
    //form.XMLschema.value = "<startdate>" + temp + "</startdate>";
}
            
function setPosix(form) {
    var ptime = (theDate.getTime() - theDate.getMilliseconds())/1000;
    form.Posix.value = ptime & 4294967295 ;
}
            
function setJulianDay(form) {
    var jdn=theDate.getTime()/86400000.0 + 2440587.499999;
    form.JDN.value=jdn.toFixed(5);   
}
function setCurrentD(form) {
    var tzo = theDate.getTimezoneOffset()/60.0;
    if (tzo>0) 
    {tzo="-"+tzo;}
    else
    {tzo="+"+tzo;}
    var first = new Date(theDate.getFullYear(),0,1);
    var doy = Math.floor((theDate.getTime()-first.getTime())/(1000*24*60*60))+1;
    form.CurrentD.value = theDate.toLocaleString() + " (GMT " + tzo + ")" + " DOY: " + doy ;
}     
function setJscript(form) {
    form.Jscript.value = theDate.getTime().toExponential(12);
}
function setMSW(form) {
	//Jan 1st,1601 to Jan 1 1970 = 369.24 yrs = 11643609600000 ms
	// total # ms * (1000000 ns /1 ms)/ 100 ns = 10000
    var  t=(theDate.getTime()+11643609600000)*10000;
	var low = t % 4294967296;
    form.MSWlo.value = "0x" + low.toString(16).toUpperCase();
    form.MSWhi.value =  "0x" + Math.floor((t-low)/4294967296).toString(16).toUpperCase();	
}

            
