loadScript("/exp_sb.aspx?v=090421");	

function add_box()
{
	var p = $i("expertise");
	var nd = document.createElement("div");
	nd.setAttribute("id", "d_" + _g_id);
	nd.className = "ex_dc";
	p.appendChild(nd);
	
	var o = $i("d_" + _g_id);
	
	var html = "<select name=\"c_" + _g_id + "\" id=\"c_" + _g_id + "\" onchange=\"pop_2d(this);\">";

	html = html.concat("<option value=\"0\">Select a Category</option>");
	for(var k in _g_c_c)
	{
		html = html.concat("<option value=\"" + k + "\">" + _g_ctg[k] + "</option>");
	}
	html = html.concat("</select><select name=\"cc_" + _g_id + "\" id=\"cc_" + _g_id
		+ "\" style=\"display:none;\"></select><a href=\"#remove\" onclick=\"return rem_box(this);\">Remove</a>");

	o.innerHTML = html;
	
	_g_id ++;
}

function pop_2d(p)
{
	if(p == null || p.tagName != "SELECT") return false;
	var v = p.value;
	var id = p.id.split("_")[1];

	var o = $i("cc_" + id);
	if(o == null || o.tagName != "SELECT") return false;

	var a = _g_c_c[v];
	//For childless categories
	if(a == null)
	{
		o.style.display = "none";
		return;
	}
	else
		o.style.display = "";
	
	while(null!=o.lastChild)
		o.removeChild(o.lastChild);
		
	o.options[0] = new Option("Any", -1);
	var val;
	for(var i = 0; i < a.length; i++)
	{
		var val = _g_ctg[a[i]];
		if(undefined!=val)
			o.options[o.options.length] = new Option(val, a[i]);
	}
}

function rem_box(o)
{
	if(o == null || o.tagName != "A") return false;
	var div = o.parentNode;

	if(div == null || div.tagName != "DIV") return false;
	div.parentNode.removeChild(div);

	return false;
}

function render_box(c1, c2)
{
	var p = null;
	var item = _g_c_c[c1];
	if(c1 != null && c1 != undefined && (undefined!=item || 0==c1))
	{
		add_box();
		p = $i("c_" + (_g_id - 1));
		p.value = c1;
		pop_2d(p);
		
		if(-1!=c2 && undefined!=c2 && undefined!=_g_ctg[c2])
		{
			p = $i("cc_" + (_g_id - 1));
			p.value = c2;
		}
	}
	else if(undefined==item)
	{
		if(syncCategoryHirarchy(c1, c2))
			render_box(_g_c1, _g_c2)
	}
}

var _g_c1 = -1, _g_c2 = -1;
function syncCategoryHirarchy(c1, c2)
{
	if(-1!=c2 && undefined!=c2)
	{
		if(undefined!=_g_c_c[c2])
		{
			_g_c1 = c2;
			_g_c2 = -1;
			return true;
		}
		else
		{
			var cc1 = findParentCategory(c2);
			if(-1!=cc1)
			{
				_g_c1 = cc1;
				_g_c2 = c2;
				return true;
			}
			else
			{
				return false;	
			}	
		}	
	}
	else if(-1!=c1 && undefined!=c1)
	{
		var cc1 = findParentCategory(c1);
		if(-1!=cc1)
		{
			_g_c1 = cc1;
			_g_c2 = c1;
			return true;
		}
		else
		{
			return false;	
		}	
	}
}

function findParentCategory(c)
{
	for(var ac in _g_c_c)
	{
		for(var cc in _g_c_c[ac])
		{
			if(_g_c_c[ac][cc]==c)
				return ac;
		}
	}
	return -1;
}

