globalTimerId = 0;

$(document).ready( function()
{
	

	dataRequest({currentView:'JSON',getGroups:true});

	//add acccount options
	
	
	$("form input[name='username']").change(function () {

		main = this;
		checkPost = "currentView=JSON&userName=" + $(this).val();
		$(this).parent().find('.alertSuccess').remove();
		$(this).parent().find('.alertFailure').remove();
		
		if($(this).val() != '')
			$.post("/func/userNameAvaliable", checkPost, function(j)
			{
				if(j.status == 1)
					$(main).after('<span class="fieldSpace"><span class="alertSuccess"></span></span>')
				else
					$(main).after('<span class="fieldSpace"><span class="alertFailure"></span></span>')
			
			},"json");

	});
	
	$(".signup .plusIcon").click(function () {
		addAccountSingle();
	});
	
	$(".signup form").submit(function() {
		addAccountSingle();
		return false;
	});

	
	
	
});


/******************
Add accounts
*******************/


function addAccountSingle()
{
	addPost = "currentView=JSON&" + $(".signup form").serialize();
	$.post("addAccount", addPost, function(j)
	{
		addAccountReturn(j)
	},"json");
}


function addAccountReturn (j)
{
	clearMessages($(".signup"));
	
	
	if (j.error.count > 0)
	{
		$(".signup .title").after(genError(j.error,'Sorry, we had errors while adding account...'));	
	}
	else if (j.success == true)
	{
		
		$(".signup .title").after(genSuccess('Your account has been created.<br/><br/>Please bookmark the login page for quick access.'));
	
		globalTimerId = setTimeout(
			function ()
			{
				clearTimeout(globalTimerId);
				window.location.href = '/signedup';
				
			}
		, 6000);
		
		
		$('.signup form').each(function(){
		        this.reset();
		});
		
		$('.signup form .alertSuccess').remove();
		$('.signup form .alertFailure').remove();
	}

}


function dataRequest(update)
{
	$.post('?', update, function(j)
	{
		dataRequestReturn(j)
	},"json");
}

function dataRequestReturn (j)
{	
	
	if (typeof j.groups != 'undefined')
	{
		displayGroups(j);
	}
}


var groupPosition = Array();
var groups = Array();
function displayGroups(j)
{
	groups = Array();
	var ID = "nodeGroup";
	
	$(".accountGroups").html(parseTemplate($("#accountGroupsTMPL").html(), {data:j.groups, selected:''} ));
	
	
}

