// JavaScript Document


function rootFolder()
{
	var hrefVal = document.location.href;
	var finalIndex = hrefVal.indexOf("lessons");
	var retVal = hrefVal.substring(0, finalIndex);
	return retVal;
}

function ShowFullScreen(flashfilename, flashFrame, disableHelpButton)
{
		var mainpath = rootFolder();
		var path=document.location.href;
		var indexIndex = path.indexOf('index.html');
		if (indexIndex < 0)
			lastIndex = path.length;
		else
			lastIndex = indexIndex;
		var lessonIndex = path.indexOf('lessons');
		var PathFromLesson = path.substring(lessonIndex-1, lastIndex);
		addr=mainpath + "lessons/" + "flashfullscreen.html?flashFrame=" + flashFrame + "&flashFileName=" + flashfilename + "&flashPath=" + PathFromLesson + "&disableHelpButton=" + disableHelpButton;
		mywin = window.open(addr,'MathBuddyOnline',"HEIGHT="+screen.availHeight+",WIDTH="+screen.availWidth+",scrollbars=yes,toolbar=no,statusbar=no,location=0");
		mywin.moveTo(0,0);
}

function ShowQuizWindow(flashfilename)
{
		var mainpath = rootFolder();
		var path=document.location.href;
		var indexIndex = path.indexOf('index.html');
		if (indexIndex < 0)
			lastIndex = path.length;
		else
			lastIndex = indexIndex;
		var lessonIndex = path.indexOf('lessons');
		var PathFromLesson = path.substring(lessonIndex-1, lastIndex);
		var dotIndex = flashfilename.indexOf('.');
		var tempfileName = flashfilename.substring(0, dotIndex);
		var quizFileName = tempfileName + ".xml";
		addr = 	mainpath + "lessons/quiz.html?file=" + quizFileName + "&path=" + pathFromLesson;
		window.open(addr,'',"location=0,HEIGHT=800,WIDTH=600,scrollbars=yes,toolbar=no,statusbar=no,location=0");
}

function InvokeSWF(fileName, width, height, tagName, isInteractive)
{
	var numWidth = Number(width);
	var numHeight = Number(height);
	
	if (isInteractive)
	{
		width = String(numWidth+20);
		height = String(numHeight+20);
	}
	else
	{
		width = String(numWidth+10);
		if ((numWidth == 300) && (numHeight == 200)) //special case
			height = String(numHeight+40);		
		else if (numHeight < 250)
			height = String(numHeight+30);
		else if (numHeight < 350)
			height = String(numHeight+40);
		else
			height = String(numHeight+50);
	}
	
	var mainpath = rootFolder();
	if (isInteractive)
		loaderFileName = mainpath + "lessons/flash/BrownPlainRect620x440.swf";
	else
	{
		var loaderFileName = mainpath + "lessons/flash/FlashLoad"+numWidth+"x"+numHeight;
		loaderFileName += ".swf";	
	}

	
	var so = new SWFObject(loaderFileName,"sotester", width, height, "8", "#FF6600");
	so.addParam("base", "media/flash");
	
	so.addVariable("flashFileName",fileName);
	so.addVariable("licenseKey","ABCDEF");

	so.useExpressInstall(mainpath+'scripts/expressinstall.swf');
	so.addParam("wmode", "transparent");
 	so.addParam("quality", "high");
	so.write(tagName);
}


function InvokeSWFWFrame(frameName, frameWidth, frameHeight, flashFileName, tagName, disableHelpButton)
{	
	GetUserName();
	RetrieveScore(frameName, frameWidth, frameHeight, flashFileName, tagName, disableHelpButton);
}

function RetrieveScore(frameName, frameWidth, frameHeight, flashFileName, tagName, disableHelpButton)
{
	$("#success").load("/server/score.php?what=1", function(){
		InvokeSWFWFrame1(frameName, frameWidth, frameHeight, flashFileName, tagName, disableHelpButton);
		});	
}

function InvokeSWFWFrame1(frameName, frameWidth, frameHeight, flashFileName, tagName, disableHelpButton)
{
	var mainpath = rootFolder();
	var loaderFileName = mainpath + "lessons/flash/"+frameName;

	var so = new SWFObject(loaderFileName,"sotester", frameWidth, frameHeight, "8", "#FFFFFF");
	so.addParam("base", "media/flash");
	
	so.addVariable("flashFileName",flashFileName);

	so.addVariable("flashFrame", frameName);
	so.addVariable("disableHelpButton", disableHelpButton);
	so.addVariable("licenseKey","ABCDEF");
	
	var isLoggedIn = document.getElementById('session').innerHTML;
	so.addVariable("isLoggedIn", isLoggedIn);
	so.useExpressInstall(mainpath+'scripts/expressinstall.swf');
	
	so.addVariable("userName", document.getElementById("pageid").innerHTML);
	so.addVariable("dbScore", document.getElementById("success").innerHTML);

	so.addVariable("msgSoundLoc",mainpath+'media/sound/');
	so.addParam("wmode", "transparent");
 	so.addParam("quality", "high");
	so.write(tagName);
}

function InvokeSWFFullScreen(frameName, frameWidth, frameHeight, flashFileName, flashFilePath, tagName, disableHelpButton)
{	
	GetUserName();
	RetrieveScoreFullScreen(frameName, frameWidth, frameHeight, flashFileName, flashFilePath, tagName, disableHelpButton);
}

function RetrieveScoreFullScreen(frameName, frameWidth, frameHeight, flashFileName, flashFilePath, tagName, disableHelpButton)
{
	$("#success").load("/server/score.php?what=1", function(){
		InvokeSWFFullScreen1(frameName, frameWidth, frameHeight, flashFileName, flashFilePath, tagName, disableHelpButton);
		});	
}

function InvokeSWFFullScreen1(frameName, frameWidth, frameHeight, flashFileName, flashFilePath, tagName, disableHelpButton)
{	
	var mainpath = rootFolder();
	var loaderFileName = mainpath + "lessons/flash/"+frameName;

	var so = new SWFObject(loaderFileName,"sotester", frameWidth, frameHeight, "8", "#f4d9ac");
	so.addParam("base", flashFilePath+"media/flash");
	so.addVariable("flashFileName",flashFileName);

	so.addVariable("disableFS", "true");
	so.addVariable("flashFrame", frameName);
	so.addVariable("licenseKey","ABCDEF");
	so.addVariable("disableHelpButton", disableHelpButton);

	var isLoggedIn = document.getElementById('session').innerHTML;
	so.addVariable("isLoggedIn", isLoggedIn);

	so.addVariable("userName", document.getElementById("pageid").innerHTML);
	so.addVariable("dbScore", document.getElementById("success").innerHTML);
	so.addVariable("msgSoundLoc",mainpath+'media/sound/');

	//alert(practiceId);
	so.useExpressInstall(mainpath+'scripts/expressinstall.swf');
	//	so.addParam("wmode", "transparent");
 	so.addParam("quality", "high");
	so.write(tagName);
}

function InvokeSWFWFrameNotTransparent(frameName, frameWidth, frameHeight, flashFileName, tagName, disableHelpButton)
{	
	GetUserName();
	RetrieveScoreTransparent(frameName, frameWidth, frameHeight, flashFileName, tagName, disableHelpButton);
}

function RetrieveScoreTransparent(frameName, frameWidth, frameHeight, flashFileName, tagName, disableHelpButton)
{
	$("#success").load("/server/score.php?what=1", function(){
		InvokeSWFWFrameNotTransparent1(frameName, frameWidth, frameHeight, flashFileName, tagName, disableHelpButton);
		});	
}

function InvokeSWFWFrameNotTransparent1(frameName, frameWidth, frameHeight, flashFileName, tagName, disableHelpButton)
{	
	var mainpath = rootFolder();
	var loaderFileName = mainpath + "lessons/flash/"+frameName;

	var so = new SWFObject(loaderFileName,"sotester", frameWidth, frameHeight, "8", "#DCF0FD");
	so.addParam("base", "media/flash");
	
	so.addVariable("flashFileName",flashFileName);

	so.addVariable("flashFrame", frameName);
	so.addVariable("disableHelpButton", disableHelpButton);
	so.addVariable("licenseKey","ABCDEF");
	
	var isLoggedIn = document.getElementById('session').innerHTML;
	so.addVariable("isLoggedIn", isLoggedIn);
	so.useExpressInstall(mainpath+'scripts/expressinstall.swf');

	so.addVariable("userName", document.getElementById("pageid").innerHTML);
	so.addVariable("dbScore", document.getElementById("success").innerHTML);

	so.addVariable("msgSoundLoc",mainpath+'media/sound/');
 	so.addParam("quality", "high");
	so.write(tagName);
}

function InvokePracticeSheetSWF(frameName, frameWidth, frameHeight, flashFileName, flashFilePath, tagName, practiceId)
{	
	var mainpath = rootFolder();
	var loaderFileName = mainpath + "lessons/flash/"+frameName;

	var so = new SWFObject(loaderFileName,"sotester", frameWidth, frameHeight, "8", "#f4d9ac");
	so.addParam("base", flashFilePath+"media/flash");
	so.addVariable("flashFileName",flashFileName);

	so.addVariable("disableFS", "true");
	so.addVariable("flashFrame", frameName);
	so.addVariable("licenseKey","ABCDEF");

	if (practiceId)
		so.addVariable("practiceId", practiceId);

	so.addVariable("msgSoundLoc",mainpath+'media/sound/');

	so.useExpressInstall(mainpath+'scripts/expressinstall.swf');
 	so.addParam("quality", "high");
	so.write(tagName);
}

function InvokeSWFNoFrame(fileName, width, height, tagName)
{
	var mainpath = rootFolder();

	var so = new SWFObject("media/flash/"+fileName,"sotester", width, height, "8", "#FF6600");
	so.addParam("base", "media/flash");
	so.addVariable("licenseKey","ABCDEF");

	so.useExpressInstall(mainpath+'scripts/expressinstall.swf');
//	so.addParam("wmode", "transparent");
 	so.addParam("quality", "high");
	so.write(tagName);
}

function InvokeSWFNoFrameFullScreen(fileName, flashFilePath, width, height, tagName)
{
	var mainpath = rootFolder();

	var so = new SWFObject("media/flash/"+fileName,"sotester", width, height, "8", "#FF6600");
	so.addParam("base", flashFilePath+"media/flash");

	so.useExpressInstall(mainpath+'scripts/expressinstall.swf');
	so.addVariable("disableFS", "true");
	so.addVariable("flashFrame", frameName);
//	so.addParam("wmode", "transparent");
 	so.addParam("quality", "high");
	so.write(tagName);
}

// JavaScript Document

function GetUserName()
{
	$("#pageid").load("/server/score.php?what=3");	
}

function updateScore(incrScore)
{
	$("#success").load("/server/score.php?what=2&incrScore="+incrScore);
}

function debugScore(posText, cookieScore, dbScore)
{
	alert(posText + " cookiescore = " + cookieScore + " and dbScore = " + dbScore);
}


function doesPageExist(pageId)
{
	$("#pageid").load("/server/utility.php?what=15&name="+pageId, CheckPageExist);	
}

function CheckPageExist()
{
	if(document.getElementById("pageid").innerHTML == 0)//free lesson
	{		
		document.getElementById("logged").style.display="block";
		$("#session").load("/server/session.php?type=2",showBlock);	
	}
	else
	{
		isSessionActive(5);
	}
}

function showBlock()
{
	if (document.getElementById("showBlock"))
	{
		if (document.getElementById("session").innerHTML != 1) 	
		{
			document.getElementById("showBlock").style.display = "block";
			$.get("/server/utility.php?what=3", function(data){
  				if( data == 0)				
					document.getElementById("showBlock1").innerHTML = "Sign up for a Free Trial Now!";
				else
					document.getElementById("showBlock1").innerHTML = "Sign up now for a very affordable price!";
			});
		}
	}
}

function qs() 
{
		var qsParm = new Array();
		qsParm['page'] = null;
		var query = window.location.search.substring(1);
		var parms = query.split('&');
		for (var i=0; i<parms.length; i++) 
		{
			var pos = parms[i].indexOf('=');
			if (pos > 0) 
			{
				var key = parms[i].substring(0,pos);
				var val = parms[i].substring(pos+1);
				qsParm[key] = val;
				if (key == 'page')
					document.getElementById("eid").value = val;
			}
    	}
} 

function isSessionActive(isOnLoad)
{
	$("#session").load("/server/session.php?type=2" , function(){ checkResponse(isOnLoad);});	
}

function checkResponse(isOnLoad)
{
	if (isOnLoad == 1)  ///to check session on myAcademy pages
	{			 
	 	var isLoggedIn = document.getElementById('session').innerHTML;
	
		if (isLoggedIn == 0)///////////not logged in
		 {
			document.location.href = "/common/login.html";	 	
		 }
		 else
		 {
			 $("#expireid").load("/server/utility.php?what=9", canLogin);	
		 }
		
	}	
	else if (isOnLoad == 2 || isOnLoad == 4) //quiz = 2 and questions = 4
	{
	 	var isLoggedIn = document.getElementById('session').innerHTML;
		
		if (isLoggedIn != 1)
		{
			var fullpath=document.location.href;
			var ind=fullpath.indexOf("lessons",1);
			var path = fullpath.substring(ind + 8,fullpath.length);
			document.location.href = "/common/login.html?path=" + path;	 	
		}
		else
		{
			//check if they are here for practice or for assessment.
			//'setid should come from the query string. If not defined, it is not a valid worksheet	
			$("#type").load("/server/utility.php?what=21&setid=" +setId , function(){ checkAssessment(isOnLoad);});			
		}
	}
	else if (isOnLoad == 3)  ///for all index pages and other common pages 
	{			 
	 	var isLoggedIn = document.getElementById('session').innerHTML;
	
		if (isLoggedIn == 1)///////////user is logged in
		 {	
			document.getElementById("dispLogin").innerHTML = "<a href='#' onClick = 'logout()'>Logout</a>";
			$("#uname").load("/server/utility.php?what=1",displayUser);	
			document.getElementById("logged").style.display = "block";
			return 1;
		 }
	}
	else if (isOnLoad == 5) //index pages for all lessons.
	{
	 	var isLoggedIn = document.getElementById('session').innerHTML;
		
		if (isLoggedIn != 1)
		{
			var fullpath=document.location.href;
			var ind=fullpath.indexOf("lessons",1);
			var path = fullpath.substring(ind + 8,fullpath.length);
			document.location.href = "/common/login.html?path=" + path;	 	
		}
		else
			document.getElementById("logged").style.display = "block";
	}
	else   //////on click of logout
	{
		var isLoggedOut = document.getElementById('session').innerHTML;
	 	if (isLoggedOut == 1)
 		{
			window.location.href = "/common/login.html";		
		}
	}
}

function canLogin()
{
	if(document.getElementById("expireid").innerHTML == 0) //////can access
	{
		document.getElementById("logged").style.display = "block";	
		document.getElementById("dispLogin").innerHTML = "<a href='#' onClick = 'logout()'>Logout</a>";
		$("#uname").load("/server/utility.php?what=1",displayUser);	
		return 0;
	}
	else
		window.location.href = "/common/upgrade.html";
}

function checkAssessment(isOnLoad)
{
	var temp = document.getElementById("type").innerHTML;

	res = temp.split(",");
	
	result = res[0];

	if (isOnLoad == 2) //quiz
	{
		
		if ( result == 1000)
		{
			document.getElementById("content").style.display = "block";
			document.getElementById("content").innerHTML = "<table width='100%' border='0' cellspacing='0' cellpadding='0' bgcolor='#F0F7FD'><tr><td width='180' height='341' align='right' valign='bottom' style='background-repeat:no-repeat; background-position:center;'><img src='/quiz/images/boy_confused.gif'/></td><td style='padding-top:1.7em; padding-right:1.5em;'valign='top'><img src='/quiz/images/green-board_invalid.gif' /></tr></table>";
			document.getElementById("content").style.display = "block";	
		}	
		else if (result == 0)
		{
			document.getElementById("content").style.display = "block";
	
		}
		else if (result == 1)
		{	
			document.getElementById("content").style.display = "block";
			document.getElementById("content").innerHTML="<table width='100%' border='0' cellspacing='0' cellpadding='0' bgcolor='#F0F7FD'><tr><td width='140' height='324' align='right' valign='bottom' style='background-repeat:no-repeat; background-position:center;'><img src='/quiz/images/professor.gif'/></td><td width='557' style='background-image:url(/quiz/images/green-board_blank.gif); background-repeat:no-repeat; background-position:left;' class='text_inside_two'><p>Please note that the scores of this assessment will not be <br />recorded as you have already attempted it 2 times.<br /><br />Choose a difficulty Level:</p><p align='left' style='padding-left:3em;'><label><input type='radio' name='RadioGroup1' value='radio' id='easyid' checked='checked'/>Easy</label><br /><br /><label><input type='radio' name='RadioGroup1' id='mediumid' value='radio' />Medium</label><br /><br /><label><input type='radio' name='RadioGroup1' value='radio' id='diffid' />Difficult</label><br /></p></tr><tr><td>&nbsp;</td></tr></table>";
		}	
		else if (result == 2)//worksheet not listed for the user's grade
		{
				document.getElementById("content").style.display = "block";
		document.getElementById("content").innerHTML = "<table width='100%' border='0' cellspacing='0' cellpadding='0' bgcolor='#F0F7FD'><tr><td width='180' height='341' align='right' valign='bottom' style='background-repeat:no-repeat; background-position:center;'><img src='/quiz/images/boy_happy.gif'/></td><td style='padding-top:1.7em; padding-right:1.5em;'valign='top'><img src='/quiz/images/green-board_not_listed.gif' /></tr></table>";
		
			document.getElementById("content").style.display = "block";	
		}	

	}
	else if (isOnLoad == 4) //questions
	{		
		if (result == 1000)
		{
			alert("yes");
			document.getElementById("content").innerHTML = "Not a valid worksheet..";
		}
	else if (isOnLoad == 4) //questions
	{		
		document.getElementById("questionid").className = "sub_question";
		if (result == 1000)
			document.getElementById("questionid").innerHTML = "Not a valid worksheet..";
		else if (result == 2)					 
			document.getElementById("questionid").innerHTML = "This worksheet is not listed for your grade. You may not be able to take it.";			
		else
		{
			loadQuestions(); //is a function in quiz.js. Cannot be used for anything else.
		}
		
		if ((result == 1000)||(result == 2))
		{
			document.getElementById("previd").className = "previous_deactivate";
			document.getElementById("prev1id").className = "previous_deactivate";
			document.getElementById("nextid").className = "Next_deactivate";
			document.getElementById("next1id").className = "Next_deactivate";
			document.getElementById("endtestid").className = "end_deactivate";
			document.getElementById("endtest1id").className = "end_deactivate";
		}

	
	}
	}
}

function displayUser()
{
   document.getElementById("uname").innerHTML = "Welcome  " + document.getElementById("uname").innerHTML;
}

function logout()
{
	$("#session").load("/server/logout.php?logout=1", function(){ checkResponse(0); });		
}

function checkEmailId()
{
	var pemail = document.getElementById("email").value;
	if(IsEmail(pemail)=="1")
	{
		document.getElementById("errMsg").className = "redFont";
	}
	else
	{
		document.getElementById("errMsg").className = "whiteFont";
		$("#getmailid").load("/server/utility.php?what=29&pemail=" + pemail,checkEmailInserted);
	}
}

function checkEmailInserted()
{
	if(document.getElementById("getmailid").innerHTML == 0)
		document.location.href = "/common/newsletterThankyou.html";
	else
	{
		document.getElementById("errMsg").style.display = "block";
		document.getElementById("errMsg").className = "redFont";
		document.getElementById("errMsg").innerHTML = "Email id is not inserted";
	}
}


function IsEmail(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1){  return 1  }

	if (str.indexOf(at)=="-1" || str.indexOf(at)=="0" || str.indexOf(at)==lstr)
	{  return 1  }
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{  return 1  }
	
	 if (str.indexOf(at,(lat+1))!=-1){  return 1  }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	 {  return 1  }

	 if (str.indexOf(dot,(lat+2))==-1){  return 1  }
	
	 if (str.indexOf(" ")!=-1)
	 {  return 1  }

		
}