var prevText = "";
var list = null; 
var list_id = "cityList";
var dvCityList = null;
var dvCityList_id = "dvCityList";
var tbCityName = null;
var tbCityName_id = "tbCityName";
var tbCityValue = null;
var tbCityValue_id = "tbCityID";

function init() {
    if (list == null) {
        list = document.getElementById(list_id);
    }
    if (dvCityList == null) {
        dvCityList = document.getElementById(dvCityList_id);
    }
    if (tbCityName == null) {
        tbCityName = document.getElementById(tbCityName_id);
    }
    if (tbCityValue == null) {
        tbCityValue = document.getElementById(tbCityValue_id);
    }
}

function handleInput(e) {
    init();
    e = e ? e : window.event;
    var charCode = e.charCode ? e.charCode : e.keyCode;
    if (charCode == 40 && dvCityList.style.display != 'none') { //KEY_DOWN
        list.selectedIndex = 0;
        list.focus();
        if (list.options.length > 0){
	        tbCityValue.value = list.options[list.selectedIndex].value;
	        tbCityName.value = list.options[list.selectedIndex].text;
        }
    } else if (charCode == 13 && dvCityList.style.display != "none") {
        dvCityList.style.display = "none";
    } else if ( (charCode == 40 || prevText != tbCityName.value) && tbCityName.value.length > 0) {
        prevText = tbCityName.value;
        /* посылаем запрос на сервер */
        //dvCityList.style.display = "none";
        url = "ajaxHandler.aspx?action=get_citys&city=" + escape(tbCityName.value);
        new Ajax.Request(url, {	
                                method: "get",
                                onSuccess: function(transport) {
                                    var len = list.options.length;
                                    for(var i = 0; i < len; i++)
                                        list.remove(0);
                                    var response = transport.responseText;
                                    if (response.length > 0) {
                                        var items = response.split(";");
                                        for (var i = 0; i < items.length; i++) {
                                            if (items[i].length > 0) {
                                                var item = items[i].split("|");
                                                if (item.length == 2) {
                                                    list.options[list.options.length] = new Option(item[1], item[0]);
                                                }
                                            }
                                        }
                                        dvCityList.style.display = "block";
                                    }
                                    
                                    return;
                                }
                              }
                         );
        
    } else if (charCode == 27 || tbCityName.value.length == 0) {
        dvCityList.style.display = 'none';
    }
    e.cancelBubble = true;
    e.returnValue = false;
    if (e.stopPropogation) e.stopPropogation();
    if (e.preventDefault) e.preventDefault();
    return false;
}

function handleListClick(e) {
    init();
    try {
        tbCityValue.value = list.options[list.selectedIndex].value;
	    tbCityName.value = list.options[list.selectedIndex].text;
        tbCityName.focus();
        dvCityList.style.display = "none";
    } catch(ex) { }
}

function handleListChange(e) {
    init();
    tbCityValue.value = list.options[list.selectedIndex].value;
	tbCityName.value = list.options[list.selectedIndex].text;
}

function handleListInput(e) {
    init();
    e = e ? e : window.event;
    var charCode = e.charCode ? e.charCode : e.keyCode;
    if (list.selectedIndex == 0 && charCode == 38) {
        tbCityName.focus();
    } else if (charCode == 13) {
        tbCityValue.value = list.options[list.selectedIndex].value;
        tbCityName.value = list.options[list.selectedIndex].text;
        tbCityName.focus();
        dvCityList.style.display = "none";
    } else if (charCode == 27) {
        tbCityValue.value = "0";
        tbCityName.value = prevText;
        tbCityName.focus();
        dvCityList.style.display = "none";
    }
}

function handleBlur(e) {
	//dvCityList.style.display = "none";
}

function sizeWin(){				
	if (document.body.clientWidth <= '1016') {
		obj = document.getElementById('inner_center');
		obj.style.width = '500px';
		obj = document.getElementById('if');
		obj.style.width = '500px';					
		obj = document.getElementById('inner_content');
		obj.style.width = '1016px';
	} else {
		obj = document.getElementById('inner_center');
		obj.style.width = document.body.clientWidth - 515 +'px';
		obj = document.getElementById('if');
		obj.style.width = document.body.clientWidth - 515 +'px';
		obj = document.getElementById('inner_content');
		obj.style.width = 'auto';				
	}
}

function showMenu(obj) {
	obj = document.getElementById(obj);		
	obj.style.display = '';			
}
function hideMenu(obj) {
	obj = document.getElementById(obj);		
	obj.style.display = 'none';			
}

