﻿if (Object.isUndefined(Nmma)) { var Nmma = { } };
if (Object.isUndefined(Nmma.Shared)) { Nmma.Shared = { } };

Nmma.Shared.Captcha = {
  clear: function() { $('Captcha').clear(); $('Captcha').focus(); },
  delayedshow: function() { $('imgCaptchaLoader').hide();$('imgCaptcha').show(); },  
  load: function() { Nmma.Shared.Captcha.clear(); $('divCaptcha').update('<img id="imgCaptchaLoader" name="imgCaptchaLoader" border="0" style="width:16px;height:16px;padding-top:20px;" src="/lib/img/icons/16/loader.gif"><img id="imgCaptcha" name="imgCaptcha" style="display:none;" border="0" src="/shared/pages/security/captcha.aspx?' + 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}).toUpperCase() + '">'); Nmma.Shared.Captcha.delayedshow.delay(0.25); },  
  help: function() {  
	  var popup = new Nmma.Dialog({ 'title': 'Instructions', 'width': 400, 'draggable': true, 'showButtons': false }); 
	  popup.setContent('<div style="padding:10px;"><ul><li style="padding-bottom:10px;>Please enter the characters. Doing so helps prevent automated programs from abusing this service.</li><li>if you are not sure what the characters are (characters are either numbers or capitalized letters), either enter your best guess or click the reload button next to the distorted characters.</li></ul></div>');
    popup.show();
  },
  validate: function(form) {
    Nmma.Ajax.request('/shared/pages/ajaxhandlers/captcha/captcha.aspx', { parameters: { 'form': form, 'arguments': { 'ActionType': 'validate' } },
		  onSuccess: function(response) {
		    switch (response.getMessage())
		    {
		      case 'ok':
		        break;
		      case 'empty':
		        break;		      
		      case 'nok':
            $('Captcha').update(''); 
            Nmma.Shared.Captcha.load(); 
            $('Captcha').focus();		      
		        break;
		      default:  
            $('Captcha').update(''); 
            Nmma.Shared.Captcha.load(); 
            $('Captcha').focus();		      
		        break;            
		    }
		  },
			onFailure: function(response) { $('Captcha').update(''); Nmma.Shared.Captcha.load(); $('Captcha').focus(); }
    });
  }  
};
