var xmlhttp;
var id;
var component;
var url;
var method;
var hideble;
//var path = "/74mail/firms";
var path = "/firms";
var tools = new Tools();
var cons = new Console();
var ids = "";

HttpXMLUtils = function()
{
	
}	



HttpXMLUtils.init = function()
{
	if(xmlhttp == null)
	{
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6 
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
}

HttpXMLUtils.collect = function(data)
{
	
	var collector = $('#collector');
	var displayItems = $('.selected-ids');
	for(var i=0; i < displayItems.length; i++)
	{
		var text = displayItems[i].innerHTML;
		var name = $('#subcat-'+data)[0].nextSibling.textContent;
		var html = '<div id="display-'+data+'" style="cursor:pointer; float: left" onclick="removeEl(\''+ data +'\')><b>'+ ((text == "")?'':', ') + name + '</b></div>';
		ids += data+",";
		displayItems[i].innerHTML = text + html;
	}
	$('#keys').val(ids);
	
	
	collector[0].value = collector[0].value + "[" + data + "]";
}

HttpXMLUtils.decollect = function(data)
{
	var collector = $('#collector');
	var displayItems = $('.selected-ids');
	for(var i=0; i < displayItems.length; i++)
	$('#display-'+data).remove();
	collector[0].value = collector[0].value.replace("[" + data + "]","");
}

HttpXMLUtils.loadPage = function(component1,url1,method1)
{
	HttpXMLUtils.init();
	component = component1;
	url = url1;
	method = method1;
	
	xmlhttp.onreadystatechange =(function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			var div = document.getElementById(component);
			div.innerHTML=xmlhttp.responseText;
		}
	});
 
	xmlhttp.open(method,url,true);
	xmlhttp.send();
}

HttpXMLUtils.hide = function(component)
{
	var div = document.getElementById(component);
	div.style.display = "none";
}

HttpXMLUtils.show = function(component)
{
	var div = document.getElementById(component);
	div.style.display = "block";
}

HttpXMLUtils.changeClass = function(component,class1,class2)
{
	var div = document.getElementById(component);
	if(div.getAttribute("class") == class2 || div.getAttribute("className") == class2)
	{
		div.setAttribute("class", class1);
		div.setAttribute("className", class1);
	}
	else
	{
		div.setAttribute("class", class2);
		div.setAttribute("className", class2);
	}
}

HttpXMLUtils.showOrHide = function(component)
{
	var div = document.getElementById(component);
	if(div.style.display == "block")
	{
		div.style.display = "none";
	}
	else
	{
		div.style.display = "block";
	}
}

function showSubCategory(id) 
{
	var el;
	
	if(document.getElementById("cdiv"+id) != null)
	{	
		HttpXMLUtils.loadPage("cdiv"+id,tools.urlb('urlSublist','urlSublistCatParam',id),"GET");
		HttpXMLUtils.showOrHide("cdiv"+id);
	}
	
	HttpXMLUtils.changeClass("li"+id,"str_v","str_n")
}

function removeEl(id)
{
	HttpXMLUtils.decollect(id);
}

function collectData(id)
{
	if($('#' + id) == null) 
	{
		cons.printErr('element with id=' + id + 'not found');
		return;
	}
	if($('#subcat-' + id)[0].checked)
	{
		HttpXMLUtils.collect(id);
	}
	else
	{
		HttpXMLUtils.decollect(id);
	}
}

function showFirmList(id)
{
	HttpXMLUtils.loadPage(id,"firm_container",path+"/list?subcatId=","GET");
}

function showFirm(id)
{
	HttpXMLUtils.loadPage(id,"firm_container",path+"/firm-item?firmId=","GET");
}
