//Form validation script
//By: Aaron LaMontagne
//Date: 03-30-05
//File Name: validation.js

//THESE FUNCTIONS BELOW ARE THE FUNCTIONS FOR CHECKING AND SUBMITTING THE FORMS
//DO NOT REMOVE
function agreeToTerms(){
	var form = document.form;
	
	if (form.agree.checked == false){
		alert("You must agree to the above Terms.");
		form.agree.focus()
		return false;
	}
		return checkForm();
}//ends Function agreeToTerms()


function checkForm(){
var form = document.form;
var ck = false;
	if ((form.email.value==null)||(form.email.value=="")){
		alert("Please enter an 'Email Address'")
		form.email.focus()
		return false
	}
	if (echeck(form.email.value)==false){
		form.email.value=""
		form.email.focus()
		return false
	} else {
		ck = true;
	}//ends check
	
	if (ck == true){
	  sendForm();
	}//ends if
}//ends checkForm()


function checkDonationForm(){
	var form = document.form;
	var radio = false
	var ck = false; 
	
//Check for donations first
if(form.annual_appeal_ckb.checked && form.annual_appeal_text.value == ""){
		 alert("Please enter an amount for your Annual Appeal donation.")
		form.annual_appeal_text.focus()
		return false
	}
	
	if(form.capital_campaign_ckb.checked && form.capital_campaign_text.value == ""){
		 alert("Please enter an amount for your Capital Campaign donation.")
		form.capital_campaign_text.focus()
		return false
	}

	if(form.memorials_honorings_ckb.checked && form.memorials_honorings_text.value == ""){
		 alert("Please enter an amount for your Memorial & Honorings donation.")
		form.memorials_honorings_text.focus()
		return false
	}

	if(form.home_for_the_holidays_ckb.checked && form.home_for_the_holidays_text.value == ""){
		 alert("Please enter an amount for your Home for the Holidays donation.")
		form.home_for_the_holidays_text.focus()
		return false
	}

	if(form.wishes_ckb.checked && form.wishes_text.value == ""){
		 alert("Please enter an amount for your Wishes donation.")
		form.wishes_text.focus()
		return false
	}

	if(form.special_events_ckb.checked && form.special_events_text.value == ""){
		 alert("Please enter an amount for your Special Events donation.")
		form.special_events_text.focus()
		return false
	}

	
	//check contact fields
	if(form.FirstName.value == ""){
		alert("Please enter a 'First Name'");
		form.FirstName.focus()
		return false;
	}//ends if

	if(form.LastName.value == ""){
		alert("Please enter a 'Last Name'");
		form.LastName.focus()
		return false;
	}//ends if

	if(form.Email.value == ""){
		alert("Please enter an 'Email Address'");
		form.Email.focus()
		return false;
	}//ends if

	if (echeck(form.Email.value)==false){
		form.Email.value=""
		form.Email.focus()
		return false
	}

	if(form.Address1.value == ""){
		alert("Please enter an 'Address1'");
		form.Address1.focus()
		return false;
	}//ends if

	if(form.City.value == ""){
		alert("Please enter a 'City'");
		form.City.focus()
		return false;
	}//ends if
	
	if(form.State.value == ""){
		alert("Please enter a 'State'");
		form.State.focus()
		return false;
	}//ends if
	
	if(form.Zip.value == ""){
		alert("Please enter a 'Zip Code'");
		form.Zip.focus()
		return false;
	}//ends if
	
	if(form.Phone.value == ""){
		alert("Please enter a 'Phone Number'");
		form.Phone.focus()
		return false;
	}//ends if

	if(form.CCNumber.value == ""){
		alert("Please enter a 'Credit Card Number'");
		form.CCNumber.focus()
		return false;
	}//ends if
	

	if(form.CCExpiration.value == ""){
		alert("Please enter a 'Credit Card Expiration Date'");
		form.CCExpiration.focus()
		return false;
	} 
	
	if(form.BillingAddress1.value == ""){
		alert("Please enter a 'Billing Address'");
		form.BillingAddress1.focus()
		return false;
	}//ends if

	if(form.BillingCity.value == ""){
		alert("Please enter a 'Billing Address City'");
		form.BillingCity.focus()
		return false;
	}//ends if

	if(form.BillingState.value == ""){
		alert("Please enter a 'Billing Address State'");
		form.BillingState.focus()
		return false;
	}//ends if

	if(form.BillingZip.value == ""){
		alert("Please enter a 'Billing Address Zip'");
		form.BillingZip.focus()
		return false;
	} else {
		ck = true;
	}//ends check

	if (ck == true){
	  sendForm();
	}//ends if
}//ends checkform

function setCheckBox(){
 var form = document.form;
	
}//ends setCheckBox()



//Start functions for Same Bill address
function billAddress(box){
var form = document.form;
	if(box.checked == true){
		form.BillingAddress1.value = form.Address1.value;
		form.BillingAddress2.value = form.Address2.value;
		form.BillingCity.value = form.City.value;
		form.BillingState.value = form.State.value;
		form.BillingZip.value = form.Zip.value;
	} else {
		form.BillingAddress1.value = ""
		form.BillingAddress2.value = ""
		form.BillingCity.value = ""
		form.BillingState.value = ""
		form.BillingZip.value = ""
	}//ends if
}//ends function billAddress()






//This function is used in form validation scripts to check email addresses
//DO NOT REMOVE!!!!!
function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }
 		 return true					
	}//ends echeck


function CurrencyFormatted(amount){
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;	
}// ends the format function

function calculateDonation(){
var amount, total
var form = document.form;

amount1 = CurrencyFormatted(form.annual_appeal_text.value) 
amount2 = CurrencyFormatted(form.capital_campaign_text.value)
amount3 = CurrencyFormatted(form.memorials_honorings_text.value)
amount4 = CurrencyFormatted(form.home_for_the_holidays_text.value) 
amount5 = CurrencyFormatted(form.wishes_text.value)
amount6 = CurrencyFormatted(form.special_events_text.value)
amount = parseFloat(amount1) + parseFloat(amount2) + parseFloat(amount3)  + parseFloat(amount4) + parseFloat(amount5) + parseFloat(amount6)
amount = CurrencyFormatted(amount);
form.Donation.value = amount
}
