﻿// jQuery-driven behaviours for the homepage

// determine the form action based on the length of the user ID entered
function userIDChangedState()
{
   oldSubmitAction = $('#login').attr('action');
   switch ($('#userid').val().length)
   {
      case 6:
         // OLO URL
         $('#login').attr('action','http://www.btsmartnumbers1.com/Teamphone/TPDir/Libs/Logon/Verify.asp');
         break;
      case 5:
         // eckoh URL
         $('#login').attr('action','http://www.smart-numbersone.com/Teamphone/TPDir/Libs/Logon/Verify.asp');
         break;
   }
   //if (oldSubmitAction != $('#login').attr('action')) alert ('Changed submit target to '+$('#login').attr('action'));
}

// validate the form values provided before submitting
function validateLogin()
{
   boolValidates = true;
   strAlert = 'Unable to login\n\n';
   
   // both userid and password must be non-empty
   if ($('#userid').val().length == 0)
   {
      boolValidates = false;
      strAlert += '• User ID is empty\n';
   }
   
   if ($('#password').val().length == 0)
   {
      boolValidates = false;
      strAlert += '• Password is empty\n';
   }
   
   if ($('#LocationID').val() == 'Temporary' && $('#TemporaryNumber').val().length == 0)
   {
      boolValidates = false;
      strAlert += '• Temporary number is empty\n';
   }
   
   strAlert += '\nPlease fix the above problem(s) and try again.';
   
   if (boolValidates == false) 
   {
      alert(strAlert);
   }
   else
   {
      // alert('submitting form to '+$('login').action);
   }
   
   return boolValidates;
}

// toggle display of the advanced login options
function toggleLoginOptionsOn()
{
   $('fieldset dl.loginoptions').slideDown();
   $('#login').animate({height: ($('#login').is('.alert')?307:245)});
   $('#login fieldset dl dt a').blur();
   $('#login fieldset dl dt a').toggleClass('expanded');
   return false;
}

// toggle display of the advanced login options
function toggleLoginOptionsOff()
{
   $('fieldset dl.loginoptions').slideUp();
   $('#login').animate({height: ($('#login').is('.alert')?207:145)});
   $('#login fieldset dl dt a').blur();
   $('#login fieldset dl dt a').toggleClass('expanded');
   return false;
}

// show the associated content and change selected state for tab clicks
function showSolutionTab(intTab)
{
   for (i=1; i<=3; i++)
   {
      $('#solutionselector a.tab'+i).removeClass('selected');
      $('#solutionselector div.tab'+i).hide();
   }
   
   // blur and add selected class to the tab just activated
   $('#solutionselector a.tab'+intTab).blur();
   $('#solutionselector a.tab'+intTab).addClass('selected');
   $('#solutionselector div.tab'+intTab).show();

   return false;
}

// show an error message if the user failed to login
// example failure messages:
// DisplayText=Failed+to+logon+with+ID%3A257325
// DisplayText=ID+not+set%2E
// DisplayText=Password+not+set%2E
//
// generated markup looks like this:
//
// <dl class="error">
//    <dt>Login failed</dt>
//    <dd>This is why</dd>
// </dl>

function displayLoginFailureMessage()
{
   var qs = new Querystring();
   if (qs.get('DisplayText') && qs.get('DisplayText').length > 0)
   {
      $('#login').addClass('alert');
      $('fieldset.login').prepend('<dl class="error"><dt>Login failed</dt><dd>'+qs.get('DisplayText')+'</dd></dl>');
   }
}

$(document).ready(function()
   {
      // attach events handling length of user ID to the form
      $('#userid').keyup(function(){userIDChangedState();});
      $('#userid').blur(function(){userIDChangedState();});

      // attach form validation event
      $('#login').submit(function(){return validateLogin();});
      
      // attach event to the 'More options' link to toggle the options
      $('a.loginoptions').toggle(function(){return toggleLoginOptionsOn();},function(){return toggleLoginOptionsOff();});

      // activate a tab if the URI fragment indicates it was previously selected at load
      strTabName = window.location.hash.replace('#','');
      if (strTabName)
      {
         for (i=1; i<=3; i++)
         {
            $('#solutionselector a.tab'+i).removeClass('selected');
            $('#solutionselector div.tab'+i).hide();
         }
         $('#solutionselector div[@rel='+strTabName+']').show();
         $('#solutionselector a[@rel='+strTabName+']').addClass('selected');
      }
  
      // attach events to the solutions selector tabs
      $('#solutionselector a.tab1').click(function(){return showSolutionTab(1);});
      $('#solutionselector a.tab2').click(function(){return showSolutionTab(2);});
      $('#solutionselector a.tab3').click(function(){return showSolutionTab(3);});
      
      // display the error message (if applicable)
      displayLoginFailureMessage();
      
      // force a refresh of element positions to fix layout bugs in IE6
      $(window).resize(
         function()
         {
            $('#solutionselector div').css({position:'relative'});
            $('#solutionselector div').css({position:'absolute'});
         }
      );
      
   }
);

