function NewsSearch(event1,TbId,NewsSearchUrl) 
{
	var kCode = String.fromCharCode(event1.keyCode);
	if(kCode == "\n" || kCode == "\r")
	{   
		validate(TbId,NewsSearchUrl);
		return false;
	}
}

function validate(TbId,NewsSearchUrl)
{ 
	var k = document.forms[0].elements[TbId].value;
	k = k.replace( /\s*$/, '' );
	if(k == '')
	{
		alert('Please enter one or more search news words.'); 
		return false;
	}
	window.location=NewsSearchUrl+"?SearchMode=1&SearchWord="+k;
        //window.location=NewsSearchUrl+k;
	return false;  
}

function NewsSearch(event1,TbId,NewsSearchUrl,searchOptionID) 
{
	var kCode = String.fromCharCode(event1.keyCode);
	if(kCode == "\n" || kCode == "\r")
	{   	
		validate(TbId,NewsSearchUrl,searchOptionID);
		return false;
	}
}

function validate(TbId,NewsSearchUrl,searchOptionID)
{
                var RHSearchURL = "http://research.ntu.edu.sg/Search/Pages/Results.aspx"
                var k = document.forms[0].elements[TbId].value;
                var l = 0;
                k = k.replace( /\s*$/, '' );
                if(k == '')
                {
                                alert('Please enter one or more search keywords.'); 
                                return false;
                }
                if (document.forms[0].elements[searchOptionID + '_0'].checked)
                {
                                l = 0;
                              window.location=RHSearchURL+"?SearchMode="+l+"&SearchWord="+k;
                }
                else
                {
                                l = 1;
                               window.location=NewsSearchUrl + trimspace(k);
                }
                //window.location=NewsSearchUrl+"?SearchMode="+l+"&SearchWord="+k;
                return false;  
}




function PortalSearch(event1,searchUrl,searchBoxId) 
{
	var kCode = String.fromCharCode(event1.keyCode);
	if(kCode == "\n" || kCode == "\r")
	{   
		fnSubmit(searchUrl,searchBoxId);
		return false;
	}
}

function fnSubmit(searchUrl,searchBoxId)
{
	try
	{
		var _textArea = document.getElementById(searchBoxId);
		if(bfnValidate(_textArea.value))
		{
			//ret=openext1(searchUrl + "?k=" + _textArea.value);
			ret=openext1(searchUrl + "/?IW_DATABASE=NTU%20Websites&IW_FIELD_WEB_STYLE=" + trimspace(_textArea.value))

		}
	}
	catch (e)
	{
    		alert('Search function does not work on authoring mode');
	}
	finally
	{
	}
	return false;
}

function bfnValidate(str)
{
	if (trim(str) != '')
	{
		return true;
	}
	else 
	{
		alert('Please enter a search keyword.');
		return false;
	}
	return false;
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}


function trimspace(str)
{
   var encoded_string;
   encoded_string = str.replace(/%/g,"%25");
   encoded_string = encoded_string.replace(/ /g,"%20");
   encoded_string = encoded_string.replace(/\$/g,"%24");
   encoded_string = encoded_string.replace(/&/g,"%26");
   encoded_string = encoded_string.replace(/,/g,"%2C");
   encoded_string = encoded_string.replace(/\+/g,"%2B");
   encoded_string = encoded_string.replace(/\//g,"%2F");
   encoded_string = encoded_string.replace(/:/g,"%3A");
   encoded_string = encoded_string.replace(/;/g,"%3B");
   encoded_string = encoded_string.replace(/=/g,"%3D");
   encoded_string = encoded_string.replace(/\?/g,"%3F");
   encoded_string = encoded_string.replace(/@/g,"%40");
   encoded_string = encoded_string.replace(/\"/g,"%22");
   encoded_string = encoded_string.replace(/</g,"%3C");
   encoded_string = encoded_string.replace(/>/g,"%3E");
   encoded_string = encoded_string.replace(/#/g,"%23");
   encoded_string = encoded_string.replace(/{/g,"%7B");
   encoded_string = encoded_string.replace(/}/g,"%7D");
   encoded_string = encoded_string.replace(/\|/g,"%7C");
   encoded_string = encoded_string.replace(/\\/g,"");
   encoded_string = encoded_string.replace(/\^/g,"%5E");
   encoded_string = encoded_string.replace(/~/g,"%7E");
   encoded_string = encoded_string.replace(/\[/g,"%5B");
   encoded_string = encoded_string.replace(/\]/g,"%5D");
   //encoded_string = encoded_string.replace(/\`/g,"");
   encoded_string = encoded_string.replace(/\(/g,"%28");
   encoded_string = encoded_string.replace(/\)/g,"29");
   encoded_string = encoded_string.replace(/\*/g,"%2A");
   encoded_string = encoded_string.replace(/\-/g,"%2D");
   encoded_string = encoded_string.replace(/\./g,"%2E");
   encoded_string = encoded_string.replace(/\_/g,"%5F");
   return encoded_string;
}



function openext1(externalURL,replacewindow)
{
	//externalURL=escape(externalURL);
	//var url = 'http://www.ntu.edu.sg/nr/publicportal/includes/ExternalPage.asp?URL=' + externalURL;
	var url = externalURL;
	var replace;

	if (replacewindow=='undefined')
	   replace='_second';
	else
	   replace=replacewindow;

	scrWidth = window.screen.availWidth - 50;
	scrHeight = window.screen.availHeight - 100;
	scrLeft = 0;
	scrTop=0;
	var strFeatures = "scrollbars=yes,status=yes,toolbar=yes,menubar=yes,location=yes,resizable=yes,left=" + scrLeft + ",top=" + scrTop + ",height=" +  scrHeight + ",width=" + scrWidth
	//url = url.toLowerCase() ;

	window.open(url, replace, strFeatures, 0);
	return;
}

function f_getcookie(name) 
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
	        var j = i + alen;
	        if (document.cookie.substring(i, j) == arg) return f_getcookieval (j);
	        i = document.cookie.indexOf(" ", i) + 1;
	        if (i == 0) break;
	}
	return null;
}

function f_setcookie(name,value,duration)
{
	duration=0
	cookiestring=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);
	document.cookie=cookiestring;
}

function get_random(ranRange)
{
  var ranNum= Math.round(Math.random()*ranRange);
  return ranNum;
}

function previewEmail(mailContent,replace1,senderNameId,replace2,emailFromId,replace3,emailSubjectId,replace4,emailContentId,replace5,url,emailToId)
{	

	if (validateEmail(emailToId,emailFromId))
	{
		var newWindow = window.open('','previewEmail','width=600;height=800');
		var newDoc = newWindow.document;
		var senderName = document.getElementById(senderNameId);
		var emailFrom = document.getElementById(emailFromId);
		var emailSubject = document.getElementById(emailSubjectId);
		var emailContent = document.getElementById(emailContentId);
		var mailContent2 = new String(mailContent);

		mailContent2 = mailContent2.replace("&lt;","<");
		mailContent2 = mailContent2.replace("&gt;",">");
		mailContent2 = mailContent2.replace(replace1,senderName.value);
		mailContent2 = mailContent2.replace(replace1,senderName.value);
		mailContent2 = mailContent2.replace(replace2,emailFrom.value);
		mailContent2 = mailContent2.replace(replace2,emailFrom.value);
		mailContent2 = mailContent2.replace(replace3,emailSubject.value);
		mailContent2 = mailContent2.replace(replace4,emailContent.value);
		mailContent2 = mailContent2.replace(replace5,url);

		newDoc.open();
		newDoc.writeln('<html><head><title>Preview Email</title></head><body>');
		newDoc.writeln(mailContent2);
		newDoc.writeln('</body></html>');
		newDoc.close();
	}
}

function validateEmail(emailToID,emailFromID)
{
	var emailFrom = document.getElementById(emailFromID);
	var emailTo = document.getElementById(emailToID);
	if (emailFrom.value == "")
	{
		alert('Email From is empty!');
		return false;
	}
	if (emailTo.value == "")
	{
		alert('Email To is empty!');
		return false;
	}
	return true;
}

function validateEmailFormat(email)
{
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	return (regex.test(email));
}

function multiEmail(fieldID) 
{
	var email = document.getElementById(fieldID).value.split(';');
	for (var i = 0; i < email.length; i++) 
	{
		if (!validateEmailFormat(email[i])) 
		{
			alert('One or more email addresses entered is invalid');
			return false;
		}
	}
	return true;
}

function sendEmail(emailToID,emailFromID)
{
	if (Email(emailToID,emailFromID))
	{
		if (multiEmail(emailToID) && multiEmail(emailFromID))
		{
		}
	}
}

//sheryll for schoolA
startList = function() {
		var temp = new Browseris();
		if (temp.verIEFull > 0 && temp.verIEFull < 7)
		{
		//alert(temp.verIEFull);
			if (document.all&&document.getElementById) {
				navRoot = document.getElementById("main_nav");
				//alert(navRoot);
			for (i=0; i<navRoot.childNodes.length; i++) 
			{	node = navRoot.childNodes[i];
				if (node.nodeName=="LI") 
				//if ((node.nodeName=="LI") && (navRoot.childNodes[i].id == "")){
				{//alert(node.className + "--" + i);
				 node.onmouseover=function() {node.className+=" over";}
			  	  node.onmouseout=function()  {node.className=node.className.replace(" over", "");}
			  	  //alert(node.onmouseover + i);
			  	}
			  	//else{alert(node.nodeName);}
			}
			}
		}
		}
//helper function to add elements to the form
function createNewFormElement(inputForm, elementName, elementValue){
 var newElement = document.createElement("<input name='"+elementName+"' type='hidden'>");
 inputForm.appendChild(newElement);
 newElement.value = elementValue;
 return newElement;
}

//newsletter
function newsLetterSearch(e,newsSearchUrl,tbId,dropDownId,resultUrl) 
{ 

	var keynum
	var keychar
	var numcheck

	if(window.event) // IE
	{
		keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which
	}
	keychar = String.fromCharCode(keynum)
	if(keychar == "\n" || keychar == "\r")
	{   
		newsLetterSubmit(newsSearchUrl,tbId,dropDownId,resultUrl);
		return false
	}    
}
function newsLetterSubmit(newsSearchUrl,tbId,dropDownId,resultUrl)
{ 
	var k = document.getElementById(tbId).value
	var urlprefix = document.getElementById(dropDownId).options[document.getElementById(dropDownId).selectedIndex].value
	var url

	k = k.replace( /\s*$/, '' )
	if(k == '')
	{
		alert('Please enter one or more search words.')
		return false
	}
	url = newsSearchUrl+"?k="+k +"&UrlPrefix="+urlprefix +"&resulturl=" + resultUrl 
        

	openext1(url)
	return false  
}
function newsLetterSearchOnChange(id)
{
     var seletedValue= document.getElementById(id).options[document.getElementById(id).selectedIndex].value
     if(seletedValue.indexOf('&IsAllIssue=True') == -1 )  
        window.open(seletedValue)
     else
     {
	return false
     }   
}


// main function to handle the mouse events //
function NTUHeaderddMenu(id,d){
  isStandard();
  var DDTIMER = 15;
  var h = document.getElementById(id + '_ntu_ddheader');
  var c = document.getElementById(id + '_ntu_ddcontent');
  var g = document.getElementById(id + '_ntu_ddbkg');

  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
      g.style.height = '0px';
      g.style.display = 'block';
    }
    c.timer = setInterval(function(){ddSlide(g,c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(g,c)},50);
  }
}

// collapse the menu //
function ddCollapse(g,c){
  var DDTIMER = 15;
  c.timer = setInterval(function(){ddSlide(g,c,-1)},DDTIMER);     
}

// cancel the collapse if a user rolls over the dropdown //
function NTUHeaderCancelHide(id){
  var h = document.getElementById(id + '_ntu_ddheader');
  var c = document.getElementById(id + '_ntu_ddcontent');
  var g = document.getElementById(id + '_ntu_ddbkg');
  var DDTIMER = 15;

  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(g,c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(g,c,d){
  var DDSPEED = 10; 

  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
      c.style.height = currh + (dist * d) + 3 + 'px';
      g.style.display = 'block';
  }else{
    dist = (Math.round(currh / DDSPEED));
      c.style.height = '0px';
      g.style.display = 'none';	
  }
  
  if(dist <= 1 && d == 1){
    dist = 1;
  }  
  g.style.height = c.style.height;

  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}

function isStandard(){
	var dataString = navigator.vendor;
	if(dataString)
	{
		if (dataString.indexOf("Camino") != -1){
		   document.getElementById('NTU_HeadertextMain').className='not_standard';}
		else{
		   document.getElementById('NTU_HeadertextMain').className='standard';  }
	}
	else{
	  document.getElementById('NTU_HeadertextMain').className='standard';  }  
}