<%@language = "JavaScript" %>
<%
// Lees de vraagstelling in die via XML binnenkomt
var objReq = new ActiveXObject("MSXML2.DOMDocument.6.0");
objReq.load(Request);

if (objReq.parseError.errorCode != 0)
{
   var myErr = objReq.parseError;
   Response.Write("Error parsing request XML: " + myErr.reason);
   Response.End;
}

// Zoek het partnummer
var strQuery = "stock_request/part/number";
objPart = objReq.selectSingleNode(strQuery);
if (!objPart)
{
   Response.Write(strQuery + " not found in XML");
   Response.End;
}

// Prepare result XML
//Response.Write("Part nummer : " + objPart.text);
//Response.End;

var l_voorraad = bepaal_voorraad_van_artikel(objPart.text);
var xmlDoc = new ActiveXObject("MSXML2.DOMDocument.6.0");

xmlDoc.appendChild(xmlDoc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""))

rootElement = xmlDoc.createElement("stock_response");
    elementPart = xmlDoc.createElement("part");
        elementNumber = xmlDoc.createElement("number");
            elementNumberText = xmlDoc.createTextNode(objPart.text);
            elementNumber.appendChild(elementNumberText);
        elementPart.appendChild(elementNumber);
        elementAmount = xmlDoc.createElement("amount");
            elementAmountText = xmlDoc.createTextNode(l_voorraad);
            elementAmount.appendChild(elementAmountText);
        elementPart.appendChild(elementAmount);
        elementInfo = xmlDoc.createElement("infotext"); // infohtml bestaat ook
            elementInfoText = xmlDoc.createTextNode("Voorraad opgevraagd op "+(new Date()) +
                                                    "\ndoor " + Request.ServerVariables("REMOTE_HOST") +
                                                    "\nvia: " + Request.ServerVariables("HTTP_HOST") + Request.ServerVariables("URL"));
            elementInfo.appendChild(elementInfoText);
        elementPart.appendChild(elementInfo);
        elementStockTime = xmlDoc.createElement("time");
            elementStockTimeText = xmlDoc.createTextNode(new Date());
            elementStockTime.appendChild(elementStockTimeText);
        elementPart.appendChild(elementStockTime);
    rootElement.appendChild(elementPart);
xmlDoc.appendChild(rootElement);

Response.ContentType = "text/xml";
Response.Write(xmlDoc.xml);

function bepaal_voorraad_van_artikel(p_artnr){
  var i = 0;
  var l_digits = "0123456789";
  	var l_result = "";


	while (i<2 && p_artnr != ""){
		l_char = p_artnr.charAt(0);
		if (l_digits.indexOf(l_char, 0) != -1){
			l_result = l_result + l_char;
		}
		i=i+1;
		p_artnr = p_artnr.substr(1);
	}
	if (l_result == ""){
		l_result = "0"
	}
	return l_result;
}

%>