/** * javascript Calculator class. * * @author: Bart Plasmans * @version 1.0, 26-2-2007 * * This class can be included by websites that have calculators. * The website can give its variables to this calculator, * and call the calculate() function to get the calculationresults back. * The calculationresult of this class is received from Pangaea. * * The result can contain the following variables: * AankoopBedrag -> het aankoopbedrag wat de klant wilt lenen * Looptijd -> de door de klant gekozen looptijd * Aanbetaling -> de door de klant aangegeven aanbetaling * kvMaand -> kredietvergoeding per maand * kvTotaal -> kredietvergoeding totaal * kvPercentageJaar -> kredietvergoedings-percentage per jaar * kvPercentageMaand -> kredietvergoedings-percentage per maand * maandBedrag -> bedrag dat de klant per maand moet betalen * * Variables specific for LEASE calculator: * bedragViaLease -> het bedrag dat de klant via Lease moet betalen * leaseBedragPerDag -> het lease bedrag dat de klant per dag moet betalen * * Variables specific for FLEX calculator: * bedragViaFlex -> het bedrag dat de klant via FLEX moet betalen * flexBedragPerDag -> het FLEX bedrag dat de klant per dag moet betalen * * Variables specific for SLIM calculator: * bedragViaSlim -> het bedrag dat de klant via SLIM moet betalen * slimBedragPerDag -> het SLIM bedrag dat de klant per dag moet betalen * * And even more! (Future changes / additions) * * */ //the constructor function Calculator() { } //bindings of functions to this class Calculator.prototype.calculate = calculate; Calculator.prototype.calculateCallback = calculateCallback; Calculator.prototype.showErrorMessage = showErrorMessage; Calculator.prototype.traverseForInput = traverseForInput; Calculator.prototype.traverseForOutput = traverseForOutput; //global calculate result this.calculateResult = null; this.prefix = null; //read the inputfields that provide the calculation data //these fields are HTMLElements with an attribute : calculatorInput function traverseForInput(node) { var allResult = ""; if (node.nodeType == 1) { var inputNode = node.getAttribute('calculatorinput'); if (inputNode != null && inputNode != "") { allResult += "&"; //get the value of this node, //or get the checked property if the node is a checkbox or radiobutton var value = null; if (node.type != null && (node.type == 'radio' || node.type == 'checkbox')) { value = node.checked; } else { value = node.value; } if (value != null) { allResult += "" + inputNode + "=" + value; } else { var temp = node.firstChild.nodeValue; allResult += "" + inputNode + "=" + temp; } } } if (node.childNodes != null) { for (var i=0; i < node.childNodes.length; i++) { allResult += traverseForInput(node.childNodes.item(i)); } } return allResult; } //read the inputfields that want the result of the calculation //these fields are HTMLElements with an attribute : calculatorResultName //the value of this attribute is mapped to the result of the calculation //for example: calculatorResultName="kvMaand" wil get the value kvMaand of the calculate result. function traverseForOutput(node) { if (node.nodeType == 1) { var outputNode = node.getAttribute('calculatoroutput'); if (outputNode != null && outputNode != "") { //if we found one, set the value //now set the text of the htmlelement //normally we should use: // node.firstChild.data = getResultValue(outputNode); //but in IE this does not work. //therefore we must check which type this HTMLelement is. //and set its value of innerHTML //see if there is a prefix provided, and use that. if (this.prefix != null) { //see if this field has a prefix that matches the given prefix. //else dont fill this field if (outputNode.indexOf(this.prefix) == 0) { outputNode = outputNode.replace(this.prefix, ""); if (node.tagName == "LABEL") { node.innerHTML = getResultValue(outputNode); } else if (node.tagName == "INPUT") { node.value = getResultValue(outputNode); } } } else { if (node.tagName == "LABEL") { node.innerHTML = getResultValue(outputNode); } else if (node.tagName == "INPUT") { node.value = getResultValue(outputNode); } } } } if (node.childNodes != null) { for (var i=0; i < node.childNodes.length; i++) { traverseForOutput(node.childNodes.item(i)); } } } //make the call and calculate the result function calculate() { body = document.getElementsByTagName('body')[0]; var dynamicContent = traverseForInput(body); var d = new Date(); //alert("url = " + "http://www.llsn.nl/CalculateModulePangaea.jsp?dummy" + d.getTime() + dynamicContent); $.ajax({ type: "GET", url: "http://www.llsn.nl/CalculateModulePangaea.jsp?dummy" + dynamicContent, dataType: "xml", success: function(data) { calculateResult = data; //set result body = document.getElementsByTagName('body')[0]; traverseForOutput(body); }, error: function(request, errorcode) { showErrorMessage(); } }); } //handles the calculate request function calculateCallback(data, ioArgs) { calculateResult = data; //set result body = document.getElementsByTagName('body')[0]; traverseForOutput(body); } //shows an errormessage if anything went wrong function showErrorMessage(msg) { if (msg == null) { alert('De opgevraagde data kan op dit moment niet worden weergegeven. Probeer het later opnieuw'); } else { alert(msg); } } //gets a resultvalue given a name function getResultValue (name) { //see if name contains a #, if so, this means that there is a parent node name in name //for example: totalamount#amount means were gonna search for: // 34.00 //or: subtotalamount#amount means : // 34.00 //or: subtotalamount#identifier=1#amount means : // // 1 // 34.00 // var elementName = name; var elementParentName = null; var splitSignIndex = name.search('#'); if (splitSignIndex > -1) { elementName = name.substring(splitSignIndex + 1); elementParentName = name.substring(0, splitSignIndex); } //check if there is a result, //and parse the received values into variables if (elementParentName == null) { result = $('ResponseBody', calculateResult).find(elementName).text(); } else { result = $('ResponseBody', calculateResult).find(elementParentName).find(elementName).text(); } return result; } //read a given child (siblingTarget) from a xml who's sibling (siblingName) has a given value (siblingValue) // // 1 // 34.00 // function getResultValueFromSibling (groupTag, siblingName, siblingValue, siblingTarget) { var result = ""; $('ResponseBody', calculateResult).find(groupTag).each(function () { if ($(this).find(siblingName).text() == siblingValue) { result = $(this).find(siblingTarget).text(); } }); return result; } //gets a resultvalue given a name /*function getResultValue (name) { var domDocument = dojo.dom.createDocumentFromText(calculateResult); //see if name contains a #, if so, this means that there is a parent node name in name //for example: totalamount#amount means were gonna search for: // 34.00 //or: subtotalamount#amount means : // 34.00 var elementName = name; var elementParentName = ""; var splitSignIndex = name.search('#'); if (splitSignIndex > -1) { elementName = name.substring(splitSignIndex + 1); elementParentName = name.substring(0, splitSignIndex); } //check if there is a result, //and parse the received values into variables var result = ""; var resultTag = domDocument.getElementsByTagName('ResponseBody'); if (resultTag != null && resultTag.length > 0) { if (resultTag) { var tagNames = resultTag[0].getElementsByTagName(elementName); if (tagNames.length > 0) { if (elementParentName == "") { result = tagNames[0].firstChild.data; } else { for (var i = 0; i < tagNames.length; i++) { if (tagNames[i].parentNode.tagName == elementParentName) { result = tagNames[i].firstChild.data; } } } } } } return result; } */ //set the prefix value function setPrefixValue(prefix_value) { this.prefix = prefix_value; }