
//auth procedure for the login form
$(document).ready(function(){

var okName  = false;
	
$("#signupform").ajaxForm({
	dataType : 'json',
	type: 'POST',
	url: '/modules/user/newuserg.php',
	data: {
			mode : 'signUp'
		},
	beforeSubmit: validate,
	success: doResponse
	
});

function doResponse(responseText, statusText){
	if(responseText.status == "Success"){
		$("#success_bar").removeClass('error').addClass('success').html("<p>You have been sent a confirmation email, please follow the instructions in it to finalise your sign up to Childhood Nannies & Carers.</p> <p>This is a necessary measure to combat fraudulent activity and to safeguard your privacy.</p>").show();
		
		
		$("#signupform").clearForm();
		$("#signupform").hide();
		
	}else{
		//send the text lines back!
		$("#eerror_bar").html(responseText.status).show();
	}	
}

function validate(formData, jqForm, options) { 
    // jqForm is a jQuery object which wraps the form DOM element 
    // 
    // To validate, we can access the DOM elements directly and return true 
    // only if the values of both the username and password fields evaluate 
    // to true 
	

	
    var form = jqForm[0]; 

    if ($(form.password).val().length < 7) { 
    	$("#eerror_bar").html("Please type a password of at least 8 characters!").show();
        return false; 
    }
    
    if (form.password.value != form.password2.value) { 
    	$("#eerror_bar").html("Your passwords do not match!").show();
        return false; 
    }
    
    if (!form.email.value) { 
    	$("#eerror_bar").html("Please enter your email address!").show();
        return false; 
    }
    
    if (form.typelogin.value == "") { 
    	$("#eerror_bar").html("Please choose an account option!").show();
        return false; 
    }
    
    if (!form.firstname.value) { 
    	$("#eerror_bar").html("Please enter your first name!").show();
        return false; 
    }
    
    if (!form.lastname.value) { 
    	$("#eerror_bar").html("Please enter your last name!").show();
        return false; 
    }
    
    
    //finally check on username
/*    if(okName == false){
    	$("#eerror_bar").html("Please use another email address, that one is already being used.").show();
    	return false;
    }*/
    
	$("#eerror_bar").hide('slow');
    
}

	 $("#email999").keyup(function()
			 {
			 var username = $(this).val();
			 if(username=='' || username.length < 5)
			 {
				 //$("#displaysearch").html(""); 
			 }
			 else
			 {
			 $.ajax({
			 type: "POST",
			 url: "/modules/user/newuserg.php",
			 data: {
				 username : username,
				 mode : 'checkUN'
			 },
			 dataType: 'json',
			 cache: false,
			 success: function(html)
			 {
			 //$("#displaysearch").html(html).show();
				 if(html.status == "Fail"){
					 $("#eerror_bar").html("- Please use another email address, that one is already being used.").show();
					 okName = false;
				 }else{
					 $("#eerror_bar").hide('slow');
					 okName = true;
				 }
			 }
			 });
			 }return false;
			 });

});

