
//
// County Info
//
var PA = new Array("", "Berks","Bucks","Carbon","Chester","Delaware","Lehigh","Montgomery","Monroe", "Northhampton","Philadelphia","Pike","Schuylkill", "Wayne");
var NJ = new Array("", "Atlantic", "Burlington", "Camden", "Cape May", "Cumberland", "Gloucester", "Hunterdon", "Mercer", "Middlesex", "Monmouth", "Morris", "Ocean", "Salem", "Somerset", "Sussex", "Warren");
var DE = new Array("", "Kent", "New Castle", "Sussex");
var MD = new Array("", "Caroline", "Cecil", "Kent", "Queen Anne's", "Talbot");
//
// Unit of measure values
//
var arrUMValues = new Array("", "IN", "FT", "MPH", "DEG", "MB");
var arrUMText = new Array("", "Inches", "Feet", "Mile/Hour", "Degrees", "mb.");
//
// Types of weather
//
var typesOfWeather = new Array("", "Snow", "Rain", "Sleet", "Freezing Rain", "Hail (report to the nearest 1/4 in.)", "Flooding - River or Stream", "Flooding - Coastal", "Wind - Peak Gust", "Wind - Damage", "Wind - Damage from Thunderstorm", "Ice - Amount", "Ice - Damage", "Temperature (High or Low)", "Tornado", "Lowest Pressure (Hurricane Only)", "-Other-");

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 validateForm(formobj){
  var flag = false;
	//1) Enter name of mandatory fields
	var fieldRequired = Array("Reported_By", "City", "County", "State", "Phone", "toe", "Type_Of_Weather");
	//2) Enter field description to appear in the dialog box
	var fieldDescription = Array("Name", "City", "County", "State", "Phone","Time","Type of Weather");	
  //3) Enter dialog message
	var alertMsg = "Please complete the following fields:\n";
	var l_Msg = alertMsg.length;
  today = new Date();
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}
	if (alertMsg.length == l_Msg){
		flag = true;
	}
  else {
		alert(alertMsg);
		flag = false;
	}
  if (flag == true) {
    //
    // Update the toe Object (time of event)
    //
    vMonth = document.f1.mI.selectedIndex +1;
    sMonth = vMonth.toString();
    if (sMonth.length == 1) {
      sMonth = "0" + sMonth;
    }
    vDay = document.f1.dI.selectedIndex + 1;
    sDay = vDay.toString();
    if (sDay.length == 1) {
      sDay = "0" + sDay;
    }
    //
    // Format the values to be sent out
    //
    document.f1.Time_Date.value = document.f1.toe.value + " " + document.f1.aP.value + "   " + sMonth + "/" + sDay;
    document.f1.Amount.value = document.f1.na.value + " " + document.f1.aum.value;
    document.f1.Storm_Total.value = document.f1.sta.value + " " + document.f1.stum.value;
    document.f1.Submitted.value = Date().toString();
    //
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
    // This is where the observation line is generated
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
    //
    var paddingFactor = 0;
    var cityLength = 25;
    var colWidth = 11;
    var myTab = "   ";  // use this instead of \t to ensure consistency in spacing
    vst = new String();
    vst = document.f1.State.value;
    loc = new String(document.f1.City.value);
    amt = new String();
    amt = document.f1.na.value;
    stamt = new String();
    stamt = document.f1.sta.value;
    // get full state spelling
    if (vst == "NJ")
      vst = "New Jersey";
    if (vst == "MD")
      vst = "Maryland";
    if (vst == "DE")
      vst = "Delaware";
    if (vst == "PA")
      vst = "Pennsylvania";
    obStr = new String("\n\n");
    obStr = obStr + document.f1.Type_Of_Weather.value + "\n"; 
    obStr = obStr + vst + "\n";
    obStr = obStr + "..." + document.f1.County.value + " County... \n";

    //
    // Column headings
    //
    for(i=0;i<cityLength;i++){obStr = obStr + " ";}
    // set amounts to 15 characters wid
    if (amt != "")
      obStr = obStr + "NEW AMOUNT " + myTab;
    if (stamt != "")
      obStr = obStr + "STORM TOTAL";
    obStr = obStr + "\n";
    for(i=0;i<cityLength;i++){obStr = obStr + " ";}
    if (amt != ""){
      for(i=0; i<colWidth; i++){obStr = obStr + "-";}
      obStr = obStr  + myTab;
    }
    if (stamt != ""){
      for(i=0; i<colWidth; i++){obStr = obStr + "-";}
    }
    obStr = obStr + "\n";
    paddingFactor = cityLength - loc.length;
    
    //
    // Observation Line
    //
    obStr = obStr + document.f1.City.value.toUpperCase();
    for (i=0; i < paddingFactor; i++){obStr=obStr+" ";}
    if (amt != ''){
      obStr = obStr + document.f1.Amount.value;
      for (i=0; i < (colWidth - document.f1.Amount.value.length); i++) { obStr = obStr + " ";}
      obStr = obStr + myTab;
    }
    if (stamt != ''){
      obStr = obStr + document.f1.Storm_Total.value;
      for (i=0; i < (colWidth - document.f1.Storm_Total.value.length); i++) { obStr = obStr + " ";}
      obStr = obStr + myTab;
    }
    obStr = obStr + document.f1.Time_Date.value  + myTab;
    obStr = obStr + document.f1.Description.value ;
    
    //
    // The rest of the message has the form input statements
    //
    obStr = obStr + "\n\n";
    document.f1.Observation.value = obStr;
    document.f1.Browser.value = window.navigator.appName + " " + window.navigator.appVersion ;
    alert("Thank you for submitting this report, it will be emailed to the Mt. Holly office Storm Focal Point.");
    if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion >= "4.0")
    	window.history.back();
  }
  return flag;
}

function updateTimeOfEvent(){
  today = new Date();
  today.getDate();
  //
  // Time
  //
  theHour = today.getHours();
  if (theHour > 11) {
    theHour = theHour - 12;
    document.f1.aP.selectedIndex = 1;
  }
  else {
    document.f1.aP.selectedIndex = 0;
  }
  sMinutes = today.getMinutes().toString();
  if ( sMinutes.length == 1 ){
    sMinutes = "0" + sMinutes;
  }
  sHour = theHour.toString();
  if ( sHour.length == 1){
    sHour = "0" + sHour;
  }
  document.f1.toe.value = sHour + ":" + sMinutes ;

  //
  // Update the date selectors
  //
  document.f1.mI.selectedIndex = today.getMonth();
  document.f1.dI.selectedIndex = today.getDate()-1;
  // year selector gets a three year window
  yIdex = today.getYear();
  // Netscape 4.9 (and maybe other versions has a bug with getYear() -- this accounts for it
  if (yIdex < 2000){
    yIdex = yIdex - 100 + 2000;
  }
  document.f1.yI.options[0].text = yIdex-1;
  document.f1.yI.options[0].value = yIdex-1;
  document.f1.yI.options[1].text = yIdex;
  document.f1.yI.options[1].value = yIdex;
  document.f1.yI.options[2].text = yIdex+1;
  document.f1.yI.options[2].value = yIdex+1;
  document.f1.yI.selectedIndex = 1;
}

function setOptionText(the_select, the_array)
{
  for (loop=0; loop < the_select.options.length; loop++)
  {
    if (the_array[loop] == undefined) {
      the_select.options[loop].text = '';
      the_select.options[loop].value = '';
    }
    else {
      the_select.options[loop].text = the_array[loop];
      the_select.options[loop].value = the_array[loop];
    }
  }
  the_select.selectedIndex = 0;
}

function SetCountySelect( the_array_name )
{
  if (the_array_name == '') {
    return;
  }
  var numbers_select = document.f1.County;
  var the_array = eval(the_array_name);
  setOptionText(document.f1.County, the_array);
}

function update_dI(){
  maxVal = 31;
  if (document.f1.mI.value=='Feb') 
    maxVal = 28;
  if (document.f1.mI.value=='Apr') 
    maxVal = 29;
  if (document.f1.mI.value=='Jun') 
    maxVal = 29;
  if (document.f1.mI.value=='Sep') 
    maxVal = 29;
  if (document.f1.mI.value=='Nov') 
    maxVal = 29;
  for (i=0; i < 31; i++){
      if (i > maxVal){
        document.f1.dI.options[i].text = '';
        document.f1.dI.options[i].value = '';
      }
      else {
        document.f1.dI.options[i].text = i + 1;
        document.f1.dI.options[i].value = i + 1;
      }
  }
  document.f1.dI.selectedIndex = 0;
}

function initTypesOfWeather(){
  var tw = new String;
  for (i=1; i < (typesOfWeather.length); i++) {  
    tw = typesOfWeather[i].toUpperCase();
    if (tw.substring(0,4) == "HAIL")
      { 
      document.f1.Type_Of_Weather.options[i].value = "HAIL";
      document.f1.Type_Of_Weather.options[i].text = typesOfWeather[i];
      }
    else
      {
      document.f1.Type_Of_Weather.options[i].value = typesOfWeather[i].toUpperCase();
      document.f1.Type_Of_Weather.options[i].text = typesOfWeather[i];
      }
  }
  document.f1.Type_Of_Weather.selectedIndex = 0;
}

function updateUnitOfMeasure(typeOfWeather){
  var mode = 0;
  // Set the mode 
  if (typeOfWeather.substring(0,4).toUpperCase()=="WIND")
    mode = 1;
  if (typeOfWeather.substring(0,4).toUpperCase()=="TORN")
    mode = 1;
  if (typeOfWeather.substring(0,4).toUpperCase()=="TEMP")
    mode = 2;
  if (typeOfWeather.substring(0,4).toUpperCase()=="LOWE")
    mode = 3;
  if (typeOfWeather.substring(0,4).toUpperCase()=="HAIL")
    mode = 4;
  if (typeOfWeather.substring(0,6).toUpperCase()=="-OTHER")
    mode = 10;
  switch (mode){
  case 1 : 
    for (i=0; i < arrUMValues.length; i++)
      if (i==1) 
        {
        document.f1.aum.options[i].value = "MPH";
        document.f1.aum.options[i].text = "Miles/Hour";
        document.f1.stum.options[i].value = "MPH";
        document.f1.stum.options[i].text = "Miles/Hour";
        }
      else 
        {
        document.f1.aum.options[i].value = "";
        document.f1.aum.options[i].text = "";
        document.f1.stum.options[i].value = "";
        document.f1.stum.options[i].text = "";
        }
    break;
  case 2 : 
    for (i=0; i < arrUMValues.length; i++)
      if (i==1) 
        {
        document.f1.aum.options[i].value = "DEG";
        document.f1.aum.options[i].text = "Degrees";
        document.f1.stum.options[i].value = "DEG";
        document.f1.stum.options[i].text = "Degrees";
        }
      else 
        {
        document.f1.aum.options[i].value = "";
        document.f1.aum.options[i].text = "";
        document.f1.stum.options[i].value = "";
        document.f1.stum.options[i].text = "";
        }
    break;
  case 3 : 
    for (i=0; i < arrUMValues.length; i++)
      if (i==1) 
        {
        document.f1.aum.options[i].value = "IN";
        document.f1.aum.options[i].text = "Inches";
        document.f1.stum.options[i].value = "IN";
        document.f1.stum.options[i].text = "Inches";
        }
      else if (i==2)
        {
        document.f1.aum.options[i].value = "MB";
        document.f1.aum.options[i].text = "mb.";
        document.f1.stum.options[i].value = "MB";
        document.f1.stum.options[i].text = "mb.";
        }
      else 
        {
        document.f1.aum.options[i].value = "";
        document.f1.aum.options[i].text = "";
        document.f1.stum.options[i].value = "";
        document.f1.stum.options[i].text = "";
        }
    break;    
  case 4 : 
    for (i=0; i < arrUMValues.length; i++)
      if (i==1) 
        {
        document.f1.aum.options[i].value = "IN";
        document.f1.aum.options[i].text = "Inches";
        document.f1.stum.options[i].value = "IN";
        document.f1.stum.options[i].text = "Inches";
        }
      else 
        {
        document.f1.aum.options[i].value = "";
        document.f1.aum.options[i].text = "";
        document.f1.stum.options[i].value = "";
        document.f1.stum.options[i].text = "";
        }
    break;    
  case 10:
    for (i=0; i < arrUMValues.length; i++)  
        {
        document.f1.aum.options[i].value = arrUMValues[i];
        document.f1.aum.options[i].text = arrUMText[i];
        document.f1.stum.options[i].value = arrUMValues[i];
        document.f1.stum.options[i].text = arrUMText[i];
        }
    break;
  default :
    for (i=0; i < arrUMValues.length; i++)  
      if ((i==1) || (i==2))
        {
        document.f1.aum.options[i].value = arrUMValues[i];
        document.f1.aum.options[i].text = arrUMText[i];
        document.f1.stum.options[i].value = arrUMValues[i];
        document.f1.stum.options[i].text = arrUMText[i];
        }
      else 
        {
        document.f1.aum.options[i].value = "";
        document.f1.aum.options[i].text = "";
        document.f1.stum.options[i].value = "";
        document.f1.stum.options[i].text = "";
        }
  }//end switch
  document.f1.aum.selectedIndex = 1;
  document.f1.stum.selectedIndex = 1;
}// function end

function initForm()
{
  update_dI();
  updateTimeOfEvent();
  initTypesOfWeather();
}

function fullReset()
{
  document.f1.reset();
  initForm();
}

function CheckBrowser(){
  var browserOk = false;
  //
  // Check browser type
  //
  browserOk = (document.getElementById() || document.all) ? true : false;
  // 
  // Redirect the page if the browser is not supported
  //  
  if ( browserOk != true)
  {
    window.location = 'nbwxreport.htm';
  }
}
 
