window.onload = function() {
	var obj = new getObj('script_based_directory');
	if (obj.ready) {
		obj.style.display = 'block';
		obj.style.visibility = 'visible';
	}
}

function rpc_select_area(type, id, name)
{
	var func;
	var atts;
	var cmd;
	
	switch (type) {
		case 'letter': {
			func = populate_agencies;
			cmd = 'agencies.getByLetter';
			atts = 'agencies';
			var obj = new getObj('agencies');
			obj.style.visibility = 'hidden';
			obj.style.display = 'none';
			myRPC(cmd,id,func,atts);
			break;
		}
		case 'regions': {
			func = populate_areas;
			cmd = 'areas.getCounties';
			atts = 'counties';
			var obj = new getObj('counties');
			obj.style.visibility = 'hidden';
			obj.style.display = 'none';
			var obj = new getObj('towns');
			obj.style.visibility = 'hidden';
			obj.style.display = 'none';
			myRPC(cmd,id,func,atts);
			
			func = populate_agencies;
			cmd = 'agencies.getByRegion';
			atts = 'agencies';
			var obj = new getObj('agencies');
			obj.style.visibility = 'hidden';
			obj.style.display = 'none';
			myRPC(cmd,id,func,atts);
			
			var obj = new getObj('options');
			obj.obj.innerHTML = '\n' +
'		<a href="/search/region/' + id + '/' + name + '">Show all Properties in ' + name + ' Region</a>\n';
			break;
		}
		case 'counties': {
			func = populate_areas;
			cmd = 'areas.getTowns';
			atts = 'towns';
			var obj = new getObj('towns');
			obj.style.visibility = 'hidden';
			obj.style.display = 'none';
			myRPC(cmd,id,func,atts);
			
			func = populate_agencies;
			cmd = 'agencies.getByCounty';
			atts = 'agencies';
			var obj = new getObj('agencies');
			obj.style.visibility = 'hidden';
			obj.style.display = 'none';
			myRPC(cmd,id,func,atts);
			
			var obj = new getObj('options');
			obj.obj.innerHTML = '\n' +
'		<a href="index.php?page=results&s=directorysearch&type=county&id=' + id + '">Show all Properties in ' + name + '</a>\n';
			break;
		}
		case 'towns': {
			func = populate_agencies;
			cmd = 'agencies.getByTown';
			atts = 'agencies';
			var obj = new getObj('agencies');
			obj.style.visibility = 'hidden';
			obj.style.display = 'none';
			myRPC(cmd,id,func,atts);
			
			var obj = new getObj('options');
			obj.obj.innerHTML = '\n' +
'		<a href="index.php?page=results&s=directorysearch&type=town&id=' + id + '">Show all Properties in ' + name + '</a>\n';
			break;
		}
	}
}

function populate_agencies(atts, values)
{
	var obj = new getObj(atts);
	var html = '';
	
	if (obj.ready) {
		obj.style.visibility = 'visible';
		obj.style.display = 'block';
		if (values.length >= 1 && values[0][0] == 0) {
			var html = 'No Agencies Found';
			obj.obj.innerHTML = '\n' + html;
		}
		else if (values.length) {
			for (n = 0; n < values.length; n++) {
				if (n % 2) {
					var odd = 'odd';
					var sbounder = '';
					var ebounder = '\n' +
'			</div>\n';
				}
				else {
					var odd = 'even';
					var sbounder = '\n' +
'			<div class="listrow">\n';
					var ebounder = '';
				}
				
				var box =
'		<div class="agency">\n' +
'			<div class="agencyContainer ' + odd + '">\n' +
'				<div class="container">\n' +
'					<div class="logo">\n';
				if (values[n][3].length) {
					var urls = '<a href="' + values[n][3] + '">';
					var urle = '</a>';
				}
				else {
					var urls = '';
					var urle = '';
				}
				if (parseInt(values[n][4]) == 1) {
					box = box +
'						' + urls + '<img src="images/logos/thumbnails/' + values[n][0] + '.jpg" title="' + values[n][1] + '" />' + urle + '\n';
				}
				else {
					box = box +
'						' + urls + '<img src="images/site/fillers/no_logo_available_thumb.png" title="' + values[n][1] + '" />' + urle + '';
				}
				box = box +
'					</div>\n' +
'					<div class="detail">\n' +
'						<h3 class="name">\n' +
'							' + urls + values[n][1] + urle + '\n' +
'						</h3>\n' +
'						<div class="description">\n' +
'							' + values[n][2] + '\n' +
'						</div>\n' +
'						<div class="properties">\n' +
'							<a href="index.php?page=results&s=directorysearch&type=agency&id=' + values[n][0] + '">Show available properties by ' + values[n][1] + '</a>\n' +
'						</div>\n' +
'					</div>\n' +
'				</div>\n' +
'			</div>\n' +
'		</div>\n';
				html = html + sbounder + box + ebounder;
			}
			obj.obj.innerHTML = '\n' + html;
		}
		else {
			var html = 'No Agencies Found';
			obj.obj.innerHTML = '\n' + html;
		}
	}
}

function populate_areas(atts, values)
{
	var obj = new getObj(atts);
	var html = '';
	if (obj.ready) {
		obj.style.visibility = 'visible';
		obj.style.display = 'block';
		
		for (n = 0; n < values.length; n++) {
			if (atts == 'counties') {
				var html = html +
'		<span>\n' +
'			<strong><a href="javascript: if (window.rpc_select_area) rpc_select_area(\'counties\', ' + values[n][0] + ',\'' + values[n][1] + '\');">' + values[n][1] + '</a></strong>\n' +
'		</span>\n';
			}
			else if (atts == 'towns') {
				var html = html +
'		<span>\n' +
'			<strong><a href="javascript: if (window.rpc_select_area) rpc_select_area(\'towns\', ' + values[n][0] + ',\'' + values[n][1] + '\');">' + values[n][1] + '</a></strong>\n' +
'		</span>\n';
			}
			else {
				var html = '';
			}
		}
		obj.obj.innerHTML = '\n' + 
'		<legend>' + atts + '</legend>\n	' + html;
	}
}

