$(document).ready(function(){

    //Open any links marked rel="external" in a new window.
    $('a').click(function(){
        if ($(this).attr('rel')=='external') {
            window.open($(this).attr("href"));
            return false;
        }
    });

    //Opens all external links in a new window
    $("a[href^=http]").each(function(){
        $(this).click(function() {
            if ($(this).attr('href') != 'http://customerarea.cabotfinancial.ib3.co.uk') {
                if($(this).attr('href').indexOf(location.hostname) == -1) {
                    window.open($(this).attr('href'));
                    return false;
                }
            }
        });
    });

    //Adds the friendlyForm class to any Zend forms that appear
    if ($('#customForm').length) {
        $('#customForm').addClass('friendlyForm');
    }

    //Make all navigation items with Stud 100 bold
    $('#mainMenu li').highlight('STUD 100');

    //Home page tab selections
    $('.selector').each(function(i) {
        $(this).mouseover(function() {
            $('.homeTab:not(.tab' + i +')').css('display', 'none');  //Sets all other tabs to display: none;
            $('.tab' + i).css('display','block'); // Sets the selected tab to display: block;
            if($.browser.msie && parseFloat($.browser.version) < 7) {  //Changes to compatiblity image for IE6
                $(this).css('background','transparent url(/images/compat/grey-arrow.gif) no-repeat top left');
            } else {
                $(this).css('background','transparent url(/images/grey-arrow.png) no-repeat top left');
            };            
            $('.selector').not(this).css('background','transparent'); // Removes the tab background on the other tabs.
            return false;
        });
    });

    if ($('#rotatingImages').length != 0) {
        $('#rotatingImages').cycle({
            fx: 'fade',
            timeout: 6000,
            speed: 2000
        });
    }

    //Set up form validation....
    if ($('#customForm').length != 0) {

        jQuery.validator.messages.required = "";  //Defines that the error labels are populated.
        
        $('#customForm').validate({
        invalidHandler: function(e, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                var message = errors == 1
                    ? 'It appears you have not completed a field.  This has been highlighted below.'
                    : errors + ' fields have not been completed. They have been highlighted below.';
                $("div.error h3").html(message);
                $("div.error").show();
                $("label.error").css('color: #fff000;');
            } else {
                $("div.error").hide();
            }
        }
     });
    };

    /*
     * Contact form validation
     */

      if ($('#contactForm').length != 0) {
            jQuery.validator.messages.required = "";  //Defines that the error labels are populated.

            $('#contactForm').validate({
            rules: {
                name: {
                    required: true
                },
                telephone: {
                    required: "#mobile:blank"
                },
                email: {
                  required: true,
                  email: true
                },
                confirmEmail: {
                    required: true,
                    equalTo: "#email"
                },
                accountNumber: {
                    required: '#lenderReference:blank',
                    number: true,
                    maxlength: 7
                }
            },
            invalidHandler: function(e, validator) {
                var errors = validator.numberOfInvalids();
                if (errors) {
                    var message = errors == 1
                        ? 'It appears you have not completed a field.  This has been highlighted below.'
                        : errors + ' fields have not been completed. They have been highlighted below.';
                    $("div.error h3").html(message);
                    $("div.error").show();
                    $("label.error").css('color: #fff000;');
                } else {
                    $("div.error").hide();
                }
            }
         });
      };

      if ($('#gateway_form').length != 0) {
        $('#gateway_form').submit();
      }
      
      $('#deliveryCharge').change(function (){
          $('#basketForm').submit();
      });
      
      $('#priority').change(function (){
          $('#basketForm').submit();
      });
      
      $('.buyNow').click(function() {
         $('.productForm').submit(); 
      });
      
      $('.quantity').change(function () {
         $('#basketForm').submit(); 
      });

});


