var stripperRow = 1;
var stripperRowCount = 0;
var stripperCol = 'code';

function stripperAdvanced() {

  // build default table
  for ( i = 1; i <=1; i++ ) {
	addStripperRow(i);
	stripperRowCount = i;
  }

  // focus first field
  //  $('stripperCodeC1').activate();
  
  Event.observe( document, 'keyup', function( event ) { 
	if ( event.keyCode == Event.KEY_RETURN) stripperCalculate( stripperRow );
	else if ( event.keyCode == Event.KEY_LEFT) { stripperKeyLeft(); }
	else if ( event.keyCode == Event.KEY_RIGHT) { stripperKeyRight(); }
	else if ( event.keyCode == 38) { stripperKeyUp(); }
	else if ( event.keyCode == 40) { stripperKeyDown(); }
  });

}

function addStripperRow ( rowID ) {
  var tr = Builder.node('tr');
  var tdCode = Builder.node('td', { className: 'stripper-code' });
  tdCodeInput = Builder.node( 'input', { id: "stripperCodeC" + rowID, name: "stripperCodeC" + rowID, onFocus: 'stripperRow=' + rowID + '; stripperCol="code"; this.select()' } );
  tdCode.appendChild(tdCodeInput);
  tr.appendChild(tdCode);

  tdPrice = Builder.node('td', { className: "stripper-price" });
  tdPriceInput = Builder.node('input', { id: "stripperPriceC" + rowID, name: "stripperPriceC" + rowID, onFocus: 'stripperRow=' + rowID + '; stripperCol="price"; this.select();' } );
  tdPrice.appendChild(tdPriceInput);
  tr.appendChild(tdPrice);

  tdComment = Builder.node('td', { className: "stripper-comment" } );
  tdCommentInput = Builder.node('input', { id: "stripperCommentC" + rowID, name: "stripperCommentC" + rowID, rows: 2, style: "width: 100%;", onFocus: 'stripperRow=' + rowID + '; stripperCol="comment";' } );
  tdComment.appendChild(tdCommentInput);
  tr.appendChild(tdComment);

  tdNote = Builder.node('td', { className: "stripper-note" } );
  tdNoteInput = Builder.node('input', { id: "stripperNoteC" + rowID, name: "stripperNoteC" + rowID, rows: 2, style: "width: 100%;", onFocus: 'stripperRow=' + rowID + '; stripperCol="note";' } );
  tdNote.appendChild(tdNoteInput);
  tr.appendChild(tdNote);

  for ( var i=1; i<=8; i++ )
	{
	  var tdResult = Builder.node('td', { className: "stripper-result", id: "stripperResultT" + rowID });
	  var tdResultInput = Builder.node('input', { id: "stripperResultC" + rowID + 'C' + i, name: "stripperResultC" + rowID + 'C' + i, onFocus: 'this.blur();' } );
	  tdResult.appendChild(tdResultInput);
	  tr.appendChild(tdResult);
	}

  $('stripsBody').appendChild(tr);
  $('stripperCodeC' + rowID).select();
 
}

function stripperCalculate ( rowID ) {

  // if hit enter in comment column, go to next row
  if ( stripperCol == 'comment' ) {
	stripperCol = 'code';
	stripperRow ++;
	$('stripperCodeC' + stripperRow ).select();
	return false;
  }


  $('stripperCodeC' + rowID).style.background='#fff';
  $('stripperCodeC' + rowID).parentNode.style.background='#fff';

  $('stripperCodeC' + rowID).value = $F('stripperCodeC' + rowID).toUpperCase();

  $('stripperPriceC' + rowID).style.background='#fff';
  $('stripperPriceC' + rowID).parentNode.style.background='#fff';

  // validate code
  var code = $F('stripperCodeC' + rowID);

  stripperCol = 'code';

  if ( code.length != 4 ) {
	$('stripperCodeC' + rowID).style.background='#fcc';
	$('stripperCodeC' + rowID).parentNode.style.background='#fcc';
	$('stripperCodeC' + rowID).select();
	return false;
  } else {
	// code can be like eeither of HNZ9 or BNH9 - Base Strip=H, Peak
	// Strip = D, Off-Peak-Strip = O (oh, not zero)
	var codeReg = new RegExp('^[HDRO][NSQV][HMUZ][0-9]$');
	if ( ! code.match(codeReg) ) {
	  $('stripperCodeC' + rowID).style.background='#fcc';
	  $('stripperCodeC' + rowID).parentNode.style.background='#fcc';
	  $('stripperCodeC' + rowID).select();
	  return false;
	}
  }

  stripperCol = 'price';

  // validate price
  var price = $F('stripperPriceC' + rowID);

  if ( price.length == 0 ) {
	$('stripperPriceC' + rowID).focus();
	return false;
  }

  var priceReg = new RegExp('^\\${0,1}\\d{1,3}\\.{0,1}\\d{1,2}?$');
  if ( ! price.match(priceReg) ) {
	$('stripperPriceC' + rowID).style.background='#fcc';
	$('stripperPriceC' + rowID).parentNode.style.background='#fcc';
	$('stripperPriceC' + rowID).select();
	return false;
  }	

  // if both valid, calculate
  //   new Ajax.Updater( 'stripperResultInputC' + rowID, '/stripper?rpc=calculate&code='+code+'&price='+price );

  new Ajax.Request( '/stripper?rpc=calculate&code='+code+'&price='+price, { method: 'get', onSuccess: function(transport) { 
	var r = transport.responseText.split('|');
	for ( var i=0; i< r.length; i++ ) {
	  $('stripperResultC' + rowID + 'C' + (i+1) ).value = r[i];
	}
	// clear offpeak columns if code has changed from offpeak
	if ( r.length == 4 ) {
	  for ( var j=5; j<=8; j++ ) {
		$('stripperResultC' + rowID + 'C' + j).value = '';
	  }
	}
  } } );
  
  // if calculated on lastd row, add a new one and focus first cell

  if ( stripperRow == stripperRowCount ) {
	stripperRowCount ++;
	addStripperRow( stripperRowCount );
	stripperCol = 'code';
	$('stripperCodeC' + stripperRow).select();
  }

  // we have at least one row calculated now, enough to export
  $('stripper-download').style.display="block";

  return true;

}

function stripperKeyLeft () {
  if ( stripperCol == 'note' ) {
	stripperCol = 'comment';
	$('stripperCommentC' + stripperRow).select();
	return false;
  }
  if ( stripperCol == 'comment' ) {
	stripperCol = 'price';
	$('stripperPriceC' + stripperRow).select();
	return false;
  }
  var currentCol = stripperCol;
  if ( ! stripperCalculate ( stripperRow ) ) {
	return false;
  }
  stripperCol = currentCol;
  if ( stripperCol == 'price' ) {
	stripperCol = 'code';
	$('stripperCodeC' + stripperRow).select();
  }
}

function stripperKeyRight () {
  if ( stripperCol == 'note' ) {
	return false;
  }
  if ( stripperCol == 'comment' ) {
	stripperCol = 'note';
	$('stripperNoteC' + stripperRow).select();
	return false;
  }

  var currentCol = stripperCol;
  if ( ! stripperCalculate ( stripperRow ) ) {
	return false;
  }
  if ( currentCol == 'code' ) {
	stripperCol = 'price';
	$('stripperPriceC' + stripperRow).select();
  } else if ( currentCol == 'price' ) {
	stripperCol = 'comment';
	$('stripperCommentC' + stripperRow).select();
  }
}

  function stripperKeyUp () {
  if ( stripperRow > 1 ) {
	stripperRow --;
	if ( stripperCol == 'code' ) {
	  $('stripperCodeC' + stripperRow).select();
	} else if ( stripperCol == 'price' ) {
	  $('stripperPriceC' + stripperRow).select();
	} else if ( stripperCol == 'comment' ) {
	  $('stripperCommentC' + stripperRow).select();
	} else if ( stripperCol == 'note' ) {
	  $('stripperNoteC' + stripperRow).select();
	}
  }
}

function stripperKeyDown () {
  if ( stripperRow < stripperRowCount ) {
	stripperRow ++;
	if ( stripperCol == 'code' ) {
	  $('stripperCodeC' + stripperRow).select();
	} else if ( stripperCol == 'price' ) {
	  $('stripperPriceC' + stripperRow).select();
	} else if ( stripperCol == 'comment' ) {
	  $('stripperCommentC' + stripperRow).select();
	} else if ( stripperCol == 'note' ) {
	  $('stripperNoteC' + stripperRow).select();
	}
  }
}
