$().ready(function() {
	var options = { 
        target:        '#success',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
    }; 
	
	var myHeights=new Array(
	'0','3\' (91cm)','3\'1" (94cm)','3\'2" (97cm)','3\'3" (99cm)','3\'4" (102cm)','3\'5" (104cm)','3\'6" (107cm)','3\'7" (109cm)','3\'8" (112cm)','3\'9" (114cm)','3\'10" (117cm)','3\'11" (119cm)',
	'4\' (122cm)','4\'1" (124cm)','4\'2" (127cm)','4\'3" (130cm)','4\'4" (132cm)','4\'5" (135cm)','4\'6" (137cm)','4\'7" (140cm)','4\'8" (142cm)','4\'9" (145cm)','4\'10" (147cm)','4\'11" (150cm)',
	'5\' (152cm)','5\'1" (155cm)','5\'2" (158cm)','5\'3" (160cm)','5\'4" (163cm)','5\'5" (165cm)','5\'6" (168cm)','5\'7" (170cm)','5\'8" (173cm)','5\'9" (175cm)','5\'10" (178cm)','5\'11" (180cm)',
	'6\' (183cm)','6\'1" (185cm)','6\'2" (188cm)','6\'3" (191cm)','6\'4" (193cm)','6\'5" (197cm)','6\'6" (198cm)','6\'7" (201cm)','6\'8" (203cm)','6\'9" (206cm)','6\'10" (208cm)','6\'11" (211cm)',
	'7\' (213cm)','7\'1" (216cm)','7\'2" (218cm)','7\'3" (221cm)','7\'4" (224cm)','7\'5" (226cm)','7\'6" (229cm)','7\'7" (231cm)','7\'8" (234cm)','7\'9" (236cm)','7\'10" (239cm)','7\'11" (241cm)'
	);
	
 $('#slider_callout').hide(); 
    var calloutVisible = false; 
    $('.slider').slider({ 
        handle: '.slider_handle', 
        minValue: 0, 
        maxValue: 100, 
        start: function(e, ui) { 
            $('#slider_callout').fadeIn('fast', function() { calloutVisible = true;}); 
        }, 
        stop: function(e, ui) { 
            if (calloutVisible == false) { 
                $('#slider_callout').fadeIn('fast', function() { calloutVisible = true;}); 
                $('#slider_callout').css('left', ui.handle.css('left')).text(myHeights[Math.round(ui.value * (61/100))]); 
            } 
            $('#slider_callout').fadeOut('fast', function() { calloutVisible = false; }); 
        }, 
        slide: function(e, ui) { 
            $('#slider_callout').css('left', ui.handle.css('left')).text(myHeights[Math.round(ui.value * (61/100))]); 
        },
		change: function(e, ui) {
			$('#cheight').val(myHeights[Math.round(ui.value * (61/100))]);
		} 
    }); 
		
	// validate signup form on keyup and submit
	$("#user_register").validate({
		rules: {
			title: "required",
			firstname: "required",
			lastname: "required",
			dateday: "required",
			datemonth: "required",
			dateyear: "required",
			gender: "required",
			ethnicity: "required",
			ethnicityother: { 
				required: function(element) {
       			 return $("#cethnicity").val() == 0;
				}
			},
			postcode: "required",
			house: "required",
			district: "required",
			email: {
				required: true,
				email: true
			},
			email2: {
				required: true,
				email: true,
				equalTo: "#cemail"
			},
			username: {
			  required: true,
			  rangelength: [3, 20],
			  remote: {
				url: "/jquery/ajax/checkuser.php",
				type: "post",
				data: {
				  user: function() {
					return $("#cusername").val();
				  }
				}
			  }
			},
			password: {
				required: true,
				rangelength: [6, 30]
			},
			password2: {
				required: true,
				rangelength: [6, 30],
				equalTo: "#cpassword"
			},
			disabilitytype: {
			  required: "#cdisabilityyes:checked"
			},
			disabilityother: { 
				required: function(element) {
       			 return $("#cdisabilitytype").val() == 0;
				}
			},
			newsport: "required",
			gpreferred: "required",
			gptold: "required",
			toldbyother: {
			  required: "#cgptoldother:checked"
			}
		},
		messages: {
			title:  "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please select your title",
			firstname: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please enter your first name",
			lastname: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please enter your last name",
			dateday: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please select your day of birth",
			datemonth: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please select your month of birth",
			dateyear: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please select your year of birth",
			gender: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please select a gender",
			ethnicity: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please select an ethnicity",
			ethnicityother: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please enter an ethnicity",
			postcode: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please enter your post code",
			house: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please enter your house name or number",
			district: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please select your district",
			email: {
				required: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please enter an email address",
				email: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please enter a valid email address"
			},
			email2: {
				required: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please enter an email address",
				email: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please enter a valid email address",
				equalTo: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Your email addresses must match"
			},
			username: {
				required: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please enter a username",
				rangelength: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Your username must be between 3 and 20 characters",
				remote: jQuery.format("<img src='/images/exclamation.png' alt='The data you entered is not valid' /> {0} is already in use")
			},
			password: {
				required: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please enter a password",
				rangelength: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Your password must be between 6 and 30 characters"
			},
			password2: {
				required: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please confirm your password",
				rangelength: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Your password must be between 6 and 30 characters",
				equalTo: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Your passwords do not match"
			},
			disabilitytype: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please select the appropriate option",
			disabilityother: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please specify a disability",
			newsport: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please select an option",
			gpreferred: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please select an option",
			gptold: "<img src='/images/exclamation.png' alt='The data you entered is not valid' /> Please select an option"
		},
		submitHandler: function(form) {
			$("#user_register").ajaxSubmit({
					target: "#registerstatus",
					beforeSubmit:  showRequest,  
					success:       showResponse 
				});
		}
	});
			
	$("#cethnicity").change(function() {
	 	if ($("#cethnicity").val() == 0)
		{
		  $("#eth_other").show();
		  
		}
		else
		{
		  $("#eth_other").hide();
		}
	});
		
	$("#cdisabilityyes").click(function() {
		  $("#disability_type").show();
	});
	
	$("#cdisabilityno").click(function() {
		  $("#disability_type").hide();
		  $("#disability_other").hide();
	});
	
	$("#cdisabilitytype").change(function() {
	 	if ($("#cdisabilitytype").val() == 0)
		{
		  $("#disability_other").show();
		  
		}
		else
		{
		  $("#disability_other").hide();
		}
	});
	
	$("#cgptoldyes").click(function() {
		  $("#toldby_other").hide();
	});
	
	$("#cgptoldno").click(function() {
		  $("#toldby_other").hide();
	});
	
	$("#cgptoldother").click(function() {
		  $("#toldby_other").show();
	});
});

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
	$("#registercontainer").hide();
	$("#registerstatus").show();
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
	setTimeout('window.location = "/login";',5000);		
} 

