// JavaScript Document

function InitializeCraigslistLocationForms(formObj) {
	var countryList = formObj.country;
	countryList.options.length = 0;
	var i = 0;
	for (var country in regions) {
		countryList.options[i] = new Option(country, country);
		i++;
	}
	handleCountryChanged();
}

function handleCountryChanged() {
	var stateList = document.getElementById('state');
	var countryList = document.getElementById('country');
stateList.options.length = 0;
    var country = countryList.options[countryList.selectedIndex].value;
	var i = 0;
	for (var state in regions[country]) {
		stateList.options[i] = new Option(state, state);
		i++;
	}
	handleStateChanged();
}

function handleStateChanged() {
	var stateList = document.getElementById('state');
	var countryList = document.getElementById('country');
	var cityList = document.getElementById('location');
	cityList.options.length = 0;
    var country = countryList.options[countryList.selectedIndex].value;
	var state = stateList.options[stateList.selectedIndex].value;
	var i = 0;	
	for (var cityObj in regions[country][state]) {
		cityData = regions[country][state][cityObj];
		cityList.options[i] = new Option(cityData[1], cityData[0]);
		i++;
	}
}



function PopulateCraigsCityList(formObj) {
	var cityList = formObj.location;
	cityList.options.length=0

	for (i=0; i < cities.length; i++) {
		cityList.options[i]=new Option(cities[i], url_cities[i]);
	}
	return;
}


        function loadSelectElement(selObjId, options) {
            var selObj = document.getElementById(selObjId);

            // clear the target select element apart from the "select your..." option
            selObj.options.length = 1;

            // copy options from array of [value, pair] arrays to select box
            // IE doesn't work if you use the DOM-standard method, however...
            if (typeof(window.clientInformation) != 'undefined') {
                // IE doesn't take the second "before" parameter...
                for (var loop=0; loop<options.length; loop++) selObj.add(new Option(options[loop][1], options[loop][0]));
            } else {
                for (var loop=0; loop<options.length; loop++) selObj.add(new Option(options[loop][1], options[loop][0]), null);
            }
        }

        function madeSelection(selObj) {
            var selectedValue = selObj.options[selObj.selectedIndex].value;
            var selectedText = selObj.options[selObj.selectedIndex].text;
            if (selectedValue == '--') return;

            if (selObj.name == 'select01') {
                document.getElementById('select02Container').style.display = 'block';
                document.getElementById('select02').options[0].text = 'Select the breed of your ' + selectedText.toLowerCase();

                switch(selectedValue) {
                    case 'type_cat':
                        loadSelectElement('select02', [
                            ['breed_persian', 'Persian'],
                            ['breed_tabby', 'Tabby'],
                            ['breed_siamese', 'Siamese']
                        ]);
                        return;

                    case 'type_dog':
                        loadSelectElement('select02', [
                            ['breed_alsatian', 'Alsatian'],
                            ['breed_springer_spaniel', 'Springer Spaniel'],
                            ['breed_king_charles_spaniel', 'King Charles Spaniel'],
                            ['breed_chihuahua', 'Chihuahua'],
                            ['breed_shih_tzu', 'Shih Tzu']
                        ]);
                        return;

                    case 'type_bird':
                        loadSelectElement('select02', [
                            ['breed_parrot', 'Parrot'],
                            ['breed_cock', 'Cock']
                        ]);
                        return;

                    case 'type_fish':
                        loadSelectElement('select02', [
                            ['breed_goldfish', 'Goldfish']
                        ]);
                        return;
                }
            } // select01

            if (selObj.name == 'select02') {
                document.getElementById('select03Container').style.display = 'block';
                document.getElementById('select03').options[0].text = 'Select the colour of your ' + selectedText;

                switch(selectedValue) {
                    case 'breed_persian':
                    case 'breed_siamese':
                        loadSelectElement('select03', [
                            ['colour_white', 'White'],
                            ['colour_grey', 'Grey'],
                            ['colour_blue', 'Blue']
                        ]);
                        return;

                    case 'breed_tabby':
                        loadSelectElement('select03', [
                            ['colour_tabby', 'Tabby']
                        ]);
                        return;

                    case 'breed_alsatian':
                    case 'breed_springer_spaniel':
                    case 'breed_king_charles_spaniel':
                    case 'breed_chihuahua':
                    case 'breed_shih_tzu':
                        loadSelectElement('select03', [
                            ['colour_brown', 'Brown'],
                            ['colour_white', 'White'],
                            ['colour_golden', 'Golden']
                        ]);
                        return;


                    case 'breed_parrot':
                        loadSelectElement('select03', [
                            ['colour_white', 'White'],
                            ['colour_yellow', 'Yellow'],
                            ['colour_red_yellow', 'Red & Yellow']
                        ]);
                        return;

                    case 'breed_cock':
                        loadSelectElement('select03', [
                            ['colour_white', 'White'],
                            ['colour_brown', 'Brown']
                        ]);
                        return;

                    case 'breed_goldfish':
                        loadSelectElement('select03', [
                            ['colour_orange', 'Orange']
                        ]);
                        return;

                }
            } // select02
        }
