
var totalDonationAmount  = 0.0;
var donationAmounts = [];
var donationCategories = [];

var donationCountry = "";
var donationFirstName = "";
var donationLastName = "";
var donationAddress1 = "";
var donationAddress2 = "";
var donationCity = "";
var donationState = "";
var donationZip = "";
var donationEmail = "";
var donationPhonePartA = "";
var donationPhonePartB = "";
var donationPhonePartC = "";

function UpdateDonationUser() {
	donationCountry = document.getElementById('donationCountryValue').value;
	donationFirstName = document.getElementById('donationFirstNameValue').value;
	donationLastName = document.getElementById('donationLastNameValue').value;
	donationAddress1 = document.getElementById('donationAddress1Value').value;
	donationAddress2 = document.getElementById('donationAddress2Value').value;
	donationCity = document.getElementById('donationCityValue').value;
	donationState = document.getElementById('donationStateValue').value;
	donationZip = document.getElementById('donationZipValue').value;
	donationEmail = document.getElementById('donationConfirmationEmailValue').value;
	donationPhonePartA = document.getElementById('donationPhoneaValue').value;
	donationPhonePartB = document.getElementById('donationPhonebValue').value;
	donationPhonePartC = document.getElementById('donationPhonecValue').value;
}


function CloseOtherPanels(currentPanel, totalPanels) {
	var i = 1;
	for(i = 1; i <= maxPanel; i++ ) {
		if( i != currentPanel )
			if( document.getElementById('CollapsiblePanel'+i) != null )
				eval("if( CollapsiblePanel"+i+".contentIsOpen ) { CollapsiblePanel"+i+".close(); }");
	}
}


function UpdateDonations() {
	
	var inputs, i;
	var fundNames = "<br/><table class='donationFloatingSummary' width='100%'>";
	totalDonationAmount = 0.0;
	
	inputs = document.getElementById('donateForm').elements;
	
	for (  var i = 0; i < inputs.length; i++ )
	{
		if ( inputs[i].name.substr(0,9) == 'donation-' ) {
			if( inputs[i].value != "" && inputs[i].value != null && isFinite(parseFloat(inputs[i].value)) ) {
				var bits = inputs[i].name.split('-');
				if( donationAmounts[bits[2]] == "" || donationAmounts[bits[2]] == null || !isFinite(parseFloat(donationAmounts[bits[2]])) ) {
					var objID = 'donation-'+bits[1]+'-'+bits[2];
					document.getElementById(objID).value = '';
				}
			}
		}
	} 
	
	for (  var i = 0; i < donationAmounts.length; i++ )
	{
		if( donationAmounts[i] != "" && donationAmounts[i] != null && isFinite(parseFloat(donationAmounts[i]))  ) {
			totalDonationAmount += parseFloat(donationAmounts[i]);
			var fundName = "<tr><td align='right' valign='top'><b>$"+ parseFloat(donationAmounts[i]).toFixed(2)+"</b></td>";
			fundName += "<td><b><a href='#"+i+"' onclick='eval(\"CollapsiblePanel"+donationCategories[i]+".open();\")'>" + document.getElementById('fundName-'+i).innerHTML + "</a></b> <a class='removeDonation' href='javascript:Do()' onclick='RemoveDonation("+donationCategories[i]+","+i+")'>(remove)</a></td>";
			//+ parseFloat(donationAmounts[i]).toFixed(2) + " "
			//<img style='cursor:pointer' align='absmiddle' src='./images/cross.png' title='Remove Donation of $" + parseFloat(donationAmounts[i]).toFixed(2) + "' onclick='RemoveDonation("+donationCategories[i]+","+i+")' />&nbsp;&nbsp;
			fundNames += fundName;
		}

	} 
	
	if( totalDonationAmount == 0.0 )
		fundNames += "<tr><td>Enter donations by expanding the fund boxes to the right.</td></tr>";
		
	fundNames += "</table>";
		
	document.getElementById('totalDonationAmount').innerHTML = "Total Donation $" + totalDonationAmount.toFixed(2);
	document.getElementById('donationFundSummary').innerHTML = fundNames;
}

function AddDonation(cat, fund) {
	var objID = 'donation-'+cat+'-'+fund;
	donationCategories[fund] = cat;
	donationAmounts[fund] = document.getElementById(objID).value;
	UpdateDonations();
}

function RemoveDonation(cat, fund) {
	var objID = 'donation-'+cat+'-'+fund;
	document.getElementById(objID).value = '';
	donationAmounts[fund] = "";
	UpdateDonations();
}

function NumberOnly(evt) {
	
	/*if( !isFinite(obj.value) ) {
		var value = obj.value;
		
		obj.value = "";
		return false;
	}*/
	
	var charCode = (evt.which) ? evt.which : event.keyCode
	 if (charCode > 31 && (charCode < 48 || charCode > 57))
			return false;
	
	 return true;

	
	//UpdateDonations();
}

function OnEnter(evt, cat, fund) {
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if( charCode === 13 ) 
		AddDonation(cat, fund);
}


function GoConfirm() {
	if( totalDonationAmount >= 1.0 ) {
		UpdateDonations(); 
		document.donateForm.submit();
	}
}


function EditAmount(cat, fund) {
	document.getElementById('updateButtonDiv').style.display = 'inline';
	document.getElementById('donateButtonDiv').innerHTML = '';
	document.getElementById('update-'+fund).innerHTML = '';
	document.getElementById('paypalTransferText').innerHTML = '';
	document.getElementById('paypalTransferText').style.display = 'none';
	document.getElementById('paybyCCdiv').style.display = 'none';
	document.getElementById('amount-'+fund).innerHTML = '';
	document.getElementById('editamount-'+cat+'-'+fund).style.display = '';
}

function EditTribute(fund) {
	document.getElementById('updateButtonDiv').style.display = 'inline';
	document.getElementById('donateButtonDiv').innerHTML = '';
	document.getElementById('paypalTransferText').innerHTML = '';
	document.getElementById('paypalTransferText').style.display = 'none';
	document.getElementById('paybyCCdiv').style.display = 'none';
	document.getElementById('tributelink-'+fund).style.display = 'none';
	//document.getElementById('canceltributelink-'+fund).style.display = '';
	document.getElementById('tributetextdiv-'+fund).style.display = '';
}

function EditFullTribute() {
	Hide('fullDonationTributeLink');
	Show('fullDonationTribute'); 	
	Hide('fullDonationTributeDiv');
	document.getElementById('updateButtonDiv').style.display = 'inline';
	document.getElementById('donateButtonDiv').innerHTML = '';
	document.getElementById('paypalTransferText').innerHTML = '';
	document.getElementById('paypalTransferText').style.display = 'none';
	document.getElementById('paybyCCdiv').style.display = 'none';
}


function RemoveAmount(cat, fund) {
	document.getElementById('txteditamount-'+cat+'-'+fund).value = '';
	document.updateForm.submit();
}



function UpdateDonationsOld() {
	
	var inputs, i;
	var fundNames = "<u>Funds Selected</u><br/><br/>";
	totalDonationAmount = 0.0;
	
	inputs = document.getElementById('donateForm').elements;
	
	for (  var i = 0; i < inputs.length; i++ )
	{
		if ( inputs[i].name.substr(0,9) == 'donation-' ) {
			if( inputs[i].value != "" && inputs[i].value != null && isFinite(parseFloat(inputs[i].value))  ) {
				var bits = inputs[i].name.split('-');
				totalDonationAmount += parseFloat(inputs[i].value);
				var fundName = "<div><img style='cursor:pointer' align='absmiddle' src='./images/cross.png' title='Remove Donation' onclick='RemoveDonation("+bits[1]+","+bits[2]+")' />&nbsp;&nbsp;" + document.getElementById('fundName-'+bits[2]).innerHTML + "</div>";
				fundNames += fundName;
			}
		}
	} 
	
	if( totalDonationAmount == 0.0 )
		fundNames += "None";
		
	document.getElementById('totalDonationAmount').innerHTML = "Total Donation $" + totalDonationAmount.toFixed(2);
	document.getElementById('donationFundSummary').innerHTML = fundNames;
}


function Show(id) {
	document.getElementById(id).style.display = '';
}

function Hide(id) {
	document.getElementById(id).style.display = 'none';
}

function Set(id, value) {
	document.getElementById(id).value = '';
}

function Do(){
	
}


function ValidateDonationPaymentInfoForm() {
	var errors = 0;
	
	
	donationPhonePartA = trim(donationPhonePartA);
	donationPhonePartB = trim(donationPhonePartB);
	donationPhonePartC = trim(donationPhonePartC);
	pattern = /^[\d]+$/ 
	if( donationPhonePartA.length != 3 || donationPhonePartB.length != 3  || donationPhonePartC.length != 4 || !pattern.test(donationPhonePartA) || !pattern.test(donationPhonePartB) || !pattern.test(donationPhonePartC)  ) {
		errors++;
		$('#donationPhoneValueError').html('Invalid Phone Number');
		$('#donationPhoneaValue').focus();
	} else {
		$('#donationPhoneValueError').html('');
	}
	
	
	val = trim(donationZip);  // value of field with whitespace trimmed off
  pattern = /^[\d]{5}$/  ;
  if (!pattern.test(val)) {
		errors++;
		$('#donationZipValueError').html('Invalid Zip Code');
		$('#donationZipValue').focus();
	} else {
		$('#donationZipValueError').html('');
	}
	
	
	val = trim(donationState);  // value of field with whitespace trimmed off
  pattern = /^[a-zA-Z\s]+$/  ;
  if (!pattern.test(val)) {
		errors++;
		$('#donationStateValueError').html('Invalid State');
		$('#donationStateValue').focus();
	} else {
		$('#donationStateValueError').html('');
	}

	
	val = trim(donationCity);  // value of field with whitespace trimmed off
  pattern = /^[a-zA-Z\s\']+$/  ;
  if (!pattern.test(val)) {
		errors++;
		$('#donationCityValueError').html('Invalid City');
		$('#donationCityValue').focus();
	} else {
		$('#donationCityValueError').html('');
	}
	


  if (trim(donationAddress1) == '') {
		errors++;
		$('#donationAddressValueError').html('Invalid Address');
		$('#donationAddress1Value').focus();
	} else {
		$('#donationAddressValueError').html('');
	}

	
	val = trim(donationLastName);  // value of field with whitespace trimmed off
  pattern = /^[a-zA-Z\s\']+$/  ;
  if (!pattern.test(val)) {
		errors++;
		$('#donationLastNameValueError').html('Invalid Last Name');
		$('#donationLastNameValue').focus();
	} else {
		$('#donationLastNameValueError').html('');
	}
	
	
	var val = trim(donationFirstName);  // value of field with whitespace trimmed off
  var pattern = /^[a-zA-Z\s\']+$/  ;
  if (!pattern.test(val)) {
		errors++;
		$('#donationFirstNameValueError').html('Invalid First Name');
		$('#donationFirstNameValue').focus();
	} else {
		$('#donationFirstNameValueError').html('');
	}
	
	
	var val = trim(donationEmail);  // value of field with whitespace trimmed off
  var pattern = /^[\w\-\_\.\+]+\@([\w\.])+\.[\w]+$/  ;
  if (!pattern.test(val)) {
		errors++;
		$('#donationConfirmationEmailValueError').html('Invalid E-mail Addres');
		$('#donationConfirmationEmailValue').focus();
	} else {
		$('#donationConfirmationEmailValueError').html('');
	}
	
	
	
	if( errors > 0 ) {
		return false;
	}
	
	return true;
	
}


function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}


function ClearDefault( id, defaultText ) {
	if( $('#'+id).val() == defaultText ) {
		$('#'+id).val('');
	}
}

