function changeActiveStyleSheet(previous, next)
{
    if(!document.styleSheets)
        return;
        
    for(var i = 0; i < document.styleSheets.length; ++i)
    {
        if(document.styleSheets[i].title == previous)
            document.styleSheets[i].disabled = true;
        
        if(document.styleSheets[i].title == next)
            document.styleSheets[i].disabled = false;
    }
}

function init()
{
    if(document.URL.charAt(document.URL.length - 1) == '#')
    {
        history.go(-1);
        return;
    }
    document.domain = "askdefine.com";
    var print = document.URL
    if(document.URL.substr(document.URL.length - 6) == "?print")
        changeActiveStyleSheet('default', 'print');
    else
        document.getElementById('searchword').focus();
}

function printerFriendly()
{
    var loc = window.location.href;
    var anchor = loc.indexOf('#');
    if(loc != -1)
        loc = loc.substr(0, anchor - 1);

    window.location.href = loc + "?print";
}

function doSearch()
{
    window.location.href = 'http://' + document.getElementById('searchword').value + '.askdefine.com';
	return false;
}

function setAuto(str)
{
	document.getElementById('searchword').value = str;
}

function closeAuto(str)
{
	document.getElementById('autocomplete').style.display = 'none';
}

function onSearchStringChange()
{
	var searchstr = document.getElementById('searchword').value;
	if(searchstr.length < 2)
	{
		document.getElementById('autocomplete').style.display = 'none';
		return;
	}
	
	if(document.getElementById('searchrequest') == null)
	{
		var sr = document.createElement('iframe');
		sr.id = 'searchrequest';
		sr.style.visibility = 'hidden';
		document.body.appendChild(sr);
	}
	
	if(document.sendRequest == null)
		document.getElementById('searchrequest').src = 'http://www.askdefine.com/autocomplete.html';
	else
		document.sendRequest();
}

