var req;

function Initialize()
{
    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();
    }

}

function SendQuery(key,location_field)
{
    Initialize();
       
    var url="http://" + document.domain + "/system/includes/ajax/location_query.asp?lookup="+key+"&location_field="+location_field
    
    
    if(req!=null)
    {
        req.onreadystatechange = Process;
        req.open("GET", url, true);
        req.send(null);

    }

}

function Process()
{
    if (req.readyState == 4)
        {
        // only if "OK"
            if (req.status == 200)
            {
                if(req.responseText=="")
                    HideDiv("autocomplete");
                else
                {
                    ShowDiv("autocomplete");
                    document.getElementById("autocomplete").innerHTML = 
                                                      req.responseText;
				                
                }
            }
            else
            {
                document.getElementById("autocomplete").innerHTML=
                    "There was a problem retrieving data:<br>" 
                    + req.statusText;
            }
        }
}

function ShowDiv(divid)
{
   if (document.layers) document.layers[divid].visibility="show";
   else document.getElementById(divid).style.visibility="visible";
}

function HideDiv(divid)
{
   if (document.layers) document.layers[divid].visibility="hide";
   else document.getElementById(divid).style.visibility="hidden";
}

//'AJAX' type function for calling urls where no feedback is required
// For example - clearing the attention list on a page within admin
function CallURL(url, refresh)
{
    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();
    }
    
    if(req!=null)
    {
    	if (refresh == 'Refresh') //Refresh page or not?
    	{
    		req.onreadystatechange = refreshPage;
    	}
        req.open("GET", url, true);
        req.send(null);
    }
}

function refreshPage()
{
	window.location.reload(true);
}

function setListColumnHeights()
{
	var objTableColumn;
	var objListColumn;

	var intListTotal;
	var intMenuBoxHeight = 0;
	var intFeatureBoxHeight = 0;
	var intMaxColumnHeight = 0;


	// Reset the heights
	for(intListTotal = 1; intListTotal <= 3; intListTotal++)
	{
		objListColumn = document.getElementById('drag_list_' + intListTotal);
		if(objListColumn)
		{
			if(isIE6())
				objListColumn.style.height = 0;
			else
				objListColumn.style.minHeight = 0;
		}
	}

	intFinalHeight = document.getElementById('infobox-table').offsetHeight;

	// Set the heights
	for(intListTotal = 1; intListTotal <= 3; intListTotal++)
	{
		objListColumn = document.getElementById('drag_list_' + intListTotal);
		if(objListColumn)
		{
			if(isIE6())
				objListColumn.style.height = intFinalHeight + 'px';
			else
				objListColumn.style.minHeight = intFinalHeight + 'px';
		}
	}
	return true;
}


function removeListItem(listtoremove, listid)
{
	//var listtoremove = document.getElementById(listid);

	if(!listtoremove)
		listtoremove = document.getElementById('floating');

	if(listtoremove)
		listtoremove.parentNode.removeChild(listtoremove);

	//Remove column in table using a random number to ensure no caching
	var iRand = Math.floor(Math.random()*11);
	//alert('/homepage/items_move.asp?remove=' + listid.replace('drag_list_box_','') + '&iRand=' + iRand,'NoRefresh');
	CallURL('/homepage/items_move.asp?remove=' + listid.replace('drag_list_box_','') + '&iRand=' + iRand,'NoRefresh');
}


function showHideDiv(iconelement,divid)
{

	//Update collapsed column in table using a random number to ensure no caching
	var iRand = Math.floor(Math.random()*11);
	CallURL('/homepage/items_move.asp?toggle=' + divid.replace('drag_list_box_','') + '&iRand=' + iRand,'NoRefresh');

	var item = document.getElementById(divid);

	if(item.style.display == 'none')
	{
		//if(isIE())
			scrollDivExpand(divid, '');
		//else
		//	item.style.display = 'block';

		iconelement.src = '/homepage/system/images/shbc/collapse.png';
	}
	else
	{
		//if(isIE())
			scrollDivCollapse(divid, '');
		//else
		//	item.style.display = 'none';

		iconelement.src = '/homepage/system/images/shbc/expand.png';
	}

}


function scrollDivCollapse(value, curHeight)
{
	var sDiv = document.getElementById(value);

	if(sDiv)
	{
		if (curHeight=='')
		{
			curHeight = parseFloat(sDiv.offsetHeight);
			sDiv.style.overflow = 'hidden';
		}

		if(Math.abs(curHeight) < 1)
		{
			sDiv.style.display = 'none';
			return null;
		}
		else
		{
			if(!curHeight)
			{
				sDiv.style.display = 'none';
				return null;
			}

			curHeight = curHeight/1.5;
			sDiv.style.height = parseInt(curHeight) + 'px';
			setTimeout('scrollDivCollapse("' + value + '",' + curHeight + ');',20);
		}
	}
	return null;
}


function scrollDivExpand(value, targetHeight)
{
	var sDiv = document.getElementById(value);

	if (sDiv)
	{
		if (targetHeight=='')
		{
			sDiv.style.height='auto';
			sDiv.style.display = 'block';
			targetHeight = sDiv.offsetHeight;
			sDiv.style.display = 'none';
			sDiv.style.height = '0px';
			sDiv.style.display = 'block';
			var i = 1;
		}

		var curHeight = parseFloat(sDiv.style.height);

		if (targetHeight - curHeight < 10)
		{
			sDiv.style.height = parseInt(targetHeight) - 9 + 'px';
			return null;
		}
		else
		{
			curHeight = parseInt(curHeight + ((targetHeight - curHeight)*0.1));
			sDiv.style.height = parseInt(curHeight) + 'px';
			setTimeout('scrollDivExpand("' + value + '",' + targetHeight + ');',20);
		}
	}
	return null;
}


function showFeature(objFeature, intFeatureId)
{
	if(!document.getElementById || !document.getElementById('featureimages') || !document.getElementById('featurelinks'))
		return false;


	var arrFeatureImages = document.getElementById('featureimages').getElementsByTagName('IMG');
	for(var intImageCount = 0; intImageCount < arrFeatureImages.length; intImageCount++)
		arrFeatureImages.item(intImageCount).style.display = 'none';


	var arrFeatureLinks = document.getElementById('featurelinks').getElementsByTagName('LI');
	for(var intLinkCount = 0; intLinkCount < arrFeatureLinks.length; intLinkCount++)
	{
		var strFeatureClass = arrFeatureLinks.item(intLinkCount).className.replace(' selected', '');
		arrFeatureLinks.item(intLinkCount).className = strFeatureClass;

		if(document.getElementById(strFeatureClass))
			document.getElementById(strFeatureClass).style.display = 'none';
	}


	document.getElementById(objFeature.className).style.display = 'block';
	document.getElementById('featureimage' + intFeatureId).style.display = 'block';
	objFeature.className+= ' selected';

	return true;
}


function displayToggleButtons(strElemId, collapsed, strTitle)
{
	document.write('<span class="toggle">');
	document.write('<img src="/homepage/system/images/shbc/delete.png" alt="Remove ' + strTitle + '" title="Remove ' + strTitle + '" onclick="javascript:removeListItem(this.parentNode.parentNode.parentNode,\'' + strElemId + '\');" />');

	if(collapsed == 1)
	{
		document.write('<img src="/homepage/system/images/shbc/expand.png" alt="Expand ' + strTitle + '" title="Expand ' + strTitle + '" onclick="javascript:showHideDiv(this, \'' + strElemId + '\');" />');
	}
	else
	{
		document.write('<img src="/homepage/system/images/shbc/collapse.png" alt="Collapse ' + strTitle + '" title="Collapse ' + strTitle + '" onclick="javascript:showHideDiv(this, \'' + strElemId + '\');" />');
	}

	document.write('</span>');
}