function y2k(nbr)    { return (nbr < 1000) ? nbr + 1900 : nbr; }

// ppyzik 20011001, Changed to add a null string to nbr when it is no less than 10.
function pad0(nbr) { return (nbr < 10) ? '0' + nbr : '' + nbr; }

// Fetches todays date and sets field GasDayDate in YYYYMMDD format.
function getTodaysDate() {

  var todaysDate = new Date();

  return y2k( todaysDate.getYear() ) +
         pad0( todaysDate.getMonth() + 1 ) +
         pad0( todaysDate.getDate() );
}

function formSubmit() {

  if ( isInvalidTextDate( document.GdsrForm.GasDayDate ) ) {
    alert( 'Please enter the date as YYYYMMDD e.g. 20010501' );
    document.GdsrForm.GasDayDate.focus();
    return false;
  }

  if ( isDateOutOfRange( document.GdsrForm.GasDayDate ) ) {
    alert( 'Report not available for the specified date.' );
    document.GdsrForm.GasDayDate.focus();
    return false;
  }

  //formUrl = 'http://localhost/Gdsr';
  formUrl = 'https://services.tcpl.ca/cor/public/gdsr/Gdsr';

  if ( document.GdsrForm.Pipeline[0].checked ) {
    formUrl = formUrl + document.GdsrForm.Pipeline[0].value;

    if ( document.GdsrForm.MeasureType[0].checked )
      formUrl = formUrl + document.GdsrForm.MeasureType[0].value;
    else
      formUrl = formUrl + document.GdsrForm.MeasureType[1].value;
  }
  else {
    formUrl = formUrl + document.GdsrForm.Pipeline[1].value;
  }

  formUrl = formUrl + document.GdsrForm.GasDayDate.value;
  var fileExtension = document.GdsrForm.ActionType.options[ document.GdsrForm.ActionType.selectedIndex ].value;
  formUrl = formUrl + fileExtension;

  //document.GdsrForm.action = formUrl;
  //document.GdsrForm.submit();
  if ( fileExtension == ".pdf" &&
       navigator.appName == "Netscape" )
    window.open( formUrl );
  else
    window.location = formUrl;

  return false;
}

var calendarWindow = null;

function CalendarWindow() {

  if ( document.GdsrForm.Pipeline[0].checked ) {
    setAlbertaStartDate();
  }
  else {
    setMainlineStartDate();
  }

  // Before calling up the calendar window set the date holding variables
  // to the display date.
  if ( !isInvalidTextDate( document.GdsrForm.GasDayDate ) ) {
    var startYYYYMM = startYear + pad0( startMonth + 1 );
    var endYYYYMM = endYear + pad0( endMonth + 1 );
    var inputYYYYMM = document.GdsrForm.GasDayDate.value.substring(0,6);

    if ( inputYYYYMM <= endYYYYMM && inputYYYYMM >= startYYYYMM ) {
      year = parseInt( document.GdsrForm.GasDayDate.value.substring(0,4), 10 );
      month = parseInt( document.GdsrForm.GasDayDate.value.substring(4,6), 10 ) - 1;
    }
  }

  var calendarUrl = 'GdsrCalendar.htm';
  calendarWindow = open( calendarUrl, 'myname', 'resizable=no,width=350,height=260,screenX=20,screenY=200' );
  calendarWindow.location.href = calendarUrl;
  if ( calendarWindow.opener == null ) calendarWindow.opener = self;
}

function CloseCalendar() {
  document.GdsrForm.GasDayDate.value = '' + year + pad0( month - 0 + 1 ) + pad0( day );
  calendarWindow.close();
  calendarWindow = null;
}

function ClosePopup() {
  if ( calendarWindow != null && !calendarWindow.closed ) calendarWindow.close();
  return true;
}

var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var year  = y2k(today.getYear());

var endDay   = today.getDate();
var endMonth = today.getMonth();
var endYear  = y2k(today.getYear());

// This is the first date for which there are Alberta reports
// Month is 0-based so, January is 0
var startAlbertaYear = 1999;
var startAlbertaMonth = 9;
var startAlbertaDay = 14;

// This is the first date for which there are Mainline reports
// Month is 0-based so, January is 0
var startMainlineYear = 2001;
var startMainlineMonth = 6;
var startMainlineDay = 14;

var startYear  = startAlbertaYear;
var startMonth = startAlbertaMonth;
var startDay   = startAlbertaDay;

function isBadStrLength( pStr, pLen )
{
  if ( pStr.value.length == pLen )
    return false;
  return true;
}

function isBadInteger( pStr, pLen )
{
  var count = 0;
  for ( i = 0; i < pStr.length; i++ )
  {
    var x = pStr.substring( i, i+1 );
    if ( x >= "0" && x <= "9" )
      count++;
  }
  if ( count != pLen )
    return true;
  return false;
}

function isInvalidTextDate( pTextDate )
{
  // check if the date is in the yyyymmdd format

  if ( isBadStrLength( pTextDate, 8 ) )
    return true;

  // number checking
  if ( isBadInteger( pTextDate.value, 8 ) )
    return true;
  
  displayYear = pTextDate.value.substring(0,4);
  displayMonth = pTextDate.value.substring(4,6);
  displayDay = pTextDate.value.substring(6,8);
  
  // basic range checking
  if ( displayMonth < 1 || displayMonth > 12 || displayDay < 1 || displayDay > 31 )
    return true;

  // more range checking
  // months with 30 days
  if ( displayMonth == 4 || displayMonth == 6 || displayMonth == 9 || displayMonth == 11 )
    if ( displayDay == 31 )
      return true;

  // february, leap year
  if ( displayMonth == 2 ) {
    if (((displayYear % 4 == 0) && (displayYear % 100 != 0)) || (displayYear % 400 == 0)) {
      if ( displayDay > 29 ) return true;
    } else {
      if ( displayDay > 28 ) return true;
    }
  }

  return false;
}

function isDateOutOfRange( pTextDate ) {

  // subtract 2 years from today's date because only 2 years worth
  // of reports will be kept
  if ( document.GdsrForm.Pipeline[0].checked ) {
    setAlbertaStartDate();
  }
  else {
    setMainlineStartDate();
  }
  var startDate = Date.UTC( startYear, startMonth, startDay, 23, 59, 59 );
  var endDate = Date.UTC( endYear, endMonth, endDay ,23 , 59, 59 );

  displayYear = pTextDate.value.substring(0,4);
  displayMonth = pTextDate.value.substring(4,6) - 1;
  displayDay = pTextDate.value.substring(6,8);
  
  if ( displayYear < 1970 || displayYear > 2030 )
    return true;

  var displayDate = Date.UTC( displayYear, displayMonth, displayDay, 23, 59, 59 );
  if (( displayDate < startDate ) || ( displayDate > endDate ))
    return true;

  return false;
}

function ValidateMeasureType() {

  if (( document.GdsrForm.Pipeline[1].checked ) &&
      ( document.GdsrForm.MeasureType[1].checked )) {
    alert( "The Gas Day Summary report for the Mainline is only available in gigajouls." );
    document.GdsrForm.MeasureType[0].checked = true;
  }
  return true;
}

function setAlbertaStartDate() {
  startYear = startAlbertaYear;
  startMonth = startAlbertaMonth;
  startDay = startAlbertaDay;
}

function setMainlineStartDate() {
  startYear = startMainlineYear;
  startMonth = startMainlineMonth;
  startDay = startMainlineDay;
}

