// common.js

function searchCities(value){
    icity = document.getElementById('city');

    if(value != ""){
        var ajax=nuevoAjax();

        ajax.open("GET", "request.php?action=cities&cid="+value, true);
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send();

        ajax.onreadystatechange = function(){
            if (ajax.readyState==4){
                var resp = ajax.responseText;

                var respObj = eval('(' + resp + ')');

                if(respObj.status == "OK"){
                    var cities = "";
                    
                    for(i = 0; i < respObj.response.length; i++){
                        cities += "<option value=\""+respObj.response[i].id+"\">"+respObj.response[i].name+"</option>";
                    }

                    icity.innerHTML = cities;
                    icity.disabled = false;
                } else {
                    icity.innerHTML = "<option value=\"\"> Choose country </option>";
                    icity.disabled = true;
                }
            }
        }
    } else {
        icity.innerHTML = "<option value=\"\"> Choose country </option>";
        icity.disabled = true;
    }
}

function nuevoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		} catch(E) {
                    xmlhttp=false;
                }
	}

	if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
            xmlhttp=new XMLHttpRequest();
        }

	return xmlhttp;
}
