var categoryHighlight_background = '';
var categoryHighlight_background_isSet = 0;
var categoryHighlight_background_highlight = '#00ffcc';
var searchResult;

function selectCategory(category, selectedCell)
{	
	categorySelectHighlight(selectedCell);
	//loadContact(null); /* mwb: attempt clear of right hand colums */
	document.getElementById("contact").innerHTML="";
	document.getElementById("subcategory").innerHTML = "<p>Loading . . .</p>";
	loadSubCategory(category);
	
}

function selectContacts(category, selectedCell)
{
	categorySelectHighlight(selectedCell);
	document.getElementById("contact").innerHTML = "<p>Loading . . .</p>";
	loadContact(category);
}

/* change background color by add / remove of class names */
function categorySelectHighlight(selectedCell)
{
	var tableBody = selectedCell.parentNode.parentNode;
	var tableRows = tableBody.getElementsByTagName("tr");
	for (var i = 0; i < tableRows.length; i++)
	{
		var tableCells = tableRows[i].getElementsByTagName("td");
		for (var j = 0; j < tableCells.length; j++)
		{
			if (tableCells[j] != selectedCell)
			{
				tableCells[j].className = '';
			}
		}
	}
	selectedCell.className = 'select';
}

function categoryMouseOver(cell)
{
	if (cell.className == '')
		cell.className = 'hover';
}

function categoryMouseOut(cell)
{
	if (cell.className == 'hover')
		cell.className = '';
}

function loadSubCategory(category)
{
	new Ajax.Request("../include/category.php", { method: 'GET', parameters:{q:category}, onSuccess: function(transport) {																							
	document.getElementById("subcategory").innerHTML = transport.responseText;
	
	},
	onFailure: function(){alert("something went wrong...1")}
 });
	
}
/*
function loadSubCategoryStateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById("subcategory").innerHTML=xmlHttp.responseText;
	}
}
*/

function loadContact(category)
{
	
	new Ajax.Request("../include/category.php", { method: 'GET', parameters:{q:category}, onSuccess: function(transport) {		
	if(transport.responseText!=false){
		try{
				var json = transport.responseText.evalJSON();
				
			}
			catch(e){
				alert(e);
			}
		searchResult = json;
		populateSearch();
	}
	else{
			html="<div class=contact-box><p> No results found for this item ("+category+"). </p></div>";	
			document.getElementById("contact").innerHTML=html;
		}
	},
	onFailure: function(){alert("something went wrong...1")}
 });

}
function populateSearch(){
	//alert(searchResult[0].organisation);
		var postName="";
		var orgName="";
		var html="";
		
		for(i=0;i<searchResult.length;i++){
			html+="<div class=contact-box>";
		if(searchResult[i].showImage!=0){html+="<div class='contactimg'><img src='/greenpages/logos/"+searchResult[i].showImage+".jpg' alt='"+searchResult[i].organisation+"'/></div>";}
			html+="<div class=contact-box-inner>";
				if(searchResult[i].orgEng){orgName = searchResult[i].orgEng}
				else{if(searchResult[i].organisation){orgName= searchResult[i].organisation;}
				}
				if(orgName!=""){
					if(searchResult[i].bold==1){
					html+="<p><b>" + orgName+"</b></p>";	
					}
					else{
						html+="<p>" + orgName+"</p>";	
					}
				}
				
				if((searchResult[i].thename)){html+="<p><strong>Contact:</strong> "+searchResult[i].thetitle+" "+searchResult[i].thename+"</p>";}
			if(searchResult[i].address){html+="<table class='address'><tr><td><p><strong>Address: </strong></p></td><td><p> "+searchResult[i].address+"</p></td></tr></table>";}
			if(searchResult[i].tel){html+="<p><strong>Tel:</strong> "+searchResult[i].tel+"</p>";}
			if(searchResult[i].fax){html+="<p><strong>Fax:</strong> "+searchResult[i].fax+"</p>";}
			if(searchResult[i].email && searchResult[i].email!='No e-mail - Do not send'){
				theEmail=searchResult[i].email;
				/*
				specChar = searchResult[i].email.indexOf("@");
				theEmail=searchResult[i].email.substr(0,specChar) + "<img src='../icon/at.jpg' alt='email'/>"+ searchResult[i].email.substr(specChar+1,searchResult[i].email.length)
				html+="<p>"+theEmail+"</p>";*/
			}
			if(searchResult[i].formURL){html+="<p><a href='http://"+searchResult[i].formURL+"'>Form mail</a></p>";}
			
			
			if(searchResult[i].website){html+="<p><a href='http://"+searchResult[i].website+"'>Website</a>";}
			
			if(theEmail){html+=" | <a href='mailto:" + theEmail +"'>Email</a></p>";}else{html+="</p>";}
			if(searchResult[i].desc){html+="<p>"+searchResult[i].desc+"</p>";}
			html+="</div></div>";
		}
			html+="</div>";
			document.getElementById("contact").innerHTML=html;
}

function limitTextLength(object, limit)
{
	if (object.value.length > limit)
	{
		alert("Character limit reached.");
		object.value = object.value.substring(0, limit);
	}
}

// mwb: print different alerts for each listing type

function limitTextLengthBronze(object)
{
	if (object.value.length > 150)
	{
		alert("You've reached the limit for Bronze listings. Please upgrade to Silver or Gold listing.");
		object.value = object.value.substring(0, 150);
	}
}

function limitTextLengthSilver(object)
{
	if (object.value.length > 300)
	{
		alert("You've reached the limit for Silver listings. Please upgrade to Gold listing.");
		object.value = object.value.substring(0, 300);
	}
}

