var company = false;
var products = true;

$(document).ready(function(){
  $('body').css("display", "none");
  $('body').fadeIn('slow');

  $('#tuotteeth2').click(function(){
   if(products){
     $('.view-tuotteet-listaus .view-content').show();
     $('#productContent').hide();
   }else{
     closecompany();
  }
  });
  $('#header h2').click(function(){
    if(company){
     /* closecompany();*/
    }else{
      opencompany();      
      }
  });
  /* show product */
  $('.productCard').click(function(){
    var getcontent = $(this).find('.fullContContainer').html();
    var returnLink = "<br /><br /><span class='returnToProducts'>Takaisin tuotteisiin</span>"
    openProduct(getcontent+returnLink);
    $('.returnToProducts').click(function(){
     $('.view-tuotteet-listaus .view-content').show();
     $('#productContent').hide();
   });
  });
  $('#tilaah2').click(function(){
     if(company){
       closecompany();
     }
     $('#page').hide();
     $('.region-bottom').hide();
     $('#orderformBg').show();
     doTheMath();
     $('#orderform').show();
   });
  
  /* Form actions */
   $('#closeForm').click(function(){
     $('#orderformBg').hide();
     $('#orderform').hide();
     $('#page').show();
     $('.region-bottom').show();
   });
  $('#printbtn').click(function(){
    $('#orderformBg').css('background','#fff');
    $('#orderformBg').css('opacity', 1);
    $('#page').hide();
    window.print();
    $('#orderformBg').css('background','#000');
    $('#orderformBg').css('opacity', 0.8);
    $('#page').show();
  });
  $('#sendbtn').click(function(){
    validateForm();
  });
  $('#orderform table input').focusout(function(){
    var thisValue=$(this).val();
    if(isNaN(thisValue)||thisValue<0){
      $(this).val(0);
    }else{
      thisValue=Math.floor(thisValue);
      $(this).val(thisValue);
    }
    doTheMath();
    });
  });
/* on load ends */

/* calculate the total */
function doTheMath(){
  var total = 0;
  $('.view-tuotetilaus tr').each(function(){
  var prize = parseFloat($(this).find('.td2').html());
  var amount = $(this).find('.td3 input').val();
  total += prize*amount;
  });   
  $('#orderTotal').html(total+' €');
}
  
function openProduct(setcontent){
  $('#productContent').show();
  $('#productContent').html(setcontent);
  $('.view-tuotteet-listaus .view-content').hide();
  
}
  
function opencompany(){
  $('#main').slideDown(400);
  $('#productContent').hide();
  $('.view-tuotteet-listaus .view-content').slideUp(400);
  $('.region-bottom').animate({paddingTop:'0px'}, 400);
  $('#tuotteetHeader').animate({top:'-69px'}, 400);
  $('#tuotteetHeader').css('padding','25px 0');
  company=true;
  products=false;
}

function closecompany(){
  $('#main').slideUp(400);
  $('#productContent').hide();
    $('.view-tuotteet-listaus .view-content').slideDown(400);
    $('.region-bottom').animate({paddingTop:'88px'}, 400);
    $('#tuotteetHeader').animate({top:'82px'}, 400);
    $('#tuotteetHeader').css('padding','27px 0');
  company=false;
  products=true;
}

/* Form Functions */
var ispm="Boolean";
function validateForm(){
  $('#err').hide();
  $('#err').html('');
  var errorMsg = 'Tilaajan nimi puuttuu';
  if($('#tilaaja').val()==''){
    $('#err').html(errorMsg);
    $('#err').show();
  }else if($('#laskutettavannimi').val()==''){
    errorMsg = 'Laskutettavan nimi puuttuu';
    $('#err').html(errorMsg);
    $('#err').show();
  }else if($('#katuosoite').val()==''){
    errorMsg = 'Laskutuosoite puuttuu';
    $('#err').html(errorMsg);
    $('#err').show();
  }else if($('#pnojatoimipaikka').val()==''){
    errorMsg = 'Laskutusosoitteen postinumero ja toimipaikka puuttuvat';
    $('#err').html(errorMsg);
    $('#err').show();
  }else if($('#katuosoitetoimitus').val()==''){
    errorMsg = 'Toimitusosoite puuttuu';
    $('#err').html(errorMsg);
    $('#err').show();
  }else if($('#pnojatoimipaikkatoim').val()==''){
    errorMsg = 'Toimitusosoitteen postinumero ja toimipaikka puuttuvat';
    $('#err').html(errorMsg);
    $('#err').show();
  }else if($('#puhelin').val()==''){
    errorMsg = 'Puhelinnumero puuttuu';
    $('#err').html(errorMsg);
    $('#err').show();
  }else if($('#sposti').val()==''){
    errorMsg = 'Sähköpostiosoite puuttuu';
    $('#err').html(errorMsg);
    $('#err').show();
  }else {
   /* process fields and set message in $('#setcontent').val */
   var parseMessage ="\n \n";
   parseMessage += "Tilaaja: " + $('#tilaaja').val()+'\n';
   parseMessage += "Laskutettavan nimi: " + $('#laskutettavannimi').val()+'\n';
   parseMessage += "Laskutusosoite: " + $('#katuosoite').val()+', ';
   parseMessage += $('#pnojatoimipaikka').val()+'\n \n';
   parseMessage += "Toimitusosoite: " + $('#katuosoitetoimitus').val()+', ';
   parseMessage += $('#pnojatoimipaikkatoim').val()+'\n \n';
   parseMessage += "Puhelin: " + $('#puhelin').val()+'\n';
   parseMessage += "Sähköposti: " + $('#sposti').val()+'\n \n';
   
   if($('#permit').is(":checked")){
   parseMessage += 'Haluan jatkossa tietoa LaCo Oy:n tuotteista \n \n';
   }

   if($('#mailpermit').is(":checked")){
   parseMessage += 'Minulle saa lähettää LaCo Oy:n tuotteisiin liittyvää sähköpostia  \n \n';
   }
   
   parseMessage += "Tilattavat tuotteet: \n \n"
   $('.view-tuotetilaus tr').each(function(){
   if($(this).find('.td3 input').val()!=0){
     parseMessage += 'Tuote: '+$(this).find('.td1').html() +' ';
     parseMessage += 'Hinta: '+$(this).find('.td2').html() +' ';
     parseMessage += 'Määrä: '+$(this).find('.td3 input').val();
     parseMessage += '\n \n';
     }
   });
   parseMessage += "Summa: "+$('#orderTotal').html()+' (+alv)\n \n';
   
    $('#setcontent').val(parseMessage);
    $('#ispm').val($('#userComment').val());
    $('#sendersmail').val($('#sposti').val());
    $('#sender').submit();
    /**/
  }
}
