// JavaScript Document

//a function to check whether a string matches an empty string or 
//white space characters.  This will basically be a check for required
//fields
function is_empty(str)
{
	var regex = /^(\s*)$/;
	return regex.test(str);
}


//function to tell whether a string is a US zipcode or not
function is_zip_code(str)
{
	var regex = /^(\d{5}(-\d{4})?)$/;
	return regex.test(str);
}


//function to tell whether a string is a US phone number or not
function is_phone_number(str)
{
	var regex = /^(((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4})$/;
	return regex.test(str);
}

//function to tell whether a string is an email address or not
function is_email(str)
{
	var regex = /^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)$/;
	return regex.test(str);
}



function is_expr(str)
{
	var regex = /^\d{4}$/;
	return regex.test(str);
}





function is_double(str)
{
	var regex = /^((\d)+(\.\d{2})?)$/;
	return regex.test(str);
}




function VerifyInfo(stipendForm)
{
		//keep the original class to change it back if the error was fixed
		var originalCss = 'flat-box-red';
		
		//check for the stipend type
		if(document.getElementById("firstname") != null && document.getElementById("lastname") != null && 
			document.getElementById("street") != null && document.getElementById("city") != null &&
			document.getElementById("State") != null && document.getElementById("zip") != null && 
			document.getElementById("Country") != null && document.getElementById("email") != null &&
			document.getElementById("phonenum") != null && document.getElementById("cardNum") != null && 
			document.getElementById("cardExp") != null && document.getElementById("amount") != null)
		{
				var error = "";
				var focusSet = false;
				
				if(is_empty(document.getElementById("firstname").value))
				{
					
					if(!focusSet)
					{
						document.getElementById("firstname").focus();
						focusSet = true;
					}
					error += ". First Name is required\n";
				}
				
				if(is_empty(document.getElementById("lastname").value))
				{
					
					if(!focusSet)
					{
						document.getElementById("lastname").focus();
						focusSet = true;
					}
					error += ". Last Name is required\n";
				}
				
				if(is_empty(document.getElementById("street").value))
				{
					
					if(!focusSet)
					{
						document.getElementById("street").focus();
						focusSet = true;
					}
					error += ". Address is required\n";
				}
				
				if(is_empty(document.getElementById("city").value))
				{
					
					if(!focusSet)
					{
						document.getElementById("city").focus();
						focusSet = true;
					}
					error += ". City is required\n";
				}
				
				if(is_empty(document.getElementById("State").value))
				{
					
					if(!focusSet)
					{
						document.getElementById("State").focus();
						focusSet = true;
					}
					error += ". State is required\n";
				}
				
				if(is_empty(document.getElementById("zip").value))
				{
					
					if(!focusSet)
					{
						document.getElementById("zip").focus();
						focusSet = true;
					}
					error += ". Zip/Postal/Code is required\n";
				}
				
				if(is_empty(document.getElementById("Country").value))
				{
					
					if(!focusSet)
					{
						document.getElementById("Country").focus();
						focusSet = true;
					}
					error += ". Country is required\n";
				}
				
				
				if(is_empty(document.getElementById("email").value))
				{
					if(!focusSet)
					{
						document.getElementById("email").focus();
						focusSet = true;
					}
					error += ". Email is required\n";
				}
				else if(!is_email(document.getElementById("email").value))
				{					
					if(!focusSet)
					{
						//if the focus hasn't been set then set it now
						document.getElementById("email").focus();
						focusSet = true;
					}
					error += ". A valid email address is required\n";
				}
				
				// removing phoneum required - Alex Adao
				// if(is_empty(document.getElementById("phonenum").value))
				// {
				//	if(!focusSet)
				//	{
				//		document.getElementById("phonenum").focus();
				//		focusSet = true;
				//	}
				//	error += ". Your phone number  is required\n";
				// }
				// else if(!is_phone_number(document.getElementById("phonenum").value))
				// {					
				//	if(!focusSet)
				//	{
				//		//if the focus hasn't been set then set it now
				//		document.getElementById("phonenum").focus();
				//		focusSet = true;
				//	}
				//	error += ". A valid Phone Number is required\n";
				// }
				
				
				if(is_empty(document.getElementById("cardNum").value))
				{
					if(!focusSet)
					{
						document.getElementById("cardNum").focus();
						focusSet = true;
					}
					error += ". The credit card number is required\n";
				}
				else if(!is_card_number(document.getElementById("cardNum").value))
				{					
					if(!focusSet)
					{
						//if the focus hasn't been set then set it now
						document.getElementById("cardNum").focus();
						focusSet = true;
					}
					error += ". A valid credit card number\n";
				}
				
				
				if(is_empty(document.getElementById("cardExp").value))
				{
					if(!focusSet)
					{
						document.getElementById("cardExp").focus();
						focusSet = true;
					}
					error += ". The card expiration date is required\n";
				}
				else if(!is_expr(document.getElementById("cardExp").value))
				{					
					if(!focusSet)
					{
						//if the focus hasn't been set then set it now
						document.getElementById("cardExp").focus();
						focusSet = true;
					}
					error += ". A MMYR is required for expiration date.\n";
				}
				
				
				if(is_empty(document.getElementById("amount").value))
				{
					if(!focusSet)
					{
						document.getElementById("amount").focus();
						focusSet = true;
					}
					error += ". The Amount is required\n";
				}
				else if(!is_double(document.getElementById("amount").value))
				{					
					if(!focusSet)
					{
						//if the focus hasn't been set then set it now
						document.getElementById("amount").focus();
						focusSet = true;
					}
					error += ". A valid  Amount is required. It must be a dollar amount without the '$' sign\n";
				}
				
				
				//if there is an error show the error message
				//and return false;
				if(error != "")
				{
					alert(error)
					return false;
				}
				
				//otherwise fall through to true
				return true;
		}
		else	//The form elements were not found
		{
			alert("The required Form Fields are missing.  Contact your system administrator for more information");		
			return false;
		}
	
	
}