 $(document).ready(function() {
          $('#login_form').submit(function(){
              var result = false;
              $.ajax({
                url: '/ajax/checkMember.php',
                async: false,
                data: "username=" + $('#username').val() + '&password=' + $('#password').val()  ,
                success: function(data) {
                         if(data != 1){
                                   result = false;
                                   alert("Account not found or wrong password");
                                   return false;
                          }else{
                                  result = true;
                                  return true;
                          }
                }
              });
              return result;
          });
          $('#search-right-end').click(function(){
              $.ajax({
                  url: '/ajax/redirectToURL.php',
                  async: false,
                  data: "location_id=" + $('#locations').val() + '&subcategory_id=' + $('#subcategories_select').val()  ,
                  success: function(data) {
                       window.location = '/' + data;
                  }                          
              });
          });
       $("#login_panel").dialog({
                resizable: false,
                show: 'slide',
                position: 'center',
                modal: true,
                hide: 'slide',
                draggable: false,
                closeOnEscape: true,
                autoOpen: false,
                height: 250,
                width: 600,
                bgiframe: true
        });
        $("#registration_panel").dialog({
                resizable: false,
                show: 'slide',
                position: 'center',
                modal: true,
                hide: 'slide',
                draggable: false,
                closeOnEscape: true,
                autoOpen: false,
                height: 500,
                width: 700,
                bgiframe: true
        });

        $("#register").validate({
                rules: {
                        usernamer: {
                                  required: true,
                                 minlength: 8,
                                 remote: {
                                        url: 'ajax/checkUsername.php',
                                        type: 'get',
                                        data: {
                                                  user: function(){
                                                            return $('#usernamer').val();
                                                  }
                                        }
                              }
                        },
                        firstname: "required",
                        lastname: "required",
                        email: "required email",
                        password1: {
                                  required: true,
                                 minlength: 8
                        },
                        confirm1: {
                              required: true,
                              equalTo: "#password1"
                            },
                        captchatext:  {
                              required: true,
                              remote: {
                                        url: 'ajax/verifyCaptcha.php',
                                        type: 'post',
                                        data: {
                                                  code: function(){
                                                            return $('#captchatext').val();
                                                  }
                                        }
                              }
                            }
                },
                 messages: {
                        usernamer: {
                                  required: jQuery.format("required"),
                                  minlength: "at least 8 chars",
                                  remote: 'taken username'
                        },
                        firstname: {
                                  required: jQuery.format("required")
                        },
                        lastname: {
                                  required: jQuery.format("required")
                        },
                        email: {
                                  required: jQuery.format("required"),
                                  email: "incorrect format"
                        },
                        password1: {
                                  required: jQuery.format("required"),
                                  minlength: "at least 8 chars"
                        },
                        confirm1: {
                                  required: jQuery.format("required"),
                                  equalTo: "unequal passwords"
                        },
                        captchatext: {
                                  required: jQuery.format("Required"),
                                  remote: "wrong text"
                            }
                     }
            });
});

function anotherimage(){
        $.ajax({
              url: 'ajax/getCaptcha.php',
          async: true,
          success: function(data) {
                  $('#captchaimage').html(data);
              }
        });
}

     function showLogin(){
         $("#login_panel").dialog('open');
 }
 function showRegistration(){
     $("#registration_panel").dialog('open');
 }
 function closeLoginshowRegistration(){
     $("#login_panel").dialog('close');
     $("#registration_panel").dialog('open');
 }


function  show_locations(val){
    $.ajax({
      url: '/ajax/get_locations.php',
      data: "ph_id=" + val,
      success: function(data) {
          $("#places").html(data);
      },
      beforeSend:function(data){
          $("#places").html("<img class=\"search\" src=\"/images/ajax-loader.gif\" alt=\"\" >");
      }
    });
}

function  show_categories(val){
    $.ajax({
      url: '/ajax/get_subcategories.php',
      data: "cat_id=" + val,
      success: function(data) {
          $("#subcategories").html(data);

      },
      beforeSend:function(data){
          $("#subcategories").html("<img class=\"search\" src=\"/images/ajax-loader.gif\" alt=\"\">");
      }
    });
}

