﻿
$(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
	$('.membershipInsuranceForm2 input').keydown(function(event) {
		if (event.which && event.which == 13)
        { 
            $('#membershipInsuranceForm-calculate2').click();
        }
        else if (event.charCode && (event.charCode < 48 || event.charCode > 57)) 
        {
			event.preventDefault();
        }
    });
    $('.membershipInsuranceForm2 select').keydown(function(event) {
    if (event.which && event.which == 13)
    {
        $('#membershipInsuranceForm-calculate2').click();
    }
    });
    $('#membershipInsuranceForm-calculate2').click(function() {
		/*efeldman 1-29-09*/
        var state;
        var savings = 0;
		var disclaimer;
        if (($('#membershipInsuranceForm-accountAddressFormFieldsState2').val()).length) {
            state = $('#membershipInsuranceForm-accountAddressFormFieldsState2').val();
			
        }
        else {
            alert('Please select a state');
            $('#membershipInsuranceForm-accountAddressFormFieldsState2').focus();
            return false;
        }
        
        if (state == 'VA') {
          savings = 289; 
		  disclaimer = 'Virginia';
        }
        else if (state == 'CT') {
          savings = 576;  
		  disclaimer = 'Connecticut';
        }
        else if (state == 'OH') {
          savings = 363;
		  disclaimer = 'Ohio & Indiana';
        }
        else if (state == 'IN') {
          savings = 363;  
		  disclaimer = 'Ohio & Indiana';
        }
        else if (state == 'KY') {
          savings = 474;
		  disclaimer = 'Kentucky';
        }
        else if (state == 'WV') {
          savings = 289; 
		  disclaimer = 'West Virginia';
        }
        else if (state == 'KS') {
          savings = 374;
		  disclaimer = 'Kansas';
        }
        
		if (savings > 0)
        {
	        $('#membershipInsuranceForm-results-output2').text(String('Members save an average of $' + savings + '*'));
            $('#membershipInsuranceForm-results2').css('display', 'block');
			
			$('#disclaimer-results-output').text(String('All insurance products sold through AAA Allied Insurance Services, Inc.  *$'+ savings +' average savings is based on annual AAA Auto Insurance premium written in January 09 – October 09 in AAA Allied Insurance Services, Inc '+ disclaimer +' territory. Discount savings are approximate and may vary by state and by policy.'));
			/*$('#disclaimer-results-output').text(String('* Based on average savings on annual auto premium written in January 08 - November 08 in '+disclaimer+'. Discount savings are approximate and may vary by state and by policy. Other restrictions may apply. All insurance sold through AAA Allied Insurance Services, Inc.  Auto, Homeowners and Personal Umbrella Coverage provided through the member companies of Cal State Auto Group. The group is part of the California State Automobile Association of Companies.'));*/
            $('#disclaimer-results-output').css('display', 'block');
			
        }
        else
        {
	        $('#membershipInsuranceForm-results-output2').text(String('You are not eligible for a Member discount at this time...'));
            $('#membershipInsuranceForm-results2').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() {
       	/*efeldman 1-26-09*/
	    var errorFormObject;
		var errorMessage = "";
	
		if (($('#tripTikForm-departing-address').val()).length == 0) {
			errorFormObject = (!errorFormObject) ? $('#tripTikForm-departing-address') : errorFormObject;
			errorMessage += "<li>Please enter a Departing Address.</li>";
		}
		if (($('#tripTikForm-departing-city').val()).length == 0) {
			errorFormObject = (!errorFormObject) ? $('#tripTikForm-departing-city') : errorFormObject;
			errorMessage += "<li>Please enter a Departing City.</li>";
		}
		if (($('#tripTikForm-departing-state').val()).length == 0) {
			errorFormObject = (!errorFormObject) ? $('#tripTikForm-departing-state') : errorFormObject;
			errorMessage += "<li>Please select a Departing State.</li>";
		}
				
		if (($('#tripTikForm-destination-address').val()).length == 0) {
			errorFormObject = (!errorFormObject) ? $('#tripTikForm-destination-address') : errorFormObject;
			errorMessage += "<li>Please enter a Destination Address.</li>";
		}
		if (($('#tripTikForm-destination-city').val()).length == 0) {
			errorFormObject = (!errorFormObject) ? $('#tripTikForm-destination-city') : errorFormObject;
			errorMessage += "<li>Please enter a Destination City.</li>";
		}
		if (($('#tripTikForm-destination-state').val()).length == 0) {
			errorFormObject = (!errorFormObject) ? $('#tripTikForm-destination-state') : errorFormObject;
			errorMessage += "<li>Please select a Destination State.</li>";
		}
		
		if (errorFormObject)
		{
			document.getElementById('error').innerHTML = "<ul class='bullet'>"+ errorMessage + "</ul>";
			errorFormObject.focus();
			return false;
		}
		
		window.open(
					'http://www.aaa.com/scripts/WebObjects.dll/AAAOnline?association=AAA&club=007&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="007" />' +
                    '<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;
    }

}