// Show select buttons for text fields with drop down lists
function showSelectButtons(type)
{
    // Get the document type
    var t=document.getElementsByTagName('table');
    var type="";
    if (t != null)
    {
        for (var i=0;i<t.length;i++)
        {
            if (t[i].id.substr(0, 25) == "ctl00_ContentPlaceHolder1")
            {
                type=t[i].id.substr(26);
            }
        }
    }
    
    // Get all input controls
    var c=document.getElementsByTagName('input');
    if (c != null)
    {
        // Check each control
        for (var j=0;j<c.length;j++)
        {
            // Check if the control is a select button
            if (c[j].id.substr(0, 14) == 'TextListButton')
            {
                // Check if there's a list for the button
                var l=document.getElementById('ctl00_ContentPlaceHolder1_'+c[j].id.substr(14)+'_List');
                var id='ctl00_ContentPlaceHolder1_'+c[j].id.substr(14);
                var h=LP_getLookup(type, id);
                if (l != null || h != "")
                {
                    // Show the select button
                    c[j].style.visibility="visible";
                    c[j].style.position="relative";
                    // Show the help link
                    var h=document.getElementById('ctl00_ContentPlaceHolder1_TextTipHyperLink'+c[j].id.substr(14));
                    if (h != null)
                    {
                        h.style.visibility="visible";
                        h.style.position="relative";
                    }
                }
            }
        }
    }
}

// Return a string containing all the lookup code to be displayed
function LP_getLookup(type, id)
{
    var results="";
    if (id != null)
    {
	    // Get the lookup list and add the HTML code for each item
	    var s=document.getElementById(id+'_List');
	    var items;
        if (s != null) // List items are provided in a select control
        {
            items=s.options.outerHTML.replace('class=hiddenselect', '');//convertToRows(s.options.innerHTML, this.cssPrefix, this.index);
        }
        else // Try to get the list items from LookupList.js
        {
            items=getLookupHTML(type, id);
        }    
        if (items == null || items == "")
        {
            return ""; // No list defined
        }
        // Create the HTML code for the list
        var result='<TABLE ALIGN=CENTER BGCOLOR=#FFFFCC BORDER=0 CELLSPACING=0 CELLPADDING=0>\n';
        result += '<TR><TH CLASS='+this.cssPrefix+'header>'+id.substr(id.lastIndexOf('_')+1)+'\n';
        result += '<TABLE WIDTH=100% CLASS="lpColumnHeader" BORDER=0 CELLSPACING=0 CELLPADDING=0 BGCOLOR=#FFFFFF>\n';
        result += '<TR CLASS="lpColumnHeader">\n';
        result += '<TD ALIGN=CENTER BGCOLOR=#CCCC99>'+items+'</TD>\n';
	    result += '</TR>\n';
	    result += '</TH><TR>\n';
	    result += '<TD ALIGN=CENTER CLASS="lpSelectText" >\n';
	    result += '<A ID="Select" HREF="javascript:if (document.getElementById(\''+id+'_List\').selectedIndex > -1) { LP_tmpReturnFunction(document.getElementById(\''+id+'_List\').options[document.getElementById(\''+id+'_List\').selectedIndex].value);LP_hideLookup('+this.index+'); }">Select</A>';
        result += '</TD></TR></TABLE></TD></TR></TABLE>\n';
    }
    return result;
}

// Populate the lookup and display it
function LP_showLookup(type, objid, anchorname)
{
    var html=this.getLookup(type, objid);
    if (html != null && html != "")
    {
	    this.populate(html);
        this.showPopup(anchorname);
    }
}

// Simple method to interface popup lookup with a text-entry box
function LP_select(inputtype, inputobj, linkname)
{
    if (inputtype == null || inputobj == null)
    {
        return; // No input object specified
    }
	if (inputobj.type!="text" && inputobj.type!="hidden" && inputobj.type!="textarea")
	{ 
		alert("lookup.select: Input object passed is not a valid form input object"); 
		window.LP_targetInput=null;
		return;
	}
	if (inputobj.disabled)
	{
	    return; // Can't use lookup input on disabled form input!
	}
	window.LP_targetInput = inputobj;
	window.LP_lookupObject = this;
	this.showLookup(inputtype, inputobj.id, linkname);
}

// CONSTRUCTOR for the LookupPopup Object
function LookupPopup()
{
    var l;
    if (arguments.length>0)
    {
        l = new PopupWindow(arguments[0]);
    }
    else        
    {
		l = new PopupWindow();
		l.setSize(150,175);
    }
	l.offsetX = 0;//-152;
	l.offsetY = 25;
	l.autoHide();
	l.cssPrefix="lookup";
	window.LP_LookupObject = null;
	window.LP_targetInput = null;
	// Method mappings
	l.showLookup = LP_showLookup;
	l.hideLookup = LP_hideLookup;
	l.getLookup = LP_getLookup;
	l.select = LP_select;
	// Return the object
	return l;
}

// Hide a lookup object
function LP_hideLookup()
{
	if (arguments.length > 0)
	{
	    window.popupWindowObjects[arguments[0]].hidePopup();
	}
	else
	{
	    this.hidePopup();
	}
}

// Temporary default functions to be called when items clicked, so no error is thrown
function LP_tmpReturnFunction(r)
{ 
	if (window.LP_targetInput!=null)
	{
		window.LP_targetInput.value = r;
	}
	else
	{
		alert('Use setReturnFunction() to define which function will get the clicked results!'); 
	}
}

