﻿// AR eShop

$(document).ready( function(){


// init price
makePrice();
 
// quantity update action
$('input[name="es-qty"]').keyup(function(){
    makePrice();
});
$('#es-country').change( function(){
    makePrice();
});

// count all and display total
function makePrice(){

    // delete Paypal hidden fields
    $('#paypal-hidden-fields').text('');

    // subtotal & total price
    var subTotal = 0;
    var total = 0;
    
    // delivery
    var delivery = 0;
    var regionPrice = $('#es-country').val() == 'United Kingdom' ? 2.2 : 4.2;

    // PAYPAL hidden field num
    var i = 1;
    $('input[name="es-qty"]').each( function(){
      
    // quantity
    var qty = parseInt( $(this).val() );
    
    // price
    var id = $(this).attr('id');
    var price = $('#'+id+'_price').val();

    // multiply if integer
      if( ! isNaN(qty) && qty > 0 ){
        subTotal += (qty * price);
        // PAYPAL hidden fields create
        paypalHiddenFields( id, i, regionPrice );
        i++;
          // free delivery for subscription
          if( $( '#' + id + '_name' ).attr('class') != 'no-shipping' ) {
              delivery += ( qty * regionPrice );
          }
        } 
    });

    // display results
    total = subTotal + delivery;
    var a = delivery.toString();
    var oh = a.indexOf('.') != -1 ? '0' : '';
    subTotal == 0 ? subTotal = '' : subTotal = '£' + subTotal;
    $('#es-js-subtotal').text( subTotal );
    delivery == 0 ? delivery = '' : delivery = '£' + delivery;
    $('#es-js-delivery').text( delivery + oh );
    total == 0 ? total = '' : total = '£' + total;
    $('#es-js-total').text( total + oh );
}

// Submit
$('#es-submit-paypal,  #es-submit-check,  #es-submit-check2').click( function(){
  // validate first
  if ( validateForm() ) {
  // paypal payment
      if( $(this).attr('id') == 'es-submit-paypal' ) {
          $('#es-form').attr('action', 'https://www.paypal.com/cgi-bin/webscr');
          $('#es-form').submit();
  // payment by check */        
      } else {
          $('#es-form').attr('action', 'http://academia-rossica.org/pay_by_check.php');
          $('#es-form').submit();
      }
  }
return false;
});

// form validation
function validateForm(){
  if( $('#es-name').val() == '' ) {
      alert( 'Tell us your name, please.' ); $('#es-name').focus(); 
  } else if ( $('#es-surname').val() == '' ) {
      alert( 'Tell us your surname, please.' ); $('#es-surname').focus();
  } else if( $('#es-surname').val() == '' ) {
      alert( 'Tell us your surname, please.' ); $('#es-surname').focus();
  } else if( $('#es-address').val() == '' ) {
      alert( 'Tell us your address, please.' ); $('#es-address').focus();
  } else if( $('#es-postcode').val() == '' ) {
      alert( 'Tell us your postcode, please.' ); $('#es-postcode').focus();
  } else if( $('#es-country').val() == '' ) {
      alert( 'Tell us your country, please.' ); $('#es-country').focus();
  } else if( $('#es-email').val() == '' ) {
      alert( 'Tell us your email, please.' ); $('#es-email').focus();
  } else if( $('#es-js-total').text() == '' ) {
      alert( 'What exactly do you want to purchase?' ); $('#es-qty1').focus();
  } else {
      return true;
  }
  return false;
}



// Paypal hidden fields
function paypalHiddenFields( id, i, regionPrice ) {
  // get name from a hidden field
  var name = $( '#' + id + '_name' ).val();
  /* name of an item */
  $('#paypal-hidden-fields').append( '<input type="hidden" name="item_name_' + i + '" value="' + name + '" />' );   
  // get price from a field class
  var price = $( '#' + id + '_price' ).val();
  /* price of an item */
  $('#paypal-hidden-fields').append( '<input type="hidden" name="amount_' + i + '" value="' + price + '" />' ); 
  // get quantity from a field
  var quantity = $( '#' + id ).val();
  // price of an item
  $('#paypal-hidden-fields').append( '<input type="hidden" name="quantity_' + i + '" value="' + quantity + '" />' );
  // quantity
  $('#paypal-hidden-fields').append( '<input type="hidden" name="' + id + '_quantity" value="' + quantity + '" />' );
  // get shipping
  var shipping = $( '#' + id + '_name' ).attr('class') != 'no-shipping'  ?  (quantity * regionPrice) : '';
  // shipping of an item
  $('#paypal-hidden-fields').append( '<input type="hidden" name="shipping_' + i + '" value="' + shipping + '" />' );  
}


});
