var i = 0;
var currYear = new Date();
var currYear = currYear.getFullYear();
var month = new Array('01','02','03','04','05','06','07','08','09','10','11','12');
var month_fullname = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
node = new Array("PHL","ABE","RDG","ACY","TTN","GED","ILG","MPO");
node_fullname = new Array("Philadelphia PA","Allentown PA","Reading PA","Atlantic City NJ","Trenton NJ","Georgetown DE","Wilmington DE","Mt. Pocono PA");

function jumpAlert() { 
  window.alert('You are now leaving this National Weather Service (NWS) web site.\n\nThe link you have chosen will take you to an external web site, with the link provided by the NWS because it may contain information of interest to you. This link does not constitute an endorsement by the NWS of any information, products or services on the external site.\n\nYou can return to this NWS web site by using the back button on your web browser.') 
} 

function startForm(){
  var today = new Date();
  today.getDate();
  monthIndex = today.getMonth();
  document.dailyArchive.monthIn.selectedIndex = monthIndex;
  document.monthlySummaryArchive.monthIn.selectedIndex = monthIndex;
  // year selector starts with 2002 -- 
  yearIndex = today.getYear();
  // Netscape 4.9 (and maybe other versions has a bug with getYear() -- this accounts for it
  if (yearIndex < 200){
    yearIndex = yearIndex + 1900;
  }
  yearIndex = yearIndex - 2002 + 5;
  document.dailyArchive.yearIn.selectedIndex = yearIndex;
  document.monthlySummaryArchive.yearIn.selectedIndex = yearIndex;
}

function CheckBrowser(){  
var ans = true;  
var ver = 0;  
ver = eval(window.navigator.appVersion.substring(0,1));  
if (window.navigator.appName == 'Netscape'){    
  if (ver < 5)      
    ans = false;  
  }  
msg = 'Some of the features available from this web page may not work correctly with your browser.\n\n';  msg = msg + 'Your browser is: ' + navigator.appName + " " + navigator.appVersion;  msg = msg + '\n\nThis page has been designed and tested for browsers equivelent to Netscape 6 and ';  msg = msg + 'Internet Explorer 4 and greater. It has also been tested against Mozilla 1.2.1 and Opera 7.0.';  
if (ans == false) {    
  alert( msg );  
  }
}

function getLocation(index){
  switch (index) {
  case 0: return "PHL"; break;
  case 1: return "ABE"; break;
  case 2: return "RDG"; break;
  case 3: return "ACY"; break;
  case 4: return "TTN"; break;
  case 5: return "GED"; break;
  case 6: return "ILG"; break;
  case 7: return "MPO"; break;
  }
}

function getLocationText(index){
  switch (index) {
  case 0: return "Philadelphia PA"; break;
  case 1: return "Allentown PA"; break;
  case 2: return "Reading PA"; break;
  case 3: return "Atlantic City NJ"; break;
  case 4: return "Trenton NJ"; break;
  case 5: return "Georgetown DE"; break;
  case 6: return "Wilmington DE"; break;
  case 7: return "Mt. Pocono PA"; break;
  }
}

function getLocation4daily(){
  switch (document.dailyArchive.location.selectedIndex) {
  case 0: return "l"; break;
  case 1: return "e"; break;
  case 2: return "r"; break;
  case 3: return "y"; break;
  case 4: return "n"; break;
  case 5: return "d"; break;
  case 6: return "g"; break;
  case 7: return "m"; break;
  }
}

function getMonthlySummary(){
  var OkToProceed = false;
  var theMonth = document.monthlySummaryArchive.monthIn.selectedIndex + 1;
  var theYear = document.monthlySummaryArchive.yearIn[document.monthlySummaryArchive.yearIn.selectedIndex].value;
  var theFile ="";
  var today = new Date();
  today.getDate();
  var tmonth = today.getMonth() + 1;
  var tyear = today.getYear();
  var obLocation = getLocation(document.monthlySummaryArchive.location.selectedIndex);
  obLocation = obLocation.toLowerCase();
  // Netscape 4.9 (and maybe other versions has a bug with getYear() -- this accounts for it
  if (tyear < 200){
    tyear = tyear + 1900;
  }
  yearStr = theYear.toString();
  yearStr = yearStr.substring(2,4);
  //  Can't get future data
  // tmonth + tyear is the "most current" month that can be checked
  if (tmonth == 1) {
    tmonth = 12;
	tyear = tyear - 1;
  }
  else
   tmonth = tmonth - 1;
  //alert("tmonth=" + tmonth + "  theMonth=" + theMonth + " tyear="+tyear + "  theYear=" + theYear );
  if (( theYear == tyear  ) && (theMonth <= tmonth))
	  OkToProceed = true;
  if (( theYear < tyear  ) && (theYear > 1996))
	  OkToProceed = true;
  // Stop if an invalid date has been entered
  if (OkToProceed == false) {
  	alert("The Monthly Climatic Summary archive is not available for \n" + getLocationText(document.monthlySummaryArchive.location.selectedIndex) + "  (" + theMonth + "/" + theYear + ").");
	return;
  }
  //
  // Observations are not on the web server for Trenton, Georgetown or Reading prior to 2002
  //
  theLocation = obLocation;
  theLocation = theLocation.toLowerCase();	 
  if ((theYear < 2002) && ((theLocation == "ged")||(theLocation == "rdg")||(theLocation == "ttn")||(theLocation == "mpo")) )
      OkToProceed = false;
  // Mount Pocono started December 2002
  if (OkToProceed == true)
     if ((theYear  == 2002) && (theLocation == "mpo") && (theMonth < 12) )
        OkToProceed = false;
  if (OkToProceed == false){
    alert("The archive for [" + getLocationText(document.monthlySummaryArchive.location.selectedIndex) + " " + theMonth + "/" + theYear + "] is not stored on this server.\n\nPlease check the National Climatic Data Center.");
	return;
  }
  //
  // The following blocks account for the different formats used to store this data
  //
  // Post October 2002 format is PHLCLMlllmmyyyy.txt
  //
  if (theFile == "")
   if ( (theYear > 2002) || ((theYear > 2001) && (theMonth > 10)) ) {   
     theFile = "climate/archives/PHLCLM" + obLocation.toUpperCase();
     if (theMonth.toString().length < 2) 
       theFile += "0" + theMonth.toString();
     else
       theFile += theMonth.toString();
     theFile += theYear.toString();
     theFile += ".txt";
   } // post October 2002
  //
  // pre November 2002 for 2002
  //
  if (theFile == "") {
    if (theYear == 1997)
	  theFile = "climat/" + obLocation;
	else
      theFile = "climat/" + theYear.toString() + "/" + obLocation;
	theFile = theFile.toLowerCase();
	if (theYear==2002){
      if (theMonth.toString().length < 2) 
        theFile += "0" + theMonth.toString();
      else
        theFile += theMonth.toString();
	}
	else
      theFile += theMonth.toString();
	// strange format here ??
	if ((theYear == 2001) && ((theMonth==1)||(theMonth==2))) 
	  theFile += "00";
	else
	  theFile += yearStr;
    theFile += ".txt";
   }
//alert( theFile );
  if (theFile != "")
    window.location.href = theFile ;
}

function getDailyInfo(){
  var OkToProceed = false;
  var theMonth = document.dailyArchive.monthIn.selectedIndex + 1;
  var theYear = document.dailyArchive.yearIn[document.dailyArchive.yearIn.selectedIndex].value;
  var theFile ="";
  var today = new Date();
  today.getDate();
  var tmonth = today.getMonth() + 1;
  var tyear = today.getYear();
  var obLocation = getLocation4daily();
  obLocation = obLocation.toLowerCase();
  // Netscape 4.9 (and maybe other versions has a bug with getYear() -- this accounts for it
  if (tyear < 200){
    tyear = tyear + 1900;
  }
  yearStr = theYear.toString();
  yearStr = yearStr.substring(2,4);
  //  Can't get future data
  // tmonth + tyear is the "most current" month that can be checked
  if (tmonth == 1) {
    tmonth = 12;
	tyear = tyear - 1;
  }
  else
   tmonth = tmonth - 1;
  //alert("tmonth=" + tmonth + "  theMonth=" + theMonth + " tyear="+tyear + "  theYear=" + theYear );
  if (( theYear == tyear  ) && (theMonth <= tmonth))
	  OkToProceed = true;
  if (( theYear < tyear  ) && (theYear > 1996))
	  OkToProceed = true;
  // Stop if an invalid date has been entered
  if (OkToProceed == false) {
  	alert("The Daily Information archive is not available for \n" + getLocation(document.dailyArchive.location.selectedIndex) + "  (" + theMonth + "/" + theYear + ").");
	return;
  }
  //
  // Observations are not on the web server for Trenton, Georgetown, Mount Pocono or Reading prior to 2002
  //
  theLocation = obLocation;
  theLocation = theLocation.toLowerCase();	
  if ((theYear < 2002) && ((theLocation == "r")||(theLocation == "m")||(theLocation == "d")||(theLocation == "n")) )
      OkToProceed = false;
  // Mount Pocono started December 2002
  if (OkToProceed == true)
     if ((theYear  == 2002) && (theLocation == "m") && (theMonth < 12) )
        OkToProceed = false;
  if (OkToProceed == false){
    alert("The archive for [" + getLocationText(document.dailyArchive.location.selectedIndex) + " " + theMonth + "/" + theYear + "] is not stored on this server.\n\nPlease check the National Climatic Data Center.");
	return;
  }
  //
  // The following blocks account for the different formats used to store this data
  //
  // Post October 2002 format is lllLCDmmyyyy.txt where  is the single letter locoation identifier
  //
  if (theFile == "")
   if ( (theYear > 2002) || ((theYear > 2001) && (theMonth > 10)) ) {   
     theFile = "climate/archives/" +  getLocation(document.dailyArchive.location.selectedIndex) + "LCD";
     if (theMonth.toString().length < 2) 
       theFile += "0" + theMonth.toString();
     else
       theFile += theMonth.toString();
     theFile += theYear.toString();
     theFile += ".txt";
   } // post October 2002
  // alert(theFile);
  //
  // pre december 2002 
  //
  if (theFile == "") {
    if (theYear == 1997)
	  theFile = "climat/" 	
	else
      theFile = "climat/" + theYear.toString() + "/";
    if (theMonth.toString().length < 2) 
      theFile += "0" + theMonth.toString() + theYear.toString();
    else
      theFile += theMonth.toString() + theYear.toString();
	//
	// Get the proper extension
	//
	if ( (theYear==2000) && ((theMonth==1)||(theMonth==2)||(theMonth==3)))
		theFile += obLocation + ".htm";
	else if ((theYear==1999)||(theYear==1998)) 
		theFile += obLocation + ".htm";
	else if (theYear==1997) 
		theFile += obLocation.toUpperCase() + ".HTM";
	else
		theFile += obLocation + ".txt";
   }
//alert( theFile );
 if (theFile != "")
 	window.location.href = theFile ;
}

function RunReportHelp(){ 
  msg = "To run the reports for the current period:\n  - Select the location, select the type of report, then hit Run Report\n\n";
  msg += "These products are based on data recieved from the following locations:\n\n";
  msg += "Allentown, Pennsylvania (ABE) - From the Lehigh Valley International Airport\n";
  msg += "Georgetown, Delaware (GED) - From the Sussex County Airport\n";
  msg += "Philadelphia, Pennsylvania (PHL) - From the Philadelphia International Airport\n";
  msg += "Reading, Pennsylvania (RDG) - From the Reading Regional / Carl A Spatz Field Airport\n";
  msg += "Trenton, New Jersey (TTN) - From the Trenton-Mercer Airport\n";
  msg += "Wilmington, Delaware (ILG) - From the New Castle County Airport\n";
  msg += "Mt. Pocono, Pennsylvania (MPO) - From the Mount Pocono, Pocono Mountains Municipal Airport\n\n";
  msg += "";
  msg += "";
  msg += "The Seasonal Snowfall Totals are through last winter.\n\n";
  msg += "The daily summary is issued twice daily. Yesterday's summary was issued early this morning. \nThe summary for the current day is issued early each evening.\n\n";
  msg += "";
  msg += "Some web browsers may not initialize this form correctly. If the desired report is not run, or if you \nget an error, hit the reset button and rerun the report.";
  msg += "";
  msg += "";
  msg += "";
  alert( msg );
}

function RunClimateReport(){
  //
  // Make sure the user selected a location and a report
  //
  if (document.fcurrentReport.locationVal.value ==""){
    alert("Select a location.");
  	return;
  }
  if (document.fcurrentReport.reportVal.value ==""){
    alert("Select a report.");
  	return;
  }
  var baseDir = new String("");
  var reportParams = new String("");
  reportParams = document.fcurrentReport.reportVal.value + document.fcurrentReport.locationVal.value ;
  // 
  // Seasonal snowfall reports aren't available for all locations
  //
  if ((document.fcurrentReport.reportVal.value == "_season.gif") &&( (document.fcurrentReport.locationVal.value == "GED") ||(document.fcurrentReport.locationVal.value == "RDG")||(document.fcurrentReport.locationVal.value == "TTN")||(document.fcurrentReport.locationVal.value == "MPO"))){
  	alert(" Seasonal Snowfall Totals are not available for " + getFullName(document.fcurrentReport.locationVal.value));
	return;
  }
  if ((document.fcurrentReport.reportVal.value == "PHLCLI") ||(document.fcurrentReport.reportVal.value == "PHLCLM")||(document.fcurrentReport.reportVal.value == "PHLRER")){
  	//baseDir = "http://www.srh.noaa.gov/data/PHI/" +  reportParams.slice(3,9);
      baseDir = 'http://www.srh.noaa.gov/fwd/productviewnation.php?pil=PHICLI'+document.fcurrentReport.locationVal.value + '&version=0&max=7';
    }
  else 
    if (document.fcurrentReport.reportVal.value == "output_f6_K") {
  	  //baseDir += "products/"+reportParams;
      // Changed 5-8-2006 D. Hagarty, ITO WFOPHI
      baseDir = 'http://www.srh.noaa.gov/fwd/productviewnation.php?pil=PHICF6'+document.fcurrentReport.locationVal.value + '&version=0';
    }
  else 
    if (document.fcurrentReport.reportVal.value == "climate.html")
  	  baseDir += "climate/" + document.fcurrentReport.locationVal.value.toLowerCase() + document.fcurrentReport.reportVal.value;
  else 
    if (document.fcurrentReport.reportVal.value == "_season.gif")
  	  baseDir += "images/" + document.fcurrentReport.locationVal.value.toLowerCase() + document.fcurrentReport.reportVal.value;
//alert(baseDir);
  if (baseDir != "")
 	window.location.href = baseDir ;
}

function getFullName( loc ) {
if (loc.toUpperCase()=="PHL")
  return "Philadelphia, PA";
if (loc.toUpperCase()=="GED")
  return "Georgetown, DE";
if (loc.toUpperCase()=="ILG")
  return "Wilmington, DE";
if (loc.toUpperCase()=="TTN")
  return "Trenton, NJ";
if (loc.toUpperCase()=="ABE")
  return "Allentown, PA";
if (loc.toUpperCase()=="ACY")
  return "Atlantic City, NJ";
if (loc.toUpperCase()=="RDG")
  return "Reading, PA";
if (loc.toUpperCase()=="MPO")
  return "Mount Pocono, PA";
return "";
}
