var req = null;

function InitXMLHttpRequest() {
	// Make a new XMLHttp object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
}


function SelectCountry(section, destination) {
	InitXMLHttpRequest();
	// Load the result from the response page
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				destination.innerHTML = req.responseText;
			} else {
				destination.innerHTML = "Loading data...";
			}
		}
		req.open("GET", "location.php?sec=" + section + "&sel=country", true);
		req.send(null);
	} else {
		destination.innerHTML = 'Browser unable to create XMLHttp Object';
	}
}

function SelectRegion(section, id_country, destination, destination2, text, text2) {
	InitXMLHttpRequest();
	if (id_country != '') {
		if (req) {
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					destination.innerHTML = req.responseText;
				} else {
					destination.innerHTML = "<select class=\"location\"><option>"+text+"</option></select>";
				}
			}
			req.open("GET", "location.php?sec=" + section + "&sel=region&id_country=" + id_country, true);
			req.send(null);
		} else {
			destination.innerHTML = 'Browser unable to create XMLHttp Object';
		}
	} else {
		req.open('GET', 'location.php?sec=' + section + '&sel=region', false);
	    req.send(null);
    	destination.innerHTML = req.responseText;
	}
//    destination2.innerHTML = "<select class=\"location\"><option>"+text2+"</option></select>";
}

function SelectRegion2(section, id_country, destination, destination2, text, text2, destination3) {
	InitXMLHttpRequest();
	if (id_country != '') {
		if (req) {
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					destination.innerHTML = req.responseText;
				} else {
					destination.innerHTML = "<select class=\"location\"><option>"+text+"</option></select>";
				}
			}
			req.open("GET", "location.php?sec=" + section + "&sel=region2&id_country=" + id_country, true);
			req.send(null);
		} else {
			destination.innerHTML = 'Browser unable to create XMLHttp Object';
		}
	} else {
		req.open('GET', 'location.php?sec=' + section + '&sel=region', false);
	    req.send(null);
    	destination.innerHTML = req.responseText;
	}
//    destination2.innerHTML = "<select class=\"location\"><option>"+text2+"</option></select>";
    if ((section == 'rnte') || (section == 'rmte')){
    	destination3.innerHTML = "";
    }
}

/*
function SelectCity(section, id_region, destination, text){
	InitXMLHttpRequest();
    if (id_region != '') {
		// Load the result from the response page
	    if (req){
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					destination.innerHTML = req.responseText;
				} else {
					destination.innerHTML = "<select class=\"location\"><option>"+text+"</option></select>";
				}
			}
	       req.open('GET', 'location.php?sec=' + section + '&sel=city&id_region=' + id_region, true);
	       req.send(null);
	    }
	    else{
	       destination.innerHTML = 'Browser unable to create XMLHttp Object';
	    }
    }
    else {
    	req.open('GET', 'location.php?sec=' + section + '&sel=city', false);
		req.send(null);
    	destination.innerHTML = req.responseText;
    }
}

function SelectCity2(section, id_region, destination, text, destination2){
	InitXMLHttpRequest();
    if (id_region != '') {
		// Load the result from the response page
	    if (req){
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					destination.innerHTML = req.responseText;
				} else {
					destination.innerHTML = "<select class=\"location\"><option>"+text+"</option></select>";
				}
			}
	       req.open('GET', 'location.php?sec=' + section + '&sel=city2&id_region=' + id_region, true);
	       req.send(null);
	    }
	    else{
	       destination.innerHTML = 'Browser unable to create XMLHttp Object';
	    }
    }
    else {
    	req.open('GET', 'location.php?sec=' + section + '&sel=city2', false);
		req.send(null);
    	destination.innerHTML = req.responseText;
    }
    if ((section == 'rnte') || (section == 'rmte')){
    	destination2.innerHTML = "";
    }
}
*/

function SelectCity(section, id_region, destination, text){
	if (id_region != '') {
		if(section=='qsi') destination.innerHTML = "<input class='text' name='city_name' id='city_name' onkeyup=\"javascript: LoadCities('load_city_window', this, '"+id_region+"');\"><input type='hidden' name='city' id='city_hidden' value=''><div id='load_city_window'></div>";

		if(section=='qsi') destination.innerHTML = "<input class='text' name='city_name' id='city_name' onkeyup=\"javascript: LoadCities('load_city_window', this, '"+id_region+"');\"><input type='hidden' name='city' id='city_hidden' value=''><div id='load_city_window'></div>";
		else if(section=='qsa') destination.innerHTML = "<input class='text2' name='city_name' id='city_name' onkeyup=\"javascript: LoadCities('load_city_window', this, '"+id_region+"');\"><input type='hidden' name='city' id='city_hidden' value=''><div id='load_city_window'></div>";
		else if(section=='acc' || section=='ch_e_v')  destination.innerHTML = "<input class='text_slim' name='city_name' id='city_name' onkeyup=\"javascript: LoadCities('load_city_window', this, '"+id_region+"');\"><input type='hidden' name='city' id='city_hidden' value=''><div id='load_city_window'></div>";
		else destination.innerHTML = "<input class='text4' name='city_name' id='city_name' onkeyup=\"javascript: LoadCities('load_city_window', this, '"+id_region+"');\"><input type='hidden' name='city' id='city_hidden' value=''><div id='load_city_window'></div>";
	}else{
		destination.innerHTML = "";
	}
}
function SelectCity2(section, id_region, destination, text, destination2){
	if (id_region != '') {
		destination.innerHTML = "<input class='text4' name='city_name' id='city_name' onkeyup=\"javascript: LoadCities('load_city_window', this, '"+id_region+"');\"><input type='hidden' name='city' id='city_hidden' value=''><div id='load_city_window'></div>";
	}else{
		destination.innerHTML = "";
	}
}

function ShowSubways(section, city_id, destination, text, text2){
	if ((section == 'rnte') || (section == 'rmte')){
		if ((city_id=='3159_4312_4400') || (city_id=='3159_4925_4962')){
			InitXMLHttpRequest();
			if (req){
				req.onreadystatechange = function() {
					if (req.readyState == 4) {
						destination.innerHTML = req.responseText;
					} else {
						destination.innerHTML = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" height=\"25\"><tr><td height=\"27\" width=\"100\">" + text2 + ":&nbsp;</td><td><select class=\"location\"><option>" + text +"</option></select></td></tr></table>";
					}
				}
		       req.open('GET', 'location.php?sec=' + section + '&sel=subway&id_city=' + city_id, true);
		       req.send(null);
		    }
		    else{
		       destination.innerHTML = 'Browser unable to create XMLHttp Object';
		    }
		} else {
			destination.innerHTML = '';
		}
	} else {
		destination.innerHTML = '';
	}
}

	function getAbsolutePos(el) {
		var r = { x: el.offsetLeft, y: el.offsetTop };
		if (el.offsetParent) {
			var tmp = getAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}
		return r;
	};

function LoadCities(div_id, input_obj, id_region){
	var url = "get_cities.php?id_region="+id_region+"&str="+input_obj.value;
	$("#city_hidden").val('0');
	$.getScript(url, function(data){
		$("#"+div_id).html(data);
	});

//	document.getElementById('city_hidden').value = 0;
//	var lineup = document.getElementById(div_id);
//	new Ajax.Updater(lineup, url, { method: 'get'});
}

function SetCity(id_city, city_name){
	$("#city_name").val(city_name);
	$("#city_hidden").val(id_city);
	$("#load_city_window").html('');
//	document.getElementById('city_name').value = city_name;
//	document.getElementById('city_hidden').value = id_city;
//	document.getElementById('load_city_window').innerHTML = '';
	return;
}

