



function Captcha(imageId,audioId, keyInputId) {
    this.captchaId = null;
    this.imageElementId = imageId;
    this.audioElementId = audioId;
    this.keyInputElementId = keyInputId;
    this.verified = false;
    this.audioSrc=null;
    this.serviceContext = "/pubajax/captchaService/v1.0/";
	this.validationKeyDelayMsg = "We're experiencing delays in processing validation keys. Please retry in 5 minutes.";
	this.errorImgId = "error_captcha_image";
}

Captcha.prototype.initCaptchaImage = function() {
	 var bindArgs = {
        url:        this.serviceContext + "?ts=" + escape(new Date()),
        method:     "get",
        load:       dojo.lang.hitch(this, this.init),
        error:      function(type, data, evt) {
                        alert("Problem initializing - please try again");
                    },
        mimetype:   "text/json"
    };

    dojo.io.bind(bindArgs);
}

Captcha.prototype.init = function(event, json) {
    this.captchaId = json.imageId;
    //.replace("http","https")
    var captchaImageSrc = json.src;
    var omDomain = document.location.hostname.toLowerCase();
      var isQA = (omDomain.indexOf("qa") != -1)?true : false;
      var isPROD = ((omDomain.indexOf("prod") != -1) || (omDomain.indexOf("beta") != -1)) ? true : false;
    if(isQA){
      captchaImageSrc = json.src.replace("http://qa.mlb.com/","https://qasecure.mlb.com/");
    }
	else{
      captchaImageSrc = json.src.replace("http://www.mlb.com/","https://secure.mlb.com/");
    }
    document.getElementById(this.imageElementId).src = captchaImageSrc;
      if(json.imageId == this.errorImgId){
            alert(this.validationKeyDelayMsg);
            Captcha.onImageError();
      }
}

Captcha.prototype.initCaptchaAudio = function() {
	 var bindArgs = {
        url:        this.serviceContext +"?captchaType=audio",
        method:     "get",
        load:       dojo.lang.hitch(this, this.play),
        error:      function(type, data, evt) {
                        alert("Problem initializing - please try again");
                    },
        mimetype:   "text/json"
    };

    dojo.io.bind(bindArgs);
}

Captcha.prototype.play = function(event, json) {

    this.captchaId = json.audioId;

    this.audioSrc=json.srcAudio;

    var captchaAudioSrc = json.srcAudio

    var omDomain = document.location.hostname.toLowerCase();

      var isQA = (omDomain.indexOf("qa") != -1)?true : false;

      var isPROD = ((omDomain.indexOf("prod") != -1) || (omDomain.indexOf("beta") != -1)) ? true : false;

    if(isQA){

      captchaAudioSrc = json.srcAudio.replace("http://qa.mlb.com/","https://qasecure.mlb.com/");

    }
	else{

      captchaAudioSrc = json.srcAudio.replace("http://www.mlb.com/","https://secure.mlb.com/");

    }

    

    var embedCode = '<EMBED SRC=' + captchaAudioSrc + ' HIDDEN="true" AUTOSTART="true" >';

    document.getElementById(this.audioElementId).innerHTML = embedCode;

      if(json.audioId == this.errorAudioId){

            alert(this.validationKeyDelayMsg);

            Captcha.onImageError();

      }

}

Captcha.prototype.verifyKey = function() {
    var inputKey = document.getElementById(this.keyInputElementId).value;
    var postURL = this.serviceContext + "?imageId=" + this.captchaId +
        "&key=" + inputKey;

    var bindArgs = {
        url:        postURL,
        sync:       true,
        method:     "post",
        load:       dojo.lang.hitch(this, this.verify),
        error:      function(type, data, evt) {
                        alert("Problem verifying - please try again");
                    },
        mimetype:   "text/json"
    };

    dojo.io.bind(bindArgs);

    return this.verified;
}

Captcha.prototype.verify = function(event, json) {
    if (json.keyValid) {
        this.verified = true;
    } else {
        this.verified = false;
    }
}

Captcha.onImageError = function(){};