paulb@65 | 1 | function requestUpdate(url, fieldName, targetName, targetFieldNames, elementPath) { |
paulb@75 | 2 | |
paulb@75 | 3 | // Note that XMLHttpRequest access may be denied if Mozilla believes that |
paulb@75 | 4 | // this resource's URL and the supplied URL are different. |
paulb@75 | 5 | |
paulb@75 | 6 | var xmlhttp = new XMLHttpRequest(); |
paulb@47 | 7 | xmlhttp.open("POST", url, false); |
paulb@56 | 8 | |
paulb@56 | 9 | // Find the value of the controlling field. |
paulb@56 | 10 | |
paulb@47 | 11 | var fieldValue = document.getElementsByName(fieldName)[0].value; |
paulb@56 | 12 | |
paulb@60 | 13 | // Process each target field name. |
paulb@60 | 14 | |
paulb@60 | 15 | var targetFieldNameArray = targetFieldNames.split(","); |
paulb@60 | 16 | |
paulb@60 | 17 | // Send the controlling field value. |
paulb@60 | 18 | |
paulb@60 | 19 | var requestBody = (fieldName + "=" + fieldValue); |
paulb@60 | 20 | |
paulb@60 | 21 | // Add the values of the dependent fields. |
paulb@60 | 22 | |
paulb@60 | 23 | for (var i = 0; i < targetFieldNameArray.length; i++) { |
paulb@60 | 24 | var targetFieldName = targetFieldNameArray[i]; |
paulb@56 | 25 | |
paulb@60 | 26 | // Find the values of the target field. |
paulb@60 | 27 | |
paulb@60 | 28 | var targetFieldValue; |
paulb@60 | 29 | var targetFieldNodes = document.getElementsByName(targetFieldName); |
paulb@60 | 30 | for (var v = 0; v < targetFieldNodes.length; v++) { |
paulb@60 | 31 | targetFieldValue = targetFieldNodes[v].value; |
paulb@60 | 32 | requestBody += ("\r\n" + targetFieldName + "=" + targetFieldValue); |
paulb@60 | 33 | } |
paulb@65 | 34 | } |
paulb@60 | 35 | |
paulb@65 | 36 | // Add the element path specification. |
paulb@65 | 37 | |
paulb@65 | 38 | requestBody += ("\r\nelement-path=" + elementPath); |
paulb@56 | 39 | |
paulb@56 | 40 | // Load the remote document with the given parameters sent as text in the request body. |
paulb@56 | 41 | |
paulb@60 | 42 | xmlhttp.send(requestBody); |
paulb@56 | 43 | |
paulb@56 | 44 | // Parse the result document. |
paulb@56 | 45 | |
paulb@44 | 46 | var newDocument = Sarissa.getDomDocument(); |
paulb@75 | 47 | newDocument = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml"); |
paulb@56 | 48 | |
paulb@56 | 49 | // Find the definition of the affected field in the result document. |
paulb@56 | 50 | |
paulb@47 | 51 | var newElement = newDocument.getElementById(targetName); |
paulb@44 | 52 | var targetElement = document.getElementById(targetName); |
paulb@56 | 53 | |
paulb@56 | 54 | // Insert the new definition into the current document. |
paulb@44 | 55 | |
paulb@44 | 56 | if (newElement != null && targetElement != null) { |
paulb@44 | 57 | var importedElement = document.importNode(newElement, true); |
paulb@44 | 58 | targetElement.parentNode.replaceChild(importedElement, targetElement); |
paulb@44 | 59 | //importedElement.setAttribute("style", "background-color:red;"); |
paulb@44 | 60 | } |
paulb@44 | 61 | } |