/* Author: 
Stuart Haston , NorthColur Ltd, 2011
*/


$(document).ready(function() {
	
	
	
	$('.team_name').keyup(function() {
		$(this).removeClass('compd');
		if ($(this).val()=="" || $(this).val()=="Team Name" ){
			$(this).addClass('required');
			hasError = true;
		} else {
			hasError = false;
			$(this).removeClass('required');
			
		}
	 });
	$('.team_tm_fn').keyup(function() {
		$(this).removeClass('compd');
		if ($(this).val()=="" || $(this).val()=="First Name" ){
			$(this).addClass('required');
			hasError = true;
		} else {
			hasError = false;
			$(this).removeClass('required');
		}
	 });
	$('.team_tm_sn').keyup(function() {
		$(this).removeClass('compd');
		if ($(this).val()=="" || $(this).val()=="Surname" ){
			$(this).addClass('required');
			hasError = true;
		} else {
			hasError = false;
			$(this).removeClass('required');
		}
	 });
	$('.team_tm_em').keyup(function() {
		$(this).removeClass('compd');
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var regex = new RegExp(emailReg);
		if ((!regex.test($(this).val())) || $(this).val()==""){
				$(this).addClass('required');
				hasError = true;
		} else {
			hasError = false;
			$(this).removeClass('required');
		}
	 });
	$('.team_tm_dob_d').keyup(function() {
		$(this).removeClass('compd');
		var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
		var str = $(this).val();
		if(numberRegex.test(str)) {
		   		hasError = false;
				$(this).removeClass('required');
		} else {
				$(this).addClass('required');
				hasError = true;
		}
		
	 });
	$('.team_tm_dob_m').keyup(function() {
		$(this).removeClass('compd');
		var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
		var str = $(this).val();
		if(numberRegex.test(str)) {
		   		hasError = false;
				$(this).removeClass('required');
		} else {
				$(this).addClass('required');
				hasError = true;
		}

	 });
	$('.team_tm_dob_y').keyup(function() {
		$(this).removeClass('compd');
		var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
		var str = $(this).val();
		if(numberRegex.test(str)) {
		   		hasError = false;
				$(this).removeClass('required');
		} else {
				$(this).addClass('required');
				hasError = true;
		}

	 });
	
	errornotice = $("#errsummary");
	errornotice.hide();
	$("#fmteamselection").submit(function(){	
		
			//if any inputs on the page have the class 'needsfilled' the form will not submit
			if ($("input").hasClass('required') || $("input").hasClass('compd')) {
				$('.compd').addClass('required');
				errornotice.fadeIn(750);
				return false;
			} else {
				errornotice.hide();
				return true;
			}
		});

		// Clears any fields in the form when the user clicks on them
		$("input").focus(function(){		
		   if ($(this).hasClass("compd") ) {
				$(this).val("");
				
		   }
		});
	
	

	
	
	
	
	$('.load').live('click', function() {
	var lastmsg = $(this).attr("id");
	if(lastmsg)	{
		$("a#"+lastmsg).html('Loading...');
		$.ajax({type: "POST",url: "/_showload.asp",data: "page_id=1004&position="+ lastmsg,success: function(html){
			$("#showload").append(html);
			$("a#"+lastmsg).remove();
			$(body).animate({scrolltop: $(body)[0].scrollHeight});
			}
		});
	}
	else {
		$(".load").html("You have reached the end");
	}
	return false;
	});
	



	

	$('#gallery a, #gallerylist a, #showload .imgblock a').lightBox(); // Select all links in object with gallery ID

	$('#submitbutton').click(function() {
		if($('#tandc').is(":checked")){
                return true;
	        }
	        else
	        {
			alert("Please read and agree to the Terms and Conditions.");
                return false;
	        }
	 
		
	});
	
	$('#nextcyc').cycle({ 
	
		    fx:     'fade', 
		    speed:  'fast', 
		    timeout: 5000,
			cleartype: true,
			cleartypeNoBg: true
		});
	
	
	$('#cnForm, #fmunsub').live('submit', function() {
		
		
		$("#flash").hide();
		
		$(".required").removeClass('required');
		var dataString = $(this).serialize() + '&Ajax=1';
		var hasError = false;
		
		
	
		var thisemail = $("input[name='stru_e']").val();
		var thisname = $("input[name='stru_n']").val();
		var thisenq = $("textarea[name='stru_en']").val();
		
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var regex = new RegExp(emailReg);
		if ((!regex.test(thisemail)) || thisemail==""){
			$("#stru_e").addClass('required');
			hasError = true;
		}
		if (thisname==""){
			$("#stru_n").addClass('required');
			hasError = true;
		}
		if (thisenq==""){
			$("#stru_en").addClass('required');
			hasError = true;
		}
		if (hasError == true)
	        {
					
				$("#flash").fadeIn(400).html('&nbsp;<span class="loading">Please enter your name and a valid email address.</span>');
				
	        }
	        else
	        {
			
	            $.ajax({
	                type: "POST",
	                url: "/scripts/sendemail.asp",
	                data: dataString,
	                cache: false,
	                success: function(html) {
					
					$('#cnForm').hide();
					$("#flash").fadeIn(400).html('&nbsp;<span class="loading">'+html+'</span>');
	                }
	            });
	        }
	  return false;
	});
	
	
	
});








