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;


namespace Microsoft.RFIDLab.EPCISDemo
{
	/// <summary>
	/// Performs initiation of shipping.
	/// </summary>
	public class InitiateShipping : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Xml Xml1;
		protected System.Web.UI.WebControls.HyperLink lnkPrint;
		private ASNGenerationService.Pip3B2AdvanceShipmentNotification obj3B2ASN;
		protected System.Web.UI.WebControls.TextBox txtAcknowledgement;
		protected System.Web.UI.WebControls.TextBox txtShippingOrder;
		protected System.Web.UI.WebControls.Button btnShipNotification;
		protected System.Web.UI.WebControls.Button btnReceiptAck;
		private Pip3B12ShippingOrderRequest obj3B12ShippingOrderRequest;
		string strPIP3B2;
		protected System.Web.UI.WebControls.Label lblStatus;
		protected System.Web.UI.WebControls.Label lblAcknowledgement;
		protected System.Web.UI.WebControls.Label lblOrderConfirmation;
		protected System.Web.UI.WebControls.Label lblOrderRequest;
		
		/// <summary>
		/// Invoked when the page loads.
		/// </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["ASN"] == null)
			{
				Response.Redirect("OrderDetails.aspx?session_expired=true", true);
			}
			
			if (!Page.IsPostBack)
			{
				
				obj3B2ASN = (ASNGenerationService.Pip3B2AdvanceShipmentNotification)Session["ASN"];
			
				btnReceiptAck.Visible = false;
				btnShipNotification.Visible = true;
											
				// Put user code to initialize the page here
				lblStatus.Text = "Send <b>Shipping Order Request</b> (PIP3B12) to Shipping Agent. <br> This authorizes Shipping Agent to Carry out Shipment on behalf of Manufacturer.";

				obj3B2ASN = (ASNGenerationService.Pip3B2AdvanceShipmentNotification)Session["ASN"];

				//Create the Shipping Order PIP 3B12
				CreateShippingOrderPIP();

				//display Shipping Order Request in the TextBox
				System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(obj3B12ShippingOrderRequest.GetType());
				System.IO.MemoryStream  memStream = new System.IO.MemoryStream();
				x.Serialize(memStream, obj3B12ShippingOrderRequest);

				Byte[] byteArray;
				byteArray = memStream.ToArray();
				strPIP3B2 = System.Text.Encoding.ASCII.GetString(byteArray);

				lblOrderRequest.Text = "Shipping Order Request";
				txtShippingOrder.Text = strPIP3B2;
				Session["strPIP3B2"] = strPIP3B2;

				//The Acknowledgement XML textbox should not be visible during page load
				txtAcknowledgement.Visible = false;
				lblAcknowledgement.Visible = false;
				lblOrderConfirmation.Visible = false;
				lblOrderRequest.Text = "Shipping Order Request";
				
			}
			
			if (Request.QueryString["mode"]=="print")
			{
				System.Xml.XmlDocument objXMLDoc = new System.Xml.XmlDocument();
				objXMLDoc.LoadXml(strPIP3B2);
				Xml1.Visible = true;
				txtShippingOrder.Visible=false;
				txtAcknowledgement.Visible=false;
				lblOrderRequest.Visible = false;
				btnShipNotification.Visible = false;
				lblStatus.Visible = false;
				Xml1.Document = objXMLDoc;
				Xml1.TransformSource="ShippingNotificationRequest.xslt";
				lnkPrint.Text = "Print Shipping Notification Requests";
				lnkPrint.NavigateUrl = "javascript:window.print()";
				lnkPrint.ImageUrl="images/printer.gif";				
			}
			else
			{
				lnkPrint.Text = "Printer Friendly Version";
				lnkPrint.NavigateUrl = Request.Path + "?mode=print";
				Xml1.Visible = false;
				txtShippingOrder.Visible=true;
				txtShippingOrder.Text = strPIP3B2;				
				txtShippingOrder.Height = 600;
				txtShippingOrder.Width = 450;
				lnkPrint.ImageUrl="images/printer_friendly.gif";
			}

		}

		#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.btnShipNotification.Click += new System.EventHandler(this.btnShipNotification_Click);
			this.btnReceiptAck.Click += new System.EventHandler(this.btnReceiptAck_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		/// <summary>
		/// Invoked when the 'Send Shipping Notification' button is clicked.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void btnShipNotification_Click(object sender, System.EventArgs e)
		{
			//local variables.
			System.Xml.Serialization.XmlSerializer x;
			System.IO.MemoryStream  memStream;
			Byte[] byteArray;

			//Create Reference to Shipping Agent WebService.
			ShippingAgentWebService objServiceProxy = new ShippingAgentWebService();

			BusinessRequest objBusinessRequest = new BusinessRequest();						

			//The manufacturer is sending a 3B12 request to the shipping agent.
			//Set the Indicator code accordingly.
			objBusinessRequest.GlobalProcessIndicatorCode = "3B12";

			//The Pip3B12 Shipping Order Request needs to be sent to the shipping agent as 
			//part of the Business Request
			//The Pip3B12 Shipping Order Request object is being populated in Page_Load event above. Use the same
			objBusinessRequest.objPip = (Pip3B12ShippingOrderRequest)Session["PIP3B12ShippingOrderRequestObject"];
			
			//now update the database for EPC status.
			LogEPCEvent(TrackTraceDBManager.Constants.ShippingNotificationFromManufacturerToShippingAgent,
				objBusinessRequest.objPip.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
				objBusinessRequest.objPip.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
				"Manufacturer Location" ) ;
			
			//Invoke Shipping Agent WebService. You get back Business Response
			//which contains Acknowledgment and PIP3B12 Shipping Order Confirmation 
			BusinessResponse objBusinessResponse = objServiceProxy.ReceiveBusinessRequest(objBusinessRequest);

			if (objBusinessResponse.GlobalProcessIndicatorCode.ToUpper() == "3B12")
			{
				Pip3B12ShippingOrderConfirmation obj3B12ShippingOrderConfirmation;
				//Extract orderconfirmation object
				obj3B12ShippingOrderConfirmation = (Pip3B12ShippingOrderConfirmation)objBusinessResponse.objPip;
				//Extract Acknowledgment object
				ReceiptAcknowledgement objAcknowledgement = (ReceiptAcknowledgement)objBusinessResponse.objAcknowledgement;

				//display the received acknowledgment information. Make the acknowledgement text box visible
				//The Acknowledgement XML textbox should be visible after the Shipping order request has been sent.
				txtAcknowledgement.Visible = true;
				txtAcknowledgement.Height = 600;
				if (objAcknowledgement!= null)
				{
					//display the received Acknowledgment information.
					x = new System.Xml.Serialization.XmlSerializer(objAcknowledgement.GetType());
					memStream = new System.IO.MemoryStream();
					x.Serialize(memStream, objAcknowledgement);
				
					byteArray = memStream.ToArray();
					String strAcknowledgementXML = System.Text.Encoding.ASCII.GetString(byteArray);
					txtAcknowledgement.Text=strAcknowledgementXML;

					Session["ShippingAgentAcknowledgement"] = objAcknowledgement;
				}
				else
				{
					//This condition should not arise
					txtAcknowledgement.Text = "Acknowledgement was not received from Shipping Agent Web Service";
				}				
				//display the received shippingOrderConfirmation information and update
				//database with EPC status.
				if (obj3B12ShippingOrderConfirmation != null)
				{
					x = new System.Xml.Serialization.XmlSerializer(obj3B12ShippingOrderConfirmation.GetType());
					memStream = new System.IO.MemoryStream();
					x.Serialize(memStream, obj3B12ShippingOrderConfirmation);
				
					byteArray = memStream.ToArray();
					String strXML = System.Text.Encoding.ASCII.GetString(byteArray);
					txtShippingOrder.Text = strXML;	
		
					lblStatus.Text = obj3B12ShippingOrderConfirmation.GlobalDocumentFunctionCode;
					lblStatus.Text = "Received <b>Acknowledgment</b> and <b>Shipment Notification Confirmation</b> from Shipping Agent. <br> Send <b>Receipt Acknowledgment</b> and move to Shipping Agent Location.";
					btnShipNotification.Visible = false;
					lblOrderRequest.Visible = false;
					btnReceiptAck.Visible = true;

					//now update the database with Order confirmation status for EPCs
					LogEPCEvent(TrackTraceDBManager.Constants.ShippingOrderConfFromShippingAgentToManufacturer,
						obj3B12ShippingOrderConfirmation.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
						 ( ( ASNGenerationService.Pip3B2AdvanceShipmentNotification ) Session["ASN"] ).fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
						"Manufacturer Location" );
				}
				else 
				{
					lblStatus.Text = "ShippingOrderConfirmation object is EMPTY!";
				}
				lnkPrint.Visible = false;
				
			}
		}

		/// <summary>
		/// Invoked when the 'Send Receipt Acknowledgement' button is clicked.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void btnReceiptAck_Click(object sender, System.EventArgs e)
		{
			//Check if the session is still alive
			if (Session["ShippingAgentAcknowledgement"] == null)
			{
				Response.Redirect("OrderDetails.aspx?session_expired=true", true);
			}

			ShippingAgentWS.BusinessResponse objBusResp = new ShippingAgentWS.BusinessResponse();

			ReceiptAcknowledgement objReceiptAcknowledgementFromShippingAgent = (ReceiptAcknowledgement)Session["ShippingAgentAcknowledgement"];
			ReceiptAcknowledgement objReceiptAcknowledgementForShippingAgent = new ReceiptAcknowledgement();
			objReceiptAcknowledgementForShippingAgent.fromRole = new fromRole1();
			objReceiptAcknowledgementForShippingAgent.fromRole.PartnerRoleDescription = objReceiptAcknowledgementFromShippingAgent.toRole.PartnerRoleDescription;
			objReceiptAcknowledgementForShippingAgent.toRole = new toRole1();
			objReceiptAcknowledgementForShippingAgent.toRole.PartnerRoleDescription = objReceiptAcknowledgementFromShippingAgent.fromRole.PartnerRoleDescription;

			objBusResp.objAcknowledgement = objReceiptAcknowledgementForShippingAgent;
			objBusResp.GlobalProcessIndicatorCode = "ACK";

			ShippingAgentWebService objServiceProxy = new ShippingAgentWebService();
			bool ackSent = objServiceProxy.ReceiveBusinessAcknowledgement(objBusResp);

			if (ackSent)
			{
				Session["ManufacturerAcknowledgement"] = objReceiptAcknowledgementForShippingAgent;
			}
			else
			{
				Session["ManufacturerAcknowledgement"] = "Acknowledgement could not be sent to Shipping Agent";
			}
			
			//We are finished with Manufacturers Web Page. Move to Shipping Agent Page
			Response.Redirect("ShippingAgent.aspx", true);
		}

		/****************************************************************
		* Helper method to log EPC Events into ManufactuerDatabase.
		* Parameter: EPCEventId. TrackTraceDBManager.Constant denoting EPC
		*   event number as stored in EPCEventTypesTable.
		*****************************************************************/
		private void LogEPCEvent(int EPCEventId)
		{
			TrackTraceDBManager dm = new TrackTraceDBManager(TrackTraceDBManager.Constants.Manufacturer);
			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.

				// MS - commented
				// dm.AddEpcStatusInfo(strEPCNumber, EPCEventId, System.DateTime.Now);

				dm.AddFullEpcStatusInfo ( strEPCNumber, EPCEventId, DateTime.Now,
					obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
					obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
					obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName.FreeFormText.Value ) ;

				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);
				}
			}
		}

		/****************************************************************
		* Helper method to log EPC Events into ManufactuerDatabase.
		* Parameter: EPCEventId. TrackTraceDBManager.Constant denoting EPC
		*   event number as stored in EPCEventTypesTable.
		*****************************************************************/
		private void LogEPCEvent(int EPCEventId, string eventFrom, string eventTo, string location)
		{
			TrackTraceDBManager dm = new TrackTraceDBManager(TrackTraceDBManager.Constants.Manufacturer);
			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, 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, DateTime.Now,
						eventFrom, eventTo, location ) ;

				}
			}
		}

		/*************************************************************************
		* Helper function to create PIP3B12ShippingOrderRequest object and put into
		* session. ASN object from the session is used to fill the details.
		**************************************************************************/
		private void CreateShippingOrderPIP()
		{
			//Now create PIP3B12 ShippingOrderRequest object and populate values.
			obj3B12ShippingOrderRequest = new Pip3B12ShippingOrderRequest();		
			//Set the function code for the document
			obj3B12ShippingOrderRequest.GlobalDocumentFunctionCode = "Request";

			//obj3B12ShippingOrderRequest.ShippingOrder.OrderInformation.orderNumber.ProprietaryDocumentIdentifier = "SHREQ00001" ;
			obj3B12ShippingOrderRequest.ShippingOrder.OrderInformation.orderNumber.ProprietaryDocumentIdentifier = obj3B2ASN.thisDocumentIdentifier.ProprietaryDocumentIdentifier;

			obj3B12ShippingOrderRequest.ShippingOrder.OrderInformation.orderDateTime.DateTimeStamp = System.DateTime.UtcNow.ToUniversalTime().ToString("yyyyMMddThhmmss.000Z");
			
			//Set the ShipFrom information to the Manufacturer's information in the ASN			
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipFrom.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier = obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier;
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipFrom.PartnerDescription.BusinessDescription.GlobalSupplyChainCode = obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.GlobalSupplyChainCode.ToString();
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipFrom.PartnerDescription.ContactInformation.contactName.FreeFormText.Value = obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.contactName.FreeFormText.Value;
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipFrom.PartnerDescription.ContactInformation.EmailAddress = obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.EmailAddress;
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipFrom.PartnerDescription.ContactInformation.facsimileNumber.CommunicationsNumber = obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.facsimileNumber.CommunicationsNumber;
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipFrom.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber = obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber;

			//Set the ShipTo information to the Receiving Agent
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipTo.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier = "100011100";
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipTo.PartnerDescription.BusinessDescription.GlobalSupplyChainCode = "ReceivingAgent";
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipTo.PartnerDescription.ContactInformation.contactName.FreeFormText.Value = "Receiving Agent";
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipTo.PartnerDescription.ContactInformation.EmailAddress = "receiving@receivingagent.com";
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipTo.PartnerDescription.ContactInformation.facsimileNumber.CommunicationsNumber = "999-999-9999";
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.shipTo.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber = "111-111-1111";

			//FROM ROLE INFORMATION FOR THE SHIPMENT
			//Setup From Role Information
			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value = obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value;
			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.EmailAddress = obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.EmailAddress;
			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber = obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber;
			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.GlobalPartnerRoleClassificationCode = obj3B2ASN.fromRole.PartnerRoleDescription.GlobalPartnerRoleClassificationCode.ToString();
			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier = obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier;
			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.GlobalSupplyChainCode = obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.GlobalSupplyChainCode.ToString();
			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.PartnerDescription.GlobalPartnerClassificationCode = obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.GlobalPartnerClassificationCode.ToString();

			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation = new PhysicalLocation () ;
			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress = new PhysicalAddress () ;
			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName = new cityName () ;
			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName.FreeFormText = new Microsoft.RFIDLab.EPCISDemo.ShippingAgentWS.FreeFormText () ;
			obj3B12ShippingOrderRequest.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName.FreeFormText.Value = "Manufacturer Location" ;

			//TO ROLE INFORMATION FOR THE SHIPMENT
			//Setup To Role Information
			//This shipping Order request is being sent to the shipping agent.
			//The shipping agents information is populated in the ToRole part

			obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value = "Shipping Agent";
			obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.ContactInformation.EmailAddress = "ship@ShipEx.com";
			obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber = "111-111-1111";
			obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.GlobalPartnerRoleClassificationCode = "Shipper";
			obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier = "100000011";
			obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.PartnerDescription.GlobalPartnerClassificationCode = "Carrier";
			
			obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation = new PhysicalLocation () ;
			obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress = new PhysicalAddress () ;
			obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName = new cityName () ;
			obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName.FreeFormText = new Microsoft.RFIDLab.EPCISDemo.ShippingAgentWS.FreeFormText () ;
			obj3B12ShippingOrderRequest.toRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName.FreeFormText.Value = "Shipping Agent Location" ;

			obj3B12ShippingOrderRequest.thisDocumentGenerationDateTime.DateTimeStamp = System.DateTime.UtcNow.ToUniversalTime().ToString("yyyyMMddThhmmss.000Z");
			obj3B12ShippingOrderRequest.thisDocumentIdentifier.ProprietaryDocumentIdentifier =  "Shipping notification request from Manufacturer to Shipping Agent";

			ShipmentLineItem[] objShipmentLineItem = new ShipmentLineItem[1];
			int numberOfPallets = obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer.Length;
			objShipmentLineItem[0] = new ShipmentLineItem();
			objShipmentLineItem[0].ShippingContainer =  new ShippingContainer[numberOfPallets];
            			
			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentInformation.containerTotalCount.CountableAmount = numberOfPallets.ToString();
			
			ShippingContainer[] ShippingContainerCollection = new ShippingContainer[numberOfPallets];
			
			for (int i=0;i<numberOfPallets;i++)
			{				
				//objShipmentLineItem.GlobalProductUnitOfMeasureCode = obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i].MassPhysicalDimension.Weight.GlobalPhysicalUnitOfMeasureCode;
				ShippingContainer objShippingContainer = new ShippingContainer();
		
				objShippingContainer.GlobalPackageTypeCode = obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i].GlobalPackageTypeCode.ToString();
				objShippingContainer.shippingContainerIdentifier.ProprietarySerialIdentifier= obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i].shippingContainerIdentifier.ProprietarySerialIdentifier;
			
				int numberOfCasesInPallet = obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i].ShippingContainerItem.Length;
				ShippingContainerItem[] ShippingContainerItemCollection = new ShippingContainerItem[numberOfCasesInPallet];
				for (int j=0; j<numberOfCasesInPallet; j++)
				{
					ShippingContainerItem objShippingContainerItem = new ShippingContainerItem();
					//Store the CASE level EPC Information
					//A new element was added to the PIP3B12 ShippingContainerItem class to hold the case level EPC information
					objShippingContainerItem.shippingContainerItemIdentifier.ProprietarySerialIdentifier = obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i].ShippingContainerItem[j].shippingContainerItemIdentifier.ProprietarySerialIdentifier;
					ShippingContainerItemCollection[j] = objShippingContainerItem;					
				}
				objShippingContainer.ShippingContainerItem = ShippingContainerItemCollection;

				ShippingContainerCollection[i] = objShippingContainer;							
			}
			objShipmentLineItem[0].ShippingContainer = ShippingContainerCollection;

			obj3B12ShippingOrderRequest.ShippingOrder.ShipmentLineItem =  objShipmentLineItem;
			//store the PIP3B12 Object in Session.
			Session["PIP3B12ShippingOrderRequestObject"] = obj3B12ShippingOrderRequest;
		}

	}
}
