var browser = new Browser();

var current = null;
var BG_OVER = "#FFFE9B";
var BG_OUT = "#FFFFFF";
var BG_CURRENT = "#DEEFFF";
var TABLE_ROW_DISPLAY = (browser.browserType == browser.IE ? "block" : "table-row");
var TABLE_DISPLAY = (browser.browserType == browser.IE ? "block" : "table");
var isListEmpty = true;		// set at the end of the list building code below.

function postInit() {

	var tables = document.getElementsByTagName("TABLE");
	var table;
	for (var k = 0; k < tables.length; k++) {
		table = tables[k];
		if (table.id.search(/agencyList/) < 0) continue;

		var rows = table.rows;
		var r;
		for (var i = 0; i < rows.length; i++) {
			r = rows.item(i);
			if (r.getAttribute("className") == "agency" || r.getAttribute("class") == "agency") {
				r.onmouseover = onMouseOverAgency;
				r.onmouseout = onMouseOutAgency;
				r.onclick = onClickAgency;
			}
		}
	}

	if (isListEmpty) {
		var tr = document.getElementById("notListed");
		if (tr != null) {
			tr.onclick();
		}
	}
}

function onMouseOverAgency() {
	if (this == current) return;
	this.style.backgroundColor = BG_OVER;
	this.cells.item(1).firstChild.style.visibility = "visible";
}

function onMouseOutAgency() {
	if (this == current) return;
	this.style.backgroundColor = BG_OUT;
	this.cells.item(1).firstChild.style.visibility = "hidden";
}

function onClickAgency() {
	var next;
	if (current != null) {
		current.style.backgroundColor = BG_OUT;
		next = current.nextSibling;
		while (next != null && next.tagName != "TR") next = next.nextSibling;
		if (next != null) next.style.display = "none";
	}

	if (this == current) {
		current = null;
		return;
	}

	current = this;
	this.style.backgroundColor = BG_CURRENT;
	this.cells.item(1).firstChild.style.visibility = "hidden";

	next = this.nextSibling;
	while (next != null && next.tagName != "TR") next = next.nextSibling;
	if (next != null) next.style.display = TABLE_ROW_DISPLAY;
}

function requestMembership(affId) {
	var win = window.open(contactURL + "?aff=" + affId + "&op=request", "contactgroup", "width=500,height=450");
	win.focus();
}

function contactAffiliate(affId) {
	if (hasDetail) {
		// For Become a Foster Parent
		var nation = document.selectForm.nation.value;
		var state = document.selectForm.state.value;
		var county = document.selectForm.county.value;
		var win = window.open(contactURL + "?affId=" + affId + "&nation=" + nation + "&state=" + state + "&county=" + county, "contactgroup", "width=500,height=550");
	} else {
		// For other contexts
		var win = window.open(contactURL + "?aff=" + affId, "contactgroup", "width=500,height=550");
	}
	win.focus();
}

function manageAffiliate(cid) {
	document.location.href = manageURL + "?cid=" + cid + "&url=/group/account/ov-aff-view.jsp";
}

function selectRegion(rAbbrev) {
	document.location.href = thisURL + "?country=USA&region=" + rAbbrev;
}

function selectCountry(cAbbrev) {
	if (cAbbrev == "USA") {
		var div = document.getElementById("usMapHolder");
		div.style.display = "block";
	} else {
		document.location.href = thisURL + "?country=" + cAbbrev;
	}
}

function onChangeNation() {
	document.selectForm.state.value = "0";
	document.selectForm.county.value = "-1";
	document.selectForm.submit();
}

function onChangeState() {
	document.selectForm.county.value = "-1";
	document.selectForm.submit();
}

function onChangeCounty() {
	document.selectForm.submit();
}

// functions used by affiliate-all.jsp
var currentRegionId = 0;

function toggleRegion(regionId) {
	if (currentRegionId != 0) {
		hideRegion(currentRegionId);
	}
	if (regionId != currentRegionId) {
		showRegion(regionId);
		currentRegionId = regionId;
	} else {
		currentRegionId = 0;
	}
}

function showRegion(regionId) {
	var listNode = document.getElementById("listContainer" + regionId);
	listNode.style.display = "block";

	var icon = document.getElementById("regionIcon" + regionId);
	icon.src = "/images/tree-minus.gif";
	var regionHead = document.getElementById("regionHead" + regionId);
	regionHead.setAttribute("className", "regionHeadOpen");
	regionHead.setAttribute("class", "regionHeadOpen");
}

function hideRegion(regionId) {
	var listNode = document.getElementById("listContainer" + regionId);
	listNode.style.display = "none";

	var icon = document.getElementById("regionIcon" + regionId);
	icon.src = "/images/tree-plus.gif";
	var regionHead = document.getElementById("regionHead" + regionId);
	regionHead.setAttribute("className", "regionHeadClosed");
	regionHead.setAttribute("class", "regionHeadClosed");
	regionHead.style.backgroundColor = "white";
}

function onMouseOverHead(div, regionId) {
	if (regionId != currentRegionId) {
		div.style.backgroundColor = "#FFFE9B";
	}
}

function onMouseOutHead(div, regionId) {
	if (regionId != currentRegionId) {
		div.style.backgroundColor = "white";
	}
}
