var workIsDone = false;

window.onbeforeunload = confirmBrowseAway;

function confirmBrowseAway()
{
  if (!workIsDone) {
    return "If you leave this page " +
    "now, the form will be cleared!";
  }
}

function fileMyTaxes() {
  // The user has finished their work, so we don't
  // need a warning anymore!
  workIsDone = true;
}

function getURLParam(strParamName){
	  var strReturn = "";
	  var strHref = window.location.href;
	  var bFound=false;
	  
	  var cmpstring = strParamName + "=";
	  var cmplen = cmpstring.length;

	  if ( strHref.indexOf("?") > -1 ){
	    var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	    var aQueryString = strQueryString.split("&");
	    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
	      if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
	        var aParam = aQueryString[iParam].split("=");
	        strReturn = aParam[1];
	        bFound=true;
	        break;
	      }
	      
	    }
	  }
	  if (bFound==false) return null;
	  return strReturn;
	}

$(document).ready(function () {

$(":input").focus(function () {
         $(this).parent().parent().css('background-color','#FFF7C0');
    });
$(":input").blur(function () {
         $(this).parent().parent().css('background-color','#FFFFFF');
    });

$("div.item label").click(function () {
         $(":input:first", $(this).parent()).focus();
    });
$("div.item small").click(function () {
         $(":input:first", $(this).parent()).focus();
    });
//FUND SELECTION - PAGE 1
	//create accordion layout
	var string = '<div id="accordion" class="ui-accordion-container">';
	//Go through and remove optgroups one at at time; transform into clickable tiles
	$("#designation_select optgroup").each(function (i) {
		string += '<h3>' + $(this).attr("label")+'<small>expand</small></h3><div class="accordionable">';
		//if more than 5 split into two columns...
		//add accordian functionality
		var size = $("option", this).size();
		string += '<div class="col">';
		var selected = $('#designation_select option:selected').val();
		$("option", this).each(function (j) {
			if (size>5 && j==Math.ceil(size/2)) {
				string += '</div><div class="col">';
			}
			this.value = $(this).val()
			string += '<label for="'+this.value+'" class="fund_choice';
			if (this.value == selected) {
				//string += ' selected';
			}
			string += '"><input name="designation" type="radio" value="'+this.value+'" id="'+this.value+'" />'+this.innerHTML +'</label>';
		});
	
			string += '</div>';

		string += '</div>';
		$(this).remove();
	});
	string += '</div><small></small>';
	$("#designation").append(string);

	//Remove select element
	$("#designation_select").remove();
		
	//activate accordion
	$('#accordion> div').hide();
	$('#accordion> h3').click(function() {
		var $nextDiv = $(this).next();
		var $visibleSiblings = $nextDiv.siblings('div:visible');
		if ($visibleSiblings.length ) {
			$('#accordion> h3.open').removeClass('open');
			//$(this).addClass('open');
			$visibleSiblings.slideUp('fast', function() {
				$nextDiv.slideToggle('fast');
			});
			//$('#accordion> h3> small:visible small:hidden').toggle();
		} else {
			$nextDiv.slideToggle('fast');
		}
		$(this).toggleClass('open');
		//$('small', this).toggle();
	});
	
	//open accordian surrounding current item (if applicable)
	/*if ($('.selected:eq(0)')) {
		$('.selected:eq(0)').parent().parent().show();
	}
	else {*/
	$('#accordion> h3').eq(getURLParam('sect')* 1).toggleClass('open');
	$('#accordion> div').eq(getURLParam('sect')* 1).show();
	
	//Add preview box and remove directions sentence.
	$(".directions").remove();
	$("#designation").before('<div id="preview"><div class="title">Choose exactly where your gift goes!</div>Our class gift to BU lets you decide exactly what part of the university you\'d like to help.<br /><br />Choose a fund to the left.  Details on how your donation will be used will appear here.</div>');
	$("#designation").removeClass('item');
	$('#designation').after('<div id="error_box"></div>');	
	
	
	//On select of an option, update the selected highlighting and the preview box
	$('label.fund_choice input').click(function () { 
		$('.fund_choice, .selected').removeClass('selected');
		$(this).parent().addClass('selected');
		$(this).parent().effect("transfer", { to: "#preview" }, 1000);
		var selection = $(this).parent().text();
		$("#preview").html('<div class="title"><span style="font-size:10px;font-weight:normal;line-height:10px;color:#FFE1E1">Your choice:<br /></span>'+selection+'</div>'); 
		$.ajax({
			url: "descriptions.html",
			cache: false,
			success: function(result){
				var description = $("#body h3:contains('" + selection + "')", result).next().html();
				if (description!=null) {
					$("#preview").append(description+'<p style="margin-top:10px;font-size:14px;"><strong>Once you’re happy with your choice, click <a href="#" rel="2" class="nav_item">next</a></strong>.</p>');
							$('a.nav_item').click(function () {onClick(this);});

				}
				else {
					$("#preview").append('<h3>Sorry.</h3>We had some trouble getting the description.  <a href="descriptions.html">Try looking at it manually</a>.');
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				$("#preview").append('<h3>Sorry.</h3>We had some trouble getting the description.  <a href="descriptions.html">Try looking at it manually</a>.');
			}
		});
	});
	


//AMOUNT SELECTION PAGE
	//pull in amounts
	var selected = $("input[name='gift_amount']:checked").attr('id');
	var pledge = $("#pledge").attr('checked');
	var gift_amount_string = '';
			var checked = '';
		if (pledge == true) {
			checked = 'checked="checked" ';
		}
		$("fieldset#amount").before('<div id="cover"><h2>Extend your gift - Make a five-year pledge!</h2><p>BU depends on generous contributions like the one you\'re making today to:<ul><li>make up the difference between tuition and the actual cost of educating BU students</li><li>enrich and enhance the experiences of current and future students</li></ul>Consider extending your commitment to keep BU great over the next five years.  A 5-year pledge starts today with your first gift and continues annually.  Each year, you\'ll help to fund the future of BU and earn yourself a tax deduction.  All without having to lift a finger.</p><div style="width:100%;margin:20px 0px;font-size:20px"><label for="gift_amount_extend"><input type="checkbox" name="gift_amount_extend" '+ checked +'id="gift_amount_extend" value="5 year pledge" /><strong>Yes!</strong> Extend my gift into an annual contribution for the next five years.<br /><small>I understand that my credit card will be charged in the same month each year until my pledge is completed.</small></label></div><div class="buttons_fake"><a href="#" rel="3" onclick="$(\'#cover\').hide();onClick(this)">Continue</a><br /><a href="#" onclick="$(\'#cover\').hide()" class="previous">Back</a></div></div>');
	gift_amount_string += '<div style="clear:both">BU tradition is to make a gift in honor of your class year.</div>' +
		'<div class="donation_levels">' +
			'<div class="donation_level">' +
				'<div class="value">' +
					'<label for="$20.09"><sup>$</sup><span class="year">20</span>.<span class="year">09</span></label>' +
					'<br  />' +
					'<input type="radio" name="gift_amount" value="$20.09" id="20" style="width:100%" />' +
				'</div>' +
				'<div class="description">' +
					'<h2>Participant Level</h2>' +
					'<ul>' +
						'<li><strong>Honor a Mentor</strong> Your Honoree will receive a personal message and small memento.</li>' +
					'</ul>' +
				'</div>' +
			'</div>' +
			'<div class="donation_level">' +
				'<div class="value">' +
					'<label for="$200.90"><sup>$</sup><span class="year">200</span>.<span class="year">9</span>0 </label>' +
					'<br  />' +
					'<input type="radio" name="gift_amount" value="$200.90" id="200" style="width:100%" />' +
				'</div>' +
				'<div class="description">' +
					'<h2>Bay State Society Level</h2>' +
					'<ul>' +
						'<li><strong>President\'s Reception</strong> You will be invited to join the Browns at a reception in their home during Senior Week.</li>' +
						'<li><strong>Honor a Mentor</strong> Your Honoree will receive a personal message and small memento.</li>' +
					'</ul>' +
				'</div>' +
			'</div>' +
			'<div class="donation_level">' +
				'<div class="value">' +
					'<label for="$2009"> <sup>$</sup><span class="year">2009</span>.00</label>' +
					'<br  />' +
					'<input type="radio" name="gift_amount" value="$2009.00" id="2000" style="width:100%" />' +
				'</div>' +
				'<div class="description">' +
					'<h2>Leadership Society Level</h2>' +
					'<ul>' +
						'<li><strong>Annual Fund Leadership Giving Societies</strong> You will have the opportunity to join one of the University\'s <a href="#" target="_blank">AFLGS</a>.</li>' +
						'<li><strong>President\'s Reception</strong> You will be invited to join the Browns at a reception in their home during Senior Week.</li>' +
						'<li><strong>Honor a Mentor</strong> Your Honoree will receive a personal message and small memento.</li>' +
					'</ul>' +
				'</div>' +
			'</div>';
	
	if (selected == 'other') {
		gift_amount_string += '<div class="donation_level" id="other_input">' +
				'<div class="value">' +
					'<input type="text" name="gift_amount_other" size="4" style="font-size:25px;" value="'+ $("input#other_amt:first").val() +'" />' +
					'<br />' +
					'<input type="radio" name="gift_amount" id="other" value="Other " style="width:100%" />' +
				'</div>' +
				'<div class="description">' +
					'<h2>Choose any amount you like</h2>' +
					'<ul>' +
						'<li>Every donation, no matter how large or small is appreciated.  The university depends on generous donors like you to help <a href="why/index.html" target="_new">cover its annual costs</a>.</li>' +
					'</ul>' +
				'</div>' +
			'</div></div><div id="error_box"></div>';
		$("#gift_amount").replaceWith(gift_amount_string);	
	} else {
		gift_amount_string += '<div>If you prefer, you can also <a href="#" id="other_reveal">choose any other amount</a>.</div>' +
			'<div class="donation_level" id="other_input">' +
				'<div class="value">' +
					'<sup>$ </sup><input type="text" name="gift_amount_other" id="other_amt" size="4" style="font-size:25px;width:110px;float:none;clear:none;" />' +
					'<br />' +
					'<input type="radio" name="gift_amount" id="other" value="Other " style="width:100%" />' +
				'</div>' +
				'<div class="description">' +
					'<h2>Choose any amount you like</h2>' +
					'<ul>' +
						'<li>Every donation, no matter how large or small is appreciated.  The university depends on generous donors like you to help <a href="why/index.html" target="_new">cover its annual costs</a>.</li>' +
					'</ul>' +
				'</div>' +
			'</div></div><div id="error_box"></div>';


		$("#gift_amount").replaceWith(gift_amount_string);	
		$('#other_input').hide();
	}
	$('#'+selected).parent().parent().addClass('selected_amt');
	$('#'+selected).attr("checked", "checked");
	
	$('#other_reveal').click(function() {
		$(this).parent().hide();
		$('#other_input').show();
	});
	
	$('.donation_level').click(function() {
		$('.donation_level, .selected_amt').removeClass('selected_amt');
		$(this).addClass('selected_amt');
		$('input:radio', this).attr("checked", "checked");
	});



	
	//HONOREE SECTION - PAGE 3
	$('#page3').after('<div class="honoree" style="clear:left;">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.</div>');

	//PERSONAL INFO SECTION - PAGE 4
	$("#payment_info").addClass('float');
	
	//SUBMIT CONFIRMATION
	$('input:submit').remove();
	$('#comment').css({'margin-right':'30px'});
	$('#feedback').css({'float' : 'left', 'clear' : 'none'});
	$('fieldset#submit').prepend('<h1>Confirm and Submit</h1><div id="confirmation"></div>');

});
