
var timerID = null;
var l_bTimerRunning = false;

var l_iSecondsCountDown;

var l_iInitialSeconds;
var l_iWarningTime;
var l_bKeepAlive;

function KeepAlive(iSeconds, iWarningTime) {

    l_bKeepAlive = true;

    l_iWarningTime = iWarningTime;
    l_iInitialSeconds = iSeconds;

    RebindTimer();
}

function InitializeTimer(iSeconds, iWarningTime) {

    l_bKeepAlive = false;

    l_iWarningTime = iWarningTime;
    l_iInitialSeconds = iSeconds;

    RebindTimer();
}

function RebindTimer() {

    clearWarnings();

    l_iSecondsCountDown = l_iInitialSeconds;

    StopTheClock();
    StartTheTimer(l_bKeepAlive);
}

function StopTheClock() {
    clearTimeout(timerID);
    l_bTimerRunning = false;
}

function clearWarnings() {
	$get("timeoutWarning1").innerHTML = "";
	$get("timeoutWarning2").innerHTML = "";
}

function StartTheTimer(autopost) {

	var delay = 1000;
	var sPostBack = "__doPostBack('upTime1','');";
	var sLink = "<a href='#' onclick=" + '"' + sPostBack + '"' + ">Click here to continue working on this page</a>";

    if (l_iSecondsCountDown==0) {
        StopTheClock();
        if (autopost) { // && isDirty) {
			__doPostBack('upTime1','');
		} else {
            clearWarnings();
			$get("timeoutWarning2").innerHTML = "You have been logged out due to inactivity.";
        }
    }
    else {

		//if (!isDirty) {
		//	if ((l_iSecondsCountDown % 10) == 0) {
		//		isDirty = hasPageChanged();
		//	}
		//}

		//if (monitorChangesValues.length == 0) {
		//	$get("cellIsDirty").innerHTML = "<b>no save available on this page</b>";
		//}
		//else {
		//	if ((l_iSecondsCountDown % 5) == 0) {
		//		$get("cellIsDirty").innerHTML = (isDirty || hasPageChanged()) ? "<B>change made</B>" : "<B>no change</B>";
		//	}
		//	else {
		//		if ($get("cellIsDirty").innerHTML != "") {
		//			$get("cellIsDirty").innerHTML = $get("cellIsDirty").innerHTML.replace("<B>", "").replace("</B>", "");
		//		}
		//	}
		//}

        if (l_iSecondsCountDown <= l_iWarningTime) {

			if (document.readyState == "loading" || autopost) { // (&& isDirty)) {
                clearWarnings();
            } else {
			    $get("timeoutWarning1").innerHTML = l_iSecondsCountDown;

			    if ($get("timeoutWarning2").innerHTML.indexOf("&nbsp;") != 0) {
			        $get("timeoutWarning2").innerHTML = "&nbsp;seconds left before you will be logged out.&nbsp;&nbsp;&nbsp;" + sLink;
			        window.scroll(0, 0);
			    }
			        
				//if (l_iSecondsCountDown == l_iWarningTime) {
				//}
			}
        }

        l_iSecondsCountDown -= 1;
        l_bTimerRunning = true;
        timerID = self.setTimeout("StartTheTimer(" + autopost + ")", delay);
    }

}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start=document.cookie.indexOf(c_name + "=");
	
		if (c_start!=-1) { 
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";",c_start);
			
			if (c_end == -1)
				c_end=document.cookie.length;
			
			return unescape(document.cookie.substring(c_start, c_end));
		} 
	}
	return "";
}

function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	
	document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function saveUserCookie(txtUser) {
	setCookie('username', $get(txtUser).value, 365);
}

function loadUserCookie(txtUser, txtFocus) {
	$get(txtUser).value = getCookie('username');
	
	if ($get(txtUser).value == "") {
		setFocusControl(txtUser);
	} else {
		setFocusControl(txtFocus);
	}
}


