var recordlist = new Array();
var nosubmit = false;

// To use this lib, be sure to
// define the following:
//Record = function(fields) {
//  this.fname = fields[0];
//  this.lname = fields[1];
//  this.phone = fields[2];
//  this.email = fields[3];
//  return this;
//}
//function showRecord(idx) {
//  var ctr = 0;
//  var record = Recordlist[idx];
//  for(i in record) {
//    var field = parent.document.getElementById('field'+ctr);
//    field.value = record[i];
//    ctr++;
//  }
//}

function validateForm(form) {
  if(nosubmit) {
    nosubmit = false;
    return false;
  }
  return true;
}

function addRecord() {
  recordlist[recordlist.length] = new Record(addRecord.arguments);
}

function filterRecordList(filter) {
  nosubmit = true;
  writeRecordList(filter);
}

function writeRecordList(filter) {
  var html = getRecordList(filter);
  var obj = document.getElementById('recordlistbox');
  obj.innerHTML=html;
}

function writeRecordListToId(htmlid) {
  var html = getRecordList();
  var obj = document.getElementById(htmlid);
  obj.innerHTML=html;
}

function getRecordList(filter) {
  var html = '<table cellpadding="0" cellspacing="0" id="Recordtable">'+"\n";
  for(var i=0;i<recordlist.length;i++) {
    if((filter!=null && filter!="" && filterMatch(recordlist[i],filter)) ||
	(filter==null || filter==null)) {
      var alt = '';
      if((i%2)==1) { alt = 'alt'; }
      var className = 'recordrow'+alt;
      html += '<tr class="'+className+'" id="Record'+i+'">'+"\n";
      html +=  getRow(recordlist[i],i,className);
      html += '</tr>'+"\n";
    }
  }
  html += '</table>';

  return html;
}

function filterMatch(record,filter) {
  var re = new RegExp(filter,"i");
  for(i in record) {
    if(re.test(record[i])) { return true; }
  }
  return false;
}

function getRow(row,idx,className) {
  var ctr = 0;
  var html = "";
  for(var i in row) {
    html += '<td class="recordcell'+ctr+'" onmouseover="shadeRow('+idx+');"';
    html += ' onmouseout="unShadeRow('+idx+',\''+className+'\');" ';
    html += ' onclick="showRecord('+idx+');">'+"\n";
    html += row[i]+'</td>';
    ctr++;
  }
  return html;
}

function shadeRow(idx) {
  var obj = document.getElementById('Record'+idx);
  obj.className="Recordrowshaded";
}

function unShadeRow(idx,className) {
  var obj = document.getElementById('Record'+idx);
  obj.className=className;
}

function ShowObj(obj) {
  text = '';
  for(i in obj) {
    text += i+"="+obj[i]+"<br/>\n";
  }
  document.write(text);
}
