var ie = document.all;
var moz = document.getElementById && !document.all; 

///////////////////////////
// Input Mutate example
///////////////////////////

// Copies attributes from one node (src) to another (dest).  Modify this to suit
// your needs.  For example, you may not want to retain the value when a new
// node is generated.
function CopyAttributes(src, dest)
{
	var i;

	dest.id = src.id;
	dest.name = src.name;
}

// Performs the mutation on the node with id given by the argument searchbox.
// opt specifies the type of mutation to make.
function UpdateSearchField(opt, searchbox)
{
	var inputbox = document.getElementById(searchbox);
	
		// Based on search type, choose which element to create and
		// set its attributes accordingly.
	
    if (opt == 'content') { // make a textarea
		var el = document.createElement("TEXTAREA");
		CopyAttributes(inputbox, el);
		el.cols = 40;
		el.rows = 4;
    }
    else if (opt == 'author') { // make a standard text box
		var el = document.createElement("INPUT");
		CopyAttributes(inputbox, el);
		el.type = 'text';
		el.size = 20;
    }
	else if (opt == 'binding') { // make a drop-down list
		var el = document.createElement("SELECT");
		CopyAttributes(inputbox, el);
		el.size = 1;
	
			// str stores the text for the drop-down selector.  Different
			// drop-down lists could be provided by simply switching in
			// different arrays for str.
	
		var i;
		var str = new Array('HardCover', 'Paperback', 'Magazine');
		for (i = 0; i < str.length; i++) {
			var opt = document.createElement("OPTION");
			opt.appendChild(document.createTextNode(str[i]));
			opt.setAttribute('value', str[i]);
			if (inputbox.value == str[i]) // check for selected item
				opt.setAttribute('selected', 'selected');
			el.appendChild(opt);
		}
    }
	
		// Use the DOM function replaceChild to put in the newly created
		// node.
	
	inputbox.parentNode.replaceChild(el, inputbox);
}


///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////
// Dynamic Table example
///////////////////////////

function rowrenumber(newrow, newid)
{
    var curnode = newrow.firstChild;
    while (curnode) {
		var curitem = curnode.firstChild;
		while (curitem) {    
	    	if (curitem.id) {
				var idx = 0;
				var spl = curitem.id.split('_');
			    var baseid = spl[0];
			    curitem.id = baseid + '_' + newid;
			    if (curitem.name)
					curitem.name = baseid + '_' + newid;
			    if (baseid == 'catno')
					curitem.tabIndex = newid;
			}
	    	curitem = curitem.nextSibling;
		}
		curnode = curnode.nextSibling;
	}
}


function AppendRow(table_id)
{
    var row = document.getElementById(table_id).rows.item(1);
	var newid = row.parentNode.rows.length;
    var newrow = row.cloneNode(true);
    rowrenumber(newrow, newid);
    row.parentNode.appendChild(newrow);    
    
    var curnode = document.getElementById('catno_' + newid);
    curnode.value = "";
    curnode.tabIndex = newid;
	curnode = document.getElementById('descr_' + newid);
    curnode.value = "";
	curnode = document.getElementById('quant_' + newid);
    curnode.value = "";
	curnode = document.getElementById('price_' + newid);
    curnode.value = "";
	curnode = document.getElementById('total_' + newid);
    curnode.value = "";
	curnode = document.getElementById('delete_' + newid);
    curnode.innerHTML = "X";
	curnode = document.getElementById('delete_1');
    curnode.innerHTML = "X";
    UpdateTotals(table_id);
}

function DeleteRow(el) {
    var row = el.parentNode.parentNode;
	var rownum = row.rowIndex;
    var tbody = row.parentNode;
	var numrows = tbody.rows.length - 1;  // don't count header row!
    if (numrows == 1)
		return false;

    var node = row;
    tbody.removeChild(node);
    var newid = -1;
    row = tbody.firstChild;
    while (row) {
		if (row.tagName == 'TR') {
		    newid++;
	    	if (newid >= rownum)
				rowrenumber(row, newid);
		}
		row = row.nextSibling;
    }
    if (numrows == 2) {
		var delbutton = document.getElementById('delete_1');
		delbutton.innerHTML = ' ';
    }
	UpdateTotals(tbody.parentNode.id);
}

function UpdateTotals(table_id)
{
	var numrows = document.getElementById(table_id).rows.length - 1;
	var i, totalcost = 0.00;
	for (i = 1; i <= numrows; i++) {
		var q = parseInt(document.getElementById('quant_' + i).value);
		var price = parseFloat(document.getElementById('price_' + i).value);
		var cost;
		if (!q || !price)
			cost = 0.00;
		else
			cost = q * price;
		var total = document.getElementById('total_' + i);
		total.value = '$' + cost;
		totalcost = totalcost + cost;
	}
	var total = document.getElementById('total');
	total.value = '$' + totalcost;
}

///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////
// Help Text example
///////////////////////////

function getElementAbsPosX(el)
{
    var dx = 0;
    if (el.offsetParent) {
		dx = el.offsetLeft + 8;
		while (el = el.offsetParent) {
		    dx += el.offsetLeft;
		}
    }
    return dx;
}

function getElementAbsPosY(el, foo)
{
    var dy = 0;
    if (el.offsetParent) {
		dy = el.offsetTop + el.offsetHeight / 2;
		while (el = el.offsetParent) {
		    if (foo == 0)
			dy += el.offsetTop;
		}
    }
    return dy;
}

function ChangeFocus(el)
{
	while (el.tagName != 'INPUT') 
		el = el.previousSibling;
	el.focus();
}

function UpdateHelpText(el)
{
  var label = el;
  while (label.tagName != 'DIV')
  	label = label.nextSibling;
  if (el.value == '') {
    label.style.left = getElementAbsPosX(el) + 'px';
    label.style.top = (getElementAbsPosY(el, 1) - 7) + 'px';
    label.style.visibility = 'visible';
  }
  else {
    if (label)
      label.style.visibility = 'hidden';
  }
}

///////////////////////////////////////////////////////////////////////////////////////////////////
