// declare new variables for each new div that you add, and link to the div by ID
// var region_div = document.getElementById("region_div");
var doc = null;

function ajax() {
	// Make a new XMLHttp object
	if (typeof window.ActiveXObject != 'undefined' ) doc = new ActiveXObject("Microsoft.XMLHTTP");
	else doc = new XMLHttpRequest();
}

function SelectCountry(section, destination){
    	ajax();
		// Load the result from the response page
		// ** As far a I know firefox will only load a document on the SAME domain!!	
	    if (doc){
	       destination.innerHTML = "Loading data...";
	       doc.open("GET", "location.php?sec=" + section + "&sel=country", false);
	       doc.send(null);
	    	// Write the response to the div	    	
	       destination.innerHTML = doc.responseText;
	    }
	    else{
	      
	       destination.innerHTML = 'Browser unable to create XMLHttp Object';
	    }        
}

function SelectRegion(section, id_country, destination, destination2){
    if (id_country != '') {
		ajax();
		// Load the result from the response page
		// ** As far a I know firefox will only load a document on the SAME domain!!	
	    if (doc){
	       destination.innerHTML = "Loading data...";
	       doc.open("GET", "location.php?sec=" + section + "&sel=region&id_country=" + id_country, false);
	       doc.send(null);
	    	// Write the response to the div	    	
	       destination.innerHTML = doc.responseText;
	    }
	    else{
	      
	       destination.innerHTML = 'Browser unable to create XMLHttp Object';
	    }        
    }
    else {
    	destination.innerHTML = "Country is not selected";
    }
    destination2.innerHTML = "";
}

function SelectCity(section, id_region, id_country , destination){
    if (id_region != '') {
		// alert(id_country);
		ajax();
		// Load the result from the response page
		// ** As far a I know firefox will only load a document on the SAME domain!!	
	    if (doc){
	       destination.innerHTML = "Loading data...";	
	       doc.open("GET", "location.php?sec=" + section + "&sel=city&id_region=" + id_region + "&id_country=" + id_country, false);
	       doc.send(null);
	    	// Write the response to the div	    	
	       destination.innerHTML = doc.responseText;
	    }
	    else{
	      
	       destination.innerHTML = 'Browser unable to create XMLHttp Object';
	    }        
    }
    else {
    	destination.innerHTML = "Region is not selected";
    }
	    
}

function SelectLocation(section,id_city){
	if (id_city != '') {
		ajax();
		
		// Load the result from the response page
		// ** As far a I know firefox will only load a document on the SAME domain!!	
		if (doc){
			doc.open("GET", "location.php?sec=" + section + "&sel=location&id_city=" + id_city, false);
			doc.send(null);
			// getting XML data
			var xmldoc=doc.responseXML;
			var lat_grad=xmldoc.getElementsByTagName('latgrad').item(0).firstChild.nodeValue;
			var lat_min=xmldoc.getElementsByTagName('latmin').item(0).firstChild.nodeValue;
			var lon_grad=xmldoc.getElementsByTagName('longrad').item(0).firstChild.nodeValue;
			var lon_min=xmldoc.getElementsByTagName('lonmin').item(0).firstChild.nodeValue;
			if(lat_grad<0 || lat_min<0){
				document.getElementById('latitude_side_2').checked=true;
				lat_grad=Math.abs(lat_grad);
				lat_min=Math.abs(lat_min);
			}
			else {
				document.getElementById('latitude_side_1').checked=true;
			}
			if(lon_grad<0 || lon_min<0){
				document.getElementById('longitude_side_2').checked=true;
				lon_grad=Math.abs(lon_grad);
				lon_min=Math.abs(lon_min);

			}
			else {
				document.getElementById('longitude_side_1').checked=true;
			}
			
			document.getElementById('latitude_hours').value=lat_grad;
			document.getElementById('latitude_minutes').value=lat_min;
			document.getElementById('longitude_hours').value=lon_grad;
			document.getElementById('longitude_minutes').value=lon_min;
		}
		else{
			alert('Browser unable to create XMLHttp Object');
		}
	}
	else {
		alert("City is not selected");
	}
}

function CheckLogin(section, login, destination){
    if (login != '') {
		ajax();
		// Load the result from the response page
		// ** As far a I know firefox will only load a document on the SAME domain!!	
	    if (doc){
	       destination.innerHTML = "Loading data...";	
	       doc.open("GET", "location.php?sec=" + section + "&sel=login&login=" + login, false);
	       doc.send(null);
	    	// Write the response to the div	    	
	       destination.innerHTML = doc.responseText;
	    }
	    else{
	      
	       destination.innerHTML = 'Browser unable to create XMLHttp Object';
	    }        
    }
    else {
    	destination.innerHTML = "Nick is empty";
    }

}


