// Refresh every 10 minutes
window.setInterval('heartbeat()',120000);
var req;

function closePopup()
{
	if($.browser.msie)
	{
		window.location = "about:blank";
	}
	self.parent.tb_remove();

	return false;
}

function heartbeat()
{
	var req = getHTTPObject();
	req.open('GET', 'invoke.heartbeat', true);
	req.send(null);
}

function getHTTPObject()
{
	if(req)
	{
		return req;
	}
	// branch for native XMLHttpRequest object
    if(window.XMLHttpRequest)
    {
    	try
    	{
			req = new XMLHttpRequest();
        }
        catch(e)
        {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    }
    else if(window.ActiveXObject)
    {
       	try
       	{
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	}
      	catch(e)
      	{
        	try
        	{
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	}
        	catch(e)
        	{
          		req = false;
        	}
		}
    }
    return req;
}

function unsupportedBrowser()
{
	return checkNetscape4();
}

function convertHref(href)
{
	if( href.charAt(0) != '/' && !(href.indexOf("javascript:") == 0) && href.indexOf("://") < 0)
	{
		return document.getElementsByTagName('base')[0].href + href;
	}
	else
	{
		return href;
	}
}

function checkNetscape4()
{
	if( navigator.appName == 'Netscape' )
	{
		if( parseInt(navigator.appVersion) <= 4 )
		{
			return true;
		}
	}
	return false;
}

function encodeURI( sVar)
{
    return sVar;
}

function AddUniqueOption( oControl, sValue, sData )
{
	AddAnOption( oControl, sValue, sData , true);
}

function AddOption( oControl, sValue, sData )
{
	AddAnOption( oControl, sValue, sData , false);
}

function AddAnOption( oControl, sValue, sData , unique)
{
        if (oControl.disabled)
            return;
        //Remove any line breaks
        while( sValue.search( '\r\n' ) != -1 )
                sValue = sValue.replace( '\r\n', ' ' );
        while( sValue.search( '\r' ) != -1 )
                sValue = sValue.replace( '\r', ' ' );
        while( sValue.search( '\n' ) != -1 )
                sValue = sValue.replace( '\n', ' ' );

        var sValue2 = sValue
        while( sValue2.search( ' \\\\ ' ) != -1 )
                sValue2 = sValue2.replace( ' \\ ', '' );

        while( sData.search( '\r\n' ) != -1 )
                sData = sData.replace( '\r\n', ' ' );
        while( sData.search( '\r' ) != -1 )
                sData = sData.replace( '\r', ' ' );
        while( sData.search( '\n' ) != -1 )
                sData = sData.replace( '\n', ' ' );


        if( sData.length == 0 )
                sData = sValue

        if( sValue2.length > 0 )
        {
            if(unique)
            {
				for(var i = 0; i < oControl.options.length; i++)
				{
					if(oControl.options[i].value == sData)
					{
						return;
					}
				}
            }
           	oControl.options[ oControl.options.length ] = new Option( sValue, sData, false, false);
        }
}

function RemoveOption( oControl )
{
        if (oControl.disabled)
            return;
        for( var nCurrent = oControl.options.length - 1; nCurrent >= 0; nCurrent-- )
        {
                if( oControl.options[ nCurrent ].selected )
                {
                        oControl.options[ nCurrent ] = null;
                }
        }
}

function MoveOption( oSrcControl, oDstControl, sAppendValue, bDelete )
{
        if (oSrcControl.disabled || oDstControl.disabled)
            return;
        for( var nCurrent = oSrcControl.options.length - 1; nCurrent >= 0; nCurrent-- )
        {
                if( oSrcControl.options[ nCurrent ].selected )
                {

                        var sValue = new String( oSrcControl.options[ nCurrent ].text );
                        var asValues = sValue.split(' \\'); //Note: No trailing space

                        var sData = new String( oSrcControl.options[ nCurrent ].value );
                        var asDatas = sData.split(' \\'); //Note: No trailing space

                        if( asValues.length > 0 )
                        {
                                AddOption( oDstControl, asValues[0] + sAppendValue,  asDatas[0] + sAppendValue );

                                if( bDelete )
                                {
                                        oSrcControl.options[ nCurrent ] = null;
                                }
                        }

                }
        }
}

function CopyOption( oSrcControl, oDstControl )
{
        for( var nCurrent = oSrcControl.options.length - 1; nCurrent >= 0; nCurrent-- )
        {
                if( oSrcControl.options[ nCurrent ].selected )
                {

                        var sValue = new String( oSrcControl.options[ nCurrent ].text );
                        var sData = new String( oSrcControl.options[ nCurrent ].value );

                        AddOption( oDstControl, sValue,  sData );
                }
        }
}

function SelectAll( oControl )
{
        for( var nCurrent = 0; nCurrent < oControl.options.length; nCurrent++ )
        {
                oControl.options[ nCurrent ].selected = true;
        }
}

function SelectVal( oControl, val )
{
        for( var nCurrent = 0; nCurrent < oControl.options.length; nCurrent++ )
        {
                if (oControl.options[ nCurrent ].value == val)
                    oControl.options[ nCurrent ].selected = true;
                else oControl.options[ nCurrent ].selected = false;
        }
}

function CtrlOff()
{
    return false;
}

function DisableText( oControl, bVis )
{
        var mHandler = null;
        if (bVis == true)
            mHandler = CtrlOff;

        oControl.disabled = bVis;
        oControl.onkeydown = mHandler;
}

function isChecked(boxes, index)
{
	if (boxes.length == undefined)
	{
		return boxes.checked;
	}
	return boxes[index].checked;
}

function DisableAll( oControl, bVis )
{
	if(oControl)
	{
		oControl.disabled = bVis;
	}
}

function DisableBoxes( oControl, bVis )
{
        var mHandler = null;
        oControl.disabled = bVis;
        for( var nCurrent = 0; nCurrent < oControl.length; nCurrent++ )
        {
                oControl[ nCurrent ].disabled = bVis;
        }
}

function checkUserSearch(oForm, oControl)
{
    if (oControl.value.length == 0)
    {
        alert('You must enter a query before searching');
        return false;
    }
    return true;
}

function setWizardFormValue(form,nest,property,value)
{
	form.elements['cf('+nest+').'+property].value = value;
}

function getWizardFormControl(form,nest,property)
{
	return form.elements['cf('+nest+').'+property];
}

function submitFormValue(form, control, val)
{
	document.forms[form].elements[control].value = val;
	document.forms[form].submit();
}

function truncateString(str, length)
{
	if(str.length > length)
	{
		return str.substring(0, length - 3) + "...";
	}
	return str
}

function removeFromArray(array, property, value)
{
	for(var i = 0; i < array.length; i++)
	{
		if(value == eval("array[i]." + property))
		{
			array.splice(i, 1);
			return array;
		}
	}
}

function disableAllForPrefix(form, prefix, on)
{
	/* Match prefixes that are not followed by a digit */
	var re = new RegExp("^" + prefix + '(?:\\D.*)?$')
    for (var i=0; i<form.elements.length; i++)
    {
        var elem = form.elements[i];
        var name = elem.name;
        if (name != undefined && re.test(name) )
        {
       		DisableAll(elem, on);
        }
    }
}
