function link_cancel(button_id, dialog_id) {
	document.getElementById(button_id).onclick = function() {
		mask = document.getElementById('transparent_mask');
		dialog = document.getElementById(dialog_id);

		mask.style.visibility = 'hidden';
		dialog.style.visibility = 'hidden';

		document.getElementById('transparent_mask').style.opacity = 0.0;
	};			
}

function link_action(button_id, func) {
	document.getElementById( button_id ).onclick = func;
}

function link_dialog(button_id, dialog_id) {
	document.getElementById(button_id).onclick = function() {
		var opac = 0.0;

		mask = document.getElementById('transparent_mask');
		dialog = document.getElementById(dialog_id);

		mask.style.top=document.body.scrollTop+'px';
		dialog.style.top=document.body.scrollTop+'px';

		mask.style.visibility = 'visible';
		dialog.style.visibility = 'visible';

		var dimmer_id = setInterval( function() {
			if( opac > 0.49 ) {
				clearInterval( dimmer_id );
			}

			opac = opac + 0.25;

			document.getElementById('transparent_mask').style.opacity = opac;

		}, 25);
	}				
}

function login() {
	var username = document.getElementById('login_dialog_username').value;
	var password = document.getElementById('login_dialog_password').value;

	var d = doSimpleXMLHttpRequest("http://www.kountr.com/simple/login/", {"password": password, "username": username});

	var logged_in = function (msg) {
		switch ( msg.responseText ) {
			case "Failed_Inactive":
				document.getElementById('login_help_msg').innerHTML = "This account has been inactivated please contact Michael if you believe this is your account.";							
				break;
			case "Failed_NoAccount":
				document.getElementById('login_help_msg').innerHTML = "This is not a valid username and password pair.";					
				break;
			case "Success":
				location.href = "http://www.kountr.com/simple/my_kounts/1/";
				break;
		}
	};
	var metadataFetchFailed = function (err) {
		alert(err);
	};
	d.addCallbacks(logged_in, metadataFetchFailed);

}

function join() {				
	var username = document.getElementById('join_dialog_username').value;
	var email = document.getElementById('join_dialog_email').value;

	var d = doSimpleXMLHttpRequest("http://www.kountr.com/simple/join/", {"email": email, "username": username});

	var joined = function (msg) {
		switch ( msg.responseText ) {
			case "Failed_Invalid_Username":
				document.getElementById('join_help_msg').innerHTML = "Your username may only contain the following characters 0-9, a-z, A-Z and '_'.<p/> Please enter another username.";
				break;
			case "Failed_Username_Taken":
				document.getElementById('join_help_msg').innerHTML = "Please try a different username, this one is already in use.";
				break;
			case "Failed_Inactive":
				document.getElementById('join_help_msg').innerHTML = "Please try a different username, this one is already in use.";
				location.href = "{{globals.kountr_url}}";
				break;
			case "Failed_NoAccount":				
				break;
			case "Success":
				location.href = "http://www.kountr.com/simple/my_kounts/1/";
				break;
		}
	};
	var metadataFetchFailed = function (err) {
		alert(err);
	};
	d.addCallbacks(joined, metadataFetchFailed);
}




function signout() {				
	var d = doSimpleXMLHttpRequest("http://www.kountr.com/simple/signout/", {});

	var joined = function (msg) {
		switch ( msg.responseText ) {
			case "Success":
				location.href = "http://www.kountr.com/";
				break;
		}
	};
	var metadataFetchFailed = function (err) {
		alert(err);
	};
	d.addCallbacks(joined, metadataFetchFailed);
}



function create_kount() {				
	var name = document.getElementById('create_dialog_name').value;
	var value = document.getElementById('create_dialog_value').value;
	
	var d = doSimpleXMLHttpRequest("http://www.kountr.com/create_kount/", {"name": name, "value": value});

	var created = function (msg) {
		switch ( msg.responseText ) {
			case "Success":
				location.href = "";
				break;
		}
	};
	var metadataFetchFailed = function (err) {
		alert(err);

		var txt = "";

		for( att in err ) {
			txt = txt + att + ' - ' + err[att] + '\n';
		}
		
		alert( txt );
	};
	d.addCallbacks(created, metadataFetchFailed);
}