var isNav4, isIE4;
isNav4=false;
isIE4=false;

var p_project_cost_old_value = "";

// geographical boundaries of a project
var PROJ_GEO_OTHER            = 0;
var PROJ_STATE_WIDE           = 1;
var PROJ_COUNTY_MULTIPLE      = 2;
var PROJ_COUNTY_SINGLE        = 5;
var PROJ_INDIAN_RESERVATION   = 6;
var PROJ_REGIONAL   		= 7;


var isReset = false;
var isCancel = false;
var isChangeFlag = false;



function validateForm(frm)
{
  if (isReset || isChangeFlag || isCancel) return true;
  valid = true;




  if (valid) valid = checkString(frm.ff_project_title, 'Activity/Project Title');
  
  geoArea = selectedValue(frm.p_project_geo);

  if (valid) {
    if (geoArea == "") {
      alert("You muse select a geographic area for this Best Practice.");
      frm.p_project_geo.focus();
      valid = false;
    }
  }

  if (valid) {
    if (geoArea == PROJ_REGIONAL || geoArea == PROJ_INDIAN_RESERVATION) {
      valid = checkSelect(frm.ff_project_region, 'FEMA Region', 0);
    }
    else {
      valid = checkSelect(frm.ff_project_state, 'State', 0);
      if (valid) {
        if (geoArea == PROJ_COUNTY_MULTIPLE) {
          valid = checkNumberSelected(frm.ff_project_county, 'Activity/Project County', -1, -1, false, true);
        }
        else if (geoArea == PROJ_COUNTY_SINGLE) {
          valid = checkSelect(frm.ff_project_county, 'Activity/Project County', 0, false);
        }
      }
    }
  }

  if (valid) valid = checkSelect(frm.q_sector_type, 'Sector', 0);
  if (valid) valid = checkString(frm.q_hazard_types, 'Type of Hazard');
  if (valid) valid = checkString(frm.ff_project_type, 'Category/Activity/Project Type');
  if (valid) valid = checkNumberSelected(frm.ff_project_type, 'Category/Activity/Project Type', 3);
  if (valid) valid = checkNumberSelected(frm.ff_structure_type, 'Structure Type', 2, -1, true, false);


  if (valid) valid = checkSelect(frm.ff_project_start_mo, 'Activity/Project Start Month', 0);
  if (valid) {
    var pattern = /^\d{4,4}$/;
    var result = frm.ff_project_start_yr.value.match(pattern);
    if (result == null) {
      alert("Invalid Activity/Project Start Year. The year should have the form: YYYY");
      frm.ff_project_start_yr.focus();
      valid = false;
    } 
  }

  if (valid) {
    if (compareDates(selectedValue(frm.ff_project_start_mo)+"-"+frm.ff_project_start_yr.value,"MM-yyyy",formatDate(new Date(),"MM-yyyy"),"MM-yyyy") == 1 ) {
      alert("Invalid Activity/Project Start Date. The date cannot be in the future.");
      frm.ff_project_start_yr.focus();
      valid = false;
    }
  }

  if (frm.p_project_ongoing.checked == false) {
    if (valid) valid = checkSelect(frm.ff_project_end_mo, 'Activity/Project End Month', 0);
    if (valid) {
      var pattern = /^\d{4,4}$/;
      var result = frm.ff_project_end_yr.value.match(pattern);
      if (result == null) {
        alert("Invalid Activity/Project End Year. The year should have the form: YYYY");
        frm.ff_project_end_yr.focus();
        valid = false;
      } 
    }

    if (valid) {
      var tmpEndDate = new Date();
      tmpEndDate.setFullYear(tmpEndDate.getFullYear() +1);
      if (compareDates(selectedValue(frm.ff_project_end_mo)+"-"+frm.ff_project_end_yr.value,"MM-yyyy",formatDate(tmpEndDate,"MM-yyyy"),"MM-yyyy") == 1 ) {
        alert("The project end date must be less than 1 year from the present date, otherwise leave the status as ongoing.");
        frm.ff_project_end_yr.focus();
        valid = false;
      }
    }
    if (valid) {
      if (compareDates(selectedValue(frm.ff_project_start_mo)+"-"+frm.ff_project_start_yr.value,"MM-yyyy",selectedValue(frm.ff_project_end_mo)+"-"+frm.ff_project_end_yr.value,"MM-yyyy") == 1 ) {
        alert("The project end date must be after the project start date.");
        frm.ff_project_end_yr.focus();
        valid = false;
      }
    }
  }



  if (valid) valid = checkString(frm.ff_fund_source, 'Funding');


  if (valid) {
    var doEnable = checkEnableApplicationProjectNumber();
    if (doEnable) {
      valid = checkString(frm.applicationProjectNumber, 'Application/Project Number');
    }
  }


  if (valid) {
    if (getRadioButtonValue(frm.p_project_cost) != 2) {		// if not Unknown
      var strippedCost = stripCharsInBag(frm.ff_project_cost.value, ',$');
      if (!isFloat(strippedCost)) {
        alert("Cost must be a valid number.");
        frm.ff_project_cost.focus();
        valid = false;
      }
      else {
        strippedCost = stripCharsInBag(strippedCost, '.0');
        if (strippedCost == '') {		// nothing but zeroes
          alert("The value 'zero' can not be entered in the Cost field. Please enter a positive value or select Unknown at this time.");
          frm.ff_project_cost.focus();
          valid = false;
        }
      }

      if (valid && frm.nonFEMACost.value != '') {
        var strippedNonFEMACost = stripCharsInBag(frm.nonFEMACost.value, ',$');
        if (!isFloat(strippedNonFEMACost)) {
          alert("Non FEMA Cost must be a valid number.");
          frm.nonFEMACost.focus();
          valid = false;
        }
      }

      if (valid) {
        var costString = document.sstoryForm.ff_project_cost.value;
        costString = costString.replace(/\$|\,/g,'');
        if (costString != '') {
          var cost = parseFloat(costString);
    
          var nonfemaCostString = document.sstoryForm.nonFEMACost.value;
          nonfemaCostString = nonfemaCostString.replace(/\$|\,/g,'');
          if (nonfemaCostString == null || nonfemaCostString == '')
            nonfemaCostString = '0';
          var nonfemaCost = parseFloat(nonfemaCostString);

          if (cost <= nonfemaCost) { 
            alert("Non FEMA Cost must be less than the amount entered in the Cost field.");
            frm.nonFEMACost.focus();
            valid = false;
          }
        }
      }
    }
  }

  if (valid) {
    if (frm.benefit_costs_ratio.value.length > 0) {
      if (!isFloat(stripCharsInBag(frm.benefit_costs_ratio.value, ','))) {
        alert("Benefit Costs Ratio must be a valid number.");
        frm.benefit_costs_ratio.focus();
        valid = false;
      }
    }
  }

  if (valid) {
    if (frm.estimated_expected_net_benefits.length > 0) {
      if (!isFloat(stripCharsInBag(frm.estimated_expected_net_benefits, ',$'))) {
        alert("Estimated Expected Net Benefits must be a valid number.");
        frm.estimated_expected_net_benefits.focus();
        valid = false;
      }
    }
  }

  if (valid) {
    if (frm.ff_losses_avoided_amount.value.length > 0) {
      if (!isFloat(stripCharsInBag(frm.ff_losses_avoided_amount.value, ',$'))) {
        alert("Estimated Losses Avoided must be a valid number.");
        frm.ff_losses_avoided_amount.focus();
        valid = false;
      }
    }
  }
  
  if (valid) {
    if (frm.benefit_costs_ratio.value.length > 0 ||
        frm.estimated_expected_net_benefits.value.length > 0 ||
        frm.ff_losses_avoided_amount.value.length > 0)
      valid = checkString(frm.information_source, 'Information Source', false);
  }

  if (valid) valid = checkSelect(frm.ff_fund_recipient, 'Funding Recipient', 0, true);
  if (valid) valid = checkString(frm.ff_fund_recipient_name, 'Funding Recipient Organization', true);
  
  if (valid) valid = checkSelect(frm.ff_dis_dcld_flag, 'Federal Disaster', 0, true);  

  if (valid) {
    if (frm.ff_dis_dcld_flag.value == "1" && frm.ff_dis_name.options.length > 1) {
      valid = checkSelect(frm.ff_dis_name, 'Disaster Number, Date', 0);

      if (!valid) {
        if (frm.ff_dis_name.selectedIndex > -1) 
          frm.ff_dis_name.focus();
        else 
          frm.ff_dis_year.focus();
      }
    }
    else if (frm.ff_dis_dcld_flag.value == "0" && frm.ff_dis_year.value.length > 0) {
      if (isDate(frm.ff_dis_year.value,"yyyy") == false) {
        alert("The Year of disaster that resulted in mitigation effort is not valid.");
        valid = false;
      }

      if (valid) {
        if (compareDates(frm.ff_dis_year.value,"yyyy",frm.ff_project_start_yr.value,"yyyy") == 1 ) {
          alert("The Project Start Date must be after or the same as the Year of disaster that resulted in mitigation effort.");
          valid = false;
        }
      }

      if (!valid) 
        frm.ff_dis_year.focus();
    }
  }

  if (valid) valid = checkSelect(frm.ff_dis_ocrd_flag, 'Mitigation Effort Tested', 0, true);
  // changed to allow the default to be Unknown which is now the first option

  if (valid && (frm.ff_dis_ocrd_flag.value == "1")) {
    if (isDate(frm.ff_dis_tested_year.value,"yyyy") == false) {
      alert("The Disaster First Tested Year is not valid.");
      valid = false;
    }

    if (valid) {
      if (compareDates(frm.ff_dis_tested_year.value,"yyyy",formatDate(new Date(),"yyyy"),"yyyy") == 1 ) {
        alert("Invalid Disaster First Tested Year. The year cannot be in the future.");
        valid = false;
      }
    }

    if (valid) {
      if (compareDates(frm.ff_project_start_yr.value,"yyyy",frm.ff_dis_tested_year.value,"yyyy") == 1 ) {
        alert("The Project Start Date must be before or the same year as the date the mitigation effort was tested.");
        valid = false;
      }
    }

    if (!valid) {
      if (frm.ff_dis_name_tested.selectedIndex > -1) 
        frm.ff_dis_name_tested.focus();
      else 
        frm.ff_dis_tested_year.focus();
    }
  }



  if (valid) valid = checkString(frm.ff_submitted_by, 'Activity/Project Contact Name');

  if (valid) valid = checkString(frm.ff_details, 'Category/Activity/Project Details');

  if (valid && !isEmpty(frm.ff_submitted_url.value)) {
    valid = checkURLHost(frm.ff_submitted_url.value);
    if (!valid) {
      alert("The URL 1 value must be a valid web site url.");
      frm.ff_submitted_url.focus();
    }
    
  }
  if (valid && !isEmpty(frm.ff_submitted_url2.value)) {
    valid = checkURLHost(frm.ff_submitted_url2.value);
    if  (!valid) {
      alert("The URL 2 value must be a valid web site url.");
      frm.ff_submitted_url2.focus();
    }
  }
  return valid;
}



/**
 * Calculate the expected net benefits based on cost and cost
 * benefits.
 *
function calculateExpectedNetBenefits() {
  var netBenefit = 0;
  var t = /,/g; 
  costString = document.sstoryForm.ff_project_cost.value;
  costString = costString.replace(/\$|\,/g,'');
  cost = parseFloat(costString);
  costBenefitString = document.sstoryForm.benefit_costs_ratio.value;
  costBenefitString = costBenefitString.replace(/\$|\,/g,'');
  costBenefit = parseFloat(costBenefitString);
  if ((isFloat(costString)) && (isFloat(costBenefitString))) { 
    // alert("cost: " + cost + " costBenefit: " + costBenefit);
  if ((isFloat(cost)) && (isFloat(costBenefit))) {
    netBenefit = costBenefit - cost;
    if (isNaN(netBenefit) == false) {
      updateElementHtmlById("netBenefit", formatCurrency(netBenefit));
    }
    else {
      updateElementHtmlById("netBenefit", "Currently Unable to calculate");
    }
  }
  else {
    updateElementHtmlById("netBenefit", "Currently Unable to calculate");
  }
  }
  else {
    updateElementHtmlById("netBenefit", "Currently Unable to calculate");
  }
}
 **/


/* used to handle enabled/disabled state of region and state fields of the submission form depending on geoArea.
 */ 
function toggleRegionAndState(onInit) {
  //alert("in toggleRegionAndState");
  var geoArea = selectedValue(document.sstoryForm.p_project_geo);

  if (geoArea == PROJ_STATE_WIDE || geoArea == PROJ_COUNTY_MULTIPLE || geoArea == PROJ_COUNTY_SINGLE) {
    // make non-required, clear out and disable region
    updateElementHtmlById("locationRegion", "");
    if (onInit)
      document.sstoryForm.ff_project_region.selectedIndex = -1;
    disableField(document.sstoryForm.ff_project_region);
    document.getElementById("displayRegion").style.display = "none";

    // make required and enable state
    document.getElementById("displayState").style.display = "";
    updateElementHtmlById("locationState", "*");
    enableField(document.sstoryForm.ff_project_state);
  } 
  else if (geoArea == PROJ_INDIAN_RESERVATION || geoArea == PROJ_REGIONAL) {
    // make required and enable region
    document.getElementById("displayRegion").style.display = "";
    updateElementHtmlById("locationRegion", "*");
    enableField(document.sstoryForm.ff_project_region);

    // make non-required and disable state
    updateElementHtmlById("locationState", "");
    if (onInit) 
      document.sstoryForm.ff_project_state.selectedIndex = -1;
    disableField(document.sstoryForm.ff_project_state);
    document.getElementById("displayState").style.display = "none";
  } 
  else {
    // make non-required, clear out and disable region
    updateElementHtmlById("locationRegion", "");
    document.sstoryForm.ff_project_region.selectedIndex = -1;
    disableField(document.sstoryForm.ff_project_region);
    document.getElementById("displayRegion").style.display = "none";

    // make non-required and disable state
    updateElementHtmlById("locationState", "");
    document.sstoryForm.ff_project_state.selectedIndex = -1;
    disableField(document.sstoryForm.ff_project_state);
    document.getElementById("displayState").style.display = "none";
  }
}


function updateCountyWide() {
  //alert("in updateCountyWide");
  var geoArea = selectedValue(document.sstoryForm.p_project_geo);

  if (geoArea == PROJ_COUNTY_SINGLE && document.sstoryForm.ff_project_state.selectedIndex != -1) {
    // enable county-wide checkbox
    document.getElementById("displayCountywide").style.display = "";
    enableField(document.sstoryForm.p_project_county_wide);
  }
  else {
    // disable county-wide checkbox
    document.sstoryForm.p_project_county_wide.checked=false;
    disableField(document.sstoryForm.p_project_county_wide);
    document.getElementById("displayCountywide").style.display = "none";
  }
}


function updateCounties() {
  //alert("in updateCounties");
  var geoArea = selectedValue(document.sstoryForm.p_project_geo);

  if ((geoArea != PROJ_COUNTY_MULTIPLE && geoArea != PROJ_COUNTY_SINGLE) ||
      document.sstoryForm.ff_project_state.selectedIndex == -1) {
    // disable the county select box
    updateElementHtmlById("locationCounty", "");
    resetSelectBoxOptions(document.sstoryForm.ff_project_county, "", "");
    document.sstoryForm.ff_project_county.selectedIndex = -1;
    disableField(document.sstoryForm.ff_project_county);
    document.getElementById("displayCounty").style.display = "none";
  }
  else {
    // enable the county select box
    document.getElementById("displayCounty").style.display = "";
    updateElementHtmlById("locationCounty", "*");
    enableField(document.sstoryForm.ff_project_county);

    var isSingle = 'true';
    if (geoArea == PROJ_COUNTY_MULTIPLE) { 
      isSingle = 'false';
      changeSelectBoxAttributes(document.sstoryForm.ff_project_county, true, 4);
      document.getElementById("countyLink").style.display = "";
    }
    else {
      changeSelectBoxAttributes(document.sstoryForm.ff_project_county, false, 1);
      document.getElementById("countyLink").style.display = "none";
    }

    // send request for counties
    document.sstoryForm.ff_project_county.selectedIndex = -1;
    strRequest="getStateCounties.do" + 
               "?state=" + selectedValue(document.sstoryForm.ff_project_state) + 
               "&formName=sstoryForm&formElem=ff_project_county" + 
               "&isSingle=" + isSingle + 
               "&destination=forEdit";
    sendRequestStateCounties(strRequest);
  }
}


function updateCommunities() {
  //alert("in updateCommunities");
  var geoArea = selectedValue(document.sstoryForm.p_project_geo);

  if (geoArea != PROJ_COUNTY_SINGLE ||
      document.sstoryForm.p_project_county_wide.checked || 
      document.sstoryForm.ff_project_county.selectedIndex < 1) {
    // disable the community select box
    updateElementHtmlById("locationCommunity", "");
    resetSelectBoxOptions(document.sstoryForm.ff_project_city, "", "");
    document.sstoryForm.ff_project_city.selectedIndex = -1;
    disableField(document.sstoryForm.ff_project_city);
    document.getElementById("displayCommunity").style.display = "none";
  }
  else {
    // enable the community select box
    document.getElementById("displayCommunity").style.display = "";
    updateElementHtmlById("locationCommunity", "*");
    enableField(document.sstoryForm.ff_project_city);

    // send request for communities
    document.sstoryForm.ff_project_city.selectedIndex = -1;
    strRequest="getStateCommunities.do" + 
               "?state=" + selectedValue(document.sstoryForm.ff_project_state) + 
               "&county=" + selectedValue(document.sstoryForm.ff_project_county) + 
               "&formName=sstoryForm&formElem=ff_project_city&isSingle=false&destination=forEdit";
    sendRequestStateCommunities(strRequest);
  }
}


function updateDisasterYear() {
  //alert("in updateDisasterYear   " + document.sstoryForm.ff_dis_dcld_flag.value);

  if (document.sstoryForm.ff_dis_dcld_flag.value == "1") {	// ff_dis_dcld_flag is set to Yes
    document.getElementById("displayResultDisasterYear").style.display = "none";
    disableTextField(document.sstoryForm.ff_dis_year);
  }
  else {
    document.getElementById("displayResultDisasterYear").style.display = "";
    enableTextField(document.sstoryForm.ff_dis_year);
  }
}

function updateDisasterTestedYear() {
  //alert("in updateDisasterTestedYear");
  if (document.sstoryForm.ff_dis_ocrd_flag.value == "1") {	// ff_dis_dcld_flag is set to Yes
    // disaster tested year is visible and required
    document.getElementById("displayTestedDisasterYear").style.display = "";
    updateElementHtmlById("disTestedYear","*");

    // check if disaster tested year should be populated off the disaster tested number multiselect
    var leastYear = 3000; 
    if (document.sstoryForm.ff_dis_name_tested.selectedIndex > -1) {
      for (var i=document.sstoryForm.ff_dis_name_tested.selectedIndex; i<document.sstoryForm.ff_dis_name_tested.options.length; i++) {
        if (document.sstoryForm.ff_dis_name_tested.options[i].selected) {
          disasterDate = document.sstoryForm.ff_dis_name_tested.options[i].text;
          dis_arr = disasterDate.split("/");
          if (dis_arr.length == 3) {
            if (leastYear > dis_arr[2])
              leastYear = dis_arr[2];
          }
        }
      }
    }

    if (leastYear != 3000) {  // value comes from disaster tested number multiselect
      disableTextField(document.sstoryForm.ff_dis_tested_year);
      document.sstoryForm.ff_dis_tested_year.value = leastYear;
    }
    else  
      enableTextField(document.sstoryForm.ff_dis_tested_year);
  }
  else {
    updateElementHtmlById("disTestedYear","");
    document.sstoryForm.ff_dis_tested_year.value = '';
    disableTextField(document.sstoryForm.ff_dis_tested_year);
    document.getElementById("displayTestedDisasterYear").style.display = "none";
  }
}

function updateFederalDisasterEdit() {
  //alert("in updateFederalDisasterEdit");
  var numSelectedStates = getSelectedCount(document.sstoryForm.ff_project_state);

  if (numSelectedStates == 1) {		// there are disasters to choose from
    // show ff_dis_dcld_flag 
    document.sstoryForm.ff_dis_dcld_flag.style.display="";
    updateElementHtmlById("declaredDisasterTxt", "");

    if (document.sstoryForm.ff_dis_dcld_flag.value == "1") {	// ff_dis_dcld_flag is set to Yes
      updateElementHtmlById("declaredDisasterTxt", "");
	
      // enable and make required ff_dis_name
      document.getElementById("displayResultDisasterNumber").style.display = "";
      enableField(document.sstoryForm.ff_dis_name);
      updateElementHtmlById("disNumDate", "*");

      // send request for disasters
      strRequest="getFederalDisasters.do" + 
                  "?state=" + selectedValue(document.sstoryForm.ff_project_state) + 
                  "&startMonth=" + selectedValue(document.sstoryForm.ff_project_start_mo) + 
                  "&startYear=" + document.sstoryForm.ff_project_start_yr.value + 
                  "&causeDisaster=true&formName=sstoryForm&formElem=ff_dis_name&isSingle=true&destination=forEdit";
      sendRequestDisasterCaused(strRequest);
    }
    else {		// ff_dis_dcld_flag is set to No or Unknown
      // clear, disable and make non-required ff_dis_name
      resetSelectBoxOptions(document.sstoryForm.ff_dis_name);
      document.sstoryForm.ff_dis_name.selectedIndex = -1;
      disableField(document.sstoryForm.ff_dis_name);
      updateElementHtmlById("disNumDate", "");
      document.getElementById("displayResultDisasterNumber").style.display = "none";
    }
  }
  else {
    // set ff_dis_dcld_flag to No and hide it; show a text message instead
    document.sstoryForm.ff_dis_dcld_flag.selectedIndex = 1;
    document.sstoryForm.ff_dis_dcld_flag.style.display = "none";
    updateElementHtmlById("declaredDisasterTxt", "No (no federally declared disasters for this state/territory)");

    // clear, disable and make non-required ff_dis_name
    resetSelectBoxOptions(document.sstoryForm.ff_dis_name);
    document.sstoryForm.ff_dis_name.selectedIndex = -1;
    disableField(document.sstoryForm.ff_dis_name);
    updateElementHtmlById("disNumDate", "");
    document.getElementById("displayResultDisasterNumber").style.display = "none";
  }

  // populate ff_dis_year with a corresponding value
  updateDisasterYear(); 
}

function updateDisasterTestedEdit() {
  //alert("in updateDisasterTestedEdit");
  var numSelectedStates = getSelectedCount(document.sstoryForm.ff_project_state);

  if (numSelectedStates == 1) {		// there are disasters to choose from
    if (document.sstoryForm.ff_dis_ocrd_flag.value == "1") {	// ff_dis_ocrd_flag is set to Yes
      // enable ff_dis_name_tested
      document.getElementById("displayTestedDisasterNumber").style.display = "";
      enableField(document.sstoryForm.ff_dis_name_tested);

      // send request for disasters
      strRequest="getFederalDisasters.do" + 
                  "?state=" + selectedValue(document.sstoryForm.ff_project_state) + 
                  "&startMonth=" + selectedValue(document.sstoryForm.ff_project_start_mo) + 
                  "&startYear=" + document.sstoryForm.ff_project_start_yr.value + 
                  "&causeDisaster=false&formName=sstoryForm&formElem=ff_dis_name_tested&isSingle=false&destination=forEdit";
      sendRequestDisasterTested(strRequest);
    }
    else {		// ff_dis_ocrd_flag is set to No or Unknown
      // clear and disable ff_dis_name_tested
      resetSelectBoxOptions(document.sstoryForm.ff_dis_name_tested);
      document.sstoryForm.ff_dis_name_tested.selectedIndex = -1;
      disableField(document.sstoryForm.ff_dis_name_tested);
      document.getElementById("displayTestedDisasterNumber").style.display = "none";
    }
  }
  else {
    // clear and disable ff_dis_name_tested
    resetSelectBoxOptions(document.sstoryForm.ff_dis_name_tested);
    document.sstoryForm.ff_dis_name_tested.selectedIndex = -1;
    disableField(document.sstoryForm.ff_dis_name_tested);
    document.getElementById("displayTestedDisasterNumber").style.display = "none";
  }
	
  // populate ff_dis_tested_year with a corresponding value
  updateDisasterTestedYear(); 
}


/* updates Application/Project Number based on Funding */ 
function updateApplicationProjectNumber() {
  //alert("in updateApplicationProjectNumber");
  var doEnable = checkEnableApplicationProjectNumber();
  var isRequired = false;
  
  if (doEnable) {
    document.getElementById("displayProjectNumber").style.display = "";
    enableField(document.sstoryForm.applicationProjectNumber);
  }
  else {
    disableField(document.sstoryForm.applicationProjectNumber);
    document.sstoryForm.applicationProjectNumber.value = '';
    document.getElementById("displayProjectNumber").style.display = "none";
  }
}


/* check if Application/Project Number should be enabled */ 
function checkEnableApplicationProjectNumber() {
  //alert("in checkEnableApplicationProjectNumber");
  var doEnable = false;
  
  var options = document.sstoryForm.ff_fund_source.options;
  for (ind in options) {
    if (options[ind] != null && options[ind].selected) {
      if (options[ind].value == '215' || 		// Flood Mitigation Assistance (FMA)
          options[ind].value == '217' || 		// Hazard Mitigation Grant Program (HMGP)
          options[ind].value == '229') {		// Pre-Disaster Mitigation (PDM)
        doEnable = true;
        break;
      }
    }
  }

  return doEnable;
}


/* updates Information Source */ 
function updateInformationSource() {
  //alert("in updateInformationSource");
  if (document.sstoryForm.benefit_costs_ratio.value.length > 0 ||
      document.sstoryForm.estimated_expected_net_benefits.value.length > 0 ||
      document.sstoryForm.ff_losses_avoided_amount.value.length > 0) {
    updateElementHtmlById("informationSourceText", "*");
  }
  else {
    updateElementHtmlById("informationSourceText", "");
  }
}


function changeToKnownProjectCost() {
  // the state of the project cost radio button has transitioned
  // from either Unknown at this time to either Actual or Estimated 
  document.getElementById("displayProjectCost").style.display = "";
  enableTextField(document.sstoryForm.ff_project_cost);
  if (p_project_cost_old_value == "2") {
    document.sstoryForm.ff_project_cost.value = "";
  }
  p_project_cost_old_value = getRadioButtonValue(document.sstoryForm.p_project_cost);

  changeToProjectCost(); 
}

function changeToUnknownProjectCost() {
  // the state of the project cost radio button has transitioned
  // from either Actual or Estimated to Unknown at this time
  p_project_cost_old_value = getRadioButtonValue(document.sstoryForm.p_project_cost);
  disableTextField(document.sstoryForm.ff_project_cost);
  document.getElementById("displayProjectCost").style.display = "none";

  changeToProjectCost(); 
}

function changeToProjectCost() {
  // the contents of the project cost field have changed,
  // check if nonFEMACost field should be enabled
  if (document.sstoryForm.ff_project_cost.value.length > 0) {
    document.getElementById("displayNonFEMACost").style.display = "";
    enableTextField(document.sstoryForm.nonFEMACost);
    //alert("nonFEMACost enabled");
  }
  else {
    document.getElementById("displayNonFEMACost").style.display = "none";
    disableTextField(document.sstoryForm.nonFEMACost);
    //alert("nonFEMACost disabled");
  }
}


function ongoingClicked() {
  if (document.sstoryForm.p_project_ongoing.checked == true) {
    disableField(document.sstoryForm.ff_project_end_mo);
    document.sstoryForm.ff_project_end_mo.selectedIndex = -1;
    disableTextField(document.sstoryForm.ff_project_end_yr);
  }
  else {
    enableField(document.sstoryForm.ff_project_end_mo);
    enableTextField(document.sstoryForm.ff_project_end_yr);
  }
} 


function ecoAnalysisChanged() {
  if (document.sstoryForm.ff_economic_analysis_flag.checked == true) {
    document.sstoryForm.benefit_costs_ratio.value = '';
    disableTextField(document.sstoryForm.benefit_costs_ratio);
    document.sstoryForm.estimated_expected_net_benefits.value = '';
    disableTextField(document.sstoryForm.estimated_expected_net_benefits);
    document.sstoryForm.ff_losses_avoided_amount.value = '';
    disableTextField(document.sstoryForm.ff_losses_avoided_amount);
    if (document.getElementById("displayEcoAnalysis"))
      document.getElementById("displayEcoAnalysis").style.display = "none";
  }
  else {
    if (document.getElementById("displayEcoAnalysis") != null)
      document.getElementById("displayEcoAnalysis").style.display = "";
    enableTextField(document.sstoryForm.benefit_costs_ratio);
    enableTextField(document.sstoryForm.estimated_expected_net_benefits);
    enableTextField(document.sstoryForm.ff_losses_avoided_amount);
  }
  updateInformationSource();
}

// this works for onKeyDown and onKeyUp
function insertBullet(tarea) {
   var pattern = /\n/;
   // var cursorPos = getCursorPos(tarea);
   var tmp = tarea.value;
   //   alert("value: " + tarea.value[tarea.value.length - 1]);
   if (pattern.test(tarea.value.charAt(tarea.value.length - 1))) {
      tarea.value+=String.fromCharCode(8226);
      tarea.value+="  ";
   }
   else {
     pattern = /\r/;
     // var cursorPos = getCursorPos(tarea);
     if (pattern.test(tarea.value.charAt(tarea.value.length - 1))) {
      tarea.value+=String.fromCharCode(8226);
      tarea.value+="  ";
     }
   }
}


function handler(e, tarea) {
  if (document.all) {
    e = window.event;
  }
   
  var key;
  key = e.keyCode;
  if (key == 13) {
    insertAtCursor(tarea,String.fromCharCode(8226)+"  ");
  }
}

function insertAtCursor(myField, myValue) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart ==  '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
      + myValue
      + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
}

