/* My very first unobtrusive ajax washing powder football buzzword-laden IE-breaking javascript (TM) 2.0 beta
(c) Jonne
*/
//needed for a firefox bug/feature. Probably a good idea to have anyway
//var root='/ietm/playground/'; //locally
var root='/'; //online
var searching;
/*
Arrays used in the script, so it might be good to create them as soon as possible 
*/
var cleared = new Array;
var xmldivs = new Array;
var timer = new Array;
/*
first function, used to bootstrap the others
*/
function initJS()
	{
	
	window.clearOnFocus?clearOnFocus():null; 			//input fields with class clear have their contents erased when you put your cursor in them
	window.hilightOnFocus?hilightOnFocus():null; 		//on focus, highlight the contents of the textfield
	window.autoSubmit?autoSubmit():null; 				//submit form fields if the selectbox value is altered
	window.popups?popups():null;		 				//a href="" class="popup" creates a popup (easiest method ever)
	window.initLiveSearch?initLiveSearch():null; 		//livesearch: like google suggest
	window.xmlRequests?xmlRequests():null; 				//this one refreshes the <div> in question according to an url  and a defined timeout (class="xmlget")
	window.xmlActions?xmlActions():null;	 			// xmldo -> send a get request to the server without having to leave the page
	window.xmlActionRel?xmlActionRel():null;	 		// similar to xmlRequests, but slightly different (used in the search results)
	window.xmlToggleBoxes?xmlToggleBoxes():null; 		//submit checkboxes without having click a submit button
	window.makeCalendar?makeCalendar():null;	 		//for input fields with 'date' class, makes a datepicker
	window.getBalloons?getBalloons():null;	 			//calendar on top, show a 'tooltip' with extended info on the eventsthat day
	window.getGlossaryItems?getGlossaryItems():null;	//get <dfn> tags, and look up the contents of them in the glossary
	window.keepSession?keepSession():null;		 		//ping server every x minutes to keep your session alive if you stay on the same page for too long
	window.askConfirmation?askConfirmation():null;	 	//ask for confirmation before submiting 'dangerous' forms
	window.closeButtons?closeButtons():null; 			//links with the id 'closeid' close the element with the id they're related to through the rel attribute (rel="id")
	window.enhanceIntFields?enhanceIntFields():null; 	//integer fields, because there's no <input type="integer" /> in html
	window.lightBox?lightBox():null;					//lightbox variant
	window.adminfunctions?adminfunctions():null;		//isn't used yet, afaik, but here for future reference
	}
/* To be called when the pages changes (when using xmlHTTPRequest and stuff). Scans for new elements and applies the cool stuff on it */	
function refresh()	{
	window.clearOnFocus?clearOnFocus():null;
	window.autoSubmit?autoSubmit():null;
	window.autoSubmit?autoSubmit():null; 
	window.popups?popups():null;
	window.xmlActions?xmlActions():null;
	window.hilightOnFocus?hilightOnFocus():null;
	window.getGlossaryItems?getGlossaryItems():null;
	window.xmlActionRel?xmlActionRel():null;
	window.closeButtons?closeButtons():null;
	//window.lightBox?lightBox():null;
	//xmlRequests();	// -> these should not be nested
	}
/*
Code to clear input boxez
*/
//adds onfocus function to all clear boxes
function clearOnFocus()
	{
	var focusBoxes = new Array;
	focusBoxes = getElementsByClassName('input','clear');
	for(i=0;i<focusBoxes.length;i++)
		{
		if(focusBoxes[i].getAttribute('name').match(/\bpass\b/))
			{
			try{
				focusBoxes[i].setAttribute("type","text");
				} catch (e) {
				//this is what IE does (you can't change the type attribute of input boxes, apparently)
				//just make sure the box starts out being a 'password' box, and IE users won't be typing
				//their passwords in a plain text box
				}
			}
		focusBoxes[i].onfocus=function(){clear(this);this.focus();this.select()};
		}
	}
//clears the inputbox when called (+ changes box type if the name contains 'pass'
function clear(element)	{
	if(!cleared.inArray(element))
		{
		element.value='';
		cleared[cleared.length]=element;
		if(element.getAttribute('name').match(/\bpass\b/))
			{
			try{
				element.setAttribute("type","password");
				element.value='';
				} catch (e) {
				//this is just needed for IE, because it sucks
				}
			}
		}
	}
/* code to highlight on focus */
function hilightOnFocus()
	{
	var hilightBoxes = new Array;
	hilightBoxes = getElementsByClassName('input','highlight');
	for(i=0;i<hilightBoxes.length;i++)
		{
		hilightBoxes[i].onfocus=function(){this.select();};
		}
	}

/* ******************************
submits forms when the selectbox has a class of 'submitonchange'
also hides submit buttons with class of 'jshide'
****************************** */
function autoSubmit()	{
	var listboxes = new Array;
	var uselesssubmitbuttons = new Array;
	listboxes = getElementsByClassName('select','submitonchange');
	uselesssubmitbuttons = getElementsByClassName('input','jshide');
	for(i=0;i<listboxes.length;i++)
		{
		listboxes[i].onchange=function(){ submitForm(this);};
		}
	for(i=0;i<uselesssubmitbuttons.length;i++)
		{
		//uselesssubmitbuttons[i].style.display='none';
		uselesssubmitbuttons[i].style.padding='0';
		uselesssubmitbuttons[i].style.border='none';
		uselesssubmitbuttons[i].style.width='0px';
		uselesssubmitbuttons[i].value='';
		}	
	}
function submitForm(e){
	e.parentNode.parentNode.submit();
	}
/* *******************************************
add popup events when there's a class="popup()" on a link
******************************************** */
function popups()	{
	var popuplinks = new Array;
	popuplinks = getElementsByClassName('a','popup');
	for(i=0;i<popuplinks.length;i++)
		{
		arr=getClassVariables(popuplinks[i],'popup');
		if(!isNaN(arr[0]*1)){width=arr[0]*1;}else{width=600;}
		if(!isNaN(arr[1]*1)){height=arr[1]*1;}else{height=400;}
		popuplinks[i].onclick=function(){ popup(this.href,width,height);return false;};
		}
	}
function popup(url,windowwidth,windowheight)	{
	window.open(url,'upload','width='+windowwidth+',height='+windowheight);
	}
/* *******************************************
add live search on specific form elements (  like in http://www.google.com/webhp?complete=1 )
******************************************** */
function initLiveSearch()	{
	var searching;
	var livesearchinputs = new Array;
	livesearchinputs = getElementsByClassName('input','livesearch');
	for(i=0;i<livesearchinputs.length;i++)
		{
		arr=getClassVariables(livesearchinputs[i],'livesearch');
		livesearchinputs[i].onkeyup=function(){ liveSearch(this,this.value,arr[0],arr[1]);return false;};
		}
	}
function liveSearch(e,query,url,height)	{
	if(searching)
		{
		clearTimeout(searching);
		}
	if(query=='')
		{
		removeElement('livesearch');
		} else	{
		if(!document.getElementById('livesearch'))
			{
				div = document.createElement("div");
				div.setAttribute("id","livesearch");
				e.parentNode.insertBefore(div,e);
			} else	{
				div=e.previousSibling;
			}
		//div.innerHTML="livesearch: "+query;
		var xmlquery = url+'?query='+query;
		searching = setTimeout('getXMLText(\''+xmlquery+'\',div)',500);
		}
	}
/* ****************************************************************************************
keepSession: keeps session alive so you're not logged out when idling on the website (if you've enabled js)
***************************************************************************************** */
function keepSession()	{
	var session = setInterval('XMLdo(\'ping.lasso\')',720000);
	}
/* *************************************
xmlHTTPrequest section
************************************** */
//this one refreshes the <div> in question according to an url  and a defined timeout
function xmlRequests()	{
	xmldivs = getElementsByClassName('div','xmlget');
	for(i=0;i<xmldivs.length;i++){
		timer[i]=new Array;
		timer[i]['params']=getClassVariables(xmldivs[i],'xmlget');
		timer[i]['element']=xmldivs[i];
		//timer[i]['interval']=window.setInterval('getXMLText(timer['+i+'][\'params\'][0],timer['+i+'][\'element\']);',timer[i]['params'][1]*1000);
		window.setInterval('getXMLText(timer['+i+'][\'params\'][0],timer['+i+'][\'element\']);',timer[i]['params'][1]*1000);
		}
	}

//this one just loads a page (without immediate feedback)
function xmlActions()	{
	var xmlactions = new Array;
	xmlactions = getElementsByClassName('a','xmldo');
	for(i=0;i<xmlactions.length;i++)
		{
		//var action=xmlactions[i].href;
		if(xmlactions[i].getAttribute('rel')=='confirm')
		{
		xmlactions[i].onclick=function(){
										this.blur();
										if(confirmIt(this))
											{
											XMLdo(this.href);
											}
										return false;};
		}else{
		xmlactions[i].onclick=function(){this.blur();XMLdo(this.href);return false;};
		}
		}
	}
function XMLdo(url)	{
	xmlhttp.open("HEAD", url,true);	
		xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//do nothing
			
				for(i=0;i<xmldivs.length;i++)
					{
					params=getClassVariables(xmldivs[i],'xmlget');
					param=params[0];
					xmldiv=xmldivs[i];
					getXMLText(param,xmldiv);
					}
			}
		}
	xmlhttp.send(null);
	}
//gets toggleable checkboxes (save checkbox value without having to click 'save')
function xmlToggleBoxes()	{
	var toggleboxes = new Array;
	toggleboxes = getElementsByClassName('input','xmltoggle');
	for(i=0;i<toggleboxes.length;i++)
		{
		toggleboxes[i].onclick=function(){sendToggle(this)};
		}
	}
function sendToggle(e)	{
	var uri=('xml/'+e.getAttribute('name')+'.lasso?id='+e.getAttribute('value')+'&checked='+e.checked);
	XMLdo(uri);
	}
function flushXMLData()	{
	for(i=0;i<xmldivs.length;i++)
		{
		params=getClassVariables(xmldivs[i],'xmlget');
		param=params[0];
		xmldiv=xmldivs[i];
		getXMLText(param,xmldiv);
		}
	}
//searches for links with the 'xmlref' class, and loads new data into the element with the id in rel=""
function xmlActionRel()	{
	var xmlrefs = new Array;
	xmlrefs = getElementsByClassName('a','xmlref');
	for(i=0;i<xmlrefs.length;i++)
		{
		xmlrefs[i].onclick=function(){xmlActionRelClick(this);return false;};
		}
	}
function xmlActionRelClick(e)	{
		var relatedId=e.getAttribute('rel');
		var secondclass = e.className.split(' ')[1];
		var originalurl = e.getAttribute('href');
		//the following lines are just needed for everyone's favourite browser...
		var urlsplitter = originalurl.split("/");
		originalurl=urlsplitter.pop();
		var originalQueryString = originalurl.split('?').pop();
		var request = secondclass+'.lasso?'+originalQueryString+'&load='+relatedId;
		var reloadElement = document.getElementById(relatedId);
		getXMLText(request,reloadElement);
	}
//gets the text from an url, and drops it into the innerHTML of the given element
function getXMLText(path,e)	{
		var request=createNewRequestObject();
		request.open("GET", root+"xml/"+path+"&rnd="+Math.round(Math.random()*10000),true);
		request.onreadystatechange=function() {
		if (request.readyState==4) {
			var text = request.responseText;
			e.innerHTML=text;
			//console.log('request: '+e.id+' = '+e.innerHTML);
			refresh();
			}
		}
		request.send(null);
		/*}*/
	}
/* **************************************
start datepicker
************************************** */
//adds a datepicker next to inputfields with the 'date' class.
function makeCalendar()	{
	dateFields = getElementsByClassName('input','date');
	for(i=0;i<dateFields.length;i++)
		{
		img = document.createElement("img");
		img.setAttribute("src","images/icons/cal.gif");
		img.setAttribute("alt","calendar");
		img.setAttribute("class","cal");
		img.onclick = function(){setDate(this);};
		img.style.cursor = "pointer";
		img.style.marginLeft = "5px";
		dateFields[i].parentNode.insertBefore(img,dateFields[i].nextSibling);
		}
	}
function setDate(e)	{
	var today = new Date();
	var month = today.getMonth() + 1;
	var day = today.getDate();
	var year = today.getFullYear();
	createDatePicker(e,month,year);
	//e.previousSibling.value=year+"-"+month+"-"+day;
	}
function createDatePicker(e,month,year)	{
	if(document.getElementById('datepicker'))
		{
		var fucker=document.getElementById('datepicker');
		fucker.parentNode.removeChild(fucker);
		}
	var calendar=document.createElement("div");
	calendar.setAttribute('id','datepicker');
	calendar.style.position='absolute';
	calendar.style.display='inline';
	calendar.style.border='1px solid #999';
	calendar.style.background='#fff';
	calendar.innerHTML=calendarString(month,year);
	var fucker=document.getElementById('datepicker');
	e.parentNode.insertBefore(calendar,e.nextSibling);
}
function calendarString(month,year)	{
	string='<table>';
	string+='<tr id="header"><td colspan="7" class="close"><img src="images/close.png" alt="click to close" onclick="closeCalendar();" /></td></tr>';
	string+='<tr>';
	string+='<td class="pickerday" onclick="changeMonth('+year+','+month+',\'prev\');return false;"><a href="#">&lt;&lt;</a></td>';
	string+='<td colspan="5" style="text-align: center;">'+monthName(month)+' '+year+'</td>';
	string+='<td class="pickerday" onclick="changeMonth('+year+','+month+',\'next\');return false;"><a href="#">&gt;&gt;</a></td>';
	string+='</tr>';
	string+='<tr>';
	string+='<td class="pickerweekday">M</td>';
	string+='<td class="pickerweekday">T</td>';
	string+='<td class="pickerweekday">W</td>';
	string+='<td class="pickerweekday">T</td>';
	string+='<td class="pickerweekday">F</td>';
	string+='<td class="pickerweekday">S</td>';
	string+='<td class="pickerweekday">S</td>';
	string+='</tr>';
	string+='<tr>';
	//calculate first weekday of the month
	var firstDayOfMonth = new Date(year,month-1,1);
	var LastDayOfMonth=daysInMonth(month,year);
	offset=(((firstDayOfMonth.getDay()+7)-1)%7);
	//draw table
	for(i=0;i<offset;i++)
		{
		string+='<td></td>';
		}
	for(i=0;i<LastDayOfMonth;i++)
		{
		if((i+offset)%7==0)	{
		string+='</tr>';	
		string+='<tr>';	
			} 
		string+='<td class="pickerday" onclick="writeDate('+year+','+month+','+(i+1)+');return false;"><a href="#">'+(i+1)+'</a></td>';
		}
	string+='</tr>';
	string+='</table>';
	return string;
	}
function monthName(monthNum)	{
	var months = new Array('january','february','march','april','may','june','july','august','september','october','november','december');
	return months[monthNum-1];
	}
// http://javascript.about.com/library/bllday.htm 
//gives the number of days in a given month. don't ask me how it works, but it does
function daysInMonth(month,year) {
	var dd = new Date(year, month, 0);
	return dd.getDate();
}
function writeDate(year,month,day)	{
	var calendar=document.getElementById('datepicker');
	if(month<10)
		{
		month='0'+month;
		}
	if(day<10)
		{
		day='0'+day;
		}
	calendar.previousSibling.previousSibling.value=year+'-'+month+'-'+day;
	closeCalendar();
	}
function changeMonth(year,month,direction)	{
	if(direction=='next')
		{
		if(month==12)	{
			month=1;
			year++;
			} else {
			month++;
			}
		} else if(direction=='prev')	{
		if(month==1)	{
			month=12;
			year--;
			} else {
			month--;
			}
		}
	var calendar=document.getElementById('datepicker');
	calendar.innerHTML=calendarString(month,year);
	}
//closecalendar
function closeCalendar()	{
var calendar=document.getElementById('datepicker');
calendar.parentNode.removeChild(calendar);
}	
/*stop calendar*/
/* ************************************************************************************************
ask confirmation before submitting a form (<form class="confirm" title="are you sure you want to kill yourself?">)
************************************************************************************************ */
function askConfirmation()
	{
	confirmations = getElementsByClassName('form','confirm');
	for(i=0;i<confirmations.length;i++)
		{
		confirmations[i].onsubmit = function(){return confirmIt(this);};
		}
	}
function confirmIt(e)
	{
	var question = ''+e.title;
	
	var result = window.confirm(question);
	if(result)
		{
		return true;
		}else{
		return false;
		}
	}
/*stop askConfirmation*/
/* ******************************************
Balloon windows for calendar (the one on top) 
******************************************* */
function getBalloons()	{
	balloons = getElementsByClassName('a','balloon');
	for(i=0;i<balloons.length;i++)
		{
		balloons[i].onmouseover = function(){this.blur();showBalloon(this);return false;};
		}
}
function showBalloon(calledFrom)	{
	//hide existing balloons (if they exist)
	removeElement('eventballoon');
	var eventDate=calledFrom.getAttribute('rel');
	var eventballoon=document.createElement('div');
	eventballoon.setAttribute('id','eventballoon');
	var coords = findPos(calledFrom);
	eventballoon.style.top=(coords[1]+20)+'px';
	eventballoon.style.left=(coords[0]-150)+'px';
	eventballoon.innerHTML='<div id="ballooncontent"> \
				<img onclick="removeElement(\'eventballoon\');"  src="images/close.png" alt="x" title="close" id="balloonclose" /> \
	<div id="throbber"><img src="images/throbber.gif" alt="loading..."/></div> \
	</div>';
	document.body.appendChild(eventballoon);
	getXMLText('event_balloon.lasso?date='+eventDate,eventballoon.firstChild);
	}
/* stop balloon windows */
/* ******************************************************************
get every <dfn> tag, and get the definition of the term in it out of the glossary db
****************************************************************** */
function getGlossaryItems()	{
	var glossaryItems = document.getElementsByTagName('dfn');
	for(i=0;i<glossaryItems.length;i++)
		{
		glossaryItems[i].className = 'js';
		glossaryItems[i].title = "Click for an explanation of this term.";
		glossaryItems[i].onclick = function(){showDefinition(this);return false;};
		}
}
function showDefinition(calledFrom)	{
	//hide existing balloons (if they exist)
		removeElement('definitionbox');
		var definitionbox=document.createElement('div');
		definitionbox.setAttribute('id','definitionbox');
		var coords = findPos(calledFrom);
		definitionbox.style.top=(coords[1]+20)+'px';
		definitionbox.style.left=(coords[0]+20)+'px';
		definitionbox.innerHTML='<div id="boxcontent"> \
					<img onclick="removeElement(\'definitionbox\');"  src="images/close.png" alt="x" title="close" id="balloonclose" /> \
		<div id="throbber"><img src="images/throbber.gif" alt="loading..."/></div> \
		</div>';
		document.body.appendChild(definitionbox);
		getXMLText('glossary.lasso?word='+calledFrom.firstChild.nodeValue,definitionbox.firstChild);
	}
/* ******************************************************************
close the id mentioned in the rel tag if the link has an class of closeid
****************************************************************** */
function closeButtons()	{
	var closeButtons = getElementsByClassName('a','closeid');
	for(i=0;i<closeButtons.length;i++)
		{
		closeButtons[i].onclick = function(){removeElement(this.getAttribute('rel'));return false;};
		}
}
/* stop balloon windows */
/* ****************************************************
numeric fields, add a +/- at the end so people don't type 'two'
***************************************************** */
/* adds arrows that allow you to increment/decrement an int field*/
var incrementImage='images/go/green_top.gif';
var decrementImage='images/go/green_bottom.gif';
function enhanceIntFields()
	{
	var intFields = getElementsByClassName('input','num');
	for(i=0;i<intFields.length;i++)
		{
		/* add - */
		img = document.createElement("img");
		img.setAttribute("src",decrementImage);
		img.setAttribute("alt","-");
		img.setAttribute("title","remove an item");
		img.setAttribute("class","fieldmodify"); //good browsers
		img.setAttribute("className","fieldmodify"); //IE
		img.onclick = function(){incrementField(this.previousSibling.previousSibling,-1);};
		intFields[i].parentNode.insertBefore(img,intFields[i].nextSibling);
		
		
		
		/* add + */
		img = document.createElement("img");
		img.setAttribute("class","fieldmodify"); //good browsers
		img.setAttribute("className","fieldmodify"); //IE
		img.setAttribute("src",incrementImage);
		img.setAttribute("alt","+");
		img.setAttribute("title","add an item");
		
		img.onclick = function(){incrementField(this.previousSibling,1);};
		intFields[i].parentNode.insertBefore(img,intFields[i].nextSibling);
		
		intFields[i].onfocus = function(){this.select();}
		intFields[i].onblur = function(){incrementField(this,0)};
		}
	}
function incrementField(field,amount,negative)
	{
	if((negative!=true && (parseInt(field.value)+amount)<0) ||isNaN(parseInt(field.value)))
		{
		field.value=0;
		field.setAttribute("value",0);
		//field.onchange(); 
		return;
		}
	field.value=parseInt(field.value)+amount;
	field.setAttribute("value",parseInt(field.value));
	//field.onchange(); // force the onchange event
	}
/* **************************
Generic functions
**************************** */
//inArray function, always useful...
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
//get the postition of an element on the page
// http://www.quirksmode.org/js/findpos.html
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
//function removeElement: removes the element with the id passed, if it exists (saves me from typing)
function removeElement(id)	{
	if(document.getElementById(id))	{
			document.getElementById(id).parentNode.removeChild(document.getElementById(id));
			}
	}
//as long as browsers don't support getElementsByClassName, let's just write our own!
//extra functionality browsers won't support ever: 
//-values can be added by appending them in brackets. example: required(*@*.*),, popup(500,400), ...
function getElementsByClassName(tag, classname)	{
	var xyzzy=document.getElementsByTagName(tag);
	var elements = new Array;
	var regulexp= new RegExp("(^|\\s)"+classname+"(\\s|$|\\(.*\\))");
	for(i=0;i<xyzzy.length;i++)
		{
		if(regulexp.test(xyzzy[i].className))
			{
			elements[elements.length]=xyzzy[i];
			}
		}
	return elements;
	}
//extract the variables passed between {}'s
function getClassVariables(e,classname)	{
	vars = new Array;
	varstring = e.className;
	//var regulexp= new RegExp("(^|\\s)"+classname+"(\\s|$|{.*})");
	var regulexp= new RegExp(classname+"\\((.*)\\)");
	varr=varstring.match(regulexp);
	varstring = varr[1];
	vars=varstring.split(',');
	return vars;
	}
//xmlHTTPRequest (because that's handy sometimes)
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
function createNewRequestObject()
	{
	var requestObj=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  requestObj = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	  requestObj = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   requestObj = false;
	  }
	 }
	@end @*/
	if (!requestObj && typeof XMLHttpRequest!='undefined') {
		try {
			requestObj = new XMLHttpRequest();
		} catch (e) {
			requestObj=false;
		}
	}
	return requestObj;
	}
//this should run the js only after the whole DOM is loaded, and it saves me from doing <body onload="blah()">
//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}
addLoadEvent(initJS);	

