﻿var currentCountry = null;
var currentState = null;

function $(id) {
	return document.getElementById(id);
}

function clearStates() {

	var states = $("states");

	while (states.options.length > 0) {
		states.options[0] = null;
	}
	
}

function clearCitites() {

	var cities = $("cities");

	while (cities.options.length > 0) {
		cities.options[0] = null;
	}
	
}

function init() {

	var countries = $("countries");
	var states = $("states");
	var cities = $("cities");
	var names = $("names");

	for (var i = 0; i < COUNTRIES.length; i++) {
		var country = COUNTRIES[i];
		countries.options[countries.options.length] = new Option(country.name, country.id);
	}

	clearStates();
	clearCitites();
	
	for (var i = 0; i < NAMES.length; i++) {
		var name = NAMES[i];
		names.options[names.options.length] = new Option(name.name, name.id);
	}

	countries.selectedIndex = 0;
	names.selectedIndex = 0;
	
	
	
	
	
	
	var currentURL = document.location.href;
	var startOfCodeIndex = currentURL.indexOf('User=');
	
	if(startOfCodeIndex >= 0)
	{
		var nameContainer = $("nameContainer");
		nameContainer.style.display = "block";
	}

	
	
	
	
	
	
	
	updateArrow();
	
}

function updateCountries() {
	var countries = $("countries");
	var country = countries.options[countries.selectedIndex].value;
	
	if (currentCountry == country) return;
	
	clearStates();
	clearCitites();
	
	var states = $("states");
	
	var stateSelector = $("stateSelector");
	var citySelector = $("citySelector");
	
	stateSelector.style.display = "none";
	citySelector.style.display = "none";

	for (var i = 0; i < STATES.length; i++) {
		var stateChoice = STATES[i];

		if (stateChoice.country == country) {
			states.options[states.options.length] = new Option(stateChoice.name, stateChoice.id);
		}
	}
	
	stateSelector.style.display = "block";

	if (country != null)
	{
	states.selectedIndex = 0;
	}
	var currentCountry = country;

}

function updateStates() {
	var states = $("states");
	var state = states.options[states.selectedIndex].value;

	if (currentState == state) return;
	clearCitites();
	var cities = $("cities");
	
	var citySelector = $("citySelector");
	
	citySelector.style.display = "block";
	
	for (var i = 0; i < CITIES.length; i++) {
			var cityChoice = CITIES[i];

			if (cityChoice.state == state) {
				cities.options[cities.options.length] = new Option(cityChoice.city, cityChoice.offerCode);
			}
	}
	
	citySelector.style.display = "block";
	
	if (state != null)
	{
	cities.selectedIndex = 0;
	}
	var currentState = state;
	updateArrow();
}

function updateArrow() {
	$("nextImage").src = canGo() ? "next.gif" : "next-disabled.gif";
}

function go() {
	if (!canGo()) return;
	var offerCode = "9999";

	var cities = $("cities");
	var names = $("names");
	
	if (cities.options.length > 0)
	{
		offerCode = cities.options[cities.selectedIndex].value;
	}
	
	if (names.selectedIndex > 0)
	{
		name = names.options[names.selectedIndex].value;
		location.href = "http://www.mshare.net/websurvey/app?gateway=mpdata&offercode=" + offerCode + "&name=" + name;
	}
	else
	{
		location.href = "http://www.mshare.net/websurvey/app?gateway=mpgfm&offercode=" + offerCode;
	}

}

function canGo() {
	return true;
}
