using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.RFIDLab.EPCISDemo.Common;
using Microsoft.RFIDLab.EPCISDemo.ShippingAgentWS;
using Microsoft.RFIDLab.EPCISDemo.EPCEvent;
//using Microsoft.RFIDLab.EPCISDemo.ReceivingAgentWS;



namespace Microsoft.RFIDLab.EPCISDemo
{
	/// <summary>
	/// Performs operations for the shipping-agent.
	/// </summary>
	public class ShippingAgent : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Xml Xml1;
		//private ASNGenerationService.Pip3B2AdvanceShipmentNotification objProxy;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.TextBox txtAcknowledgement;
		protected System.Web.UI.WebControls.Button btnPickupGoods;
		protected System.Web.UI.WebControls.Button btn3B2ASNtoReceiver;
		protected System.Web.UI.WebControls.Button btnShipGoods;
		protected System.Web.UI.WebControls.Label lblInformation;
		protected System.Web.UI.WebControls.Button btnMoveToNextPage;
		private Pip3B12ShippingOrderRequest obj3B12ShippingOrderRequest;
		//private Pip3B12ShippingOrderConfirmation objShippingOrderConfirmation;
	
		/// <summary>
		/// Invoked when the page is loaded.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void Page_Load(object sender, System.EventArgs e)
		{
			//Check if the session is still alive
			if (Session["ManufacturerAcknowledgement"] == null)
			{
				Response.Redirect("OrderDetails.aspx?session_expired=true", true);
			}
			ShippingAgentWS.ReceiptAcknowledgement objACKfromMFR = (ShippingAgentWS.ReceiptAcknowledgement)Session["ManufacturerAcknowledgement"];

            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(objACKfromMFR.GetType());
			System.IO.MemoryStream  memStream = new System.IO.MemoryStream();
			x.Serialize(memStream, objACKfromMFR);

			Byte[] byteArray;
			byteArray = memStream.ToArray();
			String strXML = System.Text.Encoding.ASCII.GetString(byteArray);
			
			txtAcknowledgement.Height = 600;
			txtAcknowledgement.Text = strXML;
			Label1.Text = "Received <b>Receipt Acknowledgment</b> from Manufacturer.<br>Initiate <b>Item Pickup</b> from Manufacturer Location.";
			btnPickupGoods.Enabled = true;
			btn3B2ASNtoReceiver.Visible = false;
			btnShipGoods.Visible = false;

			if (Request.QueryString["mode"]=="print")
			{
				System.Xml.XmlDocument objXMLDoc = new System.Xml.XmlDocument();
				objXMLDoc.LoadXml(strXML);
				Xml1.Visible = true;
				txtAcknowledgement.Visible=false;
				Xml1.Document = objXMLDoc;
				Xml1.TransformSource="ShippingNotificationRequest.xslt";
			}
			else
			{
				Xml1.Visible = false;
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnPickupGoods.Click += new System.EventHandler(this.btnPickupGoods_Click);
			this.btn3B2ASNtoReceiver.Click += new System.EventHandler(this.btn3B2ASNtoReceiver_Click);
			this.btnShipGoods.Click += new System.EventHandler(this.btnShipGoods_Click);
			this.btnMoveToNextPage.Click += new System.EventHandler(this.btnMoveToNextPage_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		/// <summary>
		/// Invoked when the 'Pick up goods' button is clicked.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void btnPickupGoods_Click(object sender, System.EventArgs e)
		{
			//Use the PIP3B12ShippingOrderRequest that was stored in the session.
			//For all EPC's listed in the order, update local database of goods pickup 
			//event and send the same to Manufacturer database.
			if (Session["PIP3b12ShippingOrderRequestObject"] == null)
			{
				Response.Redirect("OrderDetails.aspx?session_expired=true", true);
			}

			//Update Shipping Database with Goods pickup event for all EPCs.
			// Commented - MS
			// LogEPCEvent(TrackTraceDBManager.Constants.ItemPickUpEventFromShippingAgent);

			obj3B12ShippingOrderRequest = ( ShippingAgentWS.Pip3B12ShippingOrderRequest )
				Session["PIP3b12ShippingOrderRequestObject"] ;
			LogEPCEvent(TrackTraceDBManager.Constants.ItemPickUpEventFromShippingAgent,
				"Shipping Agent",
				obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
				"Shipping Agent Location" ) ;
			
			//create EPCEvent Object and populate with order number, steward and epc numbers.
			ManufacturerWS.EquipmentProductCodeInformation eventInfo = CreateEPCEventInformation () ;
			
			//Call Manufacturer Web Service to send EPC Event.
			ManufacturerWS.ManufacturerWebService proxyMfr = new ManufacturerWS.ManufacturerWebService();
			
			// MS
			proxyMfr.ReceiveEPCEvent ( eventInfo ) ;

			Label1.Text = "<b>Item Pickup</b> from Manufacturer Location is successfull.<br> Send <b>Shipment Notification</b> to Receiving Agent with expected delivery dates.";
			lblInformation.Text = "Advance Shipment Notification";

			// ASNGenerationService.Pip3B2AdvanceShipmentNotification obj3B2ASN = CreateAdvanceShipmentPIP();

			// MS
			ReceivingAgentWS.Pip3B2AdvanceShipmentNotification obj3B2ASN = CreateAdvanceShipmentPIP();

			System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(obj3B2ASN.GetType());
			System.IO.MemoryStream  memStream = new System.IO.MemoryStream();
			x.Serialize(memStream, obj3B2ASN);

			Byte[] byteArray;
			byteArray = memStream.ToArray();
			String strXML = System.Text.Encoding.ASCII.GetString(byteArray);
			
			txtAcknowledgement.Height = 600;
			txtAcknowledgement.Text = strXML;

			btnPickupGoods.Visible = false;
			btn3B2ASNtoReceiver.Visible = true;
			
		}

		/// <summary>
		/// Invoked when the 'Send Shipment Notification' button is clicked.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void btn3B2ASNtoReceiver_Click(object sender, System.EventArgs e)
		{
			Label1.Text = "Advance Shipment Notification sent to Receiving Agent.<br> <b>Deliver Items to Receiving Agent</b> Location and send <b>Shipment Order Confirmation Notification</b> to Manufacturer.<br> Move to Receiving Agent Location.";
			lblInformation.Text = "Shipping Order Confirmation";

			btnPickupGoods.Visible = false;
			btn3B2ASNtoReceiver.Visible = false;
			btnShipGoods.Visible = true;

			ManufacturerWS.Pip3B13ShippingOrderConfirmationNotification objPip3B13;
			objPip3B13 = Create3B13OrderConfirmationPIP();

			System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(objPip3B13.GetType());
			System.IO.MemoryStream  memStream = new System.IO.MemoryStream();
			x.Serialize(memStream, objPip3B13);

			Byte[] byteArray;
			byteArray = memStream.ToArray();
			String strXML = System.Text.Encoding.ASCII.GetString(byteArray);
			
			txtAcknowledgement.Height = 600;
			txtAcknowledgement.Text = strXML;


			if (Session["PIP3b12ShippingOrderRequestObject"] == null)
			{
				Response.Redirect("OrderDetails.aspx?session_expired=true", true);
			}

			obj3B12ShippingOrderRequest = (Pip3B12ShippingOrderRequest)Session["PIP3b12ShippingOrderRequestObject"];
			ReceivingAgentWS.Pip3B2AdvanceShipmentNotification obj3B2ASN = CreateAdvanceShipmentPIP();
			Session["ASNforRAfromSA"] = obj3B2ASN;
						
			//Update ShippingAgent Database EPC status.
			LogEPCEvent(TrackTraceDBManager.Constants.NotifyOfAsnFromShippingAgentToReceivingAgent,
				obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
				obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
				"Shipping Agent Location" ) ;
			
			//Create and Send PIP3B2ASN to ReceivingAgent.
			ReceivingAgentWS.ReceivingAgentWebService objRAProxy = new ReceivingAgentWS.ReceivingAgentWebService();
			ReceivingAgentWS.BusinessRequest objBusReq = new ReceivingAgentWS.BusinessRequest();
			objBusReq.GlobalProcessIndicatorCode = "3B2";
			objBusReq.objPip = obj3B2ASN;			

			// MS
			ReceivingAgentWS.BusinessResponse objBusResp = objRAProxy.ReceiveBusinessRequest(objBusReq);
			if(objBusResp != null)
			{
				if(objBusResp.objAcknowledgement != null) 
				{
					//ok we received acknowledgment from Receiving Agent to take accept the goods
					//delivery. Log business event for the epcs.
					LogEPCEvent(TrackTraceDBManager.Constants.AcknowledgementFromReceivingAgentToShippingAgent,
						objBusResp.objAcknowledgement.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
						objBusResp.objAcknowledgement.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
						"Shipping Agent Location" );
				}
			}


		}

		/// <summary>
		/// Invoked when the 'Ship goods' button is clicked.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void btnShipGoods_Click(object sender, System.EventArgs e)
		{
			btnPickupGoods.Visible = false;
			btn3B2ASNtoReceiver.Visible = false;
			btnShipGoods.Enabled = false;
			
			//Create OrderConfirmation PIP and send to Manufacturer. 
			ManufacturerWS.Pip3B13ShippingOrderConfirmationNotification objPip3B13;
			objPip3B13 = Create3B13OrderConfirmationPIP();
			 
			ManufacturerWS.ManufacturerWebService mfrProxy = new ManufacturerWS.ManufacturerWebService();
			ManufacturerWS.BusinessResponse objResp = new ManufacturerWS.BusinessResponse();
			objResp.objPip = objPip3B13;
			objResp.GlobalProcessIndicatorCode = "3B13";
	
			//Call Manufacturer WebService and send this pip. Currently we ignore any
			//response back from this method. 
			// true : webservice successfully process the acknowledgment.
			// false : failed to process or not supported.
			bool result = mfrProxy.ReceiveBusinessAcknowledgement(objResp);
			if(!result) 
			{
				//ignore now. not in scope.
			}
			
            //now Update ShippingAgent Database with goods shipped status.
			// MS - commented
			// LogEPCEvent(TrackTraceDBManager.Constants.NotifyOfShippingOrderConfFromShippingAgentToManufacturer);

			

			ReceivingAgentWS.EquipmentProductCodeInformation forRA = CreateEPCEventInformationForReceivingAgent();
			ReceivingAgentWS.ReceivingAgentWebService proxy = new Microsoft.RFIDLab.EPCISDemo.ReceivingAgentWS.ReceivingAgentWebService();
			proxy.ReceiveEPCEvent(forRA);

			//sent business event confirming the shipment of goods to manufacturer.
			LogEPCEvent(TrackTraceDBManager.Constants.NotifyOfShippingOrderConfFromShippingAgentToManufacturer,
				obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
				obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
				"Shipping Agent Location" ) ;

			/*
			// MS
			ReceivingAgentWS.BusinessResponse response = new Microsoft.RFIDLab.EPCISDemo.ReceivingAgentWS.BusinessResponse () ;
			response.objPip = CreateAdvanceShipmentPIP () ;
			ReceivingAgentWS.ReceivingAgentWebService proxy = new Microsoft.RFIDLab.EPCISDemo.ReceivingAgentWS.ReceivingAgentWebService () ;
			proxy.ReceiveBusinessAcknowledgement ( response ) ;
			btnMoveToNextPage.Visible = true ;
			*/

			//Move to Receiving Agent UI
			Response.Redirect ( "ReceivingAgent.aspx" ) ;
	
		}

		/// <summary>
		/// Creates a Pip3B13 Shipping-order confirmation notification.
		/// </summary>
		/// <returns>Pip3B13 Shipping-order confirmation notification.</returns>
		private ManufacturerWS.Pip3B13ShippingOrderConfirmationNotification Create3B13OrderConfirmationPIP()
		{
			obj3B12ShippingOrderRequest = (Pip3B12ShippingOrderRequest)Session["PIP3B12ShippingOrderRequestObject"];
			
			//Create PIP3B13 object.
			ManufacturerWS.Pip3B13ShippingOrderConfirmationNotification objPip3B13 = new ManufacturerWS.Pip3B13ShippingOrderConfirmationNotification();
			objPip3B13.GlobalDocumentFunctionCode = "3B13";
			objPip3B13.thisDocumentGenerationDateTime.DateTimeStamp = System.DateTime.UtcNow.ToUniversalTime().ToString("yyyyMMddThhmmss.000Z");
			objPip3B13.ShipmentConfirmation.OrderInformation.orderNumber.ProprietaryDocumentIdentifier = obj3B12ShippingOrderRequest.ShippingOrder.OrderInformation.orderNumber.ProprietaryDocumentIdentifier;

			//ShipTo Information. It should be Receiving Agent.
			objPip3B13.ShipmentConfirmation.ShipmentInformation.shipTo.PartnerDescription.BusinessDescription.businessName.FreeFormText.Value = "RECEIVING AGENT CORP";
			objPip3B13.ShipmentConfirmation.ShipmentInformation.shipTo.PartnerDescription.GlobalPartnerClassificationCode = "Carrier";
			objPip3B13.ShipmentConfirmation.ShipmentInformation.shipTo.PartnerDescription.ContactInformation.contactName.FreeFormText.Value = "Receiving Agent";
			objPip3B13.ShipmentConfirmation.ShipmentInformation.shipTo.PartnerDescription.ContactInformation.EmailAddress = "ReceivingAgent@ReceivingAgent.com";
			objPip3B13.ShipmentConfirmation.ShipmentInformation.shipTo.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber = "978 653 2443";
			objPip3B13.ShipmentConfirmation.ShipmentInformation.shipTo.PartnerDescription.ContactInformation.facsimileNumber.CommunicationsNumber = "978 653 2444";

			//Information of EPC sent 
			ManufacturerWS.ShipmentLineItem objShipmentLineItem = new ManufacturerWS.ShipmentLineItem();
			int numberOfPallets = obj3B12ShippingOrderRequest.ShippingOrder.ShipmentLineItem[0].ShippingContainer.Length;

			ManufacturerWS.ShippingContainer[] objPalletArray = new ManufacturerWS.ShippingContainer[numberOfPallets];
			for(int i = 0; i < objPalletArray.Length; i++)
			{
				ManufacturerWS.ShippingContainer objPalletItem = new ManufacturerWS.ShippingContainer();
				//store pallet EPC
				objPalletItem.shippingContainerIdentifier.ProprietarySerialIdentifier = obj3B12ShippingOrderRequest.ShippingOrder.ShipmentLineItem[0].ShippingContainer[i].shippingContainerIdentifier.ProprietarySerialIdentifier;
				int numberOfCases = obj3B12ShippingOrderRequest.ShippingOrder.ShipmentLineItem[0].ShippingContainer[i].ShippingContainerItem.Length;
				
				ManufacturerWS.ShippingContainerItem[] objCaseArray = new ManufacturerWS.ShippingContainerItem[numberOfCases];
				for(int j = 0; j < objCaseArray.Length; j++)
				{
					ManufacturerWS.ShippingContainerItem objCaseItem = new ManufacturerWS.ShippingContainerItem();
					objCaseItem.shippingContainerItemIdentifier.ProprietarySerialIdentifier = obj3B12ShippingOrderRequest.ShippingOrder.ShipmentLineItem[0].ShippingContainer[i].ShippingContainerItem[j].shippingContainerItemIdentifier.ProprietarySerialIdentifier;
					objCaseArray[j] = objCaseItem;
				}
				objPalletItem.ShippingContainerItem = objCaseArray;
				objPalletArray[i] = objPalletItem;
			}

			objShipmentLineItem.ShippingContainer = objPalletArray;

			objPip3B13.ShipmentConfirmation.ShipmentLineItem = new ManufacturerWS.ShipmentLineItem[1];
			objPip3B13.ShipmentConfirmation.ShipmentLineItem[0] = objShipmentLineItem;
				
			//store fromRole information with shipping agent.
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName.FreeFormText.Value = "SHIPPING AGENT CORP";
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.ContactInformation = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.ContactInformation2 () ;
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.ContactInformation.contactName = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.contactName2 () ;
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.ContactInformation.contactName.FreeFormText = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.FreeFormText2 () ;
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.ContactInformation.contactName.FreeFormText.Value = "Shipping Agent" ;
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.ContactInformation.EmailAddress = "ShippingAgent@ShippingAgent.com";
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber = "425 777 8888";
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.ContactInformation.facsimileNumber.CommunicationsNumber = "425 777 9999";
			
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.PhysicalLocation1 () ;
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.PhysicalAddress2 () ;
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.cityName1 () ;
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName.FreeFormText = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.FreeFormText2 () ;
			objPip3B13.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName.FreeFormText.Value = "Shipping Agent Location" ;

			objPip3B13.toRole = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.toRole2 () ;
			objPip3B13.toRole.PartnerRoleDescription = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.PartnerRoleDescription2 () ;
			objPip3B13.toRole.PartnerRoleDescription.ContactInformation = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.ContactInformation2 () ;
			objPip3B13.toRole.PartnerRoleDescription.ContactInformation.contactName = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.contactName2 () ;
			objPip3B13.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.FreeFormText2 () ;
			objPip3B13.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value = obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value ;

			return objPip3B13;
		}

		/// <summary>
		/// Logs EPC events for all EPC tags in the current session to the data-base.
		/// </summary>
		/// <param name="EPCEventId">The event-ID to store for all tags. </param>
		private void LogEPCEvent(int EPCEventId)
		{
			TrackTraceDBManager dm = new TrackTraceDBManager("SHIPPING_AGENT");
			string strEPC, strEPCNumber;

			obj3B12ShippingOrderRequest = (Pip3B12ShippingOrderRequest)Session["PIP3B12ShippingOrderRequestObject"];
					
			ShipmentLineItem objShipmentLineItem = obj3B12ShippingOrderRequest.ShippingOrder.ShipmentLineItem[0];
			ShippingContainer[] objShippingContainerCollection = objShipmentLineItem.ShippingContainer;
			for(int i = 0; i < objShippingContainerCollection.Length; i++)
			{
				ShippingContainer objShippingContainer = objShippingContainerCollection[i]; 
				strEPC = objShippingContainer.shippingContainerIdentifier.ProprietarySerialIdentifier;
				//Extract the EPC Number from the EPC string above
				strEPCNumber = strEPC.Substring(0, strEPC.IndexOf(" "));
				//Shipping Order confirmation Event is logged.
				dm.AddEpcStatusInfo(strEPCNumber, EPCEventId, System.DateTime.Now);
				ShippingContainerItem[] objShippingContainerItemCollection = objShippingContainer.ShippingContainerItem;
				for(int j = 0; j < objShippingContainerItemCollection.Length; j++)
				{
					ShippingContainerItem objShippingContainerItem = objShippingContainerItemCollection[j];
					strEPC = objShippingContainerItem.shippingContainerItemIdentifier.ProprietarySerialIdentifier;
					//Extract the EPC Number from the EPC string above
					strEPCNumber = strEPC.Substring(0, strEPC.IndexOf(" "));
					//Shipping Order confirmation Event is logged.
					dm.AddEpcStatusInfo(strEPCNumber, EPCEventId, System.DateTime.Now);
				}
			}
		}

		/// <summary>
		/// Logs EPC events for all EPC tags in the current session to the data-base.
		/// </summary>
		/// <param name="EPCEventId">The event-ID to store for all tags. </param>
		private void LogEPCEvent ( int EPCEventId, string eventFrom, string eventTo,
			string location )
		{
			TrackTraceDBManager dm = new TrackTraceDBManager("SHIPPING_AGENT");
			string strEPC, strEPCNumber;

			obj3B12ShippingOrderRequest = (Pip3B12ShippingOrderRequest)Session["PIP3B12ShippingOrderRequestObject"];
					
			ShipmentLineItem objShipmentLineItem = obj3B12ShippingOrderRequest.ShippingOrder.ShipmentLineItem[0];
			ShippingContainer[] objShippingContainerCollection = objShipmentLineItem.ShippingContainer;
			for(int i = 0; i < objShippingContainerCollection.Length; i++)
			{
				ShippingContainer objShippingContainer = objShippingContainerCollection[i]; 
				strEPC = objShippingContainer.shippingContainerIdentifier.ProprietarySerialIdentifier;
				//Extract the EPC Number from the EPC string above
				strEPCNumber = strEPC.Substring(0, strEPC.IndexOf(" "));
				//Shipping Order confirmation Event is logged.
				dm.AddFullEpcStatusInfo(strEPCNumber, EPCEventId, System.DateTime.Now,
					eventFrom, eventTo, location );
				ShippingContainerItem[] objShippingContainerItemCollection = objShippingContainer.ShippingContainerItem;
				for(int j = 0; j < objShippingContainerItemCollection.Length; j++)
				{
					ShippingContainerItem objShippingContainerItem = objShippingContainerItemCollection[j];
					strEPC = objShippingContainerItem.shippingContainerItemIdentifier.ProprietarySerialIdentifier;
					//Extract the EPC Number from the EPC string above
					strEPCNumber = strEPC.Substring(0, strEPC.IndexOf(" "));
					//Shipping Order confirmation Event is logged.
					dm.AddFullEpcStatusInfo(strEPCNumber, EPCEventId, System.DateTime.Now,
						eventFrom, eventTo, location );
				}
			}
		}

		/// <summary>
		/// Creates an order-information object containing all pallet and case
		/// tags present in the Session.
		/// </summary>
		/// <returns>Encapsulated Order-information object containing the entire
		/// order-information. </returns>
		private Common.OrderInformation CreateOrderInformationObject()
		{
			if (Session["PIP3b12ShippingOrderRequestObject"] == null)
			{
				Response.Redirect("OrderDetails.aspx?session_expired=true", true);
			}

			obj3B12ShippingOrderRequest = (Pip3B12ShippingOrderRequest)Session["PIP3b12ShippingOrderRequestObject"];
			Common.OrderInformation objOrder = new Common.OrderInformation();
			
			string strEPC, strEPCNumber;
			
			ShipmentLineItem objShipmentLineItem = obj3B12ShippingOrderRequest.ShippingOrder.ShipmentLineItem[0];
			ShippingContainer[] objShippingContainerCollection = objShipmentLineItem.ShippingContainer;
			int noOfPallets = objShippingContainerCollection.Length; // number of pallets.
			
			//new
			objOrder.OrderNumber = obj3B12ShippingOrderRequest.ShippingOrder.OrderInformation.orderNumber.ProprietaryDocumentIdentifier;
			Common.PalletItemCollection orderPalletCollection = new Common.PalletItemCollection();
			///

			for(int i = 0; i < noOfPallets; i++)
			{
				ShippingContainer objShippingContainer = objShippingContainerCollection[i]; 
				strEPC = objShippingContainer.shippingContainerIdentifier.ProprietarySerialIdentifier;
				//Extract the EPC Number from the EPC string above
				strEPCNumber = strEPC.Substring(0, strEPC.IndexOf(" "));
								
				Common.PalletItem orderPallet = new Common.PalletItem();
				orderPallet.EpcTagId.FreeFormText.Value = strEPCNumber; //store pallet epc
				Common.CaseItemCollection orderCaseCollection = new Common.CaseItemCollection();
				
				ShippingContainerItem[] objShippingContainerItemCollection = objShippingContainer.ShippingContainerItem;
				int noOfCases = objShippingContainerItemCollection.Length;

				for(int j = 0; j < noOfCases; j++)
				{
					ShippingContainerItem objShippingContainerItem = objShippingContainerItemCollection[j];
					strEPC = objShippingContainerItem.shippingContainerItemIdentifier.ProprietarySerialIdentifier;
					//Extract the EPC Number from the EPC string above
					strEPCNumber = strEPC.Substring(0, strEPC.IndexOf(" "));
					
					Common.CaseItem orderCase = new Common.CaseItem();
					orderCase.EpcTagId.FreeFormText.Value = strEPCNumber; //store case epc
					orderCaseCollection.Add(orderCase);
				}
				orderPallet.CaseItemCollection = orderCaseCollection;
				orderPalletCollection.Add(orderPallet);
			}
			
			objOrder.PalletItemCollection = orderPalletCollection;
			return objOrder;
		}

		/// <summary>
		/// Creates ASN to be sent to the Receiving-agent.
		/// </summary>
		/// <returns>3B2 ASN to be sent to the receiving-agent. </returns>
		private ReceivingAgentWS.Pip3B2AdvanceShipmentNotification CreateAdvanceShipmentPIP()
		{
			//Create and Send PIP3B2ASN to ReceivingAgent.
			ReceivingAgentWS.Pip3B2AdvanceShipmentNotification obj3B2ASN = new ReceivingAgentWS.Pip3B2AdvanceShipmentNotification();

			obj3B2ASN.thisDocumentGenerationDateTime = new ReceivingAgentWS.thisDocumentGenerationDateTime();
			obj3B2ASN.thisDocumentGenerationDateTime.DateTimeStamp = System.DateTime.UtcNow.ToUniversalTime().ToString("yyyyMMddThhmmss.000Z");

			obj3B2ASN.thisDocumentIdentifier = new ReceivingAgentWS.thisDocumentIdentifier();
			obj3B2ASN.thisDocumentIdentifier.ProprietaryDocumentIdentifier = "Advance Shipment Notification";
			
			//Initialize fromRole and fill the values.
			obj3B2ASN.fromRole = new ReceivingAgentWS.fromRole();
			obj3B2ASN.fromRole.PartnerRoleDescription = new ReceivingAgentWS.PartnerRoleDescription();
			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation = new ReceivingAgentWS.ContactInformation();
			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.contactName = new ReceivingAgentWS.contactName();
			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText = new ReceivingAgentWS.FreeFormText();
			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.telephoneNumber = new ReceivingAgentWS.telephoneNumber();
			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.facsimileNumber = new ReceivingAgentWS.facsimileNumber();
			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription = new ReceivingAgentWS.PartnerDescription();
			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription = new ReceivingAgentWS.BusinessDescription();
			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName = new ReceivingAgentWS.businessName();
			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName.FreeFormText = new ReceivingAgentWS.FreeFormText();

			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value = "Shipping Agent";
			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.EmailAddress					= "ShippingAgent@ShippingAgent.com";
			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber	= "425 779 4433";
			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.facsimileNumber.CommunicationsNumber	= "425 779 5533";
			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier = "Shipping Agent Unique Id";
			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName.FreeFormText.Value = "SHIPPING AGENT CORP" ;
			
			//Initialize toRole and fill the values.
			obj3B2ASN.toRole = new ReceivingAgentWS.toRole();
			obj3B2ASN.toRole.PartnerRoleDescription = new ReceivingAgentWS.PartnerRoleDescription();
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation = new ReceivingAgentWS.ContactInformation();
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.contactName = new ReceivingAgentWS.contactName();
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText = new ReceivingAgentWS.FreeFormText();
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.telephoneNumber = new ReceivingAgentWS.telephoneNumber();
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.facsimileNumber = new ReceivingAgentWS.facsimileNumber();
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription = new ReceivingAgentWS.PartnerDescription();
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription = new ReceivingAgentWS.BusinessDescription();
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName = new ReceivingAgentWS.businessName();
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName.FreeFormText = new ReceivingAgentWS.FreeFormText();

			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value	= "Receiving Agent";
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.EmailAddress						= "ReceivingAgent@ReceivingAgent.com";
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber		= "748 485 5633";
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.facsimileNumber.CommunicationsNumber		= "748 985 5688";
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier = "Receiving Agent Unique Id";
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName.FreeFormText.Value  = "RECEIVING AGENT CORP";

			//Initialize and fillup Originator information
			obj3B2ASN.AdvancedShipmentNotification = new ReceivingAgentWS.AdvancedShipmentNotification();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner = new ReceivingAgentWS.OriginatingPartner();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom = new ReceivingAgentWS.shipFrom();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription = new ReceivingAgentWS.PartnerDescription();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription = new ReceivingAgentWS.BusinessDescription();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.businessName = new ReceivingAgentWS.businessName();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.businessName.FreeFormText = new ReceivingAgentWS.FreeFormText();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation = new ReceivingAgentWS.ContactInformation();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.contactName = new ReceivingAgentWS.contactName();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.contactName.FreeFormText = new ReceivingAgentWS.FreeFormText();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.telephoneNumber = new ReceivingAgentWS.telephoneNumber();
			
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.businessName.FreeFormText.Value = "MANUFACTURER CORP";
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier			= "Manufacturer Unique id";
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.contactName.FreeFormText.Value		= "Manufacturer Agent";
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber = "425 769 4533";

			//file up destination receiver information.
			obj3B2ASN.AdvancedShipmentNotification.Shipment = new ReceivingAgentWS.Shipment();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner = new ReceivingAgentWS.ReceivingPartner();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo = new ReceivingAgentWS.shipTo();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription = new ReceivingAgentWS.PartnerDescription();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation = new ReceivingAgentWS.ContactInformation();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.contactName = new ReceivingAgentWS.contactName();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.contactName.FreeFormText = new ReceivingAgentWS.FreeFormText();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.telephoneNumber = new ReceivingAgentWS.telephoneNumber();
			
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.contactName.FreeFormText.Value	= "Receiver Name";
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber = "999 456 1023";
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.EmailAddress						= "Receiver@ReceiverPlace.com";

			//Add EPC information.
			string strEPC;
			obj3B12ShippingOrderRequest = (Pip3B12ShippingOrderRequest)Session["PIP3B12ShippingOrderRequestObject"];
			
			ShipmentLineItem objShipmentLineItem = obj3B12ShippingOrderRequest.ShippingOrder.ShipmentLineItem[0];
			ShippingContainer[] objShippingContainerCollection = objShipmentLineItem.ShippingContainer;
			int palletCount = objShippingContainerCollection.Length;

			obj3B2ASN.AdvancedShipmentNotification = new ReceivingAgentWS.AdvancedShipmentNotification();
			obj3B2ASN.AdvancedShipmentNotification.Shipment = new ReceivingAgentWS.Shipment();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.numberOfShippingContainers = new ReceivingAgentWS.numberOfShippingContainers();
            			
			obj3B2ASN.AdvancedShipmentNotification.Shipment.numberOfShippingContainers.CountableAmount = palletCount.ToString();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer = new ReceivingAgentWS.ShippingContainer[palletCount];
			
			for(int i = 0; i < palletCount; i++)
			{
				ShippingContainer objShippingContainer = objShippingContainerCollection[i]; 
				strEPC = objShippingContainer.shippingContainerIdentifier.ProprietarySerialIdentifier;
				
				ReceivingAgentWS.ShippingContainer pallet = new ReceivingAgentWS.ShippingContainer();
				pallet.shippingContainerIdentifier = new ReceivingAgentWS.shippingContainerIdentifier(); //new operator
				pallet.shippingContainerIdentifier.ProprietarySerialIdentifier = strEPC;
				obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i] = pallet;

				ShippingContainerItem[] objShippingContainerItemCollection = objShippingContainer.ShippingContainerItem;
				int caseCount = objShippingContainerItemCollection.Length;

				pallet.ShippingContainerItem = new ReceivingAgentWS.ShippingContainerItem[caseCount];

				for(int j = 0; j < caseCount; j++)
				{
					ShippingContainerItem objShippingContainerItem = objShippingContainerItemCollection[j];
					strEPC = objShippingContainerItem.shippingContainerItemIdentifier.ProprietarySerialIdentifier;

					ReceivingAgentWS.ShippingContainerItem caseItem = new ReceivingAgentWS.ShippingContainerItem();
					caseItem.shippingContainerItemIdentifier = new ReceivingAgentWS.shippingContainerItemIdentifier(); //new operator
					caseItem.shippingContainerItemIdentifier.ProprietarySerialIdentifier = strEPC;
					pallet.ShippingContainerItem[j] = caseItem;
				}
			}

			return obj3B2ASN;

		} //end of PIP3B2ASN creation routine.

		/// <summary>
		/// Create EPC information for all EPCs in the session.
		/// </summary>
		/// <returns>Equipment product-code information object containing
		/// all EPC tags. </returns>
		private ReceivingAgentWS.EquipmentProductCodeInformation CreateEPCEventInformationForReceivingAgent ()
		{
			obj3B12ShippingOrderRequest = (Pip3B12ShippingOrderRequest)Session["PIP3b12ShippingOrderRequestObject"];
			ReceivingAgentWS.Pip3B2AdvanceShipmentNotification  objASN = (ReceivingAgentWS.Pip3B2AdvanceShipmentNotification)Session["ASNforRAfromSA"];

			//Create Custom Object which stores Ordernumber, pallet and its associated 
			//case epc tags. We later use this object to populate EPCEvent object.
			Common.OrderInformation objOrder = CreateOrderInformationObject();
			
			//create EPCEvent Object and populate with order number, steward and epc numbers.
			ReceivingAgentWS.EquipmentProductCodeInformation objEPCEvent = new Microsoft.RFIDLab.EPCISDemo.ReceivingAgentWS.EquipmentProductCodeInformation () ;

			//Fillup Steward information. This is the person who 'owns' the EPC currently.
			objEPCEvent.Steward.managedBy.PartnerDescription.ContactInformation.contactName.FreeFormText.Value	= obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value;
			objEPCEvent.Steward.managedBy.PartnerDescription.ContactInformation.EmailAddress					= obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.EmailAddress;
			objEPCEvent.Steward.managedBy.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber = obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber;
			objEPCEvent.Steward.managedBy.PartnerDescription.ContactInformation.facsimileNumber.CommunicationsNumber = obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.facsimileNumber.CommunicationsNumber;

			objEPCEvent.CreationTimeStamp.DateTimeStamp = System.DateTime.UtcNow.ToUniversalTime().ToString("yyyyMMddThhmmss.000Z");
			objEPCEvent.BusinessTransactionIdentifier = obj3B12ShippingOrderRequest.thisDocumentIdentifier.ProprietaryDocumentIdentifier;
			
			//fill up Agent information. this indicates who is sending this event.
			objEPCEvent.CurrentAgent.Agent.reportedBy.PartnerDescription.ContactInformation.contactName.FreeFormText.Value = "Shipping Agent";
			objEPCEvent.CurrentAgent.Agent.reportedBy.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber = "425 653 2444";
			objEPCEvent.CurrentAgent.Agent.reportedBy.PartnerDescription.ContactInformation.facsimileNumber.CommunicationsNumber = "425 653 2333";
			objEPCEvent.CurrentAgent.Agent.reportedBy.PartnerDescription.ContactInformation.EmailAddress = "ShippingAgent@ShippingAgent.com";

			//add EPC information.
			int noOfPalletsInOrder = objOrder.PalletItemCollection.Count;
			objEPCEvent.EquipmentProductCodeEvent = new ReceivingAgentWS.EquipmentProductCodeEvent[noOfPalletsInOrder] ;
			int i = 0 ;

			foreach(Common.PalletItem pallet in objOrder.PalletItemCollection)
			{
				ReceivingAgentWS.EquipmentProductCodeEvent objEPCEventPallet = new ReceivingAgentWS.EquipmentProductCodeEvent();
				objEPCEventPallet.EquipmentProductCode.FreeFormText.Value = pallet.EpcTagId.FreeFormText.Value;
				objEPCEventPallet.EventType = Convert.ToString(TrackTraceDBManager.Constants.ItemPickUpEventFromShippingAgent);
				objEPCEventPallet.AssociateEquipmentProductCode = new ReceivingAgentWS.AssociateEquipmentProductCode[pallet.CaseItemCollection.Count] ;
				objEPCEvent.EquipmentProductCodeEvent[i++] = objEPCEventPallet;
				int j = 0 ;

				objEPCEventPallet.Agent.reportedBy.PartnerDescription.ContactInformation.contactName.FreeFormText.Value =
					objASN.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value;
				objEPCEventPallet.Agent.reportedBy.PartnerDescription.ContactInformation.facsimileNumber.CommunicationsNumber =
					objASN.toRole.PartnerRoleDescription.ContactInformation.facsimileNumber.CommunicationsNumber;
				objEPCEventPallet.Agent.reportedBy.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber =
					objASN.toRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber;
				objEPCEventPallet.Agent.reportedBy.PartnerDescription.ContactInformation.EmailAddress =
					objASN.toRole.PartnerRoleDescription.ContactInformation.EmailAddress;
			
				//set the ItemDelivered event type.
				objEPCEventPallet.EPCEventIdentifier = TrackTraceDBManager.Constants.ItemDeliveredFromShippingAgentToReceivingAgent.ToString();
					
				objEPCEventPallet.EventLocation.GlobalLocationIdentifier = "Shipping Agent Location" ;

				foreach(Common.CaseItem caseitem in pallet.CaseItemCollection)
				{
					ReceivingAgentWS.AssociateEquipmentProductCode objEPCEventCase = new ReceivingAgentWS.AssociateEquipmentProductCode();
					objEPCEventCase.FreeFormText.Value = caseitem.EpcTagId.FreeFormText.Value;
					objEPCEventPallet.AssociateEquipmentProductCode[j++] = objEPCEventCase;
				}
			}

			return objEPCEvent;

		} // end of CreateEPCEventInformationForReceivingAgent ()

		/// <summary>
		/// Create EPC information for all EPCs in the session.
		/// </summary>
		/// <returns>Equipment product-code information object containing
		/// all EPC tags. </returns>
		private ManufacturerWS.EquipmentProductCodeInformation CreateEPCEventInformation ()
		{
			obj3B12ShippingOrderRequest = (Pip3B12ShippingOrderRequest)Session["PIP3b12ShippingOrderRequestObject"];

			//Create Custom Object which stores Ordernumber, pallet and its associated 
			//case epc tags. We later use this object to populate EPCEvent object.
			Common.OrderInformation objOrder = CreateOrderInformationObject();
			
			//create EPCEvent Object and populate with order number, steward and epc numbers.
			ManufacturerWS.EquipmentProductCodeInformation objEPCEvent = new Microsoft.RFIDLab.EPCISDemo.ManufacturerWS.EquipmentProductCodeInformation () ;

			//Fillup Steward information. This is the person who 'owns' the EPC currently.
			objEPCEvent.Steward.managedBy.PartnerDescription.ContactInformation.contactName.FreeFormText.Value	= obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value;
			objEPCEvent.Steward.managedBy.PartnerDescription.ContactInformation.EmailAddress					= obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.EmailAddress;
			objEPCEvent.Steward.managedBy.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber = obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber;
			objEPCEvent.Steward.managedBy.PartnerDescription.ContactInformation.facsimileNumber.CommunicationsNumber = obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.facsimileNumber.CommunicationsNumber;

			objEPCEvent.CreationTimeStamp.DateTimeStamp = System.DateTime.UtcNow.ToUniversalTime().ToString("yyyyMMddThhmmss.000Z");
			objEPCEvent.BusinessTransactionIdentifier = obj3B12ShippingOrderRequest.thisDocumentIdentifier.ProprietaryDocumentIdentifier;
			
			//fill up Agent information. this indicates who is sending this event.
			objEPCEvent.CurrentAgent.Agent.reportedBy.PartnerDescription.ContactInformation.contactName.FreeFormText.Value = "Shipping Agent";
			objEPCEvent.CurrentAgent.Agent.reportedBy.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber = "425 653 2444";
			objEPCEvent.CurrentAgent.Agent.reportedBy.PartnerDescription.ContactInformation.facsimileNumber.CommunicationsNumber = "425 653 2333";
			objEPCEvent.CurrentAgent.Agent.reportedBy.PartnerDescription.ContactInformation.EmailAddress = "ShippingAgent@ShippingAgent.com";

			//add EPC information.
			int noOfPalletsInOrder = objOrder.PalletItemCollection.Count;
			objEPCEvent.EquipmentProductCodeEvent = new ManufacturerWS.EquipmentProductCodeEvent[noOfPalletsInOrder] ;
			int i = 0 ;

			foreach(Common.PalletItem pallet in objOrder.PalletItemCollection)
			{
				ManufacturerWS.EquipmentProductCodeEvent objEPCEventPallet = new ManufacturerWS.EquipmentProductCodeEvent();
				objEPCEventPallet.EquipmentProductCode.FreeFormText.Value = pallet.EpcTagId.FreeFormText.Value;
				objEPCEventPallet.EventType = Convert.ToString(TrackTraceDBManager.Constants.ItemPickUpEventFromShippingAgent);
				objEPCEventPallet.AssociateEquipmentProductCode = new ManufacturerWS.AssociateEquipmentProductCode[pallet.CaseItemCollection.Count] ;
				objEPCEvent.EquipmentProductCodeEvent[i++] = objEPCEventPallet;
				int j = 0 ;

				objEPCEventPallet.Agent.reportedBy.PartnerDescription.ContactInformation.contactName.FreeFormText.Value =
                    obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value ;
				objEPCEventPallet.Agent.reportedBy.PartnerDescription.ContactInformation.facsimileNumber.CommunicationsNumber =
					obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.facsimileNumber.CommunicationsNumber ;
				objEPCEventPallet.Agent.reportedBy.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber =
					obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber ;
				objEPCEventPallet.Agent.reportedBy.PartnerDescription.ContactInformation.EmailAddress =
					obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.EmailAddress ;
				objEPCEventPallet.EPCEventIdentifier = TrackTraceDBManager.Constants.ItemPickUpEventFromShippingAgent.ToString () ;
				objEPCEventPallet.EventLocation.GlobalLocationIdentifier = "Shipping Agent Location" ;

				foreach(Common.CaseItem caseitem in pallet.CaseItemCollection)
				{
					ManufacturerWS.AssociateEquipmentProductCode objEPCEventCase = new ManufacturerWS.AssociateEquipmentProductCode();
					objEPCEventCase.FreeFormText.Value = caseitem.EpcTagId.FreeFormText.Value;
					objEPCEventPallet.AssociateEquipmentProductCode[j++] = objEPCEventCase;
				}
			}

			return objEPCEvent;

		} // end of CreateEPCEventInformation ()

		/// <summary>
		/// Invoked on click of the 'Move to Receiving agent's location' button.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void btnMoveToNextPage_Click(object sender, System.EventArgs e)
		{
			Response.Redirect ( "ReceivingAgent.aspx" ) ;

		} // end of btnMoveToNextPage_Click ()

	} // class definition over

} // end of namespace
