// -----------------------------------------------------------------------------

function FormElementDefaultValue(
            FormElement) {
// Purpose :
//    Return default value for a FORM element.
// Input :
//    FormElement :  A SELECT Object.
// Output :
//    <Return value> :  default value for the FORM element.
//                      "" if none established
// Author :  Jerome Daoust, 1999/10/22.

   // Default output.
   c_value = ""

   if ( "Select_HeadWind" == FormElement.name )
      c_value = "20"
   if ( "Select_MinSink_Weight" == FormElement.name )
      c_value = "3"
   if ( "Select_StallSpeed_Margin" == FormElement.name )
      c_value = "7"
   if ( "Select_MaxGlideRatioW_Weight" == FormElement.name )
      c_value = "10"
   if ( "Select_SpeedMax_Weight" == FormElement.name )
      c_value = "4"
   if ( "Select_Sort_by_1" == FormElement.name )
      c_value = "Grade"
   if ( "Select_Sort_by_2" == FormElement.name )
      c_value = "Rating"
   if ( "Select_Units" == FormElement.name )
      c_value = "SI"
   if ( "Select_TFW_Range_Percent" == FormElement.name )
      c_value = "50"
   if ( "Select_Temperature" == FormElement.name )
      c_value = "15"
   if ( "Select_Altitude" == FormElement.name )
      c_value = "1000"
   if ( "Select_Pressure_MSL" == FormElement.name )
      c_value = "101.325"
   if ( "Select_Filter_Rating_Operator" == FormElement.name )
      c_value = "-1"
   if ( "Select_Filter_Rating_Level" == FormElement.name )
      c_value = "2"
   if ( "Filter_Model_Name" == FormElement.name )
      c_value = ""
   if ( "Select_Filter_Source_ParapenteMag" == FormElement.name )
      c_value = "0"
   if ( "Select_Filter_Source_VolLibre" == FormElement.name )
      c_value = "1"
   if ( "Select_Filter_Source_ExtraWing" == FormElement.name )
      c_value = "0"
   if ( "Select_Filter_Source_Manufacturer" == FormElement.name )
      c_value = "0"
   if ( "Select_Filter_Source_Pilot" == FormElement.name )
      c_value = "0"
   if ( "Select_Filter_Test_Age" == FormElement.name )
      c_value = "-1"
   if ( "Select_PolarCurve_NaturalCubicSpline" == FormElement.name )
      c_value = "1"

   if ( "ExtraWingData_Rating_DHV" == FormElement.name )
      c_value = "-1"
   if ( "ExtraWingData_Polar" == FormElement.name )
      c_value = ""
   if ( "ExtraWingData_Loading" == FormElement.name )
      c_value = "-1"
   if ( "ExtraWingData_TotalFlyingWeight_Min" == FormElement.name )
      c_value = "-1"
   if ( "ExtraWingData_TotalFlyingWeight_Max" == FormElement.name )
      c_value = "-1"
   if ( "ExtraWingData_TotalFlyingWeight_Test" == FormElement.name )
      c_value = "-1"
   if ( "ExtraWingData_Test_Temperature" == FormElement.name )
      c_value = "-1000"
   if ( "ExtraWingData_Test_Altitude" == FormElement.name )
      c_value = "-1000"
   if ( "ExtraWingData_Test_Pressure_MSL" == FormElement.name )
      c_value = "-1"


   return c_value
}
// -----------------------------------------------------------------------------

function SetFormValues(
            TheForm,
            InfoSource,
            IfFail_BeQuiet) {
// Purpose :
//    Set the FORM element values.
// Input :
//    TheForm :  Form which we wish to submit.
//    InfoSource :
//       0 :  Reset to defaults.
//       1 :  Use values stored in cookies if possible.
//    IfFail_BeQuiet :
//       0 :  False.  Issue messages if we fail to set a Form Element.
//       1 :  True.  Don't mention it, if we fail to set a Form Element.
// Output :
//    <Return value> :  Form select widgets are updated.
// Note :
//    1) FORM element settings are grouped into a single cookie to defeat
//       the max cookie limit of 20 per URL.
// Author :  Jerome Daoust, 1999/10/25.

   var c_cookie_name = ""
   var c_cookie_value = ""
   var c_mark_char = "~"

   // Create an array of the FORM's element names.
   for (i_element=0 ; i_element<TheForm.elements.length ; i_element++ ) {
      // Establish FORM element object.
      FormElement = TheForm.elements[i_element]
      if ( ( "select-one" == FormElement.type ) ||
           ( "text"       == FormElement.type ) ||
           ( "checkbox"   == FormElement.type ) ) {
         c_cookie_name += c_mark_char + FormElement.name
      }
   }
   c_cookie_name += c_mark_char

   // Get the cookie value
   c_cookie_value = GetCookie( c_cookie_name)
   ac_name = c_cookie_name.split( c_mark_char)
   ac_value = c_cookie_value.split( c_mark_char)

   // Loop on each FORM element.
   for (i_element=0 ; i_element<TheForm.elements.length ; i_element++ ) {
      // Establish FORM element object.
      FormElement = TheForm.elements[i_element]

      // Get the previously stored value for the SELECT object.
      c_value = ""
      if ( 1 == InfoSource ) {
         // Get value from cookie if possible.
         i_found = -1
         for ( i_name=0 ; i_name<ac_name.length ; i_name++ ) {
            if ( ac_name[i_name] == FormElement.name ) {
               i_found = i_name
            }
         }
         if ( ac_value.length-1 < i_found ) i_found = -1
         if ( 0 <= i_found ) {
            c_value = ac_value[i_found]
         }
      }
      // Get default value if not established yet. 
      if ( "" == c_value ) {
         c_value = FormElementDefaultValue(FormElement)
      }

      // Set the SELECT object selected option.
      if ( "select-one" == FormElement.type ) {
         // See if we have a valid value.
         if ( "" == c_value ) {
            if ( 0 == IfFail_BeQuiet ) {
               alert("Unable to establish c_value for FORM element '"+FormElement.name+"'")
            }
         }

         // Try to set the SELECT object.
         option_found = false
         for ( i_option = 0 ; i_option < FormElement.length ; i_option++ ) {
            if ( c_value == FormElement.options[i_option].value ) {
               FormElement.options[i_option].selected = true
               option_found = true
            }
         }

         // Verify that the SELECT object value was set.
         if ( ! option_found ) {
            if ( 0 == IfFail_BeQuiet ) {
               alert(  "Unable to establish option value matching '"
                     + c_value
                     + "', for SELECT object '"
                     + FormElement.name
                     + "'")
            }
         }
      }

      // Set a button form element.
      if ( "button" == FormElement.type ) {
         // Do nothing
      }

      // Set a checkbox form element.
      if ( "checkbox" == FormElement.type ) {
         // See if we have a valid value.
         if ( "" == c_value ) {
            if ( 0 == IfFail_BeQuiet ) {
               alert("Unable to establish c_value for FORM element '"+FormElement.name+"'")
            }
         }

         // Set the checkbox.
         FormElement.checked = 0
         if ( "1" == c_value ) {
            FormElement.checked = 1
         }
      }

      // Set a text form element.
      if ( "text" == FormElement.type ) {
         FormElement.value = c_value
      }
   }
}
// -----------------------------------------------------------------------------

function SaveFormValues(
            TheForm) {
// Purpose :
//    Save the FORM element values as cookies.
// Input :
//    TheForm :  Form which we wish to submit.
// Output :
//    <Return value> :  FORM elements are saved as cookies.
// Note :
//    1) FORM element settings are grouped into a single cookie to defeat
//       the max cookie limit of 20 per URL.
// Author :  Jerome Daoust, 1999/10/25.

   // Define how many days until cookie expiration.
   var days_to_expiration = 180

   var c_cookie_name = ""
   var c_cookie_value = ""
   var c_mark_char = "~"

   // Loop on each FORM element.
   for (i_element=0 ; i_element<TheForm.elements.length ; i_element++ ) {
      // Establish FORM element object.
      FormElement = TheForm.elements[i_element]

      // Deal with SELECT form elements.
      if ( "select-one" == FormElement.type ) {
         // Save the information.
         c_cookie_name += c_mark_char + FormElement.name
         c_cookie_value += c_mark_char + FormElement.options[FormElement.selectedIndex].value
      }

      // Deal with button form elements.
      if ( "button" == FormElement.type ) {
         // Do nothing
      }

      // Deal with checkbox form element.
      if ( "checkbox" == FormElement.type ) {
         c_cookie_name += c_mark_char + FormElement.name
         if ( FormElement.checked ) {
            c_cookie_value += c_mark_char + "1"
         } else {
            c_cookie_value += c_mark_char + "0"
         }
      }

      // Set a text form element.
      if ( "text" == FormElement.type ) {
         // Save the information.
         c_cookie_name += c_mark_char + FormElement.name
         c_cookie_value += c_mark_char + FormElement.value
      }
   }

   // Mark the end of the strings.
   c_cookie_name += c_mark_char
   c_cookie_value += c_mark_char

   // Save all the FORM data as a single cookie.
   SetCookie_DaysValid(
      c_cookie_name,
      c_cookie_value,
      days_to_expiration)
}
// -----------------------------------------------------------------------------

function FormElement_SelectOne_Populate(
            FormElement,
            DataType,
            Value_Top,
            Value_Bottom,
            Value_Unknown,
            Value_Selected) {
// Purpose :
//    Populate the options for a "select-one" FORM element for Paraglider speeds.
// Input :
//    FormElement :  FORM element.
//    DataType :
//       "Speed" :
//       "SinkRate" :
//       "L/D" :
//       "Loading" :
//       "TFW" :
//       "Temperature" :
//       "Altitude" :
//       "Pressure" :
//    Value_Top :  Value to appear at the top of the selection.
//    Value_Bottom :  Value to appear at the bottom of the selection.
//    Value_Unknown :  Values to use as "Unknown"
//    Value_Selected :  Value which will be selected by default.
// Output :
//    <Return value> :  FORM "select-one" element has more options.
// Note :
//    Netscape only :
//       You will need to refresh the form so the new options appear :
//          history.go(0)
// Author :  Jerome Daoust, 1999/10/22.

   // Set function name
   var c_fn = "FormElement_SelectOne_Populate"

   // Variables.
   var c_Value_Metric = ""
   var c_Value_US = ""
   var c_option_value = ""
   var c_option_text = ""
   var Value_Metric = 0.0
   var Value_US = 0.0
   var DataType_valid = 0

   // Verify that this is a FORM "select-one" element.
   if ( "select-one" != FormElement.type ) {
      alert(  "Form element '" + FormElement.name
            + "' is not a 'select-one' type in "
            + c_fn + "().")
      return 0
   }

   // Establish value increment.
   DataType_valid = 0
   if ( "Speed" == DataType ) {
      DataType_valid = 1
      Value_Inc = sign( Value_Bottom-Value_Top) * 1.0
   }
   if ( "SinkRate" == DataType ) {
      DataType_valid = 1
      Value_Inc = sign( Value_Bottom-Value_Top) * 0.02
   }
   if ( "L/D" == DataType ) {
      DataType_valid = 1
      Value_Inc = sign( Value_Bottom-Value_Top) * 0.1
   }
   if ( "Loading" == DataType ) {
      DataType_valid = 1
      Value_Inc = sign( Value_Bottom-Value_Top) * 0.01
   }
   if ( "TFW" == DataType ) {
      DataType_valid = 1
      Value_Inc = sign( Value_Bottom-Value_Top) * 1.0
   }
   if ( "Temperature" == DataType ) {
      DataType_valid = 1
      Value_Inc = sign( Value_Bottom-Value_Top) * 1.0
   }
   if ( "Altitude" == DataType ) {
      DataType_valid = 1
      Value_Inc = sign( Value_Bottom-Value_Top) * 100.0
   }
   if ( "Pressure" == DataType ) {
      DataType_valid = 1
      Value_Inc = sign( Value_Bottom-Value_Top) * 0.1
   }
   if ( 0 == Value_Inc ) {
      alert(  "( 0 == Value_Inc ) in"
            + c_fn + "().")
      return 0
   }

   // Set first option.
   i_option = 0 ;
   c_option_value = ""+Value_Unknown
   c_option_text = "Unknown"
   FormElement.options[i_option] = new Option( c_option_text, c_option_value)
   if ( Value_Selected == FormElement.options[i_option].value ) {
      FormElement.options[i_option].selected = true
   }
   i_option += 1

   // Loop on options (Top to Bottom).
   for ( Value_Metric = Value_Top ;
         (Value_Metric-Value_Top) / (Value_Bottom-Value_Top) <= 1.0 ;
         Value_Metric = Value_Metric+Value_Inc ) {
      // Establish Metric string value.

      DataType_valid = 0
      if ( "Speed" == DataType ) {
         Value_Metric = Math.round(1*Value_Metric)/1
         c_Value_Metric = "" + Value_Metric
         DataType_valid = 1
         c_Unit_Metric = "km/h"
         c_Unit_US = "mph"
         // Conversion :  1 km/h = 0.62139 mph
         Value_US = Value_Metric * 0.62139
         c_Value_US = "" + Math.round(10*Value_US)/10
      }
      if ( "SinkRate" == DataType ) {
         Value_Metric = Math.round(100*Value_Metric)/100
         c_Value_Metric = "" + Value_Metric
         DataType_valid = 1
         c_Unit_Metric = "m/s"
         c_Unit_US = "fpm"
         // Conversion :  1 m/s = 196.85 fpm
         Value_US = Value_Metric * 196.85
         c_Value_US = "" + Math.round(1*Value_US)/1
      }
      if ( "L/D" == DataType ) {
         Value_Metric = Math.round(10*Value_Metric)/10
         c_Value_Metric = "" + Value_Metric
         DataType_valid = 1
         c_Unit_Metric = ""
         c_Unit_US = ""
         // Conversion :  1 = 1
         Value_US = Value_Metric
         c_Value_US = c_Value_Metric
      }
      if ( "Loading" == DataType ) {
         Value_Metric = Math.round(100*Value_Metric)/100
         c_Value_Metric = "" + Value_Metric
         DataType_valid = 1
         c_Unit_Metric = "kg/m2"
         c_Unit_US = "lb/ft2"
         // Conversion :  1 kg/m2 = 0.2048 lb/ft2
         Value_US = Value_Metric * 0.2048
         c_Value_US = "" + Math.round(100*Value_US)/100
      }
      if ( "TFW" == DataType ) {
         Value_Metric = Math.round(1*Value_Metric)/1
         c_Value_Metric = "" + Value_Metric
         DataType_valid = 1
         c_Unit_Metric = "kg"
         c_Unit_US = "lb"
         // Conversion :  1 kg = 2.2046 lb
         Value_US = Value_Metric * 2.2046
         c_Value_US = "" + Math.round(1*Value_US)/1
      }
      if ( "Temperature" == DataType ) {
         Value_Metric = Math.round(1*Value_Metric)/1
         c_Value_Metric = "" + Value_Metric
         DataType_valid = 1
         c_Unit_Metric = "C"
         c_Unit_US = "F"
         // Conversion :  Deg_F = 1.802 * Deg_C + 32
         Value_US = 1.802 * Value_Metric + 32
         c_Value_US = "" + Math.round(1*Value_US)/1
      }
      if ( "Altitude" == DataType ) {
         Value_Metric = Math.round(1*Value_Metric)/1
         c_Value_Metric = "" + Value_Metric
         DataType_valid = 1
         c_Unit_Metric = "m"
         c_Unit_US = "ft"
         // Conversion :  1 m = 3.281 ft
         Value_US = Value_Metric * 3.281
         c_Value_US = "" + Math.round(1*Value_US)/1
      }
      if ( "Pressure" == DataType ) {
         Value_Metric = Math.round(10*Value_Metric)/10
         c_Value_Metric = "" + Value_Metric
         DataType_valid = 1
         c_Unit_Metric = "kPa"
         c_Unit_US = "in Hg"
         // Conversion :  1 kPa = 0.2953 in Hg
         Value_US = Value_Metric * 0.2953
         c_Value_US = "" + Math.round(100*Value_US)/100
     }

      // Verify that a valid DataType was found.
      if ( ! DataType_valid ) {
         alert(  "Invalid DataType '" + DataType
               + "' in "
               + c_fn + "().")
         return 0
      }

      // Create the new option.
      c_option_value = c_Value_Metric
      if ( "" == c_Unit_Metric ) {
         c_option_text = c_Value_Metric
      } else {
         c_option_text = c_Value_Metric+" "+c_Unit_Metric+" = "+c_Value_US+" "+c_Unit_US
      }
      FormElement.options[i_option] = new Option( c_option_text, c_option_value)

      // Determine if this option should be the one selected.
      if ( Value_Selected == FormElement.options[i_option].value ) {
         FormElement.options[i_option].selected = true
      }
      i_option += 1
   }

   // Function must return a value (Netscape)
   return 0
}
// -----------------------------------------------------------------------------
