function add_onload(element, new_function) {
	var old_onload = element.onload;

	if (typeof old_onload != 'function') {
		old_onload = function(){};
	}

	element.onload = function (){
		old_onload();
		new_function();
	}
}

function add_onunload(element, new_function) {
	var old_onunload = element.onunload;

	if (typeof old_onunload != 'function') {
		old_onunload = function(){};
	}

	element.onunload = function (){
		old_onunload();
		new_function();
	}
}

function add_onfocus(element, new_function) {
	var old_onfocus = element.onfocus;

	if (typeof old_onfocus != 'function') {
		old_onfocus = function(){};
	}

	element.onfocus = function (){
		old_onfocus();
		new_function();
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function limitTextarea(elm,limit,display_id,limit_id) {
  $(limit_id).value = limit - (elm.value.length);
  if ($(limit_id).value > 1) {
  	$(display_id).innerHTML = limit - elm.value.length+' characters remaining.';
  }
  if ($(limit_id).value == 1) {
  	$(display_id).innerHTML = '1 character remaining.';
 	}
  if ($(limit_id).value == 0) {
  	$(display_id).innerHTML = '0 characters remaining.';
 	}
 	if ($(limit_id).value < 0) {
 		$(display_id).innerHTML = limit - elm.value.length+' characters remaining, excess characters will be truncated.';
 	}
}

function callbackLink(href, element_id, inner_html) {
	$(element_id).href = href;	
	if (href == '' || href == 'javascript:;') {
		$(element_id+'_hidden').value = '';
		$(element_id).innerHTML = 'Add a Link';
		$(element_id).title = 'Add a Link';
		$(element_id+'_delete').hide();
	} else {
		if (inner_html === undefined) {
			inner_html = href.truncate(30);
		}
		$(element_id+'_hidden').value = href;
		$(element_id).innerHTML = inner_html;
		$(element_id).title = href;
		$(element_id).target = '_blank';
		$(element_id+'_delete').show();
	}
}

/**************************************************************************************************/
// Set URL
// Author: Anthony McGrath
/**************************************************************************************************/
function setURL(url){
	siteURL = url;
}
/**************************************************************************************************/
//	 Slideshow
//	 Author: Anthony McGrath
/**************************************************************************************************/

																																									// Slideshow Tweaks
																																	// Variables are measured in seconds

changeDelay = 6; 
onClickChangeSpeed = 0.5;  
automaticChangeSpeed = 1.0; 

																																							 // Automatic Variables
changeDelay = changeDelay * 1000;																																					 
repeatDelay =	changeDelay/2;

function changeSlide(currId, gotoId){  																						  // Manual Control
	var currentID = currId;
	var gotoID = gotoId;
//	$(currentID).style.display = "none";
//	$(gotoID).style.display = "block";
	Effect.Fade(currentID, { duration: onClickChangeSpeed });
	Effect.Appear(gotoID, { duration: onClickChangeSpeed });
}
function autoChange(){																															// Auto Slideshow
	
	// Find out how many slides there are
	slide1 = document.getElementById("inner_links_1");
	divs = slide1.getElementsByTagName("a");
	numberOfSlides = divs.length;
	timer = setTimeout("change('image_1', 1)", changeDelay);							
}
function repeat(old, num){
	var oldImage = old;
	var number = num;
	timer = setTimeout("change('"+oldImage+"',"+number+")", repeatDelay);
}
function change(old, num){
 	var oldImage = old;
 	
 	if (oldImage == "stop"){
 		functionStop();
 	} else {
	 	var number = num;
	 	if (number < numberOfSlides){
	 		number++;
	 	} else {
	 		number = 1;
	 	}
		var id =  "image_";
		var imageID = id+number;
		if(imageID != oldImage){
//			$(oldImage).style.display = "none";
//			$(imageID).style.display = "block";
			Effect.Fade(oldImage, { duration: automaticChangeSpeed });
			Effect.Appear(imageID, { duration: automaticChangeSpeed });
		}
		oldImage = imageID;
		 timer = setTimeout("repeat('"+oldImage+"', "+number+")", repeatDelay);
	} 
}
function functionStop(){																							 				 // Stop Show on click
	clearTimeout(timer);
} 


/**************************************************************************************************/
//	 Video Swap
//	 Author: Anthony McGrath
/**************************************************************************************************/

function switchVideo(){
	getCurrentState();
	setText();
	setVideo(npVideo, stVideo);
}

function getCurrentState(){
	npv = $('nowPlayingVideo').innerHTML;
	stv = $('switchToVideo').innerHTML;
	npVideo = npv.toLowerCase();
	stVideo = stv.toLowerCase();
	
	while (npVideo.indexOf(' ') > -1){
		npVideo = npVideo.replace(' ', '_');
	}
	while (stVideo.indexOf(' ') > -1){
		stVideo = stVideo.replace(' ', '_');
	}

} 

function setText(){
	$('nowPlayingVideo').innerHTML = stv;
	$('switchToVideo').innerHTML = npv;
}
function setVideo(npVideo, stVideo){
	Effect.Fade(npVideo, { duration: 1.0 });
	Effect.Appear(stVideo, { duration: 1.0 });
}

/**************************************************************************************************/
// Social Animation
// Author: Anthony McGrath
/**************************************************************************************************/
visibleId = 'null';
kill = false;
function showSocialText(liId, divId, setWidth){
	if(divId != visibleId){
	//	hideSocialText(divId);
			$(liId).style.width = setWidth;
			$(divId).style.display = 'block';
			mouse = false;
			visibleId = divId;
	}
}

function hideSocialText(liId, divId){
	mouse=false;
	$(liId).style.width = '32px';
	$(divId).style.display = 'none';
	visibleId = 'null';
	
}

function killfade(boolean){
	kill = boolean;
}

/**************************************************************************************************/
// Swap Gallery Images
// Author: Anthony McGrath
// Last Updated: 31/05/2010
// Version: 1.1
/**************************************************************************************************/
prevImage = '';
function initialiseSelected(id){
	if (prevImage == ''){
		prevImage = id;
	}
}
function swapLarge(newID, newTitle){
	if (prevImage != newID){
		newImage = newID;
		$('normalLarge').src = siteURL+'/user/image/normalgallerylarge/'+newImage+'/'+newTitle;
		$(prevImage).className = '';
		$(newImage).className = 'selected';
		prevImage = newImage;
	}
}
function initialisePrevImage(initialImage){
	prevImage = initialImage;
}
function swapMinimalLarge(newID, newTitle, newCaption){
	if (prevImage != newID){
		newImage = newID;
		$('minimalLarge').src = siteURL+'/user/image/minimalgallerylarge/'+newImage+'/'+newTitle;
		$('minimal_caption').innerHTML = newCaption;
		$(prevImage).className = '';
		$(newImage).className = 'selected';
		prevImage = newImage;
	}
}
function swapFlickrLarge(newID, newTitle, newUrl){
	if (prevImage != newID){
		newImage = newID;
		$('minimalLarge').src = newUrl;
		$('minimal_caption').innerHTML = newTitle;
		$(prevImage).className = '';
		$(newImage).className = 'selected';
		prevImage = newImage;
	}
}

/**************************************************************************************************/
// Word Count
// Author: Anthony McGrath
// Last Updated: 04/06/2010
// Version: 0.1
/**************************************************************************************************/
varsSet = false;
function initialiseWordCountVars(){
	if (varsSet == false){
		wordCount = $('numberOfWords');
		wordsRemaining = $('remainingWords');
//		notEnough = $('notEnough');
		remaining = 100;
		textBox = $('eml_writing_task');
		varsSet = true
	}
}
function checkWordCount(){
	dataEntry = $('eml_writing_task').value+' ';
	removeSpaces();
	splitString = nsText.split(' ');
	if (splitString.length <= 1){
		wordCountVal = 0;
	} else {
		wordCountVal = splitString.length;
		wordCountVal = wordCountVal - 1;
	}
	$('numberOfWords').innerHTML = wordCountVal;
}

function removeSpaces(){
	nsText = dataEntry.replace(/(\n\r|\n|\r)/gm,"<1br />");
	nsText = nsText.replace(/\t/g,"");
	re1 = /\s+/g;
	nsText = nsText.replace(re1," ");
	re2 = /\<1br \/>/gi;
	nsText = nsText.replace(re2, "\n");
}
/**************************************************************************************************/
// Blog Details Switcher
// Author: Anthony McGrath
// Last Updated: 15/06/2010
// Version: 0.1
/**************************************************************************************************/
postDetailsVisible = false;
function blogDetails(){
	if (postDetailsVisible == false){
		Effect.Appear($('post_details'), {duration: 0.5});
		$('details_toggle').innerHTML = "Hide post details";
		postDetailsVisible = true;
	} else {
		Effect.Fade($('post_details'), {duration: 0.5});
		$('details_toggle').innerHTML = "About this post";
		postDetailsVisible = false;
	}
}

/**************************************************************************************************/
//	 Slideshow Gallery
//	 Author: Anthony McGrath
/**************************************************************************************************/

																																									// Slideshow Tweaks
																																									
																																																																						// Variables are measured in seconds



changeDelay = 9; 
onClickChangeSpeed = 0.5;  
automaticChangeSpeed = 1.0; 
var imageID = 'normalLarge_1';
var galleryOldImage = 'slideThumb_1';																																				 // Automatic Variables
changeDelay = changeDelay * 1000;																																					 
repeatDelay =	changeDelay/2;
function slideGallerySelect(newId){
	if(newId != galleryOldImage){
		$(galleryOldImage).className = '';
		$(newId).className = ' selected';
		galleryOldImage = newId;
	}	
}
function changeSlideGallery (gotoId){  																						  // Manual Control
	var currentID = imageID;
	var gotoID = gotoId;
	positionThumbs(gotoID);
	if(gotoID != currentID){
		Effect.Fade(currentID, { duration: onClickChangeSpeed });
		Effect.Appear(gotoID, { duration: onClickChangeSpeed });
		imageID = gotoID;
	}
}
function autoChangeGallery(){																															// Auto Slideshow
	positionThumbs('normalLarge_1');
	
	// Find out how many slides there are
	slide1 = document.getElementById("normal_gallery_block");
	divs = slide1.getElementsByTagName("img");
	numberOfSlides = divs.length;
	timer = setTimeout("changeGallery('normalLarge_1', 1)", changeDelay);							
}
function repeatGallery(old, num){
	var oldImage = old;
	var number = num;
	timer = setTimeout("changeGallery('"+oldImage+"',"+number+")", repeatDelay);
}
function changeGallery(old, num){
 	var oldImage = old;
 	
 	if (oldImage == "stop"){
 		functionStop();
 	} else {
	 	var number = num;
	 	if (number < numberOfSlides){
	 		number++;
	 	} else {
	 		number = 1;
	 	}
		var id =  "normalLarge_";

		imageID = id+number;
		newThumb = 'slideThumb_'+number;
		if(imageID != oldImage){
			Effect.Fade(oldImage, { duration: automaticChangeSpeed });
			Effect.Appear(imageID, { duration: automaticChangeSpeed });
			slideGallerySelect(newThumb);
			positionThumbs(imageID);
		}
		oldImage = imageID;
		timer = setTimeout("repeatGallery('"+oldImage+"', "+number+")", repeatDelay);
	} 
}
function positionThumbs(imageId){
	pageHeight=0;
	thumbHeight = $('slideshowbreak').offsetHeight;
		
		if(IE == true){
			$(imageId).getDimensions();
		}
		width = $(imageId).width;
		if(width < 555){
//			alert('width = '+width);
			stretch = 555/width;
		} else {
			stretch = 1;
		}
//		alert('stretch = '+stretch);
		height = $(imageId).height*stretch;
		height = Math.round(height);
//		alert('Image height = '+height+', Thumb Height = '+thumbHeight);
		pageHeight = height+thumbHeight+20;
		pageHeight = Math.round(pageHeight);
//		alert(pageHeight);
		height = height+'px';
		pageHeight= pageHeight+'px';
		$('slideshowbreak').style.top = height;
		$('slideshowContainer').style.height = pageHeight;
		
}
function functionStop(){																							 				 // Stop Show on click
	clearTimeout(timer);
} 




var IE = false;	
function ieCheck(variable){
	IE = variable;
}

/**************************************************************************************************/
//	 toggleBlock
//	 Author: Anthony McGrath
/**************************************************************************************************/

function toggleBlock(id){
	if($(id).style.display == 'none'){
		Effect.BlindDown($(id), {duration : 0.5 });
	} else {
		Effect.BlindUp($(id), {duration: 0.5 });
		children = $(id).descendants('input');
		//-> [];
		elements = children.length - 1;
		
		for(i=0;i <= elements; i++){
			if(children[i].checked == true){
				children[i].checked = false;
			}
			if(children[i].type != 'radio' && children[i].type != 'checkbox'){
				if(children[i].id.startsWith('eml_') && (children[i].id.endsWith('_to') || children[i].id.endsWith('_from'))){
					children[i].value = 'day / month / year';
				} else {
					children[i].value = '';
				}
			}
		}
	}

}
function makeName(){
	first = $('eml_given_name').value;
	last = $('eml_family_name').value;
	name = first+' '+last;
	$('name').value = name;
	$('name').focus;
}
function showBlockAccommodation1(){
	if($('accommodation_preferences').style.display == 'none'){
		Effect.BlindDown($('accommodation_preferences'), {duration : 0.5 });
	} 
}
function hideBlockAccommodation1(){
	if($('accommodation_preferences').style.display != 'none'){
		Effect.BlindUp($('accommodation_preferences'), {duration: 0.5 });
	}
}

function toggleHomestay(){
	if($('homestay_preferences').style.display == 'none'){
		Effect.BlindDown($('homestay_preferences'), {duration : 0.5 });
	} else {
		Effect.BlindUp($('homestay_preferences'), {duration: 0.5 });
	}

}

function toggleDetails(key){
	var element = key;
	if (key == 1){
		alt = 2;
		text = "Student residence";
	} else {
		alt = 1;
		text = "Serviced apartment";
	}
	element = $('eml_type_of_accomodation_requested_'+element);
	alt = $('eml_type_of_accomodation_requested_'+alt);
	if (element.checked == true){
		$('residenceType').innerHTML = text;
	}
	if($('accommodationDetails').style.display == 'none' && alt.checked == false){
		Effect.BlindDown($('accommodationDetails'), {duration : 0.5 });
	} else if (alt.checked == false){
		Effect.BlindUp($('accommodationDetails'), {duration: 0.5 });
	}
	
}
function showDealer(){
	if($('agency').style.display == 'none'){
		Effect.BlindDown($('agency'), {duration : 0.5 });
	} 
}
function hideDealer(){
	if($('agency').style.display != 'none'){
		Effect.BlindUp($('agency'), {duration: 0.5 });
	}
}
function showStudied(){
	if($('study').style.display == 'none'){
		Effect.BlindDown($('study'), {duration : 0.5 });
	} 
}
function hideStudied(){
	if($('study').style.display != 'none'){
		Effect.BlindUp($('study'), {duration: 0.5 });
	}
}

function toggleCourse(id){
	if($(id).style.display == 'none'){
		Effect.BlindDown($(id), {duration : 0.5 });
	} else {
		Effect.BlindUp($(id), {duration: 0.5 });
	}
}

function showCambridgeExams(){
	if($('exams').style.display == 'none'){
		Effect.BlindDown($('exams'), {duration : 0.5 });
	} 
}
function hideCambridgeExams(){
	if($('exams').style.display != 'none'){
		Effect.BlindUp($('exams'), {duration: 0.5 });
	}
}

function doInputs(chk) {
if (chk.checked)
	Effect.BlindDown($('cambridge'), {duration : 0.5 });
else if ($('eml_auckland_courses_4').checked != true && $('eml_auckland_courses_5').checked != true && $('eml_auckland_courses_6').checked != true && $('eml_christchurch_courses_3').checked != true && $('eml_christchurch_courses_4').checked != true && $('eml_christchurch_courses_5').checked != true)
	Effect.BlindUp($('cambridge'), {duration: 0.5 });
}    

