var datestamp = new Date()
var timestamp = datestamp.getTime()
    
function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function ajaxChecklistProgress(pWebpartID)
{
    xmlHttp = GetXmlHttpObject();
    
    var iWebpartID = pWebpartID;
    var strEmpID = document.getElementById('oEmpID').value;
    
    strURL = "Handler_Checklist_Progress.ashx?webpartid=" + iWebpartID + "&empid=" + strEmpID + "&timestamp=" + timestamp;

    xmlHttp.open("GET",strURL,true);
    xmlHttp.onreadystatechange = function()
    {
        if(xmlHttp.readyState == 4)
        {
            var strReturn = xmlHttp.responseText;
            if(strReturn != "")
            {
                var items = strReturn.split("||");
                var oControl;
                
               for(i=0; i<items.length; i++)
                {
                    oControl = document.getElementById('oCL_' + pWebpartID + '_' + (i + 1));
                    if(oControl != null)
                    {
                        if(items[i] == "1")
                        {
                            oControl.src = 'images/ss/btn_checklist_done.gif';
                            UpdateProgress(1);
                        }
                        else
                        {
                            oControl.src = 'images/ss/btn_checklist_todo.gif';
                        }
                    }
                } 
            }
            else
            {
                // error
            }
        }
    }
    xmlHttp.send(null);
}

function ajaxChecklistCheck(pControl)
{
    xmlHttp = GetXmlHttpObject();
    
    var iWebpartID = pControl.id.substring(4,pControl.id.lastIndexOf('_'));
    var iItemRank = pControl.id.substring(pControl.id.lastIndexOf('_') + 1,pControl.id.length);
    var strEmpID = document.getElementById('oEmpID').value;
    iChecked = 0;
    if(pControl.src.indexOf('todo') > 0) // sets whether checkbox is being checked (1) or unchecked (0)
    {
        iChecked = 1;
    }
    
    strURL = "Handler_Checklist_Check.ashx?webpartid=" + iWebpartID + "&itemrank=" + iItemRank + "&checked=" + iChecked + "&empid=" + strEmpID + "&timestamp=" + timestamp

    xmlHttp.open("GET",strURL,true);
    xmlHttp.onreadystatechange = function()
    {
        if(xmlHttp.readyState == 4)
        {
            var iReturn = xmlHttp.responseText;
            if(iReturn == "1")
            {
                pControl.src = 'images/ss/btn_checklist_done.gif';
                UpdateProgress(1);
            }
            else if (iReturn == "0")
            {
                pControl.src = 'images/ss/btn_checklist_todo.gif';
                UpdateProgress(0);
            }
            else
            {
                // error
            }
        }
    }
    xmlHttp.send(null);
}

function UpdateProgress(value)
{
	var iCount = document.getElementById('oItemCount').innerHTML;
	var iTotalItems = parseInt(document.getElementById('oTotalItems').innerHTML);
	if (value == 0 && 0 < iCount)
	{
		document.getElementById('oItemCount').innerHTML = parseInt(iCount) - 1;
	}
	else if (iCount < iTotalItems)
	{
		document.getElementById('oItemCount').innerHTML = parseInt(iCount) + 1;
	}
	iCount = document.getElementById('oItemCount').innerHTML;
	var width = (parseFloat(iCount/iTotalItems) * 100);
	document.getElementById('oProgressBar').style.width = width + '%';
}

function ajaxUpdateSession(pSeconds)
{
    if(document.getElementById('oAuthGroup').value != "0")
    {
        var strEmpID = document.getElementById('oEmpID').value;
        var iAuthGroup = document.getElementById('oAuthGroup').value;
        
        xmlHttp = GetXmlHttpObject();
        strURL = "Handler_SessionUpdate.ashx?empid=" + strEmpID + "&expireseconds=" + pSeconds + "&authgroup=" + iAuthGroup + "&timestamp=" + timestamp;
        xmlHttp.open("GET",strURL,true);
        xmlHttp.send(null);
    }
}