/*******************************************************************************
* This library is designed to work with the captcha mark-up and styles found in
* the following files:
*    1) captcha.jsp
*    2) captcha.css
*******************************************************************************/

// Create a namespace for the code in this file.  All variables and functions
// coded below are defined within this single object.
var Captcha = {};

// Identifiers for each type of challenge.
Captcha.AUDIO  = "A";
Captcha.VISUAL = "V";

// Text for user input prompt.
Captcha.AUDIO_PROMPT  = "Type the numbers you hear:";
Captcha.VISUAL_PROMPT = "Type <a href='javascript:Captcha.helpToggle();'>security code</a> above:";

// Text used for the title attribute of the image used to switch between challenge types.
Captcha.AUDIO_IMAGE_TITLE  = "Audio version for the visually impaired";
Captcha.VISUAL_IMAGE_TITLE = "View the security code as an image";

// URLs used to obtain a challenge.
Captcha.AUDIO_URL  = "/public.ejs?command=PublicJCaptcha&amp;type=audio";
Captcha.VISUAL_URL = "/public.ejs?command=PublicJCaptcha&amp;type=visual";

// Challenge area dimensions.
Captcha.CHALLENGE_WIDTH  = "225px";
Captcha.CHALLENGE_HEIGHT = "65px";

// Path to image files.
Captcha.AUDIO_IMAGE  = "dynsite/images/buttons/audio.gif"
Captcha.VISUAL_IMAGE = "dynsite/images/buttons/info.gif"

// Path to Nifty Corners CSS file.
Captcha.NIFTY_CORNER_CSS = "stylesheets/niftyCorners.css";

// Current state of the user help text.
Captcha.HELP_DISPLAYED = false;

// This method is registered to run when the browser's onload event is fired.
Captcha.init = function() {
  // An image challenge is the default.
  Captcha.setChallengeType(Captcha.VISUAL);  

  // Library used to create rounded corners.
  AddCss(Captcha.NIFTY_CORNER_CSS);
  Nifty("div#captcha_main,div#captcha_response,div#captcha_help_inner");
  Nifty("div#captcha_options", "small")
}

// Displays a new challenge of the currently displayed type.
Captcha.refresh = function() {
  var captchaChallenge = document.getElementById("captcha_challenge");
  var captchaResponse = document.getElementById("captchaResponse");
  var captchaImage = document.getElementById("captcha_image");

  if (Captcha.getChallengeType() == Captcha.AUDIO) {
    captchaChallenge.innerHTML = Captcha.audioMarkup(true);
  }
  else {
    captchaImage.src = Captcha.VISUAL_URL + "&amp;timestamp=" + new Date().getTime();
  }

  captchaResponse.value = "";
  captchaResponse.focus();
}

// Switches between an audio and visual challenge.
Captcha.switchType = function() {
  if (Captcha.getChallengeType() == Captcha.AUDIO) {
    Captcha.displayVisualChallenge();
  }
  else {
    Captcha.displayAudioChallenge(true);
  }
}

// Displays an audio challenge.  The 'autoStart' parm is a boolean used to 
// control how the audio challenge starts playing.
Captcha.displayAudioChallenge = function(autoStart) {
  var captchaChallenge = document.getElementById("captcha_challenge");
  var captchaIcon = document.getElementById("captcha_icon");
  var captchaResponse = document.getElementById("captchaResponse");
  
  captchaChallenge.innerHTML = Captcha.audioMarkup(autoStart);
  
  captchaIcon.src = Captcha.VISUAL_IMAGE;
  captchaIcon.title = Captcha.VISUAL_IMAGE_TITLE;
  
  captchaResponse.value = "";
  captchaResponse.focus();
  
  Captcha.setChallengeType(Captcha.AUDIO);
  Captcha.setPrompt();  
}

// Displays a visual challenge. 
Captcha.displayVisualChallenge = function() {
  var captchaChallenge = document.getElementById("captcha_challenge");
  var captchaIcon = document.getElementById("captcha_icon");
  var captchaResponse = document.getElementById("captchaResponse");
  
  captchaChallenge.innerHTML = Captcha.visualMarkup();
  
  captchaIcon.src = Captcha.AUDIO_IMAGE;
  captchaIcon.title = Captcha.AUDIO_IMAGE_TITLE;
  
  captchaResponse.value = "";
  captchaResponse.focus();
  
  Captcha.setChallengeType(Captcha.VISUAL);
  Captcha.setPrompt();
}

// HTML markup that presents the audio challenge. Width & height are hard-coded 
// to prevent screen flicker.  The 'autoStart' parm is a boolean used to control 
// how the audio challenge starts playing.
Captcha.audioMarkup = function(autoStart) {
            // object tag will be read by IE.
  var s =  '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6"';
      s += ' codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"';
      s += ' type="application/x-oleobject"';
      s += ' height="' + Captcha.CHALLENGE_HEIGHT + '"';
      s += ' width="' + Captcha.CHALLENGE_WIDTH + '">';
      s += '   <param name="url" value="' + Captcha.AUDIO_URL + '"/>';
      s += '   <param name="autostart" value="' + autoStart + '"/>';
      s += '   <param name="uimode" value="mini"/>';
      s += '   <param name="volume" value="100"/>';
				 // Embed tag will be read by non-IE browsers.
      s += '     <embed class="ie_hidden"';
      s += '      classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"';
      s += '      pluginspage="http://www.apple.com/quicktime/download/"';
      s += '      type="audio/mpeg"';
      s += '      src="' + Captcha.AUDIO_URL + '"';
      s += '      height="' + Captcha.CHALLENGE_HEIGHT + '"';
      s += '      width="' + Captcha.CHALLENGE_WIDTH + '"';
      s += '      controller="true"';
      s += '      autoplay="' + autoStart + '"/>';
      s += '</object>';

  return s;
}

// HTML markup that presents the visual challenge.
// Width & height are hard-coded to prevent screen flicker.
Captcha.visualMarkup = function() {
  var s =  '<img id="captcha_image"';
      s += '  src="' + Captcha.VISUAL_URL + '&amp;timestamp=' + new Date().getTime() + '"';
      s += '  alt="captcha challenge"';
      s += '  width="' + Captcha.CHALLENGE_WIDTH + '"';
      s += '  height="' + Captcha.CHALLENGE_HEIGHT + '"/>';

  return s;
}

// Toggles the display attribute for user help.
Captcha.helpToggle = function() {
  var e = document.getElementById("captcha_help_outer");
  
  if (Captcha.HELP_DISPLAYED) {
    e.style.display = "none";
    Captcha.HELP_DISPLAYED = false;
  }
  else {
    e.style.display = "block";
    e.scrollIntoView();
    Captcha.HELP_DISPLAYED = true;
  }
}

// This value is used to keep track of the challenge type (audio or visual) currently displayed.
Captcha.setChallengeType = function(challengeType) {
  document.getElementById("captchaChallengeType").value = challengeType;
}

Captcha.getChallengeType = function() {
  return document.getElementById("captchaChallengeType").value;
}

// Sets the text of the user prompt based on the challenge type displayed.
Captcha.setPrompt = function() {
  var captchaPrompt = document.getElementById("captcha_prompt");
  
  if (Captcha.getChallengeType() == Captcha.AUDIO) {
    captchaPrompt.innerHTML = Captcha.AUDIO_PROMPT;
  }
  else {
    captchaPrompt.innerHTML = Captcha.VISUAL_PROMPT;
  }
}

