function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}
//special a
function uncheck(theForm){
	for (var cb=0; cb<10; cb++)  {
		document.donate.amount[cb].checked = false;
	}
}

//check to make sure that an amount is selected
function additup(theForm){
	//first make sure that one of the two is set
	var found_it //initial value is null because we gave it no other value
	//Check the radio buttons
	for (var i=0; i<theForm.amount.length; i++)  {
    	if (theForm.amount[i].checked)  {
    		found_it = theForm.amount[i].value //set found_it equal to checked button's value
    	}
	}
	//Now check to see if either is checked
	
	//radio button is set	
	if (found_it == null && theForm.amountOther.value == "") { 
		alert("Please select an amount to donate.");
		return true;	
	}else{
		//which is it?
		if(found_it != null){
			theForm.quantity.value = found_it;
		}else{
			theForm.quantity.value = theForm.amountOther.value;
		}
		
	}
	
	//set the shipping field values
	theForm.shipping_first.value = theForm.billing_first.value;
	theForm.shipping_last.value = theForm.billing_last.value;
	theForm.shipping_address.value = theForm.billing_address.value;
	theForm.shipping_city.value = theForm.billing_city.value;
	theForm.shipping_state.value = theForm.billing_state.value;
	theForm.shipping_zip.value = theForm.billing_zip.value;
	//theForm.ship_eq_billing.value = checked;
}

function validateForm(theForm)
{
	
	
	
	// Customize these calls for your form
	// Add up the variables for the hidden cart value
	
	if(additup(theForm))
		return false;
		
	// Start ------->
	if (!validRequired(theForm.billing_first,"First Name"))
		return false;
	if (!validRequired(theForm.billing_last,"Last Name"))
		return false;
	if (!validRequired(theForm.billing_address,"Address"))
		return false;	
	if (!validRequired(theForm.billing_city,"City"))
		return false;
	/*if (!validRequired(theForm.billing_state,"State"))
		return false;*/
	if (!validRequired(theForm.billing_zip,"Postal Code"))
		return false;
	//check email address
	if (!validEmail(theForm.email,"Email Address",true))
		return false;
	if (!validRequired(theForm.phone,"Phone"))
		return false;

	
	//if (!validDate(theForm.available,"Date Available",true))
		//return false;
	
	if (!validNum(theForm.amountOther,"Other Amount",false))
		return false;
	
	return true;
}