function objCategory (id, nm)
{
	this.catId = id;
	this.catName = nm;
	this.outputCategory = outputCategory;
	this.subCatList = new Array();
	this.subCatCount = 0;
	this.addSubCategory = addSubCategory;
	this.locList = new Array();
	this.locCount = 0;
	this.addLocation = addLocation;
	this.printSearch = printSearch;
	this.subCatStatus = subCatStatus;
	this.outputError = outputError;
	this.outputMapError = outputMapError;
	this.pageShowtype = pageShowtype;
	this.showCategory = true;
	if (el('st_showcat')!=null)
		{	this.showCategory = el('st_showcat').value=='1'; 
		}
	this.differentUserProfile = 0;
	if (el('userprof')!=null)
		{	this.differentUserProfile = el('userprof').value; 
		}
}

function subCatStatus(id)
{
	for (idx in this.subCatList)
	{
		this.subCatList[idx].show = 0;
	}
	this.subCatList[id].show = 1;
	this.printSearch();
	h('planpanel');
	s('searchrightpanel');

	return false;
}
function outputCategory()
{
	
	h('planpanel');
	s('searchrightpanel');
	if (this.catName)
	{
		if (el('valltexttop')!=null)
		{
			si('valltexttop');
		}
		el('searchcategorytitle').innerHTML = "<img src='/blomap/imglinks/cat"+el('maincat').value+".png' /> "+this.catName+"<div class='olpopuptext'> "+textNavTitle+"</div>";
	}
	else
	{
		el('searchcategorytitle').innerHTML = "<div style='margin: 3px 0px;'>"+textAllCategory+"</div><div class='olpopuptext'> "+textNavTitle+"</div>";
		if (el('valltexttop')!=null)
		{
			h('valltexttop');
		}

	}
	el('searchtextrp').innerHTML = '';
	var str = "";
	//if (this.showCategory)
	//{
		/* Read and print all categories */
		str += "<div style='text-align: center;'>";
		//alert(this.subCatCount);
		for (var i = 0; i<this.subCatCount; i++)
		{
			//alert(i);
			//alert (this.subCatList[i].scatName);
			//<img src='/blomap/imglinks/ico"+this.subCatList[i].scatId+".png'><br/>
			str += "<div style='float: left; width: 33%; font-size: 9px;'><input type='radio' name='rd_cat' id='scat_"+this.catId+"_"+i+"' href='#' onclick='catlist["+this.catId+"].subCatStatus("+i+");'/><br/> "+this.subCatList[i].scatName+"</div>";
			if ((i+1)%3==0)
			{
				str += "<div style='clear: both;'></div>";
			}
			
		}
		if (i%3!=0)
			{	str += "<div style='clear: both;'></div>";}
		/* Read and print all location */
		str += "</div>";
		//alert ("check for sidecat");
		//alert ("test "+textAddInfo);
		//alert (el('sidecat').value);
		if (el('sidecat'))
		{
			//alert ("check for values");
			str += "<div style='text-align: center; margin-top:3px;'>"+textAddInfo+"</div><img src='"+el('sidecat').value+"' class='sqpanelborder2' style='width: 232px;'/>";
		}
		else
		{
			str += "<hr/>";
		}
	//}
	//alert ("changing str");
	str += "<div id='stlocsrp' ></div>";
	el('searchtextrp').innerHTML = str;
	
	//alert ("attempting to print search result");
	this.printSearch();
	
}

function addSubCategory(id, nm, prnt)
{
	//alert ("addSubCategory "+id+" "+nm);
	var osc = new objSubCategory(id, nm, prnt);
	this.subCatList[this.subCatCount] = osc;
	this.subCatCount++;
}

function addLocation(id, nm, xar, save, desctitle)
{

	
	var nl = new objLocation(id, nm, curEdit);
	nl.saveLink = save;
	if (desctitle!=null)
	{
			nl.desctitle = desctitle;
	}
	//alert ("Created new location "+id);
	if (xar!=null)
	{	
		for (var i=0;i<xar.length;i++)
		{
			nl.catList[i]=xar[i];
		}
	}
	this.locList[this.locCount] = nl;
	this.locCount++;
	
}

function outputMapError(iderr)
{

	var str, pplink;
	var catval;
	var textPopupLink3;
	str = searchErrorText[iderr];
	var strsdbar = str;
	h('planpanel');
	s('searchrightpanel');

	if (curIdSearch!=0)
	{
		if (curIdSearchStat>0)
		{	
			textPopupLink3 = "<a href='"+locViewInfoUrl+"' class='popuplink'>MORE INFO</a> - <a href='index.php?page=my_handshakes&handler=locmod&action=dv:fw&locid="+curIdSearch+"' class='popuplink'>"+textPopupLink7+"</a> - ";

		}
		else
		{
			textPopupLink3 = "<a href='"+locViewInfoUrl+"' class='popuplink'>MORE INFO</a> - <a href='index.php?page=my_handshakes&handler=locmod&action=av:fw&locid="+curIdSearch+"' class='popuplink'>"+textPopupLink4+"</a> - ";
		}
		str = curAddrSearch+"<br/>"+str;
	}
	else
	{		
		textPopupLink3 = "";
	}
	
	if (this.catName)
	{
		
		
		if (el('valltexttop')!=null)
		{
			si('valltexttop');
		}
		el('searchcategorytitle').innerHTML = "<div style='text-align: left;'><img src='/blomap/imglinks/g_cat"+el('maincat').value+".png' /> "+this.catName+"</div><div class='olpopuptext'> "+textNavTitle+"</div>";
		pplink = textPopupLink1 +"-"+ textPopupLink3 + textPopupLink2;
		
		
		
	}
	else
	{
		
		
		if (el('sidecat')&&el('sidecat').value)
		{
			el('searchcategorytitle').innerHTML = "<div class='olpopuptext'> "+textNavTitle+"</div>";
		}
		else
		{
			el('searchcategorytitle').innerHTML = "<div style='margin: 3px 0px;'>"+textAllCategory+"</div><div class='olpopuptext'> "+textNavTitle+"</div>";
		}
		
		pplink = textPopupLink3 + textPopupLink1;
		if (el('valltexttop')!=null)
		{
			h('valltexttop');
		}
		
		if (el('sidecat')&&el('sidecat').value)
		{
			
			strsdbar = "<div style='text-align: center; margin-top:5px;'>"+str+"<div style='text-align: center; margin-top:10px;'>"+textAddInfo+"</div><img src='"+el('sidecat').value+"' class='sqpanelborder2' style='width: 238px;'/><div style='text-align: center;'>"+textSidebarLocation+"</div>";
		}

	}
	
	
			
	el('searchtextrp').innerHTML = strsdbar;
	searchMarker.popupText = "<div class='mapPopup'><div class='olpopuptext'> "+textNavTitle+"</div><strong>"+str+"</strong><div style='text-transform: uppercase; margin-top: 20px;'>"+pplink+"</div></div>";
	searchMarker.openInfoWindowHtml(searchMarker.popupText);
}


function outputError(iderr)
{
	var str, pplink;
	var catval;
	
	/* I have no idea what changes are made to this function. Therefore if the error is either -1 (no locations) or -2 (zoom too far out) then use the other function */
	if (iderr==-1||iderr==-2)
	{
		//alert ('Error detected '+iderr);
		return outputMapError(iderr);
	}
	str = searchErrorText[iderr];
	var strsdbar = str;
	
	h('planpanel');
	s('searchrightpanel');
//alert("ddffd");
	
var svall= el('searchtextrp').innerHTML;
 if(svall=="The location  "+searchTextVAL+"  has not been found<br><br>Maybe you are searching for:<br><br>" || this.catName) 
  {
	  
	if (this.catName)
	{
		//alert("cat");
		
		if (el('valltexttop')!=null)
		{
			si('valltexttop');
		}
		el('searchcategorytitle').innerHTML = "<div style='text-align: left;'><img src='/blomap/imglinks/g_cat"+el('maincat').value+".png' /> "+this.catName+"</div><div class='olpopuptext'> "+textNavTitle+"</div>";
		pplink = textPopupLink1 +"-"+ textPopupLink2;
		
		
		
	}
	else
	{
		//alert("not cat");
		
		if (el('sidecat')&&el('sidecat').value)
		{
			el('searchcategorytitle').innerHTML = "<div class='olpopuptext'> "+textNavTitle+"</div>";
		}
		else
		{
			el('searchcategorytitle').innerHTML = "<div style='margin: 3px 0px;'>"+textAllCategory+"</div><div class='olpopuptext'> "+textNavTitle+"</div>";
		}
		
		pplink = textPopupLink1;
		if (el('valltexttop')!=null)
		{
			h('valltexttop');
		}
		
		if (el('sidecat')&&el('sidecat').value)
		{
			
			strsdbar = "<div style='text-align: center; margin-top:5px;'>"+str+"<div style='text-align: center; margin-top:10px;'>"+textAddInfo+"</div><img src='"+el('sidecat').value+"' class='sqpanelborder2' style='width: 238px;'/><div style='text-align: center;'>"+textSidebarLocation+"</div>";
		}

	}
	
 	
	
 
	  
	el('searchtextrp').innerHTML = strsdbar;
  } else {
	
	//alert("cat vvv");
		
		el('searchtextrp').innerHTML ='';
		if (el('valltexttop')!=null)
		{
			//si('valltexttop');
		}
		el('searchcategorytitle').innerHTML = "<div style='text-align: left;'><img src='/blomap/imglinks/g_cat"+el('maincat').value+".png' /> "+this.catName+"</div><div class='olpopuptext'> "+textNavTitle+"</div>";
		//pplink = textPopupLink1 +"-"+ textPopupLink2;
		
	  
  }
  
	//searchMarker.popupText = "<div class='mapPopup'><div class='olpopuptext'> "+textNavTitle+"</div><b>"+str+"</b><div style='text-transform: uppercase; margin-top: 20px;'>"+pplink+"</div></div>";
	searchMarker.openInfoWindowHtml(searchMarker.popupText);
}

function printSearch()
{
	str = "";
	var locidMp=-1;
	var ftexts= searchTextVAL.split(",");
	var sTextFmap=ftexts[0];
		
	var divcat = true;
	var subText = new Array();
	// clear search result
	for (var i=0; i<this.subCatCount; i++)
	{
		subText[this.subCatList[i].scatId] = "";
	}
	subText[-1] = "";
	// look at each location
	// for each location check all category. 
	//alert ("check all category");
	for (var i = 0; i<this.locCount; i++)
	{
		//alert(i);
		//alert (this.locList[i].locName);
		var j=0;
		var shown=true;
		/*while (shown&&j<this.subCatCount)
		{
			//alert ("J = "+j+" show : ID= "+this.locList[i].locId+" "+this.subCatList[j].show+" id "+this.subCatList[j].scatId+" is category "+this.locList[i].isInCategory(this.subCatList[j].scatId));
			// is it must show, may show, don't show
			if (this.subCatList[j].show==-1) // do not show
			{
				shown = !this.locList[i].isInCategory(this.subCatList[j].scatId);
			}
			else if (this.subCatList[j].show==1)
			{
				shown = this.locList[i].isInCategory(this.subCatList[j].scatId);
			}
			j++
		}*/
		// it will show unless it doesn't have a must show, or has a don't show
		// print accordingly. Make sure javascript is printed
		
		if (!divcat&&shown)
		{	str += "<li><a class='frontlink' href='javascript: openMarker("+this.locList[i].markerId+"); showLocationData("+this.locList[i].markerId+");'>"+this.locList[i].locName+"</a></li>";
			// if it's grouped, put it in group
			 if(this.locList[i].locName==sTextFmap)
			  {
				   locidMp= this.locList[i].markerId;
				  // openMarker(locidMp);
			  }
		}
		else if (divcat&&shown)
		{
			 //alert("cc="+searchTextVAL);
			 if(this.locList[i].locName==sTextFmap)
			  {
				   locidMp= this.locList[i].markerId;
				  // openMarker(locidMp);
			  }
			
			var url;
			if (this.showCategory)
			{
				url = "javascript: openMarker("+this.locList[i].markerId+")";
			}
			else if (this.differentUserProfile)
			{
				//url = "changeSection(\"frame\", \"index.php?page=locationinfo&pplace=1&handler=locblog&type=-1&locid="+this.locList[i].locId+"\")";
			url = currentUserURL+this.locList[i].locId+"/"+this.locList[i].locName+"/";
			 if(this.showType==1)
			  {
				url = currentUserURL+this.locList[i].locId+"/"+this.locList[i].locName+"/?showglob=true";  
			  }
			}
			else
			{
				url = "/index.php?page=my_handshakes&handler=locmod&action=fpb&locid="+this.locList[i].locId;
			}
			
			//alert ("url"+this.showType);
			
			var j;
			for (j=0;j<this.locList[i].catList.length;j++)
			{
				 
				var id = this.locList[i].catList[j];
				subText[id] += "<li><a class='frontlink' href=' "+url+"'>"+this.locList[i].locName+"</a>";
				if (this.locList[i].desctitle!='')
				{
					//alert ('desctitle is not empty');
					subText[id] += "<div style='float: left; width: 100px;'><span class='greylink'>"+this.locList[i].desctitle+"</span></div>";
					//alert ('managed to print desctitle');
				}
				if (this.locList[i].saveLink)
				{
					subText[id] += "<div style='text-align: right; margin-right: 5px;'><a class='greylink'  href='"+saveURL+this.locList[i].locId+"'>Save</a></div>";
				}
				if (this.locList[i].desctitle!='')
				{
					subText[id] += "<div style='clear: both;'></div>";
				}
				subText[id] += "</li>";
			}
			
			if (this.locList[i].catList.length==0) // uncategorized?
			{
				subText[-1] += "<li><a class='frontlink' href=' "+url+"'>"+this.locList[i].locName+"</a>";
				if (this.locList[i].desctitle!='')
				{
					//alert ('desctitle is not empty');
					subText[-1] += "<div style='float: left; width: 100px;'><span class='greylink'>"+this.locList[i].desctitle+"</span></div>";
					//alert ('managed to print desctitle');
				}
				if (this.locList[i].saveLink)
				{
					subText[-1] += "<div style='text-align: right; margin-right: 5px;'><a class='greylink' href='"+saveURL+this.locList[i].locId+"'  title='Just click on to save location'>Save</a></div>";
				}
				if (this.locList[i].desctitle!='')
				{
					subText[-1] += "<div style='clear: both;'></div>";
				}
				subText[-1] += "</li>";
			}
		}
	}
	
	//alert ("divcat"+divcat+"=="+subText[-1]);
	if (divcat)
	{  
		var isText = false;
		if (subText[-1])
		{
			str += "<ul class='lmcat'>"+subText[-1]+"</ul>";
			isText = true;
		}
		var fp = '';
		var sp = '';
		
		for (var i=0; i<this.subCatCount; i++)
		{
			var strt = '';
			if (subText[this.subCatList[i].scatId]!="")
			{	if (!this.showCategory&&isText)
				{
					strt += "<img src='/themes/blomap/gfx/sqwl_borderh.gif' class='sqpanelborder2' style='margin-top: 10px; margin-bottom: 2px;width: 232px;'/>"
				}
				strt += "<h3 style='margin: 5px 0px 2px 4px;'>";
				//if (iconfiles[this.subCatList[i].scatId]!=null) //!this.showCategory&&
				//{
					strt += "<img src='/blomap/imglinks/cat"+this.subCatList[i].scatId+".png' /> ";
				//}
				strt += this.subCatList[i].scatName+"</h3><div style='margin-left: 5px;'><ul class='lmcat'>"+		subText[this.subCatList[i].scatId]+"</ul></div>";;
				isText = true;
			}
			
			if (this.subCatList[i].show==1)
			{
				fp += strt;
			}
			else
			{
				sp += strt;
			}
		}
		str += fp+sp;
	}
	
	//alert ("+++"+locidMp+"=="+sTextFmap);
	if(locidMp >=0)
	 {
	  openMarker(locidMp);
	 }
	el('stlocsrp').innerHTML = str;
	//alert ("+++"+locidMp);
	
}

function objSubCategory (id, nm, prnt)
{
	this.scatId = id;
	this.scatName = nm;
	this.scatParent = prnt;
	this.show = 0;
	this.subText = "";
}

function objLocation (id, nm, mk)
{
	this.locId = id;
	this.locName = nm;
	this.markerId = mk;
	this.saveLink = false;
	this.catList = new Array();
	this.addCategory = locAddCategory;
	this.isInCategory = locIsCategory;
	this.status = 0;
	this.desctitle = '';
}

function locAddCategory (idc)
{
	this.catList[this.catCount] = idc;
	this.catCount++;
}

function locIsCategory(idc)
{
	var found = false;
	var i = 0;
	while (!found&&i<this.catList.length)
	{
		//alert (this.locId+" --- "+this.catList[i]);
		found = this.catList[i]==idc;
		i++;
	}
	return found;
}


function pageShowtype (showtyp)
{
	this.showType = showtyp;
}
	

