var html_loading  = '<img src="/images/loading.gif"> processing';
 
 /*
function addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
		window.onload = func;
	  } else {
		window.onload = function() {
		  if (oldonload) {
			oldonload();
		  }
		  func();
		}
	  }
	}
 
 
 
 addLoadEvent(function(){
	alert('test');
	 new Ajax.Updater('header',"/app/header.asp", {asynchronous:true,evalScripts:true});
	 new Ajax.Updater('footer',"/app/footer.asp", {asynchronous:true,evalScripts:true});
	 
	  if(jsGet('ref') != "") {
			var ref = jsGet('ref') == undefined ? "" : jsGet('ref');
			if(ref!= ""){
				document.getElementById('comment').value= "I saw this site " + ref + "\n and I thought you would like to see it.";
			} 
	  }
 });*/

function accordion(el) {
    if ($('visible') == el) {
        return;
    }
    if ($('visible')) {
        var eldown = el.parentNode.id+'-body';
        var elup = $('visible').parentNode.id+'-body';
        new Effect.Parallel(
        [
            new Effect.SlideUp(elup),
            new Effect.SlideDown(eldown)
        ], {
            duration: 0.1
        });
        $('visible').id = '';
    }
    el.id = 'visible';
}

function txinit() {

    // hide all elements apart from the one with id visible
    var acc = document.getElementById('accordion');
    var apanels = acc.getElementsByTagName('div');
    for (i = 0; i < apanels.length; i++) {
        if (apanels[i].className == 'panel_body') {
            apanels[i].style.display = 'none';
        }
    }
    var avis = document.getElementById('visible').parentNode.id+'-body';
    document.getElementById(avis).style.display = 'block';
}
function addEvent(elm, evType, fn, useCapture) {
    elm["on"+evType]=fn;return;
}

  function  email_this_page(){
	var locat =  location.href;
	self.location='/tabid/169/Default.aspx?ref='+ locat;
  }
  
  function jsGet(type){
	if(location.href.match(type)){
	return location.href.split(type+'=')[1].split('&')[0];
	}}
	



function email_this_page_action(){
   
     var error_msg = ""; 
	 if(document.getElementById('fullname').value==""){
		error_msg += "\n - Your Full Name";    
	 }
	 
	 if(document.getElementById('emailaddress').value==""){
		error_msg += "\n - Your Email Address";    
	 } else {
		   var apos= document.getElementById('emailaddress').value.indexOf("@");
           var  dotpos=document.getElementById('emailaddress').value.indexOf(".");
		   if(apos < 1 && dotpos < 1) {
		         error_msg += "\n -  fill in correct format of email address e.g. you@yourdomain.com.sg";
		   }
 	 }
	 
	 if(document.getElementById('subject').value==""){
		error_msg += "\n - Your Subject";    
	 }

	 if(document.getElementById('comment').value==""){
		error_msg += "\n - Your Comment";    
	 }
	 else
	 {
		var sComment = document.getElementById('comment').value;
	    sComment = '<p>' + sComment + '</p><p>I thought you would be interested in this article: ' + gup('ref') + '</p>';
		
		document.getElementById('comment').value = sComment;
	 }
	 
	 if(document.getElementById('ffemailaddress').value==""){
		error_msg += "\n - Your Reciptient Email Address";    
	 } else {
		   var apos= document.getElementById('ffemailaddress').value.indexOf("@");
           var  dotpos=document.getElementById('ffemailaddress').value.indexOf(".");
		   if(apos < 1 && dotpos < 1) {
		         error_msg += "\n -  fill in correct format of email address e.g. you@yourdomain.com.sg";
		   }
	}


	 if(error_msg=="") {
	    var params = Form.serialize('Form');
		//params = $F(params);
			 
	     document.getElementById('email_this_page_form').innerHTML = html_loading;
		 
		 new Ajax.Updater('email_this_page_form','/app/email_this_page_form.asp', {asynchronous:true,parameters:params,evalScripts:true});
         //new Ajax.Updater('email_this_page_form','/app/email_this_page_form.asp', {method: 'post', asynchronous:true,parameters:Form.serialize('Form'),evalScripts:true});
		 
	 } else {
		alert("please complete the following:\n" + error_msg); 
	 }
}


function siteSearch(){
	// document.getElementById('Form').action='/tabid/974/Default.aspx?cx=006477145368448606018:qwfdt9cupgw&cof=FORID:11&sa=Search';
	if(document.getElementById('q').value!='' && document.getElementById('q').value!='Enter text here'){
	     
		 if(document.getElementById('opt').value=='0'){
		 	self.location = 'http://www.google.com/search?q=' +  escape(document.getElementById('q').value) + '&domains=nus.edu.sg&sitesearch=nus.edu.sg';
		 } else if(document.getElementById('opt').value=='1'){
		 	self.location = 'http://www.google.com/search?q=' +  escape(document.getElementById('q').value) + '&domains=nus.edu.sg&sitesearch=bschool.nus.edu.sg';
		 } else if(document.getElementById('opt').value=='2'){
		    self.location = '/FacultyDepartments/ExpertiseGuideSearch/tabid/589/Default.aspx?keyword=' +  escape(document.getElementById('q').value) + '&inOptions=5';
		 } else if(document.getElementById('opt').value=='3'){
		    self.location ='http://www.nus.edu.sg/search/';
		 } else {
		   alert("check");	 
		}
	} else {
	   alert("Please enter a search keyword");
	}
	
}
 
function objLoading(divload){
	document.getElementById(divload).innerHTML = '<img src="/images/loading.gif"/>';
}


function objSubmit(cmdAction,divSpot){
	   objLoading(divSpot);
       new Ajax.Updater(divSpot,cmdAction, {asynchronous:true,parameters:Form.serialize(document.forms['Form']),evalScripts:false});
}
/*
//function created by THEN ARIFIN, Info-Lab Snr Dev, 24-Feb-2011
function readFile(theLocation) {
	var sBodyMessage;
	var objXml = new ActiveXObject("Microsoft.XMLHTTP");

	objXml.open("GET", theLocation, true);
	objXml.onreadystatechange=function() {
   
	if (objXml.readyState==4) {
       sBodyMessage = objXml.responseText;
	}
	
	return sBodyMessage;
	}
}


//function created by THEN ARIFIN, Info-Lab Snr Dev, 24-Feb-2011
//function to load default message
function loadDefaultMessage() {
	var sLocation = '/app/template/tellafriend.txt';
	var sDefaultMessage = readFile(sLocation);
	var sRefSite = gup('ref');
	
	if (sRefSite == '')
		sDefaultMessage = '';
	else
		sDefaultMessage = sDefaultMessage.replace('[LINK]', gup('ref'));
		
	document.getElementById('comment').value = sDefaultMessage;
	
}
*/
//function created by THEN ARIFIN, Info-Lab Snr Dev, 24-Feb-2011
//function to get website url parameter value
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

//loadDefaultMessage();
