Viewer: LoginView class

package flashscriptMVC.views
{
	import flashscriptMVC.AbstractClass;
	import flashscriptMVC.IModel;
	import flashscriptMVC.IControlHandler;
	import flash.events.*;
	import fl.controls.TextInput;
	import flash.text.TextFormat;
	import fl.controls.Button;
	import fl.controls.Label;
	import flash.net.SharedObject;
	import flashscriptMVC.events.TextHandlerEvent;
	
	public class LoginView extends AbstractClass
	{
		private var userLabel:Label;
		private var passLabel:Label;
		private var userName:TextInput;
		private var passWord:TextInput;
		private var submit:Button;
		private var signinBut:Button;
		private var sendDataFile:String;
		private var so:SharedObject;
		private var md:IModel;
		private var c:IControlHandler;
		
		public function LoginView(aModel:IModel,aController:IControlHandler = null):void
		{
/*
* The LoginView extends the AbstractClass, which has a number of functions,
* which have to be overridden. The LoginView specifically sets all the
* parameters for a Login module. This class communicates with the Controller
* and the Model class.
*/
			super(aModel,aController);
			c = controller as IControlHandler;
			md = model as IModel;
			//
			// Textformat for all components
			//
			var format:TextFormat = new TextFormat();
			format.font = "Arial Black";
			format.size = 11;
			//
			// Adding all components to the stage.
			//
			userLabel = new Label  ;
			userLabel.setSize(125,22);
			userLabel.setStyle("textFormat",format);
			addChild(userLabel);

			passLabel = new Label  ;
			passLabel.setSize(125,22);
			passLabel.move(0,50);
			passLabel.setStyle("textFormat",format);
			addChild(passLabel);

			userName = new TextInput  ;
			userName.setSize(150,22);
			userName.move(130,0);
			userName.setStyle("textFormat",format);
			addChild(userName);

			passWord = new TextInput  ;
			passWord.setSize(150,22);
			passWord.move(130,50);
			passWord.displayAsPassword = true;
			passWord.setStyle("textFormat",format);
			addChild(passWord);

			submit = new Button  ;
			submit.move(130,90);
			submit.name = "submit";
			submit.setStyle("textFormat",format);
			submit.addEventListener(MouseEvent.CLICK,submitHandler);
			addChild(submit);

			signinBut = new Button  ;
			signinBut.setSize(210,22);
			signinBut.move(130,180);
			signinBut.name = "signin";
			signinBut.setStyle("textFormat",format);
			signinBut.addEventListener(MouseEvent.CLICK,signinHandler);
			addChild(signinBut);
		}
/*
* The update function is the communication of this LoginView with the Model
* class. The Model class sends different responses, which are identified by
* the event types. Depending on the event type the LoginViwer will respond.
*/
		override public function update (event:Event=null):void
		{
			//
			// adding a keyboard event to the submit button.
			//
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyHandler);
			if (event.type == "serverResponse")
			{
				var sResponse:String = md.getXMLData().serverresponse + "?login=" + userName.text;
				var thle:TextHandlerEvent = new TextHandlerEvent(TextHandlerEvent.SERVER_RESPONSE);
				thle.myURL = sResponse;
				c.dispatchEvent(thle);
			}
			if (event.type == "serverError")
			{
				c.alertChangeHandler(md.getXMLData().errormessage,false,parent);
			}
			//
			// setting focus on the user name textinput
			//
			md.focusObject (userName);
			//
			// data received from XML file to add labels
			//
			userLabel.text = md.getXMLData().label1;
			passLabel.text = md.getXMLData().label2;
			submit.label = md.getXMLData().submitlabel;
			signinBut.label = md.getXMLData().signinlabel;
			//
			// The php file used to handle the login.
			//
			sendDataFile = md.getXMLData().phpfile;
			//
			// We add a SharedObject in case data need to be stored
			// on the users machine. We set the third parameter (Boolean) to true,
			// which makes sure that storage is only possible with a secure (https)
			// protocol.
			//
			try
			{
				so = SharedObject.getLocal(md.getXMLData().shareobj,"/",true);
				if (so.data.myEmail != null)
				{
					userName.text = so.data.myEmail;
					passWord.text = so.data.myPassword;
				}
			}
			catch (e:Error)
			{
				trace(e);
			}
		}
		//
		// submission using a mouse click
		//
		override public function submitHandler (event:MouseEvent):void
		{
			eventhandler (event);
		}
		//
		// submission using a keyboard key
		//
		override public function keyHandler (event:KeyboardEvent):void
		{
			if (int(event.keyCode) == 13)
			{
				eventhandler (event);
			}
		}
/*
* The eventhandler is shared by the login and signin buttons.
* Here the commands go to the controller including opening an
* Alert window. Further the variable names and values are sent by dispatching an event.
*/
		private function eventhandler(event:Event):void
		{
			if (userName.text == null || userName.text == "")
			{
				c.alertChangeHandler("Enter username!",false,submit.parent);
				return;
			}
			if (passWord.text == null || passWord.text == "")
			{
				c.alertChangeHandler("Enter password!",false,submit.parent);
				return;
			}
			//
			// names for variables from the XML file.
			//
			var var1:String = md.getXMLData().variable1;
			var var2:String = md.getXMLData().variable2;
			//
			// variable names and data are stored in separate arrays.
			//
			var varArray:Array = new Array(var1,var2);
			var dataArray:Array = new Array(userName.text,passWord.text);
			var thle:TextHandlerEvent = new TextHandlerEvent(TextHandlerEvent.TEXT_HANDLER);
			thle.varAr = varArray;
			thle.dataAr = dataArray;
			thle.myURL = sendDataFile;
			c.dispatchEvent(thle);
		}
		/*
* The eventhandler for the signin button. Here the commands go to the controller.
* We dispatch an event for the controller. This uncouples the view from the controller.
*/
		private function signinHandler(event:Event):void
		{
			var sResponse:String = md.getXMLData().registration;
			var thle:TextHandlerEvent = new TextHandlerEvent(TextHandlerEvent.SERVER_RESPONSE);
			thle.myURL = sResponse;
			c.dispatchEvent(thle);
		}
	}
}