/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var obj = {              
              basePath : '/wp-content/themes/k2badminton/membership/process/',
              company  : 'K2Badminton'         
          },
url = obj.basePath + 'memberShip.php',    
login_url = obj.basePath + 'login.php',
logout_url = obj.basePath + 'logout.php',
subscribe_url = obj.basePath + 'newsLetter.php';
signup_url = obj.basePath + 'signup.php';

$(function() {
    
    //$("#datepicker").datepicker();
    
    
    $("#datepicker").datepicker({
                        changeMonth: true,
			changeYear: true,
                        yearRange: "-100:+0",
                        maxDate: 'd'
		});
    
    $("#relation").bind("keydown", function(event) {
        // track enter key
        var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
        if (keycode == 13) { // keycode for enter key
            // force the 'Enter Key' to implicitly click the submit button
            $('#submit').click();return false;
        } else return true;
    }); // end of function
 
    $("#password").bind("keydown", function(event) {
        // track enter key
        var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
        if (keycode == 13) { // keycode for enter key
            // force the 'Enter Key' to implicitly click the submit button
            $('#btn_login').click();return false;
        } else return true;
    }); // end of function
    
    $("#Subscribe").bind("keydown", function(event) {
        // track enter key
        var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
        if (keycode == 13) { // keycode for enter key
            // force the 'Enter Key' to implicitly click the submit button
            $('#Subscribe').click();return false;
        } else return true;
    }); // end of function
    
    $("#c_pwd").bind("keydown", function(event) {
        // track enter key
        var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
        if (keycode == 13) { // keycode for enter key
            // force the 'Enter Key' to implicitly click the submit button
            $('#btn_pwd').click();return false;
        } else return true;
    }); // end of function
    
    $("#reset_txt").bind("keydown", function(event) {
        // track enter key
        var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
        if (keycode == 13) { // keycode for enter key
            // force the 'Enter Key' to implicitly click the submit button
            $('#reset_btn').click();return false;
        } else return true;
    }); // end of function
 
    $('#username').focus(function(){
        if ($('#username').val() == 'Username') $('#username').val('');
    });
    
    $('#password').focus(function(){
        if ($('#password').val() == 'password') $('#password').val('');
    });

    $('#subscribe').focus(function() {
        if ($('#subscribe').val() == 'E-mail Address') $('#subscribe').val('');
    });
 
    $('#logout').click(function() {
        
        $.ajax({
                type: 'POST',
                url: logout_url,
                dataType : 'json',
                data: {data : 'data'},
                success: function(data) {
                    if (data != null) window.location.href = 'index.php';
                }
        });
    });
    
    $('#reset_btn').click(function() {
        
        if (!validMailFormat('#reset_txt')) {
    		jAlert('Please enter a valid email id', obj.company,
    		function() {
    		   $('#reset_txt').focus();	
    		});
    		return;
    	}
        
        $.ajax({
                type: 'POST',
                url: obj.basePath + 'forgotPass.php',
                dataType : 'json',
                data: {to : $.trim($('#reset_txt').val())},
                success: function(data) {
                    if (data != null) {
                        
                        if (data) {
                            jAlert('We sent a mail. Please check it', obj.company);
                        } else {
                            jAlert('This email id is not in our record, Please check it', obj.company,
                            function(){
                              $('#reset_txt').focus();
                            });
                        
                        }
                        
                    }
                }
        });
    });
    
    /**
     * Method to reset password
     */
    $('#btn_pwd').click(function() {
        
        if (!$.trim($('#rst_pwd').val())) {
    		jAlert('Password field can not be blank', obj.company,
    		function() {
    		   $('#rst_pwd').focus();	
    		});
    		return;
    	}
        
        if ($('#rst_pwd').val() != $('#c_pwd').val()) {
    		jAlert('Miss match occured in password', obj.company,
    		function() {
    		   $('#c_pwd').focus();	
    		});
    		return;
    	}
        
        $.ajax({
                type: 'POST',
                url: obj.basePath + 'resetPass.php',
                dataType : 'json',
                data: {password : $('#rst_pwd').val(), id : $('#reset_id').val()},
                success: function(data) {
                    if (data != null) {
                        if(data) {
                            
                            jAlert('Thank you, Your password is changed successfuly', obj.company);
                            window.location.href = "index.php";
                            
                        } else {
                            jAlert('Failed to reset the account');
                        }
                        
                        
                    }
                }
        });
    });

    $('#Subscribe').click(function() {
        
    	
    	if (!validMailFormat('#subscribe')) {
    		jAlert('Please enter a valid email id', obj.company,
    		function() {
    		   $('#subscribe').focus();	
    		});
    		return;
    	}
    	
        $.ajax({
                type: 'POST',
                url: subscribe_url,
                dataType : 'json',
                data: {to : $('#subscribe').val()},
                beforeSend: function() {
                    $('#subscribeMsg').html('Please wait...');
                },
                success: function(data) {
                     $('#subscribeMsg').html('');
                    if (data != null) {
                    	jAlert('Mail send successfully', obj.company);
                        $('#subscribe').val('');
                    }
                }
        });
    });
    
    /**
     * Method to check email
     */
    function email()
    { 
        
        x=$("#email").val();
        emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        if($("#email").val() == "") {
            $("#emailError").html("The Email field is required");
            valid = false;
        } else if (!emailReg.test(x)) {
            $("#emailError").html("Invalid E-mail Address! Please re-enter.");
            valid = false;return false;
        } else {
        	$("#emailError").html(" ");
        }
	}
        
    /**
     * Method to check email
     */
    function mem_email()
    { 
        
        x=$("#mem_email").val();
        emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        if($("#mem_email").val() == "") {
            $("#emailError").html("The Email field is required");
            valid = false;
        } else if (!emailReg.test(x)) {
            $("#emailError").html("Invalid E-mail Address! Please re-enter.");
            valid = false;return false;
        } else {
        	$("#emailError").html(" ");
        }
	}
    function cpwd()
    {
        
 	    y=$("#pwd").val();
	    q=$("#cpwd").val();
            if(!$.trim($("#cpwd").val())){ 
                
                $("#cpwdError").html("The Confirm password field is required");
	        $("#cpwdError").css("color", "#D80000");
                valid = false;
	        return false;
            
	    }
            if(y!=q){ 
                
        	$("#cpwdError").html("The Password and Confirm password fields must match");
		$("#cpwdError").css("color", "#D80000");	
                valid = false;
		return false;
                
	    }
            else if(y.length < 6){
                
                
	        $("#pwdError").html("Your password must be at least 6 characters long. <br>Try again.");
                valid = false;
	        return false;
                
	    }
            else{
	        $("#cpwdError").html("Your password is accepted");
	        $("#cpwdError").css("color", "#02890F");
            }
            
	}
        
    function pwd()
    {
            
       var y=$("#pwd").val();
        if(!$.trim($("#pwd").val())) { 
            $("#pwdError").html("Please enter your password !!");
        }
        //check for minimum length password
	    else if(y.length < 6){
            $("#pwdError").html("Your password must be at least 6 characters long. <br>Try again.");
            $("#cpwdError").html("");
            valid = false;return false;
	    }
	    else {
            $("#pwdError").html("");
        }
                
    }
        
    function activation()
    {
        if($("#activation_type").val()=="") { 
            $("#activationError").html("Please select any type from the box !!");
            valid = false;return false;
	    }
        $("#activationError").html("");
    }
        
    function home_phone()
    {
        x = $("#home_phone").val();
        if($("#home_phone").val() == ""){
                 
            $("#hphoneError").html("The Phone field is required");
            valid = false;     
        }  
        else if(isNaN(x)||x.indexOf(" ")!=-1)
        {
            $("#hphoneError").html("Enter numeric value");
            valid = false;return false;
        }
        else
        {
        $("#hphoneError").html(" "); 
        }      
    }   

    /**
     * Method to check valid mail format
     */
    function validMailFormat(id)
    { 
        
        var x = $(id).val(),
        emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        
        if(x == "") {
            return false
        } else if (!emailReg.test(x)) {
            return false;
        } else {
        	return true;
        }
	}
       
    function contact_phone()
    {
        x =$("#contact_phone").val();
        if(isNaN(x)||x.indexOf(" ")!=-1)
        {
            $("#cphoneError").html("Enter numeric value");
            valid = false;return false;
        }
        $("#cphoneError").html(" ");
    }
    
    $('#name').focus();
    $("#name").blur(function(){
        if(!$.trim($("#name").val())) {  
            $("#nameError").html(" Username must be filled out !!");return false;
        } else {
            $("#nameError").html("");
        }
        
    });
     
    $('#account_name').focus(); 
    $("#account_name").blur(function(){
        if(!$.trim($("#account_name").val())){ 
            $("#account_nameError").html("Username must be filled out !!");
            return false;
        } else {
            $("#account_nameError").html("");
        }
    });
     
    $("#city").blur(function(){
        if(!$.trim($("#city").val())) { 
            $("#cityError").html("City field must be filled out !!");
            return false;
        } else {
            $("#cityError").html("");
        }
    });
     
    $("#state").blur(function() {
        if(!$.trim($("#state").val())) { 
            $("#stateError").html("State field must be filled out !!");
            return false;
        } else {
            $("#stateError").html("");
        }
    });
     
    $("#activation_type").blur(function() {
        if($("#activation_type").val()=="") { 
            $("#activationError").html("Please select any type from the box !!");
            return false;
        } else {
            $("#activationError").html("");
        }
    });
     
    //check the password is blank or not
    $("#pwd").blur(function(){
        pwd();
    });

    //check the confirm password is blank or not
    $("#cpwd").blur(function(){
        cpwd();
    });
    
    //email address validation
    $("#email").blur(function(){
        email();
    });
   
    //email address validation
    $("#mem_email").blur(function(){
        mem_email();
    });
                         
    $("#home_phone").blur(function(){
        home_phone();
    });
    
    $("#contact_phone").blur(function(){
        contact_phone();
    });
                        
    $('#submit').click(function () { 
        valid = true;
        mem_email();
        contact_phone();
        home_phone();
        if($("#name").val()==""|| $("#mem_email").val()==""
              ||$("#city").val()=="" || $("#state").val()=="" || $("#home_phone").val() == "" )      
        { 
            $("#errorMsg").html("Unable to create account.Not all required fields specified.");
            return false;
        }
           
    $("#errorMsg").html("");
        if (valid) {
            $.ajax({
                type: 'POST',
                url: url,
                dataType : 'json',
                data: $("#emf-form").serialize(),
                beforeSend: function() {
                    $('#errorMsg').after('<span class="removing-img">&nbsp;</span>');
                },
                success: function(data) {
                    $('.removing-img').hide('slow');
                    if(data != null) {
                        
                        if (data) {
                            window.location.href = "index.php"
                        } else {
                            jAlert('Email should be unique', obj.company);
                        }
                        
                    }
                }
            });
        } else {
            return false;
        }
            
    });
    
    $('#btn_login').click(function () { 
    	
        $.ajax({
            type: 'POST',
            url: login_url,
            dataType : 'json',
            data: {name :$("#username").val(), password :$("#password").val()},
            success: function(data) {                         
                if(data.login == true){
                    if (data != null) window.location.href = 'index.php';
                } else {
                        jAlert("Invalid username or password!", obj.company);
                }
            }
        });
        
    });

    $('#btn_signup').click(function () { 
        valid = true;
        pwd();
        cpwd();
        email();
        if($("#account_name").val()==""|| $("#pwd").val()==""   ||$("#cpwd").val()==""  ||$("#email").val()=="")      
          
            { 
	        $("#errorMsg").html("Unable to create account.Not all required fields specified.");
                return false;
                
            }     
           
	$("#errorMsg").html("");
        
        if (valid) {
         

            $.ajax({
                  type: 'POST',
                  url: signup_url,
                  dataType : 'json',
                  data: {name :$("#account_name").val(), password :$("#pwd").val(), email :$("#email").val()},
                  beforeSend: function() {
                      
                      $('#errorMsg').after('<span class="removing-img">&nbsp;</span>');
                      
                  },
                  success: function(data) {
                          $('.removing-img').hide('slow');
                          if(data != null) {
                              if (data) {
                                  window.location.href = "index.php"
                              }
                             else {
                                 jAlert('Name and Email should be unique', obj.company);
                             }
                       }
                         
                  }
                  });


        }
        else {
            return false;
        }
    
    });
    
    
    
    $('#contact').click(function(){        
        
        x=$("#from_email").val();
        emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; 

        if (!$.trim($('#firstName').val())) {
            jAlert('Please enter your first name', obj.company, function(){
                $('#firstName').focus();
            });
            return;
        }
        
        if (!$.trim($('#lastName').val())) {
            jAlert('Please enter your last name', obj.company, function(){
                $('#lastName').focus();
            });
            return;
        }
        
        if (!$.trim($('#from_email').val())) {
            jAlert('Please enter your email address', obj.company, function(){
                $('#from_email').focus();
            });
            return;
        }
         else if (!emailReg.test(x)) {
            jAlert('Please enter valid email address', obj.company, function(){
                $('#from_email').focus();
            });
            return;
        }
        
        if (!$.trim($('#from_msg').val())) {
            jAlert('Please enter your message', obj.company, function(){
                $('#from_msg').focus();
            });
            return;
        }
        
        $.ajax({
                type: 'POST',
                url: obj.basePath + 'contact.php',
                dataType : 'json',
                data: $("#contact-form").serialize(),
                beforeSend: function() {
                    $('#errorMsg').after('<span class="removing-img">&nbsp;</span>');
                },
                success: function(data) {
                    $('.removing-img').hide('slow');
                    if(data != null) {
                        
                        if (data) {
                            jAlert('Thanks for your feedback', obj.company);
                            $('#firstName').val('');
                            $('#lastName').val('');
                            $('#from_email').val('');
                            $('#from_msg').val('');
                            
                        } else {
                            jAlert('Failed to send your information', obj.company);
                        }
                        
                    }
                }
            });
        
    });
    
    
});
