// Global Variables
// ------------------------------------------------------------------------------------------
picNum 		= 0;		// Stores the number of pictures to be used for the slideshow
slideSpeed  = 3000; 	// Stores the slide changing speed
desPath     = "";   	// Stores the path of an image and/or element
fadePage    = "";		// Stores the name of the page that uses the Fade effect
imgCounter  = 0;		// Stores the number of pictures to be used for the Fade effect
swapCounter = 2;		// Controls the picture swapping from background to div for Fade effect
preloadChk  = 1;		// Flags the pre-loading of images to be used in the Fade effect

score_total_attainable = 0;	// Stores the total attainable score for an exercise
score_total_achieved = 0;	// Stores the total achieved score for an exercise

// Parameter1: Element ID
// Function used to hide an element.
// ---------------------------------------------------------------------
function invisible(id)
{
	document.getElementById(id).style.visibility="hidden";
}

// Parameter1: Element ID
// Function used to reveal an element.
// ---------------------------------------------------------------------
function visible(id)
{
	document.getElementById(id).style.visibility="visible";
}

// Purpose: Displays the element passed as a parameter.
// ---------------------------------------------------------------------
function genericShow(id)
{
	document.getElementById(id).style.display="block";
}

// Purpose: Displays the element passed as a parameter.
// ---------------------------------------------------------------------
function genericShowInline(id)
{
	document.getElementById(id).style.display="inline";
}

// Purpose: Hides the element passed as a parameter.
// ---------------------------------------------------------------------
function genericHide(id)
{
	document.getElementById(id).style.display="none";
}

// Function that handles confirmation prompt messages.
// ---------------------------------------------------------------------
function confirmBox(msg)
{
	var r=confirm(msg);
	if(r==false) return false;
	return true;
}

// Handles the form submition to display editable events
// ---------------------------------------------------------------------
function submitFrm(id)
{
	document.getElementById(id).submit();
}

// Filters a table-list based on what is entered in an input field.
// ---------------------------------------------------------------------
function filter(term, _id, cellNr)
{
	var suche = term.value.toLowerCase();
	var table = document.getElementById(_id);
	var ele;
	var recordCnt = 0;
	
	for(var r = 1; r < table.rows.length; r++)
	{
		ele = table.rows[r].cells[cellNr].innerHTML.replace(/<[^>]+>/g,"");
		if(ele.toLowerCase().indexOf(suche)>=0 )
		{
			table.rows[r].style.display = '';
			recordCnt++;
		}else{
			table.rows[r].style.display = 'none';
		}
	}
	document.getElementById("filtCount").innerHTML = recordCnt;
}

// Purpose: Diplays the selected answer box for an FAQ.
// ---------------------------------------------------------------------
function showQuestion(k,id,qCount)
{	
	for(var i=1;i<=qCount;i++) 
	{
		genericHide('q'+i);
		document.getElementById('qq'+ i).style.fontWeight="normal";
	}
	document.getElementById(k).style.fontWeight="bold";
	genericShow(id);
}

// Purpose: Calls the appropriate correction function.
// ---------------------------------------------------------------------
function validateAnswers(type,topic)
{
	fillAnswerArray(type,topic);
	switch(type)
	{
		case 'gapFill':
			correctGapFill(topic);
			break;
		default:
			break;
	}
	calculateScore();
}

// Purpose: Calculates the final score for an exercise.
// ---------------------------------------------------------------------
function calculateScore()
{
	var str;
	genericShowInline('msgBox');
	var percentage = Math.floor(score_total_achieved/score_total_attainable * 100);
	document.getElementById('score').innerHTML = score_total_achieved + '/' + score_total_attainable + ' = ' + percentage + '%';
	if(percentage>=0 && percentage<60) str = 'Revision is required. The Information tutorial covering this topic might be helpful.';
	if(percentage>=60 && percentage<70) str = 'Re-doing this exercise might prove helpful in understanding the topic better.';
	if(percentage>=70 && percentage<85) str = 'Good Work! Doing more exercises relating to this topic will be helpful.';
    if(percentage>=85 && percentage<99) str = 'Great Work! Your understanding of the topic is nearly flawless.';
	if(percentage==100) str = 'Outstanding! A Perfect Score!';
	document.getElementById('scoreMsg').innerHTML = str;
}

// Purpose: Corrects Gap-Fill Exercises.
// ---------------------------------------------------------------------
function correctGapFill(topic)
{
	var str;
	var id;
	for(var i=1;i<=score_total_attainable;i++)
	{
		id  = 'txtFld'+i;
		str = document.getElementById(id).value;
		str = str.toLowerCase();
		if(str!=answerArr[i])
		{
			--score_total_achieved;
			document.getElementById(id).style.backgroundColor = '#FA8072';
		}else{
			document.getElementById(id).style.backgroundColor = '#ffffff';
		}
	}
}

// Purpose: Fill the answerArr with the appropriate answers needed.
// ---------------------------------------------------------------------
function fillAnswerArray(type,topic)
{
	if(type=="gapFill")
	{
		switch(topic)
		{
			case 'articles-basic':
				answerArr = new Array('temp','a','the','the','a','the','a','the','an','the','the','the','the','the','the',
									  'a','a','the','x','a','the','the','x','x','a','the','the','the','x','the','an','a');
				break;
			case 'nouns-basic':
				answerArr = new Array('temp','a','a lot of','x','many','several','a few','x','a','many','much','few',
									  'much','some','a','less','many','a','any','the','some');
				break;
			case 'nouns-gap-fill':
				answerArr = new Array('temp','several','few','some','too many','much','less','several','all of','many',
									  'several','many','a couple of','a few','so many','a couple of','one','much','much',
									  'many','some of');
				break;
			case 'nouns-correct-text':
				answerArr = new Array('temp','spanish','less','homework','much homework','soccer','information',
									  'the homework','advice');			
				break;
			case 'prep-direction-gap-fill':
				answerArr = new Array('temp','to','into','into','in/into','in','on','on','on/onto','into','to/into','on','on','into',
									  'to','on','into','to/into');			
				break;
			case 'prep-location-gap-fill':
				answerArr = new Array('temp','at','in','in','on','on','on','at','in','at','in');			
				break;
			case 'prep-location-correct-text':
				answerArr = new Array('temp','in','on','at','in','on','on','on','at','on','in','on');			
				break;
			default:
				break;
		}
		score_total_attainable = (answerArr.length) - 1;
		score_total_achieved = score_total_attainable;
	}
}

// Purpose: Displays the elected InfoBox on the grammar module main.
// ---------------------------------------------------------------------
function displayInfoBox(id)
{
	var totalBoxes=2;
	for(var i=1;i<=totalBoxes;i++)
	{
		genericHide("hiddenDiv-"+i);
	}
	genericShow(id);
}

// Purpose: Initilizes the images to be used in the slideshow.
// ---------------------------------------------------------------------
function initSlideShow(path,page,picCount)
{
	picNum = picCount;
	preLoadImg(path,page,picCount);
	document.getElementById('auto').style.display="none";
	document.getElementById('speedMode').disabled=false;
	document.getElementById('slideShow').src=fadeImg[imgCounter+1].src;
	if(captionArray[0]!="") document.getElementById('slideTxt').innerHTML=captionArray[imgCounter];
	document.getElementById('slideInfo').innerHTML='Slide: '+(imgCounter+1)+'/'+picCount;
	imgCounter++;
	if(imgCounter==picCount) imgCounter=0;
	t = window.setTimeout("initSlideShow('"+path+"','"+page+"',"+picCount+")", slideSpeed);
}

// Purpose: Manually move to the next slide.
// ---------------------------------------------------------------------
function nextSlide()
{
	if(imgCounter<picNum-1)
	{
		clearTimeout(t);
		imgCounter++;
		document.getElementById('auto').style.display="inline";
		document.getElementById('speedMode').disabled=true;
		document.getElementById('slideShow').src=fadeImg[imgCounter+1].src;
		if(captionArray[0]!="") document.getElementById('slideTxt').innerHTML=captionArray[imgCounter];
		document.getElementById('slideInfo').innerHTML='Slide: '+(imgCounter+1)+'/'+picNum;
	}
}

// Purpose: Manually move to the previous slide.
// ---------------------------------------------------------------------
function previousSlide()
{
	if(imgCounter>0)
	{
		clearTimeout(t);
		imgCounter--;
		document.getElementById('auto').style.display="inline";
		document.getElementById('speedMode').disabled=true;
		document.getElementById('slideShow').src=fadeImg[imgCounter+1].src;
		if(captionArray[0]!="") document.getElementById('slideTxt').innerHTML=captionArray[imgCounter];
		document.getElementById('slideInfo').innerHTML='Slide: '+(imgCounter+1)+'/'+picNum;
	}
}

// Purpose: Alters the slide changing speed to the new value.
// ---------------------------------------------------------------------
function changeSlideSpeed(newSpeed)
{
	slideSpeed = newSpeed;
}

// Purpose: Initilizes the images to be used in the Fade.
// ---------------------------------------------------------------------
function initImage(path, page, picCount) 
{
		// Initilize Globals
		// -------------------------------------
		desPath = path;
		fadePage = page;		
		imgCounter = picCount;
		preLoadImg(path,page,picCount);
  		image = document.getElementById("fade");
  		setOpacity(image, 0);
  		image.style.visibility = 'visible';
  		fadeIn("fade",0);
		swapCounter++;
}

// Purpose: Handles the preloading of images for later use.
// ---------------------------------------------------------------------
function preLoadImg(path, page, picCount)
{
	// Pre-load Images
	// -------------------------------------
	if(preloadChk==1)
	{ 
		fadeImg = new Array(picCount+1);
		for(var i = 1; i <= picCount; i++)
		{
			fadeImg[i] = new Image();
			fadeImg[i].src = path+page+'/'+page+'-slide-'+i+'.jpg';
		}
		preloadChk = 0;
	}
}

// Purpose: Sets the opacity for the Fading images.
// ---------------------------------------------------------------------
function setOpacity(obj, opacity) 
{
  	opacity = (opacity == 100)?99.999:opacity;
  
  	// IE/Win
 	obj.style.filter = "alpha(opacity:"+ opacity +")";
  
  	// Safari < 1.2, Konqueror
  	obj.style.KHTMLOpacity = opacity/100;
  
  	// Older Mozilla and Firefox
  	obj.style.MozOpacity = opacity/100;
  
  	// Safari 1.2, newer Firefox and Mozilla, CSS3
  	obj.style.opacity = opacity/100;
}

// Purpose: Handles the Fade effect.
// ---------------------------------------------------------------------
function fadeIn(objId,opacity) 
{
  	if(document.getElementById) 
	{
		var str;
   		obj = document.getElementById(objId);
    	if(opacity < 100) 
		{
      		setOpacity(obj, opacity);
      		opacity += 10;
      		window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
		}else if(opacity == 100){
			// Keep image in div for longer if it's full opacity
			// ----------------------------------------------------
			setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 1000);
    	}else{
			if(swapCounter > imgCounter)
			{
				swapCounter = 1;
				str = "url("+fadeImg[imgCounter].src+") 50% 50% no-repeat";
				document.getElementById("fadeHolder").style.background = str;
			}else{
				str = "url("+fadeImg[swapCounter-1].src+") 50% 50% no-repeat";
				document.getElementById("fadeHolder").style.background = str;
			}
			document.getElementById("fade").src = fadeImg[swapCounter].src;
			initImage(desPath,fadePage,imgCounter);
		}
  	}
}

function validateCalendar()
{
	var initStr  = "Click here to select start date";
	var initStr2 = "Click here to select end date";
	var errorMsg = document.getElementById('error');
	var date1 = document.getElementById('date');
	var date2 = document.getElementById('date2');
	if(date1.value == initStr || date2.value == initStr2)
	{
		errorMsg.style.display = "inline";
		errorMsg.innerHTML = "Select Start &amp; End date";
		return false;
	}else{
		var dateArr1 = date1.value.split("-");
		var dateArr2 = date2.value.split("-");
		if(dateArr1[0] > dateArr2[0])
		{
			errorMsg.style.display = "inline";
			errorMsg.innerHTML = "Start date greater than End date";
			return false;
		}else{
			if(dateArr1[1] > dateArr2[1])
			{
				errorMsg.style.display = "inline";
				errorMsg.innerHTML = "Start date greater than End date";
				return false;
			}else{
				if(dateArr1[2] > dateArr2[2] && dateArr1[1] == dateArr2[1])
				{
					errorMsg.style.display = "inline";
					errorMsg.innerHTML = "Start date greater than End date";
					return false;
				}else{
					return true;
				}
			}
		}
	}
}
