// JavaScript Document
function createCookie(name,value,mins) {
	if (mins) {
		var date = new Date();
		date.setTime(date.getTime()+(mins*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	if(document.cookie = name+"="+value+expires+"; path=/")
	return 1;
	
}

function readCookie(name) {
	var nameEQ = name+"=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ')

		c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
		return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}
//function to delete all cookies at a time
function del_all_cookies()
{
	//alert("in delete");
	var c=document.cookie.split(";");
	for(var i=0;i<c.length;i++)
	{
		var e=c[i].indexOf("=");
		var n=e>-1?c[i].substr(0,e):c[i];
		eraseCookie(n);
	}
}

function read(name)//to read the cookie.
{
	var start_time=readCookie(name);//check if the cookie exists.

	if(start_time)//if exists assign the cookie value to the hidden field strt_time.
	{
		document.getElementById('strt_time').value=start_time;
	}
	else//else create a cookie.
	{
		tStart   = new Date();//get the current time here.
	   var curtime = ""+tStart.getHours() + ":"
	                                   + tStart.getMinutes() + ":" 
	                                   + tStart.getSeconds();
		var s=createCookie(name,curtime,0);//create cookie with value=current time.
		start_time=readCookie(name);//get the cookie value by reading.
		document.getElementById('strt_time').value=start_time;
	}
}
function Stop(name)//this is called when you want to move to another page. 
{   
    
   	tEnd=new Date();//get the current time here.
	var Endtime = ""+tEnd.getHours() + ":"
                                   + tEnd.getMinutes() + ":" 
                                   + tEnd.getSeconds();
   document.getElementById('end_time').value=Endtime;
   eraseCookie(name);//later cookie is erased.
}
/////////////////for timers//////////////////////////////
var up,down;
var hour1,min1,sec1;
var chour1,cmin1,csec1;

//////////////with hour//////////
function Display(hour,min,sec) //to display the time.
{
var disp;
if(hour<=9) {disp=" 0";}
else {disp=" ";}
disp+=hour+":";
if(min<=9) disp+="0"+min;
else disp+=min;
disp+=":";
if(sec<=9) disp+="0"+sec;
else disp+=sec;

return(disp);
}
///////////////////////////////////
function check_coursetimer(start_time,Stop_time,tot_time)
{
	if(time_spent=="00:00:00")
	{
    	return true;
	}
	else
	{
		alert("You have not spent the minimum required time on this page.  Please close this box and continue reading.");
		return false;
	}
}

//to check for the section quiz cookie.
function check_sectionquiz(name,section_num)
{
  
  var section_cookie=readCookie(name);
  if(section_cookie)
  {
     //go to control panel.
      location.href='ControlPanel.php?section_quiz=1&section_num='+section_num;
  }
  else
  {
    //create the cookie.
    var a=createCookie(name,'refresh',0);
    
  }
}
//to check for the section quiz cookie.
function check_sectionquiz1(name,section_num)
{
  //var section_num=section_num-1;
  var section_cookie=readCookie(name);
  //alert(section_cookie);
  if(section_cookie)
  {
     //go to control panel.
      location.href='ControlPanel.php?section_quiz=1&section_num='+section_num;
  }
  else
  {
    //create the cookie.
    var a=createCookie(name,'refresh',0);
    
  }
}

//function to delete all the cookies of section_quiz.
function delete_cookies(name)
{
  eraseCookie(name);
}

//to set /get cookie arrays.
function setCookieArray(name)
{
	expdate = 45000;
	this.length = setCookieArray.arguments.length - 1;
		for (var i = 0; i < this.length; i++) 
		{
		data = setCookieArray.arguments[i + 1];
		createCookie (name + i, data, expdate);
		}        
}

function getCookieArray(name)
{
	var i = 0;
	while (readCookie(name + i) != null) 
	{
	this[i + 1] = readCookie(name + i);
	i++; this.length = i; 
	
	}
}
//on window close delete all cookies
function delcookies_onwinclose()
{
	var iX = window.document.body.offsetWidth - window.event.clientX ;
	var iY = window.event.clientY ;

	if (iX <=30 || iY < 0 )
	{
		del_all_cookies();
	}
}