var select0 = document.getElementById("select0");
var select1 = document.getElementById("select1");

// First menu definition
var menu0 = new Array();

menu0[0] = Array("- Select one -", "");
menu0[1] = Array("Service Partner", "service-partner");
menu0[2] = Array("Technology Partner", "technology-partner");

// Second menu definitions
var menu1 = new Array();

menu1[0] = new Array();
menu1[0][0] = new Array("- Select one -", "");

menu1[1] = new Array();
menu1[1][0] = new Array("- Select one -", "");
menu1[1][1] = new Array("Common Ground Implementation", "common-ground-implementation");
menu1[1][2] = new Array("Convio eCRM Deployment (PageBuilder)", "convio-ecrm-deployment-pagebuilder");
menu1[1][3] = new Array("Convio eCRM Website Administration Services (PageBuilder)", "convio-ecrm-website-administration-services-pagebuilder");
menu1[1][4] = new Array("Convio Advocacy Implementation", "convio-advocacy-implementation");
menu1[1][5] = new Array("Convio eCommerce Implementation", "convio-ecommerce-implementation");
menu1[1][6] = new Array("Convio Fundraising Implementation", "convio-fundraising-implementation");
menu1[1][7] = new Array("Convio Personal Events Implementation", "convio-personal-events-implementation");
menu1[1][8] = new Array("Convio TeamRaiser Implementation", "convio-teamraiser-implementation");
menu1[1][9] = new Array("Convio Tributes Implementation", "convio-tributes-implementation");
menu1[1][10] = new Array("Data Integration", "data-integration");
menu1[1][11] = new Array("Website Design & Branding", "website-design-and-branding");
menu1[1][12] = new Array("Advocacy Consulting", "advocacy-consulting");
menu1[1][13] = new Array("Direct Marketing Agency", "direct-marketing-agency");
menu1[1][14] = new Array("Event Strategy & Production", "event-strategy-and-production");
menu1[1][15] = new Array("Fundraising Consulting", "fundraising-consulting");
menu1[1][16] = new Array("Strategic Planning", "strategic-planning");
menu1[1][17] = new Array("Wealth Screening", "wealth-screening");
menu1[1][18] = new Array("Email Append/List Hygiene", "email-append-list-hygiene");
menu1[1][19] = new Array("Other", "other");

menu1[2] = new Array();
menu1[2][0] = new Array("- Select one -", "");
menu1[2][1] = new Array("Email Delivery", "email-delivery");
menu1[2][2] = new Array("Social Networking/Blogs/Forums", "social-networking-blogs-forums");
menu1[2][3] = new Array("Ticketing Database", "ticketing-database");
menu1[2][4] = new Array("Streaming Media & Storage", "streaming-media-and-storage");
menu1[2][5] = new Array("Mobile", "mobile");
menu1[2][6] = new Array("Donor/Member Management Database", "donor-member-management-database");
menu1[2][7] = new Array("Web Analytics", "web-analytics");
menu1[2][8] = new Array("Merchant Account", "merchant-account");
menu1[2][9] = new Array("Other", "other");


function addOption(selectBox, value, text) {
	var option = document.createElement('option');
	option.setAttribute('value', value);
	option.appendChild(document.createTextNode(text));
	selectBox.appendChild(option);
}

function clearOptions(selectBox) {
	for (var i = selectBox.length - 1; i >= 0; i--) {
		selectBox.options[i] = null;
	}
}

function buildFirstMenu() {
	var previousSelection = getUrlParam("partner_type");
	for (var i = 0; i < menu0.length; i++) {
		addOption(select0, i, menu0[i][0]);
		if (menu0[i][1] == previousSelection) { 
			select0.selectedIndex = i;
		}
	}
	buildSecondMenu();
}

function buildSecondMenu() {
	clearOptions(select1);
	var ss = getUrlParam("specialization_service");
	var st = getUrlParam("specialization_technology");
	var previousSelection = "";
	if (ss != "") { 
		previousSelection = ss;
	} else if (st != "") { 
		previousSelection = st;
	}
	
	var menu0Selection = select0.selectedIndex;
	for (var i = 0; i < menu1[menu0Selection].length; i++) {
		addOption(select1, i, menu1[menu0Selection][i][0]);
		if (menu1[menu0Selection][i][1] == previousSelection) { 
			select1.selectedIndex = i;
		}
	}
}

function go() {
	var menu0Selection = menu0[select0.selectedIndex][1];
	var menu1Selection = menu1[select0.selectedIndex][select1.selectedIndex][1];
	var specService = "";
	var specTech = "";
	if (menu0Selection == "service-partner") {
		specService = menu1Selection;
	}
	if (menu0Selection == "technology-partner") {
		specTech = menu1Selection;
	}
	var url = "/partners/partner-directory/?partner_type=" + menu0Selection + "&specialization_technology=" + specTech + "&specialization_service=" + specService;
	document.location = url;
}

function getUrlParam( name ) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}
