// asAtDate must be a string of type yyyymmdd // function returns price, or -1 if out of range function getHistoricalIANGPrice(asAtDate) { var count = wlIANGDateArr.length; var date = parseInt(asAtDate); if (date > wlIANGDateArr[count-1] || date < wlIANGDateArr[0]) return -1; var lower = 0; var upper = count-1; var index = parseInt(upper/2); var curDate; // binary search for date while(upper - lower > 1) { curDate = wlIANGDateArr[index]; if (curDate == date) return wlIANGPriceArr[index]; if (curDate > date) upper = index; else lower = index; index = parseInt((upper+lower)/2); } // Make sure that what we've found is actually the right day. if (wlIANGDateArr[lower] == date) { return wlIANGPriceArr[lower]; } else if(wlIANGDateArr[count-1] == date) { return wlIANGPriceArr[count-1]; } else { return -1; } } function showHistoricalIANGPrice() { var f = document.forms.HistoricalIANGDataForm; var d = ("00" + f.day.value).slice(-2); var m = ("00" + f.month.value).slice(-2); var y = ("0000" + f.year.value).slice(-4); if (isNaN(parseInt(d)) || isNaN(parseInt(m)) || isNaN(parseInt(y))) { alert("Please enter a date in dd/mm/yyyy format"); return; } var price = parseFloat(getHistoricalIANGPrice(y + m + d)) if (price.toFixed != null) { price = price.toFixed(2); } var msg = "On " + d + "/" + m + "/" + y + " the price was $" + price; if (price > -1) { alert(msg); } else { alert("No price information on that date"); } }