$(function()
{
    jQuery.extend({preloadImages: function() {
    	for(var i = 0; i<arguments.length; i++)
    	{
    		jQuery("<img>").attr("src", "/img/"+arguments[i]);
    	}
    }});


  //Add customized methods to the validator plugin
    jQuery.validator.addMethod("postal", function(value){
        return /^[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]$/i.test(value);
    }, "Please enter a valid postal code");

    jQuery.validator.addMethod("zip", function(value){
        return /^[0-9]{5}(?:-[0-9]{4})?$/.test(value);
    }, "Please enter a valid zip code");

    jQuery.validator.addMethod("phone", function(value, element){
        var reg = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
        match = value.match(reg);
        $(element).val(value.replace(reg, "($1) $2-$3"));
        return match;
    }, "Please enter a valid phone number" );

    jQuery.validator.addMethod('email', function(value){
        return value.match( /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i );
    }, "Please enter a valid E-Mail address");

  //Make the default error message class the same as Cake's
    jQuery.validator.setDefaults({
        errorClass: "error-message"
    });

  //Pagination "limit per page" select
    $('form.limitForm select.limit').change(function(){ $(this).parents('form.limitForm').submit(); });

});

function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57     
 
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}