function
LoginMngr()
{
	this.m_strRouterUrl;
	this.m_oUtils = new Utils();
	this.m_strQuestion;
	this.login = LoginMngr_login;
	this.init = LoginMngr_init;
	this.getPageParams = LoginMngr_getPageParams;
	this.checkMessageParams = LoginMngr_checkMessageParams;
	this.setQuestion = LoginMngr_setQuestion;
	this.handleFailureLogin = LoginMngr_handleFailureLogin;
	this.handleSuccessLogin = LoginMngr_handleSuccessLogin;
	this.handleForgotPass = LoginMngr_handleForgotPass;
	this.handlePassExpired = LoginMngr_handlePassExpired;
	this.getUtils = LoginMngr_getUtils;
	this.clearFields = LoginMngr_clearFields;
	this.logout = LoginMngr_logout;
	this.init();

	this.getLangId = LoginMngr_getLangId;
}

function
LoginMngr_getUtils() {
	return this.m_oUtils;
}

function
LoginMngr_init() {
	//this.m_oConsts = new X2WebConstsManager();
	this.m_oConsts = new PMP4Consts();
	//this.m_oConsts.loadFromResource("/PMP4/Client/Common/Consts/Consts.xml");
	this.m_strRouterUrl = this.m_oConsts.ROUTING_SERVLET;
	this.m_oPageConsts = this.getPageParams();
	this.m_oUtils.setPageLabels(this.m_oPageConsts);
	this.checkMessageParams();

	this.logout();
}

function
LoginMngr_getPageParams() {
	var oConstsMngr = null;
	var oRpcMngr = new RpcMngr(this.m_strRouterUrl);
	var oRpcRequests = new RpcRequests("1001");
	this.getUtils().setPageParamsRequest(oRpcRequests , 1);
	var oRpcResponses = oRpcMngr.performRequest(oRpcRequests);
	var oRes = oRpcResponses.getResponse("GetPageParams");

	if (
		oRes &&
		!oRes.isErrorResponse()
	) {
		var oXmlParams = (
			oRes.getResultContent().selectSingleNode("Constants")
		);
		oConstsMngr = new X2WebConstsManager();
		oConstsMngr.loadXml(
			this.getUtils().getXmlUtils().getXmlObject(oXmlParams.xml)
		);
	}

	return oConstsMngr;
}

function
LoginMngr_logout() {
	var oRpcMngr = new RpcMngr(this.m_oConsts.ROUTING_SERVLET);
	var oRpcRequests = new RpcRequests("1001");
	oRpcRequests.addRequest("LogOut");

	var oRpcResponses = oRpcMngr.performRequest(oRpcRequests);
	var oResponse = oRpcResponses.getResponse("LogOut");
	if (oResponse) {
		if (oResponse.isErrorResponse()) {
			var m = new MessageBox(
				oResponse.getMessage(),
				"Error",
				this.m_oPageConsts.OK
			);
			m.show();
		}
		else
		{
			this.m_nLangId = oResponse.getResultContent().selectSingleNode("LANGID").text;
		}
	}
	else {
		var m = new MessageBox(
			"Null response - LogOut",
			"Error",
			this.m_oPageConsts.OK
		);
		m.show();
	}
}

function
LoginMngr_login(
	strUserName,
	strUserPassword,
	strUserId
) {
	strUserName = strUserName.replace(/\s+$/, '');
	if ( strUserName == "" || strUserPassword == "" ) {
		var m = new MessageBox(
			this.m_oPageConsts.FillUserNameAndPassword,
			"Warnning",
			this.m_oPageConsts.OK
		);
		m.show();
	}

	var oRpcMngr = new RpcMngr(this.m_oConsts.ROUTING_SERVLET);
	var oRpcRequests = new RpcRequests("1001");
	oRpcRequests.addRequest("Login");
	var oXmlNode = this.getUtils().getXmlUtils().getXmlObject();
	oRpcRequests.addRequestParam(
		"Login",
		this.getUtils().getXmlUtils().createNode(
			oXmlNode,
			"USERNAME",
			strUserName
		)
	);
	oRpcRequests.addRequestParam(
		"Login",
		this.getUtils().getXmlUtils().createNode(
			oXmlNode,
			"USERID",
			strUserId
		)
	);
	oRpcRequests.addRequestParam(
		"Login",
		this.getUtils().getXmlUtils().createNode(
			oXmlNode,
			"USERPASSWORD",
			strUserPassword
		)
	);

	var oRpcResponses = oRpcMngr.performRequest(oRpcRequests);
	var oLoginResponse = oRpcResponses.getResponse("Login");
	if (oLoginResponse) {
		if (!oLoginResponse.isErrorResponse()) {
			this.handleSuccessLogin(oLoginResponse);
		}
		else {
			this.handleFailureLogin(oLoginResponse);
		}
	}
}

function
LoginMngr_handleFailureLogin(
	oLoginResponse
) {

	var nErrorCode = oLoginResponse.getResultCode()
	if (nErrorCode == this.m_oConsts.INVALID_USER_PASSWORD) {
		var m = new MessageBox(
			this.m_oPageConsts.getConst("InvalidUserNameOrPassword"),
			"Warnning",
			this.m_oPageConsts.OK
		);
		m.show();
		this.clearFields();
	}
	else if (nErrorCode == this.m_oConsts.PASSWORD_EXPIRED) {
		var m = new MessageBox(
			this.m_oPageConsts.getConst("PasswordExpiredSpecifyNewOne"),
			"Warnning",
			this.m_oPageConsts.OK
		);
		m.show();
		this.handlePassExpired();
	}
	else if (nErrorCode == this.m_oConsts.SUBSCRIPTION_EXPIRED) {
		var oResponseXml = oLoginResponse.getResultContent();
		var m = new MessageBox(
			this.m_oPageConsts.getConst("SubscriptionPeriodExpired"),
			"Warnning",
			this.m_oPageConsts.OK
		);
		m.show();
	}
	else if (nErrorCode == this.m_oConsts.ACCOUNT_HAS_BEEN_BLOCKED) {
		var oResponseXml = oLoginResponse.getResultContent();
		var m = new MessageBox(
			this.m_oPageConsts.getConst("AccountHasBeenBlocked"),
			"Warnning",
			this.m_oPageConsts.OK
		);
		m.show();
	}
}

function
LoginMngr_handlePassExpired() {
	var arr = new Array();
	arr["Utils"] = this.getUtils();
	arr["Consts"] = this.m_oConsts;
	arr["UserName"] = txtUserName.value;
	arr["PassExpired"] = "PassExpired";
	var strDialogRes = this.getUtils().openDialog(
		"NewPassword.htm",
		arr,
		"dialogHeight:245px;scroll:no;dialogWidth:350px;help:no;status:no"
	);
	if (strDialogRes) {
		this.login(
			txtUserName.value,
			strDialogRes
		);
	}
}

function
LoginMngr_handleSuccessLogin(
	oLoginResponse
) {
	writeSessionCookie("login.page", window.location);
	var oXmlRes = oLoginResponse.getResultContent();
	var userType = oXmlRes.selectSingleNode("SUBSCRIBER/TYPE").text;
	if (userType == this.m_oConsts.PATIENT) {
		window.location = "../Patient/default.htm";
	}
	else if (userType == this.m_oConsts.PHYSICIAN) {
		//window.location = "/PMP4/testers/tester.htm";
		window.location = "../Physician/default.htm";
	}
	else if (userType == this.m_oConsts.EXPERT_PHYSICIAN) {
		//window.location = "/PMP4/testers/tester.htm";
		window.location = "../Expert/default.htm";
		//window.location = "../Physician/default.htm";
	}
	else if (userType == this.m_oConsts.SUPERVISOR) {
		window.location = "../Supervisor/Lots/default.htm";
	}
	else if (userType == this.m_oConsts.ADMINISTRATOR ||
			 userType == this.m_oConsts.SUPER_ADMINISTRATOR	) {
				window.location = "../Admin/Subscribers/subscribersList.htm";
	} else {
		var m = new MessageBox(
			this.m_oPageConsts.InvalidUserNameOrPassword,
			"Warnning",
			this.m_oPageConsts.OK
		);
		m.show();
		this.clearFields();
	}
}

function
LoginMngr_handleForgotPass() {
	if (txtUserName.value == "") {
		var m = new MessageBox(
			this.m_oPageConsts.InsertUserName,
			"Warnning",
			this.m_oPageConsts.OK
		);
		m.show();
		txtUserName.focus();
	}
	else
	if ( this.setQuestion() ) {
		var arr = new Array();
		arr["Question"] = this.m_strQuestion;
		arr["Consts"] = this.m_oConsts;
		arr["UserName"] = txtUserName.value;
		var arrDialogRes = this.getUtils().openDialog(
			"ForgotPassword.htm",
			arr,
			"dialogHeight:230px;scroll:no;dialogWidth:350px;help:no;status:no"
		);
/*
		if (arrDialogRes) {
			if (arrDialogRes["Success"] ) {
				this.login(
					txtUserName.value,
					arrDialogRes["NewPass"]
				);
			}
		}
*/
	}
}

function
LoginMngr_checkMessageParams() {
	var arrMessageParams = new Array();
	arrMessageParams["InsertUserName"] = this.m_oPageConsts.InsertUserName;
	arrMessageParams["InvalidUserNameOrPassword"] = this.m_oPageConsts.InvalidUserNameOrPassword;
	arrMessageParams["PasswordExpiredSpecifyNewOne"] = this.m_oPageConsts.PasswordExpiredSpecifyNewOne;
	arrMessageParams["SubscriptionPeriodExpired"] = this.m_oPageConsts.SubscriptionPeriodExpired;
	arrMessageParams["FillUserNameAndPassword"] = this.m_oPageConsts.FillUserNameAndPassword;

	for( i in arrMessageParams ) {
		if ( !arrMessageParams[i] ) {
			var m = new MessageBox(
				"Could not load message: " + i,
				"Error",
				this.m_oPageConsts.OK
			);
			m.show();
		}
	}
}

function
LoginMngr_setQuestion() {
	var fReturnValue = false;
	var strUserName = document.getElementById("txtUserName").value;

	var oXmlUtils = new XmlUtilsIe();
	var oRpcMngr = new RpcMngr(this.m_strRouterUrl);

	var oRpcRequests = new RpcRequests("1001");
	oRpcRequests.addRequest("GetSubscriberSecurityDetails");
	oRpcRequests.addRequestParam(
		"GetSubscriberSecurityDetails",
		oXmlUtils.getXmlObject(
			"<FILTER>" +
				"<ITEM>" +
					"<NAME>NAME</NAME>" +
					"<VALUE>" + strUserName + "</VALUE>" +
				"</ITEM>" +
			"</FILTER>"
		).documentElement
	);
	var oRpcResponses = oRpcMngr.performRequest(oRpcRequests);
	var oResponse = oRpcResponses.getResponse("GetSubscriberSecurityDetails");
	if (oResponse && !oResponse.isErrorResponse())
	{
		var oXml = oResponse.getResultContent();
		if (oXml.selectNodes("SECURITIES/SECURITY").length != 0 )
		{
			var type = oXml.selectSingleNode("SECURITIES/SECURITY/TYPE").text;
			if ((type != this.m_oConsts.SM_PATIENT) &&
				(type != this.m_oConsts.PRIVATE_PATIENT) &&
				(type != this.m_oConsts.NEW_PATIENT))
			{
				this.m_strQuestion = oXml.selectSingleNode("SECURITIES/SECURITY/QUESTION").text;

				fReturnValue = true;
			}
			else
			{
				var m = new MessageBox(
					this.m_oPageConsts.InvalidUserName,
					"Warnning",
					this.m_oPageConsts.OK
				);
				m.show();
			}
		}
		else {
			var m = new MessageBox(
				this.m_oPageConsts.InvalidUserName,
				"Warnning",
				this.m_oPageConsts.OK
			);
			m.show();
		}
	}
	else {
		var m = new MessageBox(
			oRpcResponses.getResponse("GetSubscriberSecurityDetails").getMessage(),
			"Error",
			this.m_oPageConsts.OK
		);
		m.show();
	}

	return fReturnValue;
}

function
LoginMngr_clearFields() {
	var oUserNameText = document.getElementById("txtUserName");
	var oPasswordText = document.getElementById("txtPassword");

	oUserNameText.value = "";
	oPasswordText.value = "";
}

function
LoginMngr_getLangId()
{
	return this.m_nLangId;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

