function validate_form() {
	var error = '';

	if (document.contribution_form.shipping_first_name.value.length<1) { 
		error += "\nFirst name is required."; 
	} 

	if (document.contribution_form.shipping_last_name.value.length<1) { 
		error += "\nLast name is required."; 
	} 

	if (document.contribution_form.shipping_address.value.length<1) { 
		error += "\nAddress is required."; 
	} 

	if (document.contribution_form.shipping_city.value.length<1) { 
		error += "\nCity is required."; 
	} 

	if (document.contribution_form.shipping_state.value.length<1) { 
		error += "\nState is required."; 
	} 

	if (document.contribution_form.shipping_zip.value.length<1) { 
		error += "\nZIP code is required."; 
	} 

	if (document.contribution_form.phone.value.length<1) { 
		error += "\nPhone number is required."; 
	} 

	if (document.contribution_form.email.value.length<1) { 
		error += "\nEmail address is required."; 
	} 

	if (document.contribution_form.employer.value.length<1) { 
		error += "\nEmployer is required."; 
	} 

	if (document.contribution_form.occupation.value.length<1) { 
		error += "\nOccupation is required."; 
	}
	
//	if (document.contribution_form.amount.length < 1 || document.contribution_form.amount.value < 1 || document.contribution_form.amount.value > 500) { 
//		error += "\nPlease specify a donation amount between $1 and $500."; 
	if (document.contribution_form.amount.length < 1 || document.contribution_form.amount.value < 1) { 
		error += "\nDonation amount is required."; 
	} 

	if (document.contribution_form.card_num.value.length<1) { 
		error += "\nCredit card is required."; 
	} 

	if (!checkLuhn10(document.contribution_form.card_num.value)) { 
		error += "\nCredit card number is not valid."; 
	} 

	if (document.contribution_form.card_cvv.value.length<1) { 
		error += "\nCredit card CVV is required."; 
	} 

	if (document.contribution_form.exp_month.value.length<1) { 
		error += "\nCredit card expiration date month is required."; 
	} 

	if (document.contribution_form.exp_year.value.length<1) { 
		error += "\nCredit card expiration date year is required."; 
	} 

	// If not billing to shipping address, validate billing address too
	if(document.contribution_form.bill_to_shipping.checked == false ) {
		copyAddress();
		if (document.contribution_form.first_name.value.length<1) { 
			error += "\nBilling address first name is required."; 
		} 
		if (document.contribution_form.last_name.value.length<1) { 
			error += "\nBilling address last name is required."; 
		} 
		if (document.contribution_form.address.value.length<1) { 
			error += "\nBilling address address is required."; 
		} 
		if (document.contribution_form.city.value.length<1) { 
			error += "\nBilling address city is required."; 
		} 
		if (document.contribution_form.state.value.length<1) { 
			error += "\nBilling address state is required."; 
		} 
		if (document.contribution_form.zip.value.length<1) { 
			error += "\nBilling address ZIP code is required."; 
		} 
	}

	if (document.contribution_form.certification.checked==false) { 
		error += "\nYou must agree to the certifications and mark the checkbox."; 
	} 

	if (error != '') {
		alert("Your transaction could not be completed for the following reasons:\n" + error);
		return false;

	} else { 
		return true;
	} 

} 

var nVisaCardType 				= 0;
var nMastercardCardType 		= 1;
var nDiscoverCardType			= 2;
var nAmexCardType				= 3;
var nDinersClubCardType			= 4;
var nCarteBlancheCardType		= 5;
var nEnRouteCardType			= 6;
var nJCBCardType				= 7;
var nUnknownCardType			= 8;	
var cardPics = new Array();

cardPics[nVisaCardType] = new Image();
cardPics[nVisaCardType].src="/donate/images/cards/visa.jpg";
cardPics[nMastercardCardType] = new Image();
cardPics[nMastercardCardType].src="/donate/images/cards/mastercard.jpg";
cardPics[nDiscoverCardType] = new Image();
cardPics[nDiscoverCardType].src="/donate/images/cards/discover.jpg";
cardPics[nAmexCardType] = new Image();
cardPics[nAmexCardType].src="/donate/images/cards/amex.jpg";
cardPics[nDinersClubCardType] = new Image();
cardPics[nDinersClubCardType].src="/donate/images/cards/dinersclub.jpg";
cardPics[nCarteBlancheCardType] = new Image();
cardPics[nCarteBlancheCardType].src="/donate/images/cards/carteblanche.jpg";
cardPics[nEnRouteCardType] = new Image();
cardPics[nEnRouteCardType].src="/donate/images/cards/enroute.jpg";
cardPics[nJCBCardType] = new Image();
cardPics[nJCBCardType].src="/donate/images/cards/jcb.jpg";	
cardPics[nUnknownCardType] = new Image();
cardPics[nUnknownCardType].src="/donate/images/cards/invalid.gif";

//
// Algorithm to verify a credit number is valid
//
function checkLuhn10(number) {
 if (number.length > 19)
   return (false);

 sum = 0; mul = 1; l = number.length;
 for (i = 0; i < l; i++) {
   digit = number.substring(l-i-1,l-i);
   tproduct = parseInt(digit ,10)*mul;
   if (tproduct >= 10)
     sum += (tproduct % 10) + 1;
   else
     sum += tproduct;
   if (mul == 1)
     mul++;
   else
     mul--;
 }

 if ((sum % 10) == 0)
   return (true);
 else
   return (false);
}

//
// Determine the credit card type from the credit card number
//
function getCardType(number) {
       var numLength = number.length;

       if(numLength > 4)
       {
	       if((number.charAt(0) == '4') && ((numLength == 13)||(numLength==16)))
		       return(cardPics[nVisaCardType].src);
	       else if((number.charAt(0) == '5' && ((number.charAt(1) >= '1') && (number.charAt(1) <= '5'))) && (numLength==16))
		       return(cardPics[nMastercardCardType].src);
	       else if(number.substring(0,4) == "6011" && (numLength==16))
		       return(cardPics[nDiscoverCardType].src);
	       else if((number.charAt(0) == '3' && ((number.charAt(1) == '4') || (number.charAt(1) == '7'))) && (numLength==15))
		       return(cardPics[nAmexCardType].src);
	       else if((number.charAt(0) == '3') && (numLength==16))
		       return(cardPics[nJCBCardType].src);
	       else if(((number.substring(0, 4) == "2131") || (number.substring(0, 4) == "1800")) && (numLength==15))
		       return(cardPics[nJCBCardType].src);
	       else if(((number.substring(0, 4) == "2014") || (number.substring(0, 4) == "2149")) && (numLength==15))
		       return(cardPics[nEnRouteCardType].src);
	       else if((number.charAt(0) == '3') && (number.charAt(1) == '8') && (numLength == 14))
		       return(cardPics[nCarteBlancheCardType].src);
	       else if((number.charAt(0) == '3') && (((number.charAt(1) == '0') && ((number.charAt(2) >= '0') && (number.charAt(2) <= '5'))) 
		       || (number.charAt(1) == '6')) && (numLength == 14))
		       return(cardPics[nDinersClubCardType].src);
   }

   return(cardPics[nUnknownCardType].src);	  
}


function handleCCTyping (field, event) {
       var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

if (field.card_num.value.length >= 13) 
{
if(!checkLuhn10(field.card_num.value))
	       {
		       field.cardimage.src=cardPics[nUnknownCardType].src;
	       }
	       else
	       {
		       field.cardimage.src=getCardType(field.card_num.value);
	       }
       } else {
	       field.cardimage.src=cardPics[nUnknownCardType].src;
       }

       return true;
}

function toggleDisplay(id, disp) {
       if (document.layers) {
	       current = (document.layers[id].display == 'none') ? 'block' : 'none';
	       if (disp != null) {current = disp;}
	       document.layers[id].display = current;
       } else if (document.all) {
	       current = (document.all[id].style.display == 'none') ? 'block' : 'none';
	       if (disp != null) {current = disp;}
	       document.all[id].style.display = current;
       } else if (document.getElementById) {
	       vista = (document.getElementById(id).style.display == 'none') ? 'block' : 'none';
	       if (disp != null) {vista = disp;}
	       document.getElementById(id).style.display = vista;
       }
}


function copyAddress() 
{ 	
       if(document.contribution_form.bill_to_shipping.checked == true ) 
       { 
	       document.contribution_form.first_name.value 	= document.contribution_form.shipping_first_name.value ; 
	       document.contribution_form.last_name.value 	= document.contribution_form.shipping_last_name.value ; 
	       document.contribution_form.address.value 		= document.contribution_form.shipping_address.value ; 
	       document.contribution_form.city.value 			= document.contribution_form.shipping_city.value ; 
	       document.contribution_form.zip.value 			= document.contribution_form.shipping_zip.value ; 
	       document.contribution_form.state.value 		= document.contribution_form.shipping_state.value ; 
	       document.contribution_form.state.selectedIndex  = document.contribution_form.shipping_state.selectedIndex;
	       toggleDisplay('billing_address', 'none');
       } else {
	       toggleDisplay('billing_address', 'block');
       }
}

function get_url_param( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return unescape(results[1]);
}

function year_options() {
  var now = new Date();
  var year = now.getFullYear();
  var future = year + 15;

  do {
    document.write ("<option value=\"" + new String(year).substr(2,2) + "\">" + year + "");
    year++;
  }
  while (year < future)
}

jQuery(document).ready( function () {
	if (get_url_param('shipping_first_name') != '') {jQuery('#shipping_first_name').val(get_url_param('shipping_first_name'));}
	if (get_url_param('shipping_last_name') != '') {jQuery('#shipping_last_name').val(get_url_param('shipping_last_name'));}
	if (get_url_param('shipping_address') != '') {jQuery('#shipping_address').val(get_url_param('shipping_address'));}
	if (get_url_param('shipping_city') != '') {jQuery('#shipping_city').val(get_url_param('shipping_city'));}
	if (get_url_param('shipping_state') != '') {jQuery('#shipping_state').val(get_url_param('shipping_state'));}
	if (get_url_param('shipping_zip') != '') {jQuery('#shipping_zip').val(get_url_param('shipping_zip'));}
	if (get_url_param('email') != '') {jQuery('#email').val(get_url_param('email'));}
	if (get_url_param('phone') != '') {jQuery('#phone').val(get_url_param('phone'));}
	if (get_url_param('employer') != '') {jQuery('#employer').val(get_url_param('employer'));}
	if (get_url_param('occupation') != '') {jQuery('#occupation').val(get_url_param('occupation'));}
	if (get_url_param('amount') != '') {
		jQuery('#amount').val(get_url_param('amount'));
		jQuery('input[name=setamount][value='+get_url_param('amount')+']').attr('checked', true);
	} else {
		jQuery('input[name=setamount][value='+get_url_param('setamount')+']').attr('checked', true);
		jQuery('#amount').val(get_url_param('setamount'));
	}		
	if (get_url_param('card_num') != '') {jQuery('#card_num').val(get_url_param('card_num')).change();}
	if (get_url_param('card_cvv') != '') {jQuery('#card_cvv').val(get_url_param('card_cvv'));}
	if (get_url_param('exp_month') != '') {jQuery('#exp_month').val(get_url_param('exp_month'));}
	if (get_url_param('exp_year') != '') {jQuery('#exp_year').val(get_url_param('exp_year'));}
	if (get_url_param('bill_to_shipping') == "1") { jQuery('#bill_to_shipping').attr('checked', true); jQuery('#billing_address').hide(); } else { jQuery('#bill_to_shipping').attr('checked', false); jQuery('#billing_address').show(); }
	if (get_url_param('first_name') != '') {jQuery('#first_name').val(get_url_param('first_name'));}
	if (get_url_param('last_name') != '') {jQuery('#last_name').val(get_url_param('last_name'));}
	if (get_url_param('address') != '') {jQuery('#address').val(get_url_param('address'));}
	if (get_url_param('city') != '') {jQuery('#city').val(get_url_param('city'));}
	if (get_url_param('state') != '') {jQuery('#state').val(get_url_param('state'));}
	if (get_url_param('zip') != '') {jQuery('#zip').val(get_url_param('zip'));}
	if (get_url_param('certification') != '') { jQuery('#certification').attr('checked', true); } else { jQuery('#certification').attr('checked', false); }
	if (get_url_param('error_message') != '' || get_url_param('reason') != '') { jQuery('#transaction_error').show(); }
});



