$(document).ready(function(){	

checkForFiles();
$(document).on("click",  "#printform", function(evt){
printWithCss();

//  $("#thankyou").hide(); 
 // $("#printPreview").show();
}); 

function printWithCss() {
        //Works with Chome, Firefox, IE, Safari
        //Get the HTML of div
        var title = document.title;
        var divElements = document.getElementById('printPreview').innerHTML;
        var printWindow = window.open("", "_blank");
        //open the window
        printWindow.document.open();
        //write the html to the new window, link to css file
        printWindow.document.write('<html><head><title>' + title + 
        '</title>'+
       // '<script src="/VehicleComplaint/resources/js/global.js"></script>'+
       
                '<link rel="stylesheet" type="text/css" href="/VehicleComplaint/resources/css/normalize.css">'+
                        '<link rel="stylesheet" type="text/css" href="/VehicleComplaint/resources/css/style.css">'+
                                '<link rel="stylesheet" type="text/css" href="/VehicleComplaint/resources/css/helper.css">'+
                                 '<link rel="stylesheet" type="text/css" href="/VehicleComplaint/resources/css/print.css">'+
        '</head><body>');
        printWindow.document.write(divElements);
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        printWindow.focus();
        //The Timeout is ONLY to make Safari work, but it still works with FF, IE & Chrome.
        //setTimeout(function() {
           // printWindow.print();
           // printWindow.close();
       // }, 100);
    }
$(document).on("click",  ".preview", function(evt){
  if ($('#msform').valid()) { 
      $( "#tabBar" ).addClass( "none" );
      $( ".nexttab" ).addClass( "none" );
      $( ".previewnone" ).addClass( "none" );
      $( ".previewonly" ).removeClass( "none" );
      $( ".previewonly" ).removeClass( "hidden" );
     $( "#submit" ).removeClass( "none" );
      $('#submit').removeAttr("disabled");
    $( ".showbutton" ).removeClass( "none" );
     $( "#recaptcha" ).removeClass( "none" );
    $("#h2_text").attr('class',$("#h2_text").attr('class').replace(/\btt.*?\b/g, ''));
    $( "#h2_text" ).addClass( "tt6" );
    
     
   
    $('#tabs').tabs();
    $("#tabs").tabs("destroy");
    checkForFiles();
  }
 }); 
 


  $(document).on("submit", "form", function(evt){
     $( "#submit" ).attr('disabled','disabled');
   $("#indicator_submit").show();
 
var filecount=checkForFiles();
if(filecount >0){
 var v = grecaptcha.getResponse();
             if ( v.length ==0 ){
                   // invalid characters
                  $('#error-recaptcha').show(); 
                  evt.preventDefault();
                  $('#submit').removeAttr("disabled");
                  $("#indicator_submit").hide();
                  $("#error-recaptcha").css('display','block');
                  $("#error-recaptcha").show();
           }else{
            $("#msform").attr("action", "saveComplaint" );
           }
  
}

$("#msform").attr("action", "saveComplaint" );

});
  


  
  $(document).on('change remove click ', 'input[type="file"]' , function(){
 //alert("change,remove,click");
       checkForFiles();
     });
     
  $('#email, #cemail').bind("cut copy paste",function(e) {
    e.preventDefault();
  });

 });
 
 // count the number of files for upload and hide/show reCaptcha on preview page
 
   function checkForFiles(){
   fileCount=0;

  if($("input[type=file][name='files[0]']")){
       var f0 = $("input[type=file][name='files[0]']").val();
        if(f0 !== undefined && f0 !="")
        
       fileCount=fileCount+1;
  }
  
   if($("input[type=file][name='files[1]']")){
         var f1 =$("input[type=file][name='files[1]']").val()
         if( f1 !== undefined && f1 !="")
          fileCount=fileCount+1;
      }
    if($("input[type=file][name='files[2]']")){
       var f2 = $("input[type=file][name='files[2]']").val();
        if( f2 !== undefined && f2 !="")
        fileCount=fileCount+1;
      }
    if($("input[type=file][name='files[3]']")){
       var f3 = $("input[type=file][name='files[3]']").val();
       if(f3 !== undefined && f3 !="")
      fileCount=fileCount+1;
      }
    if($("input[type=file][name='files[4]']")){
          var  f4 = $("input[type=file][name='files[4]']").val();
            if( f4 !== undefined && f4!="")
        fileCount=fileCount+1;
      }
      

  if (fileCount>0 &&  !($( "#recaptcha").hasClass( "none" ))) {
    $("#recaptcha").show(); 
  }else{
  $("#recaptcha").hide();
  $("#error-recaptcha").hide();
  
  }
   return fileCount;  
  }

 
 

  