﻿var currentState = null;
var currentDestination = null;

function $(id) {
	return document.getElementById(id);
}

function clearDestinations() {

	var destinations = $("destinations");

	while (destinations.options.length > 1) {
		destinations.options[1] = null;
	}
	
}

function clearVenues() {

	var venues = $("venues");

	while (venues.options.length > 1) {
		venues.options[1] = null;
	}
	
}

function init() {

	var states = $("states");
	var destinations = $("destinations");
	var venues = $("venues");

	for (var i = 0; i < STATES.length; i++) {
		var state = STATES[i];
		states.options[states.options.length] = new Option(state.name, state.id);
	}
	
	clearDestinations();
	clearVenues();
	
	var offer = $("offer");
	offer.style.display = "none";
	
	states.selectedIndex = 0;
	
	var doSearch = $("feedbackSearch");
	doSearch.checked = true;
}

function updateRadio() {

	var states = $("states");

	var doSearch = $("feedbackSearch");
	var doCode = $("feedbackCode");
	
	var offer = $("offer");
	var stateSelector = $("stateSelector");
	var destinationSelector = $("destinationSelector");
	var venueSelector = $("venueSelector");
	
	if (doCode.checked)
	{
		offer.style.display = "inline";
		stateSelector.style.display = "none";
		destinationSelector.style.display = "none";
		venueSelector.style.display = "none";
		
		states.selectedIndex = 0;
	}
	else
	{
		offer.style.display = "none";
		stateSelector.style.display = "block";
		
		states.selectedIndex = 0;
	}
	updateArrow();
}

function updateStates() {
	var states = $("states");
	var state = states.options[states.selectedIndex].value;
	if (currentState == state) return;
	clearDestinations();
	clearVenues();
	var destinations = $("destinations");
	
	var destinationSelector = $("destinationSelector");
	var venueSelector = $("venueSelector");
	
	destinationSelector.style.display = "none";
	venueSelector.style.display = "none";
	
	for (var i = 0; i < DESTINATIONS.length; i++) {
		var destinationChoice = DESTINATIONS[i];

		if (destinationChoice.state == state) {
			var stateDelimiter = destinationChoice.destination.lastIndexOf(':');
			var destinationTruncated = destinationChoice.destination.substring(0,stateDelimiter);
			destinations.options[destinations.options.length] = new Option(destinationTruncated, destinationChoice.destination);
		}
	}
	
	destinationSelector.style.display = "block";

	if (state != null)
	{
	destinations.selectedIndex = 0;
	}
	var currentState = state;
	updateArrow();
}

function updateDestinations() {
	var destinations = $("destinations");
	var destination = destinations.options[destinations.selectedIndex].value;
	if (currentDestination == destination) return;
	clearVenues();
	var venues = $("venues");
	
	var venueSelector = $("venueSelector");
	
	venueSelector.style.display = "none";
	
	for (var i = 0; i < VENUES.length; i++) {
			var venueChoice = VENUES[i];

			if (venueChoice.destination == destination) {
				venues.options[venues.options.length] = new Option(venueChoice.name, venueChoice.offerCode);
			}
	}
	
	venueSelector.style.display = "block";
	
	if (state != null)
	{
	venues.selectedIndex = 0;
	}
	var currentState = state;
	updateArrow();
}

function go() {
	if (!canGo()) return;
	var offerCode = null;

	if ($("feedbackSearch").checked) {
		var venues = $("venues");
		offerCode = venues.options[venues.selectedIndex].value;
	} else {
		offerCode = $("offer").value;
	}
	
	location.href = "http://www.mshare.net/websurvey/app?gateway=guest&id=" + offerCode;
}

function canGo() {
	var doLocations = $("feedbackSearch").checked;
	return doLocations ? $("venues").selectedIndex > 0 : true;
}

function updateArrow() {
	$("nextImage").src = canGo() ? "next.gif" : "next-disabled.gif";
}

/*

function init() {

	var states = $("states");
	var cities = $("cities");
	var locations = $("locations");
	
	var citySelect = $("citySelect");
	var locationSelect = $("locationSelect");


	for (var i = 0; i < STATES.length; i++) {
		var state = STATES[i];
		states.options[states.options.length] = new Option(state.name, state.id);
	}
	
	clearCities();
	clearLocations();
	

	
	states.selectedIndex = 0;

}

function updateStates() {
	var states = $("states");
	var state = states.options[states.selectedIndex].value;
	if (currentState == state) return;
	clearCities();
	clearLocations();
	var locations = $("locations");
	var cities = $("cities");
	
	var citySelect = $("citySelect");
	var locationSelect = $("locationSelect");
	
	locationSelect.style.display = "none";
	citySelect.style.display = "none";
	
	if (state == "TX")
	{
		for (var i = 0; i < TEXAS_CITIES.length; i++) {
		var texascities = TEXAS_CITIES[i];
		cities.options[cities.options.length] = new Option(texascities.city, texascities.city);
		}
		citySelect.style.display = "block";
	} 
	else if (state == "OK")
	{
		for (var i = 0; i < OKLAHOMA_CITIES.length; i++) {
		var oklahomacities = OKLAHOMA_CITIES[i];
		cities.options[cities.options.length] = new Option(oklahomacities.city, oklahomacities.city);
		}
		citySelect.style.display = "block";
	} 
	else if (state != null)
	{
		for (var i = 0; i < LOCATIONS.length; i++) {
			var location = LOCATIONS[i];

			if (location.state == state) {
				locations.options[locations.options.length] = new Option(location.name, location.offerCode);
			}
		}
		locationSelect.style.display = "block";
	}

	if (state != null)
	{
	locations.selectedIndex = 0;
	cities.selectedIndex = 0;
	}
	var currentState = state;
	updateArrow();
}

function updateCities() {
	var cities = $("cities");
	var city = cities.options[cities.selectedIndex].value;
	var locations = $("locations");
	if (currentCity == city) return;
	clearLocations();
	
	var citySelect = $("citySelect");
	var locationSelect = $("locationSelect");
	
	locationSelect.style.display = "none";
	
	for (var i = 0; i < LOCATIONS.length; i++) {
			var location = LOCATIONS[i];

			if (location.city == city) {
				locations.options[locations.options.length] = new Option(location.name, location.offerCode);
			}
	}
	
	if (city != null)
	{
		locations.selectedIndex = 0;
		locationSelect.style.display = "block";
	}
	var currentCity = city;
	updateArrow();
}

function updateArrow() {
	$("nextImage").src = canGo() ? "next.gif" : "next-disabled.gif";
}

function go() {
	if (!canGo()) return;
	var offerCode = null;

	if ($("feedbackLocation").checked) {
		var locations = $("locations");
		offerCode = locations.options[locations.selectedIndex].value;
	} else {
		offerCode = CORPORATE_OFFER_CODE;
	}

	location.href = "http://www.mshare.net/websurvey/app?gateway=tbcfms&offercode=" + offerCode;
}

function canGo() {
	var doLocations = $("feedbackLocation").checked;
	return doLocations ? $("locations").selectedIndex > 0 : true;
}



var currentState = null;

function $(id) {
	return document.getElementById(id);
}

function init() {

	var states = $("states");

	for (var i = 0; i < STATES.length; i++) {
		var state = STATES[i];
		states.options[states.options.length] = new Option(state.name, state.id);
	}

	states.selectedIndex = 0;
	clearLocations();
	updateArrow();

}

function updateRadio() {
	var doLocations = $("feedbackLocation").checked;
	$("states").disabled = !doLocations;
	$("locations").disabled = !doLocations;
	updateArrow();
}

function updateLocations() {
	var states = $("states");
	var state = states.options[states.selectedIndex].value;
	if (currentState == state) return;
	clearLocations();
	var locations = $("locations");

	for (var i = 0; i < LOCATIONS.length; i++) {
		var location = LOCATIONS[i];

		if (location.state == state) {
			locations.options[locations.options.length] = new Option(location.name, location.offerCode);
		}
	}

	if (state != null) locations.selectedIndex = 0;
	locations.style.display = state != null ? "block" : "none";
	var currentState = state;
	updateArrow();
}

function clearLocations() {
	var locations = $("locations");

	while (locations.options.length > 1) {
		locations.options[1] = null;
	}
}

function clearCities() {
	var cities = $("cities");

	while (cities.options.length > 1) {
		cities.options[1] = null;
	}
}

function updateArrow() {
	$("nextImage").src = canGo() ? "next.gif" : "next-disabled.gif";
}

function go() {
	if (!canGo()) return;
	var offerCode = null;

	if ($("feedbackLocation").checked) {
		var locations = $("locations");
		offerCode = locations.options[locations.selectedIndex].value;
	} else {
		offerCode = CORPORATE_OFFER_CODE;
	}

	location.href = "http://www.mshare.net/websurvey/app?gateway=tbcfms&offercode=" + offerCode;
}

function canGo() {
	var doLocations = $("feedbackLocation").checked;
	return doLocations ? $("locations").selectedIndex > 0 : true;
}

*/
