function getCookie(name)
{
	var theCookie = document.cookie;
	var thePrefix = name + "=";
	var theBegin = theCookie.indexOf("; " + thePrefix);
	
	if(theBegin == -1)
	{
		theBegin = theCookie.indexOf(thePrefix);
		if(theBegin != 0)
			return null;
	}
	else
	{
		theBegin += 2;
	}
	
	var theEnd = document.cookie.indexOf(";", theBegin);
	if(theEnd == -1)
	{
		theEnd = theCookie.length;
	}
	
	return unescape(theCookie.substring(theBegin + thePrefix.length, theEnd));
}

function setCookie(domain, name, value)
{
  var exp = new Date();
  var oneYearFromNow = exp.getTime() + (365 * 24 * 60 * 60 * 1000);
  exp.setTime(oneYearFromNow);
  
  document.cookie = name + "=" + escape(value) + ";path=/;domain=" + domain + ";expires=" + exp.toGMTString();
}

function deleteCookie(domain, name)
{
	var kill_date = new Date("January 1, 1970");
	
	document.cookie = name + "=stub;path=/;domain=" + domain + ";expires=" + kill_date.toGMTString();
}

function loadLogin(form, domain, username, password, ssl)
{
	var theForm = null;
	var theCookieVal;

	if(document.getElementById && form != "")
	{ 
		theForm = document.getElementById(form);
	}
	else if(document.forms[0])
	{
		theForm = document.forms[0];
	}

	if(theForm)
	{
		if(username != "")
		{
			theCookieVal = getCookie(username);
			
			if(theCookieVal == null)
				 document.getElementById(username).value = "";
			else
				 document.getElementById(username).value = theCookieVal;
		}
		
		if(password != "")
		{
			theCookieVal = getCookie(password);
			
			if(theCookieVal == null)
				 document.getElementById(password).value = "";
			else
				 document.getElementById(password).value = theCookieVal;
		}
		
		if(ssl != "")
		{
			theCookieVal = getCookie(ssl);
			
			if(theCookieVal == null)
				 document.getElementById(ssl).checked = false;
			else
			{
				if(theCookieVal == "")
					 document.getElementById(ssl).checked = false;
				else
					 document.getElementById(ssl).checked = true;
			}
		}
	}
}

function saveLogin(form, domain, username, password, ssl, save)
{
	var theForm = null;
	var theCookieVal;

	if(document.getElementById && form != "")
	{ 
		theForm = document.getElementById(form);
	}
	else if(document.forms[0])
	{
		theForm = document.forms[0];
	}

	if(theForm && save != 0)
	{
		if(save & 1)
		{
			if(username != "")
				setCookie(domain, username,  document.getElementById(username).value);
			else
				setCookie(domain, username, "");
		}

		if(save & 2)
		{
			if(password != "")
				setCookie(domain, password,  document.getElementById(password).value);
			else
				setCookie(domain, password, "");
		}
		
		if(save & 4)
		{
			if(ssl != "")
			{
				if(document.getElementById(ssl).checked == true)
					setCookie(domain, ssl, "true");
				else
					setCookie(domain, ssl, "");
			}
		}
		
		setCookie(domain, "SaveLogin", "true");
	}
	else if(save == 0)
	{
		deleteCookie(domain, username);
		deleteCookie(domain, password);
		deleteCookie(domain, ssl);

		deleteCookie(domain, "SaveLogin");
	}
}

function focusLogin(form, username, password, button)
{
	var theForm = null;
	if(document.getElementById && form != "")
	{ 
		theForm = document.getElementById(form);
	}
	else if(document.forms[0])
	{
		theForm = document.forms[0];
	}
	
	if(theForm)
	{
		if(document.getElementById(username).value == null ||  document.getElementById(username).value == "")
		{ 
			document.getElementById(username).focus();
		}
		else if(document.getElementById(password).value == null ||	document.getElementById(password).value == "")
		{
			document.getElementById(password).focus();
		}
		else
		{
			document.getElementById(button).focus();
		}
	}
}

function doLogin(form, ssl)
{
	var theForm = null;
	var theHTTP;
	var theURLRight;
	
	if(document.getElementById && form != "")
	{ 
		theForm = document.getElementById(form);
	}
	else if(document.forms[0])
	{
		theForm = document.forms[0];
	}
	
	if(theForm)
	{
		if(ssl != "")
		{
			if(document.getElementById(ssl).checked == true)
				theHTTP = "https";
			else
				theHTTP = "http";
		}
		else
			theHTTP = "http";
	}
	
	theURLRight = theForm.action.substring(theForm.action.indexOf(":"));
	theForm.action = theHTTP + theURLRight;
	
}

