var highest_avail =0;
$(document).ready(function () {	
	$("form").each( function () {
		var whole_form = $(this);
		var elements_of_form = $("fieldset", this);
		
		//hide all fieldsets
		elements_of_form.hide();

		//show first page
		$("fieldset:first", this).show();
		
		$(this).prepend(build_nav(1));
		$(this).append(buttons(1));
		$(this).append(build_nav(1));
		$('div.nav:last').prepend('<div style="float:left;margin-left:3px;text-align:left;font-size:10px;color:#666">Having any difficulties with our gift form? <a href="support.html">Get help.</a></div>');
		$('a.nav_item').click(function () {onClick(this);});
	});

});
function onClick (clicked_link){
	$('#cover').hide();
	var dest = $(clicked_link).attr('rel');
	var visible_form = $('fieldset:visible');

	if (validate_page(visible_form)) {
		$(visible_form).hide();
		if (highest_avail < dest) {
			highest_avail = dest;
		}
		$('fieldset').eq(dest-1).show(); 
		$('div.buttons').html(buttons(dest));
		$('div.nav').html(build_nav(dest));
		$('div.nav:last').prepend('<div style="float:left;margin-left:3px;text-align:left;font-size:10px;color:#666">If you have any difficulties with our gift form, e-mail us at <a href="mailto:giftprgm@bu.edu">giftprgm@bu.edu</a>.</div>');
		$('a.nav_item').click(function () {onClick(this);});
		if (dest == 2) {
			$("a.nav_item[rel=3]").unbind();
			$("a.nav_item[rel=3]").click(function(event){
				if (validate_page($('fieldset:visible'))) {								  
					$('#cover').show();
					$('#cover').width($('fieldset:visible').width()-20);
					$('#cover').height($('fieldset:visible').height()-14);
					if ($("input[name='gift_amount']:checked").val() != 'Other ') {
						$("input[name='gift_amount_other']").val('');
					}
				}
			});
		}else if (dest == 3) {
			update_honoree();
		}
		else if (dest == 5) {
			update_confirmation();
		}	
	} 			

}
function validate_page (visible_form) {
	var page_pass = true;
	var field_pass = true;
	var temp = "";
	$(':input', visible_form).each(function () {
		field_pass = $('form').validate().element(this);
		if (field_pass == false) { // can't use simpler page_pass = page_pass && field_pass; as validate() does not always return true or false 
			page_pass = false;
		}
	});

	return page_pass;
}
function update_honoree () {
	if ($("input[name='gift_amount']:checked").val()=='Other ') {
		if($("input[name='gift_amount_other']").val()<20.09) {
			$('.honoree').html('Your gift level does not include an honoree.  <a href="#" rel="2"  class="nav_item">Increase your gift</a> if you would like to have this opportunity to honor a mentor.  Otherwise click next below to continue to the next step.');
			$('a.nav_item').click(function () {onClick(this);});
			$('.honoree').next().hide();
			$('.honoree').next().next().hide();
			$('.honoree').next().next().next().hide();
			$('.honoree').next().next().next().next().hide();
		}
	} else {
		$('.honoree').html('Your gift includes an opportunity to honor a mentor.  Fill out the information about your honoree below and we will send a small memento from the Class Gift Program along with a note card containing the personal message you select below.');
		$('.honoree').next().show();
		$('.honoree').next().next().show();
		$('.honoree').next().next().next().show();
		$('.honoree').next().next().next().next().show();
	}
}
function get_month() {
var d=new Date();

var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

return month[d.getMonth()];
}
function update_confirmation () {
	var confirm_string = '<div style="width:48%;padding:1%;float:left"><h3>Selected Fund <small><a href="#" rel="1" class="nav_item">edit</a></small></h3><span style="font-size:20px">';
	confirm_string += $("input[name='designation']:checked:first").parent('label').text();
	confirm_string += '</span><h3>Gift Amount <small><a href="#" rel="2" class="nav_item">edit</a></small></h3><span style="font-size:20px">'
	var amount = $("input[name='gift_amount']:checked").val();
	if (amount == 'Other ') {
		amount = '$'+$("input[name='gift_amount_other']").val();
	}
	confirm_string += amount;
	confirm_string += '</span>';
	if ($("input[name='gift_amount_extend']:checked").val()!=null) {
		confirm_string += '<br /><strong>Five Year Pledge</strong> - Your credit card will be charged '+amount+' today and again each ' +get_month() +' for the duration of your pledge.';
	}
	confirm_string += '<h3>Honoree Information <small><a href="#" rel="3" class="nav_item">edit</a></small></h3>';
	var honoree = $("input[name='professor']").val();
	if (honoree == '') {
		confirm_string += 'You have not designated an honoree.';
	} else {
		confirm_string += '<span style="font-size:20px">' + honoree + '</span><br />';
		var honoree_obj = $("fieldset#honoree");
		var temp = '';
		temp = $("input[name='p_address']", honoree_obj).val();
		if (temp!='') confirm_string+=temp+'<br />';
		temp = $("input[name='p_address2']", honoree_obj).val();
		if (temp!='') confirm_string+=temp+'<br />';
		temp = $("input[name='p_city']", honoree_obj).val();
		if (temp!='') confirm_string+= temp+','+$("input[name='p_state']", honoree_obj).val()+' ' + $("input[name='p_zip']", honoree_obj).val()+'<br />';
		temp = $("select[name='p_country']", honoree_obj).val();
		if (temp!='') confirm_string+=temp+'<br /><br />';
		temp = $("input[name='p_phone']", honoree_obj).val();
		if (temp!='') confirm_string+=temp+'<br />';
		temp = $("input[name='p_email_address']", honoree_obj).val();
		if (temp!='') confirm_string+=temp+'<br />';
	}
	confirm_string += '</div><div style="width:48%;padding:1%;float:right"><h3>Your Information <small><a href="#" rel="4" class="nav_item">edit</a></small></h3><span style="font-size:20px">'
	var donor_obj = $("fieldset#donor");
	confirm_string += $("input[name='first_name']", donor_obj).val()+' '+$("input[name='last_name']", donor_obj).val();
	confirm_string += ', '+$("select[name='school']", donor_obj).val()+' \'09</span><br />';
	confirm_string += $("input[name='uid']", donor_obj).val()+'<br /><br />';
	var temp = '';
	temp = $("input[name='address']", donor_obj).val();
	if (temp!='') confirm_string+=temp+'<br />';
	temp = $("input[name='address2']", donor_obj).val();
	if (temp!='') confirm_string+=temp+'<br />';
	temp = $("input[name='city']", donor_obj).val();
	if (temp!='') confirm_string+= temp+','+$("input[name='state']", donor_obj).val()+' ' + $("input[name='zip']", donor_obj).val()+'<br />';
	temp = $("select[name='country']", donor_obj).val();
	if (temp!='') confirm_string+=temp+'<br /><br />';
	temp = $("input[name='phone']", donor_obj).val();
	if (temp!='') confirm_string+=temp+'<br />';
	temp = $("input[name='email_address']", donor_obj).val();
	if (temp!='') confirm_string+=temp+'<br />';

	temp = $("input[name='credit_card_number']", donor_obj).val();
	confirm_string += '<br /><strong>'+$("select[name='credit_card_type']", donor_obj).val() + " ************" + temp.substr(temp.length-5) + '</strong>, Exp ' + $("select[name='expiration_month']", donor_obj).val() + '/' + $("select[name='expiration_year']", donor_obj).val();

	confirm_string += '</div>';
	$('#confirmation').html(confirm_string);
	$('a.nav_item').click(function () {onClick(this);});
}
function buttons (current_page) {
	var num_of_pages = $("fieldset").length;
	
	var str = '<div class="buttons">';

	
	if (current_page<num_of_pages) {
		str += '<a href="#" rel="'+(parseInt(current_page)+1)+'" class="nav_item">Next</a><br />';
	} else {
		str += '<input type="submit" onclick="fileMyTaxes()" value="Submit" name="submit" />';
	}
	if (current_page>1) {
		str += '<a href="#" rel="'+(current_page-1)+'" class="nav_item previous">Previous</a>';
	}
	str +='</div>';
	
	return str;
}
function build_nav (current_page) {
	var num_of_pages = $("fieldset").length;
	var page_names = ['Fund', 'Gift Level', 'Honoree', 'Your Information', 'Confirm &amp; Submit'];
	var str = '<div class="nav">';
	for( var i=0;i<num_of_pages;i++) {
		if (current_page>i+1) {
			str += '<a href="#" rel="'+(i+1)+'" class="nav_item">'+page_names[i]+'</a>';
		} else if (current_page==i+1) {
			str += '<span class="current">'+page_names[i]+'</span>';
		} else {
			if (i+1 > highest_avail) { str += '<span class="future">'+page_names[i]+'</span>'; }
			else { str += '<a href="#" rel="'+(i+1)+'" class="nav_item">'+page_names[i]+'</a>'; }
		}
	}
	str +='</div>';
	
	return str;
}