// JavaScript Document
$(document).ready(function() {
	/* Utilities */
		/* clear default text in search form on focus
		put default text back on blur if value is null 
			$('#bscrumb form input[type=text]').focus(function() { $(this).val(''); });
			$('#bscrumb form input[type=text]').blur(function() { if ($(this).val() == '') {$(this).val('Search the site');} }); */
		/* add border style to slideshow images */
		$('#slideshow li').append($('<div />'));
		$('#slideshow li.video div').addClass('video');
		$('.staff li:first-child').append($('<div />'));
		
		/* show / display answers in FAQ page */
		
		 $("#panel-faq .answers").each(function() { 
									   var s = $(this).prev().find("span");
									   var id = $(this).prev().prev().attr('id'); 
									   var ancre = (window.location.href).split("#")[1];
									   if (id == ancre) { $(this).css('display', 'block'); s.html('-'); }
								   }); 
		/*
		$("#panel-faq .nb_answer").css('cursor', 'pointer').click(function() {
										$("#panel-faq .answers").each(function() { 
										   var s = $(this).prev().find("span");
										   if ($(this).css('display') == 'block') { $(this).slideUp(); s.html('+'); }
									   });		
									  var s = $(this).find("span");
									  var e = $(this).parent().parent().find(".answers");
									  if (e.css('display') == 'none') { e.slideDown(); s.html('-'); } else { e.slideUp(); s.html('+'); }
									});
		*/
		
		 $("#panel-faq .title").css('cursor', 'pointer').click(function() {
				$("#panel-faq .answers").each(function() { 
				   var s = $(this).prev().find("span");
				   if ($(this).css('display') == 'block') { $(this).slideUp(); s.html('+'); }
			    });		
			  	var s = $(this).find("span");
			  	var e = $(this).parent().find(".answers");
			  	if (e.css('display') == 'none') { e.slideDown(); s.html('-'); } else { e.slideUp(); s.html('+'); }
			}); 
		

	/* Optimized CSS style */
		/* main navigation */
			$('#nav li').hover(function(){ 
					if ($(this).prev().hasClass('active') == false)
						$(this).prev().find('a').css('borderColor', '#FFF');
					if ($(this).hasClass('active') == false) 								
						$(this).find('a').css('borderColor', '#f6f6f6'); 
				}, function() {
					if (($(this).prev().hasClass('active') == false)&&($(this).hasClass('active') == false))
						$(this).prev().find('a').css('borderColor', '#f6f6f6');
					if ($(this).hasClass('active') == false)									
						$(this).find('a').css('borderColor', '#f6f6f6');
					if ($(this).next().hasClass('active') == true)									
						$(this).find('a').css('borderColor', '#FFF');
				});
			$('#nav li.active').prev().find('a').css('borderColor', '#FFF');

		$('#col-positive .col-1 h3:first-child').addClass('first-child');
		$('#col-positive .col-1 p:eq(0)').addClass('first-child');
		
		$('input[type=checkbox]').css('border', '0');
		$('input[type=radio]').css('border', '0');

	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		$('.event .date').css("background", "none").css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', src='/resources/img/bg-date.png')");
		$('.real-people-hover').each(function(){
											  var src = $(this).attr("src").replace(/png/, "gif");
											  $(this).attr("src", src);
											  });
	}
	if ($.browser.msie && $.browser.version.substr(0,1)<9) {
	
	/* add height for IE6 on positive homepage only */
	 	if($('#panel-positive #Map').size()) {
			$('#panel-positive').css('height', '275px');
		}
	/* reduce double margin for IE6 */
			$('#real_people .quote').css('margin-right', '-77px');
			$('.form-asking .submit').css('margin-top', '8px');

		
	/* apply effect to elements to fake
		hover for IE6 */
			$('.col-3parts .elt-title li').hover(function(){ 
					if ($(this).hasClass('active') == false) {
						$(this).css('borderColor', '#000');
					}
				}, function() {
					if ($(this).hasClass('active') == false) {
						$(this).css('borderColor', '#C9C9C9');
					}
				});


	/* apply a class to elements to fake
		png for IE6 */
		$('#slideshow li div').addClass('IEpng');
		$('#slideshow li.video div').addClass('IEpngvideo');

	/* apply a class of firstChild to elements to fake
		the first-child pseudo-selector for IE6 */
		$('#nav li:last-child').addClass('last-child');
		$('#links-sitemap li:first-child').addClass('first-child');
		$('#links-share li:first-child').addClass('first-child');
		$('#links-find li:first-child').addClass('first-child');
		$('#panel-home li:first-child').addClass('first-child');
		$('.col-3 .box:last-child').addClass('last-child');
		$('.getInvolved li:last-child').addClass('last-child');
		$('#slideshow li:last-child').addClass('last-child');
		
	/* apply a class to elements to fake
		the after pseudo-selector for IE */
		$('#bscrumb li.home a').addClass('after'); 
		$('#bscrumb li.home a').append(' / '); 
		
		$('#links-sitemap li:last-child').addClass('last-child');
		$('#links-sitemap li:not(:last-child)').addClass('after');
		$('#links-sitemap li:not(:last-child)').append(' / ');

	/* add a clear div for IE6 */
		$('#col-positive .col-1 p').after($('<div />').css('clear', 'left'));
		
	/* corners position */
		$('.corner-TR').css('right', '0');
		$('.corner-BL').css('bottom', '0');
		$('.corner-BR').css('bottom', '0').css('right', '0');

	/* specify negative margin for italic position IE6 bug */
		$('.event .date .num').css('margin-right', '-5px'); 

	}
	
});

/* -- Employment form -- */
function nextStep(up, down) {
	$('#stepForm #sf'+up).slideUp();
	$('#stepForm #sf'+down).slideDown();
}
function validateFields(current){
	
	var value = true;
	var li = '#stepForm #sf'+current;
	
	$(li+' .error').removeClass('error');
	
	
/* INPUT TEXT */
		/* -- add error */
		
	$(li+' input[type=text].required').each(function(){ if(($(this).val() == '')||($(this).val() == 'DD/MM/YYYY')) { $(this).addClass('error'); $(this).prev('label').addClass('error'); value = false; } }); 
			/* -- Email style -- */		
	$(li+' input[type=text].email').each(function(){
		// reg = /^([\w.])+\@(([\w])+\.)[a-zA-Z0-9]{2,5}/;
		reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z0-9]{2,4}/;
		if(!reg.test($(this).val())){ $(this).addClass('error'); $(this).prev('label').addClass('error'); value = false; }
	}); 
		/* -- remove error */
	$("#stepForm input[type=text]").blur(function() { 
		if ($(this).hasClass('email')) { reg = /^([\w.])+\@(([\w])+\.)[a-zA-Z0-9]{2,5}/; if(reg.test($(this).val())){ $(this).removeClass('error'); $(this).prev('label').removeClass('error'); } }
		else { if (($(this).val() != '')&&($(this).val() != 'DD/MM/YYYY')) { $(this).removeClass('error'); $(this).prev('label').removeClass('error'); } }
	});
	

/* -- Required "Other" radio checked -- */		
		/* -- add error */
	$(li+' input[type=text].requiredOther').each(function(){
		if (($(this).prev('label').find('input').attr('checked'))&&($(this).val() == '')) {
			 $(this).prev('label').addClass('error'); $(this).addClass('error'); $(this).parents('div.group').parent().children('label').addClass('error');
		}
	});
		/* -- remove error */
	$(li+' input[type=text].requiredOther').blur(function(){
		if ($(this).val() != '') { 
			 $(this).prev('label').removeClass('error'); $(this).removeClass('error'); $(this).parents('div.group').parent().children('label').removeClass('error');
		}
	});

/* -- Required Previous radio "YES" checked -- */
		/* -- add error */
	$(li+' input[type=text].requiredYes').each(function(){
		var prevdiv = $(this).parent('div').prev('div'); if (!prevdiv.hasClass('controlset')) { prevdiv = prevdiv.prev('div'); }									
		if (prevdiv.find('input:checked').val()=='yes'){
			if (($(this).val() == '')||($(this).val() == 'DD/MM/YYYY')) { $(this).addClass('error'); $(this).prev('label').addClass('error'); value = false; } 
		}
	}); 

/* -- Required Previous radio "No" checked -- */
		/* -- add error */
	$(li+' input[type=text].requiredNo').each(function(){
		var prevdiv = $(this).parent('div').prev('div'); if (!prevdiv.hasClass('controlset')) { prevdiv = prevdiv.prev('div'); }									
		if (prevdiv.find('input:checked').val()=='no'){
			if (($(this).val() == '')||($(this).val() == 'DD/MM/YYYY')) { $(this).addClass('error'); $(this).prev('label').addClass('error'); value = false; } 
		}
	}); 

/* -- Required Previous radio "Work Permit" checked -- */
		/* -- add error */
	$(li+' input[type=text].requiredPermit').each(function(){
		if ($(this).parent('div').prev('div').find('select').val()=='Work Visa / Permit'){
			if (($(this).val() == '')||($(this).val() == 'DD/MM/YYYY')) { $(this).addClass('error'); $(this).prev('label').addClass('error'); value = false; } 
		}
	}); 
	
/* INPUT TEXTAREA */
		/* -- add error */
	$(li+' textarea.required').each(function(){ if($(this).val() == '') { $(this).addClass('error'); $(this).prev('label').addClass('error'); value = false; } }); 
		/* -- remove error -- */
	$("#stepForm textarea").blur(function() { if ($(this).val() != '') { $(this).removeClass('error'); $(this).prev('label').removeClass('error'); } });


/* INPUT SELECT */
		/* -- add error */
	$(li+' select.required').each(function(){ if(($(this).val() == 'Select')||($(this).val() == 'Select category')) { $(this).addClass('error'); $(this).prev('label').addClass('error'); value = false; } }); 
			/* -- Required Previous radio "YES" checked -- */
	$(li+' select.requiredYes').each(function(){
		var prevdiv = $(this).parent('div').prev('div');
		if (!prevdiv.hasClass('controlset')) { prevdiv = prevdiv.prev('div'); }									
		if (prevdiv.find('input:checked').val()=='yes'){
		 	if($(this).val() == 'Select') { $(this).addClass('error'); $(this).prev('label').addClass('error'); value = false; } 
		}  
	});
		/* -- remove error */
	$("#stepForm select").blur(function() { if (($(this).val() != 'Select')&&($(this).val() != 'Select category')) { $(this).removeClass('error'); $(this).prev('label').removeClass('error') } });


/* INPUT RADIO */
/* -- YES / NO Required -- */	
		/* -- add error */
	$(li+' input[value=yes].required').each(function(){ 
		if((!$(this).attr('checked'))&&(!$(this).parent('div').find('input[value=no]').attr('checked'))) { $(this).prev('.label').addClass('error'); $(this).nextAll('label').addClass('error'); value = false; } 
	}); 

/* -- Required -- */	
		/* -- add error */	
	$(li+' input[type=radio][value!=yes][value!=no].required').each(function(){
			 alert($(this).attr('name'));
			$input = $('input[name='+$(this).attr('name')+']:checked'); 
			if($input.length==0) { $('input[name='+$(this).attr('name')+']').parent('label').addClass('error'); } 
	});
		/* -- remove error */	
	$("#stepForm input[type=radio]").click(function() { 
		if (!$(this).hasClass('requiredYesHere')) { $(this).nextAll('label').removeClass('error'); $(this).prevAll('label').removeClass('error'); $('input[name='+$(this).attr('name')+']').parent('label').removeClass('error'); $(this).prev('.label').removeClass('error'); }
	});

/* -- Required Previous radio checked -- */	
		/* -- add error */
	$(li+' input[type=radio].requiredPrevious').each(function(){
		if ($(this).parents('div.group').parent().find('input').attr('checked')) {
			$input = $('input[name='+$(this).attr('name')+']:checked'); 
			if($input.length==0) { $('input[name='+$(this).attr('name')+']').parent('label').addClass('error'); $(this).parents('div.group').parent().children('label').addClass('error'); } 
		}
	});
		/* -- remove error */
	$(li+' input[type=radio].requiredPrevious').each(function(){
		if (($(this).attr('checked'))&&(!$(this).parents('div.group').parent().find('input').attr('checked'))) {
			$('input[name='+$(this).attr('name')+']').parent('label').removeClass('error'); $(this).parents('div.group').parent().children('label').removeClass('error');
		} });
	$(li+' input[type=radio].requiredPrevious').click(function(){
			$('input[name='+$(this).attr('name')+']').parent('label').removeClass('error'); $(this).parents('div.group').parent().find('label').removeClass('error');
		 });

/* -- Required Previous radio "YES" checked -- */	
		/* -- add error */
	$(li+" input[value=yes].requiredYes").each(function(){ 
		var prevdiv = $(this).parent('div').prev('div');
		if (!prevdiv.hasClass('controlset')) { prevdiv = prevdiv.prev('div'); }									
		if (prevdiv.find('input:checked').val()=='yes'){
			if((!$(this).attr('checked'))&&(!$(this).parent('div').find('input[value=no]').attr('checked'))) { $(this).prev('.label').addClass('error'); $(this).nextAll('label').addClass('error'); value = false; } 
		} else { $(this).attr('checked', false); $(this).parent('div').find('input[value=no]').attr('checked', false); }
	}); 

/* -- Required Previous radio "NO" checked -- */	
		/* -- add error */
	$(li+" input[value=yes].requiredNo").each(function(){ 
		var prevdiv = $(this).parent('div').prev('div');
		if (!prevdiv.hasClass('controlset')) { prevdiv = prevdiv.prev('div'); }									
		if (prevdiv.find('input:checked').val()=='no'){
			if((!$(this).attr('checked'))&&(!$(this).parent('div').find('input[value=no]').attr('checked'))) { $(this).prev('.label').addClass('error'); $(this).nextAll('label').addClass('error'); value = false; } 
		} else { $(this).attr('checked', false); $(this).parent('div').find('input[value=no]').attr('checked', false); }
	}); 

/* -- Required  "YES" checked -- */	
		/* -- add error */
	$(li+" input[value=yes].requiredYesHere").each(function(){ 
			if(!$(this).attr('checked')) { $(this).prev('.label').addClass('error'); $(this).nextAll('label').addClass('error'); value = false; } 
	}); 
		/* -- remove error */
	$("#stepForm input[value=yes].requiredYesHere").click(function() { $(this).nextAll('label').removeClass('error'); $(this).prev('.label').removeClass('error'); });


	$(li+' input[type=file].requiredOr').each(function(){
		var textarea = $(this).parent().parent().find('textarea.requiredOr');
		if ((!$(this).val())&&(!textarea.val())) { $(this).prev('label').addClass('error'); textarea.addClass('error'); textarea.prev('label').addClass('error'); value = false; } 
	}); 
	$(li+' textarea.requiredOr').each(function(){
		var file = $(this).parent().parent().find('input[type=file].requiredOr');
		if ((!$(this).val())&&(!file.val())) { $(this).addClass('error'); $(this).prev('label').addClass('error'); file.prev('label').addClass('error'); value = false; } 
	}); 
	
/* INPUT CHECKBOX */
/* -- Required -- */	
		/* -- add error */	
	$(li+' input[type=checkbox].required').each(function(){ 
			if(!$(this).attr('checked')) { $(this).parent('label').addClass('error'); value = false; } 
	});
		/* -- remove error */	
	$("#stepForm input[type=checkbox]").click(function() { 
		$(this).parent('label').removeClass('error');
	});

	return value;
}
$(document).ready(function(){
	if($('#stepForm #sf1').size()) {
		$('#stepForm #sf2').hide();
		$('#stepForm #sf3').hide();
	}
	
	$("#stepForm .open1").click(function() {
		if(validateFields(1)) {
			nextStep(1,2); 
		}
	});
	$("#stepForm .open2").click(function() {
		if(validateFields(2)) {
			nextStep(2,3); 					   
		}
	});
	//$("#stepForm .submit").click(function() {
	//	return validateFields(3);
	//});
	$("#stepForm-2 .submit").click(function() {
		return validateFields(4);
	});
	
	$("#stepForm #sf2 .prevbutton").click(function(){
		nextStep(2,1); 					   
	}); 
	$("#stepForm #sf3 .prevbutton").click(function(){
		nextStep(3,2); 					   
	}); 
	
	//$("#stepForm #sf3 .submit").click(function(){
		//alert('hello');				   
	//}); 
	
	if($('#stepForm .openGroup').size()) {
		$('#stepForm .openGroup').each(function() {
			if($(this).attr('checked')) {
				$(this).parents('div').children('.group').show();
			} else { 
				$(this).parents('div').children('.group').hide();
			}
		});
		$("#stepForm .openGroup").click(function(){
			$('#stepForm .group').hide();
			$(this).parents('div').children('.group').slideDown();
		}); 
	}
	
	if($('#payANZ-eGate').size()) {
		e = $("#payANZ-eGate").parent();
		f = $("#payForm").parent();
		if($('input[name=donation]').size()) {
			i = $("input[value=One off donation]");
			j = $("input[value=Regular automatic monthly donation]");
			if(i.attr('checked')) { e.show(); } else { e.hide(); }
			if(j.attr('checked')) { f.show(); } else { f.hide(); }
			 $("input[name=donation]").click(function(){
					if($(this).val()=="One off donation"){ e.show(); } else { e.hide(); }
					if($(this).val()=="Regular automatic monthly donation"){ f.show(); } else { f.hide(); }
			 });
		}
	}
});

function popup(a, t, w, h) {
	window.open (a, t, 'location=0,status=0,scrollbars=1, width='+w+',height='+h); 
}