// An empty string; used for date & time fields (already defined in region.js?)
var empty = "";

// Just make sure date & time are the correct number of digits:
dateRE = new RegExp("^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$");
timeRE = new RegExp("^[0-9][0-9][0-9][0-9]$");

// DateRange : consists of two date values, in format YYYYMMDD.

DateRange=function () {
        // specify the date range as two values.
        this.ymd=[empty,empty];
}

DateRange.prototype.report=function() {
        var msg="dateRange[0]: "+this.ymd[0]+" dateRange[1]: "+this.ymd[1]+".";
        return msg;
}

DateRange.prototype.resetInput=function(){
        dateRangeInput[0].value=dateRange.ymd[0];
        dateRangeInput[1].value=dateRange.ymd[1];
}

DateRange.prototype.clear=function(){
        // clear model
        this.ymd=[empty,empty];

        // clear input boxes
        this.resetInput();

//      report.say("dateRange cleared. "+this.report());
        report.clear();
        info.clear();
        info.say("<br><br>");
}

// respond to a change in the value of either of the date input entry boxes.
// In the case of an incorrect value, the value gets set to the previous value (may be empty).
//  theid is the HTML id of the entry box.
//  i is either 0 or 1.
function dateInputChange(theid,i) {
        report.clear();
        // report.say("dateInputChange","red");
        var val=empty;
        var txt=document.getElementById(theid).value;

        if (dateRE.test(txt)) {
                if (validDate(txt)) {
                        // report.say( "Valid date:  " + txt);  // DEBUG
                        dateRange.ymd[i]=txt;
                } else {
                        // report.error( "Invalid date:  " + txt);
                        // error message is output by validDate()
                        dateRange.resetInput();
                }
        } else {
                report.error( "Dates must be in the format:  YYYYMMDD");
                dateRange.resetInput();
        }

//      report.say("dateInputChange("+theid+","+i+")","red");
//      report.say(dateRange.report(),"green");
}

// Check that the string (in the format YYYYMMDD) is a valid date, by checking that 
// the "overflow" features of Date objects have not been utilised.
// Javascript uses the same conventions as Java; i.e., day 33 is in the next month, etc.
function validDate(txt) {
        var year = parseInt(txt.substr(0,4), 10);
        if ( year < 100 ) { 
                report.error(txt + " has year < 100");  // if year < 100, Date() will add 1900
                return false;
        }
        var month = parseInt(txt.substr(4,2), 10);      // Note:  in Date objects, months start at 0
        var day = parseInt(txt.substr(6,2), 10);
        var dateObj = new Date(year, month - 1, day, 0, 0, 0, 0);
        var outYear = dateObj.getFullYear();            // 4 digits
        var outMonth = dateObj.getMonth() + 1;          // 1 to 12
        var outDay = dateObj.getDate();
        if (year != outYear || month != outMonth || day != outDay) {
                report.error(txt + ":  invalid date"); 
                return false;
        }
        return true;
}

// ===================================================================================

// TimeRange : consists of two time values, in format HHMM.

TimeRange=function () {
        // specify the time range as two values.
        this.hm=[empty,empty];
}

TimeRange.prototype.report=function() {
        var msg="timeRange[0]: "+this.hm[0]+" timeRange[1]: "+this.hm[1]+".";
        return msg;
}

TimeRange.prototype.resetInput=function(){
        timeRangeInput[0].value=timeRange.hm[0];
        timeRangeInput[1].value=timeRange.hm[1];
}

TimeRange.prototype.clear=function(){
        // clear model
        this.hm=[empty,empty];

        // clear input boxes
        this.resetInput();

//      report.say("timeRange cleared. "+this.report());
        report.clear();
        info.clear();
        info.say("<br><br>");
}

// respond to a change in the value of either of the time input entry boxes.
// In the case of an incorrect value, the value gets set to empty (?)
//  theid is the HTML id of the entry box.
//  i is either 0 or 1.
function timeInputChange(theid,i) {
        report.clear();
        // report.say("timeInputChange","red");
        var val=empty;
        var txt=document.getElementById(theid).value;

        if (txt==empty) { 
                timeRange.hm[i]=txt;            // It is OK to leave timeRange fields empty
        } else if (timeRE.test(txt)) {
                if (validTime(txt)) {
                        // report.say( "Valid time:  " + txt);  // DEBUG
                        timeRange.hm[i]=txt;
                } else {
                        // report.error( "Invalid time:  " + txt);
                        // error message is output by validTime()
                        timeRange.resetInput();
                }
        } else {
                report.error( "Times must be in the format:  HHMM");
                timeRange.resetInput();
        }

//      report.say("timeInputChange("+theid+","+i+")","red");
//      report.say(timeRange.report(),"green");
}

// Check that the string (in the format HHMM) is a valid time, by checking that 
// the "overflow" features of Date objects have not been utilised.
// Javascript uses the same conventions as Java; i.e., day 33 is in the next month, etc.
// Do we need to allow "2400" as a valid time?
function validTime(txt) {
        var hour = parseInt(txt.substr(0,2), 10);
        var minute = parseInt(txt.substr(2,2), 10);
        var dateObj = new Date(2001, 0, 1, hour, minute, 0, 0);         // YMD are irrelevant
        var outHour = dateObj.getHours();
        var outMinute = dateObj.getMinutes();
        if (hour != outHour || minute != outMinute) {
                report.error(txt + ":  invalid time"); 
                return false;
        }
        return true;
}

