//pop-up warning and login form for students
//warns at 18 minutes and prompts for login credentials at 20 minutes
//5/17/07 MWarner
var watchTimeOutRun=0;
var watchTimeOutDIV_displayed=null;
var watchTimeOut_checkLoginRun=0;
var loginFrame=window.frames["loginframe"];
var watchTimeOut_timeOutID=null;

function watchTimeOut(){
	$rID=$rID*1;//make sure it's an int
	if(watchTimeOutRun==2){
		watchTimeOutRun=3;
		if($rID==28){//redirected realms.  These have auto-logins.  The student won't know the CMS login credentials
			show_hide_watchTimeOutDiv("watchTimeOut_redirectDiv");
		}else{
			show_hide_watchTimeOutDiv("watchTimeOut_loginDiv");
		}
		if(GetCookie("timer_totalTime"+theData[0])){//if using timer code - IEA, for example, this cookie val is set
			save_iea_time(theData[0],whichCourseID);
			Timer_stop();
		}
		//Mwarner 3/30/2010
		$timer_stopped=1;
	}
	if(watchTimeOutRun==1){
		if(document.location.href.indexOf("courses.mindedge.com")>-1||$rID==1||$rID==7||$rID==59||$rID==75){//us, us, WGU||$rID==27, ISM, GardnerDenver //||document.location.href.indexOf("home/index.php")>-1
			//alert($rID+"\n\n"+$rID*2);
			//mindedge folks, homepages (not logged in), and any other realm id included above
			findDOM("refreshSession_btn",0).click();
			//watchTimeOut_refreshSession();
			watchTimeOutRun=0;
			//don't set it here, it's set below
			//setTimeout("watchTimeOut()",300*60*60);//18 minutes
		}else{
			//watchTimeOutRun=2;
			show_hide_watchTimeOutDiv("watchTimeOut_warningDiv");
			clearTimeout(watchTimeOut_timeOutID);
			watchTimeOut_timeOutID=setTimeout("watchTimeOut()",2*1000*60);//2 minutes
		}
	}
	if(watchTimeOutRun==0||watchTimeOutRun==-1){		
		clearTimeout(watchTimeOut_timeOutID);
		watchTimeOut_timeOutID=setTimeout("watchTimeOut()",18*1000*60);//18 minutes
	}
	watchTimeOutRun++;
}

//if(document.location.href.indexOf("courses.mindedge.com")<0){
	watchTimeOut();
//}


function show_hide_watchTimeOutDiv(whichDiv){
	//find out where to place the DIV
	scrollAmount = window.pageYOffset ? window.pageYOffset : document.body.scrollTop;
	divTop=parseInt(scrollAmount)+100;
	if(whichDiv=="watchTimeOut_warningDiv"){
		findDOM(whichDiv,1).display='block';
		findDOM(whichDiv,1).top=divTop;
		watchTimeOutDIV_displayed="watchTimeOut_warningDiv";
	}
	if(whichDiv=="watchTimeOut_loginDiv"){
		findDOM("watchTimeOut_warningDiv",1).display='none';
		watchTimeOutDIV_displayed="watchTimeOut_loginDiv";
		findDOM(whichDiv,1).display='block';
		findDOM(whichDiv,1).top=divTop;
		//Mwarner 3/30/2010
		$timer_stopped=1;
	}
	if(whichDiv=="watchTimeOut_redirectDiv"){
		findDOM("watchTimeOut_warningDiv",1).display='none';
		watchTimeOutDIV_displayed="watchTimeOut_redirectDiv";
		findDOM(whichDiv,1).display='block';
		findDOM(whichDiv,1).top=divTop;
		//Mwarner 3/30/2010
		$timer_stopped=1;
	}
}

function watchTimeOut_redirect(){
	redirectURLs=new Array();
	redirectURLs[28]="http://stage.phyins.com/member-login/index.php";
	//redirectURLs[59]="http://stage.ism.ws/";
	document.location=redirectURLs[$rID];
}

function hide_watchTimeOutDivs(){
	findDOM("watchTimeOut_warningDiv",1).display='none';
	findDOM("watchTimeOut_loginDiv",1).display='none';
	watchTimeOutDIV_displayed=null;
	watchTimeOutRun=0;
		//Mwarner 3/30/2010
		$timer_stopped=0;
	watchTimeOut();
}

function watchTimeOut_refreshSession(){
	tmp=new Image;
	//tmp.src="/p/onlineEd/courseAdmin.php?apID=0";
	tmp.src=document.location.href+"&watchTimeOutScript=watchTimeOutRun:"+watchTimeOutRun+"&rIDX="+$rID+"&ua="+navigator.appName;
	watchTimeOutRun=0
	watchTimeOut_loginAttempts=0;
	hide_watchTimeOutDivs();
	clearTimeout(watchTimeOut_timeOutID);
	setTimeout("watchTimeOut()",18*1000*60);//18 minutes
	//watchTimeOutRun=-1
}
/*
function checkLogin(){
	if(!checkLoginRun){
		checkLoginRun=1;
	}else{
		if(loginframe.document.getElementById("moduleTitleTD")){
			if(loginframe.document.getElementById("moduleTitleTD").innerHTML=="Member Login"||loginframe.document.getElementById("moduleTitleTD").innerHTML=="Realm Authentication Required"){
				alert("Login incorrect.  Please try again");
				show_hide_watchTimeOutDiv("watchTimeOut_loginDiv");
				checkLoginRun=0;
			}else{
				hide_watchTimeOutDivs();
			}
		}else{
			hide_watchTimeOutDivs();
		}
	}
}
*/
var watchTimeOut_loginAttempts=0;

function watchTimeOut_checkLogin(ajaxResponse){
	if(ajaxResponse.indexOf("Member Login")>-1||ajaxResponse.indexOf("Realm Authentication Required")>-1){
		alertText="Login incorrect.  Please try again";
		if(watchTimeOut_loginAttempts>0){
			alertText+="\n\nIf you continue to get this message, click the \"Home\" link at the top-right of the page and try again";
		    alert(ajaxResponse);
		}
		alert(alertText);
		watchTimeOut_loginAttempts++;
		show_hide_watchTimeOutDiv("watchTimeOut_loginDiv");
		watchTimeOut_checkLoginRun=0;
	}else{
		
		if(GetCookie("timer_totalTime"+theData[0])){
			//start timer again
			start_iea_time(theData[0],whichCourseID);
		}
		watchTimeOut_loginAttempts=0;
		hide_watchTimeOutDivs();
		//Mwarner 3/30/2010
		$timer_stopped=0;
	    location.reload();//refresh the page MWarner 3/30/2010
	}
}


function watchTimeOut_ajax_login(loginFormObj){

	loginUrl="/p/member/membershipRequired.php";
	loginUrlParams="memberLogin_ftl=1&_submitBut_memberLogin=Login&SM1_SF0_ftl=1&_submitBut_SM1_SF0=Login&userName="+loginFormObj.userName.value+"&passWord="+loginFormObj.passWord.value
	
	loginFormObj.userName.value="";
	loginFormObj.passWord.value="";
	xhr=null;
	// non-IE
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	// IE version - activeX
	else if (window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xhr.open("POST",loginUrl,true);
	/*only needed for POST*/
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", loginUrlParams.length);
	xhr.setRequestHeader("Connection", "close");

	xhr.setRequestHeader("Pragma", "no-cache"); 
	xhr.setRequestHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); 
	xhr.setRequestHeader("Cache-Control", "no-store, must-revalidate,must-revalidate, max-age=-1"); 
			
	if(window.XMLHttpRequest){//non-IE
		xhr.send(loginUrlParams);
	}	
	else if(window.ActiveXObject){//IE
		xhr.send(loginUrlParams);
	}
	xhr.onreadystatechange=function() {
		if(xhr.readyState==4){// &&xhr.status==200){
			watchTimeOut_checkLogin(xhr.responseText);
		}
	}
}
