var profileButton = null;

function setModalTopLeft() {
	// Bug with the version of the modal we're using
	// Have to manually set the top left of the dialog
	var loginform = $('#loginform');
	var h = ($(window).height()-loginform.height())/2;
	var w = ($(window).width()-loginform.width())/2;
	if (h > 200) h = 200;
	$('#modalContainer').css("top", h);
	$('#modalContainer').css("left", w);
}

function acceptAgentClicked() {
	if ($('input[name=acceptagent]').is(':checked')) {
		if (user != '') {
			claimAgent(user, agentId, claimResponse);
		} else {
			$("#continueChoices").show();
			$("#ignoreMe").hide();
		}
	} else {
		$("#continueChoices").hide();
		$("#ignoreMe").show();
	}		
}

function submitReview(e) {
	if (user != '') {
		window.location = "/agentReview/" + agentId + "/";
		return;
	}
	e.preventDefault();
	$('#createLink').click(createAccountThenReview);
	$('#signinLink').click(signinThenReview);
	$('#loginform').modal();
	$("#continueChoices").show();
	$("#reviewCallout").show();
	setModalTopLeft();
	if (user == '') {
		$("#logininfo").css("display", "inline");
	}
}

function claimProfile(e) {
	e.preventDefault();
	$('#createLink').click(createAccountThenClaim);
	$('#signinLink').click(signinThenClaim);
	$('#loginform').modal();
	$('#claimdiv').show();
	$("#reviewCallout").hide();
	
	setModalTopLeft();
	if (user == '') {
		$("#logininfo").css("display", "inline");
	}
}

function createAccountThenClaim() {
	window.location = "/signup/?claimid=" + agentId;
}

function createAccountThenReview() {
	window.location = "/signup/?reviewid=" + agentId;
}

function signinThenClaim() {
	window.location = "/signin?claimid=" + agentId;
}

function signinThenReview() {
	window.location = "/signin?reviewid=" + agentId;
}

/*
function submitClaim() {
	var checkbox = document.getElementById("acceptagent");
	if (!checkbox.checked) {
		return;
	}
	if (user != '') {
		claimAgent(user, agentId, claimResponse);
	} else {
		var pw = $("#password").val();
		var username = $("#screenname").val();

		if (pw == '') return;
		if (username == '') return;
		var params = "password=" + pw + "&username=" + username;
		loginAjax = new HZAjax("/phpHelpers/sessions/loginAjax.php", function() {
				if (loginAjax.checkReadyState()) {
					claimAgent(username, agentId, function() {
							window.location = "/profile/";
						});
				}
		});
		loginAjax.setRequestType("post");
		loginAjax.setParams(params);
		loginAjax.send();
	}
} */

function claimResponse() {
	if (claimAjax.checkReadyState()) {
		window.location = "/profile/";
	}
}

function claimAgent(user, agentId, responseFunction) {
	var params = "?id=" + agentId + "&user=" + user;

	claimAjax = new HZAjax("/phpHelpers/util/claimAgentAjax.php" + params, responseFunction);
	claimAjax.send(); 
}

function editProfile() {
	window.location = "/profile/";
}

function updateAgentEmail(aid) {

	var reEmail = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	var reEmpty = /^\s*$/;

	var emailID = "#email" + aid;
	var rowID = "#row" + aid;
	var email = $(emailID).val();

	email = email.replace(/\s*$/, "");
	email = email.replace(/^\s*/, "");

	if (reEmpty.test(email)) {
		alert("No email entered.");
	}
	else if (!reEmail.test(email)) {
		alert("Invalid E-mail address: "+email+".");
	}
	else {
		$(rowID).hide();
		agentAjax = new HZAjax('/phpHelpers/util/updateAgentEmailAjax.php?id='+aid+'&email='+email, function () {});
		agentAjax.send();
	}
}

