/*
--- JAVASCRIPT FUNCTIONS FOR www.ntu.ac.uk ---
--- ntujavascript.js is included in all general pages published out of the CMS
*/
//Check box to ensure users have accepted the terms and conditions for online payment
function checkme() {
missinginfo = "";
if (!document.form.agree.checked) {
missinginfo += "\n Please confirm that you have read and agree\n to the University's terms and conditions";
} 
if (missinginfo != "") {
alert(missinginfo);
return false;
}
else { 
return true;
}
}

function openScrollWindow(newpage, name) 
{ 
	window.open("" + newpage, name,  "width=1024, height=700, menubar=no,scrollbars=yes,location=no,favorites=no,resizable=yes,status=no,toolbar=no,directories=no");
}

// Popup box - not sure what for - Sorabh
function openNewWindow(URLtoOpen,windowName,height,width) 
{ 
	windowFeatures ="menubar=no,scrollbars=no,location=no,favorites=no,resizable=no,status=no,toolbar=no,directories=no"; 
	var test = "'"; 
	winLeft = (screen.width-width)/2; 
	winTop = (screen.height-(height+110))/2; 
	myWin= open(URLtoOpen,windowName,"width=" + width +",height=" + height + ",left=" + winLeft + ",top=" + winTop + test + windowFeatures + test); 
	myWin.document.open(); 
	myWin.document.close(); 
}

// New browser window - Graham
function newWindow(newpage, name) 
{ 

	window.open("" + newpage, name,  "scrollbars=yes, resizable=yes, toolbar=yes, status=yes, location=yes, menubar=yes");
}


// Popup box - Ask us a question - Graham
function askaquestionpopup(newpage, name) 
{ 
window.open("" + newpage, name,  "width=400, height=620, scrollbars=no, resizable=no, toolbar=no, status=no, location=no, menubar=no");
}

// Popup box - Sport - Jerome
function sportpopup(newpage, name) 
{ 
window.open("" + newpage, name,  "width=350, height=150, scrollbars=no, resizable=no, toolbar=no, status=no, location=no, menubar=no");
}

// Popup box - eCentral - Jerome
function policypopup(newpage, name) 
{ 
window.open("" + newpage, name,  "width=210, height=160, scrollbars=no, resizable=no, toolbar=no, status=no, location=no, menubar=no");
}

// Popup box - Podcast Map & streaming player - Jerome
function podpopup(newpage, name) 
{ 
newwindow=window.open("" + newpage, name,  "width=500, height=750, scrollbars=yes, resizable=no, toolbar=no, status=no, location=no, menubar=no");
if (window.focus) {newwindow.focus()}
}

function playerpopup(newpage, name) 
{ 
window.open("" + newpage, name,  "width=350, height=80, scrollbars=no, resizable=no, toolbar=no, status=no, location=no, menubar=no");
}

// Popup box - Flash video NLS virtual open day - Lower Resolution - Tom
function vodpopup(newpage, name) 
{
width=404;
height=308;
winLeft = (screen.width-width)/2;
winTop = (screen.height-(height+110))/2;
window.open("" + newpage, name,  "width="+width+", height="+height+", left="+winLeft+", top="+winTop+" scrollbars=no, resizable=no, toolbar=no, status=no, location=no, menubar=no");
//if (window.focus) {newwindow.focus()}
}

// Popup box - QUB Flash video - Jerome
function qubpopup(newpage, name) 
{
width=704;
height=558;
winLeft = (screen.width-width)/2;
winTop = (screen.height-(height+110))/2;
newwindow=window.open("" + newpage, name,  "width="+width+", height="+height+", left="+winLeft+", top="+winTop+" scrollbars=no, resizable=no, toolbar=no, status=no, location=no, menubar=no");
if (window.focus) {newwindow.focus()}
}

// Email vaildation - outlook web access form - Tim

var L_strMailboxPlease_Message = "Please type in your email address.";
var L_strValidMailbox_Message = "The email adress name contains invalid characters.";
var L_SessionTimedOut_Message = "Your session has timed out.  If you wish to continue, you will need to log back on.";

function sendForm(bAnon) {

var objRegExp = new RegExp("[^\u00270-9a-zA-Z._@\u002D]", "gi") 
	if (!bAnon)
		{
				if (document.frmEmailLogon.txtMailbox.value=="") 
				{
						alert(L_strMailboxPlease_Message);
				}
				else if (objRegExp.test(document.frmEmailLogon.txtMailbox.value) == true )
				{
						alert(L_strValidMailbox_Message);
				}   
				else
				{
						window.location = "https://owa2k.ntu.ac.uk/exchange/" + document.frmEmailLogon.txtMailbox.value + "/";
				}    
		}
		else
		{
				document.frmEmailLogon.txtMailbox.value="";
				document.frmEmailLogon.submit();
		}
}

// Hidden layer script, click through images on Accommodation > Residences pages - Jerome 120405
// call function popup()
function popup(div_id)
{
  var style_sheet = getStyleObject(div_id);
  if (style_sheet)
  {
    hideAll();
    changeObjectVisibility(div_id,"visible");
  }
  else 
  {
    alert("You browser does not support this");
  }
}
//  hides divs
function hideAll()
{
   changeObjectVisibility("image7","hidden");
   changeObjectVisibility("image6","hidden");
   changeObjectVisibility("image5","hidden");
   changeObjectVisibility("image4","hidden");
   changeObjectVisibility("image3","hidden");
   changeObjectVisibility("image2","hidden");
   changeObjectVisibility("image1","hidden");
}
// function getStyleObject(string) -> returns style object
//  given a string containing the id of an object
//  the function returns the stylesheet of that object
//  or false if it can't find a stylesheet.  Handles
//  cross-browser compatibility issues.
//
function getStyleObject(objectId) {  
// checkW3C DOM, then MSIE 4, then NN 4.
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}

function changeObjectVisibility(objectId, newVisibility) {
    // first get a reference to the cross-browser style object 
    // and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
}

/* Hidden layer script for Course Galleries - CH 18/11/2005 */

function showImage(div_id)
{
  var style_sheet = getStyleObject(div_id);
  if (style_sheet)
  {
    hideImages();
    changeObjectVisibility(div_id,"visible");
  }
  else 
  {
    alert("You browser does not support this");
  }
}

function showText(div_id)
{
  var style_sheet = getStyleObject(div_id);
  if (style_sheet)
  {
    hideText();
    changeObjectVisibility(div_id,"visible");
  }
  else 
  {
    alert("You browser does not support this");
  }
}

//  hides divs
function hideImages()
{
   changeObjectVisibility("image6","hidden");
   changeObjectVisibility("image5","hidden");
   changeObjectVisibility("image4","hidden");
   changeObjectVisibility("image3","hidden");
   changeObjectVisibility("image2","hidden");
   changeObjectVisibility("image1","hidden");
}

function hideText()
{
	changeObjectVisibility("text6","hidden");
	changeObjectVisibility("text5","hidden");
	changeObjectVisibility("text4","hidden");
	changeObjectVisibility("text3","hidden");
	changeObjectVisibility("text2","hidden");
	changeObjectVisibility("text1","hidden");
}

function showNames(div_id)
{
  var style_sheet = getStyleObject(div_id);
  if (style_sheet)
  {
    hideNames();
    changeObjectVisibility(div_id,"visible");
  }
  else 
  {
    alert("You browser does not support this");
  }
}

function hideNames()
{
	changeObjectVisibility("text7","hidden");
	changeObjectVisibility("text8","hidden");
	changeObjectVisibility("text9","hidden");
}
/*Art and design visitor galleries functions - 20/06/2006 Gav */
function art_changeState(state)
{
	if (state=='0'){
		changeObjectVisibility("image1","visible");
		changeObjectVisibility("image2","visible");
		changeObjectVisibility("image3","visible");
		art_showImage('1');
		changeObjectVisibility("text1","visible");
		changeObjectVisibility("image4","hidden");
		changeObjectVisibility("image5","hidden");
		changeObjectVisibility("image6","hidden");
		changeObjectVisibility("text2","hidden");
	}
	else {
		changeObjectVisibility("image4","visible");
		changeObjectVisibility("image5","visible");
		changeObjectVisibility("image6","visible");
		art_showImage('4');
		changeObjectVisibility("text2","visible");
		changeObjectVisibility("image1","hidden");
		changeObjectVisibility("image2","hidden");
		changeObjectVisibility("image3","hidden");
		changeObjectVisibility("text1","hidden");
	}
}

function art_showImage(show_image)
{
changeObjectVisibility("imagea","hidden");
changeObjectVisibility("imageb","hidden");
changeObjectVisibility("imagec","hidden");
changeObjectVisibility("imaged","hidden");
changeObjectVisibility("imagee","hidden");
changeObjectVisibility("imagef","hidden");
var d = parseInt(show_image);
switch(d)
{
case 1:
changeObjectVisibility("imagea","visible");
break
case 2:
changeObjectVisibility("imageb","visible");
break
case 3:
changeObjectVisibility("imagec","visible");
break
case 4:
changeObjectVisibility("imaged","visible");
break
case 5:
changeObjectVisibility("imagee","visible");
break
case 6:
changeObjectVisibility("imagef","visible");
break
default:
changeObjectVisibility("imagec","visible");
break
}
}
/* END - 20/06/2006 Gav */

/** LLR Induction functions **/
	/* Browser sniffer */
	/* --------------- */
	
	var isIE = false;
	var isOther = false;
	var isNS4 = false;
	var isNS6 = false;
	if(document.getElementById) {
		if(!document.all) {
			isNS6=true;
		}
		if(document.all) {
			isIE=true;
		}
	}
	else {
		if(document.layers) {
			isNS4=true;
		}
		else {
			isOther=true;
		}
	}
	

	/* Layer functions */
	/* --------------- */

	function aL(layerID) {
	var returnLayer;
		if(isIE) {
			returnLayer = eval("document.all." + layerID);
		}
		if(isNS6) {
			returnLayer = eval("document.getElementById('" + layerID + "')");
		}
		if(isNS4) {
			returnLayer = eval("document." + layerID);
		}
		if(isOther) {
			returnLayer = "null";
			alert(cgBrowserAlert);
		}
	return returnLayer;
	}


	function aLs(layerID) {
	var returnLayer;
		if(isIE) {
			returnLayer = aL(layerID).style;
		}
		if(isNS6) {
			returnLayer = aL(layerID).style;
		}
		if(isNS4) {
			returnLayer = aL(layerID);
		}
		if(isOther) {
			returnLayer = "null";
			alert(cgBrowserAlert);
		}
	return returnLayer;
	}


	/* Layer hide and show functions */
	/* ----------------------------- */
	
	function HideShow(ID) {
		if((aLs(ID).display == "block") || (aLs(ID).display == "")) {
			aLs(ID).display = "none";
		}
		else if(aLs(ID).display == "none") {
			aLs(ID).display = "block";
		}
	}

	function cgShow(ID) {
		aLs(ID).display = "block";
	}

	function cgHide(ID) {
		aLs(ID).display = "none";
	}



	/* Feedback functions for layers */
	/* ----------------------------- */

	function shfbkLyr_generic(n) {
	//Inputs:	N is the Question Number
	//Notes:	Displays Feedback for Question
		cgShow("Q" + n + "Feedback");
	}

	function shfbkLyr_mch1(n,c) {
	//Inputs:	N is the Question Number
	//		C is the Number of Choices
	//Notes:	Question Evaluates Choice from a Hidden Input in the HTML Form
	//		and Displays Correct/Incorrect Feedback
		var tmpChoice = eval("document.Q" + n + "Form.Q" + n + "Choice");
		var tmpMatch = eval("document.Q" + n + "Form.Q" + n + "Match");
		var correct=0;

		for (i = 0; i < c; i++) {
      			if (tmpChoice[i].checked == true) {
					if (tmpMatch.value == (i+1)) {
        				correct+= 1;
					}
				}
  		}

		if (correct == 0) {
			cgShow("Q" + n + "FBIncorrect");
			cgHide("Q" + n + "FBCorrect");
		}
		else {
			cgShow("Q" + n + "FBCorrect");
			cgHide("Q" + n + "FBIncorrect");
		}

	}

	function shfbkLyr_mch2(n,c) {
	//Inputs:	N is the Question Number
	//		C is the Number of Choices
	//Notes:	Question Evaluates Choice and Displays Corresponding FeedBack
		var tmpChoice = eval("document.Q" + n + "Form.Q" + n + "Choice");
		var tmpMatch = eval("document.Q" + n + "Form.Q" + n + "Match");
		var chosen = 0;
		for (i = 0; i < (c+1); i++) {
			cgHide("Q" + n + "Feedback" + i);
		}
		for (i = 0; i < c; i++) {
      			if (tmpChoice[i].checked == true) {
       				chosen = i+1;
				}
  		}

		cgShow("Q" + n + "Feedback" + chosen);
	}


	function shfbkLyr_chb2(n,c) {
	//Inputs:	N is the Question Number
	//		C is the Number of Choices
	//Notes:	Question Evaluates Choices and Displays Corresponding FeedBack
	//		Correct/Incorrect for Each Choice along with Score
	//		Gets Matches from Hidden Input in the Questions HTML Form
		var tmpChoice = eval("document.Q" + n + "Form.Q" + n + "Choice");
		var score = 0;

		for (i = 0; i < (c); i++) {
      		if (tmpChoice[i].checked == trim(eval("document.Q" + n + "Form.Q" + n + "Matches" + i + ".value"))) {
				cgShow("Q" + n + "FBCorrect" + i);
				cgHide("Q" + n + "FBIncorrect" + i);
				score += 1;
			}
			else {
				cgShow("Q" + n + "FBIncorrect" + i);
				cgHide("Q" + n + "FBCorrect" + i);
			}
  		}
	}


	function shfbkLyr_chb3(n,c) {
	//Inputs:	N is the Question Number
	//		C is the Number of Choices
	//Notes:	Question Evaluates Choices and Displays Correct/Incorrect FeedBack
	//		Gets Matches from Hidden Input in the Questions HTML Form
		var tmpChoice = eval("document.Q" + n + "Form.Q" + n + "Choice");
		var correct=0;

		for (i = 0; i < (c); i++) {
      		if (tmpChoice[i].checked != trim(eval("document.Q" + n + "Form.Q" + n + "Matches" + i + ".value"))) {
        		correct+= 1;
      		}
  		}

		if (correct == 0) {
			cgShow("Q" + n + "FBCorrect");
			cgHide("Q" + n + "FBIncorrect");
		}
		else {
			cgShow("Q" + n + "FBIncorrect");
			cgHide("Q" + n + "FBCorrect");
		}
	}


	function shfbkLyr_txt2(n){
	//Inputs:	N is the Question Number
	//Notes:	Displays user's response, correct answer and feedback for question
	
		var tmpTextEntry=eval("document.Q"+n+"Form.Q"+n+"TextEntry");
		var tmpYourAnswer=cgTextEntry2Response+tmpTextEntry.value+"<br/><br/>";
		var tmpHiddenAnswer=eval("document.Q"+n+"Form.Q"+n+"HiddenAnswer"+".value");
		var tmpFeedback=eval("document.Q"+n+"Form.Q"+n+"HiddenFeedback"+".value");
		var tmpObj=aL("Q"+n+"Feedback");
		
		tmpObj.innerHTML=tmpYourAnswer+tmpHiddenAnswer+"<br/><br/>"+tmpFeedback;
		cgShow("Q"+n+"Feedback");
	}



	function shfbkLyr_txt3(n,c) {
	//Inputs:	N is the Question Number
	//		C is the Number of Choices
	//Notes:	Question Evaluates Entered Text and Displays Correct/Incorrect FeedBack
	//		Gets Matches from Hidden Input in the Questions HTML Form
		var tmpTextEntry = eval("document.Q" + n + "Form.Q" + n + "TextEntry");
		var correct = 0;

		for (i = 0; i < (c); i++) {
      		if (trim(tmpTextEntry.value).toLowerCase() == trim(eval("document.Q" + n + "Form.Q" + n + "TextEntryMatches" + i + ".value")).toLowerCase()) {
        		correct = 1;
      		}
  		}

		if (correct == 0) {
			cgShow("Q" + n + "FBIncorrect");
			cgHide("Q" + n + "FBCorrect");
		}
		else {
			cgShow("Q" + n + "FBCorrect");
			cgHide("Q" + n + "FBIncorrect");
		}
	}


	function shfbkLyr_match2(n,c) {
	//Inputs:	N is the Question Number
	//		C is the Number of Choices
	//Notes:	Question Evaluates Entered Text and Displays Correct/Incorrect FeedBack
	//		Gets Matches from Hidden Input in the Questions HTML Form
		var correct=0;

		for (i = 1; i < (c+1); i++) {
      		if ((eval("document.Q" + n + "Form.Q" + n + "Combo" + i + ".selectedIndex")) != trim(eval("document.Q" + n + "Form.Q" + n + "Matches" + (i-1) + ".value"))) {
        		correct+= 1;
      		}
  		}

		if (correct == 0) {
			cgShow("Q" + n + "FBCorrect");
			cgHide("Q" + n + "FBIncorrect");
		}
		else {
			cgShow("Q" + n + "FBIncorrect");
			cgHide("Q" + n + "FBCorrect");
		}
	}


	function shfbkLyr_gap2(n,c) {
	//Inputs:	N is the Question Number
	//		C is the Number of Choices
	//Notes:	Question Evaluates Entered Text and Displays Correct/Incorrect FeedBack
	//		Gets Matches from Hidden Input in the Questions HTML Form
		var score=0;

		for (i = 0; i < (c); i++) {
      		if (trim(eval("document.Q" + n + "Form.Q" + n + "TextEntry" + (i+1) + ".value")).toLowerCase() == trim(eval("document.Q" + n + "Form.Q" + n + "TextEntryMatches" + i + ".value")).toLowerCase()) {
        		score += 1;
      		}
  		}

		var strTemp = cgGapfill2Response1 + score + cgGapfill2Response2 + c + cgGapfill2Response3;

		if (score == c) {
			var tmpObj1 = aL("Q" + n + "FBCorrectStore");
			var tmpObj2 = aL("Q" + n + "FBCorrect");
			tmpObj2.innerHTML = strTemp + "<br/><br/>" + tmpObj1.innerHTML;
			cgShow("Q" + n + "FBCorrect");
			cgHide("Q" + n + "FBIncorrect");
		}
		else {
			var tmpObj3 = aL("Q" + n + "FBIncorrectStore");
			var tmpObj4 = aL("Q" + n + "FBIncorrect");
			tmpObj4.innerHTML = strTemp + "<br/><br/>" + tmpObj3.innerHTML;
			cgShow("Q" + n + "FBIncorrect");
			cgHide("Q" + n + "FBCorrect");
		}
		
	}


	function shfbkLyr_gap4(n,c) {
	//Inputs:	N is the Question Number
	//		C is the Number of Choices
	//Notes:	Question Evaluates Entered Text and Displays Correct/Incorrect FeedBack
	//		Gets Matches from Hidden Input in the Questions HTML Form
		var score=0;

		for (i = 0; i < (c); i++) {
		var tmpCombo = eval("document.Q" + n + "Form.Q" + n + "Combo" + (i+1) + ".options[document.Q" + n + "Form.Q" + n + "Combo" + (i+1) + ".selectedIndex].text").toLowerCase();
      		if (tmpCombo == trim(eval("document.Q" + n + "Form.Q" + n + "TextEntryMatches" + i + ".value")).toLowerCase()) {
        		score += 1;
      		}
  		}

		var strTemp = cgGapfill4Response1 + score + cgGapfill4Response2 + c + cgGapfill4Response3;
		
		if (score == c) {
			var tmpObj1 = aL("Q" + n + "FBCorrectStore");
			var tmpObj2 = aL("Q" + n + "FBCorrect");
			tmpObj2.innerHTML = strTemp + "<br/><br/>" + tmpObj1.innerHTML;
			cgShow("Q" + n + "FBCorrect");
			cgHide("Q" + n + "FBIncorrect");
		}
		else {
			var tmpObj3 = aL("Q" + n + "FBIncorrectStore");
			var tmpObj4 = aL("Q" + n + "FBIncorrect");
			tmpObj4.innerHTML = strTemp + "<br/><br/>" + tmpObj3.innerHTML;
			cgShow("Q" + n + "FBIncorrect");
			cgHide("Q" + n + "FBCorrect");
		}
	
	}



	/* Window open functions */
	/* --------------------- */

	function winPop2(popUrl,myWidth,myHeight,myScroll) {
		popWin=window.open(""+popUrl,"popupWindow","width="+myWidth+",height="+myHeight+",scrollbars="+myScroll+",menubar=no");
		return true;
	}
	
	function winOpen(winUrl) { 
	 popWin=window.open(winUrl,"linkWindow","width=640,height=480,status=yes,toolbar=yes,menubar=yes,resizable=yes,location=yes,scrollbars=yes");
	}



	/* Utility functions */
	/* --------------------- */

	function trim(s) {
	    while (s.substring(0,1) == ' ') {
	    	s= s.substring(1,s.length);
	    }
	    while (s.substring(s.length-1,s.length) == ' ') {
	    	s = s.substring(0,s.length-1);
	    }
	    return s;
	}


	
	/* Rollover function */

    function imgSwap(imgName, imgSrc) {
         if (document.images) {
              document.images[imgName].src = imgSrc;
         }
    }
