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 != '') {
		destination.innerHTML = "<select class=\"location\"><option>"+text+"</option></select>";
		var sel="region";
		Content = $.ajax({
			url: site_root + "/location.php",
			global: false,
			type: "GET",
			async:false,
			data: ({sec : section,sel:sel,id_country:id_country}),
			dataType: "html",
			success: function(msg){
			destination.innerHTML = msg;
			}
			}
			).responseText;
		/*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 {
		Content = $.ajax({
			url: site_root + "/location.php",
			global: false,
			type: "GET",
			async:false,
			data: ({sec : section,sel:'region'}),
			dataType: "html",
			success: function(msg){
			destination.innerHTML = msg;
			//document.getElementById("search_client").innerHTML =msg;
			//document.getElementById("search_client").style.display = '';
			}
			}
			).responseText;
		/*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 != '') {
    	destination.innerHTML = "<select class=\"location\"><option>"+text+"</option></select>";
    	var sel="city";
		Content = $.ajax({
			url: site_root + "/location.php",
			global: false,
			type: "GET",
			async:false,
			data: ({sec : section,sel:sel,id_region:id_region}),
			dataType: "html",
			success: function(msg){
			//alert(msg);
			destination.innerHTML = msg;
			}
			}
			).responseText;
		// 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 {
    	var sel="city";
		Content = $.ajax({
			url: site_root + "/location.php",
			global: false,
			type: "GET",
			async:false,
			data: ({sec : section,sel:sel}),
			dataType: "html",
			success: function(msg){
			//alert(msg);
			destination.innerHTML = msg;
			}
			}
			).responseText;
    	/*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 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 = '';
	}
}
cnt__ = 0;
function LocationSearch(textfield, popup, searchtype, hiddenid) {
	cnt__ ++;
	if(document.getElementById('countries_list'))
		var id_country=document.getElementById('countries_list').value;
	else
		id_country = '';
	var sel = 'search';
	var x = textfield.value;
	var txtid = textfield.id;
	if(x == '' || x == null) {
		popup.style.display = 'none';
		hiddenid.value="";
		return true;
	}
	textfield.className = 'txt-searching';
	
	var t = cnt__;
	setTimeout(function() {
		if(cnt__ == t) {
			Content = $.ajax({
				url: site_root + "/location.php",
				global: false,
				type: "GET",
				data: ({sel:sel, x:textfield.value, txtid:txtid, searchtype:searchtype,id_country:id_country}),
				dataType: "html",
				success: function(msg){
					popup.innerHTML = msg;
					popup.style.display = 'block';
					textfield.className = '';
					cnt__ = 0;
				}
			}
			).responseText;
		}
	}, 300);
}

$(function() {
	$('body').click(function() {
		$('.my_links').hide();
		$('.my_software').hide();
		$('.location-popup').hide();
		$('.popBox').hide();
		$(".boxlink").removeClass("boxactive");
	});

	$('.location-popup,.my_links,.my_software,.popBox,.boxlink,.showAgents').click(function(event){
		event.stopPropagation();
	});
	
});
