function bookmark(ua, url, header)
{
    document.forms['inputform'].elements['emuUA'].value=ua;
    document.forms['inputform'].elements['emuURL'].value=url;
    document.forms['inputform'].elements['emuHeaders'].value=header;
}

function saveBookmark(form)
{
    name = window.prompt('Please give a name for the bookmark:', '')
    document.getElementById("hiddenform").innerHTML = '<input type="hidden" name="emuAction" value="bookmark"><input type="hidden" name="emuName" value="' + name + '"><input type="hidden" name="emuURL" value="' + document.forms['inputform'].elements['emuURL'].value + '"><input type="hidden" name="emuUA" value="' + document.forms['inputform'].elements['emuUA'].value + '"><input type="hidden" name="emuHeaders" value="' + document.forms['inputform'].elements['emuHeaders'].value + '">';
    document.forms['hiddenform'].submit();
}

function useragent(ua)
{
    document.forms['inputform'].elements['emuUA'].value=ua;
}

function toggle(toggleElement) 
{
    var currentid;
    var nextdiv;
    var id = toggleElement.id;
    var split = id.split(",");
    var firstid = split[0];
    var openid = split[1];
    var sibling = toggleElement.nextSibling;

    // find next div - need to loop through text nodes for netscape
    for ( var current = sibling; current != null; current = current.nextSibling )
    {
	    if ( current.nodeType == '1' )
	    {
		    nextdiv = current;
		    id = nextdiv.id;
		    var split = id.split(",");
		    currentid = split[0];
		    if(firstid == 0)
		    {
			    if(nextdiv.style.display  == 'block')
			    {
				    if (currentid == 0)
				    {
					    break;
				    }
				    else
				    {
					    nextdiv.style.display = 'none';
				    }
			    }
			    else
			    {
				    if(currentid == openid)
				    {
					    nextdiv.style.display = 'block';
				    }
			    }
		    }
		    else if((currentid == openid) && (!firstid == 0))
		    {
			    if (nextdiv.style.display == 'none')
			    {

				    nextdiv.style.display = 'block';
			    }
			    else
			    {
				    nextdiv.style.display = 'none';
			    }
		     }
	    }
    }
}

function pop(URL)
{
    foo=window.open(URL, 'devices', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=400,height=600,left=200,top=120');
    if (foo.opener == null) foo.opener = self;
}

function getValue(item, defaultValue)
{
    if (item == null)
    {
        if (defaultValue == null)
        {
            return '';
        }
        return defaultValue;
    }
    return item.value;
}

YAHOO.util.Event.onAvailable('emuSource', function() {
	YAHOO.util.Event.addListener('emuSource', 'click', function() {
		var sourceBox = document.getElementById('source');
		if (YAHOO.util.Dom.hasClass(sourceBox, 'hidden')) {
			YAHOO.util.Dom.removeClass(sourceBox, 'hidden');
		} else {
			YAHOO.util.Dom.addClass(sourceBox, 'hidden');
		}
	});
});

YAHOO.util.Event.onAvailable('emuShowHeaders', function() {
	YAHOO.util.Event.addListener('emuShowHeaders', 'click', function() {
		var headerBox = document.getElementById('headers');
		if (YAHOO.util.Dom.hasClass(headerBox, 'hidden')) {
			YAHOO.util.Dom.removeClass(headerBox, 'hidden');
		} else {
			YAHOO.util.Dom.addClass(headerBox, 'hidden');
		}
	});
});

YAHOO.util.Event.onAvailable('emuUA', function() {
	var deviceSource = new YAHOO.widget.DS_XHR("../emu-helpers/devices.py", ['devices', 'ua', 'name']);
	var ac = new YAHOO.widget.AutoComplete("emuUA","emuUAcomplete", deviceSource);
    ac.doBeforeExpandContainer = function(oTextbox, oContainer, sQuery, aResults) {
        var pos = YAHOO.util.Dom.getXY(oTextbox);
        pos[1] += YAHOO.util.Dom.get(oTextbox).offsetHeight;
        YAHOO.util.Dom.setXY(oContainer,pos);
        return true;
    };
    ac.formatResult = function(aResultItem, sQuery) {
		return "(" + aResultItem[1] + ") " + aResultItem[0];
	};
});
