function isValidEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      //alert('Invalid Email Address');
      return false;
   }
   return true;
}
      
$(function(){
    //$('.error').effect("highlight", {}, 3000);
    $inputDesignTemplate = $('input[name="customer\[design_template\]"]');
    $inputPackage = $('input[name="customer\[package\]"]');
    $infoBox = $('#info-box');
    $aPreview = $('#preview-giftcard');
   
    $('.required:visible').after(' *');
    $('.float-box').click(function(){
        $this = $(this);
        $('.float-box').addClass('unselected');
        $template = $this.removeClass('unselected').find('input[type=radio]');
        $template.click().attr('checked',true);
        $inputDesignTemplate.val($template.val());
        $aPreview.attr('href','download.file.php?width=620&height=450&preview-html&template='+$template.val());
        
    })
   // $('.float-box > input[type="radio"]:first').attr('checked',true);
    
    
   $('.package').click(function(){
      $this = $(this);
      $('.package').removeClass('selected'); //unselect style for all first
      $this.addClass('selected');
      $this.find('input[type=radio]').attr('checked',true);
      //alert($this.attr('id').replace("package-",""));//get package id
      $inputPackage.attr('value',$this.attr('id').replace("package-",""));
      //alert($inputPackage.attr('value'));
      
   });   
    $('.package input[type="radio"]').attr('checked',false);
    
    $('#go-to-paynova').click(function(){
        $allowSubmit = true;
        //check for input data
        $('form#offline_customer .required').each(function(){
            $field = $(this);
            //$('form#offline_customer').prepend($field.attr('name')+':'+$field.val()+'<br/>');
            if(
               !$field.val() || $field.val()==''
               || ($field.attr('name') == 'customer[email]' && !isValidEmail($field.val()))
               ) {
                $allowSubmit = false;
                $field.addClass('invalid-input');
            }
            //remove err if ok
            else if($field.hasClass('invalid-input')) $field.removeClass('invalid-input');
        });
        
        if($allowSubmit) getPaymentForm('?p=checkout&paynova-form&package='+$inputPackage.val()+'&template='+$inputDesignTemplate.val()+'&customer='+$('#offline_customer').serialize());
        
        return false;
    });
});