var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttp=false
  }
 }
@else
 xmlhttp=false
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false;
 }
}

function loadRates() {
 document.getElementById('latestRates').innerHTML = '<div style="width: 90px; float: left;">Retrieving Rates</div>';
 xmlhttp.open("GET", "valubond.xml", true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
     var xml = xmlhttp.responseText;
     var objDom = new XMLDoc(xml, xmlError);
     //get the root node
     var objDomTree = objDom.docNode;
     //get all of the elements named "RateRow"
     var rateRowElements = objDomTree.getElements("RateRow");
     //get the first "TAG1" element
     var text;
     var tr_1moask;
     var tr_3moask;
     var tr_6moask;
     for (var x=0;x <rateRowElements.length;x++) {
     	var element = rateRowElements[x];
     	if (element.getAttribute("BENCHMARK")=="10Y") {
     		TREASURY_10YR=parseFloat(element.getAttribute("ASK_YIELD"));
        } else if (element.getAttribute("BENCHMARK")=="1M") {
             	TREASURY_1MO=calculateRate(parseFloat(element.getAttribute("ASK_PRICE")), new Date(element.getAttribute("MATURITY_DATE")));
             	tr_1moask = parseFloat(element.getAttribute("ASK_YIELD"))
        } else if (element.getAttribute("BENCHMARK")=="2Y") {
		TREASURY_2YR=parseFloat(element.getAttribute("ASK_YIELD"));
        } else if (element.getAttribute("BENCHMARK")=="30Y") {
		TREASURY_30YR=parseFloat(element.getAttribute("ASK_YIELD"));
        } else if (element.getAttribute("BENCHMARK")=="3M") {
		TREASURY_3MO=calculateRate(parseFloat(element.getAttribute("ASK_PRICE")), new Date(element.getAttribute("MATURITY_DATE")));
		tr_3moask = parseFloat(element.getAttribute("ASK_YIELD"))
        } else if (element.getAttribute("BENCHMARK")=="3Y") {
		TREASURY_3YR=parseFloat(element.getAttribute("ASK_YIELD"));
        } else if (element.getAttribute("BENCHMARK")=="5Y") {
		TREASURY_5YR=parseFloat(element.getAttribute("ASK_YIELD"));
        } else if (element.getAttribute("BENCHMARK")=="6M") {
		TREASURY_6MO=calculateRate(parseFloat(element.getAttribute("ASK_PRICE")), new Date(element.getAttribute("MATURITY_DATE")));
		tr_6moask = parseFloat(element.getAttribute("ASK_YIELD"))
        } else if (element.getAttribute("BENCHMARK")=="1Y") {
		TREASURY_1YR=parseFloat(element.getAttribute("ASK_YIELD"));
	}
     }
	 
	 var latestrates;
	 latestrates = '<div style="width: 35px; float: left;">2 yr.</div>';
	 latestrates += '<div style="width: 55px; float: left;">'+ TREASURY_2YR +'%</div>';
	 latestrates += '<div style="width: 35px; float: left;">3 yr.</div>';
	 latestrates += '<div style="width: 55px; float: left;">'+ TREASURY_3YR +'%</div>';
	 latestrates += '<div style="width: 35px; float: left;">5 yr.</div>';
	 latestrates += '<div style="width: 55px; float: left;">'+ TREASURY_5YR +'%</div>';
	 latestrates += '<div style="width: 35px; float: left;">10 yr.</div>';
	 latestrates += '<div style="width: 55px; float: left;">'+ TREASURY_10YR +'%</div>';
	 
	 
	 //display latest rates
	 document.getElementById('latestRates').innerHTML = latestrates;
	 //document.getElementById('latestRates').innerHTML = '<table border="0" cellspacing="0" cellpadding="0" class="rates"><tr><td>2YR</td><td>:</td><td>&nbsp;'+ TREASURY_2YR +'%</td></tr><tr><td>3YR</td><td>:</td><td>&nbsp;'+ TREASURY_3YR +'%</td></tr><tr><td>5YR</td><td>:</td><td>&nbsp;'+ TREASURY_5YR +'%</td></tr><tr><td>10YR&nbsp;</td><td>:</td><td>&nbsp;'+ TREASURY_10YR +'%</td></tr></table>';
	 document.getElementById('rate2yrResult').value = TREASURY_2YR;
	 document.getElementById('rate3yrResult').value = TREASURY_3YR;
	 document.getElementById('rate5yrResult').value = TREASURY_5YR;
	 document.getElementById('rate10yrResult').value = TREASURY_10YR;
  }
 }
 xmlhttp.send(null);
}

function calculateRate(price, maturityDate) {
   return Math.round(((1000-(price*10))/(price*10))*(365/Math.round((maturityDate - new Date())/86400000))*100000)/1000;
}

function xmlError(e) {
//there was an error, show the user
alert(e);
} //end function xmlError