// COPYRIGHT 2002 MetService New Zealand Limited

var VersionNumber = parseInt(navigator.appVersion); // Browser version
var BrowserNetscape = (navigator.appName == "Netscape");

//************************************************************************
//
// Add or remove a string from a string field
//
// Arguments
//   newString         - input value to be added/removed
//   valueStringObject - field holding the current list of values
//
// Returns nothing
//
function AddOrRemove(valueStringObject, newValue, aComment)
{
  if (VersionNumber < 4)
  {
    alert("Your version of browser is too old to use some of the features of this form - they have been disabled");
  }
  else
  {
    // Create an array to hold the current list of values
    var currentValues = new Array();

    // Split the current string if it is not empty
    // Splitting an empty string returns an element depending on browser (bad)
    if (valueStringObject.value != "")
    {
      // Split on the determined separator
      valueStringObject.value = valueStringObject.value.toUpperCase();
      currentValues = valueStringObject.value.split(" ");
    }

    var dupIndex = _findElement(currentValues, newValue);

    // Add new element if dupIndex == -1 (i.e. new value already present)
    if (dupIndex == -1)
    {
      // Note IE does not like push()
      //currentValues.push(newValue);
      currentValues = currentValues.concat(newValue);
      if (aComment.length > 0) {
        alert(aComment);
      }
    }
    else
    {
      // Remove the duplicate element
      currentValues = _removeElement(currentValues, dupIndex);
    }

    // Reform into string and place into field
    valueStringObject.value = currentValues.join(" ");
  }
}

//************************************************************************
//
// Puts/Overwrites a string into a string field
//
// Arguments
//   newString         - input value to be replaced
//   valueStringObject - field for the value
//
// Returns nothing
//
function PutList(valueStringObject, newValue)
{
  if (VersionNumber < 4)
  {
    alert("Your version of browser is too old to use some of the features of this form - they have been disabled");
  }
  else
  {
    if (valueStringObject.value == newValue)
    {
        valueStringObject.value = "";
    }
    else
    {
        // Put string into field
        valueStringObject.value = newValue.concat();
    }
  }
}


//************************************************************************
//
// Find the first element in an array to match a value
//
// Arguments
//   inputArray - array to search for value
//   checkValue - value to look for in array
//
// Returns index of first matching element if one exists, -1 otherwise
//
function _findElement(inputArray, checkValue)
{
  var elementIndex = -1;

  for (i = 0; elementIndex == -1 && i < inputArray.length; i++)
  {
    if (inputArray[i] == checkValue)
    {
      elementIndex = i;
    }
  }

  return elementIndex;
}


//************************************************************************
//
// Remove specified element from an array, returning the updated array
// Note cannot use splice() as this is only supported in IE5.5 and upward
//
// Arguments :
//   inputArray - the unprocessed array
//   index      - the index of the element to be removed
//
// Returns an updated array with the element removed
//
function _removeElement(inputArray, index)
{
  var start;  // Holds items from start of array to index-1
  var end;    // Holds items from index+1 to end of array

  if (index > 0)
  {
    start = inputArray.slice(0, index);
  }
  else
  {
    start = new Array(0);
  }

  if (index < inputArray.length - 1)
  {
    end = inputArray.slice(index + 1);
  }
  else
  {
    end = new Array(0);
  }

  // Concatenate the two parts
  return start.concat(end);
}


//************************************************************************
//
// Checks string field for not null - if null display message
//
// Arguments
//   msgString         - string when object is empty
//   valueStringObject - field holding the values
//
// Returns true or false
//
function CheckNotNull(valueStringObject)    //, msgString)
{
  if (valueStringObject.value == "") {
//      alert(msgString);
    return false;
  }
  else {
    return true;
  }

}


//************************************************************************

//
// Start up a graphics viewer
//
// Arguments
//   ViewerURL      - the target viewer
//   ViewerType     - the viewer type for the images to display
//   ViewerOptions  - the viewer window options
//
// Returns nothing
//
function GraphicsViewer(ViewerURL,ViewerType,ViewerOptions) {
  var ViewerTitleBar = "GAWx_" + ViewerType + "_Viewer";
  window.open(ViewerURL,ViewerTitleBar,ViewerOptions);
}

//************************************************************************
//
// Start up a text viewer processor
//
// Arguments
//   ViewerURL      - the target viewer
//   ViewerOptions  - the viewer window options
//
// Returns nothing
//
function TextViewer(ViewerURL,ViewerOptions,COM) {
  window.open(ViewerURL,"GAWx_Text_VIEWER"+COM,ViewerOptions);
}

//************************************************************************
//
// Start up a help viewer
//
// Arguments
//   ViewerURL      - the target viewer
//   ViewerTitle    - the viewer type for the images to display
//   ViewerOptions  - the viewer window options
//
// Returns nothing
//
function HelpViewer(ViewerURL,ViewerType,ViewerOptions) {
  var ViewerName = "GAWx_" + ViewerType + "_Help";
  window.open(ViewerURL,ViewerName,ViewerOptions);
}


//************************************************************************
function MetFlightMenu(MenuURL,MenuType,MenuOptions) {
  var MenuTitleBar = "MetFlight_" + MenuType + "_Menu";
  window.open(MenuURL,MenuTitleBar,MenuOptions);
}


//************************************************************************
//
// Determines an Ok To Go status - and load a URL form
//
// Arguments
//   ActionForm     - the source from for the action
//
// Returns logical (true/false) [false]
//
function OkToGo(ActionForm,UserName,COM) {
  if (ActionForm == "sitNZ") {
    var Args = "MetFlightBrief.php";
    Args += "?SITNZ=NZ&User="+UserName + "&UserName="+UserName;
    Args += "&session="+document.MetFlightControlForm.session.value;
    TextViewer(Args,"WIDTH=545,HEIGHT=600,resizable,scrollbars,status",COM);
    return false;
  }
  if (ActionForm == "sitNZShort") {
    var Args = "MetFlightBrief.php";
    Args += "?SITNZ=Short&User="+UserName + "&UserName="+UserName;
    Args += "&session="+document.MetFlightControlForm.session.value;
    TextViewer(Args,"WIDTH=545,HEIGHT=600,resizable,scrollbars,status",COM);
    return false;
  }
  if (ActionForm == "gawxNI") {
    var Args = "MetFlightBrief.php";
    Args += "?GAWXNI=" + document.MetFlightControlForm.GAWXNI.value + "&User="+UserName + "&UserName="+UserName;
    Args += "&METARhours=" + document.MetFlightControlForm.METARhours.value;
    Args += "&session="+document.MetFlightControlForm.session.value;
    TextViewer(Args,"WIDTH=545,HEIGHT=600,resizable,scrollbars,status",COM);
    return false;
  }
  if (ActionForm == "gawxSI") {
    var Args = "MetFlightBrief.php";
    Args += "?GAWXSI=" + document.MetFlightControlForm.GAWXSI.value + "&User="+UserName + "&UserName="+UserName;
    Args += "&METARhours=" + document.MetFlightControlForm.METARhours.value;
    Args += "&session="+document.MetFlightControlForm.session.value;
    TextViewer(Args,"WIDTH=545,HEIGHT=600,resizable,scrollbars,status",COM);
    return false;
  }
  if (ActionForm == "gawxBrief") {
    if ((document.MetFlightControlForm.Areas.value != "") || (document.MetFlightControlForm.ExtraTAF.value != "")) {
      var Args = "MetFlightBrief.php";
      Args += "?BRIEFING=" + document.MetFlightControlForm.BRIEFING.value;
      if (document.MetFlightControlForm.ARFOR.checked) { Args += "&ARFOR=" + document.MetFlightControlForm.ARFOR.value; } else { Args += "&ARFOR="; }
      if (document.MetFlightControlForm.TAF.checked) { Args += "&TAF=" + document.MetFlightControlForm.TAF.value; } else { Args += "&TAF="; }
      if (document.MetFlightControlForm.METAR.checked) { Args += "&METAR=" + document.MetFlightControlForm.METAR.value; } else { Args += "&METAR="; }
      Args += "&METARhours=" + document.MetFlightControlForm.METARhours.value;
      if (document.MetFlightControlForm.GAWX.checked && (document.MetFlightControlForm.GAWX.value == "Yes")) { Args += "&GAWX=" + document.MetFlightControlForm.GAWX.value; } else { Args += "&GAWX="; }
      if (document.MetFlightControlForm.SIGMET.checked) { Args += "&SIGMET=" + document.MetFlightControlForm.SIGMET.value; } else { Args += "&SIGMET="; }
      Args += "&Areas=" + document.MetFlightControlForm.Areas.value;
      Args += "&ExtraTAF=" + document.MetFlightControlForm.ExtraTAF.value + "&User="+UserName + "&UserName="+UserName;
      Args += "&session="+document.MetFlightControlForm.session.value;
      TextViewer(Args,"WIDTH=545,HEIGHT=600,resizable,scrollbars,status",COM);
      return false;
    } else {
      alert("You need to supply at least one\nARFOR Area Name or Other TAF");
      return false;
    }
  }
  if (ActionForm == "NZSIGWX") {
    var Args = "MetFlightBrief.php";
    Args += "?NZSigWx=Yes";
    Args += "&session=" + document.MetFlightControlForm.session.value;
    Args += "&User=" + UserName + "&UserName=" + UserName;
    TextViewer(Args,"WIDTH=605,HEIGHT=600,left=0,top=0,resizable,scrollbars,status",COM);
    return (true);
  }
  if (ActionForm == "NZTX") {
    var Args = "MetFlightBrief.php";
    Args += "?NZTx=Yes";
    Args += "&session=" + document.MetFlightControlForm.session.value;
    Args += "&User=" + UserName + "&UserName=" + UserName;
    TextViewer(Args,"WIDTH=605,HEIGHT=600,left=0,top=0,resizable,scrollbars,status",COM);
    return (true);
  }
}


//************************************************************************
//

function MakePassword(TheValue,ResultPassword) {  // ***** create a random password of a set size *****
//  srand(time());
  var TheAlphaNumbs = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
  var ThePassword = "";
  var TheLetter = "";
  var NumbOfChar = 0 + TheValue;
  if (1 == 2) {       // ***** make password fixed *****
    ThePassword = "12345678";
  } else {
    for (var ii=0; ii < NumbOfChar; ii++) {
      TheLetter = TheAlphaNumbs.charAt(Math.round(Math.random()*(TheAlphaNumbs.length-1)))
      ThePassword += TheLetter;
    }
  }
  ResultPassword.value = ThePassword;
  return false;
}

//************************************************************************

function DoIt(ActionForm,UserName) {
  if (ActionForm == "MSLCharts") {
    var Args = "MetFlightBrief.php";
    Args += "?MSLCharts=Yes";
//      Args += "&ProgPeriod=" + document.MetFlightMenuForm.ProgPeriod.value;
    Args += "&PValidity=" + document.MetFlightMenuForm.ProgValidity.value;
    Args += "&AValidity=" + document.MetFlightMenuForm.AnalValidity.value;
    Args += "&User=" + UserName + "&UserName=" + UserName;
    Args += "&MapName=" + document.MetFlightMenuForm.MapName.value;
    Args += "&session=" + document.MetFlightMenuForm.session.value;
    GraphicsViewer(Args,"MSL_Chart","WIDTH=900,HEIGHT=750,left=10,top=20,resizable,scrollbars,status");
    return false;
  }
  if (ActionForm == "SIGWXCharts") {
    var Args = "MetFlightBrief.php";
    Args += "?MSLCharts=Yes";
    Args += "?SIGWXCharts=Yes";
//      Args += "&ProgPeriod=" + document.MetFlightMenuForm.ProgPeriod.value;
    Args += "&PValidity=" + document.MetFlightMenuForm.ProgValidity.value;
    Args += "&AValidity=" + document.MetFlightMenuForm.AnalValidity.value;
    Args += "&User=" + UserName + "&UserName=" + UserName;
    Args += "&MapName=" + document.MetFlightMenuForm.MapName.value;
    Args += "&session=" + document.MetFlightMenuForm.session.value;
    GraphicsViewer(Args,"SIGWX_Chart","WIDTH=900,HEIGHT=750,left=20,top=10,resizable,scrollbars,status");
    return false;
  }
}


//************************************************************************
function WindowMaxiMize() {
//    window.moveTo(0,0);
//    window.outerWidth = screen.availWidth;
//    window.outerHeight = screen.availHeight;
//    window.outerWidth = screen.width - 50;
//    window.outerHeight = screen.height - 50;
    if (document.all || document.layers) { self.moveTo(0,0); self.resizeTo(screen.availWidth,screen.availHeight); }
}


//************************************************************************
function GraphicsImage(ViewerURL,ViewerOptions) {
    var ViewerTitleBar = "Wx_Grafix_Viewer";
    window.open(ViewerURL,ViewerTitleBar,ViewerOptions);
}


//************************************************************************

