﻿
$(document).ready(function(){
    //Savings Calculator Callout
    $('.membershipSavingsForm input').keydown(function(event) {
        if (event.which && event.which == 13)
        {
            $('#membershipSavingsForm-calculate').click();
        }
        else if (event.charCode && (event.charCode < 48 || event.charCode > 57)) 
        {
            event.preventDefault();
        }

    });
    $('#membershipSavingsForm-calculate').click(function() {
        var total = 0.00;
        var savings;
        
        savings = (parseFloat($('#membershipSavingsForm-electronics').val()) * .08);
        total += isNaN(savings) ? 0 : savings;
        savings = (parseFloat($('#membershipSavingsForm-books').val()) * .05);
        total += isNaN(savings) ? 0 : savings;
        savings = (parseFloat($('#membershipSavingsForm-prescriptions').val()) * .30);
        total += isNaN(savings) ? 0 : savings;
        savings = (parseFloat($('#membershipSavingsForm-auto-parts').val()) * .07);
        total += isNaN(savings) ? 0 : savings;
        savings = (parseFloat($('#membershipSavingsForm-shopping-malls').val()) * .20);
        total += isNaN(savings) ? 0 : savings;
        savings = (parseFloat($('#membershipSavingsForm-travel').val()) * .10);
        total += isNaN(savings) ? 0 : savings;
        
        if (total > 0)
        {
	        $('#membershipSavingsForm-savings-total').text(String(total.toFixed(2)));
            $('#membershipSavingsForm-savings')
                .css('display', 'block')
            ;
        }
        else
        {
            $('#membershipSavingsForm-savings')
                .css('display', 'none')
            ;
        }
        
        return false;
    });
    //Insurance Calculator Callout
    $('.membershipInsuranceForm input').keydown(function(event) {
        if (event.which && event.which == 13)
        {
            $('#membershipInsuranceForm-calculate').click();
        }
        else if (event.charCode && (event.charCode < 48 || event.charCode > 57)) 
        {
            event.preventDefault();
        }
    });
    $('.membershipInsuranceForm select').keydown(function(event) {
    if (event.which && event.which == 13)
    {
        $('#membershipInsuranceForm-calculate').click();
    }
    });
    $('#membershipInsuranceForm-calculate').click(function() {
        var state;
        var years;
        if (($('#membershipInsuranceForm-accountAddressFormFieldsState').val()).length) {
            state = $('#membershipInsuranceForm-accountAddressFormFieldsState').val();
        }
        else {
            alert('Please select a state');
            $('#membershipInsuranceForm-accountAddressFormFieldsState').focus();
            return false;
        }
        if (($('#membershipInsuranceForm-years-membership').val()).length) {
            years = $('#membershipInsuranceForm-years-membership').val();
        }
        else {
            alert('Please enter your years of membership');
            $('#membershipInsuranceForm-years-membership').focus();
            return false;
        }
        
        var calculationMatrix = new Array(2);
        calculationMatrix[0] = new Array(7);
        calculationMatrix[1] = new Array(7);

        calculationMatrix[0][0] = 1;
        calculationMatrix[0][1] = 6;
        calculationMatrix[0][2] = 10;
        calculationMatrix[0][3] = 13;
        calculationMatrix[0][4] = 16;
        calculationMatrix[0][5] = 19;
        calculationMatrix[0][6] = 21;
        
        
        if ( (state == 'VA') || (state == 'PA') || (state == 'NJ') )
        {
            calculationMatrix[1][0] = .05;
            calculationMatrix[1][1] = .07;
            calculationMatrix[1][2] = .09;
            calculationMatrix[1][3] = .11;
            calculationMatrix[1][4] = .13;
            calculationMatrix[1][5] = .15;
            calculationMatrix[1][6] = .20;
        }
        else if (state == 'MD')
        {
            calculationMatrix[1][0] = .10;
            calculationMatrix[1][1] = .12;
            calculationMatrix[1][2] = .14;
            calculationMatrix[1][3] = .16;
            calculationMatrix[1][4] = .18;
            calculationMatrix[1][5] = .20;
            calculationMatrix[1][6] = .22;
        }
		else if (state == 'DC')
        {
            calculationMatrix[1][0] = .10;
            calculationMatrix[1][1] = .10;
            calculationMatrix[1][2] = .10;
            calculationMatrix[1][3] = .10;
            calculationMatrix[1][4] = .10;
            calculationMatrix[1][5] = .10;
            calculationMatrix[1][6] = .10;
        }
        else if (state == 'DE')
        {
            calculationMatrix[1][0] = .10;
            calculationMatrix[1][1] = .10;
            calculationMatrix[1][2] = .02;
            calculationMatrix[1][3] = .05;
            calculationMatrix[1][4] = .08;
            calculationMatrix[1][5] = .11;
            calculationMatrix[1][6] = .15;
        }
        
        var discountPercentage = 0;
        for (var i = 0; i < calculationMatrix[0].length; i++)
            if(calculationMatrix[0][i] <= years)
                discountPercentage = calculationMatrix[1][i];
        
        
        	        
        if (discountPercentage > 0)
        {
	        $('#membershipInsuranceForm-results-output').text(String('Members save up to ' + (discountPercentage * 100).toFixed(0) + '%'));
            $('#membershipInsuranceForm-results')
                .css('display', 'block')
            ;
        }
        else
        {
	        $('#membershipInsuranceForm-results-output').text(String('You are not eligible for a Member discount at this time...'));
            $('#membershipInsuranceForm-results')
                .css('display', 'block')
            ;
        }

        return false;
    });
    $('.tripTikForm input, .tripTikForm select').keydown(function(event) {
        if (event.which && event.which == 13)
        {
            $('#tripTik-go').click();
        }
    });
    $('#tripTik-go').click(function() {
        var errorFormObject;
        var errorMessage = "";
    
        if (($('#tripTikForm-departing-address').val()).length == 0) {
            errorFormObject = (!errorFormObject) ? $('#tripTikForm-departing-address') : errorFormObject;
            errorMessage += "\n  Please enter a Departing Address.";
        }
        if (($('#tripTikForm-departing-city').val()).length == 0) {
            errorFormObject = (!errorFormObject) ? $('#tripTikForm-departing-city') : errorFormObject;
            errorMessage += "\n  Please enter a Departing City.";
        }
        if (($('#tripTikForm-departing-state').val()).length == 0) {
            errorFormObject = (!errorFormObject) ? $('#tripTikForm-departing-state') : errorFormObject;
            errorMessage += "\n  Please select a Departing State.";
        }
        	    
        if (($('#tripTikForm-destination-address').val()).length == 0) {
            errorFormObject = (!errorFormObject) ? $('#tripTikForm-destination-address') : errorFormObject;
            errorMessage += "\n  Please enter a Destination Address.";
        }
        if (($('#tripTikForm-destination-city').val()).length == 0) {
            errorFormObject = (!errorFormObject) ? $('#tripTikForm-destination-city') : errorFormObject;
            errorMessage += "\n  Please enter a Destination City.";
        }
        if (($('#tripTikForm-destination-state').val()).length == 0) {
            errorFormObject = (!errorFormObject) ? $('#tripTikForm-destination-state') : errorFormObject;
            errorMessage += "\n  Please select a Destination State.";
        }
        
        if (errorFormObject)
        {
            alert('The following errors were found:\n\nPlease correct these errors before sumbitting the form\n' + errorMessage) + '\n';
            errorFormObject.focus();
            return false;
        }
        
        window.open(
                    'http://www.aaa.com/scripts/WebObjects.dll/AAAOnline?association=AAA&club=212&page=ITTServices&target=self&subCommand=routeLocs&routeLocs=Address|'+
                    $('#tripTikForm-departing-address').val()+'|'+$('#tripTikForm-departing-city').val()+'|'+$('#tripTikForm-departing-state').val()+
                    '| | | |Address|'+
                    $('#tripTikForm-destination-address').val()+'|'+$('#tripTikForm-destination-city').val()+'|'+$('#tripTikForm-destination-state').val()+
                    '| | |','Triptiker', 'left=20,top=20,toolbar=1,resizable=1,scrollbars=1'
                    );

        return false;
    });
    $('.bookTravelForm input, .bookTravelForm select').keydown(function(event) {
        if (event.which && event.which == 13)
        {
            $('#bookTravelForm-go').click();
        }
    });
    $('#bookTravelForm-guests-children').change(function() {
        var i = 1;
        
        $('.bookTravelForm-children div').each(function() {
            if ($('#bookTravelForm-guests-children').val() >= i)
                $(this).addClass('showChild');
            else
                $(this).removeClass('showChild');
        
            i += 1;
        });
        
    });
    $('#bookTravelForm-go').click(function() {
        var childAges = '';
        var i = 1;
        var validationFailure = false;
        
        if ($('#bookTravelForm-destination-city').val().length == 0)
        {
            alert('Please enter your destination City');
            return false;
        }
        
        $('.bookTravelForm-children div select').each(function() {
            if ($('#bookTravelForm-guests-children').val() >= i && $(this).val() == '')
            {
                alert('You must select the age of the children who will be accompanying you.');
                $(this).focus();
                validationFailure = true;
                return false;
            }
            else if ($('#bookTravelForm-guests-children').val() >= i)
                childAges += '<input type="hidden" name="childage' + i + '" value="' + $(this).val() + '" />';
        
            i += 1;
        });
        if (validationFailure)
            return false;
        
        $('#straightToPageBottomJS')
            .empty()
            .append('' +
                '<form method="post" id="HotelSearch" name="HotelSearch" action="http://acsc.americantours.com/searchHTL.php?campaign=hpati" target="_new">' +
                    '<input type="hidden" name="searchType" value="HOTEL" /> ' +
                    '<input type="hidden" name="includeSurrounding" value="YES" />' +
                    '<input type="hidden" name="diamondRating" value="0" /> ' +
                    '<input type="hidden" name="club" value="212" />' +
                    '<input type="hidden" name="memid" value="nonmem" />' +
                    '<input type="hidden" name="city" value="'          + $('#bookTravelForm-destination-city').val() + '" />' +
                    '<input type="hidden" name="checkinMonth" value="'  + $('#bookTravelForm-checkin-month').val() + '" />' +
                    '<input type="hidden" name="checkinDay" value="'    + $('#bookTravelForm-checkin-day').val() + '" />' +
                    '<input type="hidden" name="checkoutMonth" value="' + $('#bookTravelForm-checkout-month').val() + '" />' +
                    '<input type="hidden" name="checkoutDay" value="'   + $('#bookTravelForm-checkout-day').val() + '" />' +
                    '<input type="hidden" name="adults" value="'        + $('#bookTravelForm-guests-adult').val() + '" />' +
                    '<input type="hidden" name="children" value="'      + $('#bookTravelForm-guests-children').val() + '" />' +
                    childAges +
                '</form>')
            ;
        
            $('#HotelSearch').submit();
            $('#straightToPageBottomJS').empty();
        return false;
    });
       
    var today = new Date();
    var checkinDate = new Date();
    var checkoutDate = new Date();
    
    checkinDate.setDate(today.getDate() + 3);
    checkoutDate.setDate(today.getDate() + 5);
   
    $('#bookTravelForm-checkin-day').val(checkinDate.getDate());
    $('#bookTravelForm-checkin-month').val(getMonthStr(checkinDate.getMonth()));
    $('#bookTravelForm-checkin-year').val(checkinDate.getFullYear());
    
    $('#bookTravelForm-checkout-day').val(checkoutDate.getDate());
    $('#bookTravelForm-checkout-month').val(getMonthStr(checkoutDate.getMonth()));
    $('#bookTravelForm-checkout-year').val(checkoutDate.getFullYear());
    
});

function getMonthStr( month){

    switch(month){
        case 0: return 'Jan'; break;
        case 1: return 'Feb'; break;
        case 2: return 'Mar'; break;
        case 3: return 'Apr'; break;
        case 4: return 'May'; break;
        case 5: return 'Jun'; break;
        case 6: return 'Jul'; break;
        case 7: return 'Aug'; break;
        case 8: return 'Sep'; break;
        case 9: return 'Oct'; break;
        case 10: return 'Nov'; break;
        case 11: return 'Dec'; break;
    }

}