//zmg, 03.01.01
//this file needs a better description!
//
//




// File: actions_password.txt
// Description: Router password related functions




// password_processPassword(Keycode)
// IN : Takes one key input from user
// DO : String together input keys as the password, and give the entered
//	password to password_parsePassword() when <enter> is depressed
// OUT: None
function password_processPassword(keycode) {

	// get the active router object
	var rptr = eval("_root.r" + _root.VISIBLE_ROUTER);


	if (keycode == 8) {

		// if the key code is BACKSPACE
		// delete 1 character if INPUT has characters to delete
		if (rptr.INPUT.length > 0) {
			rptr.INPUT = rptr.INPUT.substring(0,rptr.INPUT.length-1);
		}

	} else if (keycode == 13) {

		// if the keycode is ENTER
		// parse the password in INPUT
		password_parsePassword();

	} else {

		// else the keycode is part of the password so add it to INPUT
		rptr.INPUT += chr(keycode);
	}
}




// password_parsePassword()
// IN : active_router.INPUT
// DO : Takes the input and checks against the password.
// 	for line con 0 and enable, parses password
// OUT: None
function password_parsePassword() {

	// get the active router object
	var rptr = eval("_root.r" + _root.active_router);

	// get the visible router object
	var visrptr = eval("_root.r" + _root.VISIBLE_ROUTER);

	// space off the output
	output_write("\n");


	if (rptr.MODE == "startUp") {

		// if we are still in startup mode


		//if ((visrptr.INPUT == rptr.run.line.con_password)) {
		// modified : Sitaram
		// checking for the vty_password instead of con_password
		// For telnet, Based on the Real Router, validation of password is done
		//through vty_passwword
		 if ((visrptr.INPUT == rptr.run.line.vty_password)) {
			// if the entered password matches the saved password
			// allow user to have access the router
		// end of modification : sitaram
			rptr.passcount = 0;

			_root.commandline_changeProcess("commandline_processCommandLine");
			_root.commandline_setMode("user", _root.active_router);
			_root.commandline_commandLine();

		} else if ((rptr.passcount == 2)) {

			// else if the user has tried to gain access three times
			// unsuccessfully, exit the user, return to startup mode

			rptr.passcount = 0;

			var vr = eval("_root.r" + _root.VISIBLE_ROUTER);
			var r = eval("_root.r" + _root.active_router);

			if(vr.isTelnetingOut == true) {
				output_write("% Bad passwords\n", "\n");
				output_write("[Connection to " + TELNET_ADDRESS + " closed by foreign host]\n");


				// if we are telneting out
				// get the variables to reinstate the router
				var tempmode = vr.telnetPath.pop();
				var tempname = vr.telnetPath.pop();
				var tempptr = eval("_root.r" + tempname);

				// resetting the router we came from
				if(r.wasntStartUp == true) {
					r.router_startup = false;
					r.wasntStartUp = false;
					r.oldMode = "";
				} else {
					commandline_setMode(r.oldMode, _root.active_router);
					r.oldMode = "";
				}

				_root.active_router = tempname;
				tempptr.MODE = tempmode;

				if(_root.VISIBLE_ROUTER == tempname) {
					vr.isTelnetingOut = false;
				}

				_root.active_router.INPUT = "";
				_root.commandline_changeProcess("commandline_processCommandLine");
				_root.commandline_commandLine();

			} else {
				_root.commandline_changeProcess("router_startup_processStartUp");
				rptr.router_startup = false;
				_root.router_startup_checkStartUp();
			}
		}



	} else if (rptr.MODE == "user") {

		// modified by sitaram
		// checking for the enable password also.
		// user entered both enable and secret password's,
		// secret password take prescendence over enable
		// if we are in user mode trying for enable mode


		// if the entered password matches the saved password
		// allow user to have access to enable mode

		if (rptr.run.secret != "") {

			if (visrptr.INPUT == rptr.run.secret) {
				rptr.passcount = 0;

				_root.commandline_changeProcess("commandline_processCommandLine");
				_root.commandline_setMode("enable", _root.active_router);
				_root.commandline_commandLine();
				//added by suresh on 29 march 2002
				stepnum = stepnum + 1;
				loadImage(stepnum,stepnum-1);
				goToNextStep(stepnum, stepnum-1);


			} else if (rptr.passcount == 2) {

			// else if the user has tried to gain access three times
			// unsuccessfully, exit the user, return to user mode

			rptr.passcount = 0;

			_root.commandline_changeProcess("commandline_processCommandLine");
			_root.commandline_setMode("user", _root.active_router);
			_root.output_write("% Bad secrets\n");
			_root.commandline_commandLine();
			}
		}
		 else if (rptr.run.password != "")
		{

			if (visrptr.INPUT == rptr.run.password) {

				rptr.passcount = 0;

				_root.commandline_changeProcess("commandline_processCommandLine");
				_root.commandline_setMode("enable", _root.active_router);
				_root.commandline_commandLine();
				//added by suresh on 29 march 2002
				stepnum = stepnum + 1;
				loadImage(stepnum,stepnum-1);
				goToNextStep(stepnum, stepnum-1);


			} else if (rptr.passcount == 2) {

			// else if the user has tried to gain access three times
			// unsuccessfully, exit the user, return to user mode

			rptr.passcount = 0;

			_root.commandline_changeProcess("commandline_processCommandLine");
			_root.commandline_setMode("user", _root.active_router);
			_root.output_write("% Bad secrets\n");
			_root.commandline_commandLine();
			}
		}
	}


	// print the password prompt again if the user has not entered the
	// correct password for the first or second time
	if (processName == "password_processPassword") {
		rptr.passcount++;
		password_passwordLine();
	}
}





// password_passwordLine()
// IN : None
// DO : Prompts the user for a password
// OUT: Password prompt
function password_passwordLine() {

	// get the active router object
	var rptr = eval("_root.r" + _root.VISIBLE_ROUTER);

	// clear the input buffer for the router
	rptr.INPUT = "";

	// print the password prompt
	output_write("Password:");

	// create the global COMMAND array
	COMMAND = new Array();
}
