function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function donationCheckAndSubmit(target) {
if (document.spForm.gDonation.checked==false && document.spForm.ssDonation.checked==false && document.spForm.tlDonation.checked==false && document.spForm.brDonation.checked==false) {
	alert("Please Choose a Donation.");
	document.spForm.gDonation.focus();
	return false;
}
else if (trim(document.spForm.rlName.value)=="") {
	alert("Please enter Last Name.");
	document.spForm.rlName.focus();
	return false;
}
else if (trim(document.spForm.rfName.value)=="") {
	alert("Please enter First Name.");
	document.spForm.rfName.focus();
	return false;
}
else if (trim(document.spForm.rcAddress.value)=="") {
	alert("Please enter Address.");
	document.spForm.rcAddress.focus();
	return false;
}
else if (trim(document.spForm.rcity.value)=="") {
	alert("Please enter City.");
	document.spForm.rcity.focus();
	return false;
}
else if (trim(document.spForm.rstate.value)=="") {
	alert("Please enter State.");
	document.spForm.rstate.focus();
	return false;
}
else if (trim(document.spForm.rzip.value)=="") {
	alert("Please enter Zip code.");
	document.spForm.rzip.focus();
	return false;
}
else if (trim(document.spForm.remail.value)=="") {
	alert("Please enter valid EMail.");
	document.spForm.remail.focus();
	return false;
}
else if (trim(document.spForm.rcPhoneNum.value)=="") {
	alert("Please enter Cell Phone Number.");
	document.spForm.rcPhoneNum.focus();
	return false;
}
else
{
   var itemText = "";
   var amountTotal = 0;
   if(document.spForm.gDonation.checked==true)
   {
      if (trim(document.spForm.gAmount.value)=="")
      {
        alert("Please enter Amount for General Donation.");
        document.spForm.gAmount.focus();
        return false;
      }
      itemText = itemText.concat(document.spForm.gDonation.value).concat(" - $").concat(document.spForm.gAmount.value).concat(" ");
      amountTotal = parseInt(amountTotal,10) + parseInt(document.spForm.gAmount.value,10);
   }
   if(document.spForm.ssDonation.checked==true)
   {
      itemText = itemText.concat(document.spForm.ssDonation.value).concat(" - $2500").concat(" ");
      amountTotal = parseInt(amountTotal,10) + 2500;
   }
   
   if(document.spForm.tlDonation.checked==true)
   {
      if (trim(document.spForm.tlAmount.value)=="")
      {
        alert("Please enter Nos of Units for Tulasi Leaves.");
        document.spForm.tlAmount.focus();
        return false;
      }
      itemText = itemText.concat(document.spForm.tlDonation.value).concat(" - ").concat(document.spForm.tlAmount.value).concat(" ");
      amountTotal = parseInt(amountTotal,10) + 501 * parseInt(document.spForm.tlAmount.value,10);
   }
    
    
   if(document.spForm.brDonation.checked==true)
   {
      var brSelections = document.spForm.brSelections.value;
      if (brSelections== 'Carpet - $600') {
		    amountTotal = parseInt(amountTotal,10) + 600;
	   } else if (brSelections== 'Filing Cabinet Office - $150') {
		    amountTotal = parseInt(amountTotal,10) + 150;
	   } else if (brSelections== 'Office Desks - $250') {
		    amountTotal = parseInt(amountTotal,10) + 250;
	   } else if (brSelections== 'Class Room Boards - $1500') {
		    amountTotal = parseInt(amountTotal,10) + 1500;
	   } else if (brSelections== 'Lawn Mowing Equipment - $2050') {
		    amountTotal = parseInt(amountTotal,10) + 2050;
	   } else if (brSelections== 'Security System Monitoring - $10,000') {
		    amountTotal = parseInt(amountTotal,10) + 10000;
	   } else if (brSelections== 'Audio System - $6000') {
		    amountTotal = parseInt(amountTotal,10) + 6000;
	   } else if (brSelections== 'Shoe Rack & Painting - $1,500') {
		    amountTotal = parseInt(amountTotal,10) + 1500;
	   } else if (brSelections== 'Sri Ayyappa Utsava Idol - $2000') {
		    amountTotal = parseInt(amountTotal,10) + 2000;
	   } else if (brSelections== 'Sri Krishna Utsava Idol with Calf - $2000') {
		    amountTotal = parseInt(amountTotal,10) + 2000;
	   } else if (brSelections== 'Sri Subrahmanya, Sri Vall & Devayani Utsava Idols - $2500') {
		    amountTotal = parseInt(amountTotal,10) + 2500;
	   } else if (brSelections== 'Sri Dattatreya Utsava Idol with cow and dogs - $2500') {
		    amountTotal = parseInt(amountTotal,10) + 2500;
	   } else if (brSelections== 'Sri Saraswati Utsava Idol - $2000') {
		    amountTotal = parseInt(amountTotal,10) + 2000;
	   } else if (brSelections== 'Gomatha Utsava Idol with calf - $2000') {
		    amountTotal = parseInt(amountTotal,10) + 2000;
	   } 
		
     itemText = itemText.concat(brSelections).concat(" ");
   }
    
  
if (target == 0)
{
  document.spForm.action="./donationStatus.php";
  document.spForm.item_name.value =  itemText;
  document.spForm.amount.value = amountTotal;
  document.spForm.submit();
}
else if(target == 1)
{
   document.spForm.action="https://www.paypal.com/cgi-bin/webscr";
   document.spForm.item_name.value =  itemText;
   document.spForm.amount.value = amountTotal;
   document.spForm.first_name.value = document.spForm.rlName.value;
   document.spForm.last_name.value = document.spForm.rfName.value;
   document.spForm.address1.value = document.spForm.rcAddress.value;
   document.spForm.city.value = document.spForm.rcity.value;
   document.spForm.state.value = document.spForm.rstate.value;
   document.spForm.zip.value = document.spForm.rzip.value;
   document.spForm.night_phone_a.value = document.spForm.rcPhoneNum.value;
   document.spForm.email.value = document.spForm.remail.value;
   document.spForm.submit();
}
return true;
}
}

