﻿var loading = '<div id="formBuilderWrapperLoading"><img src="/assets/images/global/forms/loading.gif"></div>';

$(document).ready(function(){
	$("form#RentalForm").submit(function(){
		var submitLocation='/components/rentals/index.cfm?step=validateStep1';
		formLabelsAndValues=$("form#RentalForm").serialize();
		$("#rentalFormWrapper").html(loading);
		$.post(submitLocation,formLabelsAndValues, function(response) {
			commentResponse(response);
		});
		return false;
	});
	rentalType();
	checkSeasonPassNo();
	rentalAmount();
	liftTicketToggle();
	birthDate();
	$("#rentalTypeID").change(function() {
		rentalType();
		rentalAmount();
	});
	$("#damageWaiver").click(function() {
		rentalAmount();
	});	
	$("#adultHelmet").click(function() {
		rentalAmount();
	});	
	$("#FromRentalDate").change(function() {
		liftTickets();
	});	
	$("#ToRentalDate").change(function() {
		liftTickets();
	});	
	$("#hasRentalSeasonPass").change(function() {
		checkSeasonPassNo();
	});	
	$("#hasLiftTicket").change(function() {
		liftTicketToggle();
		rentalAmount();
	});
	$("#liftTicketID").change(function() {
		rentalAmount();
	});	
	$("#BirthDate").change(function() {
		birthDate();
	});
});

function commentResponse(response) {
	if($("status",response).text() == "2") return;
	$("#rentalFormWrapper").replaceWith(response);
}

function birthDate() {
	//ShowRentalTypes//
	$.getJSON("/components/rentals/index.cfm?step=rentalTypes",{birthdate: $("#BirthDate").val()}, function(j){
		var options = '';
		for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i].optionValue + '" ' + j[i].optionSelected + ' >' + j[i].optionDisplay + '</option>';
		}
		$("#rentalTypeID").html(options);
	});
	liftTickets();
}

function liftTickets() {
	//Show Lift Tickets//
	$.getJSON("/components/rentals/index.cfm?step=liftTickets",{birthdate: $("#BirthDate").val(),fromDate: $("#FromRentalDate").val(), toDate: $("#ToRentalDate").val()}, function(j){
		var options = '';
		for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i].optionValue + '" ' + j[i].optionSelected + ' >' + j[i].optionDisplay + '</option>';
		}
		$("#liftTicketID").html(options);
	});
	rentalAmount();
}


function rentalType() {
	rentalTypeID=$("#rentalTypeID").val();
	$("#Skis").hide();
	$("#AdultHelmetDisplay").hide();
	if (rentalTypeID=='0fc92bbf-7e3e-4447-b678-19465652f251' || rentalTypeID=='084900fc-faa5-4c54-8b9c-f76df6847bbb' || rentalTypeID=='e8439af2-dc6e-4e29-930d-ce35824ba5f1') {
		$("#Skis").show();
	};
	/*
	if (rentalTypeID=='b6ca3bdd-d350-4177-aa03-4613689ddd22' || rentalTypeID=='c11d545c-6d8d-411f-836c-9c7a2792244f') {
		$("#Snowboard").show();
	};
	*/
	if (rentalTypeID=='0fc92bbf-7e3e-4447-b678-19465652f251' || rentalTypeID=='b6ca3bdd-d350-4177-aa03-4613689ddd22') {
		$("#AdultHelmetDisplay").show();
	};
}

function liftTicketToggle() {
	hasLiftTicket=$("#hasLiftTicket").val();
	$("#liftTicketDisplay").hide();
	if (hasLiftTicket==1) {
		$("#liftTicketDisplay").show();
	};
}

function checkSeasonPassNo() {
	hasPass=$("#hasRentalSeasonPass").val();
	$("#rentalSeasonPassNumber").hide();
	$("#creditCard").hide();
	if (hasPass=='0') {
		$("#creditCard").show();
	};
	if (hasPass=='1') {
		$("#rentalSeasonPassNumber").show();
	};	

}

function rentalAmount() {
	formLabelsAndValues=$("form#RentalForm").serialize();
	$.post('/components/rentals/index.cfm?step=rentalAmount',formLabelsAndValues, function(response) {
		$("#rentalAmount").html(response);
	});
}
		
