var xmlHttp = createXmlHttpObject();

function manageEmailLogin()
{
	var data = "";
	var returnvar = 0;

	if (document.getElementById("username").value == '')
	{
		document.getElementById('usernameleft').className = 'error';
		returnvar = 1;
	} 
	else
	{
		data = "username=" + document.getElementById("username").value + "&";
	}	

	if (document.getElementById("password").value == '')
	{
		document.getElementById('passwordleft').className = 'error';
		returnvar = 1;
	} 
	else
	{
		data = data + "password=" + encodeURIComponent(document.getElementById("password").value) + "&";
	}	

	data = data + "action=login";

	if (returnvar == 0)
	{

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


function displayMailboxSettings(mailbox)
{
	if (mailbox == '')
	{
		return;
	}

	data = "pid=" + document.getElementById("pid").value + "&mailbox=" + mailbox + "&action=displaymailboxsettings";

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

function displayEmailAccounts()
{
	data = "pid=" + document.getElementById("pid").value + "&action=displayaccounts";

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


function displayEmailAliases()
{
	data = "pid=" + document.getElementById("pid").value + "&action=displayaliases";

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

function displayAutoResponse()
{
	data = "pid=" + document.getElementById("pid").value + "&action=displayautoresponse";

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

function disableAutoResponse()
{
	data = "pid=" + document.getElementById("pid").value + "&action=disableautoresponse";

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

function displaySpamSettings()
{
	data = "pid=" + document.getElementById("pid").value + "&action=displayspamsettings";

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

function displayBlacklist()
{
	data = "pid=" + document.getElementById("pid").value + "&action=displayblacklist";

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

function setFiltering(policy)
{
	if (policy == '')
	{
		return;
	}
	if (policy == 'select')
	{
		level = document.manageemail.Filterlevel.value;
		action = document.manageemail.Action.value;
		if (level == 'Normal')
		{
			policy = 1;
		}
		if (level == 'Conservative')
		{
			policy = 3;
		}
		if (level == 'Aggressive')
		{
			policy = 2;
		}
		if (level == 'Militant')
		{
			policy = 4;
		}
		if (action == 'reject')
		{
			policy += 6;
		}
	}

	data = "pid=" + document.getElementById("pid").value + "&policy=" + policy + "&action=setfiltering";

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

function removeAlias(alias)
{
	if (alias == '')
	{
		return;
	}
	data = "pid=" + document.getElementById("pid").value + "&alias=" + alias + "&action=removealias";

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

function updateAlias(alias)
{
	if (alias == '')
	{
		return;
	}

	newrcpt = document.getElementById(alias).value;

	data = "pid=" + document.getElementById("pid").value + "&alias=" + alias + "&newrcpt=" + newrcpt + "&action=updatealias";

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

function removeWhitelist(whitelistdeletion)
{
	if (whitelistdeletion == '')
	{
		return;
	}
	data = "pid=" + document.getElementById("pid").value + "&whitelistdeletion=" + whitelistdeletion + "&action=removewhitelist";

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

function removeBlacklist(blacklistdeletion)
{
	if (blacklistdeletion == '')
	{
		return;
	}
	data = "pid=" + document.getElementById("pid").value + "&blacklistdeletion=" + blacklistdeletion + "&action=removeblacklist";
	
	if (xmlHttp)
	{
		try
		{ 
			document.body.style.cursor = "wait";
			xmlHttp.open("POST", "/proc/manageemail.php", true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttp.onreadystatechange = handleManageEmailGenResponse;
			xmlHttp.send(data);
		}
		catch(e)
		{
			alert("Failed to connect to server: " + e.toString());
		}
	}
}

function deleteAccount(account)
{
	if (account == '')
	{
		return;
	}

	data = "pid=" + document.getElementById("pid").value + "&account=" + account + "&action=deleteaccount";

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

function updatePassword()
{

	if (document.getElementById("newpassword").value == '')
	{
		document.getElementById('newpasswordleft').className = 'error';
		return;
	} 
	if (document.getElementById("newpassconf").value == '')
	{
		document.getElementById('newpassconfleft').className = 'error';
		return;
	} 
	data = "pid=" + document.getElementById("pid").value + "&newpassword=" + encodeURIComponent(document.getElementById("newpassword").value);
	data = data + "&newpassconf=" + encodeURIComponent(document.getElementById("newpassconf").value) + "&action=updatepassword";

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

function whitelistAddress()
{

	if (document.getElementById("whitelistemail").value == '')
	{
		return;
	} 

	data = "pid=" + document.getElementById("pid").value + "&whitelistemail=";
	data = data + document.getElementById("whitelistemail").value + "&action=whitelistaddress";

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

function blacklistAddress()
{

	if (document.getElementById("blacklistemail").value == '')
	{
		return;
	} 

	data = "pid=" + document.getElementById("pid").value + "&blacklistemail=";
	data = data + document.getElementById("blacklistemail").value + "&action=blacklistaddress";

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

function enableAutoResponse()
{

	if (document.getElementById("subject").value == '')
	{
		document.getElementById('subjectleft').className = 'error';
		return;
	} 
	if (document.getElementById("message").value == '')
	{
		document.getElementById('messageleft').className = 'error';
		return;
	} 
	data = "pid=" + document.getElementById("pid").value + "&subject=" + document.getElementById("subject").value;
	data = data + "&message=" + document.getElementById("message").value + "&action=enableautoresponse";

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

function forwardMail()
{

	if (document.getElementById("forwardaddress").value == '')
	{
		document.getElementById('forwardaddressleft').className = 'error';
		return;
	} 
	data = "pid=" + document.getElementById("pid").value + "&forwardaddress=" + document.getElementById("forwardaddress").value + "&action=forwardaddress";

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

function removeForward()
{

	data = "pid=" + document.getElementById("pid").value + "&action=removeforward";

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

function createNewAlias()
{

	if (document.getElementById("newaliasusername").value == '')
	{
		document.getElementById('newusernameleft').className = 'error';
		return;
	} 

	if (document.getElementById("newrcpt").value == '')
	{
		return;
	} 

	data = "pid=" + document.getElementById("pid").value + "&newaliasusername=" + document.getElementById("newaliasusername").value;
	data = data + "&newaliasdomain=" + document.manageemail.newaliasdomain.value;
	data = data + "&newrcpt=" + document.getElementById("newrcpt").value + "&action=createnewalias";

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

function createNewAccount()
{

	if (document.getElementById("newusername").value == '')
	{
		document.getElementById('newusernameleft').className = 'error';
		return;
	} 
	else
	{
		data = "pid=" + document.getElementById("pid").value + "&newusername=" + document.getElementById("newusername").value;
	}
	if (document.getElementById("newpassword").value == '')
	{
		document.getElementById('newpasswordleft').className = 'error';
		return;
	} 
	else
	{
		data = data + "&newpassword=" + encodeURIComponent(document.getElementById("newpassword").value);
	}


	data = data + "&domain=" + document.manageemail.domain.value + "&action=createnewaccount";

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

function linkEmailAccount()
{

	if (document.getElementById("linkemail").value == '')
	{
		document.getElementById('linkemailleft').className = 'error';
		return;
	} 
	else
	{
		data = "pid=" + document.getElementById("pid").value + "&linkemail=" + document.getElementById("linkemail").value;
	}
	if (document.getElementById("linkpassword").value == '')
	{
		document.getElementById('linkpasswordleft').className = 'error';
		return;
	} 
	else
	{
		data = data + "&linkpassword=" + encodeURIComponent(document.getElementById("linkpassword").value) + "&action=linkemailaccount";
	}

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

function displayCreateNewAlias()
{
	data = "pid=" + document.getElementById("pid").value + "&action=displaycreatenewalias";

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

function displayCreateNewEmailAccount()
{
	data = "pid=" + document.getElementById("pid").value + "&action=displaycreatenewaccount";

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

function displayLinkEmailAccount()
{
	data = "pid=" + document.getElementById("pid").value + "&action=displaylinkemailaccount";

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

function logout()
{
	data = "pid=" + document.getElementById("pid").value + "&action=logout";

	target2 = document.getElementById("loggedin");
	target2.innerHTML = "";


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


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

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

function displayManageEmailLoginResponse()
{
	document.body.style.cursor = "default";
	response = xmlHttp.responseText;
	if (response == 1) 
	{
                target = document.getElementById("manageemailerror");
                target.innerHTML = "<br><p style='color:#B81137;'>Login incorrect.</p>";		
	}
	else
	{
		username = document.getElementById("username").value;
		output = '<div class="left">You are logged in as<br><strong>' + username + '</strong></div><div class="right">';
		output = output + '<a href="#" class="buttonsm" onclick="logout()"><span>Logout</span></a></div>';
		ourput = output + '<div class="clear"></div>';

		target2 = document.getElementById("loggedin");
		target2.innerHTML = output;

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

function displayManageEmailGenResponse()
{
	document.body.style.cursor = "default";
	response = xmlHttp.responseText;
	target = document.getElementById("maincontent");

	if (response == 1) 
	{
                target.innerHTML = "<br><p style='color:#B81137;'>There has been an unknown error.</p>";		
	}
	else
	{	
		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;
}



