function clickThis(obj) {
	var oRow = document.getElementById("tabBarRow");
	for(var td = 0; td < oRow.cells.length; td++) {
		oRow.cells[td].className = 'tabOff';
	}
	if(obj) obj.className = 'tabOn';
}

function content(cLabel,cURL,cWidth,cHeight,cType,cGroup,cCID) {
	this.cLabel 	= cLabel;
	this.cURL 		= cURL;
	this.cWidth 	= cWidth;
	this.cHeight 	= cHeight;
	this.cType	 	= cType;
	this.cGroup		= cGroup;
	this.cCID		= cCID;
}

function previewQuiz(ques) {
	var obj = document.getElementById('tabQuiz');
	clickThis(obj);
	buildIFrame('quiz_ques.asp?mid=' + iMID + '&user_id=0&preview=yes&ques_num=' + ques,'auto',sFullWidth,sFullHeight,false);
}

function selectWelcome() {
	clickThis(null);
	document.getElementById('contentNavTable').style.visibility = 'hidden';
	buildIFrame('quiz_welcome.asp?mid=' + iMID + '&user_id=' + iUserID,'auto',sFullWidth,sFullHeight,false);
}

function selectLogin() {
	clickThis(null);
	document.getElementById('contentNavTable').style.visibility = 'hidden';
	buildIFrame('quiz_login.asp?mid=' + iMID + '&user_id=' + iUserID,'auto',sFullWidth,sFullHeight,false);
}

function selectTab(tabName) {
	if(!checkQuizLogin()) return null;
	
	if(tabName == 'Lesson') {
		clickThis(document.getElementById('tabLesson'));
		document.getElementById('contentNavTable').style.visibility = '';
		contentNav(iCurContent);
	} else if(tabName == 'Quiz' && !bPreview) {
		if(checkContentStatus()) {
			clickThis(document.getElementById('tabQuiz'));
			document.getElementById('contentNavTable').style.visibility = 'hidden';
			buildIFrame('quiz_ques.asp?mid=' + iMID + '&user_id=' + iUserID,'auto',sFullWidth,sFullHeight,false);
		} else {
			alert('Please review all content before taking the quiz.');
		}
	} else if(tabName == 'Quiz' && bPreview) {
		clickThis(document.getElementById('tabQuiz'));
		document.getElementById('contentNavTable').style.visibility = 'hidden';
		buildIFrame('quiz_ques.asp?mid=' + iMID + '&user_id=0&preview=yes&ques_num=1','auto',sFullWidth,sFullHeight,false);
	}	
}

function contentNav(dir) {
	if(dir == 'back' && iCurContent == 0) {
		alert('You are at the beginning.');
		return null;
	} else if(dir == 'next' && iCurContent == arrContent.length-1) {
		//alert('You are at the end.');
		buildTOC();
		return null;
	} else if(dir == 'toc' || dir.toString() == '0') {
		buildTOC();
		return null;
	} else if(dir == 'back') {
		iCurContent -= 1
		if(iCurContent == 0) {
			buildTOC();	
			return null;
		}			
	} else if(dir == 'next') {
		iCurContent += 1
	} else if(dir) {
		iCurContent = dir;
	}
		
	document.getElementById('contentNavPage').innerHTML = '<span class=\"subNavColor\">Page ' + iCurContent + ' of ' + (arrContent.length-1) + ':</span>&nbsp;&nbsp;&nbsp;';
	if(arrContent[iCurContent].cGroup != '') {
		document.getElementById('contentNavPage').innerHTML += arrContent[iCurContent].cGroup;
	 } else {
	 	document.getElementById('contentNavPage').innerHTML += arrContent[iCurContent].cLabel;
	}
	
	if(arrContent[iCurContent].cWidth > 0 && arrContent[iCurContent].cHeight > 0) {
		buildIFrame(arrContent[iCurContent].cURL,'no',arrContent[iCurContent].cWidth + 'px',arrContent[iCurContent].cHeight + 'px',true);
	} else {
		buildIFrame(arrContent[iCurContent].cURL,'auto',sFullWidth,sFullHeight,false);
	}

	saveViewArray(arrContent[iCurContent].cCID);
}

function buildIFrame(u,s,w,h,b) {
	var tmpHTML = '<div ';
	if(b) {
		tmpHTML += 'style=\"background-color:#FFFFFF; width:' + w + '; height:' + h + '; ';
		tmpHTML += 'border-top:1px solid #CCCCCC; ';
		tmpHTML += 'border-left:1px solid #CCCCCC; ';
		tmpHTML += 'border-bottom:1px solid #999999; ';
		tmpHTML += 'border-right:1px solid #999999; ';
		tmpHTML += '\">';
	} else {
		tmpHTML += 'style=\"background-color:#FFFFFF; width:' + w + '; height:' + h + ';\">';
	}
	tmpHTML += '<iframe name=\"contentFrame\" id=\"contentFrame\" width="' + w + '" height="' + h + '" frameborder="0" scrolling="' + s + '"></iframe>';	
	tmpHTML += '</div>';
	document.getElementById('contentCell').innerHTML = tmpHTML;
	document.getElementById('contentFrame').src = u;
}

function checkQuizLogin() {
	if(iUserID != 0 || bPreview) {
		return true;
	} else {
		alert('Please login.');
		return false;
	}
}

function setUserInfo(id,viewarray) {
	if(id != 0 && iUserID == 0) {
		iUserID = id;
		document.getElementById('footerLinkGroup').style.visibility = '';
		strContentViewed = updateViewArray(viewarray);
	}
}

function updateViewArray(viewarray) {
	if(viewarray.indexOf(',') != -1) {
		var strTmp = '';
		var arrTmp = viewarray.split(',');
		
		for(var c = 0; c < arrTmp.length; c++)
			if(arrTmp[c] == 1 && arrContent[c]) strTmp += arrContent[c].cCID + '|';
		strTmp = strTmp.substr(0, strTmp.length-1);
		
		var objImg = document.getElementById('imgTransmit');
		objImg.src = 'quiz_content_viewed.asp?user_id=' + iUserID + '&viewed=' + escape(strTmp);		
		
		return strTmp;
	} else
		return viewarray;
}

function saveViewArray(cid) {
	if(('|' + strContentViewed + '|').indexOf('|' + cid + '|') == -1) {
		if(strContentViewed.length == 0)
			strContentViewed = cid;	
		else
			strContentViewed += '|' + cid;
				
		var objImg = document.getElementById('imgTransmit');
		objImg.src = 'quiz_content_viewed.asp?user_id=' + iUserID + '&viewed=' + escape(strContentViewed);
	}
}

function checkContentStatus() {
	if(!blnViewAll) {
		return true;
	} else {
		var blnComplete = true;
		for(var c = 0; c < arrContent.length; c++) {
			if(('|' + strContentViewed + '|').indexOf('|' + arrContent[c].cCID + '|') == -1) {
				blnComplete = false;
				break;
			}
		}
		return blnComplete;
	}
}

function buildTOCList() {
	var sCurGroup = '';
	var oMainUL = document.createElement('ul');
	var oLI;
	var oSubUL;
	var sLink;
	var cidIndex;

	for(var c = 1; c < arrContent.length; c++) {
		sLink = '<a href=\"#\" onClick=\"contentNav(' + c + '); return false;\">' + arrContent[c].cLabel + '</a> (' + arrContent[c].cType + ')';	
		cidIndex = ('|' + strContentViewed + '|').indexOf('|' + arrContent[c].cCID + '|');
		if(arrContent[c].cGroup == '') {
			oLI = document.createElement('li');
			oLI.innerHTML = sLink;
			if(cidIndex != -1) {
				oLI.className = 'checkedLI';
			} else oLI.className = 'uncheckedLI';
			oMainUL.appendChild(oLI);
		} else if(arrContent[c].cGroup != sCurGroup) {
			oLI = document.createElement('li');
			oMainUL.appendChild(oLI);
			oLI.innerHTML = '<strong>' + arrContent[c].cGroup + '</strong>';
			oSubUL = document.createElement('ul');
			oLI.appendChild(oSubUL);
			oLI = document.createElement('li');
			oLI.innerHTML = sLink;
			if(cidIndex != -1) {
				oLI.className = 'checkedLI';
			} else oLI.className = 'uncheckedLI';
			oSubUL.appendChild(oLI);
		} else {
			oLI = document.createElement('li');
			oLI.innerHTML = sLink;
			if(cidIndex != -1) {
				oLI.className = 'checkedLI';
			} else oLI.className = 'uncheckedLI';
			oSubUL.appendChild(oLI);			
		}
		sCurGroup = arrContent[c].cGroup;
	}
	return oMainUL;
}


// ************** Scripts to resize & center ************************ //

window.onload = resizeAndCenter;

function resizeAndCenter() {
	if(window.opener != null) {
		var targetWidth = 780;
		var targetHeight = 580;
		
		// Turn off main page scroll bar.
		var oBody = document.getElementsByTagName("body")[0];
		oBody.style.overflow = 'hidden';
		
		/*
		// Check sizes and resize window accordingly.
		var arrWinSizes = checkWindowSizes();
		var iDiffWidth = (targetWidth - arrWinSizes[0]) + 2;
		var iDiffHeight = (targetHeight - arrWinSizes[1]) + 2;
		window.resizeBy(iDiffWidth, iDiffHeight);
		*/
			
		// Check window size and center on screen.
		var scrWidth = self.screen.width;
		var scrHeight = self.screen.height;
		var arrWinSizes = checkWindowSizes();
		var newX = Math.round((scrWidth - arrWinSizes[0] - 10)/2);
		var newY = Math.round((scrHeight - arrWinSizes[1] - 50)/2);
		window.moveTo(newX,newY);
	}
}

function checkWindowSizes() {
	var x,y;
	if (self.innerWidth) {
		// all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
		
	} else if(document.documentElement && document.documentElement.clientWidth) {
		// Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
		
	} else if(document.body) {
		// other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
		
	} else {
		x = intWinWidth;
		y = intWinHeight;
	}
	
	return [x,y];
}



