var req_fields = new Array ('email', 'username', 'passwd1', 'passwd2', 'firstname', 'name', 'location', 'shopname', 'address');

var errors = new Array();

errors[1] = '';
errors[2] = '';
errors[3] = '';
errors[4] = '';

function checkusername()
{
	var name = $('username').value;
	new Ajax.Request
	(
		install_path+'/login/checkname/',
		{
			parameters: 'checkname='+name,
			onComplete: function(t)
			{
				if(t.responseText == 'OK')
				{
					$('username').parentNode.style.color="";
					errors[1] = '';
				}
				else
				{
					$('username').parentNode.style.color="#ff0000";
					
					errors[1] = 'the user already exists';
				}
				show_erros();
			}
		}
	);
}

function checkemail()
{
	var email = $('email').value;
	new Ajax.Request
	(
		install_path+'/login/checkmail/',
		{
			parameters: 'checkmail='+email,
			onComplete: function(t)
			{
				if(t.responseText == 'OK')
				{
					$('email').parentNode.style.color="";
					errors[4] = '';
				}
				else
				{
					$('email').parentNode.style.color="#ff0000";
					
					errors[4] = 'the e-mail address is already in use or not valid';
				}
				show_erros();
			}
		}
	);
}


function show_erros()
{
	if(errors[1] != '' || errors[2] != '' || errors[3] != '' || errors[4] != '')
	{
		$('notice').style.display = 'block';
		$('notice').innerHTML = '<strong>The Following Errors Occoured</strong>:<br />';
		
		if(errors[1] != '')
			$('notice').innerHTML += '- ' + errors[1] + '<br />';
		
		if(errors[2] != '')
			$('notice').innerHTML += '- ' + errors[2] + '<br />';
			
		if(errors[3] != '')
			$('notice').innerHTML += '- ' + errors[3] + '<br />';
			
		if(errors[4] != '')
			$('notice').innerHTML += '- ' + errors[4] + '<br />';
	}
	else
	{
		$('notice').style.display = 'none';
	}
}

var myrules = {
	'#signinbutton' : function(el){
		
		el.onclick = function(){
			var error = false;
			
			errors[2] = '';
			for(var i=0; i<req_fields.length; i++)
			{
				if($(req_fields[i]).value == '')
				{
					errors[2] = 'fill in all required fields (marked with a *)';
					$(req_fields[i]).parentNode.style.color="#ff0000";
				}
				else
				{
					$(req_fields[i]).parentNode.style.color="";
				}
			}

			if($('passwd1').value != $('passwd2').value)
			{
				errors[3] = "the passwords don't match";
				$('passwd1').parentNode.style.color="#ff0000";
				$('passwd2').parentNode.style.color="#ff0000";
			}
			else
			{
				if($('passwd1').value != '')
				{
					$('passwd1').parentNode.style.color="";
					$('passwd2').parentNode.style.color="";
				}
				errors[3] = '';
			}
			
			if(errors[1] == '' && errors[2] == '' && errors[3] == '')
			{
				return true;	
			}
			else
			{
				show_erros();
				return false;
			}
			
		}
	},
	
	'#resetbutton' : function(el){
		el.onclick = function(){
			errors[1] = '';
			errors[2] = '';
			errors[3] = '';
			
			for(var i=0; i<req_fields.length; i++)
			{
				$(req_fields[i]).parentNode.style.color="";
			}
			
			$('notice').style.display = 'none';
		}
	},
	
	'#username' : function(el){
		el.onchange = function(){
			if(el.value != '')
				checkusername();
		}
	},

	'#email' : function(el){
		el.onchange = function(){
			if(el.value != '')
				checkemail();
		}
	}
};

Behaviour.register(myrules);