// Show and hide submenu

function ShowHide(id, display) {
alert(display);
    obj = document.getElementsByTagName("div");
    obj[id].style.display = display;
}

function doWhenElementAvailable(elementid, func, delay)
{
    delay = delay || 0;

	var timerid = setInterval(function () {
		var node = document.getElementById(elementid);

		if (node) {
			clearInterval(timerid);
			setTimeout(func, delay);
		}
	}, 100);
}


function lstSelectAll(controlid)
{
    var control = getObj(controlid);    
    if(control)
    {    
        for(i=0; i< control.options.length; i++)
        {
            control.options[(control.options.length-1)-i].selected = true;
        }
    }
    
}

function lstDeselectAll(controlid)
{
    var control = getObj(controlid);
    if(control)
    {    
        for(i=0; i< control.options.length; i++)
        {
            control.options[i].selected = false;
        }
    }
}

function getObj(name)
{
  if (document.getElementById)
  {
  	return document.getElementById(name);	
  }
  else if (document.all)
  {
	return document.all[name];
  }
  else if (document.layers)
  {
    return document.layers[name];
  }
}



function doWhenElementAvailable(elementid, func, delay)
{
    delay = delay || 0;

	var timerid = setInterval(function () {
		var node = document.getElementById(elementid);

		if (node) {
			clearInterval(timerid);
			setTimeout(func, delay);
		}
	}, 100);
}
function setLocationsDDLSelection(locationddlid,values)
{
    var locationddl = document.getElementById(locationddlid);
    for (var i=0; i<locationddl.options.length; i++) {        
        if (values.indexOf('|' + locationddl.options[i].value + '|') > -1)
        {        
            locationddl.options[i].selected = true;
        }
    }
     
}
function updateLocationsDDL(locationddlid, globalregionid)
{
    var locationddl = document.getElementById(locationddlid);
    var idzerooptiontext = null;
    var selectedids = '|';
    
    if (locationddl.options.length > 0 && parseInt(locationddl.options[0].value) === 0) {
        idzerooptiontext = locationddl.options[0].text;
    }
    
    
    
    for (var i=0; i<locationddl.options.length; i++) {        
        if (locationddl.options[i].selected)
        {
          selectedids +=  locationddl.options[i].value + '|'
        }
    }
    
    locationddl.options.length = 0;
    
    /*
    if (idzerooptiontext !== null) {
        var newopt = document.createElement("option");
        newopt.value = 0;
        newopt.innerText = idzerooptiontext;
        newopt.text = idzerooptiontext;
        locationddl.appendChild(newopt);
        if (selectedid === 0) {
            setTimeout(function () { locationddl.selectedIndex = 0}, 5); // setTimeout req'd for IE6
        }
    }
    */
        
    for (var i=0; i<locationoptionsperregion['gl:' + globalregionid].length; i++) {
        var o = locationoptionsperregion['gl:' + globalregionid][i];
        var newopt = document.createElement("option");
        newopt.value = o[0];
        newopt.innerText = o[1];
        newopt.text = o[1];
        locationddl.appendChild(newopt);
        /*if (selectedid === o[0]) {
            var selectedidx = locationddl.options.length-1;
            setTimeout(function () { locationddl.selectedIndex = selectedidx}, 5); // setTimeout req'd for IE6
        }*/
    }

    setTimeout(function () {setLocationsDDLSelection(locationddlid,selectedids)}, 5); // setTimeout req'd for IE6

    return;    
}

function updateSalaryDDL(salaryddlid, jobtypeid)
{
    /* alert('updating salaries : ' + jobtypeid); */
    var salaryddl = document.getElementById(salaryddlid);
    var idzerooptiontext = null;
    var selectedid = null;
    
    if (salaryddl.options.length > 0 && parseInt(salaryddl.options[0].value) === 0) {
        idzerooptiontext = salaryddl.options[0].text;
    }
    
    if (salaryddl.selectedIndex >= 0) {
        selectedid = parseInt(salaryddl.options[salaryddl.selectedIndex].value, 10);
    }
    
    salaryddl.options.length = 0;
    
    if (idzerooptiontext !== null) {
        var newopt = document.createElement("option");
        newopt.value = 0;
        newopt.innerText = idzerooptiontext;
        newopt.text = idzerooptiontext;
        salaryddl.appendChild(newopt);
        if (selectedid === 0) {
            setTimeout(function () { salaryddl.selectedIndex = 0}, 5); // setTimeout req'd for IE6
        }
    }
    
    for (var i=0; i<salaryoptionsperjobtype['jt:' + jobtypeid].length; i++) {
        var o = salaryoptionsperjobtype['jt:' + jobtypeid][i];
        var newopt = document.createElement("option");
        newopt.value = o[0];
        newopt.innerText = o[1];
        newopt.text = o[1];
        salaryddl.appendChild(newopt);
        if (selectedid === o[0]) {
            var selectedidx = salaryddl.options.length-1;
            setTimeout(function () { salaryddl.selectedIndex = selectedidx}, 5); // setTimeout req'd for IE6
        }
    }

    return;    
}

function cloneSubElement(source)
{
    
    var toCopy = document.getElementById(source + '_##subcount');    
    var toInsert = toCopy.cloneNode(true);
    var container = document.getElementById(source + '_container')
    
    toInsert.style.display='';               
    
    var next = parseInt(document.getElementById(source + '_next').value);
    updateIDs(toInsert,'##subcount',next)
    container.appendChild(toInsert);
    
    document.getElementById(source + '_next').value = next + 1;
        
    var dataitems = '';
    
    
    for(var i=0; i<container.childNodes.length; i++){       
        if ( container.childNodes[i].tagName == 'TABLE' ) 
        {          
          if( dataitems != '' ) dataitems += ',';
          dataitems += container.childNodes[i].id.replace(source + '_','');
        }        
    }
    
    document.getElementById(source + '_dataitems').value = dataitems;
}

function cloneKillerSubElement(source,fail)
{
    
    var toCopy = document.getElementById(source + '_##subcount');    
    var toInsert = toCopy.cloneNode(true);
    var container = document.getElementById(source + '_container')
    
    toInsert.style.display='';               
    
    var next = parseInt(document.getElementById(source + '_next').value);
    updateIDs(toInsert,'##subcount',next)    
    updateIDs(toInsert,'##fail_value',fail)
    
    if(fail == '1') updateIDs(toInsert,'##fail_label','Fail')
    if(fail == '0') updateIDs(toInsert,'##fail_label','Pass')
    
    if(fail == '1') updateIDs(toInsert,'##fail_reason_display','');
    if(fail == '0') updateIDs(toInsert,'##fail_reason_display','display:none;');
    
    container.appendChild(toInsert);
    
    document.getElementById(source + '_next').value = next + 1;
        
    var dataitems = '';
    
    
    for(var i=0; i<container.childNodes.length; i++){       
        if ( container.childNodes[i].tagName == 'TABLE' ) 
        {          
          if( dataitems != '' ) dataitems += ',';
          dataitems += container.childNodes[i].id.replace(source + '_','');
        }        
    }
    
    document.getElementById(source + '_dataitems').value = dataitems;
}

function deleteElement(source)
{
    var container = source.parentNode;    
    source.parentNode.removeChild(source);       
    var dataitems = '';
    
    for(var i=0; i<container.childNodes.length; i++){       
        if ( container.childNodes[i].tagName == 'TABLE' ) 
        {          
          if( dataitems != '' ) dataitems += ',';
          var thisid = container.childNodes[i].id;         
          dataitems += thisid.substring(thisid.lastIndexOf('_')+1);
        }
        if ( container.childNodes[i].tagName == 'DIV' ) 
        {       
          if( dataitems != '' ) dataitems += ',';   
          var thisid = container.childNodes[i].id;         
          dataitems += thisid.substring(thisid.lastIndexOf('_')+1);
        }
    }
    document.getElementById(container.id.replace('_container','_dataitems')).value = dataitems;
}

function cloneElement(source)
{
    
    var toCopy = document.getElementById(source + '_##count');
    var toInsert = toCopy.cloneNode(true);
    var container = document.getElementById(source + '_container')
    toInsert.style.display='';               
    
    updateIDs(toInsert,'##count',eval(source + '_next'))
    
    container.appendChild(toInsert);
    
    eval(source + '_next = ' + source + '_next + 1');
    var dataitems = '';
    
    
    for(var i=0; i<container.childNodes.length; i++){       
        if ( container.childNodes[i].tagName == 'TABLE' ) 
        {          
          if( dataitems != '' ) dataitems += ',';
          dataitems += container.childNodes[i].id.replace(source + '_','');
        }
        if ( container.childNodes[i].tagName == 'DIV' ) 
        {          
          if( dataitems != '' ) dataitems += ',';
          dataitems += container.childNodes[i].id.replace(source + '_','');
        }
    }
    
    document.getElementById(source + '_dataitems').value = dataitems;
}


function updateIDs(rootnode,find,replacement)
{
    if (rootnode.id) { rootnode.id = rootnode.id.replace(find,replacement) };
    if (rootnode.name) { rootnode.name = rootnode.name.replace(find,replacement) };
    
    var toreplace = rootnode.getElementsByTagName("input"); 
    
    for (var i = 0; i < toreplace.length; i++) { 
        if (toreplace[i].id) { 
            toreplace[i].id = toreplace[i].id.replace(find,replacement);
            toreplace[i].name=toreplace[i].id;
            toreplace[i].value = toreplace[i].value.replace(find,replacement);
            toreplace[i].className = toreplace[i].className.replace(find,replacement);
                    }
    }
    var toreplace = rootnode.getElementsByTagName("textarea"); 
    for (var i = 0; i < toreplace.length; i++) { 
        if (toreplace[i].id) { 
            toreplace[i].id = toreplace[i].id.replace(find,replacement);
            toreplace[i].name=toreplace[i].id;
            toreplace[i].value = toreplace[i].value.replace(find,replacement);
            toreplace[i].className = toreplace[i].className.replace(find,replacement);
        }
    }
    var toreplace = rootnode.getElementsByTagName("div"); 
    for (var i = 0; i < toreplace.length; i++) { 
        if (toreplace[i].id) { 
            toreplace[i].id = toreplace[i].id.replace(find,replacement);
            toreplace[i].name=toreplace[i].id;            
            toreplace[i].className = toreplace[i].className.replace(find,replacement);            
        }
    }
    var toreplace = rootnode.getElementsByTagName("table"); 
    for (var i = 0; i < toreplace.length; i++) { 
        if (toreplace[i].id) { 
            toreplace[i].id = toreplace[i].id.replace(find,replacement);
            toreplace[i].name=toreplace[i].id;            
            toreplace[i].className = toreplace[i].className.replace(find,replacement);
        }
    }
    var toreplace = rootnode.getElementsByTagName("tr"); 
    for (var i = 0; i < toreplace.length; i++) { 
            toreplace[i].id = toreplace[i].id.replace(find,replacement);
            toreplace[i].name=toreplace[i].id;            
            toreplace[i].className = toreplace[i].className.replace(find,replacement);
    }        
    var toreplace = rootnode.getElementsByTagName("label"); 
    for (var i = 0; i < toreplace.length; i++) { 
            toreplace[i].id = toreplace[i].id.replace(find,replacement);
            toreplace[i].name=toreplace[i].id;            
            toreplace[i].className = toreplace[i].className.replace(find,replacement);
            toreplace[i].innerHTML = toreplace[i].innerHTML.replace(find,replacement);
    }  
}



function setExpertiseListSelection(expertiselistid,values)
{
    var expertiselist = document.getElementById(expertiselistid);
    for (var i=0; i<expertiselist.options.length; i++) {        
        if (values.indexOf('|' + expertiselist.options[i].value + '|') > -1)
        {        
            expertiselist.options[i].selected = true;
        }
    }
     
}

function updateExpertiseList(expertiselistid, specialismid)
{
    var expertiselist = document.getElementById(expertiselistid);
    var idzerooptiontext = null;
    var selectedids = '|';
    
    if (expertiselist.options.length > 0 && parseInt(expertiselist.options[0].value) === 0) {
        idzerooptiontext = expertiselist.options[0].text;
    }
    
    for (var i=0; i<expertiselist.options.length; i++) {        
        if (expertiselist.options[i].selected)
        {
          selectedids +=  expertiselist.options[i].value + '|'
        }
    }
           
    expertiselist.options.length = 0;

    if(expertiseperspecialism['sp:' + specialismid]) 
    {
        for (var i=0; i<expertiseperspecialism['sp:' + specialismid].length; i++) {
            var o = expertiseperspecialism['sp:' + specialismid][i];
            var newopt = document.createElement("option");
            newopt.value = o[0];
            newopt.innerText = o[1];
            newopt.text = o[1];
            expertiselist.appendChild(newopt);
        }
    }
    setTimeout(function () {setExpertiseListSelection(expertiselistid,selectedids)}, 5); // setTimeout req'd for IE6

    return;    
}

function setJobFunctionListSelection(jobfunctionlistid,values)
{
    var jobfunctionlist = document.getElementById(jobfunctionlistid);
    for (var i=0; i<jobfunctionlist.options.length; i++) {        
        if (values.indexOf('|' + jobfunctionlist.options[i].value + '|') > -1)
        {        
            jobfunctionlist.options[i].selected = true;
        }
    }
     
}

function updateJobFunctionList(jobfunctionlistid, specialismid)
{
    var jobfunctionlist = document.getElementById(jobfunctionlistid);
    var idzerooptiontext = null;
    var selectedids = '|';
    
    if (jobfunctionlist.options.length > 0 && parseInt(jobfunctionlist.options[0].value) === 0) {
        idzerooptiontext = jobfunctionlist.options[0].text;
    }

    for (var i=0; i<jobfunctionlist.options.length; i++) {        
        if (jobfunctionlist.options[i].selected)
        {
          selectedids +=  jobfunctionlist.options[i].value + '|'
        }
    }
        
    jobfunctionlist.options.length = 0;
    if(jobfunctionperspecialism['sp:' + specialismid]) 
    {
        for (var i=0; i<jobfunctionperspecialism['sp:' + specialismid].length; i++) {
            var o = jobfunctionperspecialism['sp:' + specialismid][i];
            var newopt = document.createElement("option");
            newopt.value = o[0];
            newopt.innerText = o[1];
            newopt.text = o[1];
            jobfunctionlist.appendChild(newopt);

        }
    }

    setTimeout(function () {setJobFunctionListSelection(jobfunctionlistid,selectedids)}, 5); // setTimeout req'd for IE6

    return;    
}
