//------------------------------------
//  retrieve target for event
//------------------------------------

function getTarget(e) {
	var target;
	if (e.target) target = e.target;
	else if (e.srcElement) target = e.srcElement;
	if (target.nodeType == 3) // defeat Safari bug
		target = target.parentNode;

	return target;
}

//------------------------------------
//  retrieve related target for event
//------------------------------------

function getRelatedTarget(e) {
	return (e.relatedTarget) ? e.relatedTarget : e.toElement;
}

function addListener(obj, event, handler) {
	if (obj.addEventListener) {
		obj.addEventListener(event, handler, false);
	} else if (obj.attachEvent) {
		obj.attachEvent('on' + event, handler);
	} else {
		window.status = 'Please upgrade to more recent browser like ie6.0 or firefox.';
	}
}

function addStyleSheet(title, href) {
	var head = document.getElementsByTagName('head')[0];
	var link = document.createElement('link')
	link.rel = 'stylesheet';
	link.type = 'text/css';
	link.title = '';
	link.href = href;
	
	head.appendChild(link);
}

// load our stylesheet
addStyleSheet('menu.horizontal', 'css/menu.css');

// register onload init
addListener(window, 'load', addBehavior);

//------------------------------------
//
//------------------------------------

function addBehavior() {
	// get menu roots
	var node;
	var i = 0;
	var k = 1;
	while((node = document.getElementsByTagName('UL').item(i++))) {
		if (node.className == 'hmenu') {
			for (var j = 0; j < node.childNodes.length;j++) {
				if (node.childNodes[j].tagName == 'LI')
					initMenuItem(node.childNodes[j], k++);
			}
		}
	}
}

function initMenuItem(li, num) {
	// to emulate :first-child
	if (num == 1) {
		li.style.borderTopWidth = '1px';
		li.style.borderLeftWidth = '1px';
	}

	for (var i = 0; i < li.childNodes.length;i++) {
		if (li.childNodes[i].tagName == 'UL') {
			var nodeUL = li.childNodes[i];
			li.cachedUL = nodeUL;
			// 1 + n-level submenu
			if (li.parentNode.className != 'hmenu') {
				// background arrow
				li.style.backgroundRepeat = 'no-repeat';
				li.style.backgroundImage = 'url("css/images/sub2.gif")';
				//li.style.backgroundColor = '#3b4a56';
				li.style.backgroundPosition = '99%';
			}
			addListener(li, 'mouseover', showSubmenu);
			addListener(li, 'mouseout',  hideSubmenu);

			var k = 1;
			for (var j = 0; j < nodeUL.childNodes.length;j++) {
				if (nodeUL.childNodes[j].tagName == 'LI')
					initMenuItem(nodeUL.childNodes[j], k++);
			}
		}
	}
}

function showSubmenu(e) {
	if (!e) var e = window.event;
	var target = getTarget(e);
	if (target.tagName == 'A')
		target = target.parentNode;
	if (target.cachedUL) {
		target.cachedUL.style.display = 'block';
	}
}

function hideSubmenu(e) {
	if (!e) var e = window.event;
	var target = getTarget(e);
	var related = getRelatedTarget(e);
	if (!related || related.tagName == 'HTML')
		related = document.getElementsByTagName('body')[0];

	// maybe mouse left only into successor, handle
	if (target.tagName == 'A')
		target = target.parentNode;
	if (isAncestor(target, related))
		return;
	// it left into ancestor
	// find ul upon related node or body
	var commonUL = related;
	while (commonUL.tagName != 'UL' && commonUL.tagName != 'BODY')
		commonUL = commonUL.parentNode;
	// iterate up the tree from target till commonUL and close submenu
	while (target != commonUL && target.tagName != 'BODY') {
		if (target.cachedUL)
			target.cachedUL.style.display = 'none';
		target = target.parentNode;
	}
}

function isAncestor(ancestor, child) {
	while (child != ancestor && child.nodeName != 'BODY')
		child = child.parentNode;
	return child == ancestor ? 1 : 0;
}

function cnt(abstract,x){
  var y=abstract.value;
  var r = 0;
  a=y.replace('\n',' ');
  a=a.split(' ');
  for (z=0; z<a.length; z++) {
    if (a[z].length > 0) r++;
  }
  x.value=r;
}

function loadFormRules() {
    var rules = new Array();
    rules.push("required,title,Please enter a title.");
    rules.push("required,authors,Please enter the author or authors.");
    rules.push("required,organisation,Please enter an organisation.");
    rules.push("required,country,Please enter your home country.");
    rules.push("required,corresponding,Please enter the corresponding author.");
    rules.push("required,address,Please enter an address.");
    rules.push("valid_email,email,Please enter a valid e-mail address.");
    rules.push("required,email,Please enter an e-mail address.");
    rules.push("required,telephone,Please enter a telephone number.");
    rules.push("required,abstract,Please enter an abstract.");
    rules.push("required,presentation,Please specify the type of presentation");
    return rules;
}

function fill_fields() {
  document.abstract_submission.title.value = "Tissue Engineering Mood-Changing Hair Implants.";
  document.abstract_submission.authors.value = "* Blair A., Chirac J., Balkenende JP., Merkel A., Erdogan T.";
  document.abstract_submission.organisation.value = "UCL, Dept Mining Mineral & Materials, Regents Park, London, W2C 14U. United Kingdom. *QMUL, Anaesthetics Research Centre, Chelsea Campus, London, SW1 2EE. UK.";
  document.abstract_submission.abstract.value = "It is now well established that mood is critically dependent on the type and location of hair growth, with optimal follicle density increasing mean happiness index (MHI) by 2 fold in politicians1. Follicle distribution and density, then, maybe major determinants of European Harmony; even world peace. We investigated the possibility that engineered hair could be controlled enough to direct decision-making.\n\nEngineered injectable cell-matrix constructs (black hair follicle cells (BHFCs) were seeded at 3x105 cell/ml into a fibrin-alginate bead suspension in DMEM, as described prevously1 : mean particle diameter 255+/- 13µm. Particles were injected into the scalp and forearm of 15 normal, informed volunteer global leaders (with full ethical approval). At the end of 3 mood-decision cycles the follicular density was measured for correlation against their MHI and composite official dictat score (CODS) (1).\n\nGrowth of new tufts was found to be highly controllable in terms of density and spatial location, with a dose-response curve correlation coefficient of 0.855 against MHI and CODS. Ultrasound analysis of the hair-beds indicated 95% survival of follicles, though this was 4 fold lower in leaders sharing strong hereditary links with their predecessors. We conclude that successful tissue engineering of follicle implants can have major implications for modern politics. Further research is needed to improve both colour (red and white) and subject acceptance.\n\n1. Walbeehm E. et al. (2007) .We do hope that no one returns this abstract: it will not be accepted. J.Cell Abus. 3:12-19.\n\nWe are grateful for funding from the Bush Foundation.";
  return;
}
