﻿var previousCheckBox = '';
var previousState = false;

var provinceStartIndex = 1;
var provinceEndIndex = 13;
var defaultProvinceIndex = 1;
var stateStartIndex = 14;
var stateEndIndex = 64;
var defaultStateIndex = 61;
var otherIndex = 0;

$(document).ready(function() {
  $("#cbPrintedGuide").click(function() {
    if (!this.checked) {
      if (!$("#cbEGuide").is(":checked")) {
        $("#cbEGuide").attr('checked', true);
      }
    }
    show_hide_fields($("#cbPrintedGuide").is(":checked"));
  });
  $("#cbEGuide").click(function() {
    if (!this.checked) {
      if (!$("#cbPrintedGuide").is(":checked")) {
        $("#cbPrintedGuide").attr('checked', true);
      }
    }
    show_hide_fields($("#cbPrintedGuide").is(":checked"));
  });

  $("#ddlPrimaryInterest").change(function() {
    if (previousCheckBox != '') {
      $(previousCheckBox).attr('checked', previousState);
      $(previousCheckBox).attr('disabled', false);
    }
    if ($("#ddlPrimaryInterest").val().length > 0) {
      var checkBox = $('.' + $("#ddlPrimaryInterest").val() + ' input:checkbox');
      previousCheckBox = '#' + checkBox.attr('id');
      previousState = checkBox.attr('checked');
      checkBox.attr('disabled', true);
      checkBox.attr('checked', true);
    }
  });

  $('#preview-button').click(function() {
    if ($.browser.msie && $.browser.version < 7) {
      $('select').hide();
    }
    $('#preview').fadeIn();
    return false;
  });

  $('#preview-close').click(function() {
    if ($.browser.msie && $.browser.version < 7) {
      $('select').show();
    }
    $('#preview').fadeOut();
    return false;
  });

  $('#ddlProvince').change(function() {
    var index = $('#ddlProvince')[0].selectedIndex;
    if (index >= provinceStartIndex && index <= provinceEndIndex) {
      $('#ddlCountry').val('CA');
    }
    else {
      if (index >= stateStartIndex && index <= stateEndIndex) {
        $('#ddlCountry').val('US');
      }
      else {
        $('#ddlCountry').val('');
      }
    }
    var country = $('#ddlCountry').val();
    if (country == 'US' || country == 'CA') {
      show_hide_fields($("#cbPrintedGuide").is(":checked"));
    }
    else {
      show_hide_fields(false);
    }
    checkGuideOption(country);
  });


  $('#ddlCountry').change(function() {
    var index = $('#ddlProvince')[0].selectedIndex;
    var country = $('#ddlCountry').val();

    $('#tbPostalCode').removeClass('zipcode').removeClass('postalcode');
    switch (country) {
      case 'CA':
        if (index < provinceStartIndex || index > provinceEndIndex) {
          $('#ddlProvince')[0].selectedIndex = defaultProvinceIndex;
          $('#lblPostalZip').text('Postal Code');
          $('#tbPostalCode').addClass('postalcode').attr('title', 'Please enter a valid Canadian postal code.');
        }
        show_hide_fields($("#cbPrintedGuide").is(":checked"));
        break;
      case 'US':
        if (index < stateStartIndex || index > stateEndIndex) {
          $('#ddlProvince')[0].selectedIndex = defaultStateIndex;
          $('#lblPostalZip').text('Zip Code');
          $('#tbPostalCode').addClass('zipcode').attr('title', 'Please enter a valid US zip code.');
        }
        show_hide_fields($("#cbPrintedGuide").is(":checked"));
        break;
      default:
        $('#ddlProvince')[0].selectedIndex = otherIndex;
        $('#lblPostalZip').text('Postal Code or Zip Code');
        $('#tbPostalCode').attr('title', 'Please enter a valid postal code or zip code.');
        show_hide_fields(false);
        break;
    }
    checkGuideOption(country);
  });
});

function checkGuideOption(country) {
  switch (country) {
    case 'CA':
    case 'US':
      $('#cbEGuide').attr('disabled', false);
      $('#cbPrintedGuide').attr('disabled', false);
      $('#InternationalMessage').hide();
      break;
    default:
      $('#InternationalMessage').show();
      $('#cbEGuide').attr('disabled', true);
      $('#cbPrintedGuide').attr('disabled', true);
      $('#cbEGuide').attr('checked', true);
      $('#cbPrintedGuide').attr('checked', false);
  }
}


function show_hide_fields(b) {
  var rows = "tr.phone, tr.address1, tr.address2, tr.city, tr.length-of-stay span.required";
  var fields = "#tbPhone, #tbAddress1, #tbCity, #tbLengthOfStay, #ddlProvince, #ddlCountry";
  if (b) {
    $(rows).show();
    $(fields).addClass('required');
  }
  else {
    $(rows).hide();
    $(fields).removeClass('required');
  }
}

jQuery.validator.addMethod('zipcode', function(value) {
  return /^(\d{5}-\d{4})|(\d{5})$/.test(value);
}, 'Please enter a valid US zip code.');

jQuery.validator.addMethod('postalcode', function(value) {
  return /^[A-Za-z]\d[A-Za-z][\s]?\d[A-Za-z]\d$/.test(value);
}, 'Please enter a valid Canadian postal code.');

var validator = $("#signupForm").validate();
