﻿// JScript File


//NOTE SO FAR AGENT AND WEBUSER ARE NOT USED...CLEAN UP IF NOT NEEDED
//Represents an Agent
var Agent=Class.create(
{
	initialize: function()
	{
		this.AgentId=null;
		this.StatementName=null;
		this.Telephone=null;
		this.EmailAdd=null;
	}//end intialize
}); //End Agent

//Represents a web user, this is usually associated with an agent
var WebUser=Class.create(
{
	initialize: function()
	{
		this.UserId=null;
		this.UserName=null;
		this.PassWord=null;
		this.Agent=null;
		this.Email=null;
	}//end intialize
});//end WebUser

//Represents agent login functionality. This bridges between the form and the eventual request for login.
//It has to contain all the details for the loggin in functionality
var AgentLogin=Class.create(
{
	initialize: function (CommonId,ObjectMap,NotifyContainer)
	{
		this.UserNameControl=$(CommonId + ObjectMap.get('UserName'));
		this.PassWordControl=$(CommonId + ObjectMap.get('PassWord'));
		this.LoginDiv=$(CommonId + ObjectMap.get('LoginDiv'));
		this.LoginButton=$(CommonId + ObjectMap.get('LoginButton'));
		this.RequestResult=$(CommonId + ObjectMap.get('RequestResult'));
		this.NotifyContainer=$(NotifyContainer);
		this.url=location.href;
	},//end initialize
	getUserName: function()
	{
		return hex_md5($F(this.UserNameControl));
	},//end getUserName
	getPassWord: function()
	{
		return hex_md5($F(this.PassWordControl));
	},//end getPassWord
	RaiseLoginAttempt: function(sender)
	{
		if (sender===this.LoginButton)
		{
			var rawUserName=$F(this.UserNameControl);
			var rawPassWord=$F(this.PassWordControl);
			//first check lengths - if length < 3 or length > 20 (15 for password - see DB) return false
			if (rawUserName.length>2 && rawUserName.length<21)
			{
				if (rawPassWord.length>2 && rawPassWord.length<16)
				{
					this.SetLoginRequest();
				}
			}
			else
			{
				return false; //Do not do anything - no message needed
			}
		}
		else
		{
			return false;
		}
	},
	RaiseStartAgain: function()
	{
		this.RequestResult.innerHTML="&nbsp;";
		Element.hide(this.RequestResult);
		this.UserNameControl.value="";
		this.PassWordControl.value="";
		Element.show(this.LoginDiv);
		return false;
	},
	RaiseLogOut: function()
	{
		//set log out request
		this.SetLogoutRequest();
		return false;
	},
	SetLogoutRequest: function()
	{
		try
		{
			this.UserNameControl.value="";
			this.PassWordControl.value="";
			Element.hide(this.RequestResult);
			var LoginDiv=$(this.LoginDiv);
			var NotifyContainer=$(this.NotifyContainer);
			Element.show(NotifyContainer);
			new Ajax.Updater(this.RequestResult,this.url,
			{
				method: 'post',
				evalScripts: true,
				parameters:
				{
					ajaxLoad:	REQUESTTYPE['AGENTLOGOUT']
				},
				onComplete: function(transport) //note this keyword will not work in here
				{
					Element.hide(NotifyContainer);
					Element.show(LoginDiv);
				}
			});
		}
		catch (err)
		{
			alert(err)
			return false;
		}
	},
	SetLoginRequest: function()
	{
		try
		{
			Element.hide(this.LoginDiv);
			var NotifyContainer=$(this.NotifyContainer);
			var RequestResult=$(this.RequestResult);
			Element.show(NotifyContainer);
			new Ajax.Updater(this.RequestResult,this.url,
			{
				method: 'post',
				evalScripts: true,
				parameters:
				{
					ajaxLoad:	REQUESTTYPE['AGENTLOGIN'],
					UserName:	this.getUserName(),
					PassWord:	this.getPassWord()
				},
				onComplete: function(transport) //note this keyword will not work in here
				{
					Element.hide(NotifyContainer);
					Element.show(RequestResult);
				}
			});
		}
		catch (err)
		{
			alert(err)
			return false;
		}
	}//end UpdatePassengerRequest
});//end Agent Login

