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 System.Configuration;
//for wse
//using Microsoft.Web.Services2.Security;
//using Microsoft.Web.Services2.Security.Tokens;
//using Microsoft.Web.Services2.Security.X509;
//for EPC Event
using Microsoft.RFIDLab.EPCISDemo.EPCEvent;

namespace Microsoft.RFIDLab.EPCISDemo
{
	/// <summary>
	/// Displays the ASN to the manufacturer.
	/// </summary>
	public class DisplayASN : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Xml Xml1;
		protected System.Web.UI.WebControls.Label lblStatus;
		protected System.Web.UI.WebControls.TextBox txtASNXML;
		protected System.Web.UI.WebControls.HyperLink lnkPrint;		
		private ASNGenerationService.Pip3B2AdvanceShipmentNotification objProxy;
		private String orderNumber;
		protected System.Web.UI.WebControls.Button btnSendASNToReceiver;
		protected System.Web.UI.WebControls.Button btnBeginShipping;

		//WSE 2.0
		// Replace the key id below with your own certificate Id in Base64String here
		// You can get these values for any cert with the WSE 2.0 Certificate Tool
		public static string ClientBase64KeyId = ConfigurationSettings.AppSettings["ClientBase64KeyId"];
	
		/// <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)
		{
			if ( ! Page.IsPostBack )
			{	
				//Reset EPCEventStatus and EPCEventTypesTable Tables for MicrosoftEPCISManufacturer, MicrosoftEPCISShippingAgent,
				//MicrosoftEPCISReceivingAgent and MicrosoftEPCISReceiver Databases. 
				Common.TrackTraceDBManager dbMgr = new Common.TrackTraceDBManager();
				dbMgr.ResetDatabase();
				
				ASNGenerationService.ASNGenerationService proxy = new ASNGenerationService.ASNGenerationService();
				orderNumber = (String)Session["OrderNumber"];
				

				//Check if security is enabled
				if (ConfigurationSettings.AppSettings["EnableWSSecurity"].ToLower() == "true")
				{
//					X509SecurityToken token = null;
//
//					//Open the CurrentUser Certificate Store and try MyStore only
//					//X509CertificateStore store = X509CertificateStore.CurrentUserStore( X509CertificateStore.MyStore);
//					X509CertificateStore store = X509CertificateStore.LocalMachineStore( X509CertificateStore.MyStore );
//				
//					token = RetrieveTokenFromStore(store, ClientBase64KeyId);
//				
//					// Add the signature element to a security section on the request
//					// to sign the request
//					proxy.RequestSoapContext.Security.Tokens.Add( token );
//					proxy.RequestSoapContext.Security.Elements.Add( new MessageSignature( token ) );
				}
				
				objProxy = proxy.GenerateASN(orderNumber);
				Session["ASN"] = objProxy;
				
				btnBeginShipping.Visible = false;
			}
			else
			{
				objProxy = (ASNGenerationService.Pip3B2AdvanceShipmentNotification)Session["ASN"];
			}

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

			Byte[] byteArray;
			byteArray = memStream.ToArray();
			String strXML = System.Text.Encoding.ASCII.GetString(byteArray);
			
			if (Request.QueryString["mode"]=="print")
			{
				System.Xml.XmlDocument objXMLDoc = new System.Xml.XmlDocument();
				objXMLDoc.LoadXml(strXML);
				Xml1.Visible = true;
				txtASNXML.Visible=false;
				Xml1.Document = objXMLDoc;
				Xml1.TransformSource="ASN.xslt";
				lnkPrint.Text = "Print ASN";
				lnkPrint.NavigateUrl = "javascript:window.print()";
				lnkPrint.ImageUrl="images/printer.gif";
				btnSendASNToReceiver.Visible = false;
			}
			else
			{
				lnkPrint.Text = "Printer Friendly Version";
				lnkPrint.NavigateUrl = Request.Path + "?mode=print";
				Xml1.Visible = false;
				txtASNXML.Visible=true;
				txtASNXML.Text = strXML;
				txtASNXML.Height = 600;
				lnkPrint.ImageUrl="images/printer_friendly.gif";
			}

			lblStatus.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.btnSendASNToReceiver.Click += new System.EventHandler(this.btnSendASNToReceiver_Click);
			this.btnBeginShipping.Click += new System.EventHandler(this.btnBeginShipping_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		/// <summary>
		/// Invoked on click of the 'Send ASN to Receiver' button.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void btnSendASNToReceiver_Click(object sender, System.EventArgs e)
		{
			
			ASNReceivingService.ASNReceivingService ASNReceivingProxy = new ASNReceivingService.ASNReceivingService();

			//Check if security is enabled
			if (ConfigurationSettings.AppSettings["EnableWSSecurity"].ToLower() == "true")
			{
//				X509SecurityToken token = null;
//
//				X509CertificateStore store = X509CertificateStore.LocalMachineStore( X509CertificateStore.MyStore );
//				
//				token = RetrieveTokenFromStore(store, ClientBase64KeyId);
//
//				// Add the signature element to a security section on the request
//				// to sign the request
//				ASNReceivingProxy.RequestSoapContext.Security.Tokens.Add( token );
//				ASNReceivingProxy.RequestSoapContext.Security.Elements.Add(new MessageSignature( token ));				
			}

            objProxy = (ASNGenerationService.Pip3B2AdvanceShipmentNotification)Session["ASN"];
			
			System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(objProxy.GetType(),"http://www.microsoft.com/RFIDLab/EPCISDemo");
			System.IO.MemoryStream  memStream = new System.IO.MemoryStream();

			x.Serialize(memStream, objProxy);
			
			Byte[] byteArray;
			byteArray = memStream.ToArray();
			String strXML = System.Text.Encoding.ASCII.GetString(byteArray);

			// MS
			ArrayList tagList = GetTagList () ;
			object[] array = tagList.ToArray () ;
			Common.TrackTraceDBManager manager = new Microsoft.RFIDLab.EPCISDemo.Common.TrackTraceDBManager ( Common.TrackTraceDBManager.Constants.Manufacturer ) ;

			// MS - commented
			// manager.AddEpcStatusInfo ( array, Common.TrackTraceDBManager.Constants.AsnFromManufacturerToReceiver, DateTime.Now ) ;

			ReceiverWS.Pip3B2AdvanceShipmentNotification asn = new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.Pip3B2AdvanceShipmentNotification () ;
			x = new System.Xml.Serialization.XmlSerializer ( asn.GetType (), "http://www.microsoft.com/RFIDLab/EPCISDemo" ) ;
			memStream = new System.IO.MemoryStream ( System.Text.Encoding.ASCII.GetBytes ( strXML ) ) ;
			// asn = ( Microsoft.RFIDLab.EPCISDemo.ReceiverWS.Pip3B2AdvanceShipmentNotification ) x.Deserialize ( memStream ) ;
			asn = CreateAdvanceShipmentPIP () ;
			manager.AddFullEpcStatusInfo ( array,
				Common.TrackTraceDBManager.Constants.AsnFromManufacturerToReceiver,
				DateTime.Now, asn.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
				asn.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
				"Manufacturer Location" ) ;

			ReceiverWS.BusinessRequest request = new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.BusinessRequest () ;
			request.GlobalProcessIndicatorCode = "3B2" ;
			request.objPip = asn ;
			ReceiverWS.ReceiptAcknowledgement acknowledgement =
				new ReceiverWS.ReceiverWebService ().ReceiveBusinessRequest (
					request ).objAcknowledgement ;

			// MS - commented
			// manager.AddEpcStatusInfo ( GetTagList ().ToArray (), Common.TrackTraceDBManager.Constants.AcknowledgementFromReceiverToManufacturer, DateTime.Now ) ;

			manager.AddFullEpcStatusInfo ( array,
				Common.TrackTraceDBManager.Constants.AcknowledgementFromReceiverToManufacturer,
				DateTime.Now, asn.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
				asn.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value,
				"Manufacturer Location" ) ;

			ASNReceivingService.ReceiptAcknowledgement objAcknowledgement= ASNReceivingProxy.ReceiveASN(strXML);
			
			x = new System.Xml.Serialization.XmlSerializer(objAcknowledgement.GetType(), "http://www.microsoft.com/RFIDLab/EPCISDemo");
			memStream = new System.IO.MemoryStream();

			x.Serialize(memStream, objAcknowledgement);
			
			byteArray = memStream.ToArray();
			strXML = System.Text.Encoding.ASCII.GetString(byteArray);

			Xml1.Visible = false;
			txtASNXML.Visible=true;
			txtASNXML.Text = strXML;
			txtASNXML.Height = 600;
			lblStatus.Visible = true;
			btnSendASNToReceiver.Enabled = false;
			
			lblStatus.Text = "ASN sent.<BR>";
			
			lblStatus.Text += "The following acknowledgement was received from the receiver.";
			btnBeginShipping.Visible = true;
		}	
		
		/// <summary>
		/// Gets a list of tags present in the ASN.
		/// </summary>
		/// <returns>Array-list containing all EPC tags. </returns>
		private ArrayList GetTagList ()
		{
			ArrayList result = new ArrayList () ;
			ASNGenerationService.Pip3B2AdvanceShipmentNotification objPipNotification
				= ( ASNGenerationService.Pip3B2AdvanceShipmentNotification ) Session["ASN"] ;

			if ( objPipNotification != null ) // if the ASN is not null
			{
				if ( objPipNotification.AdvancedShipmentNotification == null ||
					objPipNotification.AdvancedShipmentNotification.Shipment == null ||
					objPipNotification.AdvancedShipmentNotification.Shipment.ShippingContainer == null )
					Response.Redirect ( "OrderDetails.aspx?session_expired=true" ) ; // end if

				// for each ASN shipping container in the ASN
				foreach ( ASNGenerationService.ShippingContainer container in 
					objPipNotification.AdvancedShipmentNotification.Shipment.ShippingContainer )
				{
					// for each shipping container-item in the container
					// get the proprietary serial-identifier
					string propriertarySerialIdentifer =
						container.shippingContainerIdentifier.ProprietarySerialIdentifier ;
					int index = propriertarySerialIdentifer.IndexOf ( ' ' ) ;
					string epc = null ;

					// set the EPC from the serial-identifier
					if ( index >= 0 )
						epc = propriertarySerialIdentifer.Substring ( 0,
							index  ) ; // end if 

					else
						epc = propriertarySerialIdentifer ; // end else

					result.Add ( epc ) ; // add it to the list

					foreach ( ASNGenerationService.ShippingContainerItem item in
						container.ShippingContainerItem )
					{
						// get the proprietary serial-identifier
						propriertarySerialIdentifer =
							item.shippingContainerItemIdentifier.ProprietarySerialIdentifier ;
						index = propriertarySerialIdentifer.IndexOf ( ' ' ) ;
						epc = null ;

						// set the EPC from the serial-identifier
						if ( index >= 0 )
							epc = propriertarySerialIdentifer.Substring ( 0,
								index  ) ; // end if 

						else
							epc = propriertarySerialIdentifer ; // end else

						result.Add ( epc ) ; // add it to the list

					} // end nested for

				} // end outer for

			} // end if

			return result ; // return the result

		} // end of GetTagList ()

//		/// <summary>
//		/// Gets an X-509 security-token.
//		/// </summary>
//		/// <param name="store">The store to retrieve from. </param>
//		/// <param name="keyIdentifier">The key to identify. </param>
//		/// <returns>Instance of the security-token. </returns>
//		private static X509SecurityToken RetrieveTokenFromStore (
//			X509CertificateStore store, string keyIdentifier )
//		{
//			// if the store specified is null, throw an exception 
//			if ( store == null )
//				throw new ArgumentNullException ( "store" ) ; // end if
//            
//			X509SecurityToken token = null ;
//
//			try 
//			{
//				if ( store.OpenRead () ) // if the store gets opened for reading
//				{
//					/* Place the key-ID of the certificate in a byte-array.
//					 * This key-ID represents the Wse2Quickstart certificate
//					 * included with the WSE 2.0 Quickstarts.
//					 * ClientBase64KeyId is defined in the ClientBase.AppBase
//					 * class */
//					X509CertificateCollection certs =
//						store.FindCertificateByKeyIdentifier (
//						Convert.FromBase64String ( keyIdentifier ) ) ;
//
//					// if a certificate is found, get the first
//					if ( certs.Count > 0 )						
//						token = new X509SecurityToken ( (
//							( X509Certificate ) certs[0] ) ) ; // end if
//
//					else // no certificates found - throw an exception
//						throw new ApplicationException (
//							"X509-certificate not found in local store." ) ;
//					// end else
//
//				} // end if
//
//			} // end of try block
//
//			finally
//			{
//				if ( store != null ) // if the store is not null, close it
//					store.Close () ; // end if
//
//			} // end of finally block
//  
//			return token ; // return the token
//
//		} // end of RetrieveTokenFromStore ()

		/// <summary>
		/// Invoked when the 'Begin Shipping' button is clicked.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void btnBeginShipping_Click ( object sender, System.EventArgs e )
		{
			Response.Redirect ( "InitiateShipping.aspx", true ) ;
		
		} // end of btnBeginShipping_Click ()

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

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

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

			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value =
				objAsn.fromRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value ;
			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.EmailAddress					=
				objAsn.fromRole.PartnerRoleDescription.ContactInformation.EmailAddress ;
			obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber	=
				objAsn.fromRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber ;
			/* obj3B2ASN.fromRole.PartnerRoleDescription.ContactInformation.facsimileNumber.CommunicationsNumber	=
				objAsn.fromRole.PartnerRoleDescription.ContactInformation.facsimileNumber.CommunicationsNumber ; */
			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier =
				objAsn.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier ;
			/* obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName.FreeFormText.Value =
				objAsn.fromRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName.FreeFormText.Value ; */

			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation = new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.PhysicalLocation () ;
			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress = new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.PhysicalAddress () ;
			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName = new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.cityName () ;
			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName.FreeFormText = new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.FreeFormText () ;
			obj3B2ASN.fromRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName.FreeFormText.Value = "Receiver Location" ;
			
			//Initialize toRole and fill the values.
			obj3B2ASN.toRole = new ReceiverWS.toRole();
			obj3B2ASN.toRole.PartnerRoleDescription = new ReceiverWS.PartnerRoleDescription();
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation = new ReceiverWS.ContactInformation();
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.contactName = new ReceiverWS.contactName();
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText = new ReceiverWS.FreeFormText();
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.telephoneNumber = new ReceiverWS.telephoneNumber();
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.facsimileNumber = new ReceiverWS.facsimileNumber();
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription = new ReceiverWS.PartnerDescription();
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription = new ReceiverWS.BusinessDescription();
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName = new ReceiverWS.businessName();
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName.FreeFormText = new ReceiverWS.FreeFormText();

			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value	=
				objAsn.toRole.PartnerRoleDescription.ContactInformation.contactName.FreeFormText.Value ;
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.EmailAddress						=
				objAsn.toRole.PartnerRoleDescription.ContactInformation.EmailAddress ;
			obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber		=
				objAsn.toRole.PartnerRoleDescription.ContactInformation.telephoneNumber.CommunicationsNumber ;
			/* obj3B2ASN.toRole.PartnerRoleDescription.ContactInformation.facsimileNumber.CommunicationsNumber		=
				objAsn.toRole.PartnerRoleDescription.ContactInformation.facsimileNumber.CommunicationsNumber ; */
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier =
				objAsn.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier ;
			/* obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName.FreeFormText.Value  =
				objAsn.toRole.PartnerRoleDescription.PartnerDescription.BusinessDescription.businessName.FreeFormText.Value ; */

			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation = new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.PhysicalLocation () ;
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress = new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.PhysicalAddress () ;
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName = new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.cityName () ;
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName.FreeFormText = new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.FreeFormText () ;
			obj3B2ASN.toRole.PartnerRoleDescription.PartnerDescription.PhysicalLocation.PhysicalAddress.cityName.FreeFormText.Value = "Receiver Location" ;
			
			//Initialize and fillup Originator information
			obj3B2ASN.AdvancedShipmentNotification = new ReceiverWS.AdvancedShipmentNotification();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner = new ReceiverWS.OriginatingPartner();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom = new ReceiverWS.shipFrom();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription = new ReceiverWS.PartnerDescription();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription = new ReceiverWS.BusinessDescription();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.businessName = new ReceiverWS.businessName();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.businessName.FreeFormText = new ReceiverWS.FreeFormText();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation = new ReceiverWS.ContactInformation();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.contactName = new ReceiverWS.contactName();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.contactName.FreeFormText = new ReceiverWS.FreeFormText();
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.telephoneNumber = new ReceiverWS.telephoneNumber();
			
			/* obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.businessName.FreeFormText.Value =
				objAsn.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.businessName.FreeFormText.Value ; */
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier			=
				objAsn.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.BusinessDescription.GlobalBusinessIdentifier ;
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.contactName.FreeFormText.Value		=
				objAsn.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.contactName.FreeFormText.Value ;
			obj3B2ASN.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber =
				objAsn.AdvancedShipmentNotification.OriginatingPartner.shipFrom.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber ;

			//file up destination receiver information.
			obj3B2ASN.AdvancedShipmentNotification.Shipment = new ReceiverWS.Shipment();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner = new ReceiverWS.ReceivingPartner();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo = new ReceiverWS.shipTo();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription = new ReceiverWS.PartnerDescription();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation = new ReceiverWS.ContactInformation();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.contactName = new ReceiverWS.contactName();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.contactName.FreeFormText = new ReceiverWS.FreeFormText();
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.telephoneNumber = new ReceiverWS.telephoneNumber();
			
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.contactName.FreeFormText.Value	=
				objAsn.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.contactName.FreeFormText.Value ;
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber =
				objAsn.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.telephoneNumber.CommunicationsNumber ;
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.EmailAddress						=
				objAsn.AdvancedShipmentNotification.Shipment.ReceivingPartner.shipTo.PartnerDescription.ContactInformation.EmailAddress ;
			obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer =
				new ReceiverWS.ShippingContainer[objAsn.AdvancedShipmentNotification.Shipment.ShippingContainer.Length] ;

			int i = 0 ;
			// for each ASN shipping container in the ASN
			foreach ( ASNGenerationService.ShippingContainer container in 
				objAsn.AdvancedShipmentNotification.Shipment.ShippingContainer )
			{
				// get the proprietary serial-identifier
				string propriertarySerialIdentifer =
					container.shippingContainerIdentifier.ProprietarySerialIdentifier ;
				obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i] = new ReceiverWS.ShippingContainer () ;
				obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i].shippingContainerIdentifier =
					new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.shippingContainerIdentifier () ;
				obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i].shippingContainerIdentifier.ProprietarySerialIdentifier
					= propriertarySerialIdentifer ;

				int index = propriertarySerialIdentifer.IndexOf ( ' ' ) ;
				string epc = null ;

				// set the EPC from the serial-identifier
				if ( index >= 0 )
					epc = propriertarySerialIdentifer.Substring ( 0,
						index  ) ; // end if

				else
					epc = propriertarySerialIdentifer ; // end else

				obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i].ShippingContainerItem =
					new ReceiverWS.ShippingContainerItem[objAsn.AdvancedShipmentNotification.Shipment.ShippingContainer[i].ShippingContainerItem.Length] ;
				int j = 0 ;

				// for each shipping container-item in the container
				foreach ( ASNGenerationService.ShippingContainerItem item in
					container.ShippingContainerItem )
				{
					// get the proprietary serial-identifier
					propriertarySerialIdentifer =
						item.shippingContainerItemIdentifier.ProprietarySerialIdentifier ;
					obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i].ShippingContainerItem[j] = new ReceiverWS.ShippingContainerItem () ;
					obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i].ShippingContainerItem[j].shippingContainerItemIdentifier =
						new Microsoft.RFIDLab.EPCISDemo.ReceiverWS.shippingContainerItemIdentifier () ;
					obj3B2ASN.AdvancedShipmentNotification.Shipment.ShippingContainer[i].ShippingContainerItem[j++].shippingContainerItemIdentifier.ProprietarySerialIdentifier
						= propriertarySerialIdentifer ;

					index = propriertarySerialIdentifer.IndexOf ( ' ' ) ;
					epc = null ;

					// set the EPC from the serial-identifier
					if ( index >= 0 )
						epc = propriertarySerialIdentifer.Substring ( 0,
							index  ) ; // end if

					else
						epc = propriertarySerialIdentifer ; // end else

				} // end nested for				
	
				i ++ ;

			} // end outer for

			return obj3B2ASN ;

		} //end of PIP3B2ASN creation routine.

	} // class definition over

} // end of namespace
