//Load jQuery
google.load("jquery", "1.4");

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false
	 }

	 return true
}

// effectively 'on document ready'
google.setOnLoadCallback(function() {
	
	var J = jQuery.noConflict();
	J(function($) {
		
		if ($('#EntryForm').length) {
			
			var count = 1;
			$('.CompositeField:not(#PeopleComposite)').each(function(){
				var $this = $(this);
				
				if (count < 4) {
					$this.append('<h3 class="nextjs" style="clear: both"><a href="#">Next Photo</a></h4>').children('h3:last').children('a').click(function() {
						$(this).parent().parent().next('.CompositeField').children('h3:first').click();
						return false;
					});
				}
                else {
                    $this.append('<h3 class="nextjs" style="clear: both">&nbsp;</h4>').children('h3:last').children('a');
                }
				
				var Height = $this.height();
				
				if(count > 1) {
					$this.css({
						height: '35px',
						overflow: 'hidden'
					});
				}
				else {
					$this.addClass('on');
				}
				$this.css({
					border: '1px solid #79A33F',
					marginTop: '-1px'
				}).children('h3:first').css({
					paddingLeft: '25px',
					background: 'url(themes/sdaonb/images/down-arrow.png) top left no-repeat',
					cursor: 'pointer',
					display: 'block',
					width: 'auto'
				}).append(' <span>Click here to add a photo</span>').click(function() {
					$this.stop().animate({'height':Height + 'px'},'slow',null,$this.addClass('on'));
					$this.siblings('.on:not(#PeopleComposite)').animate({'height':'35px'},'slow',null,$(this).removeClass('on'));
				}).hover(function() {
					$(this).css('text-decoration','underline');
				},function() {
					$(this).css('text-decoration','none');
				});
				count += 1;
			});
			
			var $submit = $('#Form_RegistrationForm_action_SignupAction');
			$submit.click(function() {
			var $name = $('#Form_RegistrationForm_Name');
			var $email = $('#Form_RegistrationForm_Email');
			var $tac = $('#Form_RegistrationForm_TCs');
				var error = false;
				if (!$name.val().replace(/^\s+|\s+$/g,"")) {
					if (!$('#NameError').length) {
						$name.parent().append('<div id="NameError"><label for="Form_RegistrationForm_Name">Please enter your name</label></div>').addClass('required');
					}
					error = true;
				}
				else {
					$name.parent().removeClass('required').children('#NameError').remove();
				}
				if (!$email.val().replace(/^\s+|\s+$/g,"")) {
					if (!$('#EmailError').length) {
						$email.parent().append('<div id="EmailError"><label for="Form_RegistrationForm_Email">Please enter your email address</label></div>').addClass('required');
					}
					error = true;
				}
				else if (!echeck($email.val())) {
					if (!$('#EmailError.invalid').length) {
						$email.parent().removeClass('required').children('#EmailError').remove();
					}
					$email.parent().append('<div id="EmailError" class="invalid"><label for="Form_RegistrationForm_Email">Please enter a valid email address</label></div>').addClass('required');
					error = true;
				}
				else {
					$email.parent().removeClass('required').children('#EmailError').remove();
				}
				if (!$tac.attr('checked')) {
					if (!$('#TCsError').length) {
						$tac.parent().append('<div id="TCsError"><label for="Form_RegistrationForm_TCs">Please agree to out Terms &amp; Conditions</label></div>').addClass('required');
					}
					error = true;
				}
				else {
					$tac.parent().removeClass('required').children('#TCsError').remove();
				}
				
				var count = 1;
				var hasPic = false;
				var errors = new Array();
				$('.CompositeField:not(#PeopleComposite)').each(function() {
					var $title = $('#Form_RegistrationForm_Title' + count);
					if ($title.val().replace(/^\s+|\s+$/g,"")) { // if it has a value after whitespace is removed
						hasPic = true;
						var $caption = $('#Form_RegistrationForm_Caption' + count);
						if (!$caption.val().replace(/^\s+|\s+$/g,"")) {
							errors.push('Please enter a caption for photo ' + count);
						}
						var $location = $('#Form_RegistrationForm_Location' + count);
						if (!$location.val().replace(/^\s+|\s+$/g,"")) {
							errors.push('Please enter a location for photo ' + count);
						}
						var $file = $('#Form_RegistrationForm_Photo' + count + 'ID');
						if (!$file.val()) {
							errors.push('Please pick an image to upload for photo ' + count);
						}
					}
					count += 1;
				});
				if (!hasPic) {
					errors.push('You must upload at least one picture');
				}
				if (errors.length) {
					error = true;
					var errString = '';
					for (i in errors) {
						if (errors[i].indexOf('function') == -1) {
							errString += '<h3>' + errors[i] + '</h3>';
						}
					}
					$('#ErrorBox').html(errString);
				}
				else {
					$('#ErrorBox').html('');
				}
				
				return !error;
			});
			
		}
		
		 if ($('#VotingWizard').length) {
			$('tbody tr').addClass('js').css('cursor','pointer').hover(function() {
			   $(this).stop().animate({
				  backgroundColor:'#b9ffb9'
			   });
			},function() {
			   if ($(this).is('.odd')) {
				  var color = '#e0e0e0';
			   }
			   else {
				  var color = '#E9E9E9';
			   }
			  $(this).stop().animate({
			   backgroundColor: color
			  });
			}).click(function() {
			   $('input',this).attr('checked','checked');
			});
			
			$('td a').fancybox();
		 }
		
	});
});

//Google analytics tracking
/*var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-5532417-24']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();*/
