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(){
	 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 addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
		window.onload = func;
	  } else {
		window.onload = function() {
		  if (oldonload) {
			oldonload();
		  }
		  func();
		}
	  }
	}
	
  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";    
	 }
	 
	 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=="") {
	     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: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});
}
