﻿//**** START UNIT PRICE FUNCTIONS **** //
  
/* usage: BuildUnitPriceDaily({file name},{data start record},{column no of product ID},{data columns needed, in order}) */
function BuildUnitPriceDaily(inURL, inStartRow, inKey, inStrLoc) {
 var txtFile = new XMLHttpRequest();
 var txtFilename = inURL + '?rand=' + Math.floor(100000*Math.random());
 var htmlStr = ""; 
 inStartRow = inStartRow - 1;    //account for zeroth index
 inKey = inKey - 1;  //account for zeroth index
 
 txtFile.open("GET", txtFilename, true);
 
 txtFile.onreadystatechange = function() {
   if (txtFile.readyState === 4) {  // Makes sure document is ready to parse.
  if (txtFile.status === 200) {  // Makes sure it's found the file.
   allText = txtFile.responseText;
   aFileContents = txtFile.responseText.split("\n"); // separate each line into an array
 
   htmlStr += '<TABLE class="simpletable">';
   
   var curProd = 0;
   // loop through file contents, each line is also an array
   for (lineLoop = inStartRow ; lineLoop < aFileContents.length ; lineLoop ++ ) {
    aLine = aFileContents[lineLoop].split(","); // turn a single line into a sub-array of elements
    // Only show the first record per fund (if file contains multiple rows per fund)
    if (curProd != aLine[inKey] && !(aLine[inKey] == undefined || aLine[inKey] == '') ) { // the product ID
     htmlStr += '<TR>';
     // loop through lines to build HTML row data
     for (locLoop = 0 ; locLoop < inStrLoc.length ; locLoop ++ ) {
      valCell = aLine[inStrLoc.charAt(locLoop)-1];
      if (lineLoop == 0) { // header row
       htmlCell = 'TH';
      } else {
       htmlCell = 'TD';  
       if (inStrLoc.charAt(locLoop) > 3 && !(valCell == "\r" || valCell == '') ) { // set dec pl for values (\r in trailing empty cells)
        valCell = parseFloat(valCell);
        valCell = valCell.toFixed(4);
       }
      }
      htmlStr = htmlStr + '<' + htmlCell + '>' + valCell + '</' + htmlCell + '>';   
     }// end per-value loop
     htmlStr += '</TR>';
     curProd =  aLine[inKey]; // the product ID 
    } 
   } // end per-line Loop
 
   htmlStr = htmlStr + '</TABLE>';  
   document.getElementById('UP_table').innerHTML = htmlStr;
  }
   }
 return htmlStr;
 }
 txtFile.send(null);
}
 
/* usage: BuildUnitPriceStrategy({file name},{data start record},{column no of product ID},{ID needed},{HTML element ID, column no*4}) */
function BuildUnitPriceStrategy(inURL, inStartRow, inKey, inProduct, inElement1, inElement2, inElement3, inElement4, inLoc1, inLoc2, inLoc3, inLoc4) {
 var txtFile = new XMLHttpRequest();
 var txtFilename = inURL + '?rand=' + Math.floor(100000*Math.random());
 inStartRow = inStartRow - 1;    //account for zeroth index
 inKey = inKey - 1;  //account for zeroth index 
 
 txtFile.open("GET", txtFilename, true);
 txtFile.onreadystatechange = function() {
   if (txtFile.readyState === 4) {  // Makes sure document is ready to parse.
  if (txtFile.status === 200) {  // Makes sure it's found the file.
   allText = txtFile.responseText;
   aFileContents = txtFile.responseText.split("\n"); // separate each line into an array
 
   // loop through file contents, each line is also an array
   for (lineLoop = inStartRow ; lineLoop < aFileContents.length ; lineLoop ++ ) {
    aLine = aFileContents[lineLoop].split(","); // turn a single line into a sub-array of elements
    if (aLine[inKey] == inProduct) { // Only use the first record per fund (if file contains multiple rows per fund)
     // set dec pl for values (\r in trailing empty cells)
     if (inLoc1 > 3 && !(aLine[inLoc1] == "\r" || aLine[inLoc1] == '') ) {
      aLine[inLoc1] = parseFloat(aLine[inLoc1]);
      aLine[inLoc1] = aLine[inLoc1].toFixed(4);
     }
     if (inLoc2 > 3 && !(aLine[inLoc2] == "\r" || aLine[inLoc2] == '') ) {
      aLine[inLoc2] = parseFloat(aLine[inLoc2]);
      aLine[inLoc2] = aLine[inLoc2].toFixed(4);
     }
     if (inLoc3 > 3 && !(aLine[inLoc3] == "\r" || aLine[inLoc3] == '') ) {
      aLine[inLoc3] = parseFloat(aLine[inLoc3]);
      aLine[inLoc3] = aLine[inLoc3].toFixed(4);
     }
     if (inLoc4 > 3 && !(aLine[inLoc4] == "\r" || aLine[inLoc4] == '') ) {
      aLine[inLoc4] = parseFloat(aLine[inLoc4]);
      aLine[inLoc4] = aLine[inLoc4].toFixed(4);
     }
     document.getElementById(inElement1).innerHTML = aLine[inLoc1];
     document.getElementById(inElement2).innerHTML = aLine[inLoc2];
     document.getElementById(inElement3).innerHTML = aLine[inLoc3];
     document.getElementById(inElement4).innerHTML = aLine[inLoc4];
     break;
    }
   } // end per-line Loop
  }
   }
 }
 
 txtFile.send(null);
}
 
//**** END UNIT PRICE FUNCTIONS **** //
