// JavaScript Document
var _siteRoot='index.php',_root='index.php';

$(function(){
	$('.error_note').hide();
	$('.lead_error').hide();
	$('#parent_details').hide();
	$('.band_member').hide();
	$('#terms_container').hide();
	$('.waiting').hide();
	$('#change_password_form').hide();
	$('#change_photo_form').hide();
	$('.hidden').hide();
	
	// Add another image, application_form_secretwars
	$('#add_another_image').click(function(){
		$(this).parent().hide();
		$('#image_1_area').slideDown();
	});
	$('#add_another_image_1').click(function(){
		$(this).parent().hide();
		$('#image_2_area').slideDown();
	});
	
	// Account change photo
	$('#changePhoto').click(function(){
		$('#change_password_form').hide();
		$('#change_photo_form').slideDown();											 
	});
	$('#changePassword').click(function(){
		$('#change_password_form').slideDown();													 
		$('#change_photo_form').hide();											 
	});
	$('.cancel_password').click(function(){
		$('#change_password_form').slideUp();																		 
	});
	$('.cancel_photo').click(function(){
		$('#change_photo_form').slideUp();																		 
	});
	$('#form_change_photo').submit(function(){
		$('#user_image').removeClass('error');
		if($('#user_image').val() == ''){
			$('#error_user_image').slideDown();
			$('#user_image').addClass('error');
			return false;
		}
	});
	
	// Change password form
	$('#form_change_password').submit(function(){
		$('.not_null').removeClass('error');
		$('#form_change_password .error_note').hide();
		var valid = true;
		var notNulls = $('#form_change_password .not_null');
		$.each(notNulls, function(){
			if($(this).val() == ''){
				$(this).addClass('error');
				var id = "error_"+$(this).attr('id');
				$('#'+id).show();	
				if(valid == true){
					$(this).focus();
				} 
				valid = false;
			}
		});						
		if($('#new_password').val() != $('#new_password_confirm').val()){
			$('#error_new_password_confirm').show();
			$('#error_new_password').show();
			$('#new_password').addClass('error');
			$('#new_password_confirm').addClass('error');
			valid = false;
		}
		if(valid == false){
			return false;
		}
	});
	
	// Application form
	$('#application_form').submit(function(){
		$('.not_null').removeClass('error');
		$('.not_null_select').removeClass('error');
		$('.error_note').hide();
		var valid = true;
		var notNulls = $('#.not_null:visible');
		$.each(notNulls, function(){
			if($(this).val() == ''){
				$(this).addClass('error');
				var id = "error_"+$(this).attr('id');
				$('#'+id).show();	
				if(valid == true){
					$(this).focus();
				} 
				valid = false;
			}
		});
		
		var notNulls = $('.not_null_select:visible');
		$.each(notNulls, function(){
			if($('option:selected',this).val() == ''){
			var id = "error_"+$(this).attr('id');
				$(this).addClass('error');
				$('#'+id).show();	
				if(valid == true){
					$(this).focus();
				} 
				valid = false;
			}
		});
		if($('#track').length){
			strLen = $('#track').val().length;
			var temp = $('#track').val().substr(strLen-4);
			if(!strchr(temp, '.mp3')){
				$('#error_track_type').show();
				$('#track').val('');
				valid = false;
			}
		}
		if(!echeck($('#email').val())){
			$('#error_email').show();
			valid = false;
		}
		if($('#accept_terms_section').val() == 1){
			if(!$('#accept_terms:checked').length){
				$('#error_accept_terms').show();
				return false;
			}
		} else {
			if(valid == false){
				$('.lead_error').slideDown();
				return false;
			} else {
				if($('#consent').val() == 1){
					$('#main_form').hide();
					$('#terms_container').show();
					$('#accept_terms_section').val('1');
					return false;
				}
			}
		}
		$('.button_submitentry').hide();
		$('.waiting').show()
	});
	
	$('.addlink').focus(function(){
		if($(this).val() == 'http://'){
			$(this).val('');
		}
	});
	$('.addlink').blur(function(){
		if($(this).val() == ''){
			$(this).val('http://');
		}
	});
	
	// Leave comment
	$('#leave_comment').submit(function(){
		$('.not_null').removeClass('error');
		var valid = true;
		var notNulls = $('.not_null:visible');
		$.each(notNulls, function(){
			if($(this).val() == ''){
				$(this).addClass('error');
				var id = "error_"+$(this).attr('id');
				$('#'+id).show();	
				if(valid == true){
					$(this).focus();
				} 
				valid = false;
			}
		});																		
		if(valid == false){
			return false;
		}
	});
	// Contact form
	$('#contact_form').submit(function(){
		$('.not_null').removeClass('error');
		$('.error_note').hide();
		var valid = true;
		var notNulls = $('.not_null:visible');
		$.each(notNulls, function(){
			if($(this).val() == ''){
				$(this).addClass('error');
				var id = "error_"+$(this).attr('id');
				$('#'+id).show();	
				if(valid == true){
					$(this).focus();
				} 
				valid = false;
			}
		});
		if(!echeck($('#email').val())){
			$('#error_email').show();
			valid = false;
		}
		if(valid == false){
			$('.lead_error').slideDown();
			return false;
		}
	});
	
	if($('#login_email').length){
		$('#login_email').val('');
	}
	
	if($('#login_password').length){
		$('#login_password').val('');
	}
	// login form
	$('#login_form').submit(function(){
		$('.not_null').removeClass('error');
		$('.error_note').hide();
		var valid = true;
		var notNulls = $('#login_form .not_null:visible');
		$.each(notNulls, function(){
			if($(this).val() == ''){
				$(this).addClass('error');
				var id = "error_"+$(this).attr('id');
				console.log(id);
				$('#'+id).show();	
				if(valid == true){
					$(this).focus();
				} 
				valid = false;
			}
		});
		if(!echeck($('#login_email').val())){
			$('#error_login_email').show();
			valid = false;
		}
		if(valid == false){
			$('.lead_error').slideDown();
			return false;
		}
	});
		
	// Band members
	$('#no_of_band_members').change(function(){
		$('.band_member').hide();
		var members = $('option:selected', this).val();
		if(members>=1){
			count = 1;
			while(count<=members){
				$('#band_member_'+count).slideDown();
				count++;
			}
		}
	});
	
	$('#dob_month').change(function(){
		checkDOB();																
	});
	$('#dob_year').change(function(){
		checkDOB();																
	});
	$('#dob_day').change(function(){
		checkDOB();																
	});
	
	// Register form
	$('#register_form').submit(function(){
		var valid = true;
		$('.error').removeClass('error');
		$('#register_form .error_note').hide();
		if(!echeck($('#register_email').val())){
			$('#register_email').addClass('error');
			$('#error_register_email').show();
			valid = false;
		}
		if($('#register_nickname').val() == ''){
			$('#error_register_nickname').show();
			$('#register_nickname').addClass('error');
			valid = false;
		} else if($('#register_nickname').val().length < 4){
			$('#error_register_nickname_length_small').show();
			$('#register_nickname').addClass('error');
			valid = false;
		}
		if($('#register_password').val() == ''){
			$('#error_register_password').show();
			$('#register_password').addClass('error');
			valid = false;
		} else if($('#register_password').val() != $('#register_password_confirm').val()){
			$('#error_register_password_confirm').show();
			$('#register_password').addClass('error');
			$('#register_password_confirm').addClass('error');
			valid = false;
		}
		if(!$('#register_agree:checked').length){
			$('#error_register_agree').show();
			$('#register_agree').addClass('error');
			valid = false;
		}
		if(valid == false){
			return false;
		}
	});
	
	$('.votenote').click(function(){
		var id = $(this).attr('id').split('_');
		$('#voteId').val(id[1]);		
	});
	
	$('#voteNowButton').click(function(){
		$(this).parent().parent().submit();														 
	});
	$('.loginNow').click(function(){
		$(this).parent().parent().submit();														 
	});
	$('ol#music_list li').hover(function(){
		$(this).css('background', '#464646 url(/images/music_list_background.jpg) bottom repeat-x');
	}, function(){
		$(this).css('background', '');
	});
});

function checkDOB(){
	if($('#dob_day option:selected').val() >= 1){
		if($('#dob_month option:selected').val() >= 1){
			if($('#dob_year option:selected').val() >= 1){
				var now = new Date();
				var then = new Date();
				then.setUTCFullYear($('#dob_year option:selected').val());
				then.setUTCMonth($('#dob_month option:selected').val()-1);
				then.setUTCDate($('#dob_day option:selected').val());
				diff = ((((now-then)/1000)/60)/60)/24;
				if(diff<=6209){
					//	if they are under 17
					$('#parent_details').slideDown();
					var button = $('#application_submit');
					button.removeClass('button_submitentry');
					button.addClass('button_confirmation');
					button.html('<span>Confirmation</span>');
					$('#consent').val('1');
				} else {
					// If they are over 17
					$('#parent_details').slideUp();
					var button = $('#application_submit');
					button.removeClass('button_confirmation');
					button.addClass('button_submitentry');
					button.html('<span>Sumbit Entry</span>');
					$('#consent').val('0');
				}
			}
		}
	}
}

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					
}

function strchr ( haystack, needle) {
    return strstr( haystack, needle);
}

function strstr( haystack, needle, bool ) {
    var pos = 0;
    haystack += '';
    pos = haystack.indexOf( needle );
    if (pos == -1) {
        return false;
    } else{
        if( bool ){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}

// jQuery
    $(function () {
  // IE6 doesn't handle the fade effect very well - so we'll stick with
  // the default non JavaScript version if that is the user's browser.
  if ($.browser.msie && $.browser.version < 7) return;
  
  $('#follow_us li')
  
    // remove the 'highlight' class from the li therefore stripping 
    // the :hover rule
    .removeClass('highlight')
    
    // within the context of the li element, find the a elements
    .find('a')
    
    // create our new span.hover and loop through anchor:
    .append('<span class="hover" />').each(function () {
      
      // cache a copy of the span, at the same time changing the opacity
      // to zero in preparation of the page being loaded
      var $span = $('> span.hover', this).css('opacity', 0);
      
      // when the user hovers in and out of the anchor
      $(this).hover(function () {
        // on hover
        
        // stop any animations currently running, and fade to opacity: 1
        $span.stop().fadeTo(500, 1);
      }, function () {
        // off hover
        
        // again, stop any animations currently running, and fade out
        $span.stop().fadeTo(500, 0);
      });
    });
});
$(function () {
  // IE6 doesn't handle the fade effect very well - so we'll stick with
  // the default non JavaScript version if that is the user's browser.
  if ($.browser.msie && $.browser.version < 7) return;
  
  $('#partners li')
  
    // remove the 'highlight' class from the li therefore stripping 
    // the :hover rule
    .removeClass('highlight')
    
    // within the context of the li element, find the a elements
    .find('a')
    
    // create our new span.hover and loop through anchor:
    .append('<span class="hover" />').each(function () {
      
      // cache a copy of the span, at the same time changing the opacity
      // to zero in preparation of the page being loaded
      var $span = $('> span.hover', this).css('opacity', 0);
      
      // when the user hovers in and out of the anchor
      $(this).hover(function () {
        // on hover
        
        // stop any animations currently running, and fade to opacity: 1
        $span.stop().fadeTo(500, 1);
      }, function () {
        // off hover
        
        // again, stop any animations currently running, and fade out
        $span.stop().fadeTo(500, 0);
      });
    });
});
$(function () {
  // IE6 doesn't handle the fade effect very well - so we'll stick with
  // the default non JavaScript version if that is the user's browser.
  if ($.browser.msie && $.browser.version < 7) return;
  
  $('#enter_comps li')
  
    // remove the 'highlight' class from the li therefore stripping 
    // the :hover rule
    .removeClass('highlight')
    
    // within the context of the li element, find the a elements
    .find('a')
    
    // create our new span.hover and loop through anchor:
    .append('<span class="hover" />').each(function () {
      
      // cache a copy of the span, at the same time changing the opacity
      // to zero in preparation of the page being loaded
      var $span = $('> span.hover', this).css('opacity', 0);
      
      // when the user hovers in and out of the anchor
      $(this).hover(function () {
        // on hover
        
        // stop any animations currently running, and fade to opacity: 1
        $span.stop().fadeTo(500, 1);
      }, function () {
        // off hover
        
        // again, stop any animations currently running, and fade out
        $span.stop().fadeTo(500, 0);
      });
    });
});