function SetSearchEngineRegions()
{

	//Conteiner for Regions
	var divSearchEngineRegionsConteiner;
	var checkbox;
	var Table;
	var TBody;
	var tr;
	var td;
	
	divSearchEngineRegionsConteiner = document.createElement('div'); 
	divSearchEngineRegionsConteiner.setAttribute('id','divSearchEngineRegionsConteiner');
	Table = document.createElement('table');
	Table.setAttribute('id','tblSearchEngineRegions');
	Table.cellPadding = "0px";
	Table.cellSpacing = "0px";
	Table.setAttribute('width','100%');
	TBody = document.createElement('tbody');
	document.getElementById(RegionID).innerHTML = '';
	
	//Checkbox select all regions
	tr = document.createElement('tr');
	td = document.createElement('td');
	checkbox = document.createElement('input'); 
	checkbox.type = "checkbox";
	checkbox.setAttribute('id','chkRegions_-1'); 
	checkbox.onclick = function(){SetRegionMarkedRow(this.id);};
	checkbox.setAttribute('value','-1');
	td.appendChild(checkbox);
	$(checkbox).attr('checked',true);
	tr.setAttribute("id","trJobRegions_-1");
	tr.onclick = function(){SetRegionMarkedRow(this.id);};
	tr.appendChild(td);
	td = document.createElement('td'); 
	td.setAttribute("class","tdRegionsName");
	td.appendChild(document.createTextNode('הכל'));
	$(tr).attr('class','RowTypesMarked')
	tr.appendChild(td);
	TBody.appendChild(tr);
	
	for(i=0;i<arrRegions.length;i+=2)
	{
		tr = document.createElement('tr'); 
		tr.setAttribute("id","trJobRegions_" + arrRegions[i]);
		tr.onclick = function(){SetRegionMarkedRow(this.id);};
		
		//Set checkbox
		td = document.createElement('td'); 
		td.setAttribute("id", "tdJobRegionsChk_" + arrRegions[i]);
		checkbox = document.createElement('input'); 
		checkbox.type = "checkbox";
		checkbox.setAttribute('id','chkRegions_' + arrRegions[i]);
		checkbox.onclick = function(){SetRegionMarkedRow(this.id);};
		checkbox.setAttribute('value',arrRegions[i]);
		td.setAttribute('width','10px');
		td.appendChild(checkbox);
		tr.appendChild(td);
		
		//Set RegionName
		td = document.createElement('td'); 
		td.setAttribute("id", "tdRegionsName_" + arrRegions[i]);
		td.setAttribute("class","tdRegionsName");
		td.appendChild(document.createTextNode(arrRegions[i+1]));
		tr.appendChild(td);
		TBody.appendChild(tr);
	}
	Table.appendChild(TBody);
	divSearchEngineRegionsConteiner.appendChild(Table);
	document.getElementById(RegionID).appendChild(divSearchEngineRegionsConteiner);
	document.getElementById(RegionID).className = 'ComboViewRegions'; 

}

//Marked or Unmarked Row
function SetRegionMarkedRow(CatID)
{
	CatID = CatID.split('_')[1];

	//Check if checkbox checked
	if($('#chkRegions_'+CatID).attr('checked'))
		$('#chkRegions_'+CatID).attr('checked',false);
	else
		$('#chkRegions_'+CatID).attr('checked',true);
		
	//Unchecked others checkboxs exept CatID == '-1'
	if(CatID == '-1' && $('#chkRegions_'+CatID).attr('checked'))
		$('#tblSearchEngineRegions tr input[@type="checkbox"][@checked]').each(
			function()
			{
				if(this.id.split('_')[1] != -1)
					$(this).attr('checked',false);
			}
		);
	//Uncheked checkbox with CatID == '-1'
	if(CatID != -1 && $('#chkRegions_'+CatID).attr('checked'))
		$('#chkRegions_-1').attr('checked',false);
		
	//Unmarked all rows	
	$('#tblSearchEngineRegions tr input[@type="checkbox"]').parent().parent().attr('class', 'RowRegionsUnMarked');
	
	//Marked rows where checkbox is cheked
	$('#tblSearchEngineRegions tr input[@type="checkbox"][@checked]').parent().parent().attr('class', 'RowRegionsMarked');
}

//Return values to Main JS file
function GetSearchResultsRegions()
{
	var regionIDs = '';
	
	//Regions
	$('#' + RegionID +' input[@type="checkbox"][@checked]').each(
			function()
			{
				//Create regions string IDs
				if(this.value != '-1')
					regionIDs += this.value + ',';
			}
	);
	
	//Remove last ','
	if(regionIDs != '')
		regionIDs = regionIDs.substring(0, regionIDs.length - 1);

	return regionIDs;
}
//Get Regions values
function GetSearchEngineRegionName()
{
	var regionNames = "";
	
	//Regions
	$('#' + RegionID +' input[@type="checkbox"][@checked]').each(
			function()
			{
				//Create regions string Names
				if(this.value != '-1')
					regionNames += $('#tdRegionsName_' + this.value).html() + '<br/>';
			}
	);
	
	if(regionNames == '')
		return 'הכל';
		
	return regionNames;
}
//Set Regions values
function SetSearchEngineRegionValues(regionIDs)
{
	var i; 
	var arrRegionIDs = new Array();
		 	
	if(regionIDs != '')
	{	
		if(regionIDs.indexOf(',') == -1)
			arrRegionIDs[0] = regionIDs;
		else
			arrRegionIDs = regionIDs.split(',');
			
		for(i=0;i<arrRegionIDs.length;i++)
		{
			//Regions
			$('#' + RegionID +' input[@type="checkbox"]').each(
					function()
					{
						if(this.value == arrRegionIDs[i])
							$(this).attr('checked',true);
						
					}
			);
		}	
		$('#tblSearchEngineRegions tr input[@type="checkbox"][@checked]').parent().parent().attr('class', 'RowRegionsMarked');
		$('#chkRegions_-1').parent().parent().attr('class', 'RowRegionsUnMarked');
		$('#chkRegions_-1').attr('checked',false);
	}
	else
		SetSearchEngineRegions();
}
