﻿var hashCheck, currentHash = '';
var formData;

function setFormData(data) {
    formData = data;
}

function getFormData() {
    return formData
}

/**
 * Check the current hash location and update the div's accordingly
 */
function checkHash() {
  var step = 1;

  if (window.location.hash === '') {
    window.location.hash = '#step1';
  }

  if (window.location.hash !== currentHash) {
    currentHash = window.location.hash;
    step = currentHash.substr(5, currentHash.length) * 1;
    $('.customize-step').hide();
    $('div#customize-step' + step).show();
    $('body').removeClass('customize-package-step-' + (step - 1));
    $('body').removeClass('customize-package-step-' + (step + 1));
    $('body').addClass('customize-package-step-' + step);
  }
}

$(document).ready(function () {

  // We will do some javascript here to simulate the progression between
  // steps without having to create completely separate templates for each
  // step. We merely show/hide different divs depending on the current step
  hashCheck = setInterval("checkHash()", 50);

  $('form').submit(function () {
    if (currentHash === "#step3") {
      if (validateCustomTicketForm(getFormData())) {
        window.location.hash = '#step4';
      } else {
        alert('Please select tickets before continuing');
      }
    }
    else if (currentHash === "#step4") {
        // Format the contact information for display
        var html = '';   
        var inputs = '';
        
            
        html += '<li>' + $("#step1-first-name").val() + ' ' + $("#step1-last-name").val() + '</li>';
        html += '<li>' + $("#step1-phone").val() + '</li>';
        html += '<li>' + $("#step1-email").val() + '</li>';
        
        inputs += '<input type="hidden" id="first-name" value="' + $("#step1-first-name").val() + '" />';
        inputs += '<input type="hidden" id="last-name" value="' + $("#step1-last-name").val() + '" />';
        inputs += '<input type="hidden" id="phone" value="' + $("#step1-phone").val() + '" />';
        inputs += '<input type="hidden" id="email" value="' + $("#step1-email").val() + '" />';
        
        $("#step5-contact-information").html('');
        $("#step5-contact-information").append(html);
        
        
        // Format the hotel information
        html = '';
        
        if ($('#step2-preferred-hotel').val()) {
            html += '<li>' + $("#step2-preferred-hotel").val() + '</li>';
            inputs += '<input type="hidden" id="hotel" value="' + $("#step2-preferred-hotel").val() + '" />';
        } else {
            html += '<li>' + $("#step2-select-hotel").val() + '</li>';
            inputs += '<input type="hidden" id="hotel" value="' + $("#step2-select-hotel").val() + '" />';
        }

        html += '<li>Check-in: ' + $("#check-in-date").val() + '</li>';
        html += '<li>Check-out: ' + $("#check-out-date").val() + '</li>';
        html += '<li>Number of Rooms: ' + $("#step2-rooms").val() + '</li>';
        html += '<li>Number of Adults: ' + $("#step2-adults").val() + '</li>';
        html += '<li>Number of Beds: ' + $("#step2-beds").val() + '</li>';
        html += '<li>Special Room Request: ' + $("#step2-room-request").val() + '</li>';
        
        inputs += '<input type="hidden" id="check-in-date" value="' + $("#check-in-date").val() + '" />';
        inputs += '<input type="hidden" id="check-out-date"" value="' + $("#check-out-date").val() + '" />';
        inputs += '<input type="hidden" id="rooms" value="' + $("#step2-rooms").val() + '" />';
        inputs += '<input type="hidden" id="adults" value="' + $("#step2-adults").val() + '" />';
        inputs += '<input type="hidden" id="beds" value="' + $("#step2-beds").val() + '" />';
        inputs += '<input type="hidden" id="hotel-request" value="' + $("#step2-room-request").val() + '" />';
        
        $("#step5-hotel-information").html('');
        $("#step5-hotel-information").append(html);
        

        // Format the ticket information
        html = '';
        var count = 1;
        
        // Add up ticket totals
        for (i = 0; i < getFormData().events.length; i = i + 1) {
            
          if ($('#ticket-level-' + i).val() && 
              $('#ticket-quantity-' + i).val()) {
              html += '<li> (' + $('#ticket-quantity-' + i).val() + ') ' + $('#ticket-level-' + i).val() + ' | ' + $('#ticket-session-' + i).val() + ' | ' + $('#ticket-date-' + i).val() + ' </li>';
              
              inputs += '<input type="hidden" id="ticket-quantity-' + count + '" value="' + $('#ticket-quantity-' + i).val() + '" />';
              inputs += '<input type="hidden" id="ticket-level-' + count + '" value="' + $('#ticket-level-' + i).val() + '" />';
              inputs += '<input type="hidden" id="ticket-session-' + count + '" value="' + $('#ticket-session-' + i).val() + '" />';
              inputs += '<input type="hidden" id="ticket-date-' + count + '" value="' + $('#ticket-date-' + i).val() + '" />';
              inputs += '<input type="hidden" id="ticket-date-new-' + count + '" value="' + $('#ticket-date-new-' + i).val() + '" />';
              
              count++;
          }
        }

        inputs += '<input type="hidden" id="number-tickets" value="' + (count - 1) + '" />';
        
        $("#step5-ticket-information").html('');
        $("#step5-ticket-information").append(html);


        // Format the extra options information
        html = '';
        count = 1;
        
         for (i = 1; i < $('#number-of-options').val(); i++) {
            if ($('#step4-option-yes-' + i).is(':checked')) {
                html += '<li>' + $('#step4-option-desc-' + i).val() + ': ' + $("#step2-adults").val();
                
                inputs += '<input type="hidden" id="option-id-' + count + '" value="' + $('#step4-option-id-' + i).val() + '" />';
                
                if ($("#step2-adults").val() === '1') {
                    html += ' Person';
                } else {
                    html += ' People';
                }
                
                html += '</li>';
                count++;
            }
        }
         
        inputs += '<input type="hidden" id="number-options" value="' + (count - 1) + '" />';

        html += '<li>Any Other Special Requests or Comments:<ul><li>' + $('#step4-special-requests').val() + '</li></ul></li>';
        inputs += '<input type="hidden" id="option-requests" value="' + $('#step4-special-requests').val() + '" />';

        $("#step5-option-information").html('');
        $("#step5-option-information").append(html);
        
        
        html = '';
        
        // Now copy all inputs over to the last form
        $('#data').html(inputs);
    
        window.location.hash = '#step5';
    } else if (currentHash === "#step5") {
        processData();
    }

    return false;
  });

  $('.back-button').click(function () {
    if (currentHash === '#step2') {
      window.location.hash = '#step1';
    }
    else if (currentHash === "#step3") {
      window.location.hash = '#step2';
    }
    else if (currentHash === "#step4") {
      window.location.hash = '#step3';
    }
    else if (currentHash === "#step5") {
      window.location.hash = '#step4';
    }

    return false;
  });
  
  // Assign the date pickers necessary for this form
  $("#check-in-date").datepicker();
  $("#check-out-date").datepicker();

  $('#step4-specific-performance').placeholder();
  
  jQuery.validator.messages.required = "";  
  
  $("#customize-step1-form").validate({
    submitHandler: function(form) {
         window.location.hash = '#step2';
         return false;
    }
  });

  $("#customize-step2-form").validate({
    submitHandler: function(form) {
        setupCustomizeTicketsForm(getFormData());
        calculateCustomTicketForm(getFormData());
        window.location.hash = '#step3';
        return false;
    }
  });

});


function processData() {

    var data = "";

    $("#customize-step5-form").find(":input").each(
        function() {
            if ($(this).attr("id")) {
                data += $(this).attr("id") + "=" + escape($(this).val()) + "&";
            }
        }
    );
    
    data = data.substr(0, data.length - 1);
//    data += 'empty=empty';
    
    $.post("/order/booking_custom_process.asp", 
        data,
        function(data){
            if (data != null) {
                if (data.status == "SUCCESS") {
                    window.location.hash = '#step6';
                } else {
                    alert("An error was encountered processing your request, please contact customer support.");
                    alert(data.message);
                }
            }
        }, 
        "json"
    );
}