(function($) {
	var domains = ["rencontres-paris.love.easy-rencontres.com","rencontres-paris.sexy.easy-rencontres.com","rencontres-paris.gay.easy-rencontres.com","rencontres-paris.destuny.com"],
		target = "",
		template = "<!-- Start form -->\n<form id=\"ez-login-form\" action=\"\" method=\"get\">\n\t<div id=\"ez-login-errors\" class=\"ez-form-errors\" style=\"display: none;\"><\/div>\n\t<dl>\n\t\t<dt><label for=\"ez-login-login\">Pseudo<\/label><\/dt>\n\t\t<dd><input id=\"ez-login-login\" type=\"text\" name=\"login\" class=\"text\" \/><\/dd>\n\t\t\n\t\t<dt><label for=\"ez-login-password\">Mot de passe<\/label><\/dt>\n\t\t<dd><input id=\"ez-login-password\" type=\"password\" name=\"pass\" class=\"text\" size=\"4\" maxlength=\"4\" \/><\/dd>\n\t\t\n\t\t\t\t<dd style=\"display: none;\"><input type=\"hidden\" name=\"ct\" value=\"0\" \/><\/dd>\n\t\t\t\t<dd><input type=\"submit\" name=\"\" value=\"ok\" class=\"ez-button\" \/><\/dd>\n\t<\/dl>\n<\/form>\n<!-- End form -->\n",
		hideTimer;
	
	if (target.length) {
		$(function() {
			$(target).html(template);
		});
	} else {
		document.write(template);
	}
	
	$(function() {
		$('#ez-login-form').submit(function() {
			var $this = $(this),
				$submit = $this.find('input[type=submit]'),
				rawData = $this.serializeArray(),
				data = {};
		
			clearTimeout(hideTimer);
			
			for (var i=0; i<rawData.length; i++) {
				data[rawData[i].name] = rawData[i].value;
			}
	
			var domain = domains[data.ct];
			
			$submit.attr('disabled', true);
			
			$.ajax({
				url: 'http://rencontres-paris.love.easy-rencontres.com/webservices/testLog.php',
				dataType: 'jsonp',
				data: data,
				success: function(response) {
					if (/^\d+$/.test(response)) {
						("" ? window : window.top).location = 'http://' + domain + '/login.php?m_login=' + data.login + '&m_pass=' + data.pass;
					} else {
						var errorMsg = "";
						switch (response) {
							case 'BADPASS':
							case 'BADLOGIN':
								errorMsg = "Identifiants incorrects <a href=\"http://%domain%/?lost=1\">passe perdu ?</a>".replace('%domain%', domain);
								break;
							
							case 'TOOMANYCNX':
								errorMsg = "Identifiants incorrects, veuillez rÃ©essayer plus tard";
								break;
							
							default:
								break;
						}
						
						$('#ez-login-errors').html(errorMsg).show(200);
						$submit.attr('disabled', false);
						hideTimer = setTimeout(function() {$('#ez-login-errors').hide(200);}, 30*1000);
					}
				}
			});
			
			return false;
		});
	});
})(jQuery);
