function getXmlHttp() {
	var xmlhttp = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function appendOption(selectElement, text, value) {
	opt = document.createElement('option');
	opt.text = text;
	opt.value = value;

	try {
		selectElement.add(opt, null);
	} catch (ex) {
		selectElement.add(opt);
	}
}

function populateSelect(updatedElement) {
	var xmlHttp = getXmlHttp();

	if (xmlHttp) {
		var country = document.getElementById('nearby_country');
		var region = document.getElementById('nearby_region');
		var city = document.getElementById('nearby_city');
		var url = '/ajax/receiver.php?';
		var results = null;

		if (updatedElement == 'nearby_country') {
			url += 'method=ajaxGetRegions';
			url += '&country_code=' + country.options[country.selectedIndex].value;
		} else {
			resetInput(updatedElement);

			url += 'method=ajaxGetCities';
			url += '&country_code=' + country.options[country.selectedIndex].value;
			url += '&region_name=' + region.options[region.selectedIndex].value;
		}

		xmlHttp.open('GET', url, true);
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					results = eval('(' + xmlHttp.responseText + ')');
					if (updatedElement == 'nearby_country') {
						region.length = 0;
						if (results.length != 0) {
							appendOption(region, 'Select a Region...', '');
							for (var result in results) {
								appendOption(region, results[result], results[result]);
							}
						} else {
							appendOption(region, 'Select a Country First', '');
						}
						city.length = 0;
						appendOption(city, 'Select a Region First', '');
					} else {
						city.length = 0;
						if (results.length != 0) {
							appendOption(city, 'Select a City...', '');
							for (var result in results) {
								appendOption(city, results[result], results[result]);
							}
						} else {
							appendOption(city, 'Select a Region First', '');
						}
					}
					return;
				}
			}
		}
		xmlHttp.send(null);
	}
}

function resetInput(updatedElement) {
    if (updatedElement != 'nearby_postal') {
        document.getElementById('nearby_postal').value = '';
    } else {
        var region = document.getElementById('nearby_region');
        var city = document.getElementById('nearby_city');

        if (region.selectedIndex != 0) {
            //region.options[0].setAttribute('selected',true);
            region.selectedIndex = 0;
        }

        if (city.selectedIndex != 0) {
            //city.options[0].setAttribute('selected',true);
            city.selectedIndex = 0;
        }
    }
}

function manageDropdowns() {
    var country = document.getElementById('nearby_country');
    var region = document.getElementById('nearby_region');
    var city = document.getElementById('nearby_city');

    if (country.options[country.selectedIndex].value == '') {
        var map_cookie = readCookie('map');
        if (map_cookie == null || map_cookie == 'mx') {
            map_cookie = '';
        }
        for (var i = 0; i < country.options.length; i++) {
            if (map_cookie == country.options[i].value) {
                //country.options[i].setAttribute('selected','selected');
                country.selectedIndex = i;
                break;
            }
        }
    }

    if (country.options[country.selectedIndex].value != '') {
        if (region.selectedIndex == -1 || region.options[region.selectedIndex].value == '') {
            populateSelect('nearby_country');
        } else if (city.selectedIndex == -1 || city.options[city.selectedIndex].value == '') {
            populateSelect('nearby_region');
        }
    }
}
