<!--
// file: spaces.js
// author: philip pang
// date: 15 May 2007
// updates:
// - (18/11/07) added printFormatted()


function spacesTrimLeft (str) {
  str = str.replace(/^[ \t]+/,"");
  return str;
}

function spacesTrimRight (str) {
  str = str.replace(/^[ \t]+/,"");
  return str;
}

function spacesTrimBoth (str) {
  str = spacesTrimLeft (str);
  str = spacesTrimRight (str);
  return str;	
}

function spacesSqueeze (str) {
  str = str.replace(/[ \t]+/g,"");
  return str;
}

function spacesMake (n) {
  var str = "";
  while (n-- > 0) str += " ";
  return str;
}

function justifyLeft (str,wide) {
  if (str.length > wide) return str.substr(0,wide);
  return str + spacesMake (wide - str.length);  	
}

function justifyRight (str,wide) {
  if (str.length > wide) return str.substr(str.length-wide,wide);
  return spacesMake (wide - str.length) + str;
}

// var str = printFormatted (new Array("L10,L9,R10"),
//    new Array("61121122","S8912345A","James"));
// L:left R:right S:spacer
function printFormatted (fmtArr,fldArr) {
  var str = "";
  var j = 0;
  for (var i=0; i < fmtArr.length; i++) {
    var fmt = fmtArr[i];
    var val = fldArr[j];
    var jtype = fmt.substr(0,1).toUpperCase();
    var jlen = fmt.substr(1);
    if (jtype == "L") str += justifyLeft (val,jlen); 
    if (jtype == "R") str += justifyRight (val,jlen); 
    if (jtype == "S") str += jlen; 
    if (jtype != "S") j++;
  }
  return str;
}

//-->
