// JavaScript Document

var ppspw = 0.25;

$(function(){

	set_date_thingy();

	$('#price_calculator input').change(function(){
		if(calculator_validate()){
			$('#total_cost').text(calculate_price());
		}else{
			$('#total_cost').text('');
		}
	})



})

function calculate_price(){
	var first_perf = new Date($('#first_perf').val().split("/")[1]+"/"+$('#first_perf').val().split("/")[0]+"/"+$('#first_perf').val().split("/")[2])
	var last_perf = new Date($('#last_perf').val().split("/")[1]+"/"+$('#last_perf').val().split("/")[0]+"/"+$('#last_perf').val().split("/")[2])
//	alert(last_perf-first_perf)
	var weeks = (Math.floor((last_perf-first_perf)/604800000))+1
//	alert(weeks);
	var seats = $('#num_seats').val()
	var total = (weeks*seats*ppspw);
	if(total<(10*weeks)){
		total = 10 * weeks;
	}
	total = total.toFixed(2)
	var amount = total;
	return amount;
}

function calculator_validate(){
	var is_okay=1;
	$('#price_calculator :input').each(function(){
		if(!$(this).val() || $(this).val()==0){
			is_okay=0;
		}else{
			if($(this).valid()){
				//
			}else{
				is_okay=0;
			}
		}
	})
	if($('#first_perf').val() && $('#last_perf').val()){
		var first_perf = new Date($('#first_perf').val().split("/")[1]+"/"+$('#first_perf').val().split("/")[0]+"/"+$('#first_perf').val().split("/")[2])
		var last_perf = new Date($('#last_perf').val().split("/")[1]+"/"+$('#last_perf').val().split("/")[0]+"/"+$('#last_perf').val().split("/")[2])
		if(last_perf<first_perf){
			is_okay=0;
			$('#form_error').text('First performance must be before last performance.')
		}else{
			$('#form_error').text('')
		}
	}else{
			$('#form_error').text('')
		}
	return is_okay;
}
