var xmlHttp = createXmlHttpObject();

function platlogin()
{
	if (document.getElementById("platusername").value == '')
	{
		document.getElementById('username').className = 'error';
	} 
	else
	{
		var username = document.getElementById("platusername").value;
	}	

	if (document.getElementById("platpassword").value == '')
	{
		document.getElementById('password').className = 'error';
	} 
	else
	{
		var password = document.getElementById("platpassword").value;
	}	

	if (typeof(username) != "undefined" && typeof(password) != "undefined")
	{

		var data = "action=platlogin&username=" + username + "&password=" + password;
		if (xmlHttp)
		{
			try
			{ 
				document.body.style.cursor = "wait";
				xmlHttp.open("POST", "/proc/platypus.php", true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttp.onreadystatechange = handleLogin;
				xmlHttp.send(data);
			}
			catch(e)
			{
				alert("Failed to connect to server: " + e.toString());
			}
		}
	}
}


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

function handleLoginResponse()
{
	document.body.style.cursor = "default";
	response = xmlHttp.responseText;

	if (response == 1)
	{
		target = document.getElementById("platloginformerror");
		target.innerHTML = "<br><p style='color:#B81137;'>Login incorrect.</p>";
	} 
	else
	{
		target = document.getElementById("platloginformerror");
		target.innerHTML = "<p></p>";
		target = document.getElementById("platloginform");
		target.innerHTML = response;
	}
}

function payments()
{
	var sid = document.getElementById("sid").value;


	if (typeof(sid) != "undefined")
	{

		var data = "action=showpayments&sid=" + sid;		
		if (xmlHttp)
		{
			try
			{ 
				document.body.style.cursor = "wait";
				xmlHttp.open("POST", "/proc/platypus.php", true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttp.onreadystatechange = handleGeneric;
				xmlHttp.send(data);
			}
			catch(e)
			{
				alert("Failed to connect to server: " + e.toString());
			}
		}
	}
}

function invoices()
{
	var sid = document.getElementById("sid").value;

	if (typeof(sid) != "undefined")
	{

		var data = "action=showinvoices&sid=" + sid;
		if (xmlHttp)
		{
			try
			{ 
				document.body.style.cursor = "wait";
				xmlHttp.open("POST", "/proc/platypus.php", true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttp.onreadystatechange = handleGeneric;
				xmlHttp.send(data);
				
			}
			catch(e)
			{
				alert("Failed to connect to server: " + e.toString());
			}
		}
	}
}

function account()
{
	var sid = document.getElementById("sid").value;


	if (typeof(sid) != "undefined")
	{

		var data = "action=accountinfo&sid=" + sid;
		if (xmlHttp)
		{
			try
			{ 
				document.body.style.cursor = "wait";
				xmlHttp.open("POST", "/proc/platypus.php", true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttp.onreadystatechange = handleGeneric;
				xmlHttp.send(data);
				this.className += 'active';
			}
			catch(e)
			{
				alert("Failed to connect to server: " + e.toString());
			}
		}
	}
}


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

function handleGenericResponse()
{
	document.body.style.cursor = "default";
	response = xmlHttp.responseText;

	if (response == 1)
	{
		target = document.getElementById("platloginform");
		target.innerHTML = "<br><p style='color:#B81137;'>Server Error. Please email contact technical support.</p>";
	} 
	else
	{


		target = document.getElementById("platloginform");
		target.innerHTML = response;
	}
}




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;
}
