var content = new Array()

function loadXMLDoc(dname) {
	var xmlDoc;
	if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.load(dname);		
 	}
	else if(window.XMLHttpRequest)    {
            var d = new XMLHttpRequest();
            d.open("GET", dname, false);
            d.send(null);
            xmlDoc=d.responseXML;
	}
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.load(dname);		
	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	//alert(xmlDoc);
	//alert(dname);	
	return(xmlDoc);	
}


function loadContent(dname, docTag) {
	xmlDoc=loadXMLDoc(dname);
	content[docTag]=xmlDoc.getElementsByTagName(docTag);
	
	// Put Date String into Standard Date Format
	for (i=0;i<content[docTag].length;i++) { 
		for (j=0; j < content[docTag][i].childNodes.length; j++) {
			if (content[docTag][i].childNodes[j].nodeName == "date") {
				var dmy = content[docTag][i].childNodes[j].firstChild.nodeValue.split("/",3);
				var condate = new Date(dmy[2],dmy[0]-1,dmy[1],23,59,59);
				content[docTag][i].childNodes[j].firstChild.nodeValue = condate;
			}
		}
	}
}

function displayContentNodes(docTag) {
	var ELEMENT_NODE = 1;

	var content_nodes ='<h1>Content Nodes -- '+docTag+'</h1>';
	content_nodes += '<ul>';
	for (i=0; i < content[docTag].length; i++) {
		content_nodes +=  '<li>(' +content[docTag][i].getAttribute('id') + ')</li>';
		content_nodes += '<ul>';
		for (j=0; j < content[docTag][i].childNodes.length; j++) {
			if (content[docTag][i].childNodes[j].nodeType != ELEMENT_NODE) continue;
			content_nodes += '<li>Node#'+j+': '+content[docTag][i].childNodes[j].nodeName+' = '+content[docTag][i].childNodes[j].firstChild.nodeValue+'</li>';
			content_nodes +=  '<ul><li>(' +content[docTag][i].childNodes[j].getAttribute('type') + ')</li></ul>';
		}
		content_nodes += '</ul>';
	}
	content_nodes += '</ul>';
	document.write(content_nodes)
	return;
}

function searchContent(docTag,searchtype,srchparam,srchfield1,srchfield2,srchfield3,displaynum,containerid) {
	if (displaynum =="all" || displaynum == "") { var stopspot=content[docTag].length; } else { var stopspot=parseInt(displaynum); }
	var search_content = '';

// Set Display Container
	var srchresults = document.getElementById(containerid);

	if (searchtype == 'none' || searchtype == 'noheader') {
		search_content += '<ul>\n';
		var srchstr = null;
	} else if (searchtype == 'param') {
		search_content += '<h3>'+srchparam+'</h3><ul>\n';
		var srchstr = null;
	// Default Clearinghouse Configuration
	} else if (searchtype == 'documents') {
		var srchstr = document.getElementById(searchtype).value;
		search_content += '<h3>'+srchparam+'</h3><ul>\n';
	// Pull Search Variables From Form
	} else {
		var srchstr = document.getElementById(searchtype).value;
		if (srchstr == "" && srchparam == "") {
			search_content += '<ul>\n';
		} else if (srchstr != "" && srchparam == "") {
			search_content += '<h3>Search results for "'+srchstr+'":</h3><ul>\n';
		} else if (srchstr == "" && srchparam != "") {
			search_content += '<h3>'+srchparam+'</h3><ul>\n';
		} else {
			search_content += '<h3>'+srchstr+' ( '+srchparam+' )</h3><ul>\n';
		}
	}
	
	var no_search_content = search_content;
	
// Create Search Validators
	var paramcheck = null;
	var strcheck = null;

// Create Counter and End-Of-Loop Designators
	var ctr=0;
	var ctrtripped=new Boolean(false);
	var ELEMENT_NODE = 1;
	
	for (i=0;i<content[docTag].length;i++) { 
// Reset Search Validators
		paramcheck = null;
		strcheck = null;

		for (j=0; j < content[docTag][i].childNodes.length; j++) {
			if ( content[docTag][i].childNodes[j].nodeType != ELEMENT_NODE ) continue;

		// Check for Search Parameter in Search Fields
			if ( srchparam == '' ) {
				paramcheck = "Parameter Not Checked";
			} else if ( paramcheck == null && 
				((content[docTag][i].childNodes[j].nodeName == srchfield1 ) || 
				 (content[docTag][i].childNodes[j].nodeName == srchfield2 ) || 
				 (content[docTag][i].childNodes[j].nodeName == srchfield3 ))) {
				paramcheck = content[docTag][i].childNodes[j].firstChild.nodeValue.toLowerCase().match(srchparam.toLowerCase());
			}

		// Check for Search String in Search Fields
			if ( strcheck == null && srchstr != null && 
				((content[docTag][i].childNodes[j].nodeName == srchfield1 ) || 
				 (content[docTag][i].childNodes[j].nodeName == srchfield2 ) || 
				 (content[docTag][i].childNodes[j].nodeName == srchfield3 ))) {
				strcheck = content[docTag][i].childNodes[j].firstChild.nodeValue.toLowerCase().match(srchstr.toLowerCase());
			}
		}

//				search_content += '<li>Param Check = '+paramcheck+'; String Check = '+strcheck+'</li>\n';
//				search_content += '<li>Value = '+eval(paramcheck != null && (strcheck != null || searchtype == 'none'))+'</li>\n';

		if (ctr < stopspot && (paramcheck != null && (strcheck != null || searchtype == 'none' || searchtype == 'param'))) {
			for (j=0; j < content[docTag][i].childNodes.length; j++) {
				if (content[docTag][i].childNodes[j].nodeType != ELEMENT_NODE) continue;

				//Trip Counter
				ctrtripped = Boolean(true);
			
				// Never Write Keywords!
				if ( content[docTag][i].childNodes[j].nodeName == "keywords" ) {

				// Never Write "None" Fields!
				} else if ( content[docTag][i].childNodes[j].firstChild.nodeValue == "None" ) {

				// Write Dates
				} else if ( content[docTag][i].childNodes[j].nodeName == "date" ) {
					var reldate = new Date(content[docTag][i].childNodes[j].firstChild.nodeValue);
					var month = reldate.getMonth()+1;
					var day = reldate.getDate();
					var year = reldate.getFullYear();
					search_content += '<li id="'+content[docTag][i].childNodes[j].nodeName+'">'+month+'/'+day+'/'+year+'</li>\n';

				// Write URLs
				} else if ( content[docTag][i].childNodes[j].nodeName == "url" ) {
					search_content += '<li id="'+content[docTag][i].childNodes[j].nodeName+'"><a href="'+content[docTag][i].childNodes[j].getAttribute('link')+'">'+content[docTag][i].childNodes[j].firstChild.nodeValue+'</a></li>\n';

				// Write Phone Numbers
				} else if ( content[docTag][i].childNodes[j].nodeName == "phone" || content[docTag][i].childNodes[j].nodeName == "fax" || content[docTag][i].childNodes[j].nodeName == "mobile" || content[docTag][i].childNodes[j].nodeName == "pager" ) {
					search_content += '<li id="'+content[docTag][i].childNodes[j].nodeName+'"><strong>'+content[docTag][i].childNodes[j].nodeName+':</strong> '+content[docTag][i].childNodes[j].firstChild.nodeValue+'</li>\n';

				// Write Email Addresses
				} else if ( content[docTag][i].childNodes[j].nodeName == "email" ) {
					search_content += '<li id="'+content[docTag][i].childNodes[j].nodeName+'"><strong>'+content[docTag][i].childNodes[j].nodeName+':</strong> <a href="mailto:'+content[docTag][i].childNodes[j].firstChild.nodeValue+'">'+content[docTag][i].childNodes[j].firstChild.nodeValue+'</a></li>\n';

				// Write HTTP Addresses
				} else if ( content[docTag][i].childNodes[j].nodeName == "web" || content[docTag][i].childNodes[j].nodeName == "plan") {
					search_content += '<li id="'+content[docTag][i].childNodes[j].nodeName+'"><strong>'+content[docTag][i].childNodes[j].nodeName+':</strong> <a href="'+content[docTag][i].childNodes[j].firstChild.nodeValue+'">'+content[docTag][i].childNodes[j].firstChild.nodeValue+'</a></li>\n';

				// Write EDOCS Documents
				} else if ( content[docTag][i].childNodes[j].nodeName == "document" ) {
					// Write Release Type
					search_content += '<li id="'+content[docTag][i].childNodes[j].nodeName+'">'+content[docTag][i].childNodes[j].getAttribute('type')+': ';
					if ( content[docTag][i].childNodes[j].getAttribute('url') != null ) {
						// Display Links to Releases by URL
						search_content += '<a href="'+content[docTag][i].childNodes[j].getAttribute('url')+'">'+content[docTag][i].childNodes[j].firstChild.nodeValue+'</a><br />\n';
					} else {
						// Display Links to Releases by EDOCS Document Number
						search_content += '<a href="http://fjallfoss.fcc.gov/edocs_public/attachmatch/'+content[docTag][i].childNodes[j].firstChild.nodeValue+'.doc">Word</a> | ';
						search_content += '<a href="http://fjallfoss.fcc.gov/edocs_public/attachmatch/'+content[docTag][i].childNodes[j].firstChild.nodeValue+'.pdf">Acrobat</a></li>\n';
					}

				// Write Everything Else
				} else {
					search_content += '<li id="'+content[docTag][i].childNodes[j].nodeName+'">'+content[docTag][i].childNodes[j].firstChild.nodeValue+'</li>\n';
				}
			}
			if (ctrtripped) {
					//Increment Counter
					ctr++;
					ctrtripped = Boolean(false);
			}
		}
	}

// Check To See If The Search Criteria Was Satisfied
	if (ctr == 0) {
		search_content = no_search_content;
		search_content += '<li id="zerocount">There are no '+docTag+'s that satisfy the search criteria.</li>\n';
	}

	search_content += '</ul>\n';

	srchresults.innerHTML = srchresults.textContent = search_content;
	srchresults.display = 'block';
	srchresults.style.visibility='visible';
	return;
}
