function loadCountries(el){
		if(el.value!=''){
			var req = new XMLHttpRequest();
			var url = "loadTowns.php";
			var requestString = "id="+el.value;
			req.onreadystatechange = function()
			{ hndTowns(req)};
			req.open('POST', url, true);
			req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			req.send(requestString);
		}
	}
	
	function hndTowns(req) {
	    if (req.readyState == 4){
			if (req.status == 200) {
				var td = document.getElementById("towns");
				while(td.hasChildNodes()){
					td.removeChild(td.firstChild);
				}
				var td2 = document.getElementById("places");
				while(td2.hasChildNodes()){
					td2.removeChild(td2.firstChild);
				}
				var sel = document.createElement("select");
				td.appendChild(sel);
				sel.name="townID";
				sel.appendChild(document.createElement("option"));
				sel.onchange=function()
				{loadPlaces(this)};
				var objects = eval(req.responseText);
				for(var msg in objects){
					if(typeof objects[msg].townID !='undefined' && objects[msg].townID){
						var el = document.createElement("option");
						el.value=objects[msg].townID;
						el.appendChild(document.createTextNode(objects[msg].townName));
						sel.appendChild(el);
					}
				}
			}
	    }
	}
	
	function loadPlaces(el){
		var c=document.getElementById("countryID");
		if(el.value!='' && c.value!=''){
			var req = new XMLHttpRequest();
			var url = "loadPlaces.php";
			var requestString = "tid="+el.value+"&cid="+c.value;
			req.onreadystatechange = function()
			{ hndPlaces(req)};
			req.open('POST', url, true);
			req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			req.send(requestString);
		}
	}
	
	function hndPlaces(req) {
	    if (req.readyState == 4){
			if (req.status == 200) {
				var td = document.getElementById("places");
				while(td.hasChildNodes()){
					td.removeChild(td.firstChild);
				}
				var objects = eval(req.responseText);
				for(var msg in objects){
					if(typeof objects[msg].placeID !='undefined' && objects[msg].placeID){
						var el = document.createElement("input");
						el.type="radio";
						el.name="placeID";
						el.value=objects[msg].placeID;
						//el.appendChild();
						td.appendChild(el);
						td.appendChild(document.createTextNode(objects[msg].placename));
						td.appendChild(document.createElement("br"));
					}
				}
				var i2=document.createElement("input");
				td.appendChild(i2);
				i2.type="text";
				i2.name="new_place";
				//i2.onmousedown=function(){};
			}
	    }
	}
	
  function loadTownList(el){
		if(el.value!=''){
			var req = new XMLHttpRequest();
			var url = "loadTowns.php";
			var requestString = "id="+el.value;
			req.onreadystatechange = function()
			{ hndTownList(req)};
			req.open('POST', url, true);
			req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			req.send(requestString);
		}
  }

  function loadPlaceList(value){
  	if(value!=""){
  		var el = document.getElementById("places");
  		el.style.display="none";
      var req = new XMLHttpRequest();
      var url = "loadPlaceList.php";
      var requestString = "townID="+value;
      req.onreadystatechange = function()
      { hndPlaceList(req)};
      req.open('POST', url, true);
      req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      req.send(requestString);
    }
  }

  function loadPlaceListWithType(value){
  	var el = document.getElementById("places");
  	el.style.display="none";
  	var el=document.getElementById("townID");
  	var val2;
  	if(el)
  		val2=el.value;
  	if(value!="" && val2!=""){
      var req = new XMLHttpRequest();
      var url = "loadPlaceListWithType.php";
      var requestString = "townID="+val2+"&placetypeID="+value;
      req.onreadystatechange = function()
      { hndPlaceList(req)};
      req.open('POST', url, true);
      req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      req.send(requestString);
    }
  }

  function hndTownList(req) {
    if (req.readyState == 4){
			if (req.status == 200) {
				var td = document.getElementById("towns");
				while(td.hasChildNodes()){
					td.removeChild(td.firstChild);
				}
				var sel = document.createElement("select");
				td.appendChild(document.createElement("option"));
				var objects = eval(req.responseText);
				for(var msg in objects){
					if(typeof objects[msg].townID !='undefined' && objects[msg].townID){
						var el = document.createElement("option");
						el.value=objects[msg].townID;
						el.appendChild(document.createTextNode(objects[msg].townName));
						td.appendChild(el);
					}
				}
			}
	    }
  }

  function hndPlaceList(req) {
    if (req.readyState == 4)
    {

      if (req.status == 200) {
        var el = document.getElementById("places");
        if(el){
         	el.innerHTML=req.responseText;
         	el.style.display="inline";
       }
        return;
      } else {
        alert('There was a problem with the URL.');
      }
    }
  }

  
	 function sendRate(sel,id){
    if(sel!="" && id!=""){
      var req = new XMLHttpRequest();
      var url = "rate.php";
      var requestString = "rate="+sel+"&id="+id;
      req.onreadystatechange = function()
      { hndRate(req)};
      req.open('POST', url, true);
      req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      req.send(requestString);
    }
  }

	function hndRate(req) {
    if (req.readyState == 4)
    {

      if (req.status == 200) {
      	eval("var result = ("+req.responseText+")");
      	document.getElementById("rate").innerHTML=result[1];
      	document.getElementById("count").innerHTML=result[0];
        return;
      } else {
        alert('There was a problem with the URL.');
      }
    }
  }
