(function() {
  Neaux.setNamespace( 'Neaux.Xtra');

  var SELECT_CAREER_OPPORTUNITIES = 3;
  Neaux.merge( Neaux.Xtra, {
    // Given an ID of an element at the top of a UL-based menu, return an array of the menu's LI
    // elements.
    //
    getMenuNodes: function( aMenuID) { 
      var menu = $ID(aMenuID);
      // Get all of the anchors in the menu hierarchy, iterate through them until 
      // the href attribute matches the page name
      var anchors = menu.getElementsByTagName('a');
      for ( var i = 0; i < anchors.length; ++i)
        if ( window.location.href == anchors[i].href) break;

      // For a page not in the menu, return null
      if ( i == anchors.length) return null;

      var theNodes = [];
      // Now we go walk back up the tree and get the node hierarchy and save it for later (e.g., for breadcrumbs)
      var ptr = anchors[i];
      while( ptr && ptr.id != aMenuID) {
        if ( ptr.nodeName.toLowerCase() == 'li')
          theNodes.push(ptr);
        ptr = ptr.parentNode;
      }
      var home = menu.getElementsByTagName('li')[0];
      if ( home != theNodes[theNodes.length-1])
        theNodes.push(home);

      // Return 'em in top-down order
      return theNodes.reverse();
    },
    //
    // Given an array of list nodes, generate a document fragment to be used as breadcrumbs.
    //
    makeBreadcrumbs: function( aMenuNodes, aBreadcrumbID) { 
      if ( typeof aMenuNodes == 'string') aMenuNodes = Neaux.Xtra.getMenuNodes(aMenuNodes);
      if ( !aMenuNodes) return;
      var theClone, theNode, theFrag = document.createDocumentFragment();
      for ( var i in aMenuNodes) {
        theNode = aMenuNodes[i].getElementsByTagName('a');
        if ( i > 0) theFrag.appendChild(document.createTextNode(' > '));
        theClone = theNode[0].cloneNode(true);
        theClone.style.cssText = '';
        theFrag.appendChild(theClone);
      }
      var theBC = $ID(aBreadcrumbID);
      theBC.replaceChild( theFrag, theBC.firstChild);
    }
  });

  Trc = {
    shortForm: null,
    longForm: null,

    getMaxHeight: function() {
      var theElm, maxHeight = 0;
      for ( var i = 0; i < arguments.length; ++i) {
        theElm = $ID(arguments[i]);
        if ( theElm && theElm.offsetHeight > maxHeight) maxHeight = theElm.offsetHeight;
      }
      return maxHeight;
    },
    alignHeight: function() {
      var theHeight = Trc.getMaxHeight.apply( this, arguments);
      for ( var i = 0; i < arguments.length; ++i)
        $ID(arguments[i]).style.height = theHeight + 'px';
    },
    setLoginUrl: function() {
      var theAnchor = $ID('hdr_login_link');
      if( !theAnchor) return;
	    var thePath = document.location.pathname;
	    var theDir = thePath.substring(0,thePath.lastIndexOf('/')+1);
	    var theFileName = thePath.substring(thePath.lastIndexOf('/')+1, thePath.length)
      if ( theFileName != Trc.SITE_LOGIN_PAGE)
  	    theAnchor.href = 'https://' + document.location.host + theDir + Trc.SITE_LOGIN_PAGE;
  	  else
  	    $ID('hdr_login').style.visibility = 'hidden';
    },
    setBreadcrumbs: function( aBcContainer, aMenu) {
      if ( !$ID(aBcContainer)) return;
      // Clear Sample Breadcrumbs
      $ID(aBcContainer).innerHTML = '&nbsp;';
      Neaux.Xtra.makeBreadcrumbs( aMenu, aBcContainer);
    },

    initContactShortForm: function() {
      if ( $ID('frmContact')) Trc.shortForm = new Trc.ContactFormShort('frmContact', 'btnSubmit');
    },
    initContactLongForm: function() {
      if ( $ID('frmContactLong')) Trc.longForm = new Trc.ContactFormLong('frmContactLong', 'btnSubmitLong');
    },

    start: function() {
      Trc.alignHeight( 'content_left', 'content_right', 'content_container');
      Trc.setBreadcrumbs( 'breadcrumbs_bar', 'hdr_nav');
      Trc.initContactShortForm();
      Trc.initContactLongForm();
      //Trc.setLoginUrl();
      //alert(document.location.search);
    },

    toString: function() { return '[Trc]' }
  }

  Trc.ContactForm = Neaux.Class( {
    form: null,
    submit: null,

    init: function( aFormID, aBtnSubmit) {
      this.form = $ID(aFormID);
      this.submit = $ID(aBtnSubmit);
      $ON(this.submit, 'click', [this,this.handleSubmit]);
    },
    handleSubmit: function( aEvt) {
      this.handleBeforeSubmit.call(this, aEvt);
      this.submit.disabled = true;
      var theHeader = this.getContentType();
      var theUrl = 'assets/trc.php';
      Neaux.Http.request( theUrl, 'post', Neaux.Function.bind(this.handleFormResponse,this), this.form, theHeader);
    },
    handleFormResponse: function( aResponse) { 
      if ( !aResponse.isDone()) return;
      var theResponse = aResponse.getText();
      var hasNak = (theResponse.indexOf('NAK') >= 0) ? true : false;
      var theStatus = (theResponse.indexOf('ERROR:') >= 0) ? true: false;
      if ( theStatus) theResponse = theResponse.substr('ERROR:'.length);
      //DBG:
      if ( hasNak && !theStatus) { 
        alert('INTERNAL ERROR\n'+theResponse);
        return;
      }
      this.processResults( theStatus, theResponse);
    },
    handleBeforeSubmit: function( aEvt) {},
    processResults: function() { return '[ABSTRACT METHOD]'; },
    getContentType: function() { return null; },
    toString: function() { return '[ContactForm]'; } 
  }); 

  Trc.ContactFormShort = Neaux.Class().Extends(Trc.ContactForm).Define( {
    __construct: function() {
      this.init.apply(this, arguments);
    }, 
    processResults: function( aStatus, aResults) {
      var theStatusText = Trc.CONTACT_SUCCESS_MSG;
      var theMsgArea = $ID('contact_msg');
      var theSidebarHeight = $ID('sidebar').offsetHeight;
      this.submit.disabled = false;
      if ( aStatus) {
        theStatusText = '<u>Please correct the following:</u><br/><br/>' + aResults;
        theMsgArea.className = 'error';
        theMsgArea.innerHTML = theStatusText;
        theMsgArea.style.display = 'block';
      }
      else {
        this.form.reset();
        theMsgArea.className = '';
        //this.form.style.display = 'none';
        document.location.href = 'thank_you.html';
      }
      theSidebarHeight = $ID('sidebar').offsetHeight - theSidebarHeight;
      $ID('content_left').style.height = ($ID('content_left').offsetHeight + theSidebarHeight) + 'px';
      Trc.alignHeight( 'content_left', 'content_right', 'content_container');      
    },
    toString: function() { return '[ContactFormShort]'; } 
  });

  Trc.ContactFormLong = Neaux.Class().Extends(Trc.ContactForm).Define( {
    resumeHtml: null,

    __construct: function() {
      this.init.apply(this, arguments);
      $ON('selReasonLong','change', [this,this.handleReasonChange]);
    },
    loadResume: function() {
      if ( !this.resumeHtml) {
        var resp = Neaux.Http.request( 'assets/resume_form.php');
        if ( !resp.isOK()) throw new Error( 'Server Error # ' + xhr.getStatus());
        this.resumeHtml = resp.getText();
      }
      $ID('resume_section').innerHTML = this.resumeHtml;
    },
    unloadResume: function() { $ID('resume_section').innerHTML = ''; },
    handleBeforeSubmit: function( aEvt) {
      if ( Neaux.isJSIE) {
        var foo = $ID('selReasonLong');
        foo.options[foo.selectedIndex].value = foo.options[foo.selectedIndex].text;
      }
      $ID('reason_code').value = $ID('selReasonLong').selectedIndex;
    },
    handleReasonChange: function( aEvt) {
      var isResumeDisplayed = $ID('resume_container');
      var theSelection = aEvt.getTarget().selectedIndex;
      if ( theSelection != SELECT_CAREER_OPPORTUNITIES && isResumeDisplayed)
        this.unloadResume();
      else if ( theSelection == SELECT_CAREER_OPPORTUNITIES && !isResumeDisplayed)
        this.loadResume();
    },
    processResults: function( aStatus, aResults) {
      var theStatusText = Trc.CONTACT_SUCCESS_MSG;
      var theMsgArea = $ID('contact_form_msg_area');
      var theFormHeight = $ID('contact_form_container').offsetHeight;
      this.submit.disabled = false;
      if ( aStatus) {
        theStatusText = '<u>Please make the following corrections:</u><br/>' + aResults;
        theMsgArea.className = 'error';
        theMsgArea.innerHTML = theStatusText;
        theMsgArea.style.display = 'block';
      }
      else {
        this.form.reset();
        theMsgArea.className = '';
        //this.form.style.display = 'none';
        document.location.href = 'thank_you.html';
      }
      theFormHeight = $ID('contact_form_container').offsetHeight - theFormHeight;
      $ID('content_right').style.height = ($ID('content_right').offsetHeight + theFormHeight) + 'px';
      Trc.alignHeight( 'content_left', 'content_right', 'content_container');      
    },
    getContentType: function() { return $ID('resume_container') ? { 'content-type': Neaux.Http.CONTENT_TYPE.UPLOAD } : null },
    toString: function() { return '[ContactFormLong]'; } 
  });

  Trc.SITE_LOGIN_PAGE = 'site_login.html';
  Trc.CONTACT_SUCCESS_MSG = 'Your request was submitted.<br/><br/>We will contact you shortly.<br/><br/>If you haven\'t received a response from us in 48 hours, please call 1-800-783-5337';

  Neaux.onload(Trc.start);
})();
