$(document).ready(function() {

  // Prevent access to the next steps
	$('.step_one').addClass('step_selected');
	$('.step_two').addClass('notyet');
	$('.step_three').addClass('notyet');
	$('.step_four').addClass('notyet');
		

	// When the url/email is changed, open the next step
	$('.step_one').click(function() {
		$('.step_selected').addClass('notyet').removeClass('step_selected');
		$('.step_one').removeClass('notyet').addClass('step_selected');
	});

	$('.step_two').click(function() {
		$('#url').valid(function () {
			$('#url').after('<img class="ajax_loader" src="/images/ajax-loader.gif" alt="" /><span class="ajax_loader" >Verifying...</span>');
		})
		
		if($('#url').valid()) {
			$('.step_selected').removeClass('step_selected').addClass('notyet');
			$('.step_two').removeClass('notyet').addClass('step_selected');
		}		
	});
	
	$('.step_three').click(function() {
		if($('#url').valid() && $('#email').valid()) {
			$('.step_selected').removeClass('step_selected').addClass('notyet');
			$('.step_three').removeClass('notyet').addClass('step_selected');
			$('#key').val("<meta name='verify-mb' content='"+$.md5($('#url').val()).substr(0,20)+"' />");
			$('.download_button_a').attr('href','/download/'+$.md5($('#url').val()).substr(0,20));			
			$('.giantcheckbox').removeClass('disabled');
		}		
	});	
	
	$('.step_four').click(function() {
		if($('#url').valid() && $('#email').valid()) {
			$('.step_selected').removeClass('step_selected').addClass('notyet');
			$('.step_four').removeClass('notyet').addClass('step_selected');
		}		
	});	
	
	// Giant checkbox it!
	$('.giant-checkbox').each(function() {
		$(this).giantcheckbox({size:'39px',bgcolor:'url(/images/giant_tick_bg.png) 0 0 no-repeat;padding:10px 0 0 10px',dborder:'none',margin:'0',resize:false}); 
	});	
	//$('.giantcheckbox').addClass('disabled');
			
	// Prevent submit
	$('#submission').submit(function() {
		if (html_result == 1) {
			$('.ajax_loader').remove();
			return true;
		} else if (html_result == -1) {
			return false;
		} else {
		if ($('.giant-checkbox').is(':checked')) {
			
			$('.submit_button').after('<img class="ajax_loader" src="/images/ajax-loader.gif" alt="" /><span class="ajax_loader" >Verifying...</span>');
			html_result = -1;
			
		  // 0 Page offline
		  // 1 Validated properly
		  // 2 Page exists no validation key
		  // 3 Not a valid demo store				

			$.ajax({
			   type: "POST",
			   async: false,
			   url: "/inc/ajax/validation_check.inc.php",
			   data: "url="+$('#url').val(),
			   success: function(html) {
			     html_result = html;	
			   },
			   complete: function(){
					if (html_result == 0) {
						Sexy.alert("<h1>Domain not found</h1><br />We just tried to access the page and didn't have any luck - did you type in a correct URL?");
						$('.step_one').click();
						$('.ajax_loader').remove();
						return false;						
					} else if (html_result == 1) {	
						$('#submission').submit();
						return true;
					} else if (html_result == 2) {
						Sexy.alert("<h1>Store cannot be validated</h1><br />It doesn't look like you have put the validation key in place. View our <a href='/how-to/' title='tutorials'>tutorials</a> if you need help");
						$('.step_three').click();
						$('.ajax_loader').remove();
						return false;
					} else if (html_result == 3) {
						Sexy.alert("<h1>Domain does not qualify as a demo store</h1><br />We just tried to access the page but it didn't look like a demo store. Please <a href='mailto:info@magentobenchmark.com'>email us</a> if you think you are recieving this message in error");
						$('.ajax_loader').remove();
						return false;							
					} else {
						Sexy.alert("<h1>Something has gone wrong</h1><br />We don't know why, but this script has failed to execute. You best <a href='mailto:info@magentobenchmark.com'>email</a> us.");
						$('.ajax_loader').remove();
						return false;							
					}
			   }
			 });
			}
		}
		
		return false;
	});

	// Validate the form
	$("#submission").validate({    
			rules: {
				'url': { 
					url: true,
					required: true
				},
				'email': {
					email: true,
					required: true			
				}
		},
		
		messages: {
			'url': { remote: jQuery.format("{0} is not a valid Magento demo store") }
		},
		
		onkeyup: false
	});
		
});
