function checkForm() {
	if (getSelectedRadioValue(document.donate.amount1) == "" && document.donate.amount2.value == "") {
		alert("Please select an amount or enter and alternate amount in the box provided.");
		return false;
	}
	if (document.donate.amount2.value != "") {
		document.donate.amount2.value = replaceSubstring(document.donate.amount2.value, ",", "");
		if (isNaN(document.donate.amount2.value)) {
			alert("Please enter a valid number.");
			document.donate.amount2.focus();
			document.donate.amount2.select();
			return false;
		}
		if (document.donate.amount2.value.indexOf(".")!=-1) {
			var tmp = document.donate.amount2.value.split(".");
			if (tmp.length > 2) {
				alert("Please enter a valid number."); // for good measure.
				document.donate.amount2.focus();
				document.donate.amount2.select();
				return false;
			}
			if (tmp[1].length > 2) {
				alert("Please round your donation to two decimal places.");
				document.donate.amount2.focus();
				document.donate.amount2.select();
				return false;
			}
		}
		document.donate.amount.value=document.donate.amount2.value;
	} else {
		document.donate.amount.value=getSelectedRadioValue(document.donate.amount1);
	}
};

function clearTextBox() {
	document.donate.amount2.value = "";
};

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
};

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
};

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));

         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
};

function AvisAJAX(URL, METHOD, PROCESSDATA) {
	var that = this;
	this.request = null;
	this.url = URL || null;
	this.method = METHOD.toUpperCase() || 'GET';
	this.async = true;
	this.args = [];
	this.format = "TEXT";
	this.mimeType = null;
	this.processData = PROCESSDATA || function () {};

	this.setMimeType = function() {
		if (that.mimeType) {
			try {
				that.request.overrideMimeType(that.mimeType);
			} catch (e) {
				//alert('Caught Exception: ' + e.description);
				alert('An unknown error has occurred: ' + e.description);
			}
		}
	};

	this.init = function() {
		if(!this.request) {
			try {
				// Not Internet Explorer, e.g. Firefox, Opera 8.0+, Safari
				this.request = new XMLHttpRequest();
			} catch (e) {
				// Internet Explorer
				try {
					this.request = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					this.request = new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
		}
		return this.request;
	};

	this.doRequest = function(loadingFunction) {
		this.loadingFunction = loadingFunction || function () {};
		this.loadingFunction();
		if (this.request == null) {
			alert ("AJAX not supported or class not initialized using init().");
			return;
		}
		var params = "";
		for (i=0;i<=this.args.length-1;i++) {
			sep = this.args[i].split("=");
			params += sep[0] + "=" + sep[1];
			if (i<this.args.length-1) params += "&";
		}
		if (this.method.toUpperCase() == "POST") {
			this.request.open(this.method, this.url, this.async);
			this.setMimeType();
			this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.request.setRequestHeader("Content-length", params.length);	
			this.request.setRequestHeader("Connection", "close");
			this.request.send(params);
		} else {
			this.request.open(this.method, this.url+"?"+params, this.async);
			this.setMimeType();
			//this.request.setRequestHeader("Cache-Control", "no-cache");
			this.request.send(null);
		}
		this.request.onreadystatechange = function() {
			try {
				if (that.request.readyState == 4) {
					if (that.request.status >= 200 && that.request.status <= 299) {
						switch (that.format.toUpperCase()) {
							case "TEXT":
								response = that.request.responseText;
								break;
							case "XML":
								response = that.request.responseXML;
								break;
							default:
								response = request;
							break;
						}
						that.processData(response);
					} else {
						alert("There was a problem with your request. Please contact\nThe Norensberg Foundation using our Contact form.");
					}
				}
			} catch(e) {
				//alert('Caught Exception: ' + e.description);
				alert('An unknown error has occurred: ' + e.description);
			}
		};
	};

	// haven't found a place for this one yet although
	// I am sure it will come in handy one day.
	this.abort = function() {
		if (this.request) {
			this.request.onreadystatechange = function() { };
			this.request.abort();
			this.request = null;
		}
	};
};
