// JavaScript Document

var logonURL = window.location.href; //get the initial path to this file
//then drop the index.htm and everything after it from the end of path
path = logonURL.substring(0, logonURL.lastIndexOf("/") + 1);

/**/	var INBAdminServletURL	= path + "INBAdminServlet";	//************
/** /	var INBAdminServletURL	= path + "NetBenchServer";	//************ For the URL displayed */
/**** THIS WILL BE SEEN IN THE URL BY THE USER    ***********************/

document.write("<body onLoad=\"self.status=\'Welcome to Net-Bench\'; logonForm.USERID.focus()\">");
//specifically to give the USERID focus if browser is not netscape

var imageSource = "LogonEnabled.gif";
var imgMousedOver = false;

var logonFailed = 
(document.cookie.charAt(0) == "F") //for FAILED, S (or null) for SUCCESS
				  ? true : false;
//if this is the first attempt to log in
//and there was no previous failed attempt
//then logonFailed = false

//******************************************************************
//The Javascript functions
//Handle the functionality once submit has been pressed
function submitted(form)
{
	var name = form.USERID.value;
	var pass = form.PASSWORD.value;

	//Check to make sure the form inputs are valid
	if ( !ValidInputs(form))
	{	
		//do nothing if the form inputs are invalid
		return false;
		//ValidInputs() takes care of alerts and setting focus if the inputs are invalid
	}
	
	//else name and password can be validated by the servlet
	form.submit();
}

//a check to make sure both UserID and Password have been entered and all characters are valid
function ValidInputs(form)
{
	var name = form.USERID.value;
	var pass = form.PASSWORD.value;
	if (name == "")
	{
		alert("Please enter a user ID.");
		form.USERID.focus();
		return false;
	}
	if (pass == "")
	{
		alert("Please enter a password.");
		form.PASSWORD.focus();
		return false;
	}
	if( checkBadChars(name))
	{
		alert("Please enter a VALID user id.");
		form.USERID.focus();
		return false;
	}
	if( checkBadChars(pass))
	{
		alert("Please enter a VALID password.");
		form.PASSWORD.focus();
		return false;
	}
	return true;
}


//check to see if the string contains bad characters
function checkBadChars(string)
//returns true if string contains illegal password and username values
//such as blanks, quotes and equal signs, as well as ~ and `
{
	for(var i = 0; i <string.length; i++)
	{
		var c = string.charAt(i);
		if( (c == ' ')	|| (c == '\n') || (c == '\t') || //whitespace
			(c == '\'') || (c == '\"') ||				 //quotes
			(c == '=')	||								 //equals sign
			(c == '~')	||								 // ~
			(c == '`') )								 // `
		   return true;
	}
	return false;
}

function imageHandler(form, isMouseOver)
{
	imgMousedOver = isMouseOver;
	setLogonImage(form);
}

function setLogonImage(form)
{
	if (navigator.appName!="Netscape" && event.keyCode == 13) //if enter key was pressed
	{						 //to allow the client to press enter to submit
		submitted(form);
		return;
	}
	if(form.USERID.value == "" || form.PASSWORD.value == "") //if either form is blank
		imageSource = "LogonDisabled.gif";
	else
	{
		if(imgMousedOver) //both forms are filled and the mouse is over the submit button
		{
			self.status = "Click to log in to NetBench.";
			imageSource = "LogonMouseOver.gif";
		}
		else //both forms are filled by the mouse is not over the submit button
		{
			self.status = "";
			imageSource = "LogonEnabled.gif";
		}
	}
	document.logonImage.src = imageSource;
}

function handleKeyPress(e, form)
{
//	if (!e) e = window.event;

	if (e && e.keyCode == 13)
	{
		submitted(form);
	}
}
//******************************************************************
document.writeln("<form name=\"logonForm\" action=\"" + INBAdminServletURL + "\" method=\"post\">");

document.writeln("	<table width=\"100%\" border=\"0\" cellpadding=\"0\"  cellspacing=\"5\" bgcolor=\"#393127\" id=\"Table2\">");
document.writeln("		<tr height=\"25\">");
document.writeln("			<td align=\"center\">");
document.writeln("				<strong>");
document.writeln("					<font color=\"#FFFFFF\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">SPINS NetBench Login</font>");
document.writeln("				</strong>");
document.writeln("			</td>");
document.writeln("		</tr>");
document.writeln("		<tr>");
document.writeln("			<td>");
document.writeln("				<table width=\"100%\" border=\"0\" cellpadding=\"0\"  cellspacing=\"5\" bgcolor=\"#FFFFFF\" id=\"Table2\">");
document.writeln("					<tr height=\"25\">");
document.writeln("						<td valign=\"bottom\" align=\"left\" colspan=\"2\">");
document.writeln("							<strong>");
document.writeln("								<em>&nbsp;");
													if (logonFailed) document.write("<font color=red>Logon Failed</font>");
document.writeln("								</em>");
document.writeln("							</strong>");
document.writeln("						</td>");
document.writeln("					</tr>");
document.writeln("					<tr height=\"25\">");
document.writeln("						<td align=\"right\">");
document.writeln("							<strong>User ID: </strong>");
document.writeln("						</td>");
document.writeln("						<td align=\"left\">");
document.writeln("							<input type=text NAME=\"USERID\" value=\"\" SIZE=17 onKeyUp=\"setLogonImage(logonForm)\">");
document.writeln("						</td>");
document.writeln("					</tr>");
document.writeln("					<tr height=\"25\">");
document.writeln("						<td align=\"right\">");
document.writeln("							<strong>Password: </strong>");
document.writeln("						</td>");
document.writeln("						<td align=\"left\">");
document.writeln("							<input type=password NAME=\"PASSWORD\" value=\"\" size=17 onKeyUp=\"setLogonImage(logonForm)\" onkeypress=\"handleKeyPress(event, logonForm)\">");
document.writeln("						</td>");
document.writeln("					</tr>");
document.writeln("					<tr height=\"25\">");
document.writeln("						<td align=\"center\" colspan=\"2\">");
document.writeln("							<img name=\"logonImage\" alt=\"Logon Button\" onMouseOver=\"imageHandler(logonForm, true)\" onMouseOut=\"imageHandler(logonForm, false)\" onClick=\"submitted(logonForm)\" width=\"95\" height=\"22\" src=\"" + imageSource + "\" border=\"0\">");
document.writeln("						</td>");
document.writeln("					</tr>");
document.writeln("				</table>");
document.writeln("			</td>");
document.writeln("		</tr>");																		
document.writeln("	</table>");

document.writeln("	<input type=hidden name=\"TYPE\" value=\"LOGON\">");
document.writeln("	<input type=hidden name=\"LogonURL\" value=\"" + logonURL + "\">");
document.writeln("	<input type=hidden name=\"ServletURL\" value=\"" + INBAdminServletURL + "\">");

document.writeln("</form>");
