$(document).ready(function()
{
  $('input').focus(function(i) {if(this.type == 'text' && (this.value == 'name' || this.value == 'email')) this.value = '';});
  $('input').blur(function(i) {if(this.type == 'input'){return}; if (this.value == '') this.value = this.defaultValue;});
  $('textarea').focus(function(i) {if (this.value == 'Type your personal message here') this.value = '';});

  // Maxlength limiter for textareas.
  $('textarea[maxlength]').keyup(function() {
     var jQueryObj = $(this); 
     // get the limit from maxlength attribute  
     var limit = parseInt(jQueryObj.attr('maxlength'));  

     if (isNaN(limit) == true) {
       return false;
     }

     // get the current text inside the textarea  
     var text = jQueryObj.val();  

     // count the number of characters in the text  
     var chars = text.length;  

     // check if there are more characters then allowed  
     if (chars > limit) {  
       // and if there are use substr to get the text before the limit  
       var new_text = text.substr(0, limit);  

       // and change the current text with the new text  
       jQueryObj.val(new_text);
     }

     var diff = (limit - chars); 
     if (diff < 0) {
       diff = 0;
     }

     $('#' + jQueryObj.attr('id') + '_remain').html(diff + ' characters remain'); 
  });  

  jQuery.validator.addMethod("non_laser_card", function()
  {
    var card_type_val = $("#card_type").val();
    if(card_type_val == '27')
    { // Laser (27) is an optional CVV2 number
      return true;
    }

    var cvv = $("#cvv2").val();
    if(cvv.length > 0)
    {
      return true;
    }
 
    return false;
  }, "Please add your security code");

  $('form').each(
    function(i)
    {
      try
      {
        $(this).validate();
      } catch (e)
      {
        //alert("Error occured\n" + 'Error: ' + e.name + '\nMessage: ' + e.message + '\nObject' + this);
      }
    }
    );
});
