    var thisURL = window.location.href;
    var error_messages = new Array;
    var system_messages = new Array;
    var loadingGif = new Image;
    loadingGif.src = '/themes/mychoice/images/loading.gif';
    
    $.fn.validatePin=function(card_number,pin_number,propery_id,form){
        $("#verify_progress").html("<img src=\""+loadingGif.src+"\"><br>Please wait while we verify your pin.").fadeIn();
        
        /**
         *
         * validate inputs here
         *
         **/
        
        card_number.attr('disabled',true);
        pin_number.attr('disabled',true);           
        $("#pin_verify").attr('disabled',true);
        var property_id = propery_id.val().replace(/[^0-9]/g, '');
        
        //alert(property_id);
        //return;
        
        $.ajax({
                type: 'POST',
                url: '/Ajax/Validate/',
                data: 'property_id='+property_id+'&card_number='+card_number.val()+'&pin_number='+pin_number.val(),
                dataType: 'json',
                success: function(response){
                    if(response){
                        if(response.responseCode == 10){
                            //form.submit();
                            window.location.href = '/profile-register/';
                        } else {
                            $("#errormsg_body").html(response.message);
                            $(".errormsg").center().show();
                            $("#verify_progress").html('');
                            $("#button_create-account").show();
                        }
                    } else {
                        $("#errormsg_body").html("The server couldn't process this request.");
                        $(".errormsg").center().show();
                        $("#verify_progress").html('');
                        $("#button_create-account").show();
                    }
                }    
        });
       
        card_number.removeAttr('disabled');
        pin_number.removeAttr('disabled');
        $("#pin_verify").removeAttr('disabled');      
    };
    
    $.fn.addNewCard=function(card_number,pin_number,property_id){
        $("#add_new_card_submit").hide();
        $("#add_new_card_progress").html("<img src=\""+loadingGif.src+"\"><br>Please wait while we verify your pin.");
       
        $.ajax({
                type: 'POST',
                url: '/Ajax/Validate/',
                data: 'property_id='+property_id+'&card_number='+card_number+'&pin_number='+pin_number,
                dataType: 'json',
                success: function(response){
                    if(response){
                        if(response.responseCode == 10){
                            $("#add_new_card_progress").html(response.message);
                            
                                $.ajax({
                                    type: 'POST',
                                    url: '/Ajax/AddNewCard/',
                                    data: 'property_id='+property_id+'&card_number='+card_number+'&pin_number='+pin_number,
                                    dataType: 'json',
                                    success: function (response2){                                        
                                        if(response2.responseCode == 10){
                                            $("#ModalAddCardForm").html("You have successfully added a new card.<br><br><a href=\"/my-profile/\" class=\"close_modal_reload\">Close</a>");
                                        } else {
                                            showError(response2.message);
                                            $("#add_new_card_submit").show();
                                            $("#add_new_card_progress").html('');
                                        }
                                    }
                                });
                                
                        } else {
                           showError(response.message);
                           $("#add_new_card_submit").show();
                           $("#add_new_card_progress").html('');                           
                        }
                    } else {
                        showError("The server couldn't process this request.");
                        $("#add_new_card_submit").show();
                        $("#add_new_card_progress").html('');   
                    }
                }    
        });
        $("#pin_verify").removeAttr('disabled');      
    };
    
    $.fn.signup=function(form,url){
        
        $("#profile_submit").hide();
        $("#profile_submitted").html("<img src=\""+loadingGif.src+"\"><br>Please wait...");
        
       // $("#signup_progress").html('Please wait while we create your account.').fadeIn();
       
        var login_email = $("#signup_login_email").val();
        var email = $("#signup_email").val();
        var password = $("#signup_password").val();
        var phone = $("#signup_phone").val();
        var card_number = $("#patron_cardnumber").val();
        var property_id = $("#patron_property_id").val();
        var update_account_email = $("#update_account_email").val();
        
        var preferences = new Array;
        var preference_args = '';
        $(".signup_preference").each(function(){
           if($(this).attr('checked')) preferences.push('preference[]='+$(this).val()); 
        });
       
        if(preferences.length) preference_args = preferences.join('&');

        $.ajax({
               type: 'POST',
                url: '/Ajax/Register/',
                data: 'email='+login_email+'&password='+password+'&phone='+phone+'&property_id='+property_id+'&card_number='+card_number+'&'+preference_args+'&update_account_email='+update_account_email,
                dataType: 'json',
                success: function(response){
                    if(response.responseCode == 10){
                        tb_show('','/home/Modal_RegisterVerify?height=280&width=354&modal=true');
                    } else {   
                        showError(response.message);
                        $("#profile_submit").show();
                        $("#profile_submitted").html('');
                    }
                }    
        });
    };
    
    $.fn.editprofile=function(form,url){
        $("#profile_submit").hide();
        $("#profile_submitted").html("<img src=\""+loadingGif.src+"\"><br>Please wait...");
        
        var login_email = $("#signup_login_email").val();
        var email = $("#signup_email").val();
        var password = $("#signup_password").val();
        var phone = $("#signup_phone").val();
        var card_number = $("#patron_cardnumber").val();
        var property_id = $("#patron_property_id").val();
        var editfields = new Array;
        var preferences = new Array;
        var preference_args = '';
        
        $(".edit_profile_field").each(function(){
            var key = $(this).attr('id').substr(5);
            var value = $(this).val();
            editfields.push(key + '=' + escape(value));
        });      
        
        $(".signup_preference").each(function(){
           if($(this).attr('checked')) preferences.push('preference[]='+$(this).val()); 
        });
            
        if(preferences.length) preference_args = preferences.join('&');
        
        $.ajax({
            type: 'POST',
            url: '/Ajax/EditProfile/',
            data: 'email='+email
                    +'&login_email='+login_email
                    +'&password='+password
                    +'&phone='+phone
                    +'&property_id='+property_id
                    +'&card_number='+card_number
                    +'&'+preference_args
                    +'&'+editfields.join('&'),
            dataType: 'json',
            success: function(response){
                if(response.responseCode == 10){                       
                   //alert();
                   showSystemMessage("Your profile updates have been sent to Pinnacle Entertainment for review.\n\nPlease allow up to 72 hours for changes to be completed.");
                   $("#systemmsgclose").attr('href','/my-profile');
                   //window.location.href = ;
                } else {   
                    showError(response.message);
                    $("#profile_submit").show();
                    $("#profile_submitted").html('');
                }
            }
        });
    };
    
    $.fn.patron_login=function(email,password,suffix){
        if(!suffix) var suffix = '';
        $.getJSON(api_url+"/authenticate/Login/?callback=?",
                  'email='+email+'&password='+password,
                function(response) {
                    if(response.responseCode == 'SUCCESS'){
                        $.getJSON("/Ajax/Authenticate/",
                            'hash='+response.result.hash+'&property_id='+$("#select_property"+suffix).val(),
                            function(response2){
                                if(response2.message){
                                    $("#redirect_url").val(response2.result);
                                    $("#redirect_name").val($("#select_property").text());
                                    if($("#remember_me"+suffix).attr('checked')==true){
                                        $.cookie('email_address', email, { expires: 90, path: '/' }); 
                                    } else {
                                        $.cookie('email_address',false,{expires:-100,path:'/'});
                                    }
                                    window.location.href = "/my-profile/";                               
                                } else {
                                    $("#errormsg_body").html('There was a problem verifying your login credentials. Please check the following:<br><span style="font-size:11px;">Did you choose the correct property?<Br> Did you enter your login email address properly?<br> Did you enter your password properly?</span>');
                                    $(".errormsg").center().show();
                                    $("#login_loading"+suffix).hide().html('');
                                    $("#member_login"+suffix).show();
                                }
                            });
                } else { 
                    $("#errormsg_body").html(response.message);
                    $(".errormsg").center().show();
                    $("#login_loading"+suffix).hide().html('');
                    $("#member_login"+suffix).show();
                }
        });      
    };
    
     $.fn.patron_login_enquiry=function(){
     
        $.getJSON(api_url+"/authenticate/Logged/?propertyId=" + propertyId + "&callback=?",
        '', 
        function(response) {
            if(response.hash){
                $.getJSON(ajax_url+"Ajax/Authenticate/",
                    'hash='+response.hash+'&property_id='+propertyId,
                    function(response2){
                        if(response2.message){
                            showSystemMessage("Please wait while we log you in...");
                            //$.cookie('pnklogged', true);
                            window.location.href = window.location.href;
                        } else {
                            showError('We could not log you in. Please try again.'); 
                        }
                    });
            } else {
                //showSystemMessage("You are not logged in");    
            }
        
        });
       
    };
    
    
    jQuery.fn.center = function () {
        this.css("position","absolute");
        this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
        this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
        return this;
    }

