// Euroflorist.js
// Sending xml as string based on HTTP POST
//

function SendXml(l_url, l_username, l_password, p_string, ResultFile1, l_xsl_file)
{
		var SXH_PROXY_SET_PROXY = 2;
		ProxyServerIPAddress = "127.0.0.1:8888"

    var objXMLHTTP = new ActiveXObject("MSXML2.ServerXMLHTTP.6.0");

		//    objXMLHTTP.setProxy(SXH_PROXY_SET_PROXY, ProxyServerIPAddress);


    // ------------------------------------------------------------------------------------------------------------------------------
	    	WScript.Echo( "Requires Server UserName and Password." );

				objXMLHTTP.open("POST", l_url, false, l_username, l_password);

				objXMLHTTP.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
				objXMLHTTP.setRequestHeader ("SOAPAction", "urn:Afas.Profit.Services/GetData");

		  	WScript.Echo("Before send p_string");
		    objXMLHTTP.send(p_string);
		    if (objXMLHTTP.status==200)
		    {
		        WScript.Echo("Succeeded, status = " + objXMLHTTP.status);
		        oStream = new ActiveXObject("ADODB.Stream");
		        oStream.Open();
		        oStream.Type = 1; // adTypeBinary
		        oStream.Write(objXMLHTTP.responseBody); // responseText geeft encoding problemen!
		        oStream.SaveToFile (ResultFile1, 2); // adSaveCreateOverWrite
		        oStream.Close();
		        WScript.Echo("Before xmlResp");

		        var xmlResp = new ActiveXObject("MSXML2.DOMDocument.6.0");
		        xmlResp = objXMLHTTP.responseXML;
		        WScript.Echo("After xmlResp");

            if (l_xsl_file){

			        var xslDoc = new ActiveXObject("MSXML2.DOMDocument.6.0");
			        xslDoc.load(l_xsl_file);
			        WScript.Echo("After xslDoc");

			        var oStream = new ActiveXObject("ADODB.Stream");
			        oStream.Open();
			        oStream.Type = 2; //
			        oStream.Charset = "UTF-8"; //
			        // oStream.Write(xmlResp.transformNodeToObject(xslDoc, xmlResp));
			        oStream.WriteText(xmlResp.transformNode(xslDoc));
			
			        WScript.Echo("After transform");
			        oStream.SaveToFile (ResultFile1, 2); // adSaveCreateOverWrite
			        oStream.Close();
			       }


		    }
		    else
		    {
		        WScript.Echo("Server Error / Failure");
		        WScript.Echo("Status = " + objXMLHTTP.status);
		        WScript.Echo("Description = " + objXMLHTTP.statusText);
		    }

}

try
{


        WScript.Echo("Start sending string");

				var l_output_xml = WScript.Arguments(0);
				
				if (WScript.Arguments.length > 1){
					var l_xsl_file   = WScript.Arguments(1);
				}	
        // l_url = "https://profitweb.afasonline.nl/profitservices/getconnector.asmx";
        l_url = "https://71217.afasonlineconnector.nl/profitservices/appconnectorget.asmx"
				var l_username = "71217.facilitor";
				var l_password = "Profit1!";
				var l_environment = "O71217AA";

				var connectorId = "Facilitor_medewerkergegevens";


				my_string = ""
				+ "	<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:Afas.Profit.Services\">"
	   		+ "		<soapenv:Header/>"
	   		+ "		<soapenv:Body>"
				+ "      <urn:GetData>"
				+ "         <!--Optional:-->"
				+ "         <urn:token>"
				+ "         <![CDATA[<token><version>1</version><data>B8C2BBEBE1E54A08B103348A45FAD7A9F4A4F8B94C8C7FEE581BF18F859C712D</data></token>]]>"
				+ "         </urn:token>"
				+ "         <!--Optional:-->"
				+ "         <urn:connectorId>" + connectorId + "</urn:connectorId>"
				+ "         <!--New and mandatory:-->"
				+ "         <urn:skip>-1</urn:skip>"
				+ "         <urn:take>-1</urn:take>"
				+ "         <!--Optional:-->"
				+ "         <urn:filtersXml></urn:filtersXml>"
				+ "      </urn:GetData>"
				+ "   </soapenv:Body>"
				+ "	</soapenv:Envelope>";


 				//WScript.Echo("Webservice URL="+l_url);
 				//WScript.Echo("String="+my_string);


        SendXml(l_url, "AOL\\" + l_username, l_password, my_string, l_output_xml, l_xsl_file);
        WScript.Echo("End sending xml-order");
		    WScript.Quit(0);
}
catch (e)
{







    WScript.Echo("Fout in AFASOnline.js: " + e.description);
    WScript.Quit(1);
}

