<%@ LANGUAGE="JScript" %>

<%

var domRequest = new ActiveXObject("Microsoft.XMLDOM");
var domResponse = new ActiveXObject("Microsoft.XMLDOM");
var root;

Init();
HandleRequest();
SendReqponse();


function Init()
{

	domRequest.async = false;
	domResponse.async = false;
	domRequest.load(Request);
	root=domRequest.documentElement;
}

function HandleRequest()
{



	if(root.nodeName=="REQUEST_GENERAL")
		HandleGeneralRequest();
	else if(root.nodeName=="REQUEST_CONF_DETAILS")
		HandleConfDetailsRequest();
	else if(root.nodeName=="REQUEST_PARTY_DETAILS")
		HandlePartyDetailsRequest();
	else if(root.nodeName=="REQUEST_USER_RECORDING_DETAILS")
		HandleUserRecordingDetailsRequest();
	else if(root.nodeName=="REQUEST_USER_DETAILS")
		HandleLoginDetailsRequest();

}

function CreateConfResponse(TokenNode,status,description)
{
	transNode =CreateResponse("CONFIRM_CONF_DETAILS",status,description);

	if (TokenNode!=null)
	{
		nodeToken=domResponse.createNode (1,"TOKEN", "");
		nodeToken.text=TokenNode.text;
		transNode.appendChild(nodeToken);
	}
	actionNode = domResponse.createNode (1,"ACTION", "");
	transNode.appendChild(actionNode);

	createNode = domResponse.createNode (1,"CREATE", "");
	actionNode.appendChild(createNode);
	return transNode;
}

function HandleConfDetailsRequest()
{
	try
	{
		NumericIdNode = root.selectSingleNode("ACTION//CREATE//NUMERIC_ID");
		TokenNode = root.selectSingleNode("TOKEN");

		if (NumericIdNode==null)
		{

			CreateConfResponse(TokenNode,Session("statusIllegalNid"));

		}
		else
		{

			status=ConfQueryLdap(NumericIdNode.text)
			transNode =CreateConfResponse(TokenNode,status,"");

						
			if(status==0)
			{


				createNode=transNode.selectSingleNode("ACTION//CREATE");
				AddElementToResponse("NAME","ConfElementToAtt","ConfAttToQuery",createNode);
				AddElementToResponse("PASSWORD","ConfElementToAtt","ConfAttToQuery",createNode);
				AddElementToResponse("ENTRY_PASSWORD","ConfElementToAtt","ConfAttToQuery",createNode);
				AddElementToResponse("BILLING_DATA","ConfElementToAtt","ConfAttToQuery",createNode);

				infoNode = domResponse.createNode (1,"CONTACT_INFO_LIST", "");
				createNode.appendChild(infoNode);

				AddElementToResponse("CONTACT_INFO_1","ConfElementToAtt","ConfAttToQuery",infoNode,"CONTACT_INFO");
				AddElementToResponse("CONTACT_INFO_2","ConfElementToAtt","ConfAttToQuery",infoNode,"CONTACT_INFO");
				AddElementToResponse("CONTACT_INFO_3","ConfElementToAtt","ConfAttToQuery",infoNode,"CONTACT_INFO");
				
				AddElementToResponse("DISPLAY_NAME","ConfElementToAtt","ConfAttToQuery",createNode);
			}

		}

	}
	catch(error)
	{
		TokenNode = root.selectSingleNode("TOKEN");
		CreateConfResponse(TokenNode,Session("statusInternalError"),error.description);
	}

}

function HandleGeneralRequest()
{
	try
	{
		var transNode =CreateResponse("CONFIRM_GENERAL",0,"OK");

		actionNode = domResponse.createNode (1,"ACTION", "");
		transNode.appendChild(actionNode);

		Node = domResponse.createNode (1,"KEEP_ALIVE", "");
		actionNode.appendChild(Node);

	}
	catch(error)
	{
		CreateResponse("CONFIRM_GENERAL",Session("statusInternalError"),error.description);
	}

}


function CreatePartyResponse(TokenNode,status,description)
{
	transNode =CreateResponse("CONFIRM_PARTY_DETAILS",status,description);

	if (TokenNode!=null)
	{
		nodeToken=domResponse.createNode (1,"TOKEN", "");
		nodeToken.text=TokenNode.text;
		transNode.appendChild(nodeToken);
	}
	actionNode = domResponse.createNode (1,"ACTION", "");
	transNode.appendChild(actionNode);

	createNode = domResponse.createNode (1,"ADD", "");
	actionNode.appendChild(createNode);
	return transNode;
}


function HandlePartyDetailsRequest()
{
	
	try
	{
		PasswordNode=root.selectSingleNode ("ACTION//ADD//PASSWORD");
		TokenNode = root.selectSingleNode("TOKEN");
		
		if (PasswordNode==null)
		{
			CreatePartyResponse(TokenNode,Session("Illegal MCU user name/password"));

		}
		else
		{
			NumericIdNode = root.selectSingleNode("ACTION//ADD//NUMERIC_ID");
			
			if (NumericIdNode==null)
			{
				CreatePartyResponse(TokenNode,Session("statusIllegalNid"));
			}
			else
			{
				status=PartyQueryLdap(NumericIdNode.text,PasswordNode.text)
				transNode =CreatePartyResponse(TokenNode,status,"");


				if(status==0)
				{
					addNode=transNode.selectSingleNode("ACTION//ADD");	
					AddElementToResponse("NAME","PartyElementToAtt","PartyAttToQuery",addNode);
					if (AddElementToResponse("LEADER","PartyElementToAtt","PartyAttToQuery",addNode)==false)
					{
						leaderNode = domResponse.createNode (1,"LEADER", "");
						leaderNode.text="false";
						addNode.appendChild(leaderNode);
					}

					AddElementToResponse("VIP","PartyElementToAtt","PartyAttToQuery",addNode);

					infoNode = domResponse.createNode (1,"CONTACT_INFO_LIST", "");
					addNode.appendChild(infoNode);

					AddElementToResponse("CONTACT_INFO_1","PartyElementToAtt","PartyAttToQuery",infoNode,"CONTACT_INFO");
					AddElementToResponse("CONTACT_INFO_2","PartyElementToAtt","PartyAttToQuery",infoNode,"CONTACT_INFO");
					AddElementToResponse("CONTACT_INFO_3","PartyElementToAtt","PartyAttToQuery",infoNode,"CONTACT_INFO");
					AddElementToResponse("CONTACT_INFO_4","PartyElementToAtt","PartyAttToQuery",infoNode,"CONTACT_INFO");

				}
			}
		}


	}
	catch(error)
	{
		TokenNode = root.selectSingleNode("TOKEN");
		CreatePartyResponse(TokenNode,Session("statusInternalError"),error.description);
	}

}

function AddElementToResponse(name,ElementToAtt,AttToQuery,ParentNode,actualName)
{
	attValue=Session(ElementToAtt).Item(name);
	if(attValue!=null)
	{
		value=Session(AttToQuery).Item(attValue);
		if(value!=null)
		{
			if(actualName!=null)
				Node = domResponse.createNode (1,actualName, "");
			else
				Node = domResponse.createNode (1,name, "");
			Node.text=value;
			ParentNode.appendChild(Node);
		}
		return true;
	}
	else
		return false;
}

function HandleUserRecordingDetailsRequest()
{
	
	try
	{

		UserIdNode=root.selectSingleNode("USER_RECORDING_DETAILS//USER_ID");
		if (UserIdNode==null)
		{

			CreateResponse("CONFIRM_USER_RECORDING_DETAILS",Session("Illegal UserId"));

		}
		else
		{

			status=UserIdQueryLdap(UserIdNode.text);
			transNode =CreateResponse("CONFIRM_USER_RECORDING_DETAILS",status);
			if(status==0)
			{
				UserRecordingNode = domResponse.createNode (1,"USER_RECORDING_DETAILS", "");
				transNode.appendChild(UserRecordingNode);

				AddElementToResponse("NAME","RecordingElementToAtt","RecordingAttToQuery",UserRecordingNode);
				AddElementToResponse("EMAIL","RecordingElementToAtt","RecordingAttToQuery",UserRecordingNode);

			}
		}
	}
	catch(error)
	{
		CreateResponse("CONFIRM_USER_RECORDING_DETAILS",Session("statusInternalError"),error.description);
	}

}


function HandleLoginDetailsRequest()
{
	
	try
	{

		PasswordNode=root.selectSingleNode ("ACTION//AUTHENTICATE//PASSWORD");
		TokenNode = root.selectSingleNode("TOKEN");


		if (PasswordNode==null)
		{

			CreateLoginResponse(TokenNode,Session("Illegal MCU user name/password"),"");

		}
		else
		{
			UserNameNode = root.selectSingleNode("ACTION//AUTHENTICATE//USER_NAME");
			

			if (UserNameNode==null)
			{
				CreateLoginResponse(TokenNode,Session("Illegal MCU user name and\or password"),"");
			}
			else
			{
				status=LoginQueryLdap(UserNameNode.text,PasswordNode.text)
				transNode =CreateLoginResponse(TokenNode,status,"");
				
				if(status==0)
				{
					
					authenNode=transNode.selectSingleNode("ACTION//AUTHENTICATE");
					AddElementToResponse("USER_ID","LoginElementToAtt","LoginAttToQuery",authenNode);
					nodeGroup=domResponse.createNode (1,"AUTHORIZATION_GROUP", "");
					nodeGroup.text="administrator";
					authenNode.appendChild(nodeGroup);
				}
			}
		}
	}
	catch(error)
	{
		TokenNode = root.selectSingleNode("TOKEN");
		CreateLoginResponse(TokenNode,Session("statusIllegalUserNamePassword"),error.description +  UserNameNode.text);

	}
}

function CreateLoginResponse(TokenNode,status,description)
{
	transNode =CreateResponse("CONFIRM_USER_DETAILS",status,description);
	if (TokenNode!=null)
	{
		nodeToken=domResponse.createNode (1,"TOKEN", "");
		nodeToken.text=TokenNode.text;
		transNode.appendChild(nodeToken);
	}
	actionNode = domResponse.createNode (1,"ACTION", "");
	transNode.appendChild(actionNode);

	UserLoginNode = domResponse.createNode (1,"AUTHENTICATE", "");
	actionNode.appendChild(UserLoginNode);
	return transNode;
}

function SendReqponse()
{
	Response.buffer=true;
	domResponse.save(Response);
	Response.end();
}

function ConfQueryLdap(NumericId)
{


	var ldapConfQuery = Session("ConfLdapQuery");

	ldapConfQuery=ldapConfQuery.replace(/%/g,NumericId);
	var ArrayAtt = (new VBArray(Session("ConfAttToQuery").Keys()));    // Get the keys.

	var ADOConn;
	ADOConn = new ActiveXObject("ADODB.Connection");
	ADOConn.Provider = "ADSDSOObject";
	ADOConn.Open("ADs Provider");

	var ADOCommand,oRs;
	ADOCommand = new ActiveXObject("ADODB.Command");
	ADOCommand.ActiveConnection = ADOConn;
	ADOCommand.CommandText =ldapConfQuery;

	
	oRs = ADOCommand.Execute();

	if (oRs!=null && !oRs.Eof)
	{

		for (i = 0; i < Session("ConfAttToQuery").Count; i++)
		{
			field=GetLdapField(oRs,ArrayAtt.getItem(i)) ;
			if(field!=null)
				Session("ConfAttToQuery").Item(ArrayAtt.getItem(i))=field;

		}
		ADOConn.Close();
		return Session("statusOK");

	}
	else
	{
		ADOConn.Close();
		return Session("statusIllegalNid");
	}

}


function PartyQueryLdap(NumericId,Password)
{


	var PartyLdapQuery = Session("PartyLdapQuery");

	PartyLdapQuery=PartyLdapQuery.replace(/\$/g,Password);

	PartyLdapQuery=PartyLdapQuery.replace(/\%/g,NumericId);

	var ArrayAtt = (new VBArray(Session("PartyAttToQuery").Keys()));    // Get the keys.

	var ADOConn;
	ADOConn = new ActiveXObject("ADODB.Connection");
	ADOConn.Provider = "ADSDSOObject";
	ADOConn.Open("ADs Provider");

	var ADOCommand,oRs;
	ADOCommand = new ActiveXObject("ADODB.Command");
	ADOCommand.ActiveConnection = ADOConn;
	ADOCommand.CommandText =PartyLdapQuery;
	

	oRs = ADOCommand.Execute();

	if (oRs!=null && !oRs.Eof)
	{

		for (i = 0; i < Session("PartyAttToQuery").Count; i++)
		{
			ArrayAtt.getItem(i);
			field=GetLdapField(oRs,ArrayAtt.getItem(i)) ;
			if(field!=null)
				Session("PartyAttToQuery").Item(ArrayAtt.getItem(i))=field;

		}
		ADOConn.Close();
		return Session("statusOK");

	}
	else
	{
		ADOConn.Close();
		return Session("statusIllegalNid");
	}
	



}

function UserIdQueryLdap(UserId)
{

	var RecordingLdapQuery = Session("RecordingLdapQuery");
	RecordingLdapQuery=RecordingLdapQuery.replace("#",UserId);
	var ArrayAtt = (new VBArray(Session("RecordingAttToQuery").Keys()));    // Get the keys.

	var ADOConn;
	ADOConn = new ActiveXObject("ADODB.Connection");
	ADOConn.Provider = "ADSDSOObject";
	ADOConn.Open("ADs Provider");

	var ADOCommand,oRs;
	ADOCommand = new ActiveXObject("ADODB.Command");
	ADOCommand.ActiveConnection = ADOConn;
	ADOCommand.CommandText =RecordingLdapQuery;

	
	oRs = ADOCommand.Execute();

	if (oRs!=null && !oRs.Eof)
	{

		for (i = 0; i < Session("RecordingAttToQuery").Count; i++)
		{
			ArrayAtt.getItem(i);
			field=GetLdapField(oRs,ArrayAtt.getItem(i)) ;
			if(field!=null)
				Session("RecordingAttToQuery").Item(ArrayAtt.getItem(i))=field;

		}
		ADOConn.Close();
		return Session("statusOK");

	}
	else
	{
		ADOConn.Close();
		return Session("statusIllegalUserId");
	}

}



function LoginQueryLdap(UserName,Password)
{


	var LoginLdapQuery = Session("LoginLdapQuery");
	LoginLdapQuery=LoginLdapQuery.replace("*",UserName);
	LoginLdapQuery=LoginLdapQuery.replace("~",Password);

	var ArrayAtt = (new VBArray(Session("LoginAttToQuery").Keys()));    // Get the keys.
 	var ADOConn;
    ADOConn = new ActiveXObject("ADODB.Connection");
	ADOConn.Provider = "ADSDSOObject";
	ADOConn.Open("ADs Provider");
	

	//ADOConn.Open("ADs Provider","accord-domain\\"+UserName,Password, true); only when needed !!!

	var ADOCommand,oRs;
	ADOCommand = new ActiveXObject("ADODB.Command");
	ADOCommand.ActiveConnection = ADOConn;
	ADOCommand.CommandText =LoginLdapQuery;

	oRs = ADOCommand.Execute();



	if (oRs!=null && !oRs.Eof)
	{

		for (i = 0; i < Session("LoginAttToQuery").Count; i++)
		{
			ArrayAtt.getItem(i);
			field=GetLdapField(oRs,ArrayAtt.getItem(i)) ;
			if(field!=null)
				Session("LoginAttToQuery").Item(ArrayAtt.getItem(i))=field;

		}
		ADOConn.Close();
		return Session("statusOK");

	}
	else
	{
		ADOConn.Close();
		return Session("statusIllegalUserNamePassword");
	}
	return Session("statusOK");
	



}


function GetLdapField(oRs,field)
{



	//while (!oRs.Eof)
	{


		if(oRs.Fields.Item(field).value!=null)
		{
			if(oRs.Fields.Item(field).type==12) //array
			{
				a = new VBArray(oRs.Fields.Item(field).value);
				b = a.toArray();
				return b[0];
			}
			else
				return oRs.Fields.Item(field).value;
		}
		else
			return null;
		//oRs.Movenext();
	}


}

function CreateResponse(ResponseName,ResponseValue,ErrorDescription)
{
	transNode = domResponse.createNode (1,ResponseName, "");
	domResponse.appendChild(transNode);

	statusNode = domResponse.createNode (1,"RETURN_STATUS", "");
	transNode.appendChild(statusNode);

	Node = domResponse.createNode (1,"ID", "");
	Node.text=ResponseValue;
	statusNode.appendChild(Node);

	Node = domResponse.createNode (1,"DESCRIPTION", "");
	strDescription=Session("StatusDic").Item(ResponseValue.toString(10));
	if (ErrorDescription!=null && ErrorDescription!="")
	{
		strDescription=strDescription+"("+ErrorDescription+")";
	}
	Node.text=strDescription;
	statusNode.appendChild(Node);
	return transNode;
}
%>


