// JavaScript Document

$(document).ready(function() {
	
	//if create account button is clicked
	$('#regsubmit').click(function () {
								 
	 var name = $("input#name").val();
	  var email = $("input#email").val();
	  var password = $("input#password").val();
	  var confirmpassword = $("input#confirmpassword").val();
	  
	  $(".error").hide();
	  var hasError = false;

	  var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		if(email == '') {
			$("#email").after('<span class="error">You forgot to enter your email address.</span>');
			hasError = true;
		} else if(!emailReg.test(email)) {	
			$("#email").after('<span class="error">Enter a valid email address.</span>');
			hasError = true;
		}

	  
	  if(name == '') {
			$("#name").after('<span class="error">You forgot to enter your name.</span>');
			hasError = true;
		}
		
	  if(password == '') {
			$("#password").after('<span class="error">You forgot to enter a password.</span>');
			hasError = true;
		}
		
	  if(password != confirmpassword) {
			$("#confirmpassword").after('<span class="error">Your password doesn\'t match.</span>');
			hasError = true;
		}
		
		
	 //check the email exists or not from ajax
	 $.post("http://www.yorkscene.com/members/user_availability.php",{ e_mail:email } ,function(data)
	 {
	  if(data=='no') //if username not avaiable
	  {
	   $("#email").after('<span class="error">This email already exists.</span>').fadeTo(900,1);
		
	  }
	  else
	  {
	   doReg();
	  }
	 });


   function doReg()
{
	  var dataString = 'name='+ name + '&email=' + email + '&password=' + password + '&confirmpassword=' + confirmpassword;
		
	if(hasError == false) {

	  $.ajax({
      type: "POST",
      url: "http://www.yorkscene.com/members/register.php",
      data: dataString,
      success: function(html) {
        $('#modal_contents').html("<div id='message' align='center'></div>");
        $('#message').html("<h3>Registration Submitted!</h3>")
        //.append("<p>Your account information has been mailed to your email address! <br /> When you receive it please follow the instructions to verify your e-mail address.</p>")
		$("#message").append(html)
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='images/check.png' />");  
        });
		
      }
     });
	  
	} // end if hasError
		
	}
	
    return false;
	});		
			
			
	
	//if login button is clicked
	$('#loginsubmit').click(function () {
								 
	 var username = $("input#login_username").val();
	  var password = $("input#login_password").val();
	  
	  $(".error2").hide();
	  var hasError2;

	  
	  if(username == '') {
			$("#login_username").after('<span class="error2">You forgot to enter a username.</span>');
			hasError2 = true;
		}
		
	  if(password == '') {
			$("#login_password").after('<span class="error2">You forgot to enter a password.</span>');
			hasError2 = true;
		}
		
		
	 //check the username exists or not from ajax
	 $.post("http://www.yorkscene.com/members/user_availability.php",{ e_mail:username } ,function(data)
	 {
	  if(data=='yes') //if username doesn't exist
	  {
		  
	   $("#login_username").after('<span class="error2">This username does not exist.</span>'+hasError2).fadeTo(900,1);
			
	  }
	  else
	  {
	   doLogin();
	  }
	 });
	 

	 //check the username and password
	/* $.post("http://www.yorkscene.com/members/check_login.php",{ user_name:username, password:password } ,function(data2)
	 {
	  if(data2=='no') //if login is incorrect
	  {
		  hasError2 = true;
	   $("#login_error").after('<span class="error2">Wrong username and/or password!</span>').fadeTo(900,1);
	   
			
	  }
	  
	 });
	 
	 alert(hasError2); */

  function doLogin()
{
	  var dataString = 'username='+ username + '&password=' + password;
		
	if(!hasError2 == true) {

	  $.ajax({
      type: "POST",
      url: "http://www.yorkscene.com/members/login.php",
      data: dataString,
      success: function(html) {
        $('#login_error').html("<h3 id='message' align='center'></h3>");
        //$('#message').html("You are logged in!")
		$("#message").html(html)
        //.hide()
		
      }
     });
	  
	} // end if hasError2
		
	}
	
    return false;
	});	
	
	
	//if edit account button is clicked
	$('#editsubmit').click(function () {
	
	  var memberID = $("input#editusername").val();
	  var oldpassword = $("input#oldpassword").val();
	  var password = $("input#editpassword").val();
	  var confirmpassword = $("input#confirmeditpassword").val();
	  
	  $(".error3").hide();
	  var hasError3 = false;

	
		
	  if(password == '') {
			$("#password").after('<span class="error">You forgot to enter a password.</span>');
			hasError3 = true;
		}
		
	  if(password != confirmpassword) {
			$("#confirmpassword").after('<span class="error">Your password doesn\'t match.</span>');
			hasError3 = true;
		}
		
		
		//remove all the class add the messagebox classes and start fading
//$("#username").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
	 //check the username exists or not from ajax
	 $.post("http://www.yorkscene.com/members/user_password.php",{ user_ID:memberID, oldpassword:oldpassword } ,function(data)
	 {
	  if(data=='no') //if password is wrong
	  {
	   $("#oldpassword").after('<span class="error">Your password is incorrect.</span>').fadeTo(900,1);
			
	  }
	  else
	  {
	   doEdit();
	  }
	 });


  function doEdit()
{
	  var dataString = 'memberID='+ memberID + '&password=' + password + '&confirmpassword=' + confirmpassword;
		
	if(hasError3 == false) {

	  $.ajax({
      type: "POST",
      url: "http://www.yorkscene.com/members/editMember.php",
      data: dataString,
      success: function(html) {
        $('#modal_contents_edit').html("<div id='message' align='center'></div>");
        $('#message').html("<h3>New Password Submitted!</h3>")
        //.append("<p>Your account information has been mailed to your email address! <br /> When you receive it please follow the instructions to verify your e-mail address.</p>")
		$("#message").append(html)
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='images/check.png' />");  
        });
		
      }
     });
	  
	} // end if hasError3  
		
	}
	
    return false;
	});		
	
});	


