

var enableCache = true;
var jsCache = new Array();
var mode=0;
var currentpage=0;

var dynamicContent_ajaxObjects = new Array();

function ajax_showContent(divId,ajaxIndex,url)
{
	document.getElementById(divId).innerHTML = dynamicContent_ajaxObjects[ajaxIndex].response;
	if(enableCache){
		jsCache[url] = 	dynamicContent_ajaxObjects[ajaxIndex].response;
	}
	dynamicContent_ajaxObjects[ajaxIndex] = false;
}

function ajax_loadContent(divId,url)
{
	if(enableCache && jsCache[url]){
		document.getElementById(divId).innerHTML = jsCache[url];
		return;
	}
	
	var ajaxIndex = dynamicContent_ajaxObjects.length;
	document.getElementById(divId).innerHTML = '<span style="font-size:12pt;text-algin:center;">Loading ...<img src="/_img/loading.gif"></span>';
	dynamicContent_ajaxObjects[ajaxIndex] = new sack();
	dynamicContent_ajaxObjects[ajaxIndex].requestFile = url;	// Specifying which file to get
	dynamicContent_ajaxObjects[ajaxIndex].onCompletion = function(){ ajax_showContent(divId,ajaxIndex,url); };	// Specify function that will be executed after file has been found
	dynamicContent_ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	
	
	
}
function la(divID,myURL)
{
	Stamp = new Date();
	ajax_loadContent(divID,myURL + '&stamp=' + Stamp.getHours() + '' + Stamp.getMinutes() + '' +  Stamp.getSeconds());
}
function lax(divID,myURL)
{
	Stamp = new Date();
	initSlideLeftPanel(false,'dhtmlgoodies_leftPanel');
	ajax_loadContent(divID,myURL + '&stamp=' + Stamp.getHours() + '' + Stamp.getMinutes() + '' +  Stamp.getSeconds());
	
}
function al(divID,URL)
{
		Stamp = new Date();
		
		ajax_loadContent(divID,'/_user/loader.php?module='+URL + '&timestamp=' + Stamp.getHours() + '' + Stamp.getMinutes() + '' +  Stamp.getSeconds());
}

  var http_request = false;
   function makePOSTRequest(url, parameters,fp) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      //http_request.onreadystatechange = alertContents;
      http_request.onreadystatechange = fp;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('middle').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
    function middleContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('panel_body').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   function mylogin(obj) {
      var poststr = "username=" + document.getElementById("username").value +
      				"&password=" + encodeURI( document.getElementById("password").value ) +
          			"&module=auth";
      makePOSTRequest('_user/loader.php', poststr,middleContents);
   }
   
   function get(obj) {
      var poststr = "cid=" + document.getElementById("cid").value +
      				"&cgroup=" + encodeURI( document.getElementById("cgroup").value ) +
      				"&cname=" + encodeURI( document.getElementById("cname").value ) +
                    "&ctext=" + encodeURI( document.getElementById("ctext").value );
      makePOSTRequest('updatepage.php', poststr,alertContents);
   }
   function goto_page(ex,pn,pid)
   {
   			Stamp = new Date();
   			la('pcont','getpage.php?e='+ ex + '&p=' +pid + '&pn=' +pn + '&stamp=' + Stamp.getHours() + '' + Stamp.getMinutes() + '' +  Stamp.getSeconds() );
   			document.getElementById('pageno').innerHTML='Page '+ pn;
   			document.getElementById('page' + currentpage).style.borderBottom="0";
   			document.getElementById('page' + pn).style.borderBottom="5px solid black";
   			currentpage=pn;
	}
	function goto_pager(ex,pn,pid)
   {
   			Stamp = new Date();
   			la('pcont','getpager.php?e='+ ex + '&p=' +pid + '&pn=' +pn + '&stamp=' + Stamp.getHours() + '' + Stamp.getMinutes() + '' +  Stamp.getSeconds() );
   			document.getElementById('pageno').innerHTML='Page '+ pn;
   			document.getElementById('page' + currentpage).style.borderBottom="0";
   			document.getElementById('page' + pn).style.borderBottom="5px solid black";
   			currentpage=pn;
	}
   function goto_sample(g)
   {
   			Stamp = new Date();
   			la('pcont','getpagews.php?g='+ g +'&stamp=' + Stamp.getHours() + '' + Stamp.getMinutes() + '' +  Stamp.getSeconds() );   			
	}

	function select_option(a,b,ex,qn)
	{
				document.getElementById('q' + a + '1').style.background='#FFF';
					document.getElementById('q' + a + '2').style.background='#FFF';
								document.getElementById('q' + a + '3').style.background='#FFF';
										document.getElementById('q' + a + '4').style.background='#FFF';
		document.getElementById('q' + a + '' + b).style.background='#DDD';
					document.getElementById('q' + a + '' + b).style.textDecoration='none';
		la('buffer','answer.php?e=' + ex + '&q=' + a + '&a=' + b + '&qn=' + qn);
		
	}
	function st(a,b)
	{
			if(document.getElementById('q' + a + '' + b).style.textDecoration=='line-through')
			{
				document.getElementById('q' + a + '' + b).style.textDecoration='none';
			}else
			{
				document.getElementById('q' + a + '' + b).style.textDecoration='line-through';
			}
	}
	
var timerID = 0;
var tStart  = null;
var duration = 0;
var wsample=0;
var wsample_page=1;

function UpdateTimer(d) {
   if(timerID) {
      clearTimeout(timerID);
      clockID  = 0;
   }

   if(!tStart)
      tStart   = new Date();

   var   tDate = new Date();
   var   tDiff = tDate.getTime() - tStart.getTime();

   tDate.setTime(tDiff);
 var m = tDate.getMinutes();
 var s = tDate.getSeconds();
 
 m = d - (m + 1);
 s = 60 - s;
 if(m==0 && s==1)
   {
	   	if(wsample==1)
	   	{
	   		if(wsample_page==1)
	   		{   			
	   			location='wsresults.php';
	   		}else
	   		{	   				
	   				goto_sample(currentpage,2);
	   		}   		   		
	   	}else
	   	{
			location='results.php';   			   			
	   	}   	
   }    
   
 if(m < 10)
 {
 	m = '0' + m ;
 }
 if(s < 10)
 {
 	s = '0' + s ;
 }

if(m==4 && s==60)
{
alert("You have 5 more minutes to finish this Writing Sample.");
}

 document.getElementById('timekeeper').innerHTML = "Time Left  " 
                                   + m + ":" 
                                   + s;
   timerID = setTimeout("UpdateTimer("+d+")", 1000);
}

function Start(a,b,c,d) {
	duration=d;
   tStart   = new Date();

   document.getElementById('timekeeper').innerHTML = "Time Left " + duration + ":00";

   timerID  = setTimeout("UpdateTimer("+d+")", 1000);
	if(mode==0){ 
	mode=1; 
	document.getElementById('startexam').innerHTML = "Finish (submit exam)";
	goto_page(a,b,c);
	}else { 
		location='results.php';	
	}
	
}

function Startws(g,num,d) {
	duration = d;
   tStart   = new Date();

      //document.getElementById('timekeeper').innerHTML = "Time Left " + duration + ":00";
	document.getElementById('timekeeper').innerHTML = "Time Left " + 30 + ":00";

   timerID  = setTimeout("UpdateTimer("+d+")", 1000);
	if(mode==0){ 
	mode=1; 
	document.getElementById('startexam').innerHTML = "";
		goto_sample(g);
	}else { 
		location='submitessay.php';	
	}
	
}

function Stop() {
   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
   }

   tStart = null;
}

function Reset() {
   tStart = null;

   document.getElementById('timekeeper').innerHTML = "Exam Duration  00:00";
}

	function gofullscreen(URL,num)
		{
			window1=window.open(URL,"FS"+num,"scrollbars=Yes,width=700,height=500,status=yes,fullscreen=yes").focus();
		}
		function gopopup(URL,num,w,h)
		{
			window1=window.open(URL,"FS"+num,"scrollbars=Yes,pageXoffset=0,pageYoffset=0,width="+w+",height="+h+",status=no").focus();
		}
		function goexams(num,dur,check)
	{	
		if (check!=4)
		{
			window1=window.open("http://www.mcat-prep.com/oldmcat/viewexam.php?examID="+num+"&duration="+dur,"examwindows"+num,"scrollbars=Yes,width=700,height=500,status=yes,fullscreen=yes").focus();
		}else
		{
			window1=window.open("http://www.mcat-prep.com/oldmcat/wsexam.php?examID="+num+"&duration="+dur,"examwindows"+num,"scrollbars=Yes,width=700,height=500,status=yes,fullscreen=yes").focus();
		}
	}
	function gocbt(examID,examtype,dur,g)
	{			
		document.getElementById('panel_body').innerHTML='';
		if(examtype==1)
		{
			la('panel_body','cbt.php?e='+examID + '&d=' + dur + '&g=' + g + '&examtype=' + examtype);
		}else
		{
			la('panel_body','wsample.php?e='+examID + '&d=' + dur + '&g=' + g + '&examtype=' + examtype);
				
		}
		
		initSlideLeftPanel(false,'dhtmlgoodies_leftPanel');	
		check_selection();	
	}
	function gocbtr(examID,examtype,dur,g)
	{			
		
			la('panel_body','cbtr.php?e='+examID + '&d=' + dur + '&g=' + g + '&examtype=' + examtype);		
			initSlideLeftPanel(false,'dhtmlgoodies_leftPanel');	
			//check_selection();	
	}
	function gocbtws(examID,nextexam,dur,g,et)
	{
		//alert("Found");
		var nxt=et;
		window.location="http://www.mcat-prep.com/_members/wsample.php?e="+examID +"&d="+examID +"&g="+g+"&examtype="+nxt+"&nextexam="+nextexam;
		//document.getElementById('panel_body').innerHTML='';
		
			//la('panel_body','wsample.php?e='+examID+'&d='+dur+'&g='+g+'&nextexam='+nextexam);								
		//initSlideLeftPanel(false,'dhtmlgoodies_leftPanel');
		//alert("This feature is under testing");		
	}

	function loadinparent(url, closeSelf)
	{
	self.opener.location = url;
	if(closeSelf) self.close();
	}

	function load_exams(num)
	{	
		la('lexams','listexams.php?examID='+num);
		document.getElementById('lpages').innerHTML='';
		document.getElementById('lquestions').innerHTML='';
	}
	function load_pages(num)
	{	
		la('lpages','listpages.php?examID='+num);		
		document.getElementById('lquestions').innerHTML='';
		
	}
	function load_questions(num)
	{	
		la('lquestions','listquestion.php?pageno='+num);
		
	}
	
	function submit_essay()
	{		
		//essay =  '<b>Question</b><br/>' + encodeURI(document.getElementById('myquestion').value)+ '<b>Student's Response:</b><br/>'+ encodeURI(document.getElementById('essaybox').value);
		//alert(document.getElementById('essaybox').value);	
		//var essay =encodeURI(document.getElementById('essaybox').value);
		document.getElementById('essay_b').value = document.getElementById('essaybox').value;

		var essaystr = document.getElementById('essaybox').value;
		
//confirmation
		if(essaystr.length > 8000)
		{	
		var answer = confirm("The essay that you have submitted is too large in size. For he security reason the eassy will crop to the maximum size. Do you want to continue?")
			if(answer)
			{
				document.essayform.submit();
			}
			
		}
		else
		{
		document.essayform.submit();
		}
//alert(document.essayform.essay_b.value);
// 		alert(document.getElementById('essay_b').value);
		
    		//location = 'submitessay.php?essay=' + essay;
				
	}

	function show_pt()
	{
		win=window.open("http://www.mcat-prep.com/admin/images/pTable.gif","pt","scrollbars=Yes,width=700,height=500,status=yes").focus();
	}
	function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
	function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
	var down=0;
	var lastselection="";
	function check_selection()
	{
	   var sel1 = window.getSelection?window.getSelection():document.getSelection?document.getSelection():document.selection.createRange().text;
	  if(sel1!='')	  
	  {	
	  		/*if(lastselection!=window.getSelection())
	  		{
	  			overlib(window.getSelection(),FIXX, 400,FIXY, 200,BGCOLOR,'white',FGCOLOR,'YELLOW',STICKY);
	  		}*/
	  		lastselection= sel1 + '';
	  }else{
	  	if(lastselection!='')
	  	{
	  		highlight(lastselection);
		}
	 	//nd(); 
	}	  	
	  //lastsearch=window.getSelection();		
	  setTimeout("check_selection()", 500);	
	}
