﻿$(document).ready(function() {
    $("#cbPrintedGuide, #cbEGuide").click(function() {
        show_hide_fields();
    });

    $('#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(OnStateSelectedIndexChanged);
    $('#ddlCountry').change(OnCountrySelectedIndexChanged);
    
    $('#ddlPrimaryActivity').change(OnPrimaryActivityChange);
    OnPrimaryActivityChange();

    var stateDropDownList = $('#ddlProvince').get(0);
    var countryDropDownList = $('#ddlCountry').get(0);

    $('#ddlProvince, #ddlCountry').change(function() {
        $('#tbPostalCode').removeClass('zipcode').removeClass('postalcode');

        if (stateDropDownList.selectedIndex >= provinceStartIndex && stateDropDownList.selectedIndex <= provinceEndIndex) {
            $('#lblPostalZip').text('Postal Code');
            $('#tbPostalCode').addClass('postalcode').attr('title', 'Please enter a valid Canadian postal code.');
        }
        else if (stateDropDownList.selectedIndex >= stateStartIndex && stateDropDownList.selectedIndex <= stateEndIndex) {
            $('#lblPostalZip').text('Zip Code');
            $('#tbPostalCode').addClass('zipcode').attr('title', 'Please enter a valid US zip code.');
        }
        else {
            $('#lblPostalZip').text('Postal Code/Zip Code');
            $('#tbPostalCode').attr('title', 'Please enter a valid postal code or zip code.');
        }
    });
    
    show_hide_fields();
    
    var validator = $("#form1").validate();
});

function OnPrimaryActivityChange()
{
    var selectedActivity = $('#ddlPrimaryActivity').get(0).value;
    var activities = $('td.other-activity input:checkbox');
    activities.each(function()
    {
        this.checked = false;
        this.disabled = false;
        
        if (selectedActivity != '' && $(this).parent('span.'+selectedActivity).length > 0)
        {
            this.checked = true;
            this.disabled = true;
        }
    });
}

jQuery.validator.addMethod('zipcode', function (value) {
    return /^(\d{5})(-\d{4})?$/.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.');

