    function getLoanType() {
        return document.calcForm.selectLoanType.options[document.calcForm.selectLoanType.selectedIndex].value;
    }

    function OnLoanSelectionChanged() {
        //alert("selection changed")
	retrieveRate(getLoanType())
    }


    function getLoanAmount() {
        return parseFloat(document.calcForm.loanAmount.value)
    }

    function OnCalculatePayment() {
       try {
          var loanType = getLoanType();
          var loanAmount = getLoanAmount();
	  calculatePayment(loanType, loanAmount);
       } catch(e) {
          alert(e)
       }
    }

    function calculatePayment(loanType, loanAmount) {
       //alert("calculating payment: " + window.document.calcForm.loanAmount.value);
	  
       var req3 = createXHR();
       var rate  = "";
       try {
          var encodedLoanType = escape(loanType)
          var encodedLoanAmount = escape(loanAmount)
          //alert(loanAmount)
	  //alert("loan type " + encodedLoanType)
          req3.open('POST', '/LoanServices/Service1.asmx/calculateMonthlyPayment', false);
          req3.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
          req3.send("loanType="+encodedLoanType+"&loanAmount="+encodedLoanAmount);
	  if (req3.readyState == 4 && req3.status == 200) {
	     //alert(req3.responseXML.documentElement.firstChild.nodeValue)	  
	     payment = parseFloat(req3.responseXML.documentElement.firstChild.nodeValue)
	     payment = payment.toFixed(2)
	    
	     //alert(payment);
             var paymentDiv = document.getElementById('monthlyPayment');
             if (paymentDiv.firstChild) {
	         paymentDiv.removeChild(paymentDiv.firstChild);
	     }
             paymentDiv.appendChild(document.createTextNode("$"+payment)) 
          }
       } catch (e) {
          alert("exception " + e)
       }
       req3 = null;
    }

    function createXHR() {
       var reqTry = [ 
          function() { return new XMLHttpRequest(); },
          function() { return new ActiveXObject('Msxml2.XMLHTTP') },
          function() { return new ActiveXObject('Microsoft.XMLHTTP' )} 
       ]
       var i = 0;
       var retval = null;
       while (null == retval && (i < reqTry.length)) {
          try { 
             retval = reqTry[i++]();
          } catch(e) {
          }
       }
       return retval;
    }

    function retrieveRate(loanType) {
       var req2 = createXHR();
       var rate  = "";
       try {
          var encodedLoanType = escape(loanType)
	  //alert("loan type " + encodedLoanType)
          req2.open('POST', '/LoanServices/Service1.asmx/getRate', false);
          req2.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
          req2.send("loanType="+encodedLoanType);
	  if (req2.readyState == 4 && req2.status == 200) {
	     //alert(req2.responseXML.documentElement.firstChild.nodeValue)	  
	     rate = parseFloat(req2.responseXML.documentElement.firstChild.nodeValue)
	     rate *= 100;
             rate = rate.toFixed(2)
	     //alert(rate);
             var rateDiv = document.getElementById('loanAPR');
             if (rateDiv.firstChild) {
	         rateDiv.removeChild(rateDiv.firstChild);
	     }
             rateDiv.appendChild(document.createTextNode(rate))
          }
       } catch (e) {
          alert("exception " + e)
       }
       req2 = null;
    }

    function addSelectOptions(listOfNodes, formname, selectctrl) {
        for (var i = 0; i < listOfNodes.length; i++) {
            if (listOfNodes[i].nodeValue) {
	        //alert("select " + listOfNodes[i].nodeValue)
                var option = document.createElement("option");
                option.value = listOfNodes[i].nodeValue;
		option.text = listOfNodes[i].nodeValue;
                if (window.navigator.appName && window.navigator.appName.match("Internet Explorer")) {
                    window.document[formname][selectctrl].add(option);
                } else {
		    window.document.calcForm.selectLoanType.add(option, null)
                }
            }
            if (listOfNodes[i].childNodes) {
                addSelectOptions(listOfNodes[i].childNodes, formname, selectctrl);
            }
	}
    }

