// captcha js

$(document).ready(function(){
	
	$captcha = '<div class="captchawrapper"><img id="captcha" src="/site-resources/captcha/secureimage/securimage_show.php?sid=<?php echo md5(uniqid(time())); ?>"><br /><a href="/site-resources/captcha/secureimage/securimage_play.php" style="font-size: 13px"><img src="/site-resources/captcha/secureimage/images/audio_icon.gif" alt="Audio" /> listen</a><br /><a href="javascript:void(0);" onclick="document.getElementById(\'captcha\').src = \'/site-resources/captcha/secureimage/securimage_show.php?\' + Math.random(); return false"><img src="/site-resources/captcha/secureimage/images/refresh.gif" alt="Reload Image" /> try a different image</a><br /><br /><strong>Please enter the text in the above image into the following text box</strong><br /><input type="text" name="captcha_code" class="captcha_code" /><br /><div class="captchaFeedback"></div></div>';

	$('.formcaptcha .captchawrapper').remove();
	$('.formcaptcha input[type="submit"]').before($captcha);
	$('.formcaptcha').attr('onsubmit','return captchaCheck(this);');
	
});

function captchaCheck(theform){
	$f = $(theform);
	var savingData = "captcha_code="+$f.find('.captcha_code').val();
	$.ajax({
		url: '/site-resources/captcha/secureimage/captchacheck.php',
		type: 'post',
		data: savingData,
		success: function(msg){
			//console.log(msg);
			if(msg == "1"){
				//console.log('ok');
				var hotel="";
				if($('select[name=hotel__mandatory]').length>0){
					hotel=$('select[name=hotel__mandatory]').val();
				}
				_gaq.push(['_trackEvent','formsend',window.location.href+hotel,'1']);
				theform.submit();
			} else {

			
				$f.find('.captchaFeedback').html("Captcha code incorrect. Please try again.");
			}
		}
	});
	return false;
	//myConn.connect("/site-resources/secureimage/captchacheck.php","POST",savingData,captchaDone);	
}
