var xmlHttp = createXmlHttpObject();

function createXmlHttpObject()
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
		catch(e) { }
	}
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

function changepassword()
{
	if (document.getElementById('username').className == 'error' || document.getElementById('curpass').className == 'error' || document.getElementById('newpass').className == 'error' || document.getElementById('confpass').className == 'error')
	{
		return;
	}

	account = document.getElementById('account').value;
	curpassword = document.getElementById('curpassword').value;
	newpassword = document.getElementById('newpassword').value;

	var data = "action=submit&username=" + account + "&curpassword=" + curpassword + "&newpassword=" + newpassword;

	if (xmlHttp)
	{
		try
		{ 
			xmlHttp.open("POST", "/proc/password.php", true);
			xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlHttp.onreadystatechange = handleSubmit;
			xmlHttp.send(data);
		}
		catch(e)
		{
			alert("Can't connect to server: " + e.toString());
		}
	}
}

function handleSubmit()
{
	if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			try
			{
				handleSubmitResponse();
			}
			catch(e)
			{
				alert("Error reading the response: " + e.toString());
			}
		}
		else
		{
			alert("There was a problem retrieving the data: " + xmlHttp.statusText);
		}
	}
}

function handleSubmitResponse()
{
	response = xmlHttp.responseText;

	target = document.getElementById("passwordform");
	if (response == 1)
	{
		errortarget = document.getElementById('formerror');
		errortarget.innerHTML = '<p style=\'color:#B81137;\'>Please fill out all fields.</p>';
		return;
	}
	if (response == 2)
	{
		errortarget = document.getElementById('formerror');
		errortarget.innerHTML = '<p style=\'color:#B81137;\'>We could not locate your account. Please contact technical support.</p>';
		return;
	}
	if (response == 3)
	{
		errortarget = document.getElementById('formerror');
		errortarget.innerHTML = '<p style=\'color:#B81137;\'>Your Password cannot contain your Username.</p>';
		return;
	}
	if (response == 4)
	{
		errortarget = document.getElementById('formerror');
		errortarget.innerHTML = '<p style=\'color:#B81137;\'>Your Password must be at least 6 characters long.</p>';
		return;
	}
	if (response == 5)
	{
		errortarget = document.getElementById('formerror');
		errortarget.innerHTML = '<p style=\'color:#B81137;\'>Your Password must contain at least one number.</p>';
		return;
	}
	if (response == 6)
	{
		errortarget = document.getElementById('formerror');
		errortarget.innerHTML = '<p style=\'color:#B81137;\'>Your Password must contain at least one letter.</p>';
		return;
	}
	target.innerHTML = response;
}



