

function showHotel()
{
  $('FORUMS_HAC').style.display = 'block';
  $('FORUMS_FLIGHTS').style.display = 'none';
}

function showFlight()
{
  $('FORUMS_HAC').style.display = 'none';
  $('FORUMS_FLIGHTS').style.display = 'block';
}

rules['#FLIGHT_FORM input.flightTypeAhead[type=text]'] = function(elmt) {
  var action = elmt.className.match(/\bact(\w+)\b/);
  if (action) {
    new Autocompleter.Ajax.Json2(elmt, "/TypeAheadJson?action="+action[1], {
      ajaxOptions: {method:'get'},
      postVar: 'query',
      className: 'autocompleter-choices flights',
      inheritWidth: false,
      selectOnBlur: true,
      onSelect: function(elmt, resObj) {
        elmt.value = resObj.value;
        elmt.fireEvent('updateairport');
      }
    });
  }
};

ta.forums = {
  showExpertOverlay: function(evnt, elmt) {
    evnt.stop();
    elmt.onclick = null;
    new ta.overlays.RelativeOverlayLeft({
      showCloseButton: true,
      activate: ta.overlays.ACTIVATE_CLICK,
      style: 'typeO expertOverlay'
    }, elmt);
  }
}

function hideRightColumn(bHide,pid)
{
  Cookie.remove('HideRightColumn'); // for people who may have old cookies from a previous release
  if (bHide)
    Cookie.set('HideRightColumn', "true", {domain:cookieDomain,duration:365});
  else
    Cookie.remove('HideRightColumn', {domain:cookieDomain});
  setOneTimeCookie('NPID',pid);
  location.reload();
}

function widthOnOver(elem){
  $(elem).addClass('over');
}

function widthOnOut(elem){
  $(elem).removeClass('over');
}


function getReturnURL(formId)
{
  return inap_returnUrl + '#' + formId;
}

var errorRetry = function() { this.setContent('An error has occurred, please try again later.'); };


/**
 * Launch an ajax request for a forum vote selector.  On success, selector is written
 * into page with an onchange event handler that launches an ajax request saving
 * selection.
 *
 * Assumes a container with id in this form: voteForm<post id>
 * The selector will be written into a child of this container with the id 'reasons'
 * The last selected form is stored in window.forumVote and is used to keep only one form
 * visible at a time.  Once a vote has been cast, the form is replaced by a success message,
 * which will be shown each time the user clicks the report link.
 */
var showVoteForm = function(postId) {
  if (window.forumVote) {
    window.forumVote.hide();
  }
  var formId = postId;
  var forumVote = $("voteForm" + formId);
  new Ajax('/ForumVote', {
             method: 'get',
             update: forumVote.getElement('.reasons'),
             onSuccess: function() {
               window.forumVote = forumVote;
               forumVote.show();
               var sel = forumVote.getElement('select');
               if (sel) {
                 sel.addEvent('change', function(){
                                if (inap_loggedin) {
                                  var form = forumVote.getElement('form');
                                  processFootprint(form);
                                  new Ajax('/ForumVote', {
                                             update: forumVote,
                                             data: form,
                                             onFailure: errorRetry.bind(forumVote)
                                  }).request();
                                } else {
                                  var callbackParam = "voteAfterLogin|" + formId + "," + sel.selectedIndex;
                                  Cookie.set('ajaxAction', callbackParam, {domain:cookieDomain, path:"/"});
                                  sel.selectedIndex = 0;
                                  sel.hide();
                                  login(['returnTo', getReturnURL(formId)]);
                                }
                 });
               }
             },
             onFailure: errorRetry.bind(forumVote)
           }).request();
  return false;
};


function voteAfterLogin(stringParam)
{
  if (!inap_loggedin) return;
  var params = stringParam.split(",");
  var postId = params[0];
  var reason = parseInt(params[1]);
  window.location.hash = '#' + postId;

  var elmt = $("voteForm" + postId);
  var fm = elmt.getElement('form');

  processFootprint(fm);
  new Element('input', {type: 'hidden', value: reason, name: 'reason'}).injectInside(fm);
  new Ajax('/ForumVote', {
    update: elmt,
    data: fm,
    onSuccess: function () { elmt.show(); },
    onFailure: errorRetry.bind(elmt)
  }).request();
}

function processFootprint(formElement) {
  formElement.screenWidth.value = screen.width;
  formElement.screenHeight.value = screen.height;

  try {
    var FootprintId = TALSO.get('CookieId');
    if(FootprintId == '') {
      TALSO.set('CookieId', formElement.footprint.value);
      if (TALSO.get('CookieId') != formElement.footprint.value) {
	formElement.footprint.value='';
      }
    } else {
      formElement.footprint.value=FootprintId;
    }
  } catch(e) {
    formElement.footprint.value='';
  }
}


function watchTopic(url, waitingText)
{
  // hide all watch buttons before the request to avoid duplicate notification requests
  $$('.watchTopicButton').each( function(item, index) { item.remove(); } );

  // only needed during bounce, can be removed after - dkogan
  if (!waitingText) {
    waitingText = 'Waiting for response...';
  }
  

  $$('.watch_topic_response').each(
    function(item, index) { item.setHTML("<div><span>" + waitingText + "</span></div>"); }
  );

  new Ajax(url, {onComplete: watchTopicResponse, onFailure: watchTopicError}).request();
}

// watch a topic AJAX response
function watchTopicResponse(content)
{
  if (!content) {
    watchTopicError({message: "empty response"});
    return;
  }

  $$('.watch_topic_response').each(function(item, index) {item.setHTML(content);});
}

function watchTopicError(e)
{
  $$('.watch_topic_response').each(
    function(item, index) {
      item.setHTML("<div class='errMsg'><span>An error has occured while making your request.  Please try again later</span></div>");
	});
}

function showSpinner()
{
    $('SBMT_WRP').hide();
    $('SBMT_SPNR').show();	
}

function hideSpinner()
{
    $('SBMT_WRP').show();
    $('SBMT_SPNR').hide();
}


function previewLinkHandler(formName)
{
   $('submitAction').value = "submitPreview";
   showSpinner();
   $(formName).submit();
}

function moreLink(postCount)
{
   $('post'+postCount).removeClass('off');
}

function lessLink(postCount)
{
   $('post'+postCount).addClass('off');
}

function submitFacebookComplete(formName)
{
   $('submitAction').value = "fbComplete";
   showSpinner();
   $(formName).submit();
}

function fbConnectForums(formName)
{
	popupFacebookConnect('FORUMREPLY',function(evnt){if(!evnt)submitFacebookComplete(formName);},'post reply checkbox');
}


function submitPreview()
{
   manageFootprint();
   $(forumFormName).submit();
}

function notYouFunction(formName)
{
   $('notYou').value = "1";
   $(formName).submit();
}

function viewMax(formName)
{
   var form = $("RECENT");
   var url = window.location.href
   url += '?viewMaxReplies=1';
   form.innerHTML = "";
   $("LOADING").removeClass("off");
   new Ajax(url, {
      //evalScripts: true,
      onComplete: function (txt, xml) {            	
         $("LOADING").addClass("off");
         form.innerHTML = txt;
         window.behavior.apply(form);
     }
   }).request();  
}

var forumFormName = "";

function loginForums(form, forceMigrate)
{
   // manage login/migration case
   var params = ['tt','forums','returnTo',window.location];
   forumFormName = form;
  if (forceMigrate) {
      migrate(params);
   } else {
      login(params);
   }
}

var loginSuccess = function(returnTo, tt) {
    manageFootprint();
    $(forumFormName).submit();
}

function requestPreview(formName) {
   showSpinner(); 
   forumFormName = formName;
    new Ajax('/ForumPostPreview', {
        data: $(formName),
        evalScripts: true,
        onComplete: previewResponse
    }).request();
 
}

var previewResponse = function(t, x) {
      new ta.overlays.CenteredOverlay({
         backdrop: ta.overlays.BACKDROP_ALWAYS,
         showCloseButton: false,
         style: 'ur_prev',
         onLoad: function(lb) {
         var h = window.getHeight() - 50;
         $('PREVIEW_CANCEL').addEvent('click', lb.hide.bind(lb));
    }
  }).loadRemoteSuccess(t);
  hideSpinner();
}

