var SEARCH_COMPLETE = true;

function setupLayer(dropdown, textfield) {
	//var div = document.getElementById(dropdown);
	var div = document.createElement("div");
	div.id = dropdown;
	document.body.appendChild(div);
	
	div.className = "dynamicdropdown";
	var element = document.getElementById(textfield);
	div.setAttribute("parentfield", element.id);
	
	at_attach(textfield, dropdown, "hover", "y", "pointer");
}
function positionLayer(divElement, parentElement) {
		
	var array = findPos(parentElement);
   	divElement.style.left 	= array[0]+1;
	divElement.style.top 	= array[1] + 18;
	divElement.style.width	= parseInt(parentElement.offsetWidth) + 18;
	var filter 				= /time/;
	if (filter.test(parentElement.id) && parseInt(parentElement.offsetWidth)+18 < 75) 
	{
		divElement.style.width = "75";
	}else if (filter.test(parentElement.id)) {
		divElement.style.width = parseInt(divElement.style.width) + 18;
	}
}
//TODO:  pass variable to describe where the div should be placed
function setupLayerUp(dropdown, textfield) {
	
	var div = document.getElementById(dropdown);
	//set width
    var filter = /time/;
	if (filter.test(textfield)) {
		div.style.width = document.getElementById(textfield).offsetWidth + 28;
	} else {
    	div.style.width = document.getElementById(textfield).offsetWidth;
    }
    var element = document.getElementById(textfield)
	var array = findPos(element);
	div.style.left = array[0];
	div.style.top = array[1] - parseInt(div.style.height) - 10;//+ element.offsetHeight;
}
//Runs syncroniously
function createSearch(textField, valueField, targetField, fields, orderBy, isSelect, searchObject, populate) {
		if(!populate) {
			populate = false;
		}
		document.getElementById(textField).onkeyup = function() {
            var value = document.getElementById(textField).value;
            if(value.length > 2 || populate == true) {
                /*REMOVE ALL ROWS*/
                var layer = document.getElementById(targetField);
                if(!isSelect) {
	                while(layer.childNodes[0]) {
	                    layer.removeChild(layer.childNodes[0]); 
	                }
                }else {
                	while(layer.options.length > 0) {
                		layer.options[0] = null;
                	}
                }
                if(SEARCH_COMPLETE) {
                	SEARCH_COMPLETE = false;
                //div.style.display = "block";
                var div = document.getElementById(targetField);
                //div.style.display = "block";
                
   	            var searchRequest = getRequestObject();
       			var address = "DynamicSearch";
                var data = "fields="+fields+"&objecttypecode="+searchObject+"&orderby="+orderBy+"&value="+value;
                //alert(data);
                sendRequestPostNoFunctionSynchronous(address, data, searchRequest);   
	          	//searchRequest.onreadystatechange = function() {
		    	    if(searchRequest.readyState == 4) {
						if(searchRequest.status == 200) {
						    //alert(searchRequest.responseText);
						    //alert(isSelect);
							var xmlDoc = getXmlDoc(searchRequest.responseText);
							var error = xmlDoc.getElementsByTagName("error")[0];
							if(error != null) {
							    alert("Dynamic Search AJAX call failed: "+error.text);
							}else {
							    var rows = xmlDoc.getElementsByTagName("row");
							    //alert(rows.length);
							    var table = document.createElement("table");
							    table.style.borderCollapse="collapse";
							    table.style.borderSpacing="0px 0px";
							    table.style.width="100%";
							    var tbody = document.createElement("tbody");
							    table.appendChild(tbody);
							   	div.appendChild(table);    
							   	if(!isSelect && rows.length == 0) {/*makes sure the user is typing in a value that exists*/
							   		document.getElementById(valueField).value = "";
			                        document.getElementById(textField).value = "";
							   	} 	
							    for(var x = 0; x<rows.length; x++) {
							        var data = rows[x];
							        if(!isSelect) {
							        	var tr = document.createElement("tr");
							        	var td = document.createElement("td");
							        	tr.appendChild(td);
							        	td.onmouseover = function() {this.className='ddover';}
							        	td.onmouseout = function() {this.className='ddout';}
							        	td.style.cursor = "pointer";
							        	td.appendChild(document.createTextNode(data.getElementsByTagName("text")[0].childNodes[0].nodeValue));
			                            td.id = data.getElementsByTagName("value")[0].childNodes[0].nodeValue;
			                            
			                            td.onclick = function() {
			                            	document.getElementById(valueField).value = this.id;
			                                document.getElementById(textField).value = this.childNodes[0].nodeValue;
			                                document.getElementById(targetField).style.display = "none";
			                                document.getElementById(valueField).setAttribute("displayvalue", this.childNodes[0].nodeValue);
			                            }
			                            tbody.appendChild(tr);
							        }else {
			                        	var text = data.getElementsByTagName("text")[0].childNodes[0].nodeValue;
			                        	var value = data.getElementsByTagName("value")[0].childNodes[0].nodeValue;
			                        	layer.options[layer.options.length] = new Option(text, value);
			                        }
			                    }
			                    SEARCH_COMPLETE = true;    
							}
						}else {alert("Error AJAX Call to DynamicSearch failed.  Could not contact web service: "+searchRequest.status);}
					}
				//}
				}
			}
      	}	
	}
	function showLayer(dropdown) {
	 	//document.getElementById(dropdown).style.display = "block";
	}
	function hideLayer(dropdown) 
	{
		//get parent field name
		var parentField = document.getElementById(dropdown).attributes.getNamedItem("parentfield").value;	
		
		//Get the displayvalue in the id field
		//Not all calls to this function will have the below need
		if(document.getElementById(parentField+"id") != null) {
			var displayValue = 	document.getElementById(parentField+"id").attributes.getNamedItem("displayvalue").value;
			//Get the value of the lookup input
			var value = document.getElementById(parentField).value;
			if(value != displayValue && displayValue != "") {
				document.getElementById(parentField).value = displayValue;
			}else {
				//document.getElementById(parentField).value = "";/*make sure that a value in the dropdown list has been selected*/
			}
		}
	}
	
	
	
/******************************************************************************/
/******************For displaying the dymanic dropdowns************************/
	
// Copyright (C) 2005-2008 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Re-distribute this code or any part of it.
//     Instead, you may link to the homepage of this code:
//     http://www.php-development.ru/javascripts/dropdown.php
//
// YOU MAY
// (1) Use this code on your website.
// (2) Use this code as part of another product.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind.
// You expressly acknowledge and agree that use of this code is at your own risk.


// ***** Popup Control *********************************************************

// ***** at_show_aux *****

function at_show_aux(parent, child)
{
  
  var p = document.getElementById(parent);
  var c = document.getElementById(child);
  
  var top  = (c["at_position"] == "y") ? p.offsetHeight : 0;
  var left = (c["at_position"] == "x") ? p.offsetWidth : 0;

  for (; p; p = p.offsetParent)
  {
    top  += p.offsetTop;
    left += p.offsetLeft;
  }
 
  //Reset the value of the object
  p = document.getElementById(parent);
  //alert(c.innerHTML);
  c.style.position   = "absolute";
  c.style.top        = top +'px';
  c.style.left       = parseInt(left)+1+'px';
  c.style.width		 = parseInt(p.offsetWidth);
  c.style.display = "block";
  
}

// ***** at_show *****

function at_show()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child"]);

  at_show_aux(p.id, c.id);
  clearTimeout(c["at_timeout"]);

}

// ***** at_hide *****

function at_hide()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.display = 'none'", 333);
}

// ***** at_click *****

function at_click()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  if (c.style.visibility != "visible") at_show_aux(p.id, c.id); else c.style.display = "none";
  return false;
}

// ***** at_attach *****

// PARAMETERS:
// parent   - id of the parent html element
// child    - id of the child  html element that should be droped down
// showtype - "click" = drop down child html element on mouse click
//            "hover" = drop down child html element on mouse over
// position - "x" = display the child html element to the right
//            "y" = display the child html element below
// cursor   - omit to use default cursor or specify CSS cursor name

function at_attach(parent, child, showtype, position, cursor)
{
    var p = document.getElementById(parent);
  
    var filter = /time/;
	if (!filter.test(parent)) {
		p = document.getElementById(p.id+"_d");
	}else {//must be time
		p = p.parentNode.parentNode.parentNode.parentNode.parentNode;//get up to the td
	} 
    
  var c = document.getElementById(child);

  p["at_parent"]     = p.id;
  c["at_parent"]     = p.id;
  p["at_child"]      = c.id;
  c["at_child"]      = c.id;
  p["at_position"]   = position;
  c["at_position"]   = position;

  c.style.position   = "absolute";
  c.style.display = "none";
  
   
  if (cursor != undefined) p.style.cursor = cursor;

  switch (showtype)
  {
    case "click":
      p.onclick     = at_click;
      p.onmouseout  = at_hide;
      c.onmouseover = at_show;
      c.onmouseout  = at_hide;
      break;
    case "hover":
      /*p.onmouseover = at_show;*/
      p.onclick	  = at_show;
      p.onmouseout  = at_hide;
      c.onmouseover = at_show;
      c.onmouseout  = at_hide;
      break;
  }
}
	
	
	
	

