function addEventHandler(o,evtName,evtHandler) {
  typeof(attachEvent)=='undefined'?o.addEventListener(evtName,evtHandler,false):o.attachEvent('on'+evtName,evtHandler);
}

function removeEventHandler(o,evtName,evtHandler) {
  typeof(attachEvent)=='undefined'?o.removeEventListener(evtName,evtHandler,null):o.detachEvent('on'+evtName,evtHandler);
}

function showTab(e) {
  var e = e?e:event;
  if (e.target) obj = e.target;
  if (e.srcElement) obj = e.srcElement;
  if (obj.nodeType == 3) obj = obj.parentNode; // defeat Safari bug
  document.getElementById('tabheadUpload').className = 'tabheadOff';
  document.getElementById('tabheadSearch').className = 'tabheadOff';
  document.getElementById('tabheadAbout').className = 'tabheadOff';
  document.getElementById('tabheadUpload').parentNode.style.zIndex = 1;
  document.getElementById('tabheadSearch').parentNode.style.zIndex = 1;
  document.getElementById('tabheadAbout').parentNode.style.zIndex = 1;
  obj.className = 'tabheadOn';
  obj.parentNode.style.zIndex = 5;
  if (obj.id == 'tabheadSearch') {
    document.getElementById('tabSearch').style.display = 'block';
    document.getElementById('tabUpload').style.display = 'none';
    document.getElementById('tabAbout').style.display = 'none';
  }
  else if (obj.id == 'tabheadUpload') {
    document.getElementById('tabSearch').style.display = 'none';
    document.getElementById('tabUpload').style.display = 'block';
    document.getElementById('tabAbout').style.display = 'none';
  }
  else if (obj.id == 'tabheadAbout') {
    document.getElementById('tabSearch').style.display = 'none';
    document.getElementById('tabUpload').style.display = 'none';
    document.getElementById('tabAbout').style.display = 'block';
  }
}

function searchSubmit() {
  var s = document.forms['search'].elements['expertise'].value;
  return true;
}

function revealExpertiseDetails() {
  $('.hideme').show('normal');
  //setTimeout('window.scrollBy(0,300)', 1000);
  var obj = document.getElementById('expertise1');
  removeEventHandler(obj, 'enter', revealExpertiseDetails);
  removeEventHandler(obj, 'keyup', revealExpertiseDetails);
  removeEventHandler(obj, 'click', revealExpertiseDetails);
}

var level2exp = [];
// blank
level2exp[0] = [];
//Humanities
level2exp[1] = ["","Philosophy","Religion","Art","Literature","Linguistics"];
//Technology
level2exp[2] = ["","Information Technology","Industrial Technology","Applied Technology","Computer Engineering/Science","Interdisciplinary Expertise"];
//Natural / Life Sciences
level2exp[3] = ["","Astronomy","Biology","Chemistry","Earth science","Physics","Cross-disciplines","Interdisciplinary Expertise"];
//Social Sciences
level2exp[4] = ["","Anthropology","Economics","Education","Geography","History","Law","Political science","Business and Management","Interdisciplinary Expertise"];
//Emergency Management
level2exp[5] = ["","Medical","Health and Safety","Communication","Transport","Building","Firefighting","Mass Care","Housing and Human Care","Search and Rescue","Hazardous Materials","Environmental Risks","All risks management expertise"];
//Other
level2exp[6] = [];


function expertFieldChange(obj) {
  var i = obj.selectedIndex;
  var id = obj.id;
  var obj2 = document.getElementById(id + "_level2");
  var soptions = ""
  for (var j=0; j<level2exp[i].length; j++) {
    soptions += "<option>" + level2exp[i][j] + "</option>";
  }
  obj2.innerHTML = soptions;
  soptions == "" ? obj2.style.display='none':obj2.style.display='inline';
}

function addAnother(context, thing) {
  sContext = "";
  if (context != null) {
    for (var icount = 0; icount < context.length; icount++) {
      sContext += context[icount] + "_";
    }
  }
  idEnd = '#' + sContext + thing + 'end';

  // find the last node and it's number
  var lasthtml = $(idEnd).prev().html();
  var lastname = $(idEnd).prev().find('*[@name]').attr('name');
  var num = lastname.substr(sContext.length + thing.length);
  var i = num.indexOf('_');
  if (i > -1) num = num.substr(0,i);
  num = parseInt(num);
  num++;
  // copy the master and update details with new number
  var base = clones[thing];
  var oID = base.attr('id');
  var tag = document.getElementById(oID).tagName;
  var newhtml = base.html();
  // replace the original context with the new
  var sContextBase = "";
  if (context != null) {
    i = oID.indexOf(thing + 'base');
    sContextBase = oID.substr(0, i);
  }
  var re = new RegExp(sContextBase + thing + '1', 'g');
  newhtml = newhtml.replace(re, sContext + thing + num.toString());
  re = new RegExp('#1', 'g');
  newhtml = newhtml.replace(re, '#'+num.toString());
  // add the new thing at the end, before the end marker
  $(idEnd).before("<" + tag + ">" + newhtml + "</" + tag + ">");
}

var clones = new Object();

// when the document is ready
$(document).ready(function () {
  $(".hideme").hide();
  $("div")
    .filter(".clickme")
      .click(function(){
        alert("You just clicked.");
        return false;
      })
    .end();
  var obj = document.getElementById('expertise1');
  addEventHandler(obj, 'enter', revealExpertiseDetails);
  addEventHandler(obj, 'keyup', revealExpertiseDetails);
  addEventHandler(obj, 'click', revealExpertiseDetails);
  // first level
  clones["language"] = $("#languagebase");
  clones["expertise"] = $("#expertisebase");
  // second level
  clones["education"] = $("#expertise1_educationbase");
  clones["workExperience"] = $("#expertise1_workExperiencebase");
  clones["interest"] = $("#expertise1_interestbase");
  clones["publication"] = $("#expertise1_publicationbase");
  clones["contribution"] = $("#expertise1_contributionbase");
  clones["grantsAndAwards"] = $("#expertise1_grantsAndAwardsbase");
  clones["skill"] = $("#expertise1_skillbase");
  clones["other"] = $("#expertise1_otherbase");
  clones["fluency"] = $("#expertise1_fluencybase");

});

