// JavaScript Document

function sendSearchInfo() {
	
	
	var keywords = document.getElementById("keywords").value;
	var location = document.getElementById("location").value;
	
	// setup the naics variable to be zero if nothing selected
	var naics_main = document.getElementById("mainCat").options[document.getElementById("mainCat").selectedIndex].value;
	var naics_sub = document.getElementById("subCat").options[document.getElementById("subCat").selectedIndex].value;
	
	 if (naics_sub == "0") {
		 var naics = naics_main;
	 } else {
		 var naics = naics_sub;
	 }
	 
	 var url = "includes/functions.inc.php?action=searchDirectory";
	 request = new Ajax.Request(url,
								{
									method: 'post',
									parameters: {keywords:keywords, location:location, naics:naics},
									onSuccess: updatePage
								});
}

function updatePageOld(request) {
	// when php returns the search results in xml
	// load the XML and style with an XSL sheet
	// then display the results where the search box was originally
	
	 var maincontent = document.getElementById("content");
	 var mainParent = maincontent.parentNode;
	 
	 var xmlDoc = request.responseXML;
	 var fragment;

	// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("http://dev.topfloormedia.com/yukon/directory/search.xsl")

// Transform
fragment = xmlDoc.transformNode(xsl);


	
	divholder = document.createElement("div");
	
	mainParent.replaceChild(divholder,maincontent);
	mainParent.appendChild(fragment);
}

function updatePage(request) {
	// when php returns the search results in xml
	// load the XML and style with an XSL sheet
	// then display the results where the search box was originally
	
	var xmlDoc = request.responseXML;
	var fragment;
	
	var maincontent = document.getElementById("content");
	var mainParent = maincontent.parentNode;
	
	var xslStylesheet;
	var xsltProcessor = new XSLTProcessor();
	
	var myXMLHTTPRequest = new XMLHttpRequest();
	//var myXMLHTTPRequest = new ActiveXObject("Msxml2.XMLHTTP.4.0");
	myXMLHTTPRequest.open("GET", "http://dev.topfloormedia.com/yukon/directory/search.xsl", false);
	myXMLHTTPRequest.send(null);
	
	
	xslStylesheet = myXMLHTTPRequest.responseXML;
	
	// ie good up to here
	xsltProcessor.importStylesheet(xslStylesheet);
	// ** ie fails here **
	
	//alert(new XMLSerializer().serializeToString(fragment));
	//fragment = xsltProcessor.transformToFragment(xmlDoc, document);
	fragment = xsltProcessor.transformToFragment(xmlDoc, document);
	
	
	divholder = document.createElement("div");
	
	mainParent.replaceChild(divholder,maincontent);
	mainParent.appendChild(fragment);
}

function getMainCat() {
	var url = "includes/functions.inc.php?action=getMainCat";
	request = new Ajax.Request(url,
								{
									method: 'post',
									onSuccess: populate
								});
}

function populate(request) {
	var xmlDoc = request.responseXML;
	
	
	var mainCat = document.getElementById("mainCat");
	// disable it while we work
	mainCat.disabled = true;
	
	
	for (var i =0; i < xmlDoc.getElementsByTagName("naics").length; i++) {
		// loop through each category element and add it to the drop down of list
		var catId = xmlDoc.getElementsByTagName("naics")[i].firstChild.nodeValue;
		var catName = xmlDoc.getElementsByTagName("desc")[i].firstChild.nodeValue;		
		
		// populate the next option from  i cause the 0 option is a 'please choose'
		mainCat.options[i+1] = new Option(catName);
		mainCat.options[i+1].value = catId;
	} // end for loop

	// enable the element for use
	mainCat.disabled = false;
}

function getSubCat() {
	var url = "includes/functions.inc.php?action=getSubCat";
	// get the naics value of the selected item from the drop down
	var naics = document.getElementById("mainCat").options[document.getElementById("mainCat").selectedIndex].value;
	// fire the ajax request and pass the naics value
	request = new Ajax.Request(url,
								{
									method: 'post',
									parameters: {naics:naics},
									onSuccess: popSubCat
								});
}



function popSubCat(request) {
	var xmlDoc = request.responseXML;	
	var i =0;
	
	var subCat = document.getElementById("subCat");
	// disable the element while we work
	subCat.disabled = true;
	
	// clear the options first
	 for (var i = (subCat.options.length-1); i >= 1; i--){
         subCat.options[i]=null;
     }
	
	
	for (var i =0; i < xmlDoc.getElementsByTagName("naics").length; i++) {
		// loop through each category element and add it to the drop down of list
		var catId = xmlDoc.getElementsByTagName("naics")[i].firstChild.nodeValue;
		var catName = xmlDoc.getElementsByTagName("desc")[i].firstChild.nodeValue;		
		
		// populate the next option from  i cause the 0 option is a 'please choose'
		subCat.options[i+1] = new Option(catName);
		subCat.options[i+1].value = catId;
	} 
	//console.log('calling init');
	//initCastomForms();
	// enable the element
	subCat.disabled = false;
}
