// From dynamic validation scripts. Last updated: 15/01/2009

jQuery(function($){

	var errorCount = 0;
	

	// Handlers for sector checkboxes to reveal 'please specify' info box //
	$('.show_specify').each(function(){
		$(this).click(function(){
			var el = $(this).nextAll('.specify');
			if ($(this).attr('checked') ==  true) {
				el.fadeIn();
				if(el.hasClass('err')){
					errorCount++;
				}
			} else {
				el.fadeOut();
				if(el.hasClass('err')){
					errorCount--;
				}
			}
		});
	});

    $('.required').each(function(){
		if($(this).is(':text') || $(this).is('select')){
			$(this).prev().append('<img src="images/required.gif" alt="required field" />');
			if($(this).is(':text')){
				$(this).blur(function(){
					test_err($(this));
				});
			} else {
				$(this).change(function(){
					test_err($(this));
				});
			}
		}
		if($(this).is('fieldset')){
			$(this).children('legend').prepend('<img src="images/required.gif" alt="required field" />');
		}
	});
	
	
	$('input:text').each(function(){
		$(this).focus(function(){
			if(!$(this).hasClass('err')){
				$(this).css({borderTop: '#000000 solid 2px', borderLeft: '#000000 solid 2px'});
			}
		}).blur(function(){
			if(!$(this).hasClass('err')){
				$(this).css({borderTop: '1px solid #444', borderLeft: '1px solid #444', borderBottom: '1px solid #CCC', borderRight: '1px solid #CCC'});
			}
		});
	
	});
	
	
	$('form').submit(function(){
		var es = $('.required');
		es.each(function(){
		    var e = $(this);
			if(e.is('fieldset')){
				check_fieldset(e);
				
			} else if(e.is('input:text') || e.is('select')){
				test_err(e);
			}
		});
		if(errorCount == 0){
			return true;
		} else { 
			window.scrollTo(0,0);
			alert("Please fill in all required fields");
			return false;
		}
	});
	
	
	
	function test_err(el){ //test for input in required fields and display error message
		if(el.val().match(/^\s*$/) && !el.is(':hidden')){
			if(!el.hasClass('err')){
				el.addClass('err')
			         .css({border: '#9B0D2E 1px solid'})
				     .prev()
				     .append('<span class="error">This is a required field</span>')
					 .children()
					 .remove('img');
					 
					 errorCount++;
			}
		} else if (!el.is(':hidden')){
			if(el.hasClass('err')){
				errorCount--;
			} 
			el.removeClass('err')
				 .css({borderTop: '1px solid #444', borderLeft: '1px solid #444', borderBottom: '1px solid #CCC', borderRight: '1px solid #CCC'})
				 .prev()
				 .children()
				 .replaceWith('<img src="images/tick-icon.gif" alt="valid input" class="tick" />');
		}
		if(el.is('.email')){
			validate_email(el);
		}
		if(el.is('.health_email')){
			validate_health_email(el);
		}
		if(el.is('.phone')){
			validate_phone(el);
		}
		if(el.is('.he_number')){
			validate_henumber(el);
		}
	}
	
	function validate_email(e){
		
		var label = e.prev();
		var value = jQuery.trim(e.val());
		if(!value.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)){
			if(!label.children('.warning').length && !label.children('.error').length){
				
				label.children().replaceWith('<span class="warning">This does not appear to be a valid email address</span>');
			} else if (label.children('.error').length){ //there was blank
				label.children().remove('.warning'); //let it silently fail if not there
			}
		} else {
			label.children().remove('.warning'); //let it silently fail if not there
		}
	}
	
	function validate_health_email(e){
		var label = e.prev();
		var value = jQuery.trim(e.val());
		if(!value.match(/^[A-Z0-9._%-]+@health.wa.gov.au$/i)){
			if(!label.children('.warning').length && !label.children('.error').length){
				label.children().replaceWith('<span class="warning">This does not appear to be a valid @health.wa.gov.au email address</span>');
			} else if (label.children('.error').length){ //there was blank
				label.children().remove('.warning'); //let it silently fail if not there
			}
		} else {
			label.children().remove('.warning'); //let it silently fail if not there
		}
	}
	
	function validate_phone(e){
		var label = e.prev();
		var value = jQuery.trim(e.val());
		if(!value.match(/^[0-9.+\s-]{5,}$/)){
			if(!label.children('.warning').length && !label.children('.error').length){
				label.children().replaceWith('<span class="warning">This does not appear to be a valid phone number</span>');
			} else if (label.children('.error').length){ //there was blank
				label.children().remove('.warning'); //let it silently fail if not there
			}
		} else {
			label.children().remove('.warning'); //let it silently fail if not there
		}
	}
	
	function validate_henumber(e){
		var label = e.prev();
		var value = jQuery.trim(e.val());
		if(!value.match(/^he[0-9]{5}$/i)){
			if(!label.children('.warning').length && !label.children('.error').length){
				label.children().replaceWith('<span class="warning">This does not appear to be a valid HE number - Format HE12345 </span>');
			} else if (label.children('.error').length){ 
				label.children().remove('.warning'); //let query silently fail if not there
			}
		} else {
			label.children().remove('.warning'); //let query silently fail if not there
		}
	}
	
	function check_fieldset(e){ // check for at least one checked box or radio button
	
		var checked = 0;
		var check_inputs = e.contents().find('input:checkbox');
		var radio_inputs = e.contents().find('input:radio');
		var legend = e.children('legend');
				
		if(check_inputs.length > 0){
			check_inputs.each(function(){
				if($(this).attr('checked') == true){
					checked++;
				}
			});
		} else if(radio_inputs.length > 0){
			radio_inputs.each(function(){
				if($(this).attr('checked') == true){
					checked++;
				}
			});
		}
		if(checked == 0 && !e.hasClass('err')){
			errorCount++;
			e.css({border: '#9B0D2E solid 1px'})
			  .addClass('err');
			legend.prepend('<span class="error">Please select at least one option </span>');
		} else if (checked > 0 && e.hasClass('err')){
			e.css({border: 'none'})
			 .removeClass('err');
			legend.children().remove('span.error');
			errorCount--;
		} else {
			//leave error message as is
		} 
	}

});
