// JAVASCRIPT FUNCTIONS FOR LOGIN

var http = createAjaxObject();

function createAjaxObject()
{
	var xmlhttp;

	try
	{
		xmlhttp = new ActiveXObject("MSXML2.XMLHTTP")
	}
	catch(e)
	{
		try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") }
		catch(f) { xmlhttp = null; }
	}

	if (!xmlhttp&&typeof XMLHttpRequest != "undefined")
	{
		xmlhttp = new XMLHttpRequest();
	}

	return xmlhttp;

}

function sendRequestTextGet(opType,xuser,xpass) {

	var args = sendRequestTextGet.arguments;

	switch (args.length)
	{
		case 1:
		{
			opType = args[0];
		}
		case 3:
		{
			opType = args[0];
			xuser = args[1];
			xpass = args[2];
			break;
		}
		default:
		{
			break;
		}
	}

	try{
		if(opType == 'login')
		{
			filename = "/login_ajax.asp?tuser=" + xuser + "&tpass=" + xpass;
		}
		else
		{
			filename = "/logout_ajax.asp";
		}

		http.open('GET', filename,true);
		http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
		http.setRequestHeader('Content-Type','text/plain'); // 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponseText;
		http.send(null);
	}
	catch(e)
	{
		alert("An error has occurred with the login procedure\n"+e.message);
	}
	finally{}

}


function handleResponseText() {

	try{
		if((http.readyState == 4) && (http.status == 200))
		{
			var response = http.responseText;
			var a = response.split("@");

			document.getElementById("LT_1").innerHTML = a[0];

			//alert(document.getElementById("LoginTool").innerHTML);

			if (window.location.toString().indexOf('stat') > 0 || window.location.toString().indexOf('sustainability') > 0)
				window.location.reload(false);

		}
		else
		{
			//alert("Failed " + http.status);
		}
    }
	catch(e)
	{
		alert("An error occurred in the login sub-system\n\n"+e.number+' '+e.message);

	}
	finally{
		//drawLoginRequest();
	}
}

function drawLoginRequest()
{
		try
		{
			filename = "/drawLoginTool_ajax.asp";

			http.open('GET', filename,true);
			//http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
			http.setRequestHeader('Content-Type','text/html'); //application/x-www-form-urlencoded');
			http.onreadystatechange = drawLoginResponse;
			http.send(null);
		}
		catch(e)
		{
		}

}

function drawLoginResponse()
{
	var login;

		try{
			if((http.readyState == 4) && (http.status == 200))
			{
				var response = http.responseText;

				//alert(response);
				login = document.getElementById("LT_1");
				login.innerHTML = response;

				//alert(login);
				//document.getElementById("LoginTool").innerHTML = response;

				response = null;
			}
			else
			{  }
	    }
		catch(e)
		{
			//alert("An error occurred while creating the login sub-system\n\n"+e.number+" "+e.message);

		}

}

function checkForm(a,u,p) {

	var submit = true;

	if (a == "Login")
	{
		if (u.value == "" || p.value == "")
		{
			alert ("You must enter a username and password to log in to the system");
			if (u.value == "") { u.style.backgroundColor = 'red'; }
			if (p.value == "") { p.style.backgroundColor = 'red'; }

			if (p.value == "") { p.focus(); }
			if (u.value == "") { u.focus(); }

			submit = false;
		}
	}
	else
	{
		//Run logout procedure
		sendRequestTextGet('logout');

		submit = false;
	}

	SubmitForm(submit,u,p)

	return submit;
}


function SubmitForm(submitForm,u,p)
{
	try
	{
		if (submitForm)
			sendRequestTextGet('login',u.value,p.value);
	}
	catch (e)
	{
		alert(e.number + " " + e.message);
	}
}


function CheckLoggedIn(session_userid,txtName)
{

	var fb = document.getElementById("bLogin");

		if (session_userid != 1)
		{
			document.getElementById("bName").innerHTML = "Welcome " + txtName
			fb.value = "Logout"
		}
		else
		{
			document.getElementById("bName").innerHTML = ""
			fb.value = "Login"
		}
}

function hideLogin(bolHide)
{
	//Currently unused

	if (bolHide)
	{
		document.getElementById("??").style.display = 'none';
	}
	else
	{
		document.getElementById("??").style.display = 'block';
		//Let the ASP page refresh handle it.
	}
}

			function checkKeyPress(e)
			{
				var keyID = (window.event) ? e.keyCode : e.which;

				if (keyID == 13)
				{
					checkForm(document.getElementById("bLogin").value,document.frmLogin.tUser,document.frmLogin.tPass);
				}

			}