$(document).ready(function(){
	$("#bookwrap").css("min-height",$(window).height());

    if ( !! !document.createElement('canvas').getContext) {
        return;
    }
    var canvas = document.createElement("canvas");
    canvas.width = 120;
    canvas.height = 120;
    var ctx = canvas.getContext("2d");
    var x, y;
    for (x = 0; x < canvas.width; x += 1) {
        for (y = 0; y < canvas.height; y += 1) {
			var c = Math.floor(Math.random() * 30);
			ctx.fillStyle = "rgba(" + c + "," + c + "," + c + ",0.20)";
            ctx.fillRect(x, y, 1, 1);
        }
    }
    jQuery('body').css({
        'background-image': "url(" + canvas.toDataURL("image/png") + ")"
    });
    
    var active_panel = 1;
     
	$("a[id^='lnk']").click(function(){
		var new_panel = this.id.substr(3);
		$(".panel"+active_panel).fadeOut("slow",function(){
			active_panel = new_panel;
			$(".panel"+active_panel).fadeIn("slow");
		});
	});

	$(".damt").click(function(){
		$("#other_amount").val("");
	});

	$("#other_amount").click(function(){
		$("[name=amt_sel]").removeAttr("checked");
		$("[name=amt_sel]").filter("[value=other]").attr("checked","checked");
	});

	$("#donatebutton").click(function(){

		//alert("CLICKED");
		var error_message = "";

		if(($.trim(document.payPalForm.firstname.value) == "") || ($.trim(document.payPalForm.lastname.value) == ""))
		{
			error_message = "Please enter your name to continue with your donation.";
		}
		else if(($.trim(document.payPalForm.employer.value) == "") || ($.trim(document.payPalForm.occupation.value) == ""))
		{
			error_message = "Federal law requires us to use our best efforts to collect and report donor informating including the name, occupation, and employer of individuals whose contributions exceed $200 in an election cycle.<br/><br/>Please provide this information to continue with your donation."
		}
		else if($.trim(document.payPalForm.phone.value) == "")
		{
			error_message = "Please specify a phone number.";
		}
		else if($.trim(document.payPalForm.email.value) == "")
		{
			error_message = "Please specify an email address.";		
		}
		else
		{
			if( $('input[name=amt_sel]:radio:checked').length )
			{
				if($('input[name=amt_sel]:radio:checked').val() == "other")
				{
					if($.trim(document.payPalForm.other_amount.value) == "")
					{
						error_message = "Please specify a value for your donation";
					}
					else
					{
						if(!isNaN(parseFloat(document.payPalForm.other_amount.value)) && isFinite(document.payPalForm.other_amount.value))
							document.payPalForm.amount.value = document.payPalForm.other_amount.value;
					}
				}
				else
				{
					document.payPalForm.amount.value = $('input[name=amt_sel]:radio:checked').val();
				}
			}
			else
			{
				error_message = "Please specify a value for your donation.";
			}
		}

		if(error_message != "")
		{
			$("#error_panel").html(error_message);
			$("#error_panel").fadeIn('slow');
		}
		else
		{
			document.payPalForm.os0.value = $.trim(document.payPalForm.firstname.value) + " " + $.trim(document.payPalForm.lastname.value) + "|" + $.trim(document.payPalForm.phone.value) + "|" + $.trim(document.payPalForm.email.value);
			document.payPalForm.os1.value = $.trim(document.payPalForm.employer.value) + "|" + $.trim(document.payPalForm.occupation.value);
			
			//"MassiveDesigns_President";
			//"David Cooper"; 
			
			$("#payPalForm").submit();
		}
	});

});
