/***********************************************
* Calculate current NZ time and date
***********************************************/

var timerID ;
function tzone(os)
{
  this.ct = new Date(0) ;    // datetime (array)
  this.os = os ;    // GMT offset
//  this.ds = ds ;    // has daylight savings
}


function UpdateClocks()
{
  // www.timeanddate.com/worldclock
  var ct = new Array(
    new tzone(12)
//    new tzone(+11, 1)
//  new tzone(0, 1)
  ) ;

  var dt = new Date() ;  // [GMT] time according to machine clock

  var startDST = new Date(dt.getFullYear(), 3, 1) ;
  while (startDST.getDay() != 0) {
    startDST.setDate(startDST.getDate() + 1) ;
  }
  var endDST = new Date(dt.getFullYear(), 9, 31) ;
  while (endDST.getDay() != 0) {
    endDST.setDate(endDST.getDate() - 1) ;
  }
  var ds_active ;    // DS currently active
  if (startDST < dt && dt < endDST) {
    ds_active = 0 ;
//    ds_active = 1 ;
  } else {
    ds_active = 0 ;
  }
  // Adjust each clock offset if that clock has DS and in DS.
  for(n=0 ; n<ct.length ; n++) {
    if (ct[n].ds == 1 && ds_active == 1) ct[n].os++ ;
  }
  // compensate time zones
  gmdt = new Date() ;
  for (n=0 ; n<ct.length ; n++) {
    ct[n].ct = new Date(gmdt.getTime() + ct[n].os * 3600 * 1000) ;
  }
  	document.the_clock.Clock0.value = ClockString(ct[0].ct);
//  document.the_clock.Clock1.value = ClockString(ct[1].ct);
  
  timerID = window.setTimeout("UpdateClocks()", 1001) ;
}


function ClockString(dt)
{
  var stemp, ampm ;

  var dt_year = dt.getUTCFullYear() ;
  
  var monthNames = new Array("Jan","Feb","March","April","May","June","July","August","Sept","Oct","Nov","Dec");
//  var dt_month = dt.getUTCMonth() + 1 ;
  var dt_month = monthNames[dt.getUTCMonth()] ;
  var check_month = dt.getUTCMonth() + 1 ;
  
  var dt_day = dt.getUTCDate() ;





// Adjust hours according to month to suit daylight savings - STARTS 28th Sept - ENDS 6th April
// (Note: certain days in April & Sept needed special adjustment - presumably to overide default DS times).

if (check_month == 5 || check_month == 6 || check_month == 7 || check_month == 8) {

  var dt_hour = dt.getUTCHours() - 1;

} else if (check_month == 1 || check_month == 2 || check_month == 3 || check_month == 10 || check_month == 11 || check_month == 12) {

  var dt_hour = dt.getUTCHours() + 1;

} else if (check_month == 4) {
	if (dt_day <= 4) {
		var dt_hour = dt.getUTCHours() + 1;
	} else if (dt_day == 5 || dt_day == 6 || dt_day == 7) {
		var dt_hour = dt.getUTCHours();
	} else if (dt_day >= 8) {
		var dt_hour = dt.getUTCHours() - 1;
	}

} else if (check_month == 9) {
	if (dt_day <= 26) {
		var dt_hour = dt.getUTCHours() - 1;
	} else if (dt_day == 27) {
		var dt_hour = dt.getUTCHours();
	} else if (dt_day >= 28) {
		var dt_hour = dt.getUTCHours() + 1;
	}

}

//  var dt_hour = dt.getUTCHours() ;
  var dt_minute = dt.getUTCMinutes() ;
  var dt_second = dt.getUTCSeconds() ;
  
  dt_year = dt_year.toString() ;
  if (0 <= dt_hour && dt_hour < 12)
  {
    ampm = 'AM' ;
    if (dt_hour == 0) dt_hour = 12 ;    
  } else {
    ampm = 'PM' ;
    dt_hour = dt_hour - 12 ;
    if (dt_hour == 0) dt_hour = 12 ;    
  }
  
  if (dt_minute < 10)
    dt_minute = '0' + dt_minute ;
  
  if (dt_second < 10)
    dt_second = '0' + dt_second ;

//  stemp = dt_month + '/' + dt_day + '/' + dt_year.substr(2,2) ;
//  stemp = dt_day + ' ' + dt_month + ',' ;
  stemp = dt_day + ' ' + dt_month + ',' ;
  stemp = stemp + ' ' + dt_hour + ":" + dt_minute + ":" + dt_second + ' ' + ampm ;
  return stemp ;
}




/***********************************************
* Drop Down Date select script- by JavaScriptKit.com
* This notice MUST stay intact for use
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
***********************************************/

var monthtext=['Jan','Feb','Mar','Apr','May','Jun','July','Aug','Sept','Oct','Nov','Dec'];

function populatedropdown(dayfield, monthfield, yearfield){
	var today=new Date();
	var dayfield=document.getElementById(dayfield);
	var monthfield=document.getElementById(monthfield);
	var yearfield=document.getElementById(yearfield);

	for (var i=1; i<32; i++) {
		dayfield.options[i]=new Option(i, i);
		dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true); //select today's day
	}

	for (var m=0; m<12; m++) {
		monthfield.options[m]=new Option(monthtext[m], monthtext[m]);
		monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true); //select today's month
	}
	
	var thisyear=today.getFullYear();
	for (var y=0; y<6; y++){
		yearfield.options[y]=new Option(thisyear, thisyear);
		thisyear+=1;
}
	yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true); //select today's year
}






/***********************************************
* format date as '2009-11-22' and put in 'from' field
***********************************************/
	 function do_dates() {
		var monthvalue = document.getElementById("EDA_MONTHS").value;
		if (monthvalue == 'Jan') {
			monthvalue = '01'
		} else if (monthvalue == 'Feb') {
			monthvalue = '02'
		} else if (monthvalue == 'Mar') {
			monthvalue = '03'
		} else if (monthvalue == 'Apr') {
			monthvalue = '04'
		} else if (monthvalue == 'May') {
			monthvalue = '05'
		} else if (monthvalue == 'Jun') {
			monthvalue = '06'
		} else if (monthvalue == 'July') {
			monthvalue = '07'
		} else if (monthvalue == 'Aug') {
			monthvalue = '08'
		} else if (monthvalue == 'Sept') {
			monthvalue = '09'
		} else if (monthvalue == 'Oct') {
			monthvalue = '10'
		} else if (monthvalue == 'Nov') {
			monthvalue = '11'
		} else if (monthvalue == 'Dec') {
			monthvalue = '12'
		}
	 	document.getElementById("from").value = 
			document.getElementById("EDA_YEARS").value + '-' +
			monthvalue + '-' +
			document.getElementById("EDA_DAYS").value;
			//alert(document.getElementById("from").value);
	 }