<!--

function initSmartComboboxByDataInElements()
{
   var elems = document.getElementsByName("SmartComboboxData");
   for(var i=0; i<elems.length; i++)
   {
      var args = elems[i].value.split(",");
      initSmartCombobox(args[0], args[1], args[2], args[3], args[4], args[5], eval(args[6]));
   }
}

/* SMART COMBOBOX INITIALIZATION */
function initSmartCombobox(textboxID, selectedvalueboxID, divID, buttonID, xmlReqId, smartComboboxClientID, autoPostBack)
{
    var scb = new smartCombobox(textboxID, selectedvalueboxID,  divID, buttonID);
    scb.xmlReqId = xmlReqId; 
    scb.smartComboboxClientID = smartComboboxClientID;
    scb.autoPostBack = autoPostBack;
    
    document.getElementById(textboxID).scb = scb;
    document.getElementById(divID).scb = scb;
    document.getElementById(buttonID).scb = scb;
    document.onclick = closeOpenedComboboxes;
}

function smartCombobox(_textboxID, _selectedvalueboxID,  _divID, _buttonID)
{
   this.textbox = document.getElementById(_textboxID);
   this.div = document.getElementById(_divID);
   this.selectedvaluebox = document.getElementById(_selectedvalueboxID); 
   this.button = document.getElementById(_buttonID);
   
   this.nm = _divID;
   
   this.initialText = this.textbox.value;
   this.initialValue = this.selectedvaluebox.value;
}

function callPostBack(scb)
{
   if (scb.autoPostBack == true)
		__doPostBack(scb.smartComboboxClientID, scb.selectedvaluebox.value);
}

function InitializeXMLHttpRequest(req)
{
    if (req != null)
       req.abort();
       
	try{ 
		req=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e){    
		try { req=new ActiveXObject("Microsoft.XMLHTTP"); }
		catch(oc){ req=null;}
	}
	if(!req && typeof XMLHttpRequest!="undefined")
		req = new XMLHttpRequest();
	
	return req;	
}

var reqSCBox;

function requestSCBoxItems(sender, key)
{
	var url = document.utilityPagePath+"?app="+document.applicationId+"&scbc="+sender.scb.xmlReqId+"&skey="+key+"&divid="+sender.scb.div.id+"&ts="+(new Date()).getMilliseconds(); 
	reqSCBox = InitializeXMLHttpRequest(reqSCBox);
	reqSCBox.onreadystatechange = requestSCBoxItems_processresult;
	
	showSCBoxItems(sender.scb.div.id, "<div style='width:100%;border:solid 1px black;background-color:white;color:blue;padding: 3 0 3 3'>(loading...)</div>");
	
	reqSCBox.open('GET', url, true);
	reqSCBox.send(null);
    return false;
}    

function requestSCBoxItems_processresult()
{

    if (reqSCBox.readyState == 4)
    {
      if (reqSCBox.status == 200)
      {
          var clientId = reqSCBox.responseText;
          var responseText = null;
          var pos = clientId.indexOf("<select");
          if (pos != -1)
          {
              clientId = reqSCBox.responseText.substr(0, pos);
              responseText = reqSCBox.responseText.substr(pos);
          }   
          
          
          
          if(responseText == null)
              hideSCBoxItems(clientId);
          else
              showSCBoxItems(clientId, responseText);
      }
    }
}


function showSCBoxItems(clientId, responseText)
{
  var divlist = document.getElementById(clientId);
  divlist.innerHTML = responseText;
  
  var left = divlist.scb.textbox.offsetLeft;
  var top = divlist.scb.textbox.offsetTop + divlist.scb.textbox.offsetHeight;
  var pn = divlist.scb.textbox.offsetParent;
  while(pn != null && pn.offsetTop != null && pn.offsetLeft != null)
  {
       left+=pn.offsetLeft;
       top+=pn.offsetTop;
       pn=pn.offsetParent;
  } 
  divlist.style.position ="absolute"; 
  
  var modalFormCoords = MF.getModalFormCoords();
  
  divlist.style.left = (left - modalFormCoords.Left) + "px";
  divlist.style.top = (top - modalFormCoords.Top) + "px";
  divlist.style.width = divlist.scb.textbox.offsetWidth;
    
  divlist.style.visibility = "visible";
}

function hideSCBoxItems(clientId)
{
  var divlist = document.getElementById(clientId);
//  divlist.style.visibility = "hidden";
}



/* PROCESS ACTIONS IN SELECT */
function buttonClick(sender)
{
   if (sender.scb.div.style.visibility != "hidden")
      sender.scb.div.style.visibility = "hidden";
   else   
   {
	  document.lastopenedlistbox = sender.scb.div;
      return requestSCBoxItems(sender, "");
   }
}

function listClick(sender)
{
   var scb = sender.parentNode.scb;
   
   if (sender.selectedIndex != -1)
   {
       if (sender.options[sender.selectedIndex].value.search(/page:\d+/) != -1)
       {
          moreRecords(sender);
          return false;
       }

       if (document.all)
	      scb.textbox.value = sender.options[sender.selectedIndex].innerText;
       else		
          scb.textbox.value = sender.options[sender.selectedIndex].text;
        
       var valueChanged = (scb.selectedvaluebox.value != sender.options[sender.selectedIndex].value);   
       scb.selectedvaluebox.value = sender.options[sender.selectedIndex].value;
       scb.div.style.visibility = "hidden";
       scb.textbox.focus(); 

       if (valueChanged)
	      callPostBack(scb);
   }

   return false;
}

function listProcessKeys(sender, e)
{
   var scb = sender.parentNode.scb;
   
   if (document.all)
     e = event;
     
   if (e.keyCode == 27) //esc
   {
       textboxProcessEscKey(scb);
       return;
   }    
     
   if (e.keyCode == 13 && sender.selectedIndex != -1) //Enter
   {
      if (sender.options[sender.selectedIndex].value.search(/page:\d+/) != -1)
      {
         moreRecords(sender);
         return false;
      }
   
      if (document.all)
    	  scb.textbox.value = sender.options[sender.selectedIndex].innerText;
      else		
          scb.textbox.value = sender.options[sender.selectedIndex].text;
      var valueChanged = (scb.selectedvaluebox.value != sender.options[sender.selectedIndex].value);
      scb.selectedvaluebox.value = sender.options[sender.selectedIndex].value;

      scb.div.style.visibility = "hidden";
      scb.textbox.focus(); 
      
      if (valueChanged)
		callPostBack(scb);

      return false;
   }  
   
}


/* PROCESS ACTIONS IN TEXTBOX */
function textboxProcessKeys(sender, e)
{
	if (e.keyCode == 27) //esc
	{
	    textboxProcessEscKey(sender.scb);
		return;
	}    
	
    if (e.keyCode == 13 || e.keyCode == 9 || e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39) //enter, tab, left, right, up arrow
        return;
        
    if (e.keyCode == 40) //down arrow
    {
        if (sender.scb.div.style.visibility != "hidden")
        {
	        sender.scb.div.firstChild.focus();
			if (sender.scb.div.firstChild.options.length > 0)
               sender.scb.div.firstChild.selectedIndex = 0;
        }
        else
            return requestSCBoxItems(sender, sender.value);
    }  
	else  
        return requestSCBoxItems(sender, sender.value);
      
   return false;

}

/*closes list, put old value*/
function textboxProcessEscKey(scb)
{
    var options = scb.div.firstChild.options;
	for(var i=0; i<options.length; i++)
	{
		if (options[i].value == scb.selectedvaluebox.value)
		{
			scb.textbox.value = (document.all ? options[i].innerText : options[i].text); 
			break;
		}   
	}
    scb.div.style.visibility = "hidden";
    scb.textbox.focus(); 
}

/*if list contains newly entered text, select a new item, and
if new value adding isn't allowed, select the initial item */
function textboxProcessEnter(sender, e)
{
   if (e.keyCode == 13)
   {
        if (sender.scb.div.style.visibility != "hidden")
        {
	        var options = sender.scb.div.firstChild.options;
	        var contains = false;
		    for(var i=0; i<options.length; i++)
			{
			    if (options[i].text.toLowerCase() == sender.scb.textbox.value.toLowerCase())
			    {
			        sender.scb.textbox.value = options[i].text;
			        var valueChanged = (sender.scb.selectedvaluebox.value != options[i].value);
   			        sender.scb.selectedvaluebox.value = options[i].value;
	   	            contains = true;
 	      
 	                if (valueChanged)
 						callPostBack(sender.scb);
		            break;
			    }   
		    }
	    
		    if (!contains)
		    {
		        sender.scb.textbox.value = sender.scb.initialText;
		        var valueChanged = (sender.scb.selectedvaluebox.value != sender.scb.initialValue);
		        sender.scb.selectedvaluebox.value = sender.scb.initialValue;

                if (valueChanged)
					callPostBack(sender.scb);
		    }

	        sender.scb.div.style.visibility = "hidden";
		    sender.scb.textbox.focus(); 
        }
        return false;
   }  
   return true;
}


function textboxProcessTab(sender, e)
{
   if (sender.scb.div.style.visibility != "hidden" && e.keyCode == 9)
   {
        var options = sender.scb.div.firstChild.options;
        var contains = false;
	    for(var i=0; i<options.length; i++)
		{
		    if (options[i].text.toLowerCase() == sender.scb.textbox.value.toLowerCase())
		    {
		        sender.scb.textbox.value = options[i].text;
		        var valueChanged = (sender.scb.selectedvaluebox.value != options[i].value);
   	            sender.scb.selectedvaluebox.value = options[i].value;
   	            contains = true;

                if (valueChanged)
					callPostBack(sender.scb);
	            break;
		    }   
	    }
	    
	    if (!contains)
	    {
	        sender.scb.textbox.value = sender.scb.initialText;
	        var valueChanged = (sender.scb.selectedvaluebox.value != sender.scb.initialValue);
            sender.scb.selectedvaluebox.value = sender.scb.initialValue;

			if (valueChanged)
				callPostBack(sender.scb);
	    }

        sender.scb.div.style.visibility = "hidden";
        sender.scb.textbox.focus(); 
        return false;
   }  
   return true;
}


/*click on any place within document except dropdown image closes all opened listboxes*/
function closeOpenedComboboxes()
{
   var listboxes = document.getElementsByTagName("SELECT");
   for(var i=0; i<listboxes.length; i++)
   {
      /*smart combobox*/
      if (listboxes[i].scb != null && listboxes[i].style.visibility == "visible")
      {
          if (document.lastopenedlistbox == null || document.lastopenedlistbox.scb.nm != listboxes[i].scb.nm)
          {
				for(var j=0; j<listboxes[i].scb.items.length; j++)
				{
					if (listboxes[i].scb.items[j].value == listboxes[i].scb.selectedvaluebox.value)
          			{
						listboxes[i].scb.textbox.value = listboxes[i].scb.items[j].text;
						break;
					}  
 				}
	            listboxes[i].style.visibility = "hidden";
          }   
      } 
      
      /*smart combobox2*/
      if (listboxes[i].parentNode.scb != null && listboxes[i].parentNode.style.visibility == "visible")
      {
          if (document.lastopenedlistbox == null || document.lastopenedlistbox.scb.nm != listboxes[i].parentNode.scb.nm)
          {
                var valuefound = false;
			 	for(var j=0; j<listboxes[i].options.length; j++)
		        {
		            if (listboxes[i].options[j].value == listboxes[i].parentNode.scb.selectedvaluebox.value)
          		    {
	                    listboxes[i].parentNode.scb.textbox.value = (document.all ? listboxes[i].options[j].innerText : listboxes[i].options[j].text); 
	                    valuefound = true;
	                    break;
		            }  
 	            }
 	            if (!valuefound)
 	            {
 	                listboxes[i].parentNode.scb.textbox.value = listboxes[i].parentNode.scb.initialText;
 	                listboxes[i].parentNode.scb.selectedvaluebox.value = listboxes[i].parentNode.scb.initialValue;
 	            }
                listboxes[i].parentNode.style.visibility = "hidden";
          }   
      } 
   }
   document.lastopenedlistbox = null;
}

function moreRecords(sender)
{
   var scb = sender.parentNode.scb;
   scb.div.style.visibility = "hidden";
   scb.textbox.focus(); 

   requestSCBoxItems(scb.textbox, scb.textbox.value + sender.options[sender.selectedIndex].value);
   
   return false;
}

/*
function validateSmartCombobox(source, arguments)
{
  var value = document.getElementById(source.controltovalidate).scb.selectedvaluebox.value;
  arguments.IsValid=(value.trim() != "");
}
*/

//-->

