/**
 * parse an int safely
 */
function safeParseInt(x) {
  if (!x) return 0;
  
  // kill non-numeric values
  x=x.replace(/[^\-0-9\.]+/g, '');
  
  // kill leading zeros (isNaN doesn't like them)
  x=x.replace(/^(-?)0+/, '$1');
  
  // check if it's a legal number
  if(x=='' || isNaN(x))
    return 0;
  
  // us js's internal function
  return parseInt(x);
}

function pop_help(message, senderEl) {
  var popEl = document.createElement('span');
  popEl.className = 'pop_help';
  
  var html = message + '<br /><br /><a href="#" onclick="pop_help_close(this); return false;">close this tip</a>';
  popEl.innerHTML = html;
  
  senderEl.parentNode.insertBefore(popEl, senderEl);
}

function pop_help_close(senderEl) {
  var popEl = senderEl;
  while (popEl.className != 'pop_help') { popEl = popEl.parentNode; };
  popEl.parentNode.removeChild(popEl);
}

function clickChildLink(elem) {
  for (var i = 0; i < elem.childNodes.length; i++) {
    var child = elem.childNodes[i];
    if (child.tagName == 'A') {
      document.location = child.href;
      return;
    }
  }
}

function mouseOverStep(stepEl) {
  // change the bg image
  stepEl.className = stepEl.className.replace(/$/, ' hover');
  
  // find the first link el
  var firstAEl = null;
  for (var i = 0; i < stepEl.childNodes.length; i++) {
    var child = stepEl.childNodes[i];
    if (child.tagName == 'A') {
      firstAEl = child;
      break;
    }
  }
  if (!firstAEl)
    return;
  
  // find the image in the a tag
  var imgEl = null;
  for (var i = 0; i < firstAEl.childNodes.length; i++) {
    var child = firstAEl.childNodes[i];
    if (child.tagName == 'IMG') {
      imgEl = child;
      break;
    }
  }
  if (!imgEl)
    return;
  
  imgEl.src = imgEl.src.replace(/(.*)\.gif$/, '$1-active.gif');
}

function mouseOutStep(stepEl) {
  // change the bg image
  stepEl.className = stepEl.className.replace(' hover', '');
  
  // find the first link el
  var firstAEl = null;
  for (var i = 0; i < stepEl.childNodes.length; i++) {
    var child = stepEl.childNodes[i];
    if (child.tagName == 'A') {
      firstAEl = child;
      break;
    }
  }
  if (!firstAEl)
    return;
  
  // find the image in the a tag
  var imgEl = null;
  for (var i = 0; i < firstAEl.childNodes.length; i++) {
    var child = firstAEl.childNodes[i];
    if (child.tagName == 'IMG') {
      imgEl = child;
      break;
    }
  }
  if (!imgEl)
    return;
  
  imgEl.src = imgEl.src.replace(/(.*)-active\.gif$/, '$1.gif');
}

function sidebarSizeClick(event) {
	el=Event.element(event);
	idpattern = /(.*)_footer_(.*)/;
	
	//Find a match for the footer div id strint to know which action to take.
	//If there is no mathc try the parent - the click may have landed inside a child
	bits=idpattern.exec(el.id);
	if(!bits) {
		bits=idpattern.exec(el.parentNode.id);
		if(!bits)
		  return;
	}
	type=bits[1];
	state=bits[2];

	switch(state) {
		case 'collapsed':
  	  $(type+'_list_sidebar').style.height='auto';
      $(type+'_footer_collapsed').hide();
			$(type+'_footer_expanded').show();
		  break;
		case 'expanded':
   	  $(type+'_list_sidebar').style.height='20px';
      $(type+'_footer_collapsed').show();
			$(type+'_footer_expanded').hide();
		  break;
	}
}


