
// Show/Hide collapsable sections
jQuery(function(){
	jQuery(".click_to_open").click(showsection, showsection);
	jQuery(".click_to_close").click(showsection, showsection);
});

function showsection(){
	var currentid = $(this).parent().attr('id');
	jQuery('#'+currentid+' .thefields').toggleClass('hidden');
	jQuery('#'+currentid+' .click_to_open').toggleClass('hidden');
	jQuery('#'+currentid+' .click_to_close').toggleClass('hidden');
	jQuery('#'+currentid+'_marker').val('');
}

// Alert user if trying to type in more than 255 characters
// (does not REMOVE characters beyond 255)

function check_length(){
	if (this.value.length > 255) { 
		this.blur();
		alert('The maximum number of characters you may enter is 255. If you keep typing, you will go over the limit.') };
}

jQuery(function(){
	jQuery(".front-end input").keyup(check_length);
	jQuery(".front-end textarea").keyup(check_length);
});

// Toggle requiredness for 'for participant required' fields based on
// whether the name has been filled in

function toggle_required(){
	
	var classList = $(this).parent().attr('class').split(/\s+/); // get list of classes on containing div
	var partnum = '';
	jQuery.each( classList, function(index, item) { // foreach class
		var match = item.match(/^part[0-9]$/);      // is it the part number ?
		if (match) partnum = match;                 // if so, save it
	});

	if ($(this).val() != '')
		jQuery('.'+partnum+'required').addClass('required');    // add 'required' class to dependent requireds
	else
		jQuery('.'+partnum+'required').removeClass('required'); // remove 'required' class from dependent requireds
}

jQuery(function(){
	jQuery(".front-end .part_name input").keyup(toggle_required);
	jQuery(".front-end .part_name input").blur(toggle_required);
});


// Alert user if any required fields are blank
// and prevent form submission

jQuery(function(){
	if (!$('#testing_mode').html()) {
	
		$('#EventRegForm').submit(function() {
			str = '';
			$('.required').each(function() {
				if (!$(this).hasClass('checkboxes')) {
					if ($(this).find('input').val()=='' || $(this).find('select').val()=='')
						str += ' - ' + $(this).find('label').text() + '\n';
				}
			});
			if (str != '') {
				alert('You left these required fields blank:\n\n'+str+'\nPlease fill them out and try again.');
				return false;
			} else {
				return true;		
			}
		});
	}
});



// calculate total due and why on any non-HS event
function run_gen_calculations() {
	costs = Array();
		
	$("select[id*='price']").each(function() {
		costs[$(this).parent().find('label').text()] = $(this).val();
	});
	$("input[id*='price']").each(function() {
		costs[$(this).parent().find('label').text()] = $(this).val();
	});

	costs_to_table();
}

jQuery(function(){
	jQuery("input").keyup(run_gen_calculations);
	jQuery("select").change(run_gen_calculations);
});



/* HEBREW SCHOOL FORM ONLY - visitor front-end - - - - - - - - - - - */

var bjmember = 'hs_p1_member';
var newtohs = 'hs_p1_newtohs';
var child1name = 'hs_child1_first';
var child1grade = 'hs_child1_grade';
var child2name = 'hs_child2_first';
var child2grade = 'hs_child2_grade';
var child3name = 'hs_child3_first';
var child3grade = 'hs_child3_grade';
var child4name = 'hs_child4_first';
var child4grade = 'hs_child4_grade';
var radult = 'hs_retreat_adult1';
var ryouth1 = 'hs_retreat_youth1';
var ryouth2 = 'hs_retreat_youth2';
var ryouth3 = 'hs_retreat_youth3';
var rchild1 = 'hs_retreat_child1';
var rchild2 = 'hs_retreat_child2';
var rchild3 = 'hs_retreat_child3';
var donation = 'donation_price';
var dayofweek1 = 'hs_child1_weekday';
var dayofweek2 = 'hs_child2_weekday';
var dayofweek3 = 'hs_child3_weekday';
var dayofweek4 = 'hs_child4_weekday';
var child_req = Array('first','last','hname','birthdateMonth','gender','bjew','grade','walkhome','ftrips','permdoc');

var costs = Array();

var tuition = Array();
tuition[1] = Array();
tuition[1]['name'] = "Gan (Kindergarten)";
tuition[1]['cost'] = 975;
tuition[2] = Array();
tuition[2]['name'] = "Aleph (1st grade)";
tuition[2]['cost'] = 975;
tuition[3] = Array();
tuition[3]['name'] = "Bet (2nd grade)";
tuition[3]['cost'] = 1025;
tuition[4] = Array();
tuition[4]['name'] = "Gimmel (3rd grade)";
tuition[4]['cost'] = 1625;
tuition[5] = Array();
tuition[5]['name'] = "Dalet (4th grade)";
tuition[5]['cost'] = 1625;
tuition[6] = Array();
tuition[6]['name'] = "Hey (5th grade)";
tuition[6]['cost'] = 1625;
tuition[7] = Array();
tuition[7]['name'] = "Vav (6th grade)";
tuition[7]['cost'] = 1225;
tuition[8] = Array();
tuition[8]['name'] = "Zayin (7th grade)";
tuition[8]['cost'] = 1025;
tuition[9] = Array();
tuition[9]['name'] = "Mem*";
tuition[9]['cost'] = 1025;

function get_cost(type,num) {
	if (type=='member') {
		if (num == 2) return totalkids * 300;
		else return 0;
	}
	if (type=='newtohs') {
		if (num == 1) return 35;
		else return 0;	
	}
	if (type=='late') {
		var endDate = new Date();
		endDate.setFullYear(2011,07,01);
		var today = new Date();
		latefee = 150;
		if (endDate > today) return 0;
		else return latefee;
	}
	if (type=='retreat') {
		var rcosts = 0;
		if (jQuery('#'+radult).val().length > 1 ) rcosts += 250;
		if (jQuery('#'+ryouth1).val().length > 1 ) rcosts += 200;
		if (jQuery('#'+ryouth2).val().length > 1 ) rcosts += 200;
		if (jQuery('#'+ryouth3).val().length > 1 ) rcosts += 200;
		if (jQuery('#'+rchild1).val().length > 1 ) rcosts += 135;
		if (jQuery('#'+rchild2).val().length > 1 ) rcosts += 135;
		if (jQuery('#'+rchild3).val().length > 1 ) rcosts += 135;
		return rcosts;
	}
}

var totalkids = 0;

function calculate_kids() {
	totalkids = 0;

	var kid1 = jQuery('#'+child1grade)[0].selectedIndex; var kid1name = jQuery('#'+child1name).val();
	var kid2 = jQuery('#'+child2grade)[0].selectedIndex; var kid2name = jQuery('#'+child2name).val();
	var kid3 = jQuery('#'+child3grade)[0].selectedIndex; var kid3name = jQuery('#'+child3name).val();
	var kid4 = jQuery('#'+child4grade)[0].selectedIndex; var kid4name = jQuery('#'+child4name).val();

	if (kid1 != 0) { costs['Tuition for '+kid1name] = tuition[kid1]['cost']; totalkids += 1; }
	else { costs['Tuition for '+kid1name] = 0; }
	if (kid2 != 0) { costs['Tuition for '+kid2name] = tuition[kid2]['cost']; totalkids += 1; } 
	else { costs['Tuition for '+kid2name] = 0; }
	if (kid3 != 0) { costs['Tuition for '+kid3name] = tuition[kid3]['cost']; totalkids += 1; }
	else { costs['Tuition for '+kid3name] = 0; }
	if (kid4 != 0) { costs['Tuition for '+kid4name] = tuition[kid4]['cost']; totalkids += 1; }
	else { costs['Tuition for '+kid4name] = 0; }
	
	if (totalkids > 1) costs['Multi-child discount'] = -200;
	else costs['Multi-child discount'] = 0;
}

function show_hide_dayofweek() {
	var kid1 = jQuery('#'+child1grade)[0].selectedIndex;
	var kid2 = jQuery('#'+child2grade)[0].selectedIndex;
	var kid3 = jQuery('#'+child3grade)[0].selectedIndex;
	var kid4 = jQuery('#'+child4grade)[0].selectedIndex;
	if (kid1 < 4 && kid1 > 0) jQuery('#'+dayofweek1).parent().show();
	else jQuery('#'+dayofweek1).parent().hide();
	if (kid2 < 4 && kid2 > 0) jQuery('#'+dayofweek2).parent().show();
	else jQuery('#'+dayofweek2).parent().hide();
	if (kid3 < 4 && kid3 > 0) jQuery('#'+dayofweek3).parent().show();
	else jQuery('#'+dayofweek3).parent().hide();
	if (kid4 < 4 && kid4 > 0) jQuery('#'+dayofweek4).parent().show();
	else jQuery('#'+dayofweek4).parent().hide();
}

function run_calculations() {
	costs = Array();
	calculate_kids();
	costs['Retreat'] = get_cost('retreat',0);
	costs['Non-Member surcharge'] = get_cost('member', jQuery('#'+bjmember)[0].selectedIndex);
	costs['New To School'] = get_cost('newtohs', jQuery('#'+newtohs)[0].selectedIndex);
	costs['Late Fee (if after July 1)'] = get_cost('late',0);
	costs['Donation'] = jQuery('#'+donation).val();
	costs_to_table();
	var total = get_total('no'); // do not include the donation because it's kept separate by the stats page and CW
	jQuery('#final_price').val(total);
	
	// also show/hide the 'choose date' stuff
	show_hide_dayofweek();
}

function get_total(include_donation) {
	var total = 0;
	for (var i in costs) {
		var amount = costs[i]*1.0; // convert string to number
		total += amount;
	}	

	if (include_donation != null) { total = total - costs['Donation']; }
	
	return total;
}

function costs_to_table() {
	var str = '';
	str += '<table class="normal_grid">';
	for (var i in costs) {
		if (costs[i] != 0)
			str += '<tr><td>'+i+'</td><td align="right">$'+costs[i]+'</td></tr>';
	}
	var total = get_total();
	str += '<tr><td><b>Total</b></td><td align="right"><b>$'+total+'</b></td></tr>';
	str += '</table>';
	jQuery('#note').html(str);
}

jQuery(function(){
	jQuery(".eventtypeid_6 input").keyup(run_calculations);
	jQuery(".eventtypeid_6 select").change(run_calculations);
});


// for hebrew school form only, make child2 and child3 sub-fields required if shown

jQuery(function(){
	jQuery(".click_to_open").click(add_requiredness);
});

function add_requiredness(){
	var currentid = $(this).parent().attr('id');

	if (currentid == 'section_hs_child2_divider') prefix = 'hs_child2_';
	else if (currentid == 'section_hs_child3_divider') prefix = 'hs_child3_';
	else if (currentid == 'section_hs_child4_divider') prefix = 'hs_child4_';

	for (i in child_req) {
		jQuery('#'+prefix+child_req[i]).parent().addClass('required');
	}
}


