function g_http_obj() {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }            
    return xmlhttp;
}
   
function g_get_place_data(place_name) {
    xmlhttp = g_http_obj();
    xmlhttp.open('GET', 'ajax/check_place.php?place_name='+encodeURI(place_name), false);
    xmlhttp.send(null);        
    resp = xmlhttp.responseXML;
    if ( resp.getElementsByTagName("place_id").item(0).firstChild ) {
        place_id = resp.getElementsByTagName("place_id").item(0).firstChild.data;    
        area_id = resp.getElementsByTagName("area_id").item(0).firstChild.data;    
        region_id = resp.getElementsByTagName("region_id").item(0).firstChild.data;
    } else {
        place_id   = 0;
        region_id = 0;
        area_id   = 0;
    }
    return {"place_id" : place_id, "region_id" : region_id, "area_id" : area_id};
}  

function g_trim(str) {
    return str.replace(/^\s*|\s*$/g,"");
}         


function g_search_region(reg_id) {
    if (!reg_id) reg_id = document.forms[0].region_sel.value;
    if (reg_id == '0') {
        alert(js_texts['region_error']);
        document.forms[0].region_sel.focus();
        return;
    }
    document.forms[0].elements["g_object[region_id]"].value = reg_id;
    document.forms[0].elements["g_object[area_id]"].value = 0;
    document.forms[0].elements["g_object[place_id]"].value = 0;
    document.forms[0].action = "browse.php";
    document.forms[0].submit();
}

function g_search_place() {
    place_name = g_trim(document.forms[0].place_name.value);
    if (place_name.toLowerCase() == "zagreb") {
        g_browse_region(ZG_REGION_ID);
        return;   
    }
    place_arr = g_get_place_data(place_name);
    if ( place_arr["area_id"] > 0 ) {
        g_browse_place(place_arr["place_id"], place_arr["area_id"], place_arr["region_id"]);
    } else {
        alert(js_texts['city_error']);
        document.forms[0].elements["place_name"].blur();
    }
}

function g_browse_place(place_id, area_id, region_id) {
    document.forms[0].elements["g_object[place_id]"].value = place_id;
    document.forms[0].elements["g_object[area_id]"].value = area_id;
    document.forms[0].elements["g_object[region_id]"].value = region_id;
    document.forms[0].submit();    
}    

function g_browse_area(area_id, region_id) {
    document.forms[0].elements["g_object[place_id]"].value = "0";
    document.forms[0].elements["g_object[area_id]"].value = area_id;
    document.forms[0].elements["g_object[region_id]"].value = region_id;
    document.forms[0].submit();    
}    

function g_browse_region(region_id) {
    document.forms[0].elements["g_object[place_id]"].value = "0";
    document.forms[0].elements["g_object[area_id]"].value = "0";
    document.forms[0].elements["g_object[region_id]"].value = region_id;
    document.forms[0].submit();    
}    

function getEvent(event) {
   return (event ? event : window.event);
}

function handleKeyPress(event) {
    e = getEvent(event);
    kc = e["keyCode"];
    if (kc == 13) {
       if (document.forms[0].elements['place_name'].value.replace(/^\s*|\s*$/g,"") != '') {
           window.setTimeout('g_search_place()', 100);
           //g_search_place();
       }
    }
}

function open_window(url, height, width) {
    window.open(url, '', "toolbar=no,status=1,resizable=yes,scrollbars=yes,height="+height+",width="+width);
}

if (document.addEventListener) { 
    document.forms[0].elements["place_name"].addEventListener("keyup", handleKeyPress, false);
} else {
    document.forms[0].elements["place_name"].onkeyup = handleKeyPress;   
}