// JavaScript Document
//check a text feild for content
function checkfeild(strng, stmnt){
	if(strng == ""){
			return stmnt;
	}else{
			return "";
	}
}

//check email feild
function checkemail(strng, xVar){
var stmnt = "";
var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) { 
		   stmnt = xVar+" email address is not valid.\n";
	}
	
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) {
	   stmnt += xVar+" email address contains illegal characters.\n";	
	}
	
	if(stmnt != ""){
		return stmnt;
	}else{
			return "";
	}
}

//check phon number
function checkPhone(strng, location){
	var stmnt = "";
	var stripped = strng.replace(/[\(\)\.\-\ a-z A-Z]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
	   stmnt += "The "+location+" phone number contains illegal characters.\n";
	}
	
	if (!(stripped.length == 10)) {
	stmnt += "The "+location+" phone number is the wrong length. Make sure you included an area code.\n";
	}
	
		if(stmnt != ""){
		return stmnt;
		}else{
		return "";
		}
	
}


//check radio buttons
function checkradio(path_radioname, stmnt){
	
	function checkRadio(checkvalue) {
	var errorr = "";
		if (!(checkvalue)) {
		   errorr = stmnt+"\n";
		}
	return errorr;    
	}
	
for (i=0, n=path_radioname.length; i<n; i++) {
	  if (path_radioname[i].checked) {
		 var checkvalue = path_radioname[i].value;
		 break;
	  }
}	
return checkRadio(checkvalue);
}

function IsNumeric(sText, stmnt){

	if(isNaN(sText)){
		return stmnt;
	}else{
		return '';
	}
   
}


//########### Validate Form
function fValidate(){	
	
	var error = "";
	var path = document.sagefrm;	

//###############################################################################################################################
error += checkfeild(path.name.value, "Please enter your first name.\n"); 						//first name
//error += checkfeild(path.comporg.value, "Please enter your Comapny / Orgonization.\n"); 		//Company / Org
error += checkfeild(path.address.value, "Please enter your address.\n"); 						//Address
error += checkPhone(path.tel.value, ""); 														//Tel
error += checkemail(path.email.value, "Your"); 													//email

error += checkradio(path.tax, "Please select wether you wish to recieve a tax reciept."); 		//Question 1support
error += checkradio(path.support, "Please select wether you wish to be a supporter."); 			//Question 1
error += checkfeild(path.amount.value, "Please enter an amount to donate.\n"); 					//Address
error += IsNumeric(path.amount.value, "Donation amount must be numeric.\n");

error += checkfeild(path.challenge_response.value, "Please answer the Anti-Spam question.\n"); 						//Anti Spam

//###############################################################################################################################

	if(error != ""){
		alert(error);
	}else{
		//path.submit();
		//alert('worked');
		path.method = 'post';
		path.action = 'email/donate.email.php';
		path.submit();
	}	
	
}

