<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
                xmlns:msxsl='urn:schemas-microsoft-com:xslt'
                xmlns:var='urn:var'
                xmlns:user='urn:user'
                version='1.0'
                exclude-result-prefixes='var user msxsl'>
  <xsl:output method='xml'
              indent='yes'
              omit-xml-declaration='yes'
              standalone='yes'/>

  <xsl:template match = "*">
<xsl:variable name="controlnum" select="format-number(user:GetControlNum(),'000000000')"/>
<PurchaseOrder>
	<ISA>
		<LineID>ISA</LineID>
		<AuthInfoQualifier>00</AuthInfoQualifier>
		<AuthInfo/>
		<SecurityInfoQualifier>00</SecurityInfoQualifier>
		<SecurityInfo/>
		<InterchangeIDQualifier1>01</InterchangeIDQualifier1>
		<InterchangeSenderID>05922333338</InterchangeSenderID>
		<InterchangeIDQualifier2>12</InterchangeIDQualifier2>
		<IntechangeReceiverID>51043332205</IntechangeReceiverID>
		<InterchangeDate>030207</InterchangeDate>
		<InterchangeTime>1015</InterchangeTime>
		<InterchangeControlID>U</InterchangeControlID>
		<InterchangeControlVerNum>00401</InterchangeControlVerNum>
		<InterchangeControlNum><xsl:value-of select="$controlnum"/></InterchangeControlNum>
		<AckRequested>1</AckRequested>
		<UsageIndicator>P</UsageIndicator>
		<ElementSeparator>&#62;</ElementSeparator>
		<GS>
			<LineID>GS</LineID>
			<FunctionalIDCode>PO</FunctionalIDCode>
			<ApplicationSenderCode>0544446348</ApplicationSenderCode>
			<ApplicationReceiverCode>5104222205</ApplicationReceiverCode>
			<Date><xsl:value-of select="user:GetX12Date()"/></Date>
			<Time><xsl:value-of select="user:GetX12Time()"/></Time>
			<GroupControlNum><xsl:value-of select="$controlnum"/></GroupControlNum>
			<ResponsibleAgencyCode>X</ResponsibleAgencyCode>
			<VersionIDCode>004010</VersionIDCode>
			<ST>
				<LineID>ST</LineID>
				<IDCode>850</IDCode>
				<ControlNum>20771</ControlNum>
				<BEG>
					<LineID>BEG</LineID>                                        <!-- What do I use here? -->
					<PurposeCode>00</PurposeCode>                       <!-- What do I use here? -->
					<TypeCode>RL</TypeCode>                                <!-- What do I use here? -->
					<PONum><xsl:value-of select="item_number"/></PONum>                                 <!-- What do I use here? -->
					<Date><xsl:value-of select="user:GetX12Date()"/></Date>
					<ContractNum></ContractNum>
				</BEG>
				<MSG>
				  <LineID>MSG</LineID>
				  <Message><xsl:value-of select="normalize-space(payment_memo)"/></Message>
				</MSG>
				<Address>
					<LineID>N1</LineID>
					<AddressType>ST</AddressType>
					<Name><xsl:value-of select="address_name"/></Name>
					<IDCodeQualifier></IDCodeQualifier>
					<IDCode></IDCode>
					<AdditionalName>
					  <LineID>N2</LineID>
					  <Name1><xsl:value-of select="payer_email"/></Name1>
					  <Name2></Name2>
					</AdditionalName>
					<AddressInfo>
						<LindID>N3</LindID>
						<AddressInfo1><xsl:value-of select="address_street"/></AddressInfo1>
						<AddressInfo2></AddressInfo2>
					</AddressInfo>
					<GeographicLocation>
						<LineID>N4</LineID>
						<CityName><xsl:value-of select="address_city"/></CityName>
						<StateCode><xsl:value-of select="address_state"/></StateCode>
						<PostalCode><xsl:value-of select="address_zip"/></PostalCode>
						<CountryCode><xsl:value-of select="address_country"/></CountryCode>
						<LocationQualifier>CC</LocationQualifier>
						<LocationIdentifier/>
					</GeographicLocation>
				</Address>
				<PO1>
					<LineID>PO1</LineID>
					<AssignedID>1</AssignedID>
					<QuantityOrdered><xsl:value-of select="item_qty"/></QuantityOrdered>
					<UnitMeasureCode>EA</UnitMeasureCode>
					<UnitPrice/>
					<BaseUnitPriceCode/>
					<!-- How do we figure out the true Part ID? -->
					<ProductIDQualifier1></ProductIDQualifier1>
					<ProductID1><xsl:value-of select="item_name"/></ProductID1>
					<ProductIDQualifier2></ProductIDQualifier2>
					<ProductID2></ProductID2>
					<ProductIDQualifier3/>
					<ProductID3/>
					<ProductIDQualifier4/>
					<ProductID4/>
					<ProductIDQualifier5/>
					<ProductID5/>
					<ProductIDQualifier6/>
					<ProductID6/>
					<ProductIDQualifier7/>
					<ProductID7/>
					<ProductIDQualifier8/>
					<ProductID8/>
					<ProductIDQualifier9/>
					<ProductID9/>
					<ProductIDQualifier10/>
					<ProductID10/>
					<PID>
						<LineID>PID</LineID>
						<DescType>F</DescType>
						<Description><xsl:value-of select="item_name"/></Description>
						<AgencyQualifierCode></AgencyQualifierCode>
						<ProductDescCode/>
						<Description1></Description1>
						<SurfaceCode/>
						<SourceSubqualifier/>
						<ResponseCode/>
						<LanguageCode/>
					</PID>
				</PO1>
				<CTT>
					<LineID>CTT</LineID>
					<NumberOfLineItems>1</NumberOfLineItems>
					<HashTotal/>
				</CTT>
			</ST>
			<SE>
				<LineID>SE</LineID>
				<NumberOfSegments>9</NumberOfSegments>
				<TransactionSetControlNum>20771</TransactionSetControlNum>
			</SE>
		</GS>
		<GE>
			<LineID>GE</LineID>
			<NumTransactionSets>1</NumTransactionSets>
			<GroupControlNum><xsl:value-of select="$controlnum"/></GroupControlNum>
		</GE>
	</ISA>
	<IEA>
		<LineID>IEA</LineID>
		<NumFunctionalGroups>1</NumFunctionalGroups>
		<InterchangeControlNum><xsl:value-of select="$controlnum"/></InterchangeControlNum>
	</IEA>
</PurchaseOrder>

  </xsl:template>

  <msxsl:script language='VBScript' implements-prefix='user'>
    <![CDATA[

Function GetX12Date( )
  GetX12Date = GetYear & GetMonth & GetDay
End Function

Function GetX12Time( )
  GetX12Time = GetHour & GetMinute
End Function

Function GetCurrentDate(  )
'  GetCurrentDate = FormatDateTime(Date, 0)
  GetCurrentDate = FormatDateTime(Now, 0)
End Function

Function GetCurrentTime(  )
'  GetCurrentTime = FormatDateTime(Time, 4)
  GetCurrentTime = FormatDateTime(Now, 4)
End Function

Function GetMonth()
'  Result = Month(Date)
  Result = Month(Now)
  If Len(Result) < 2 Then Result = "0" & Result
  GetMonth = Result
End Function

Function GetDay()
'  Result = Day(Date)
  Result = Day(Now)
  If Len(Result) < 2 Then Result = "0" & Result
  GetDay = Result
End Function

Function GetYear()
'	GetYear = Year(Date)
	GetYear = Year(Now)
End Function

Function GetHour()
'	GetHour = Hour(Time)
	GetHour = Hour(Now)
End Function

Function GetMinute()
'	GetMinute = Minute(Time)
	GetMinute = Minute(Now)
End Function

Function GetSecond()
'	GetSecond = Second(Time)
	GetSecond = Second(Now)
End Function

Function GetControlNum()
  GetControlNum = 1
'This is a counter in EDI X12, hardcoded to one, but if you are venturous, you can use the Registry to store an incrementor
'Simply uncomment this out, and make sure the key exists in the registry
'  set WSHShell = CreateObject("WScript.Shell")
'  RegValue = WSHShell.RegRead("HKEY_CURRENT_USER\SOFTWARE\SysOnyx\EDI\ControlNum")
'  WshShell.RegWrite "HKEY_CURRENT_USER\SOFTWARE\SysOnyx\EDI\ControlNum", RegValue + 1, "REG_DWORD"
'  GetInfinity = RegValue
End Function
    ]]>
  </msxsl:script>
</xsl:stylesheet>


