$(document).ready(function(){
	// Setup the waiting widget and message container
	$("#domain").after("<span>."+$("#rootDomain").html()+"</span><div class='helpers'><img src='"+SETTINGS.skinRoot+"/img/ajax-loader.gif' id='working' /><div id='domainStatusMsg' class='message' /></div>");
	$("#working").hide();
	
	$("#signupForm").validate({
		onkeyup:false,
		rules:{
			name_first:{required:true},
			name_last:{required:true},
			login:{required:true,email:true},
			pass:{required:true},
			pass_confirm:{required:true,equalTo:"#pass"},
			eusa:{required:"input#eusa:not(:checked)"},
			domain:{
				required:true,
				remote:{
					url:SETTINGS.webRoot+"/administration/signup/check-domain/index.php",
					beforeSend:function(req)
					{
						$("#working").show();
						$("#domainStatusMsg").removeClass("good error").html("Checking sub-domain...");
					},
					complete:function(data,txtStatus)
					{
						$("#working").hide();
						if(data.responseText == "true")
						{
							$("#domainStatusMsg").addClass("good").removeClass("error").html("Sub-Domain available!");
						}else{
							$("#domainStatusMsg").removeClass("good").addClass("error").html("Sub-Domain not available!");
						}
					}
				}
			},
			site_name:{required:true}
		},
		messages:{
			domain:{
				remote:"That domain is not available - please choose another one."
			},
			pass_confirm:{
				equalTo:"Passwords do not match."
			},
			eusa:{
				required:"Please indicate your agreement with our Terms and Conditions."
			}
		},
		errorPlacement:function(error,element){
			if($(element).attr("id") != "domain")
			{
//				error.addClass("domainError");
//				element.after(error);
//			}else{
				element.after(error);
			}
		},
		submitHandler:function(form)
		{
			$("#submit").attr("disabled","true").addClass("disabled");
			
			// Submit the form via ajax
			$.ajax({
				url:window.location.href,
				type:"POST",
				data:$(form).serialize(),
				login:$("#login",form).val(),
				pass:$("#pass",form).val(),
				beforeSend: function(XMLHttpRequest)
				{
					var signupForm = $("#signupForm");
					signupForm.slideUp();
					var $waitMsg = $("<div>Please wait just a few seconds. We are creating your new Eden Platform website! <img src='"+ SETTINGS.skinRoot +"/img/ajax-loading-signup.gif' align='left' /></div>");
					signupForm.before($waitMsg);
				},
				success:function(data)
				{
					try{
						response = $(data);
						var newsite = $("#newsite",response); 
						if(newsite.length > 0)
						{
							// The new site was created successfully
							
							// Register the conversion with GA if possible
							if(window.pageTracker)
							{
								pageTracker._trackPageview(window.location.pathname+"/submitted");
								setTimeout("loginToNewSite('"+newsite.html()+"', '"+this.login+"', '"+this.pass+"')",5000);
							}else{
								loginToNewSite(newsite.html(), this.login, this.pass);
							}
							// Create a login form on the fly and submit it
//							loginForm = "" +
//									"<form id='quickLogin' action='"+newsite.html()+"/auth/index/login' method='post'>" +
//										"<input type='hidden' name='username' value='"+this.login+"' />" +
//										"<input type='hidden' name='password' value='"+this.pass+"' />" +
//									"</form>";
//							$("body").append(loginForm);
//							$("#quickLogin").submit();
						}else{
							// There was a problem creating the new site
							// TODO: re-init handlers?
							$("#signupForm").replaceWith($("#signupForm",response));
						}
					}catch(e)
					{
						
					}
				}
			});
		}
	});
});

function loginToNewSite(url, login, pass)
{
	// Create a login form on the fly and submit it
	loginForm = "" +
			"<form id='quickLogin' action='"+url+"/auth/index/login' method='post'>" +
				"<input type='hidden' name='username' value='"+login+"' />" +
				"<input type='hidden' name='password' value='"+pass+"' />" +
			"</form>";
	$("body").append(loginForm);
	$("#quickLogin").submit();	
}

function checkdomain(dosubmit)
{
	if(dosubmit == null) dosubmit = false;
	$("#working").show();
	$(this).attr("disabled","disabled");
	$("#domainStatusMsg").removeClass("good error").html("Checking sub-domain...");
	$.ajax({
		type:"post",
		data:{domain:$("#domain").val()},
		url:SETTINGS.webRoot+"/administration/signup/check-domain/format/json/",
		dosubmit:dosubmit,
		success:function(data)
		{
			try{
				data = JSON.parse(data);
				if(data.result)
				{
					$("#domainStatusMsg").addClass("good").removeClass("error").html("Sub-Domain available!");
					$("#domain").data("verified",true);
					if(this.dosubmit)
					{
						$("#signupForm").submit(function(){return true;}).submit();
					}
				}else{
					$("#domainStatusMsg").removeClass("good").addClass("error").html("Sub-Domain not available!");
					$("#domain").data("verified",false);
				}
			}catch(e){
				// There was an error getting the domain status - probably a permission problem
				$("#domain").data("verified",true);
				$("#domainStatusMsg").removeClass("good").removeClass("error").html("There was a problem checking the domain status.");				
			}

			$("#domain").removeAttr("disabled");
			$("#working").hide();
		}
	});
	return false;
}