$(document).ready(function() {
    // load the Juice font
    //Cufon.replace('#top .phoneno, #top .logo a, .topnavButton', {fontFamily: 'Juice'});
    Cufon.replace('#top .phoneno, #top .logo a', {fontFamily: 'Juice'});
});

// placement of the validation errors (see this and the css for error)
function validationErrorPlacement(error, element) {
    error.insertBefore(element);
    error.wrap('<span style="cursor: pointer" title="click to clear error messages" alt="click to clear error messages" onclick="javascript: clearAllErrors()"></span>');
}

function clearAllErrors() {
   $(".error").remove();
}

// the action when highlighting error
function highlight(element, errorClass) {
      //$(element).fadeOut(function() {
      //   $(element).fadeIn();
      //});
      $(element).addClass("invalidField");
}

// how to reverse the obove
function unhighlight(element, errorClass) {
      $(element).removeClass("invalidField");
};

// use jquery.datalink to bind the widgets to the model variable
// NOTE: databinding doesn't handle checkboxes yet so these have to be explicitly maintained
//
function setupDataBinding(baseSelector, model) {
   $(baseSelector+" select,input:not(:checkbox)").link(model);
   bindCheckboxes(baseSelector, model);
}


function bindCheckboxes(baseSelector, model) {
    $(baseSelector+' input[type="checkbox"]').bind('change',function() {
        var property = $(this).attr('name');
        if($(this).is(':checked')) {
             model[property] = true;
        } else {
             model[property] = false;
        }
    });
}

// return a currency formatted value (basic formatting - we can get a jQuery plugin if we want more)
function formatCurrency(amount) {
    var i = parseFloat(amount);
    if(isNaN(i)) { i = 0.00; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + "&pound;" + s;
    return s;
}

$.validator.addMethod("phoneNumber",
        function(value, element) {
            return this.optional(element) || /^[\d ]+$/.test(value);
        },
                'Please enter a valid phone number.'
);

