// Refresh every 2 minutes
window.setInterval('heartbeat()',120000);
var req;

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("://") < 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.length ] = new Option( sValue, sData, false, false);
        }
}

function RemoveOption( oControl )
{
        if (oControl.disabled)
            return;
        for( var nCurrent = oControl.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.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.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.length; nCurrent++ )
        {
                oControl.options[ nCurrent ].selected = true;
        }
}

function SelectVal( oControl, val )
{
        for( var nCurrent = 0; nCurrent < oControl.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;
		}
	}
}


// -----------------------------------------------------------------------------
// Custom additions

// Adds a new box below the task list
function getHTTP (url, callback) {
   xmlhttp=null;
   // code for Mozilla, etc.
   if (window.XMLHttpRequest) {
      xmlhttp=new XMLHttpRequest();

   } else if (window.ActiveXObject) {
      // code for IE
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

   if (xmlhttp!=null) {
      xmlhttp.onreadystatechange=callback;
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);
   } else {
      ;
   }
}



// Don't uncomment this until there's content in the index boxes.

if (location.href.match(/^http:\/\/(lptest|learningplace)\.eq\.edu\.au\/(cx3|tle30|cx|8036|8035)\/(resources|cxrc)\/access\/Tasks.jsp/)){
   if( window.addEventListener ) {
     window.addEventListener('load',__initPage,false);
   } else if( document.addEventListener ) {
     document.addEventListener('load',__initPage,false);
   } else if( window.attachEvent ) {
     window.attachEvent('onload',__initPage);
   }
}

function __initPage () {
   var taskList = document.getElementById('task-list');
   if (taskList) {
      getHTTP('http://learningplace.eq.edu.au/cx/resources/index-boxes/index.xml?x='+Math.random(), function () {
         if (xmlhttp.readyState==4) {
            if (xmlhttp.status == 200) {
               var d = document.createElement('div');
               d.innerHTML = xmlhttp.responseText;
               document.getElementById('tasks-home').appendChild(d);
            }
         }
      });
   }
}

// -----------------------------------------------------------------------------
// Hide BIRT frame.
//
if (location.href.match(/^http:\/\/(lptest|learningplace).eq.edu.au\/(cx|8035|8036)\/(cxrc|resources)\/access\/reports\.do\?method=nav/)) {
   var framesets = top.document.getElementsByTagName('frameset');
   if (framesets[0]) {
      framesets[0].rows = '0,100%';
   }
}