/* withing the /admin - - - - - - - - - - - - - - - - - - - - - */

// for the vields index page
function gosearch() {
	this.location.href='/admin/vields/index/search/' + document.getElementById('choice').value;
	return false;
}

// ajax to instantly remove deleted registrants
jQuery(function(){
	jQuery(".clicktoremove").click(removerow);
});

function removerow() {
	if (confirm('You definitely want to change this person\'s Attendance Status?')) {
		var parentclass = $(this).parent().parent().attr('class');
		var classes = parentclass.split(' ');
		var regid = classes[0].replace('regid-','');

		$.get("/admin/registrants/ajaxremove/"+regid, function(data){
			if (data != 'success') alert('Whoops, there was a problem!\n\nError Info:\n'+data);
			else {
				jQuery('.'+classes[0]).addClass('red');
				jQuery('.'+classes[0]).fadeOut(1500);
			}
		});
	}
}


/* visitor front-end - - - - - - - - - - - - - - - - - - - - - */


// 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);
});

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

jQuery(function(){
	$('#EventRegForm').submit(function() {
		str = '';
		$('.required').each(function() {
			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;		
		}
	});
});


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

var bjmember = 'hs_p1_member';
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 radult = 'hs_retreat_adult1';
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 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'] = 950;
tuition[2] = Array();
tuition[2]['name'] = "Aleph (1st grade)";
tuition[2]['cost'] = 950;
tuition[3] = Array();
tuition[3]['name'] = "Bet (2nd grade)";
tuition[3]['cost'] = 1000;
tuition[4] = Array();
tuition[4]['name'] = "Gimmel (3rd grade)";
tuition[4]['cost'] = 1600;
tuition[5] = Array();
tuition[5]['name'] = "Dalet (4th grade)";
tuition[5]['cost'] = 1600;
tuition[6] = Array();
tuition[6]['name'] = "Hey (5th grade)";
tuition[6]['cost'] = 1600;
tuition[7] = Array();
tuition[7]['name'] = "Vav (6th grade)";
tuition[7]['cost'] = 1200;
tuition[8] = Array();
tuition[8]['name'] = "Zayin (7th grade)";
tuition[8]['cost'] = 1000;
tuition[9] = Array();
tuition[9]['name'] = "Mem*";
tuition[9]['cost'] = 1000;

function get_cost(type,num) {
	if (type=='member') {
		if (num == 2) return totalkids * 300;
		else return 0;
	} else if (type=='late') {
		var days = 0;
		var difference = 0;
		endDate = new Date("July 15, 2010"); // starting july 16, late fee applies
		today = new Date();
		difference = endDate - today;
		days = Math.round(difference/(1000*60*60*24));
		latefee = 150;
		if (days >= 0) return 0;
		else return latefee;
	} else if (type=='retreat') {
		var rcosts = 0;
		if (jQuery('#'+radult).val().length > 1 ) rcosts += 235;
		if (jQuery('#'+rchild1).val().length > 1 ) rcosts += 175;
		if (jQuery('#'+rchild2).val().length > 1 ) rcosts += 175;
		if (jQuery('#'+rchild3).val().length > 1 ) rcosts += 175;
		return rcosts;
	}
}

var totalkids = 0;

function calculate_kids() {
	totalkids = 0;
	
	var kid1 = jQuery('#'+child1grade).attr("selectedIndex"); var kid1name = jQuery('#'+child1name).val();
	var kid2 = jQuery('#'+child2grade).attr("selectedIndex"); var kid2name = jQuery('#'+child2name).val();
	var kid3 = jQuery('#'+child3grade).attr("selectedIndex"); var kid3name = jQuery('#'+child3name).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 (totalkids > 1) costs['Multi-child discount'] = -200;
	else costs['Multi-child discount'] = 0;
}

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 border="1">';
	for (var i in costs) {
		if (costs[i] != 0)
			str += '<tr><td>'+i+'</td><td>$'+costs[i]+'</td></tr>';
	}
	var total = get_total();
	str += '<tr><td><b>Total</b></td><td><b>$'+total+'</b></td></tr>';
	str += '</table>';
	jQuery('#note').html(str);
}

function show_hide_dayofweek() {
	var kid1 = jQuery('#'+child1grade).attr("selectedIndex");
	var kid2 = jQuery('#'+child2grade).attr("selectedIndex");
	var kid3 = jQuery('#'+child3grade).attr("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();
}

function run_calculations() {
	costs = Array();
	calculate_kids();
	costs['Retreat'] = get_cost('retreat',0);
	costs['Non-Member surcharge'] = get_cost('member', jQuery('#'+bjmember).attr("selectedIndex"));
	costs['Late Fee (if after July 15)'] = 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();
}

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_';

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