// JavaScript Document


// Return true if string only contains whitespace characters
function isBlank(s) {
 for(var i=0; i < s.length; i++) {
    var c = s.charAt(i);
    if ((c != " ") && (c != "\n") && (c != "")) return false;
 }
 return true;
}


// Check whether string s is empty.

function isEmpty(s) {
   return ((s == null) || (s.length == 0))
}


// Returns true if character c is a digit 
// (0 .. 9) or is a decimal

function isDigit (s) {
 for(var i=0; i < s.length; i++) {
    var c = s.charAt(i);
    if ((c >= "0") && (c <= "9")) return false;
 }
 return true;

}




// form verification
function buildURL(f) {
  var msg;
  var empty_fields = "";
  var errors = "";
  var amount;
  var descr;
  

  // Check any text element that has a "required" field defined.
  // If field is empty, make a list.
  for(var i=0; i < f.length; i++) {
    var e = f.elements[i];
	// Check if field is empty
	 if (e.name == "thisAmount") {
	 	if (!isDigit(e.value) && (isEmpty(e.value) || isBlank(e.value))) {
			var spaceName = e.name;
			spaceName = spaceName.replace(/_/g, " ");
			spaceName = spaceName.toLowerCase();
			errors += "\n     Amount should only contain numbers or a decimal (ex: 45.00).";
			continue;
		 } else {
			amount = e.value; 
		 }
	}
	 if (e.name == "thisDescr") {
	 	if (isEmpty(e.value) || isBlank(e.value)) {
			var spaceName = e.name;
			spaceName = spaceName.replace(/_/g, " ");
			spaceName = spaceName.toLowerCase();
			errors += "\n     Please provide a description.";
			continue;
		 } else {
			 descr = e.value;
			descr = descr.replace(/ /g, "+");
		 }
	 }
  }


  // If there are errors, display error message and return false
  // to prevent the form from being submitted.
  // If there are NO errors, return true.

  if (empty_fields || errors) {

  msg  = "_________________________________________________\n\n"
  msg += "The form was not submitted because of the following error(s).\n";
  msg += "Please correct the error(s) and re-submit.\n";
  msg += "_________________________________________________\n\n";

  if (empty_fields) {
    msg += "- The following required field(s) are empty:"
	   + empty_fields + "\n";
    if (errors) msg += "\n";
  }
  msg += errors;
  alert(msg);
 } else {

	var url = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=aspooner@cox.net&amount=";
	url += amount;
	url += "&item_name=";
	url += descr;
	url += "&currency_code=USD&no_shipping=1&pbtype=general&bn=paywiz4mso-001.000&mrb=R-4VT64848FL959270J";
    // Submit Form
    window.open(url);
 }

}
