//This function preloads rollover images with a,i and o suffixes for active, inactive and on states.
//The function can preload any number of images.
//Usage: uPreload('list of images separated by commas','path where the images live','OPTIONAL: pass in any variable to load active and on states','OPTIONAL: pass in a different file extension other than gif');

function uPreload(imgList, uPath, iSufx, iExt)
{
   if(iExt != null)
   {
      var imgExt = iExt;
   }
   else 
   {
      var imgExt = 'gif';
   }
   if(iSufx != null)
   {
      var imgSuffix = new Array('i', 'a', 'o');
   }
   else 
   {
      var imgSuffix = new Array('i', 'a');
   }
   if(document.images)
   {
      imgArgArray = imgList.split(',');
      for(var z = 0; z < imgArgArray.length; z++)
      {
         var imgArg = imgArgArray[z];
         var imgSufLen = imgSuffix.length;
         for(j = 0; j < imgSufLen; j++)
         {
            var imgSuf = imgSuffix[j];
            imgNew = imgArg + imgSuf + ' = new Image();';
            eval(imgNew);
            imgSrc = imgArg + imgSuf + '.src = \'' + uPath + '/' + imgArg + imgSuf + '.' + imgExt + '\';';
            eval(imgSrc);
         }
      }
   }
}

function plImages()
{
    var linkArray = document.getElementsByTagName("img");
    for (var loop=0; loop<linkArray.length; loop++)
    {
        if (linkArray[loop].className == "pl")
        {
			var imgPath = linkArray[loop].src.substring( 0, linkArray[loop].src.lastIndexOf( '/' ) + 1 );
			uPreload( linkArray[loop].id, imgPath, linkArray[loop].src.substring( linkArray[loop].src.lastIndexOf( '.' ) ) );
        }
    }
}

//This function preloads the global navigation elements
navRoll = false;
//Set this variable to false so no global rollovers will work until all preloading is done.

function plGnav(lnList, lnPath)
{
  plImages();
  uPreload('gFutureSoldier,gNavHome,gNavOrientation,gReferral,gNowWhat','/images/nav/');
  //uPreload('', 'frontGraphics_07', '', null, 'jpg');
  MM_preloadImages('/images/section/familyON.jpg',
                   '/images/section/soldierON.jpg');
  MM_preloadImages('/images/nav/rightNav/rightNavON_03.gif',
                   '/images/nav/rightNav/rightNavON_05.gif',
                   '/images/nav/rightNav/rightNavON_09.gif',
                   '/images/nav/rightNav/rightNavON_11.gif',
                   '/images/nav/rightNav/rightNavON_13.gif',
                   '/images/spf/spfAlphabetON.gif');
  if(lnList)
   {
      uPreload(lnList, lnPath);
   }
   navRoll = true;
}

//this function adds leading zeros to digits if they are less than 10

function addLz(dGt)
{
   if(dGt < 10)
   {
      dGt = '0' + dGt;
   }
   else 
   {
      dGt = dGt;
   }
   return dGt;
}
//This function is for navigation rollover images and window status; uName is name of graphic, uState is on, off or over state, uWstat is window status message

function nr(uName, uState, uWstat)
{
   if(navRoll)
   {
      document.images[uName].src = self[uName + uState].src;
      ws(uWstat);
   }
}
//This is used to display window status in browsers. It should be used for all links. The nr function makes use of it as well.

function ws(uWstat)
{
   uWstat = uWstat.replace('|', "'");
   //Use the vertical bar (|) character if you need to display an apostrophe in the window status area
   window.status = uWstat;
}
//This is used to popup a new centered window with no chrome. The last variable can be passed in to make the window scrollable.

function wPop(url, winName, wWidth, wHeight, scrll)
{
   var scrollB;
   if(!scrll)
   {
      scrollB = 'no';
      var pWidth = wWidth;
   }
   else 
   {
      scrollB = scrll;
      wWidth = parseInt(wWidth) + 20;
   }
   var iMyWidth;
   var iMyHeight;
   wHeight = wHeight + 12;
   //Added twelve pixels to ALL popup heights to compensate for newer/larger popup header logo. 
   iMyWidth =(window.screen.width / 2) - (wWidth / 2 + 10);
   //half the screen width minus half the new window width (plus 5 pixel borders).
   iMyHeight =(window.screen.height /2) - (wHeight / 2 + 15);
   //half the screen height minus half the new window height (plus title and status bars).
   var zWin = window.open(url, winName, "status=no,width=" + wWidth + ",height=" + wHeight + ",resizable=no,left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight + ",scrollbars=" + scrollB);
   zWin.focus();
}
//This is used to popup a new centered window with the Toolbar and Menubar. The last variable can be passed in to make the window scrollable.

function wPopTools(url, winName, wWidth, wHeight, scrll)
{
   var scrollB;
   if(!scrll)
   {
      scrollB = 'no';
      var pWidth = wWidth;
   }
   else 
   {
      scrollB = scrll;
      wWidth = parseInt(wWidth) + 20;
   }
   var iMyWidth;
   var iMyHeight;
   iMyWidth =(window.screen.width / 2) - (wWidth / 2 + 10);
   //half the screen width minus half the new window width (plus 5 pixel borders).
   iMyHeight =(window.screen.height /2) - (wHeight/ 2 + 15);
   //half the screen height minus half the new window height (plus title and status bars).
   var zWin = window.open(url, winName, "status=no,toolbar=yes,menubar=yes,width=" + wWidth + ",height=" + wHeight + ",resizable=no,left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight + ",scrollbars=" + scrollB);
   zWin.focus();
}

// Detects if a parent window is open and loads URL into it.  Otherwise, pops a new window for URL.
function toParent(zUrl)
{
   // First check to see if there's a parent window, or a parent.parent window
   var pWin = 0;
   // Parent window is assumed closed
   if(window.opener != null && !window.opener.closed)
   {
      // One parent
      pWin = 1;
   }
   if(window.opener.opener != null && !window.opener.opener.closed)
   {
      // Two parents
      pWin = 2;
   }
   if(pWin == 0)
   {
      // Open a blank window
      var blankWin = window.open(zUrl, blankWin, "width=775,height=600,resizable=yes,left=10,top=10,screenX=10,screenY=10,scrollbars=yes,toolbar=yes,titlebar=yes,status=yes,menubar=yes,personalbar=yes,location=yes,directories=yes");
   }
   if(pWin == 1)
   {
      // Load into parent window
      opener.location.href = zUrl;
      opener.focus();
   }
   if(pWin == 2)
   {
      // Load into parent parent window
      opener.opener.location.href = zUrl;
      opener.opener.focus();
   }
}

//This function will be used to direct to contact pages from popup windows. Path/Strategy to be determined later.
function sendinfo()
{
   parent.window.opener.location = "https://www.goarmy.com/contact/send_for_info.jsp";
   parent.window.opener.focus();
   parent.window.close();
   //alert('We will send to a page in the future once we determine a path.');
   window.close();
}
//This function is for submitting a form from a graphic or text link.

function submitSearch(formName)
{
   document.forms[formName].submit();
}
// This function is for creating arrays of elements to be used with the elSwap function.
// The first argument is the name of the array you wish to create.
// The variable number of following arguments are the list of element ID's you wish have stored in the array.
// The created array will have it's name as the first argument and all additional elements follow in sequence
// as passed in to the buildElArray() function. -M.Y. 2/26/04

function buildElArray()
{
   argv = buildElArray.arguments;
   argc = argv.length;
   if(argc > 1)
   {
      eval(argv[0] + '= argv');
   }
   else 
   {
      alert('no element IDs were passed to the buildElArray function');
   }
}
// This function will show whatever element's ID you pass into it and hide all the others in the array.
// The first argument is the name of the array to use.
// The second argument is the ID of the element which you want to show.
// This function needs to be used with arrays created with the buildArray(); function. -M.Y. 2/26/04

function elSwap(whichArray, showID)
{
   eval('elArray=' + whichArray);
   //loop starts at element 1 of the array to skip the name of the array which is stored in 0
   for(i = 1; i < elArray.length; i++)
   {
      var curID = elArray[i];
      if(showID != curID)
      {
         elmt = document.getElementById(curID);
         elmt.style.display = 'none';
      }
      else 
      {
         elmt = document.getElementById(curID);
         elmt.style.display = 'block';
      }
   }
}

function forParents(whichArray, showID)
{
   var ansID = 'detail' + showID;
   var quesID = 'ques' + showID;
   elSwap(whichArray, ansID);
   eval('elArray=allQues');
   if(showID != ' ')
   {
      for(i = 1; i < elArray.length; i++)
      {
         var curID = elArray[i];
         elmt = document.getElementById(curID);
         elmt.style.display = 'block';
         quesDisp = document.getElementById(quesID);
         quesDisp.style.display = 'none';
      }
   }
   else 
   {
      for(i = 1; i < elArray.length; i++)
      {
         var curID = elArray[i];
         elmt = document.getElementById(curID);
         elmt.style.display = 'block';
      }
   }
}

function MM_findObj(n, d)
{
   //v4.01
   var p, i, x;
   if(!d) d = document;
   if((p = n.indexOf("?")) > 0 && parent.frames.length)
   {
      d = parent.frames[n.substring(p + 1)].document;
      n = n.substring(0, p);
   }
   if(!(x = d[n]) && d.all) x = d.all[n];
   for(i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
   for(i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
   if(!x && d.getElementById) x = d.getElementById(n);
   return x;
}

function MM_setTextOfLayer(objName, x, newText)
{
   //v4.01
   if((obj = MM_findObj(objName)) != null)
   with(obj)
   if(document.layers)
   {
      document.write(unescape(newText));
      document.close();
   }
   else innerHTML = unescape(newText);
}
// ***------------- Media Player functions start here. --------------*** //

function setTextVersion(textOnorOff)
{
   cookiesOfforOn();
   if(cookiesOn)
   {
      SetCookie('text_version', textOnorOff, null, '/');
   }
   else 
   {
      var text_version = false;
   }
}

function setBandwidth(bndWdth)
{
   cookiesOfforOn();
   if(cookiesOn)
   {
      parseInt(bndWdth);
      if(bndWdth != 0 || null || 'null')
      {
         SetCookie('flashBandWidth', bndWdth, null, '/');
      }
   }
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// This function is intended for use by forms that do not contain a submit button, but 
// rather use javascript to perform the submit.
// The form registers an onkeyup event for all input elements of the form which allows
// the form to be submitted by pressing the Enter key.
function regFormForSubmitOnReturn(formId){
    function KeyCheck(e){
        var KeyID = (window.event) ? event.keyCode : e.keyCode;
        //If the Enter key was pressed, submit the element's parent form
        if(KeyID == 13){
            var parent = this.parentNode;
            while (parent.tagName != 'FORM' && parent.tagName != 'BODY'){
                parent = parent.parentNode;
            }
            if(parent.tagName == 'FORM'){
                parent.submit();
            }
        }
    }
    var formFields = document.getElementById(formId).elements;
    for (i=0; i<formFields.length; i++){
        if(formFields[i].tagName == 'INPUT'){
            formFields[i].onkeyup = KeyCheck;
        }
    }    
}