
//regular expression for date validation. MM/DD/YYYY (i.e 02/04/2002)
var reDate = /[0-1]\d\/[0-3]\d\/\d{4}/  

//prompt messages
var pEntryPrompt = "Please enter a "
var pDate = "date (like 01/05/2005)"

//error messages
var iDateFormat = "Please enter a date in this format: MM/DD/YYYY\n(like 01/05/2005)"
var iDateRange = "Please enter a From date that is before the To date"
var iSelected = "Please select a station"
var iMaxChecked = "Only the first 13 selected stations will be graphed"
var iAggregateMaxChecked = "Only the first 13 selected stations will be graphed on the Pressure and Energy graphs.\n(Flow for all selected stations will be graphed in aggregate)"

var dateSeparator = "/"

//max number of data series the graphs can handle
var maxChecked = 13;


function isValidDate(theField)
{
  	value = theField.value
  	if(reDate.test(value))
  		return true;
  	else
  		return warnInvalid(theField, iDateFormat)
}

function checkDateRange(fromDateField, toDateField, fromHourOption, toHourOption)
{	
	fhour = 9;
	thour = 9;
	
	if (checkDateRange.arguments.length == 4)
	{
		fhour = fromHourOption.value;
		thour = toHourOption.value;		
	}
	
	from = fromDateField.value;
	to = toDateField.value;
	
	fromDateFields = from.split(dateSeparator);
	fmonth = fromDateFields[0];
	fday = fromDateFields[1]
	fyear = fromDateFields[2];
	
	toDateFields = to.split(dateSeparator);
	tmonth = toDateFields[0];
	tday = toDateFields[1];
	tyear = toDateFields[2];	
	
	fromDate = new Date(fyear, fmonth - 1, fday, fhour);
	toDate = new Date(tyear, tmonth - 1, tday, thour);
	
	fmillis = Date.parse(fromDate);
	tmillis = Date.parse(toDate);
	
	if(fmillis >= tmillis)
		return warnInvalid(fromDateField, iDateRange);
	
	return true;
}

function checkSelected()
{
	form = arguments[0];
	numOfCheckboxFields = checkSelected.arguments.length - 1
	isSelected = false;
	for (i = 1; i <= numOfCheckboxFields; i++)
	{
		cbFieldName = arguments[i];
		for(k = 0; k < form.elements.length; k++)
		{
			if(isSelected) break;				
			element = form.elements[k];
			if((element.name == cbFieldName) && (element.checked))
			{
				isSelected = true;
				break;
			}			
		}
	}
	
	if(!isSelected) alert(iSelected);
	return isSelected;
}

checkboxCount = 0;
warned = false;

function countSelected(checked)
{
	if(checked)
		checkboxCount++;
	else
		checkboxCount--;
		
	if((checkboxCount > maxChecked) && (!warned))
	{
		if (document.forms[0].isAggregate.checked)
			alert(iAggregateMaxChecked);
		else
			alert(iMaxChecked);
			
		warned = true;
	}
}

// Display data entry prompt string s in status bar.

function promptEntry (s)
{   window.status = pEntryPrompt + s
}

function defaultStatus(s)
{	window.status = s
}

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, put focus in it, and return false.

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}
