
function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
}
createXMLHttpRequest();

function clearnodes(el)
{
	if ( el.hasChildNodes() )
	{
		while ( el.childNodes.length > 0 )
		{
			el.removeChild( el.firstChild );       
		} 
	}
}

var m_iModuleId;
function check_boxes(obj,module_id){
	ajaxxing = true;
	m_iModuleId = module_id;
		
	str = obj.id;
	str = str.replace("cat_id[","");

	var key = str.replace("]","");
	var next_key = parseInt(key)+1;

	e = document.getElementById("cat_id["+next_key+"]");
	var ehs = document.getElementById('cat_selected');
	var ehl = document.getElementById('cat_level');

	if (obj.value != 'NULL') {
		ehs.value = obj.value;
		ehl.value = next_key;
	} else {
		var eo = document.getElementById("cat_id["+(next_key-2)+"]");
		if(eo) {
			ehs.value = eo.value;	
			ehl.value = (next_key-2);
		} else {
			ehs.value = 'NULL';
			ehl.value = '1';
		}
	}

	if (e)	{
		var remaining_key = next_key + 1;
		var td = document.getElementById('select_box_holder');
		if(obj.value != 'NULL')
		{
			//e.options.length = 0;			
			e.parentNode.getElementsByTagName("div")[1].getElementsByTagName("div")[0].innerHTML = '<div class="content">Even geduld a.u.b.</div>';
			e.parentNode.getElementsByTagName("td")[1].firstChild.nodeValue = 'Even geduld a.u.b.';
		} else {
			td.removeChild(e);
		}
		while(r = document.getElementById("cat_id["+remaining_key+"]")){
			td.removeChild(r);
			remaining_key++;
		}

		xmlhttp.open("GET", "/logic/ajax/get_categories.php?cat_id="+obj.value+"&level_id="+next_key+"&module_id="+module_id, true);
		xmlhttp.onreadystatechange=set_options;
		xmlhttp.send(null);
	} else {
		xmlhttp.open("GET", "/logic/ajax/get_categories.php?cat_id="+obj.value+"&level_id="+next_key+"&module_id="+module_id, true);
		xmlhttp.onreadystatechange=set_options;
		xmlhttp.send(null);
	}
}


function createSanBox(name, opts,onchange,width)
{
	d = document.createElement('div');
	d.className = 'select';
	d.onselectstart = function() { return false; };
	d.onkeypress = function() { return selectkey(event, this); };
	d.onkeyup = function() { return false; };
	d.onkeydown = function() { return selectkey(event, this); };

	html='';
	html+='<input type="hidden" name="'+name+'" id="'+name+'" value="'+opts[0][1]+'" onchange="'+onchange+'"/>';
	html+='	<table cellpadding="0" cellspacing="0" onClick="clickselect(this);" onfocus="focusbox(this);" onmousewheel="return scrollwheel(event, this)" width="'+width+'">';
	html+='		<tr class="box">';
	html+='			<td class="left"></td>';
	html+='			<td class="selectvalue" onblur="blurbox(this.parentNode.parentNode.parentNode.parentNode,event)">'+opts[0][0]+'</td>';
	html+='			<td class="right"><div class="right"></div></td>';
	html+='		</tr>';
	html+='	</table>';
	html+='	<div class="optionbox" onMouseOver="hoveroptionbox()" onMouseOut="leaveoptionbox()">';
	html+='		<table cellpadding="0" cellspacing="0" width="100%">';
	html+='		<tr><td width="14"><img src="/images/dropdown/box/option_topleft.gif"/></td>';
	html+='			<td style="background: url(\'/images/dropdown/box/option_top.gif\');" />';
	html+='			<td width="14"><img src="/images/dropdown/box/option_topright.gif"/></td>';
	html+='		</tr>';
	html+='		<tr><td style="background: url(\'/images/dropdown/box/option_left.gif\');"/>';
	html+='			<td class="optioncontent"><div name="contentdiv" style="height: 145px; width: 400px; overflow: auto;">';

for(i = 0; i < opts.length; i++)
	html+='				<div class="option" onMouseOver="this.className=\'option_hover\'; if(oldselecthover != null) { oldselecthover.className=\'option\'; oldselecthover = null; }" onMouseOut="this.className=\'option\'" onClick="clickoption(this)" value="'+opts[i][1]+'">'+opts[i][0]+'</div>';


	html+='			</div></td>';
	html+='			<td style="background: url(\'/images/dropdown/box/option_right.gif\');"/>';
	html+='		</tr>';
	html+='		<tr><td><img src="/images/dropdown/box/option_bottomleft.gif"/></td>';
	html+='			<td style="background: url(\'/images/dropdown/box/option_bottom.gif\')" />';
	html+='			<td><img src="/images/dropdown/box/option_bottomright.gif"/></td>';
	html+='		</tr>';
	html+='		</table>';
	html+='	</div>';

	d.innerHTML = html;
	return d;
}

function set_options()
{
	if (xmlhttp.readyState==4)
	{
		if(xmlhttp.responseText)
		{
			var response = eval("(" + xmlhttp.responseText + ")");
			var categories = response.categories;
			
			var td = document.getElementById('select_box_holder');
			if(e)
			{			
				myoptionbox = e.parentNode.getElementsByTagName("div")[1].getElementsByTagName("div")[0];
				clearnodes(myoptionbox);
				d = document.createElement("div");
				d.className = 'option';
				d.appendChild(document.createTextNode("Gebruik bovenstaande categorie"));
				d.setAttribute('value', 'NULL');
				d.onmouseover = function() { this.className='option_hover'; if(oldselecthover != null) { oldselecthover.className='option'; oldselecthover = null; } };
				d.onmouseout = function() { this.className='option'; };
				d.onclick = function() { clickoption(this); };
				myoptionbox.appendChild(d);
				if(categories.category)
				{
					if(categories.category.length > 0)
					{
						var j=1;
						for(var i=0; i<categories.category.length; i++)
						{
							d = document.createElement("div");
							d.className = 'option';
							d.innerHTML = categories.category[i].title;
							d.setAttribute('value', categories.category[i].id);
							d.onmouseover = function() { this.className='option_hover'; if(oldselecthover != null) { oldselecthover.className='option'; oldselecthover = null; } };
							d.onmouseout = function() { this.className='option'; };
							d.onclick = function() { clickoption(this); };
							myoptionbox.appendChild(d);
							j++;
						}
					} else {
						str = e.id;
						str = str.replace("cat_id[","");
						var key = str.replace("]","");
						var prev_key = parseInt(key)-1;
						td.removeChild(e.parentNode);
						document.getElementById("cat_id[" + prev_key + "]").parentNode.getElementsByTagName("tr")[0].className = 'box';
					}
				} else {
					str = e.id;
					str = str.replace("cat_id[","");
					var key = str.replace("]","");
					var prev_key = parseInt(key)-1;
					td.removeChild(e.parentNode);
					document.getElementById("cat_id[" + prev_key + "]").parentNode.getElementsByTagName("tr")[0].className = 'box';
				}
				e.parentNode.getElementsByTagName("td")[1].firstChild.nodeValue = 'Gebruik bovenstaande categorie';
				e.setAttribute('value','NULL');
			}
			else
			{				
				var key = 1;
				while(document.getElementById("cat_id["+key+"]")){
					key++;
				}
						
				var opts = new Array();
				//var s = document.createElement("SELECT");
				//s.setAttribute("id","cat_id["+key+"]");
				//s.setAttribute("className","boxpulldown");
				//s.setAttribute("style","");
				//s.style.width = "100%";
				//s.attachEvent("onchange",function(){check_boxes(s,m_iModuleId)});

				opts[0] = new Array('Gebruik bovenstaande categorie','NULL');
				if(categories.category)
				{					
					if(categories.category.length > 0)
					{
						var j=1;
						for(var i=0; i<categories.category.length; i++)
						{
							opts[j] = new Array(categories.category[i].title, categories.category[i].id);
							j++;
						}
						var s = createSanBox("cat_id["+key+"]", opts,"check_boxes(this,"+m_iModuleId+")",350);
						td.appendChild(s);
						document.getElementById("cat_id[" + (key-1) + "]").parentNode.getElementsByTagName("tr")[0].className = 'blue';
					}
				}
			}
		}
		ajaxxing = false;
	}
}

function get_projects_for_customer(obj){
	var v;
	v = obj.value;
	
	createXMLHttpRequest();
	
	if(v > 0){
		
		xmlhttp.open("GET", "/logic/ajax/get_projects.php?id="+v, true);
		xmlhttp.onreadystatechange=set_projects;
		xmlhttp.send(null);
	} else {
		var sb = document.getElementById('project_id');
		sb.options.length = 1;
		sb.setAttribute("disabled",true);
	}
}
function set_projects(){
	var response, projects;
	
	if (xmlhttp.readyState==4)
	{
		if(xmlhttp.responseText)
		{
			response = eval("(" + xmlhttp.responseText + ")");
			
			projects = response.projects;
			var sb = document.getElementById('project_id');
			if(sb){
				if(projects.project)
				{
					sb.options.length = 1;
					if(projects.project.length > 0)
					{
						var j=1;
						for(var i=0; i<projects.project.length; i++)
						{
							sb.options[j] = new Option(projects.project[i].title, projects.project[i].id);
							j++;
						}
						sb.setAttribute("disabled",false);
					}
				} else
				{
					sb.options.length = 0;
					sb.options[0] = new Option('Selecteer een project', 0);
				}
			}
		}
	}
}
