function LBD_LoadSound(soundPlaceholderId, soundLink)
{
    if(document.getElementById)
    {
				soundLink = soundLink + '&d=' + new Date().getTime();

        var placeholder = document.getElementById(soundPlaceholderId);
        var objectSrc = "<object id='captchaSound' classid='clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95' height='0' width='0' style='width:0; height:0;'><param name='AutoStart' value='1' /><param name='Volume' value='0' /><param name='PlayCount' value='1' /><param name='FileName' value='" + soundLink + "' /><embed id='captchaSoundEmbed' src='"+ soundLink + "' autoplay='true' hidden='true' volume='100' type='"+ LBD_GetMimeType() +"' style='display:inline;' /></object>";

        placeholder.innerHTML = "";
        placeholder.innerHTML = objectSrc;
    }
}

function LBD_GetMimeType()
{
    var mimeType = "audio/x-wav"; //default
    return mimeType;
}

var LBD_ImgId = null;
var LBD_Img = null;
var LBD_NewImg = null; 
var LBD_Parent = null;
var LBD_Prompt = null;

function LBD_ReloadImage(imgId)
{  
	if(imgId)
	{
		LBD_ImgId = imgId;
		LBD_Img = document.getElementById(LBD_ImgId);
		var newSrc = LBD_Img.src + '&d=' + new Date().getTime();

		LBD_NewImg = document.createElement('img');
		LBD_NewImg.onload = LBD_ShowImage;
		LBD_NewImg.id = LBD_Img.id;
		LBD_NewImg.alt = LBD_Img.alt;
		LBD_NewImg.src = newSrc;

		LBD_Prompt = document.createElement('span');
		LBD_Prompt.appendChild(document.createTextNode('loading...'));

		LBD_Parent = LBD_Img.parentNode;
		LBD_Parent.removeChild(LBD_Img);
		LBD_Parent.appendChild(LBD_Prompt);
	}
}

function LBD_ShowImage()
{
	if(LBD_NewImg && LBD_Parent && LBD_Prompt)
	{
		LBD_Parent.removeChild(LBD_Prompt);
		LBD_Parent.appendChild(LBD_NewImg);
	}
}

var validationResult = false;

function Validate()
{
    if(!validationResult)
    { 
        StartValidation(); 
    } 
    
    return validationResult;
}

var inputElem = null;
var prompt = null;
var labelIncorrect = null;
var parent = null;

function StartValidation()
{
    validationResult = false;
    
    inputElem = document.getElementById('TextBoxUserCode');
    labelIncorrect = document.getElementById('MessageIncorrectLabel');
    parent = labelIncorrect.parentNode;
        
    prompt = document.createElement('span');
    prompt.id = 'LBD_ValidatingPrompt';
    prompt.appendChild(document.createTextNode('validating...'));
    
    labelIncorrect.style.display = 'none';
    parent.appendChild(prompt);
    
    return LBD_ValidateCode(inputElem.value, EndValidation);
}

function EndValidation(result)
{
    parent.removeChild(prompt);
    labelIncorrect.style.display = 'inline';
    
    if(result)
    {
        labelIncorrect.style.visibility = 'hidden'; 
        validationResult = true;
        document.getElementById('ProcessForm').click();
    }
    else
    {
        labelIncorrect.style.visibility = 'visible';
    }
}

function LBD_ValidateCode(code, callback)
{
    if(document.getElementById)
    {
        var input = code; // user input
        
        var result = false;
        $.getJSON(  "LanapBotDetectHandler.asp", // server path
                { Command: "Validate", Code: input }, // querystring params
                function(json){ LBD_ProcessValidationResult(json.result, callback) } // callback function
        );

        return result;
    }
}

function LBD_ProcessValidationResult(result, callback)
{
    if(!result)
    {
        LBD_ReloadImage('LBD_CaptchaImage');
    }
    
    if("function" == typeof(callback))
    {
        callback(result);
    }
}