function addLoadEvent(func) {
  // use this function to add multiple event handlers to window.onload
	var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


function HighlightRow(tr, changeTo)
{ tr.style.backgroundColor = changeTo; }

function TRchangeTo(tdname) {
  var elements=document.getElementsByName(tdname);
  for (index = 0; index < elements.length; index++) {
    elements[index].style.className = 'tdHighlight';  
    elements[index].style.backgroundColor = '#D0D0D0';  
  }

  var element=document.getElementById(tdname);
  if (element) {  
    element.style.className = 'tdHighlight';
    element.style.backgroundColor = '#D0D0D0';  
  }
}

function TRchangeBk(tdname) {
  var elements=document.getElementsByName(tdname);
  for (index = 0; index < elements.length; index++) {
    elements[index].style.className = 'tdDefault';  
    elements[index].style.backgroundColor = '';
  }

  var element=document.getElementById(tdname);
  if (element) {
    element.style.className = 'tdDefault';
    element.style.backgroundColor = '';
  }	

}


function tdclick(td) {

  // find the id which belongs to this cell. If the cell has no id, then find an id in a cell to the left

	var cell = td;
	var id = '';	
	while (cell) { 	
		if ((cell.id != '') && (id == '')) {
			id = cell.id;
			break;
		}
		cell = cell.previousSibling;
	}	
	if ((id) && (id != '')) {
		document.location = id;
	}
}	

function handleTd(td, aclassname, abgcolor) {
  function walkLeft(td) {
	  var cell = td;
	  var id = '';	
		while (cell) { 	
	    if ((cell.id != '') && (id == '')) {
			  id = cell.id;
			}
			cell.style.className = aclassname;
		  cell.style.backgroundColor = abgcolor;
			
			if (!cell.previousSibling) {
		    // there are no more TD cells left
				// Check if any previous row has more TD's than this row. If true, there there is a rowspan. The rowspanned cell must be highlighted too
				// find the parent's previous sibbling which must be a TR
        tr = cell.parentNode;
				
				if ((tr) && (tr.tagName == 'TR')) {
					// tr is the parent TR
					parentchildcount = tr.childNodes.length;
					
					// find the previous TR which has more cells than the current row. Other nodes like \n  must be skipped
					tr = tr.previousSibling;
					while ( (tr) && 
					        ( (tr.tagName != 'TR') || (tr.childNodes.length <= parentchildcount) ) 
								) {
					  tr = tr.previousSibling;
					}
					
					if (tr) {
					  // find the first TD and highlight it. Recursivly handle more rowspans in cells which are located left of the current cell.
						walkLeft(tr.firstChild);
					}
					
        }				
				break;
			}
			
			cell = cell.previousSibling;
		}
		if ( (id != '') && (td) && (td.addEventListener) ) {  // skip this for internet explorer because that browser doesn't support it
		  td.addEventListener("click", function(e){ tdclick(id); }, false);
		}
  }	

  function walkRight(cell) {
	  while (cell) { 	
	    if (cell.id != 'exclude') {
				cell.style.className = aclassname;
			  cell.style.backgroundColor = abgcolor;
				
				if (cell.rowSpan > 1) {
	        tr = cell.parentNode;
					tbody = tr.parentNode;
//				alert(tr.sectionRowIndex + ' ' + cell.rowSpan);
					
					var rowNum = -1;
					for (var childIndex = 0; childIndex < tbody.childNodes.length; childIndex++) {
					  if (tbody.childNodes[childIndex].tagName == 'TR') {
						  rowNum ++;
							if (rowNum >= tr.sectionRowIndex + cell.rowSpan) {
							  break;
							}
							if (rowNum >= tr.sectionRowIndex) {
								spanRow = tbody.childNodes[childIndex];
								spanCell = spanRow.firstChild;
								//alert(spanRow + ' ' + rowIndex);
								while (spanCell) {
				          spanCell.style.className = aclassname;
			      		  spanCell.style.backgroundColor = abgcolor;
								  
								  spanCell = spanCell.nextSibling;
								}					  
							}
						}
					}
	        break; // Done with all rows of the rowspan, including the current row
				}
			}	
			cell = cell.nextSibling;
		}	    
  }	
	
  // loop from this to the left
  walkLeft(td);	
	
  // loop from this to the right
  walkRight(td);	

}

function tdmouseover(td) {
  handleTd(td, 'tdHighlight', '#D0D0D0');
}

function tdmouseout(td) {
  handleTd(td, 'tdDefault', '');
}



function InitGroupTbody(tbody, allowclick) {
  // loop through body rows of the table
	for (rowindex = 0; rowindex < tbody.rows.length; rowindex++) {
		row = tbody.rows[rowindex];
		// handpoint cursor
		if (allowclick) {
		  row.style.cursor='pointer';
		}	
		
		// loop through rows
		for (cellindex = 0; cellindex < row.cells.length; cellindex++) {
		  cell = row.cells[cellindex]; /*
			if ( (cell.id != '') && (cell.id[0]=='m') ) {
			  cellmanufacturer=cell;
			}
			if ( (cell.id != '') && (cell.id[0]=='v') ) {
			  cellvehicle=cell;
			}  */
			if (cell.id == 'exclude') {
			  cell.style.cursor='default';
			}
			else {
  			if (cell.addEventListener) {  // skip this for internet explorer because that browser doesn't support it. Alternativly the user can only click the 'view' hyperlink
    			cell.addEventListener("mouseover", function(e){ tdmouseover(this); }, false);
	    		cell.addEventListener("mouseout", function(e){ tdmouseout(this); }, false);
 		      if (allowclick) {
					  cell.addEventListener("click", function(e){ tdclick(this); }, false);
					}	
        }
			}	

//			cell.attachEvent('onmouseover', function(e){ alert(this.nodeName); });
//			cell.onmouseover=tdmouseover;
//			alert(cell.getAttributeNode('onmouseover'));
			// cell.addEventListener("mouseover", tdmouseover, false);
		//	cell.addEventListener("mouseout", tdmouseout);
		}
	}
//	document.getElementById('k1').addEventListener("mouseover", tdmouseover, false);
//	document.getElementById('k1').onMouseOver = tdmouseover;
	
}

function menu(td) {
  // a menu is a simple <TD><a></a></TD>
	// this function handles a click on a menu cell
  // find <A> tag, which should be the first tag
	a = td.firstChild;
	if ((a) && (a.href)) {
	  document.location = a.href;
	}
}

function itemAddFavourite(itemid) {
  loadFragmentInToElementPost('useritem_'+itemid, '/useritemlistedit.php', 'listkind=F&action=add&id='+itemid, false);
}
function itemRemoveFavourite(itemid) {
  loadFragmentInToElementPost('useritem_'+itemid, '/useritemlistedit.php', 'listkind=F&action=del&id='+itemid, false);
}

function itemAddWishlist(itemid) {
  loadFragmentInToElementPost('useritem_'+itemid, '/useritemlistedit.php', 'listkind=W&action=add&id='+itemid, false);
}
function itemRemoveWishlist(itemid) {
  loadFragmentInToElementPost('useritem_'+itemid, '/useritemlistedit.php', 'listkind=W&action=del&id='+itemid, false);
}

function itemEditInventory(itemid,itemname) {
  loadFragmentInToElementPost('useritem_'+itemid, '/useritemlistedit.php', 'listkind=I&action=form&id='+itemid, false);
}

function collapseOrExpand(divid) {
  element = document.getElementById(divid);
	img = document.getElementById(divid+'_img');
	if (element) {
	  if (element.style.display!='') {
		  element.style.display='';
			if (img) {
			  img.src='/img/minus.png';
				img.title='collapse';
			}
		}
		else {
		  element.style.display='none';		
			if (img) {
			  img.src='/img/plus.png';
				img.title='expand';
			}
		}
	}
}

function deletekit(kitid) {
  if (confirm('Delete this kit?')) {
	  document.location = '/editkit.php?delete&id=' + kitid;
	}
}

function toggleCheckbox(aCheckboxId) {
  var checkbox = document.getElementById(aCheckboxId);
  checkbox.checked = !checkbox.checked;
}