/*
     2006 Ramon Gonzalez (KP4TR) For PRVI
*/
var myWindow;
var totalSearchCount = 0;
var Mhz144 = "table1";
var Mhz440 = "table2";
var MhzMisc = "table3";
var searchresultsTD=new Array();
var searchresultsTDtext="";
var tblStart = '<table align="center" border="1">';
var tblEnd = '</table><br>';

/* 
	Parse URL parameters if searchText param is blank. If URL params is blank
	then do nothing
*/ 
function searchCoordinaciones(searchText) {
	var elementText = "";
	if (searchText.value != "") {
		elementText = searchText.value;
		writeToTable(elementText);
	} else {
		urlquery = location.href.split("?");
		if (urlquery[1] != null) {
			urlterms = urlquery[1].split(",");
			if (urlterms[0] != null) {
				urlparams = urlterms[0].split("&");
				if (urlparams[0] != null) {
					urlparam0 = urlparams[0].split("=");
					urlparam1 = urlparams[1].split("=");
					urlparam2 = urlparams[2].split("=");
					if (urlparam0[1].length > 0) {
						elementText = urlparam0[1];
					}
					if (urlparam1[1].length > 0) {
						elementText = urlparam1[1];
					}
					if (urlparam2[1].length > 0) {
						elementText = urlparam2[1];
					}
					elementText = elementText.replace("+", " ");
					writeToTable(elementText);
				}
			}
		}
	}
}

/*
	Process search text. Get a handle on the table that will contain the
	search results. Then process each table with coordinations data.
*/
function writeToTable(elementText) {	
	searchresultsTD=document.getElementById('searchresultsTABLE').rows[0].cells;
	searchresultsTDtext="";
	totalSearchCount = 0;
	
	if (document.getElementById(Mhz144).rows.length > 0) {
		searchByTable(Mhz144, elementText);
	}
	if (document.getElementById(Mhz440).rows.length > 0) {
		searchByTable(Mhz440, elementText);
	}
	if (document.getElementById(MhzMisc).rows.length > 0) {
		searchByTable(MhzMisc, elementText);
	}
	if (totalSearchCount < 1) {
		searchresultsTDtext = searchresultsTDtext +
		tblStart +  
		'<tr><td align="center"><B>Busqueda de ' + 
		elementText + 
		' no encontro nada! </B></td></tr>' + 
		tblEnd;
	}	
	searchresultsTD[0].innerHTML = searchresultsTDtext;
}

/*
 	Get search text and do a regular expression search row by row. If a search
 	results is returned, then append the table data <td> to a string.
 	During first loop, get the table header in row 0 so table will have 
 	a heading. 
*/
function searchByTable(tableName, elementText) {
	searchresultsTDtext = searchresultsTDtext + tblStart;
	var eRows = document.getElementById(tableName).rows;
	var loops = 0;
	for (var x = 0; x < eRows.length; x++) {
		var str = String(eRows[x].innerHTML);
		var searchString = new RegExp(elementText, "i");
		var searchValue = str.search(searchString);
		if  (searchValue > -1) {
			if (loops == 0) {
					searchresultsTDtext = searchresultsTDtext + '<tr>' + eRows[0].innerHTML + '</tr>';
			}
			searchresultsTDtext = searchresultsTDtext + '<tr>' + String(eRows[x].innerHTML) +'</tr>';
			totalSearchCount = totalSearchCount + 1;
			loops++;
		}
	}
	searchresultsTDtext = searchresultsTDtext +  tblEnd;
}

function browseToURL(searchInput) {
	theForm = document.forms["prvicoords"];
	var searchText = document.getElementById(searchInput);
	if (searchText.value == "") {
		alert("Favor de entrar Pueblo, Frecuencia o Siglas");
		return;
	}
	var newURL = "prvicoords.html?" + searchText.value;
	theForm.action = newURL;
	theForm.submit();
}

function selPueblo(theValue) {
	dropDownPopulateTextField(theValue,"pueblo");
}

function selFrecuencia(theValue) {
	dropDownPopulateTextField(theValue,"frecuencia");
}

function selSiglas(theValue) {
	dropDownPopulateTextField(theValue,"siglas");
}

function dropDownPopulateTextField(theValue,elementName) {
	selIndex = theValue.selectedIndex;
	document.getElementById(elementName).value = theValue.options[selIndex].text;
}

function hideAllTables() {
	document.getElementById(Mhz144).style.display = 'none';
	document.getElementById(Mhz440).style.display = 'none';
	document.getElementById(MhzMisc).style.display = 'none';
}
