var data_handler = '';
var arrCompidlist = new Array();
var catListing;
var appGroup = new Array();
appGroup[0]="";

function makePOSTList(datarequest,cat,subcat) {
	data_handler = datarequest;
	switch(datarequest) {
	case 'companies':
		url_request = 'http://www.unitedengines.com/locator/cfcs/locatorUtils.cfc?Method=getCompanies';
		break;
	case 'categories':
		url_request = 'http://www.unitedengines.com/locator/cfcs/locatorUtils.cfc?Method=getCategoriesSubCategories';
		break;
	}

	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert ('Sorry your browser does not support ajax and must update your browser in order to use this system.');
		return false;
	}
	http_request.onreadystatechange = listingresponse;
	http_request.open('GET', url_request, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Connection", "close");
	http_request.send('');
}

function listingresponse() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var result = http_request.responseText;
			var resulteval = eval('('+result+')');
			switch(data_handler) {
			case 'companies':
				buildCompanyListing(resulteval);
			break;
			case 'categories':
				buildCategoryListing(resulteval);
			break;
			}
		} else {
			alert ('There was a problem with processing your information.');
		}
	}
}

function buildCompanyListing(sCompanies) {
	var currComp = '';
	var msg = '<ol>';
	for(i=0; i < sCompanies.DATA.length; i++) {
		var strCompDtls = sCompanies.DATA[i].toString();
		var arrCompDtls = strCompDtls.split(',');
			
		if (currComp != arrCompDtls[0]) {
			if (i > 0) {
				msg += '</ol>';
				msg += '</li>';
			}
			msg += '<li class="subdivision">'+arrCompDtls[0]+' --------------------------------------------------------------------------------------------------------------------------------------------------';
			msg += '<ol>';
			currComp = arrCompDtls[0];
		}
			
		msg += '<li class="vevent">';
		msg += '<span class="location vcard" id="'+arrCompDtls[1]+'">';
		msg += '<abbr class="geo adr" title="'+arrCompDtls[10]+';'+arrCompDtls[11]+'"></abbr>';
		msg += '<span class="street-address">'+arrCompDtls[2]+'</span> <br />';
		msg += '<span class="locality">'+arrCompDtls[3]+'</span>, ';
		msg += '<span class="region">'+arrCompDtls[4]+'</span>';
		msg += '<span class="postal-code">'+arrCompDtls[5]+'</span> <br />';
		msg += '<span class="phone">office</span> '+arrCompDtls[6]+' <br />';
		msg += '<span class="fax">fax</span> '+arrCompDtls[7];
		msg += '</span>';
		msg += '</li>';
		arrCompidlist.push(arrCompDtls[1]);
	}
	msg += '</ol>';
	msg += '</li>';
	msg += '</ol>';
	document.getElementById("events").innerHTML = msg;
	makePOSTList('categories');
	partyOn();
}

function buildCategoryListing(sCat) {
	var currCat = '';
	var iCatCntr = 1;
	catListing = sCat;
	
	var filteronelist=document.filtercontrols.filterone;
	filteronelist.options.length=1;
	for(i=0; i < sCat.DATA.length; i++) {
		var strCatDtls = sCat.DATA[i].toString();
		var arrCatDtls = strCatDtls.split(',');
		
		if (currCat != arrCatDtls[0]) {
			filteronelist.options[filteronelist.options.length]=new Option(arrCatDtls[0], iCatCntr);
			currCat = arrCatDtls[0];
			iCatCntr++;
		}
	}
	buildSubCategoryListing();
}

function buildSubCategoryListing() {
	var currMainCat = '';
	var msg = '';
	
	for(i=0; i < catListing.DATA.length; i++) {
		var strCatDtls = catListing.DATA[i].toString();
		var arrCatDtls = strCatDtls.split(',');
		if (currMainCat != arrCatDtls[0]) {
			if (currMainCat.length > 0) {
				appGroup.push(currArrSubCats);
			}
			var currArrSubCats = new Array();
			currMainCat = arrCatDtls[0];
		}
		currArrSubCats.push(arrCatDtls[1]);
	}
	appGroup.push(currArrSubCats);
}

function buildfiltertwo(selectedfilterone){
	var filteronelist=document.filtercontrols.filterone
	var filtertwolist=document.filtercontrols.filtertwo
	textToBlack();
	setMapDefault();
	if (selectedfilterone == 0) {
		filtertwolist.style.display = 'none';
	}
		
	if (filtertwolist.style.display == 'none' && selectedfilterone != 0) {
		filtertwolist.style.display = '';
		filtertwolist.style.zIndex = '1000';
	}
	
	switch(selectedfilterone) {
	case 1:
		filtertwolist.options[filtertwolist.options.length]=new Option("Select an Application", 0);
	break;
	case 2:
		filtertwolist.options[filtertwolist.options.length]=new Option("Select an Part or Service", 0);
	break;
	case 3:
		filtertwolist.options[filtertwolist.options.length]=new Option("Select an Product", 0);
	break;
	}
	
	
	filtertwolist.options.length=1;
	if (selectedfilterone>0){
		for (i=0; i<appGroup[selectedfilterone].length; i++) {
			//filtertwolist.options[filtertwolist.options.length]=new Option(appGroup[selectedfilterone][i].split("|")[0], appGroup[selectedfilterone][i].split("|")[1])
			filtertwolist.options[filtertwolist.options.length]=new Option(appGroup[selectedfilterone][i], i+1);
		}
		
	}
}

function getfilteredList(selectedFilter, selectedindex) {
	textToBlack();
	if (selectedindex > 0) {
		var catName = document.filtercontrols.filterone.options[document.filtercontrols.filterone.value].text;
		var subCatName = document.filtercontrols.filtertwo.options[selectedindex].text;
		var poststr = "Method=getCompaniesBySubCategory&CAT_NAME="+encodeURI(catName)+"&SUBCAT_NAME="+encodeURI(subCatName);
		makePOSTfilter(poststr);
	}
}

function makePOSTfilter(parameters) {
	http_request = false;
	url_request = 'http://www.unitedengines.com/locator/cfcs/locatorUtils.cfc?'+parameters;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert ('Sorry your browser does not support ajax and must update your browser in order to use this system.');
		return false;
	}
	http_request.onreadystatechange = filteredListResponse;
	http_request.open('GET', url_request, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Connection", "close");
	http_request.send('');
}

function filteredListResponse() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var result = http_request.responseText;
			var resulteval = eval('('+result+')');
			updateLocationPg(resulteval);
		} else {
			alert('There was a problem with processing your information.');
		}
	}
}

function updateLocationPg(resultFltrd) {
	var filtrd = ':::'+resultFltrd.DATA.join(':::')+':::';
	for(i=0; i < arrCompidlist.length; i++) {
		if (filtrd.indexOf(':::'+arrCompidlist[i]+':::') === -1) {
			document.getElementById(arrCompidlist[i]).style.color = '#CCC';
		}
	}
}

function textToBlack() {
	for(i=0; i < arrCompidlist.length; i++) {
		document.getElementById(arrCompidlist[i]).style.color = '#333';
	}
}

function resetLocator() {
	var filteronelist=document.filtercontrols.filterone
	var filtertwolist=document.filtercontrols.filtertwo
	if (filtertwolist.style.display == '') {
		filtertwolist.style.display = 'none';
	}
	filteronelist.selectedIndex = 0;
	textToBlack();
	setMapDefault();
}

