//*INSTAL**************************************************************************************/
//      <script src="/JSHttpRequest.js" type="text/javascript"></script>
//      <script src="/ListControl.js" type="text/javascript"></script>
//      <script type="text/javascript" language="javascript">
//              window.onload = function()
//              {
//                      ListControl('city', 'city', 'addr');
//                      debug('onload');
//              }
//      </script>
//*********************************************************************************************/
function hideElement(subMenu)
{
  var selx,sely,selw,selh,i,x,y,w,h;
  x=0;y=0;var selp;
  if(subMenu.offsetParent) {
    selp=subMenu;
    while(selp.offsetParent) {
      selp=selp.offsetParent;
      x+=selp.offsetLeft;
      y+=selp.offsetTop;
    }
  }
  x+=subMenu.offsetLeft;
  y+=subMenu.offsetTop;
  w=subMenu.offsetWidth;
  h=subMenu.offsetHeight;
  var sel=document.getElementsByTagName("select");
  for(i=0;i<sel.length;i++) {
    selx=0; sely=0; var selp;
    if(sel[i].offsetParent) {
      selp=sel[i];
      while(selp.offsetParent) {
        selp=selp.offsetParent;
        selx+=selp.offsetLeft;
        sely+=selp.offsetTop;
      }
    }
    selx+=sel[i].offsetLeft;
    sely+=sel[i].offsetTop;
    selw=sel[i].offsetWidth;
    selh=sel[i].offsetHeight;
    if(sely>y && sely<y+h) {
      if(sel[i].style.visibility!="hidden") {
        sel[i].style.visibility="hidden";
      }
    }
  }
}

function showElement()
{
        var sel=document.getElementsByTagName("select");
        for(i=0;i<sel.length;i++) {
                sel[i].style.visibility="visible";
        }
}

//<param name="id">ID контрола, к которому подключаем все свои действия</param>
//<param name="nc">ID контрола, к которому должен перейти фокус после работы с тек. меню</param>
function ListControl(id, t, nc)
{
        window.LSControls[id] = new Object();
        window.LSControls[id]['isIE'] = false;
        window.LSControls[id]['t'] = null;
        window.LSControls[id]['type'] = t;
        window.LSControls[id]['req'] = null;
        window.LSControls[id]['ignoreNext'] = false;
        window.LSControls[id]['nextControl'] = nc;
		if (ListControl.arguments[3]) {
			window.LSControls[id]['access_check']=ListControl.arguments[3];
		} else {
			window.LSControls[id]['access_check']='';
		}
        document.getElementById(id).onfocus = function(e) {itemFocus(id)};
        document.getElementById(id).form.onsubmit = function(e) {return Submit();}
        if (navigator.product == "Gecko")
        {
                document.getElementById(id).addEventListener("keypress", this.KeyPress,false);
                document.getElementById(id).addEventListener("blur", this.HideDelayed,false);
        }
        else
        {
                document.getElementById(id).attachEvent("onkeydown", this.KeyPress);
                document.getElementById(id).attachEvent("onblur", this.HideDelayed);
                window.LSControls[id].isIE = true;
        }

        document.getElementById(id).setAttribute("autocomplete","off");
}

/*******************************************************************************************/
function HideDelayed ()
{
        window.setTimeout("Hide()",400);
}
/*******************************************************************************************/
function Hide()
{
        document.getElementById("LSResult"+window.LSControlsActiveId).style.display = "none";
        var highlight = document.getElementById("LSHighlight");
        if (highlight)
        {
                highlight.removeAttribute("id");
        }
        showElement();
}
/*******************************************************************************************/
function KeyPress(event)
{
        if (event.keyCode == 13) {
        return this.Submit();
        } else
        if (event.keyCode == 40 )
        //KEY DOWN
        {
                highlight = document.getElementById("LSHighlight");
                if (!highlight)
                {
                        highlight = document.getElementById("LSShadow"+window.LSControlsActiveId).firstChild.firstChild;
                }
                else
                {
                        highlight.removeAttribute("id");
                        highlight = highlight.nextSibling;
                }
                
                if (highlight)
                {
                        highlight.setAttribute("id","LSHighlight");
                }
                
                if (! window.LSControls[window.LSControlsActiveId].isIE)
                {
                        event.preventDefault();
                }
        }
        //KEY UP
        else if (event.keyCode == 38 )
        {
                highlight = document.getElementById("LSHighlight");
                if (!highlight)
                {
                        highlight = document.getElementById("LSResult"+window.LSControlsActiveId).firstChild.firstChild.lastChild;
                }
                else
                {
                        highlight.removeAttribute("id");
                        highlight = highlight.previousSibling;
                }
                if (highlight)
                {
                        highlight.setAttribute("id","LSHighlight");
                }
                
                if (! window.LSControls[window.LSControlsActiveId].isIE)
                {
                        event.preventDefault();
                }
        }
        //ESC
        else if (event.keyCode == 27)
        {
                highlight = document.getElementById("LSHighlight");
                if (highlight)
                {
                        highlight.removeAttribute("id");
                }

                Hide();
        }
        else
        {
                if (window.LSControls[window.LSControlsActiveId].t)
                {
                        window.clearTimeout(window.LSControls[window.LSControlsActiveId].t);
                }
                
                window.LSControls[window.LSControlsActiveId].t = window.setTimeout("DoSearch()",200);
        }
}
/*******************************************************************************************/
function DoSearch()
{
        var query = '' + document.getElementById(window.LSControlsActiveId).value;
        if (query.length > 1)
        {
                if (window.LSControls[window.LSControlsActiveId].ignoreNext)
                {
                        window.LSControls[window.LSControlsActiveId].ignoreNext = false;
                }
                else
                {
                        window.LSControls[window.LSControlsActiveId].req = new Subsys_JsHttpRequest_Js();
                        
                        window.LSControls[window.LSControlsActiveId].req.onreadystatechange = ReadyStateChange;
                        
                        window.LSControls[window.LSControlsActiveId].req.caching = false;
                        window.LSControls[window.LSControlsActiveId].req.open('POST', '/load.php', true);
                        window.LSControls[window.LSControlsActiveId].req.send({ q: query, type:window.LSControls[window.LSControlsActiveId].type, access_check:window.LSControls[window.LSControlsActiveId].access_check });
                }
        }
        else
        {
                Hide();
        }
}

function ReadyStateChange()
{
//alert(window.LSControls[window.LSControlsActiveId].req.readyState)
        if (window.LSControls[window.LSControlsActiveId].req  && window.LSControls[window.LSControlsActiveId].req.readyState == 4)
        {
        //alert(window.LSControls['city'].req.responseText)
                document.getElementById('LSShadow'+window.LSControlsActiveId).innerHTML = ''+window.LSControls[window.LSControlsActiveId].req.responseJS.result;
                document.getElementById('LSResult'+window.LSControlsActiveId).style.display = 'block';
                hideElement(document.getElementById('LSResult'+window.LSControlsActiveId));
        }
}

function Submit()
{
		var highlight = document.getElementById("LSHighlight");
        if (highlight)
        {
                window.LSControls[window.LSControlsActiveId].ignoreNext = true;
				if (document.getElementById('firma_for_skidka')) {
					document.getElementById('firma_for_skidka').value=document.getElementById(window.LSControlsActiveId).value;
					skidka_change();
				}
                document.getElementById(window.LSControlsActiveId).value = highlight.getAttribute("title");
                document.getElementById("LSResult"+window.LSControlsActiveId).style.display = "none";
                Hide();
                return false;
        }
        else
        {
                return true;
        }
}

function itemMouseClick(text)
{
        document.getElementById(window.LSControlsActiveId).value = text;
		//alert(text);
        Submit();
}
 
function itemMouseOver(sender)
{
        if (document.getElementById('LSHighlight'))
                document.getElementById('LSHighlight').removeAttribute('id');
        sender.id='LSHighlight';
}

function itemFocus(id)
{
        window.LSControlsActiveId = id;
}

function debug(str)
{
        document.getElementById('addr').value += str+"\n";
}


/*****************************************************************************************/

window.LSControls = new Object();
window.LSControlsActive = '';