// squirrelmail remote login script
// brett d, 12/05/07

// remove spaces from any string
function removeSpaces(string)
{
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

// pass all the info to squirrelmail login!
function processLogin(account,domain,pass)
{
	var form = document.createElement("form");
	form.action = "https://webmail." + domain + "/src/redirect.php";
	form.method = "POST";
	var af = function(n, v) {
		var f = document.createElement("input");
		f.type = "hidden"; f.name = n; f.value = v;
		form.appendChild(f);
	};
	af("login_username", account);
	af("secretkey", pass);
	af("js_autodetect_results", "0");
	af("just_logged_in", "1");
	document.getElementById("webmaillogin").appendChild(form);
	form.submit(); 
}

function webmail()
{

	// get the values from the form
	var wmAddress = document.forms['webmaillogin'].webmailusername.value;
	var wmPassword = document.forms['webmaillogin'].webmailpassword.value;
	
	// delete any spaces
	wmAddress = removeSpaces(wmAddress);
	wmPassword = removeSpaces(wmPassword);
	
	// hey user!  there are no spaces in addresses or passwords!
	document.forms['webmaillogin'].webmailusername.value = wmAddress;
	document.forms['webmaillogin'].webmailpassword.value = wmPassword;
	
	// filter for valid email
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	if (filter.test(wmAddress))
	// email's valid
		{
			if (wmPassword != "")
			// and password's not blank!
				{
					var wmAddressArray = wmAddress.split("@");
					processLogin(wmAddressArray[0],wmAddressArray[1],wmPassword);
				}
			else
				{
				alert("Please enter your password.")
				}
		}
	// email's invalid
		else
			{
				alert("Please enter a valid email address.")
			}
return false;
}

