paulb@65 | 1 | function requestUpdate(url, fieldName, targetName, targetFieldNames, elementPath) { |
paulb@75 | 2 | |
paulb@127 | 3 | // If no field names exist as targets for the update, do not attempt to |
paulb@127 | 4 | // update them. |
paulb@127 | 5 | |
paulb@127 | 6 | if (targetFieldNames == "") { |
paulb@127 | 7 | return; |
paulb@127 | 8 | } |
paulb@127 | 9 | |
paulb@75 | 10 | // Note that XMLHttpRequest access may be denied if Mozilla believes that |
paulb@75 | 11 | // this resource's URL and the supplied URL are different. |
paulb@75 | 12 | |
paulb@75 | 13 | var xmlhttp = new XMLHttpRequest(); |
paulb@47 | 14 | xmlhttp.open("POST", url, false); |
paulb@56 | 15 | |
paulb@56 | 16 | // Find the value of the controlling field. |
paulb@56 | 17 | |
paulb@47 | 18 | var fieldValue = document.getElementsByName(fieldName)[0].value; |
paulb@56 | 19 | |
paulb@127 | 20 | // Send the controlling field value. |
paulb@127 | 21 | |
paulb@127 | 22 | var requestBody = (fieldName + "=" + fieldValue); |
paulb@127 | 23 | |
paulb@60 | 24 | // Process each target field name. |
paulb@60 | 25 | |
paulb@60 | 26 | var targetFieldNameArray = targetFieldNames.split(","); |
paulb@60 | 27 | |
paulb@60 | 28 | // Add the values of the dependent fields. |
paulb@60 | 29 | |
paulb@60 | 30 | for (var i = 0; i < targetFieldNameArray.length; i++) { |
paulb@60 | 31 | var targetFieldName = targetFieldNameArray[i]; |
paulb@56 | 32 | |
paulb@60 | 33 | // Find the values of the target field. |
paulb@60 | 34 | |
paulb@60 | 35 | var targetFieldValue; |
paulb@60 | 36 | var targetFieldNodes = document.getElementsByName(targetFieldName); |
paulb@60 | 37 | for (var v = 0; v < targetFieldNodes.length; v++) { |
paulb@60 | 38 | targetFieldValue = targetFieldNodes[v].value; |
paulb@60 | 39 | requestBody += ("\r\n" + targetFieldName + "=" + targetFieldValue); |
paulb@60 | 40 | } |
paulb@127 | 41 | |
paulb@127 | 42 | // NOTE: Konqueror hack: disable fields. |
paulb@127 | 43 | |
paulb@127 | 44 | disableFields(targetFieldName); |
paulb@65 | 45 | } |
paulb@60 | 46 | |
paulb@65 | 47 | // Add the element path specification. |
paulb@65 | 48 | |
paulb@65 | 49 | requestBody += ("\r\nelement-path=" + elementPath); |
paulb@56 | 50 | |
paulb@56 | 51 | // Load the remote document with the given parameters sent as text in the request body. |
paulb@56 | 52 | |
paulb@60 | 53 | xmlhttp.send(requestBody); |
paulb@56 | 54 | |
paulb@56 | 55 | // Parse the result document. |
paulb@56 | 56 | |
paulb@44 | 57 | var newDocument = Sarissa.getDomDocument(); |
paulb@75 | 58 | newDocument = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml"); |
paulb@56 | 59 | |
paulb@56 | 60 | // Find the definition of the affected field in the result document. |
paulb@56 | 61 | |
paulb@47 | 62 | var newElement = newDocument.getElementById(targetName); |
paulb@44 | 63 | var targetElement = document.getElementById(targetName); |
paulb@56 | 64 | |
paulb@56 | 65 | // Insert the new definition into the current document. |
paulb@44 | 66 | |
paulb@44 | 67 | if (newElement != null && targetElement != null) { |
paulb@44 | 68 | var importedElement = document.importNode(newElement, true); |
paulb@44 | 69 | targetElement.parentNode.replaceChild(importedElement, targetElement); |
paulb@44 | 70 | //importedElement.setAttribute("style", "background-color:red;"); |
paulb@44 | 71 | } |
paulb@127 | 72 | |
paulb@127 | 73 | // NOTE: Test Konqueror bug. |
paulb@127 | 74 | |
paulb@127 | 75 | //showMismatch(targetFieldNameArray); |
paulb@44 | 76 | } |
paulb@127 | 77 | |
paulb@127 | 78 | function disableFields(targetFieldName) { |
paulb@127 | 79 | |
paulb@127 | 80 | for (var i = 0; i < document.forms.length; i++) { |
paulb@127 | 81 | var form = document.forms[i]; |
paulb@127 | 82 | for (var j = 0; j < form.elements.length; j++) { |
paulb@127 | 83 | if (form.elements[j].name == targetFieldName) { |
paulb@127 | 84 | form.elements[j].name = ""; |
paulb@127 | 85 | } |
paulb@127 | 86 | } |
paulb@127 | 87 | } |
paulb@127 | 88 | } |
paulb@127 | 89 | |
paulb@127 | 90 | function showMismatch(targetFieldNameArray) { |
paulb@127 | 91 | |
paulb@127 | 92 | // Show how the number of field elements with a given name can be different |
paulb@127 | 93 | // from the number known to the DOM Level 0 part of the API. |
paulb@127 | 94 | |
paulb@127 | 95 | for (var h = 0; h < targetFieldNameArray.length; h++) { |
paulb@127 | 96 | var targetFieldName = targetFieldNameArray[h]; |
paulb@127 | 97 | var targetFieldNodes = document.getElementsByName(targetFieldName); |
paulb@127 | 98 | alert("Nodes for " + targetFieldName + ": " + targetFieldNodes.length); |
paulb@127 | 99 | |
paulb@127 | 100 | var count = 0; |
paulb@127 | 101 | for (var i = 0; i < document.forms.length; i++) { |
paulb@127 | 102 | var form = document.forms[i]; |
paulb@127 | 103 | for (var j = 0; j < form.elements.length; j++) { |
paulb@127 | 104 | if (form.elements[j].name == targetFieldName) { |
paulb@127 | 105 | count++; |
paulb@127 | 106 | } |
paulb@127 | 107 | } |
paulb@127 | 108 | } |
paulb@127 | 109 | alert("Fields for " + targetFieldName + ": " + count); |
paulb@127 | 110 | } |
paulb@127 | 111 | } |