// --------------------------------------------------------------------------------
// bd_Application.js
// Travis Musika Mar 23, 2007
// Contains Javascript relevant to the BusinessDirectory contact form.
// --------------------------------------------------------------------------------

var bd_srchFieldPrefix = "tmpl_bdSearch_bS_";

// category/certification matrix
var bd_srchCatCerts = null;

// --- Attach JS to document elements and execute simple statements. ---
ow_f_AppendLoadEvent(
    function() {

		// set up the page/make matrix for the advanced search control
		var bd_lstCategory = document.getElementById(bd_srchFieldPrefix + "bd_lstCategory");
		var bd_lstCertifications = document.getElementById(bd_srchFieldPrefix + "bd_lstCertifications");
		if (bd_lstCategory != null && bd_lstCertifications != null) {
			
			if (document.getElementById(bd_srchFieldPrefix + "bd_xCatCerts") != null) {

				ow_f_AddEvent(bd_lstCategory, "change", bd_srchSelectCategory, false);
				
				var srchCats = document.getElementById(bd_srchFieldPrefix + "bd_xCatCerts").value.split(";");
				bd_srchCatCerts = new Array(bd_lstCategory.options.length);
				
				var catIdx;
				for (i = 0; i < srchCats.length; i++) {
					catIdx = parseInt(srchCats[i]);
					if (bd_srchCatCerts[catIdx] == null)
						bd_srchCatCerts[catIdx] = new Array();
					if (bd_lstCertifications.options[i] != null)
						bd_srchCatCerts[catIdx].push(bd_lstCertifications.options[i]);
				}
				
				// do the initial call for the listbox
				bd_srchSelectCategory(null);
			}

		}
   
		if (document.getElementById("bd_page1") != null) {
			var inp = document.getElementById("bd_page1").getElementsByTagName("input");
			for (var i = 0; i < inp.length; i++) {
				if (inp[i].type == "text") 
					ow_f_AddEvent(inp[i], "keypress", bd_formTextSubmit, false);
			}
		}
	
		bd_ProcessEmails();
    }
);

// --------------------------------------------------------------------------------
// bd_srchSelectCategory()
// Changes the options in the certification select depending on the current category.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- none
// --------------------------------------------------------------------------------
function bd_srchSelectCategory(e) {
	var bd_lstCategory = document.getElementById(bd_srchFieldPrefix + "bd_lstCategory");
	if (bd_lstCategory != null) {
		var catIdx = bd_lstCategory.selectedIndex;
		
		if (bd_srchCatCerts != null) {
			var bd_lstCertifications = document.getElementById(bd_srchFieldPrefix + "bd_lstCertifications");
			var currCat = "";
			if (bd_lstCertifications.selectedIndex > -1)
				currCat = bd_lstCertifications.options[bd_lstCertifications.selectedIndex].value;
			bd_lstCertifications.options.length = 0;
			if (catIdx != 0) {
				//bd_lstCertifications.options.add(bd_srchCatCerts[0][0]);
				bd_lstCertifications.disabled = false;
			} else
				bd_lstCertifications.disabled = true;
				
			if (bd_srchCatCerts[catIdx] != null)
				for (i = 0; i<bd_srchCatCerts[catIdx].length;i++) {
					var option = bd_srchCatCerts[catIdx][i];
					if (option != null) {
						if (option.value == currCat)
							option.selected = true;
						bd_lstCertifications.options.add(option);
					}
				}
		}
	}
}

// --------------------------------------------------------------------------------
// bd_formTextPostSubmit()
// Fires when a textfield had focus and ENTER was pressed.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function bd_formTextSubmit(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (code == 13) {
		document.getElementById(bd_srchFieldPrefix + "bd_btnSearch").click();
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	}
}

// --------------------------------------------------------------------------------
// bd_ProcessEmails()
// Converts an email address from the mangled format to the normal format.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function bd_ProcessEmails() {
	var mailto = ow_f_GetElementsByClassName("bd_mailto", "a");
	
	for (var i = mailto.length - 1; i >= 0; i--) {
		var m = mailto[i];

		var p = m.parentNode;
		var editor = false;
		while (p) {
			if (p.id)
				if (p.id.indexOf("radEditorContainer") == 0) {
					editor = true;
					break;
				}
			p = p.parentNode;
		}

		if (!editor) {
			if (m.getAttribute("prefix")) {
				if (m.childNodes.length == 1 && m.childNodes[0].nodeType == 3) {
					var href = m.href;
					if (href.indexOf("?") != -1) href = href.substr(0, href.indexOf("?"));
					if (href == "mailto:" + m.childNodes[0].nodeValue)
						m.replaceChild(document.createTextNode(m.getAttribute("prefix") + m.childNodes[0].nodeValue), m.childNodes[0]);
				}
				m.href = (m.href).replace("@", m.getAttribute("prefix") + "@");
			}
		}
		
	}
	
}