/*jslint undef: true, browser: true */  
/*global jQuery, $, window */  
$(document).ready(function() {
    
    // Postal Code validation
    $.validator.addMethod("postalCode", function(value) {
         return value.match(/^[a-zA-Z][0-9][a-zA-Z](-| )?[0-9][a-zA-Z][0-9]$/);
    }, 'Please enter a valid postal code');
    
    // Phone number validation
    $.validator.addMethod("phoneUS", function(phone_number, element) {
        phone_number = phone_number.replace(/\s+/g, ""); 
        return this.optional(element) || phone_number.length > 9 && phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
    }, "Please specify a valid phone number");
    
    // Currency validation
    $.validator.addMethod("decimalTwo", function(value, element) {
        return this.optional(element) || value.match(/^(\d{1,6})(\.\d{1,2})?$/);
    }, "Must be in currency format (0.99)");
    
    // Custom bot validation
    jQuery.validator.addMethod("botprotection", function(value, element) { 
        return this.optional(element) || value.match('8'); 
    }, jQuery.format("The bot validation is incorrect"));

    // Validate our assessment form
    $("#formassessment").validate({
        errorClass: "formerror"
    });
    
    // Mouseover event for getting started banner
    $(".getstarted").mouseover(function() {
        $(".consultant_action a").css("backgroundPosition", "0px -51px");
    }).mouseout(function() {
        $(".consultant_action a").css("backgroundPosition", "0px 0px");
    }).click(function() {
        window.location = "/home/assessment";
    });
    
    // Referral change event for assessment form
    $("#referral").change(function() {
        $("#referral_more").hide();
        if ($(this).val() == 'Personal Referral') {
            $("#referral_more").show();
        }
    });
    
    // Common questions search
    $('input#inlinesearch').search('.questionbox');

    $("input#inlinesearch").click(function() {
        if($(this).val() == "Enter keywords to search our questions database") {
            $(this).val('');
        }
    });
      
    //  Common questions question submission
    $('#addquestion').submit(function() { 
        if ($("#addquestion_security").val() == '10') {
            $(this).ajaxSubmit({
                success: function() {
                    $("#addquestion").html('<div class="success">Thank you for your submission! We\'ll be sure to get back to you and perhaps post the question here!');
                }
            }); 
        // return false to prevent normal browser submit and page navigation 
        }
        return false; 
    }).validate({
        errorClass: "formerror"
    });
    
    // Consultants dropdown selection
    $("#select_consultants").change(function() {
        window.location = '/home/contact/' + $(this).val();
    });
    
    // Generic olorbox popup
    $(".cbox").colorbox({width:"800px", height:"500px", iframe:true, opacity: 0.5});
    
    // Privacy Policy popup
    $("#privacypolicy").colorbox({ width:"700px", height:"600px",  iframe:true, opacity: 0.5});
    
    // Calendar
    $('.datepicker').datepicker({
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        yearRange: '1920:2020',
        dateFormat: 'yy-mm-dd',
        gotoCurrent: true
    });
    
    // Contact dropdown
    if ($(".select").length > 0) {
        $(".select").selectmenu({
            style: 'dropdown',
            maxHeight: 200
        });
    }

});

$(".tip").qtip({ 
    fixed: false,
    position: {
        corner: { 
            target: 'topMiddle',
            tooltip: 'bottomMiddle'
        }
    },
    style: {
        background: '#d6e4ed',
        border: {
            width: 3,
            color: '#8eb9bf'
        },
        tip: 'bottomMiddle',
        color: '#525252'
    }
}); 

$.extend($.expr[":"], {
    "containsNC": function(elem, i, match, array) {
        return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3]||"").toLowerCase()) >= 0;
    }
});

$.fn.search = function(searchElements) {
    $(this).keyup(function(){
        $(searchElements).removeHighlight();
        var searchString = $(this).val();
        if (searchString.length > 0){
            $(searchElements).hide();
            $(searchElements+':containsNC(' +searchString+ ')').show();
            if (searchString.length > 2 ) {
                $(searchElements).highlight($(this).val());
            }
        } else {
            $(searchElements).show();
        }
    });
};


