function lsShowHide(div_id, no_load){
  var mainDIV   = document.getElementById(div_id);

  if (!mainDIV._cache){
		mainDIV._cache = new Array();
		mainDIV._cache['sub_list'] = new Array();
		mainDIV._cache['this_list'] = new Array();
  }

  mainDIV.search_first_count = 15;

  pdShowHide(div_id);

  if (mainDIV.style.display == 'none')
    return;

  if (!mainDIV.onChangeSelection)
    mainDIV.onChangeSelection = '';

  if (!mainDIV.dsSelect){
    mainDIV.dsSelect = new dsDivSelect(div_id, div_id+'_text');
    mainDIV.dsSelect.nobr = 1;
    mainDIV.dsSelect.class_sub_names = new Array('el_name', 'el_link');
    mainDIV.dsSelect.onSelect    = new Function("lsOnSelect('"+div_id+"')");
    mainDIV.dsSelect.onDblClick  = new Function("lsOnDblClick('"+div_id+"')");
  }

  var selected_id = 0;

  if (!no_load){
	  pdSetText(div_id, '');
	  if (!selected_id){
	    lsLoad(div_id, 'sub_list', 0, '', '');
	  }else{
			lsLoad(div_id, 'this_list', selected_id, '', '');
	  }
  }

}


function lsAddButtonClick(div_id){
  if (document.getElementById(div_id+'_add_button').style.cursor != 'pointer')
    return;
  var mainDIV   = document.getElementById(div_id);
  mainDIV.dsSelect.up();
}

function lsEditButtonClick(div_id){
   if (document.getElementById(div_id+'_edit_button').style.cursor != 'pointer')
     return;
   var mainDIV   = document.getElementById(div_id);
   mainDIV.dsSelect.down();
}

function lsOkButtonClick(div_id){

  if (document.getElementById(div_id+'_ok_button').disabled)
     return;
  var mainDIV   = document.getElementById(div_id);
  var selected_id = mainDIV.dsSelect.selected_id;

  if (document.getElementById(mainDIV.ls_id).value != selected_id){
		document.getElementById(mainDIV.ls_id).value = selected_id;
		var has_changed = 1;
  }

  var selected_record = lsGetRecord(div_id, selected_id);
  if (!selected_record){
		var cur_city_path = mainDIV.cur_city_path;
		var str_name = '';
	  var i, first=1;
	  if (cur_city_path){
		  for (i=cur_city_path.length-1;i>=0;i--){
  		  if (cur_city_path[i]){
	  		  if (first){
				  	first = 0;
				  	selected_id = cur_city_path[i]['id_city'];
				  }else
					  str_name += ', ';
				  str_name += cur_city_path[i]['name'];
			  }
			}
		  if (document.getElementById(mainDIV.ls_id).value != selected_id){
				document.getElementById(mainDIV.ls_id).value = selected_id;
				var has_changed = 1;
		  }
		}
  }else{
		var str_name = selected_record['name'];
		var cur_city_path = mainDIV.cur_city_path;
		var i, first=1;
		if (cur_city_path){
			for (i=cur_city_path.length-1;i>=0;i--){
			  if (cur_city_path[i]){
	  		  if (first){
				  	str_name += ', ';
				  	first = 0;
				  }

				  str_name += cur_city_path[i]['name'];
	       	if (i)
			  		str_name += ', ';
			  }
			}
		}
	}

	document.getElementById(mainDIV.id_input).value = str_name;
	moveCaretToStart(document.getElementById(mainDIV.id_input));



	pdHide(div_id);
  if (mainDIV.ls_required){
  	document.getElementById(mainDIV.id_input).style.border = 'solid ' +mainDIV.ls_default_color+ ' 1px';
  }
  document.getElementById(mainDIV.id_input).style.color = document.getElementById(mainDIV.id_input).default_color;

	mainDIV.selected_id=selected_id;
	document.getElementById(mainDIV.id_input).start_typing = false;

	if (has_changed && mainDIV.onChangeSelection)
		mainDIV.onChangeSelection(div_id);
}

function lsResetButtonClick(div_id, no_hide){
  var mainDIV   = document.getElementById(div_id);
  if (mainDIV.dsSelect)
	  var selected_id = mainDIV.dsSelect.selected_id;
	if (mainDIV.ls_required){
  	document.getElementById(mainDIV.id_input).style.border = 'solid red 1px';
  }
  document.getElementById(mainDIV.ls_id).value = '';
  if (selected_id){
		mainDIV.dsSelect.Reset();
  }
  document.getElementById(mainDIV.id_input).value = mainDIV.start_typing;
  document.getElementById(mainDIV.id_input).style.color = document.getElementById(mainDIV.id_input).gray_color;
  document.getElementById(mainDIV.id_input).start_typing =1;

	  pdHide(div_id);

	if (mainDIV.onChangeSelection){
		mainDIV.onChangeSelection(div_id);
	}
}

function lsTranslateButtonClick(div_id){
  if (document.getElementById(div_id+'_translate_button').style.cursor != 'pointer')
     return;
  alert('TranslateButtonClick');
}

function lsSetButtonActive(div_id, type, active){
  var obj = document.getElementById(div_id+'_'+type+'_button');

  if (type=='ok'){
	  if (active){
	    obj.disabled = false;
	  }else{
	    obj.disabled = true;
	  }
	  return;
  }


  if (active){
    obj.style.cursor = 'pointer';
    obj.src          = portal_url+'img/city/'+type+'-button-active.gif';
  }else{
    obj.style.cursor = 'default';
    obj.src          = portal_url+'img/city/'+type+'-button-inactive.gif';
  }
}

function lsSetButtonPressed(div_id, type, pressed){
  var img = document.getElementById(div_id+'_'+type+'_button');
  if (img.style.cursor != 'pointer')
     return;
  if (pressed)
    img.src          = portal_url+'img/city/'+type+'-button-pressed.gif';
  else{
    if (img.src != portal_url+'img/city/'+type+'-button-active.gif'){
      img.src          = portal_url+'img/city/'+type+'-button-active.gif';
    }
  }
}

function lsSetButtonVisibility(div_id, type, visible){
  var set_visibility = visible ? 'visible' : 'hidden';
  var img = document.getElementById(div_id+'_'+type+'_button');
  if (img.style.visibility == set_visibility)
     return;
  img.style.visibility = set_visibility;
}


function lsLoad(div_id, load_type, parent_id, psearch, limit){
	var mainDIV   = document.getElementById(div_id);
if(!limit)
    limit=0;
  if(!psearch)
  {
    if(mainDIV._cache[load_type][parent_id])
    {
      // TODO: Alex Krass use cache;
      lsArrayLoaded(div_id, mainDIV._cache[load_type][parent_id]);
      return;
    }
  }
  //var URL="/ajax.php?Action=cityUploadByParent&load_type="+load_type+"&div_id="+div_id+"&id_parent="+parent_id;
  var URL="/ajax2/?Action=get_cities&load_type="+load_type+"&div_id="+div_id+"&id_parent="+parent_id;
  //var URL="/ajax.php?Action=get_cities&load_type="+load_type+"&div_id="+div_id+"&id_parent="+parent_id;
  if (psearch)
	  URL+="&psearch="+escape(psearch);
	  
  URL+="&limit="+limit;
  pdSetAjaxIndicator(div_id, 1);
  //alert(URL);
  //document.write(URL);
  LoadData(URL,'lsLoadProcessing','lsData');

}

function lsLoadProcessing()
{
  var Response = GetXMLResponse('lsData');
  if (Response === false) return false;
  if (Response === '') return false;

  var LO = Response.getElementsByTagName('Cities');
  if (!LO || LO.length==0)
  {
    UnsetRequest('lsData');
    return false;
  }
 
  LO=LO[0];
//alert(LO.childNodes.length); 
  var div_id=LO.getAttribute('div_id');
  var parent_id=LO.getAttribute('parent_id');
  var psearch=LO.getAttribute('psearch');
  var load_type = LO.getAttribute('load_type');
  var r_count = LO.getAttribute('r_count');
  var mainDIV   = document.getElementById(div_id);
  var limit = LO.getAttribute('limit');
  mainDIV.search_over_flow = false;
  if (psearch && limit && parseInt(limit) < parseInt(r_count))
  	mainDIV.search_over_flow = true;

  var is_path=0;
  var result = new Array();
  if (psearch && !LO.childNodes.length){
  	if (mainDIV.dsSelect)
	  	mainDIV.dsSelect.Reset();
  	pdSetText(div_id, mainDIV.ls_not_found);
  	UnsetRequest('lsData');
  	pdSetAjaxIndicator(div_id, 0);
  	lsSetButtonActive(div_id, 'ok', false);
  	lsSetTopSearchResult(div_id, 0);
  	return;
  }
  
  for(i=0;i<LO.childNodes.length;i++)
  {
    var item = LO.childNodes[i];
    result[i] = new Array();
    result[i]['id_city']=item.getAttribute('id_city');
    //result[i]['name']=item.getAttribute('name').replace("\\'", "'");
    result[i]['name']=item.getAttribute('name');
    result[i]['name']=result[i]['name'].replace("\\'", "'");
    result[i]['name']=result[i]['name'].replace("\\'", "'");
    if (psearch)
    	result[i]['add_info']=item.getAttribute('add_info');
  }
  pdSetAjaxIndicator(div_id, 0);
  UnsetRequest('lsData');
  if(!psearch){
    mainDIV._cache[load_type][parent_id]=result;
  }
  if (psearch)
	mainDIV.last_search_word = unescape(psearch);
  lsArrayLoaded(div_id, result, !!psearch, r_count, parent_id);
}




function lsArrayLoaded(div_id, city_arr, is_search, r_count, parent_id){
  if(!city_arr.length)
    return;

  var i,j=0;
  var mainDIV   = document.getElementById(div_id);
  var res=new Array();
  var resL=new Array();
  var cur_city_path=new Array();
  var ls_open=mainDIV.ls_open;

  var str = '';
  for(i=0;i<city_arr.length;i++){
  	if(!city_arr[i]['is_path']){
  		if (is_search && city_arr[i]['add_info'])
	  		city_arr[i]['name'] += ', ' + city_arr[i]['add_info'];

		if (city_arr[i]['sub_exists'] == '1'){
  			res[city_arr[i]['id_city']] = '<table cellspacing="0" cellpadding="0" border="0" ><tr><td width="100%" el_name>'+ city_arr[i]['name'] + '</td><td align="right"><a href="#" onClick="lsLoadList(' + "'" + div_id + "', '" + city_arr[i]['id_city'] + "'" + '); return false;" el_link>'+ls_open+'</a><td></tr></table>';
  		}else{
  			res[city_arr[i]['id_city']]=city_arr[i]['name']+'<br />';
  		}
      str += ','+'|'+city_arr[i]['id_city']+'|'+city_arr[i]['name'];
  		resL[city_arr[i]['id_city']] = city_arr[i];
  	}else{
  		cur_city_path[j++] = city_arr[i];
  	}
  }

  mainDIV.city_arr = resL;
  mainDIV.cur_city_path = null;

  if (cur_city_path.length){
  	mainDIV.cur_city_path = cur_city_path;
  }

  if(!res.length && !is_search){
		if (mainDIV.cur_city_path.length){
			mainDIV.cur_city_path[mainDIV.cur_city_path.length-1] = null;
		}
		lsSetTop(div_id);
    return;
  }

  if (!is_search)
	  lsSetTop(div_id);

  if(is_search){
		pdSetText(div_id, '');
		lsSetTopSearchResult(div_id, r_count);
  }

  if (is_search && r_count){
		if (r_count>mainDIV.search_first_count){
			mainDIV.dsSelect.Reset();

			if (mainDIV.search_over_flow){
				var text = mainDIV.ls_first;
				text = text.replace('<count>', mainDIV.search_first_count);
				text = text.replace('<all>', '<a href="#" onClick="lsSearchFullList(' + "'" + div_id + "'" + '); return false;">'+mainDIV.ls_show_all+'</a>');
				text = '<table cellspacing="0" cellpadding="0" border="0" ><tr><td width="100%">&nbsp;</td><td align="right" nowrap class="ls_small">'+text+'<td></tr></table>';
				pdSetText(div_id, text);
			}
		}
  }


  if(!is_search && mainDIV.cur_city_path){
    if (mainDIV.cur_city_path.length>0)
      var parent_id = mainDIV.cur_city_path[mainDIV.cur_city_path.length-1].id_city;
    else
    	parent_id = 0;
    if (mainDIV.cur_city_path.length>1){
	    var upper_row = mainDIV.cur_city_path[mainDIV.cur_city_path.length-2];
			pdSetText(div_id, '<a href="#" onClick="lsLoadList(' + "'" + div_id + "', '" + upper_row['id_city'] + "', '" + parent_id + "'"+'); return false;">'+mainDIV.ls_upper+'</a>');
    }else{
			pdSetText(div_id, '<a href="#" onClick="lsLoadList(' + "'" + div_id + "', '" + 0 + "', '" +  parent_id + "'"+'); return false;">'+mainDIV.ls_upper+'</a>');
    }
  }





  mainDIV.dsSelect.Load(res);

  var add_text='';
  if (is_search && r_count && mainDIV.search_over_flow){
  	add_text = '<br /><br /><span class="ls_small">'+mainDIV.ls_continue+'</span>';
  }

  mainDIV.is_search = is_search;

  mainDIV.dsSelect.Show(0, add_text);

  if (mainDIV.selected_id){
	  mainDIV.dsSelect.Select(mainDIV.selected_id);
  }
}

function lsSearchFullList(div_id){
	var mainDIV   = document.getElementById(div_id);
	lsLoad(div_id, 'sub_list', 0, mainDIV.last_search_word);
}

function lsOnSelect(div_id){
  var mainDIV   = document.getElementById(div_id);
  var selected_name = mainDIV.city_arr[mainDIV.dsSelect.selected_id]['name'];
  lsSetButtonActive(div_id, 'ok', true);
  setTimeout("lsFocusInput('"+mainDIV.id_input+"')", 100);
  if (!mainDIV.is_search)
	  lsSetTop(div_id, 0, selected_name);
}

function lsFocusInput(id_input){
	var input = document.getElementById(id_input);
	if (input.style.visibility != 'hidden')
		document.getElementById(id_input).focus();
}

function lsSetTop(div_id, no_path, selected_name){
  var mainDIV   = document.getElementById(div_id);
  var s;

  var cur_city_path = mainDIV.cur_city_path;
/*  if (cur_city_path || selected_name)
	  if (cur_city_path)
		  s = '<a href="#" onClick="lsTopListButton(' + "'" + div_id + "'" + '); return false;">'+mainDIV.ls_start+'</a>';
		else
			s = '<a href="#" onClick="lsResetSelection(' + "'" + div_id + "'" + '); return false;">'+mainDIV.ls_start+'</a>';
	else
		s = '<b>'+mainDIV.ls_start+'</b>';*/
s='';



  if (!cur_city_path || no_path){
		if (selected_name){
			s += '&nbsp;&raquo;&nbsp;<b>'+selected_name+'</b>';
		}

    pdSetTopText(div_id, s);
    return;
  }


  for (var i=0;i<cur_city_path.length;i++){
	  if (!cur_city_path[i]){
	    continue;
	  }
	  if (i == cur_city_path.length-1){
	  	if (selected_name)
		  	s += '&nbsp;&raquo;&nbsp;<a href="#" onClick="lsResetSelection(' + "'" + div_id + "', '" + cur_city_path[i]['id_city'] + "'" + '); return false;">'+cur_city_path[i]['name']+'</a>';
		  else
			  s += '&nbsp;&raquo;&nbsp;<b>'+cur_city_path[i]['name']+'</b>';
	  }else
	  	s += '&nbsp;&raquo;&nbsp;<a href="#" onClick="lsLoadList(' + "'" + div_id + "', '" + cur_city_path[i]['id_city'] + "'" + '); return false;">'+cur_city_path[i]['name']+'</a>';
	}
	if (selected_name){
		s += '&nbsp;&raquo;&nbsp;<b>'+selected_name+'</b>';
	}
	pdSetTopText(div_id, s);

}

function lsGetRecord(div_id, id){
  var mainDIV   = document.getElementById(div_id);
  return mainDIV.city_arr[id];
}

function lsGetParam(div_id, id, name){
  var mainDIV   = document.getElementById(div_id);
  return mainDIV.city_arr[id][name];
}

function lsOnDblClick(div_id){
  var mainDIV   = document.getElementById(div_id);
  var sub_exists = lsGetParam(div_id, mainDIV.dsSelect.selected_id, 'sub_exists')
  if (sub_exists == '1')
	  lsLoad(div_id, 'sub_list', mainDIV.dsSelect.selected_id, '', '');
	else
		lsOkButtonClick(div_id);
}

function lsTopListButton(div_id){
	var mainDIV   = document.getElementById(div_id);
	pdSetText(div_id, '');
	lsLoad(div_id, 'sub_list', 0, '', '');
}

function lsLoadList(div_id, id_city, set_id_city){
	var mainDIV   = document.getElementById(div_id);
	mainDIV.selected_id = set_id_city;
	pdSetText(div_id,'');
	lsLoad(div_id, 'sub_list', id_city, '', '');
}

function lsOnKeyPress(e_event, div_id){

	if (!e_event && window.event)
		e_event = window.event;

	if (e_event.keyCode == 27){
		pdHide(div_id);
		return;
	}

	if (e_event.keyCode != 13)
		return;

	var mainDIV   = document.getElementById(div_id);
	var input_el  = document.getElementById(mainDIV.id_input)

  lsOkButtonClick(div_id);
  return false;


}

function lsOnKeyDown(e_event, div_id){

	if (!e_event && window.event)
		e_event = window.event;

	if (e_event.keyCode == 13)
		return;

	var mainDIV   = document.getElementById(div_id);
	var input_el  = document.getElementById(mainDIV.id_input)

  if (e_event.keyCode == 38 || e_event.keyCode == 40){
  	if (mainDIV.style.display == 'none'){
  		lsShowHide(div_id);
  	}
  	if (!mainDIV.dsSelect.elements_arr)
  	  return;
  }
  if (e_event.keyCode == 40){
		mainDIV.dsSelect.down();
		mainDIV.repeat_flag = e_event.keyCode;
		setTimeout("lsRepeatKey('"+div_id+"', "+e_event.keyCode+")", 500);
		return false;
  }

  if (e_event.keyCode == 38){
		mainDIV.dsSelect.up();
		mainDIV.repeat_flag = e_event.keyCode;
		setTimeout("lsRepeatKey('"+div_id+"', "+e_event.keyCode+")", 500);
		return false;
  }

	if (e_event.keyCode == 37 ||
			e_event.keyCode == 39 ||
			e_event.keyCode == 10 ||
			e_event.keyCode == 11 ||
			e_event.keyCode == 12 ||
			e_event.keyCode == 33 ||
			e_event.keyCode == 34 ||
			e_event.keyCode == 35 ||
			e_event.keyCode == 36 ||
			e_event.keyCode == 45 ||
			e_event.keyCode == 9
			){
		return;
	}

  if (e_event.keyCode == 27){
  	if (mainDIV.last_input_value){
  		input_el.value = mainDIV.last_input_value;
  		input_el.focus();
  		moveCaretToStart(input_el);
  	}else
  		input_el = '';
  	mainDIV.isWriting = 0;
  	return;
  }

  if (!mainDIV.isWriting && input_el.value){
		mainDIV.last_input_value = input_el.value;
		mainDIV.isWriting = 1;
  }
  if (!input_el.value_version)
	  input_el.value_version = 0;
	input_el.value_version++;

  setTimeout("lsTrySearch('"+div_id+"', '"+input_el.value_version+"')", 10);
}

function lsRepeatKey(div_id, key_code){
	var mainDIV   = document.getElementById(div_id);
	if (!mainDIV.repeat_flag || mainDIV.repeat_flag != key_code)
		return;

	if (key_code == 40 && mainDIV.repeat_flag == key_code){
		mainDIV.dsSelect.down();
  }

  if (key_code == 38 && mainDIV.repeat_flag == key_code){
		mainDIV.dsSelect.up();
  }
	setTimeout("lsRepeatKey('"+div_id+"', "+key_code+")", 500);
}

function lsOnKeyUp(e_event, div_id){
	var mainDIV   = document.getElementById(div_id);
	var input_el  = document.getElementById(mainDIV.id_input)
	if (!e_event && window.event)
		e_event = window.event;

	if (e_event.keyCode == 38 || e_event.keyCode == 40){
		mainDIV.repeat_flag = false;
	}
}

function lsOnChange(div_id){
 	var mainDIV   = document.getElementById(div_id);
	var input_el  = document.getElementById(mainDIV.id_input);


	if (!input_el.value)
		lsResetButtonClick(div_id, 1);
	return false;
}

function lsTrySearch(div_id, value_version){
 	var mainDIV   = document.getElementById(div_id);
	var input_el  = document.getElementById(mainDIV.id_input)
  if (input_el.value_version == value_version && input_el.value.length > 2 && !input_el.start_typing){
  	if (mainDIV.style.display == 'none')
  		lsShowHide(div_id, 1);
  	mainDIV.dsSelect.Reset();
  	pdSetText(div_id, mainDIV.ls_searching);
  	lsLoad(div_id, 'sub_list', 0, input_el.value, mainDIV.search_first_count);
  }
}

function lsResetSelection(div_id, id_city){
	var mainDIV   = document.getElementById(div_id);
	mainDIV.dsSelect.ResetSelection();
  if (id_city){
  	mainDIV.dsSelect.selected_id = id_city;
  	mainDIV.selected_id = id_city;
  }
	lsSetTop(div_id, 0);
}

function  lsOnBlur(div_id){
	var mainDIV   = document.getElementById(div_id);
	if (document.getElementById(div_id).style.display=='none' && !document.getElementById(mainDIV.ls_id).value){
	  document.getElementById(mainDIV.id_input).value = mainDIV.start_typing;
	  document.getElementById(mainDIV.id_input).style.color = document.getElementById(mainDIV.id_input).gray_color;
	  document.getElementById(mainDIV.id_input).start_typing =1;
	}
}


function lsSetTopSearchResult(div_id, found){
	var mainDIV   = document.getElementById(div_id);
	var top_text = '';//'<a href="#" onClick="lsTopListButton(' + "'" + div_id + "'" + '); return false;">'+mainDIV.ls_start+'</a>';
	var top_text2 = '&nbsp;&nbsp;&nbsp;&nbsp;';//<b>'+ mainDIV.ls_found + '</b>&nbsp;'+ found;
	top_text = '<table cellspacing="0" cellpadding="0" border="0" ><tr><td width="100%">'+top_text2+'</td><td align="right" nowrap>'+top_text+'<td></tr></table>';
  pdSetTopText(div_id, top_text);
}

function lsCancelButtonClick(div_id){
	pdHide(div_id);
}





