Success = {'username' : 0,'password' : 0,'confirmpassword' : 0,'email' : 0,'realname' : 0,'phonenumber' : 0};

function ResetRegistration()
{
	if (window.wait == 1) return (false);
	for (key in Success)
	{
		Success[key] = 0;
		document.getElementById('wait_' + key).innerHTML = '';
		document.getElementById('error_' + key).innerHTML = '';
		document.getElementById('row_' + key).className = '';
		window.NextItem = '';
	}
	return (true);
}

function CheckTrue()
{
	var NumTrue = 0;
	for (key in Success)
	{
		NumTrue += Success[key];
	}
	return (NumTrue);
}

function SubmitRegistration()
{
	if (window.wait == 1) return (false);
	if(!document.getElementById('username').value) ShowError('username', phrase['username_must']);
	if(!document.getElementById('password').value) ShowError('password', phrase['password_must']);
	if(!document.getElementById('confirmpassword').value) ShowError('confirmpassword', phrase['confirmpassword_must']);	
	if(!document.getElementById('email').value) ShowError('email', phrase['email_must']);
	if(!document.getElementById('realname').value) ShowError('realname', phrase['fullname_must']);			
	if(!document.getElementById('phonenumber').value) ShowError('phonenumber', phrase['phonenumber_must']);				
	if (CheckTrue() < 6)
	{
		alert(phrase['check_fields']);
		return (false);
	}
		else
	{
		return (true);
	}
}

function ReloadRegistration()
{
	http = createXmlHttpRequestObject();
	for (key in Success)
	{
		if (document.getElementById(key).value != '') CheckRegistration(key);
	}
	document.getElementById('username').focus();
}

function ShowError(item, error)
{
	document.getElementById('wait_' + item).style.display = '';
	document.getElementById('wait_' + item).innerHTML = '<img src="styles/' + window.style + '/images/error.gif" /> ';
	document.getElementById('row_' + item).className = 'error';
	document.getElementById('error_' + item).innerHTML = error;
	Success[item] = 0;	
}

function handleCheckRegistration()
{
  if (http.readyState == 4)
  {  	
    if(http.status==200)
    {
      var Result = http.responseText;
	  if (!Result)
	  {
	  		document.getElementById('wait_' + window.CurrentItem).innerHTML = '<img src="styles/' + window.style + '/images/tick.gif" /> ';
	  		document.getElementById('row_' + window.CurrentItem).className = '';
	  		document.getElementById('error_' + window.CurrentItem).innerHTML = '';
	  		Success[window.CurrentItem] = 1;
	  }
	  		else
	  {
			document.getElementById('wait_' + window.CurrentItem).innerHTML = '<img src="styles/' + window.style + '/images/error.gif" /> ';
			document.getElementById('row_' + window.CurrentItem).className = 'error';
			document.getElementById('error_' + window.CurrentItem).innerHTML = Result;
			Success[window.CurrentItem] = 0;
	  }
	  window.wait = 0;
	  http = createXmlHttpRequestObject();
	  if (window.NextItem)
	  {
			if (window.NextItem.indexOf(',') != '-1')
			{
				Next = window.NextItem.indexOf(',');
				Current = window.NextItem.substr(0,Next);
				window.NextItem = window.NextItem.substr(Next+1);
				CheckRegistration(Current);
			}
				else
			{
				CheckRegistration(window.NextItem);
				window.NextItem = '';
			}
	  }
    }
  }
}

function CheckRegistration(item)
{
	if (window.wait != 1 || (item == 'password' || item == 'confirmpassword'))
	{
		document.getElementById('wait_' + item).innerHTML = '<img src="styles/' + window.style + '/images/loading.gif" /> ';
		document.getElementById('wait_' + item).style.display = '';
		if (item != 'password' && item != 'confirmpassword')
		{
			var CItem = item + "=" + encodeURI(document.getElementById(item).value);
			http.onreadystatechange = handleCheckRegistration;
			http.open("POST", 'register.php?ajax&method=check&item=' + escape(item), true);  	
		    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    http.setRequestHeader("Content-length", CItem.length);
		    http.setRequestHeader("Connection", "close");	
			http.send(CItem);
			window.CurrentItem = item;
			window.CurrentValue = document.getElementById(item).value;
			window.wait = 1;
		}
			else
		{
			switch(item)
			{
				case 'password':
					if (document.getElementById(item).value)
					{
						document.getElementById('wait_' + item).innerHTML = '<img src="styles/' + window.style + '/images/tick.gif" /> ';
						document.getElementById('row_' + item).className = '';
						document.getElementById('error_' + item).innerHTML = '';
						Success[item] = 1;
					}
						else
					{
						document.getElementById('wait_' + item).innerHTML = '<img src="styles/' + window.style + '/images/error.gif" /> ';
						document.getElementById('row_' + item).className = 'error';
						document.getElementById(item).focus();
						Success[item] = 0;
					}
					if (document.getElementById(item).value != document.getElementById('confirmpassword').value)
					{
						document.getElementById('wait_confirmpassword').innerHTML = '<img src="styles/' + window.style + '/images/error.gif" /> ';
						document.getElementById('row_confirmpassword').className = 'error';
						Success["confirmpassword"] = 0;
					}
						else
					{
						document.getElementById('wait_confirmpassword').innerHTML = '<img src="styles/' + window.style + '/images/tick.gif" /> ';
						document.getElementById('row_confirmpassword').className = '';
						document.getElementById('error_confirmpassword').innerHTML = '';
						Success["confirmpassword"] = 1;
					}				
				break;
				case 'confirmpassword':
					if (document.getElementById(item).value && document.getElementById(item).value == document.getElementById('password').value)
					{
						document.getElementById('wait_' + item).innerHTML = '<img src="styles/' + window.style + '/images/tick.gif" /> ';
						document.getElementById('row_' + item).className = '';
						document.getElementById('error_' + item).innerHTML = '';
						Success[item] = 1;
					}
						else
					{
						document.getElementById('wait_' + item).innerHTML = '<img src="styles/' + window.style + '/images/error.gif" /> ';
						document.getElementById('row_' + item).className = 'error';
						document.getElementById(item).focus();
						Success[item] = 0;
					}			
				break;
			}
		}
	}
		else
	{
		if (window.NextItem)
			window.NextItem = window.NextItem + ',' + item;
		else
			window.NextItem = item;
	}
}

var http = createXmlHttpRequestObject();

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// if running Internet Explorer
	if(window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	// if running Mozilla or other browsers
		else
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
	alert("Error creating the XMLHttpRequest object.");
	else
	return xmlHttp;
}
