var psk75 = (function () {
	var createRandomString = function () {
		var length = (arguments.length === 1) && (arguments[0].constructor === Number) ? arguments[0] : 10;
		var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		var randomString = "";

		while (randomString.length !== length) {
			randomString += chars.charAt(Math.floor(Math.random() * chars.length));
		}

		return randomString;
	};

	var replaceCaptchas = function () {
		$(document).find('img[src*="captcha.ashx"]').each(function () {
			var captcha = document.createElement("img");
			captcha.src = "./captcha.ashx?id=" + createRandomString();
			captcha.alt = "CAPTCHA";
			captcha.title = "CAPTCHA";
			$(this).replaceWith(captcha);
		});
	};

	var ajaxifyCaptchaForms = function () {
		$('form:has(img[src*="captcha.ashx"])').each(function () {
			var nextWebPageId = $(this).find('input[name="__nextwebpageid"]').val();
			var responseMessage = "0 1";
			$(this).find('input[name="__nextwebpageid"]').remove();
			$(this).find('input[name="__errorwebpageid"]').remove();

			if ($(this).find('input[name="__RESPONSEMESSAGE"]').length === 0) {
				var input = document.createElement("input");
				input.type = "hidden";
				input.name = "__RESPONSEMESSAGE";
				input.value = responseMessage;
				$($(this).find('input[type="hidden"]')[0]).before(input);
			}

			$(this).submit(function () {
				$.ajax({
					type: 'POST',
					url: $(this).attr('action'),
					data: $(this).serialize(),
					success: function (data, textStatus, XMLHttpRequest) {
						if (data === responseMessage) {
							location.assign(/^http/i.test(nextWebPageId) ? nextWebPageId : (location.protocol + "//" + location.hostname + "/?pg=" + nextWebPageId));
						} else {
							$("#psk75message").html(data);
							replaceCaptchas();
						}
					}
				});

				return false;
			});
		});
	};

	return {
		ajaxifyCaptchaForms: ajaxifyCaptchaForms,
		replaceCaptchas: replaceCaptchas
	};
}());
