var SearchDataCategoriesSelector = true; //for SearchData.js
var CatsParentWasInit = false;
var CatsParentState = 'Close';
var PrevParentId = '';
var ChoosenParentID = '';
var divCategoriesConteiner

function InitUserAgentDropDown()
{
	divCategoriesConteiner = document.createElement('div'); 
	divCategoriesConteiner.setAttribute('id','divCategoriesConteiner');
	var InitTxt = document.createTextNode('בחר תחומים');
	
	var divParentChoosenCategoriesConteiner;
	var txt;
	var divParentNameTextBox;
	var divImgArrow;
	var divBotton;
	var imgBotton;
	var div;
	
	divParentChoosenCategoriesConteiner = document.createElement('div');
	divParentChoosenCategoriesConteiner.setAttribute('id','divParentChoosenCategoriesConteiner');
	if(navigator.appName == 'Microsoft Internet Explorer')
		$(divParentChoosenCategoriesConteiner).width(210);
	
	divParentNameTextBox = document.createElement('div'); 
	divParentNameTextBox.setAttribute('id','divParentNameTextBox');
	
	txt = InitTxt;
	divImgArrow = document.createElement('div');
	divImgArrow.setAttribute('id','divImgArrow');
	
	divParentNameTextBox.appendChild(txt);
	divParentChoosenCategoriesConteiner.appendChild(divImgArrow);
	divParentChoosenCategoriesConteiner.appendChild(divParentNameTextBox);
	divParentChoosenCategoriesConteiner.onclick = function(){SetParentsAndCildrenCategoris();};
	divCategoriesConteiner.appendChild(divParentChoosenCategoriesConteiner);
	
	//Create The Botton Area And Error Msg
	imgBotton = document.createElement('img');
	imgBotton.setAttribute('id','imgBotton');
	imgBotton.setAttribute('src','/images/Landing_Page/NewLanging_Page/btnSave1.jpg');
	imgBotton.setAttribute('width','210px');
	imgBotton.setAttribute('height','25px');
	imgBotton.onclick = function(){checkMarketCategories();};
	
	div = document.createElement('div');
	div.setAttribute('id','divAutoCompleteMsg');
	
	divBotton = document.createElement('div'); 
	divBotton.setAttribute('id','divBotton');
	
	divBotton.appendChild(imgBotton);
	divBotton.appendChild(div);
	$(divBotton).attr('class','DisplayN');

	if(navigator.appName == 'Microsoft Internet Explorer')
		$(divBotton).width(210);
			
	divCategoriesConteiner.appendChild(divBotton);
	document.getElementById('divCategories').appendChild(divCategoriesConteiner);
}
	
function checkMarketCategories()
{
	$('#CategoriesIDs').val('');

	if(GetCategoriesIDs() == '')
	{
		$('#divParentNameTextBox').html('<span class="Red B">לא נבחרו תחומים רצויים</span>');
	}
	else
	{
		$('#divAutoCompleteMsg').html('');
		$('#divParentNameTextBox').html('<span class="Green B">התחומים נבחרו בהצלחה</span>');
		$('input[@id$="hdnCategoriesIDs"]').val(GetCategoriesIDs());
	}
	
	$('#divDropDownPTable').hide();
	$('#divBotton').hide();
	CatsParentState = 'Close';
}

function SetParentsAndCildrenCategoris()
{
	if(!CatsParentWasInit)
	{
		CatsParentWasInit = true;
		var nobr;
		var divDropDownPTable;//Conteiner for Parents Categories
		var Table;
		var TBody;
		var tr;
		var td;
		var div;
		var a;
		
		divDropDownPTable = document.createElement('div'); 
		divDropDownPTable.setAttribute('id','divDropDownPTable');
		Table = document.createElement('table');
		Table.setAttribute('id','tblParentsCat'); 
		Table.cellPadding = "0px";
		Table.cellSpacing = "0px";
		if(navigator.appName == 'Microsoft Internet Explorer')
			$(divDropDownPTable).width(210);
		
		TBody = document.createElement('tbody');

		//Others categories
		for(i=0;i<CatPIDs.length;i++)
		{
			//If this parent category
			if(CatPIDs[i] == CatIDs[i])
			{
				tr = document.createElement('tr'); 
				tr.setAttribute("id","trParentCat_" + CatPIDs[i]);
				tr.setAttribute("Status","Close");
				tr.onclick = function(){SetParentChilds(this.id)};
				
				//Set CatParentName
				td = document.createElement('td'); 
				a = document.createElement('a');
				a.setAttribute('id', CatPIDs[i]);
				td.setAttribute("id", "tdParentName_" + CatPIDs[i]);
				td.setAttribute('class','tdParentName');
				td.appendChild(a);
				td.appendChild(document.createTextNode(CatNames[i]));
				td.style.height = "20px";
				td.style.paddingRight = "4px";
				tr.appendChild(td);
				
				tr.onmouseover = function(){$(this).attr('class','RowParentCatMarked');};
				tr.onmouseout = function(){$(this).attr('class','RowParentCatUnMarked');};
				TBody.appendChild(tr);
				
				//Set unvisible param
				tr = document.createElement('tr'); 
				td = document.createElement('td'); 
				div = document.createElement('div'); 
				div.setAttribute("id", "divShowChildrenCategory_" + CatPIDs[i]);
				$(div).attr('class','DisplayN');
				td.appendChild(div);
				tr.appendChild(td);
				TBody.appendChild(tr);
				
			}
		}	
		
		Table.appendChild(TBody);
		divDropDownPTable.appendChild(Table);
		divCategoriesConteiner.appendChild(divDropDownPTable);
		document.getElementById('divCategories').appendChild(divCategoriesConteiner);
	}

	 
	if(CatsParentState == 'Close')
	{
		
		CatsParentState = 'Open';
		$('#divDropDownPTable').show();
		$('#divBotton').show();
		if(ChoosenParentID != ''){if(AnchorJump == 1) document.location.href = '#' + ChoosenParentID;}
	}
	else
	{
		
		checkMarketCategories();	
	}
	
}

	
//Create Children of Parent category
function SetParentChilds(parentId)
{
	if(PrevParentId != parentId.split('_')[1] || $('#' + parentId).attr('Status') == 'Close')
	{
		$('#' + parentId).attr('Status','Open');
		
		var parentFullID = parentId;
		parentId = parentId.split('_')[1];

		$("tr[@id^='trParentCat_']").attr('class','RowParentCatUnMarked');
		$("div[@id^='divShowChildrenCategory_']").html('');
		
		$('#trParentCat_' + PrevParentId).mouseout(function(){$(this).attr('class','RowParentCatunMarked');});
		$('#trParentCat_' + parentId).mouseout(function(){$(this).attr('class','RowParentCatMarked');});
		$('#trParentCat_' + parentId).attr('class','RowParentCatMarked');

		//Save parentId
		ChoosenParentID = parentId;
		
		InnerCategoryChildern(parentId);

		if(AnchorJump == 1)
			document.location.href = '#' + parentId;
		PrevParentId = parentId;
	}
	else
	{
		$('#' + parentId).attr('Status','Close');
		$('#' + parentId).attr('class','RowParentCatUnMarked');
		$("div[@id^='divShowChildrenCategory_']").html('');
		$('#' + parentId).mouseout(function(){$(this).attr('class','RowParentCatunMarked');});
	}
}
function InnerCategoryChildern(parentId)
{
	//Conteiner for Children Categories
	var nobr;
	var divChildCatConteiner;
	var checkbox;
	var Table;
	var TBody;
	var tr;
	var td;
	
	divChildCatConteiner = document.createElement('div'); 
	divChildCatConteiner.setAttribute('id','divChildCatConteiner');
	Table = document.createElement('table');
	Table.setAttribute('id','tblChildCat'); 
	Table.cellPadding = "0px";
	Table.cellSpacing = "0px";
	TBody = document.createElement('tbody'); 
	document.getElementById('divShowChildrenCategory_' + parentId).innerHTML = '';
	
	for(i=0;i<CatPIDs.length;i++)
	{
		if(CatPIDs[i]==parentId && CatIDs[i] != parentId)
		{
			tr = document.createElement('tr'); 
			tr.setAttribute("id","trChildCat_" + CatIDs[i]);
			tr.onclick = function(){SetCategoryMarkedRow(this.id);};
			
			//Set checkbox
			td = document.createElement('td'); 
			td.setAttribute("id", "tdChildChk_" + CatIDs[i]);
			checkbox = document.createElement('input'); 
			checkbox.type = "checkbox";
			checkbox.setAttribute('id','chkChildCat_' + CatIDs[i]);
			checkbox.onclick = function(){SetCategoryMarkedRow(this.id);};
			checkbox.setAttribute('value',CatIDs[i]);
			checkbox.style.cursor = "pointer";
			td.appendChild(checkbox);
			td.style.width = "10px";
			td.style.height = "22px";
			tr.appendChild(td);
			
			//Set CatChildName
			td = document.createElement('td'); 
			td.setAttribute("id", "tdChildName_" + CatIDs[i]);
			td.setAttribute("class","tdChildName");
			td.appendChild(document.createTextNode(CatNames[i]));
			tr.appendChild(td);
			
			TBody.appendChild(tr);
		}
	}
	Table.appendChild(TBody);
	divChildCatConteiner.appendChild(Table);
	document.getElementById("divShowChildrenCategory_" + parentId).appendChild(divChildCatConteiner);
	document.getElementById("divShowChildrenCategory_" + parentId).className = 'ComboViewChildrenCategories';
	$('#divShowChildrenCategory_' + parentId).css('padding-right', '20px');
}

//Marked or Unmarked Row
function SetCategoryMarkedRow(CatID)
{
	CatID = CatID.split('_')[1];
	
	//Check if checkbox checked
	if($('#chkChildCat_'+CatID).attr('checked'))
		$('#chkChildCat_'+CatID).attr('checked',false);
	else
		$('#chkChildCat_'+CatID).attr('checked',true);
		
	//Unmarked all rows
	$('#tblChildCat tr input[@type="checkbox"]').parent().parent().attr('class', 'RowChildCatUnMarked');
	
	//Marked rows where checkbox is cheked
	$('#tblChildCat tr input[@type="checkbox"][@checked]').parent().parent().attr('class', 'RowChildCatMarked');
}

//Get The Choosen Categories In String With ,
function GetCategoriesIDs()
{
	var categoriesIDs = '';
	if (ChoosenParentID != '' )
	{
		//Create ChildernIDs // -> ChildID 
		$('#divShowChildrenCategory_' + ChoosenParentID +' input[@type="checkbox"][@checked]').each(
			function()
			{
				//Create categories string IDs
				categoriesIDs += this.value + ',';
			}
		);
		
		categoriesIDs =   categoriesIDs.substring(0, categoriesIDs.length - 1);
		return categoriesIDs;
	}
	else
		return '';
}

