window.name="main";
function opener(x,y){
  if(navigator.appVersion.charAt(0) + 0 > 3){
        window.open(x,y,'resizable,width=400,height=300,scrollbars');
  }
  if(navigator.appVersion.charAt(0) + 0 <= 3){
        window.open(x,y,'resizable,width=400,height=300,scrollbars');
  }
        return;
}

function helpwin(x){
  if (navigator.appVersion.charAt(0) + 0 > 3) {
        newWindow = window.open(x,"help","resizable,width=490,height=400,scrollbars,screenX=400,left=400");
  } else if (navigator.appVersion.charAt(0) + 0 <= 3) {
        newWindow = window.open(x,"help","resizable,width=490,height=400,scrollbars");
  }
  
  if (newWindow.focus) {
    newWindow.focus();
  }
  return;
}

function imgdisplay(x,y){
  if (navigator.appVersion.charAt(0) + 0 > 3) {
        newWindow = window.open(x,y,"resizable,width=660,height=500,menubar,scrollbars,screenX=400,left=400");
  } else if (navigator.appVersion.charAt(0) + 0 <= 3) {
        newWindow = window.open(x,y,"resizable,width=660,height=500,menubar,scrollbars");
  }
  
  if (newWindow.focus) {
    newWindow.focus();
  }
  return;
}

// suite of subroutines to validate dates
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++) {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year) {
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
   } 
   return this;
}

function isDate(dtStr,type){
	// Declaring valid date character, minimum year and maximum year
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;

	var dateType = type + " Date";

	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("The " + dateType + " format should be : mm/dd.");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Error with " + dateType + " month value " + month + ". Please enter a valid month.");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]) {
		alert("Error with " + dateType + " day value " + day + ". Please enter a valid day.");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear) {
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert(dateType + " contains unacceptable characters. Please enter a valid date.");
		return false;
	}
	return true;
}
// end of suite of subroutines to validate dates
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// all graphics and text query pages using makegraph, 
//  except adult which uses validateAdultYear
function validateYear() {
	// validate output format and number of years selected
	// reject multiple years for fancyReport output due to leap year issues
	var outElement = document.query.outputFormat;
	var o;
	for ( var i = 0; i < outElement.length; i++ ) {
		if ( outElement[i].checked ) {
			 o = outElement[i].value;
		}
	}

	if ( o == "fancyReport" ) {
		var message = "";
		var yearElement;
		var ryearElement;
		if ( document.query.esuYear ) {
			yearElement = document.query.esuYear;
		} else {
			yearElement = document.query.year;
		}
		var yCount = 0;
		for ( var i = 0; i < yearElement.length; i++ ) {
			if ( yearElement[i].selected ) {
				yCount++;
			}
		}

		var rCount = 0;
		if ( document.query.riverYear ) {
			ryearElement = document.query.riverYear;
			for ( var i = 0; i < ryearElement.length; i++ ) {
				if ( ryearElement[i].selected ) {
					rCount++;
				}
			}
		}

		if ( yCount > 1 ) {
			// this option is not allowed because of leap year
			message = message + "Only one (1) selection for " +
			"parameter Year is allowed " +
			"for 'View Data Table (MM/DD)' output format, " +
			"which aligns data by month and day.\n" +
			"Please select a single year from the Year " +
			"parameter menu for this output format.\n" +
			"Multiple year selections are allowed for the " +
			"'View Data Table (DOY)' output format, " +
			"which aligns data by day of year.\n\n";
		}
		if ( rCount > 1 ) {
			// this option is not allowed because of leap year
			message = message + "Only one (1) selection for " +
			"parameter River Year is allowed " +
			"for 'View Data Table (MM/DD)' output format, " +
			"which aligns data by month and day.\n" +
			"Please select a single year from the River Year " +
			"parameter menu for this output format.\n" +
			"Multiple year selections are allowed for the " +
			"'View Data Table (DOY)' output format, " +
			"which aligns data by day of year.\n\n";
		}
		if ( message ) {
			alert("QUERY ALERT\n\n" + message + "After making " +
			"new selections, click 'Begin Run'.");
			return false;
		}
	}
}

// pit_obs_adult_conrate.html
function validateMonth() {
	// validate start month and end month range

	var startElement = document.conrate[8];
	var endElement = document.conrate[9];
	var s,e;
	var extract = /^\d+/;

	for ( var i = 0; i < startElement.length; i++ ) {
		if ( startElement[i].selected ) {
			s = startElement[i].value;
		}
	}
	for ( var i = 0; i < endElement.length; i++ ) {
		if ( endElement[i].selected ) {
			e = endElement[i].value;
		}
	}
	var sNum = Number(s.match(extract));
	var eNum = Number(e.match(extract));

	if ( sNum > eNum ) {
		alert("QUERY ALERT\n\n" + 
		"Selected Start Month is later than selected End Month.\n\n" +
		"Please make new selections for Month Range and " +
		"click 'Begin Run'.");
		return false;
	} else {
		return true;
	}
}

// adult_sum.html
function validateAdMonth() {
	// validate start month and end month range

	var startElement = document.adultmonth[5];
	var endElement = document.adultmonth[6];
	var s,e;
	var extract = /^\d+/;

	for ( var i = 0; i < startElement.length; i++ ) {
		if ( startElement[i].selected ) {
			s = startElement[i].value;
		}
	}
	for ( var i = 0; i < endElement.length; i++ ) {
		if ( endElement[i].selected ) {
			e = endElement[i].value;
		}
	}
	var sNum = Number(s.match(extract));
	var eNum = Number(e.match(extract));

	if ( sNum > eNum ) {
		alert("QUERY ALERT\n\n" + 
		"Selected Start Month is later than selected End Month.\n\n" +
		"Please make new selections for Month Range and " +
		"click 'Begin Run'.");
		return false;
	} else {
		return true;
	}
}

// Validate dates from DART queries
// dart.c dates
// crt/get dates
function ValidateDartDates(){
	var extract = /span$/;

	var sdElement;
	if ( document.query.startdate ) {
		sdElement=document.query.startdate;
	} else if (document.query.DataStartDate ) {
		sdElement=document.query.DataStartDate;
	} else {
		alert("Issue establishing start date.\n");
	}

	var edElement;
	if ( document.query.enddate ) {
		edElement=document.query.enddate;
	} else if (document.query.DataEndDate ) {
		edElement=document.query.DataEndDate;
	} else {
		alert("Issue establishing end date.\n");
	}

	var r;
	var rElement;
	if ( document.query.report ) {
		rElement=document.query.report;
		// report
		for ( var i = 0; i < rElement.length; i++ ) {
			if ( rElement[i].checked ) {
				r = rElement[i].value;
			}
		}
		// if report is null, report is being passed as a hidden input parameter
		if ( r == null ) {
			r = rElement.value;
		}
	}

	var yElement;
	if ( document.query.year ) {
		yElement=document.query.year;
	} else if (document.query.Year ) {
		yElement=document.query.Year;
	} else {
		alert("Issue establishing year.\n");
	}

	var sd = new Array();
	var ed = new Array();

	// split month and day values
	sd = sdElement.value.split("/");
	ed = edElement.value.split("/");

	if ( sd.length > 2 ) {
		alert("The Start Date format should be : mm/dd.");
		return false;
	}
	if ( ed.length > 2 ) {
		alert("The End Date format should be : mm/dd.");
		return false;
	}

	// year
	var y;
	for ( var i = 0; i < yElement.length; i++ ) {
		if ( yElement[i].selected ) {
			y = yElement[i].value;
		}
	}
	// if year is null, year is being passed as a hidden input parameter
	if ( y == null ) {
		y = yElement.value;
	}

	// validate start date and end date values
	// create full date in mm/dd/yyyy format
	var startDate = sdElement.value + "/" + y;
	var endDate = edElement.value + "/" + y;

	// check validity of start date as a date
	if (isDate(startDate,"Start")==false){
		sdElement.focus();
		return false;
	}

	// check validity of end date as a date
	if (isDate(endDate,"End")==false){
		edElement.focus();
		return false;
	}

	// does not validate initial month and day values
	// create dates of start date and end date for
	// comparison to each other and "today"
	var sDate=new Date();
	var smindex = sd[0]-1;
	sDate.setFullYear(y,smindex,sd[1]);

	var eDate=new Date();
	var emindex = ed[0]-1;
	eDate.setFullYear(y,emindex,ed[1]);

	var today = new Date();

	// check if start date is in future
	if ( sDate > today ) {
			alert("No data days possible. Start Date in the future.\n" +
			"Please change your Start Date.\n");
			return false;
	} else if ( sDate < today ) {
			// dates okay, continue
			
	} else {
			alert("No data days possible. Start Date equal to current day.\n" +
			"Please change your Start Date.\n");
			return false;
	}

	// if report, check if span years or within year query type
	// else check startdate before enddate
	if ( r ) {
		var result = r.match(extract);
		if ( result != null ) {
			if ( ed[0] == 2 && ed[1] == 29 ) {
				alert("End Date 2/29 not allowed for Span Calendar Years query.\n" +
				"Please change your End Date to 2/28.\n");
				return false;
			// want to check for sd > ed
			} else if ( sDate > eDate ) {
				// dates good
				return true;
			} else {
				// dates bad
				alert("Longer than one year selected.\n" +
				"Please change your Start Date and/or End Date or\n" +
				"select Within Calendar Year option for Report Year Type.\n");
				return false;
			}
		} else {
			// want to check for sd < ed
			if ( sDate < eDate ) {
				// dates good
				return true;
			} else {
				// dates bad
				alert("End Date occurs before Start Date.\n" +
				"Please change your Start Date and/or End Date.\n");
				return false;
			}
		}
	} else {
		// want to check for sd < ed
		if ( sDate < eDate ) {
			// dates good
			return true;
		} else {
			// dates bad
			alert("End Date occurs before Start Date.\n" +
			"Please change your Start Date and/or End Date.\n");
			return false;
		}
	}
}

// adult_annual.html
function validateAdultRun() {
	// validate project and run selection for Adult Passage annual query page
	// if it passes, call validateDartDates to validate query dates

	var projElement = document.query.proj;
	var runElement = document.query.run;
	var p,r;

	for ( var i = 0; i < projElement.length; i++ ) {
		if ( projElement[i].selected ) {
			p = projElement[i].value;
		}
	}
	for ( var i = 0; i < runElement.length; i++ ) {
		if ( runElement[i].selected ) {
			r = runElement[i].value;
		}
	}

	if ( p == "PRO" ) {
		if ( r.search(/3/) != -1 ) {
			// this option is not allowed because of overwintering
			alert("QUERY ALERT\n\n" + 
			"Run selection Fall is not allowed for project " +
			"Prosser.\n" +
			"Prosser Fall Run dates span 8/16 through 2/28 of "+
			"the following year.\n" +
			"Please use Start Date and End Date to limit your " +
			"query by dates within year.\n\n" + 
			"After making new selections, click 'Begin Run'.");
			return false;
		} else if ( r.search(/2/) != -1 ) {
			// this option is not valid
			alert("QUERY ALERT\n\n" + 
			"Run selection Summer is not valid for " +
			"Prosser.\n" +
			"After making new selections, click 'Begin Run'.");
			return false;
		} else {
			return ValidateDartDates();
		}
	} else if ( p == "ROZ" ) {
		if ( r.search(/3/) != -1 ) {
			// this option is not allowed because of overwintering
			alert("QUERY ALERT\n\n" + 
			"Run selection Fall is not valid for " +
			"Roza.\n" +
			"After making new selections, click 'Begin Run'.");
			return false;
		} else if ( r.search(/2/) != -1 ) {
			// this option is not valid
			alert("QUERY ALERT\n\n" + 
			"Run selection Summer is not valid for " +
			"Roza.\n" +
			"After making new selections, click 'Begin Run'.");
			return false;
		} else {
			return ValidateDartDates();
		}
	} else if  ( p == "WFF" ) {
		if ( r.search(/2/) != -1 ) {
			// this option is not valid
			alert("QUERY ALERT\n\n" + 
			"Run selection Summer is not valid for " +
			"Willamette Fals.\n" +
			"After making new selections, click 'Begin Run'.");
			return false;
		} else {
			return ValidateDartDates();
		}
	} else {
		return ValidateDartDates();
	}
}

// adultpass.html
function validateAdultYear() {
	// validate project and year selection for Adult Passage query pages

	var projElement = document.query.proj;
	var speciesElement = document.query.ftype;
	var yearElement = document.query.year;
	var ayearElement = document.query.avgyear;
	var outElement = document.query.outputFormat;

	var o;
	for ( var i = 0; i < outElement.length; i++ ) {
		if ( outElement[i].checked ) {
			 o = outElement[i].value;
		}
	}

	var s = new Array();
	for ( var i = 0; i < speciesElement.length; i++ ) {
		if ( speciesElement[i].selected ) {
			var j = s.push(speciesElement[i].value);
		}
	}

	var y = new Array();
	var yCount = 0;
	for ( var i = 0; i < yearElement.length; i++ ) {
		if ( yearElement[i].selected ) {
			var j = y.push(yearElement[i].value);
			yCount++;
		}
	}
	var aCount = 0;
	for ( var i = 0; i < ayearElement.length; i++ ) {
		if ( ayearElement[i].selected ) {
			aCount++;
		}
	}

	if ( o == "fancyReport" ) {
		var message = "";
		if ( yCount > 1 ) {
			// this option is not allowed because of leap year
			message = message + "Only one (1) selection for " +
			"parameter Year is allowed " +
			"for 'View Data Table (MM/DD)' output format, " +
			"which aligns data by month and day.\n" +
			"Please select a single year from the Year " +
			"parameter menu for this output format.\n" +
			"Multiple year selections are allowed for the " +
			"'View Data Table (DOY)' output format, " +
			"which aligns data by day of year.\n\n";

		}
		if ( aCount > 1 ) {
			// this option is not allowed because of leap year
			message = message + "Only one (1) 10 Year Avg " +
			"selection is allowed for 'View Data Table " +
			"(MM/DD)' output format, " +
			"which aligns data by month and day.\n" +
			"Please select a single year from the 10 Year Avg " +
			"parameter menu for this output format.\n" +
			"Multiple year selections are allowed for the 'View " +
			"Data Table (DOY)' output format, " +
			"which aligns data by day of year.\n\n";
		}
		if ( message ) {
			alert("QUERY ALERT\n\n" + message + "After making " +
			"new selections, click 'Begin Run'.");
			return false;
		}
	}

	var p = new Array();
	for ( var i = 0; i < projElement.length; i++ ) {
		if ( projElement[i].selected ) {
			var j = p.push(projElement[i].value);
		}
	}

	var warn = "WARNING\n";
	var warnWkFlag = 0;
	var warnFlag = 0;

	// cycle through project array "p" looking for "PRO" or "ROZ" for weekly
	// new check for "RRH" for warning about video loss in 2005
	for ( var i = 0; i < p.length; i++ ) {
		if ( p[i] == "PRO" ) {
			for ( var j = 0; j < y.length; j++ ) {
				if ( (y[j] >= 1983) && (y[j] <= 1991) ) {
					warnWkFlag = 1;
					warnFlag = 1;
					warn = warn + "Daily counts are not available for Prosser Dam data for years 1983-1991.\n" +
						"The Yakama Nation has provided weekly counts with data assigned to an\n" +
						"arbitrary day in the week for which counts are available.\n\n";
					break;
				}
 			}
		} else if ( p[i] == "ROZ" ) {
			for ( var j = 0; j < y.length; j++ ) {
				if ( (y[j] >= 1986) && (y[j] <= 1990) ) {
					warnWkFlag = 1;
					warnFlag = 1;
					warn = warn + "Daily counts are not available for Roza Dam data for years 1986-1990.\n" +
						"The Yakama Nation has provided weekly counts with data assigned to an\n" +
						"arbitrary day in the week for which counts are available.\n\n";
					break;
				}
			}
		} else if ( p[i] == "WEL" ) {
			for ( var j = 0; j < y.length; j++ ) {
				if ( (y[j] >= 2007) && (y[j] <= 2008) ) {
					for ( var k = 0; k < s.length; k++ ) {
						if ( s[k] == "fl" ) {
							warnFlag = 1;
							warn = warn + "Lamprey counts at Wells Dam artificially lowered by tagging\n" +
									"and trapping efforts during 2007-2008.\n\n";
							break;
						}
					}
				}
			}
		} else if ( p[i] == "RRH" ) {
			for ( var j = 0; j < y.length; j++ ) {
				if ( y[j] == 2005 ) {
					warnFlag = 1;
					warn = warn + "Video was lost at Rocky Reach Dam from 1751 hours on 8 June 2005 and was not\n" +
						"returned to operation until 0729 hours on 9 June 2005. As a result, the adult passage\n" +
						"counts at Rocky Reach Dam are incomplete for both days.\n\n";
					break;
				}
			}
		} else if ( p[i] == "ZOS" ) {
			for ( var j = 0; j < y.length; j++ ) {
				if ( y[j] == 2008 ) {
					for ( var k = 0; k < s.length; k++ ) {
						if ( s[k] == "fb" ) {
							warnFlag = 1;
							warn = warn + "Zosel Dam estimated 2008 adult sockeye passage counts missed due to equipment malfunction.\n" +
							"15 May 12:00 - 20 May 12:00 = 0\n" +
							"17 Jul 00:00 - 18 Jul 19:00 = 6678\n" +
							"09 Aug 10:00 - 12 Aug 12:00 = 649\n" +
							"31 Aug 00:00 - 04 Sep 10:00 = 570\n" +
							"Total Missed Sockeye count = 7897\n" +
							"Initial 2008 Sockeye count = 77533\n" +
							"Revised 2008 Sockeye count = 85430\n" +
							"At this time, missed sockeye counts are not reflected in query results.\n\n";
							break;
						}
					}
				}
			}
		}
	}

	if ( warnFlag ) {
		if ( warnWkFlag ) {
			if ( o == "plot" ) {
				warn = warn + "To view the weekly data values for Output Format 'Graph', check\n" +
					"'Graph Nulls' in the Optional section.\n";
			}
		}
		alert(warn);
	}
	return true;
}

// pass.html
function validateSmoltYear() {
	// validate project and year selection for Smolt Index Passage query pages

	var projElement = document.query.proj;
	var yearElement = document.query.year;
	var outElement = document.query.outputFormat;

	var o;
	for ( var i = 0; i < outElement.length; i++ ) {
		if ( outElement[i].checked ) {
			 o = outElement[i].value;
		}
	}

	var p = new Array();
	var y = new Array();
	var yCount = 0;
	for ( var i = 0; i < yearElement.length; i++ ) {
		if ( yearElement[i].selected ) {
			var j = y.push(yearElement[i].value);
			yCount++;
		}
	}

	if ( o == "fancyReport" ) {
		var message = "";
		if ( yCount > 1 ) {
			// this option is not allowed because of leap year
			message = message + "Only one (1) selection for " +
			"parameter Year is allowed " +
			"for 'View Data Table (MM/DD)' output format, " +
			"which aligns data by month and day.\n" +
			"Please select a single year from the Year " +
			"parameter menu for this output format.\n" +
			"Multiple year selections are allowed for the " +
			"'View Data Table (DOY)' output format, " +
			"which aligns data by day of year.\n\n";

		}
		if ( message ) {
			alert("QUERY ALERT\n\n" + message + "After making " +
			"new selections, click 'Begin Run'.");
			return false;
		}
	}

	for ( var i = 0; i < projElement.length; i++ ) {
		if ( projElement[i].selected ) {
			var j = p.push(projElement[i].value);
		}
	}

	var warn = "WARNING\n";
	var warnFlag = 0;

	// cycle through project array "p" looking for "MCN"
	for ( var i = 0; i < p.length; i++ ) {
		if ( p[i] == "MCN" ) {
			for ( var j = 0; j < y.length; j++ ) {
				if ( y[j] >= 2003 ) {
					warnFlag = 1;
					warn = warn + "Since 2003, McNary operations result in sampling every other day during the spring,\n" +
						"approximately April 1 through June 30. \n\n";
					break;
				}
			}
		}
	}

	if ( warnFlag ) {
		if ( o == "plot" ) {
			warn = warn + "To view the data values for Output Format 'Graph', check\n" +
				"'Graph Nulls' in the Optional section.\n";
		}
		alert(warn);
	}
	return true;
}

