// Utility function that returns true if a string contains only whitespace characters
function isblank(e)
{
	if(e.value == null || e.value == "")
	return true;
	
	for(var i = 0; i < e.value.length; i++)
	{
		var c = e.value.charAt(i);
		if((c != ' ') && (c != '\n') && (c != '\t'))
		return false;
	}
	return true;
}// function ends

function isEmail(e)
{
	var reEmail=/^[0-9a-zA-Z_\.-]+\@[0-9a-zA-Z_\.-]+\.[0-9a-zA-Z_\.-]+$/;
	if(reEmail.test(e.value))
	{
		return true;
	}
	return false;
}

//checks if an optional field is blank
// TODO: check this
function checkblank(e)
{
	if(isblank(e))
	{
		//showCallout(e.id, "This field must be filled in.");
		return false;
	}
	else
	{
		//user_email
			if(e.id == "user_email")
			{
			 if(isEmail_r(e))
				{
				//showCallout(e.id, "Email id is not valid.");
				return false;
				}
			}
	return true;
	
	}
	return true;
}

//checks if a field is numeric.
//If the optional min property is set, it checks it is greater than its value
//If the optional max property is set, it checks it is less than its value
function checknumber(e)
{
	var v = parseFloat(e.value);
	
	if(isNan(v))
	{
		//showCallout(e.id, "This field must be a number");
		return false;
	}
	
	if((e.minNumber != null) && (v < e.minNumber))
	{
		//a//showCallout(e.id, "This field must be greater than or equal to " + e.minNumber);
		return false;
	}
	
	if(e.maxNumber != null && v > e.maxNumber)
	{
		//showCallout(e.id, "The field must be less than or equal to " + e.maxNumber);
		return false;
	}
	return true;
}//function ends

//Checks if a field looks like a date in the 99/99/9999 format
function checkdate(e)
{
	var slashCount=0;
	if(e.value.length != 10)
	{
		//showCallout(e.id, " The field must have the format 99/99/9999" + " and be 10 characters in length");
		return false;
	}
	
	for(var j = 0; j < e.value.length; j++)
	{
		var c = e.value.charAt(j);
		
		if((c == '/'))
			slashCount++;
			
		if(c != '/' && (c < '0' || c > '9'))
		{
			//showCallout(e.id, " This field can contain only numbers and forward-slashes");
			return false;
		}
	}//for ends
	
	if(slashCount != 2)
	{
		//showCallout(e.id, " This field must have the format 99/99/9999");
		return false;
	}
	return true;
}//function ends

//Checks if a field contains any whitespaces
function checkwhitespace(e)
{
	var seenAt = false;
	
	for(var j = 0; j < e.value.length; j++)
	{
		var c = e.value.charAt(j);
		
		if((c ==' ') || (c == '\n') || (c == '\t'))
		{
			//showCallout(e.id, "This field must not contain whitespaces");
			return false;
		}
	}
	return true;
}//function ends

//Now check for fields that are supposed to be emails.
//Only checks that there's one @ symbol and no whitespaces
function checkmail(e)
{
	var seenAt=false;
	for(var j = 0; j < e.value.length; j++)
	{
		var c = e.value.charAt(j);
		
		if((c == ' ') || (c == '\n') || (c == '\t'))
		{
			//showCallout(e.id, "This is an invalid email address - there should be no blank spaces.");
			return false;
		}
		
		if((c == '@') && (seenAt == true))
		{
			//showCallout(e.id, "This is an invalid email address.");
			return false;
		}
		
		if((c == '@'))
			seenAt =true;
	}
	
	if(seenAt == false)
	{
		////showCallout(e.id, "This is an invalid email address.");
		return false;
	}
	return true;
}//function ends


//This is the function that performs<form> validation. It is invoked from the onSubmit()
//event handler. The handler should return whatever value this function returns.
function verify(f)
{
	//Loop through elements of the form,looking for all text and text area elements.
	//Report errors using a post validation,field-by-field approach
	for(var i = 0; i < f.length; i++)
	{
		var e = f.elements[i];
		
		if(((e.type == "text") || (e.type == "textarea")))
		{
			//first check if the field is empty and shouldnt be
			if(!e.isOptional && !checkblank(e))
				return false;
			
			//Now check for fields that are supposed to be numeric.
			if(!isblank(e) && e.isNumeric && !checknumber(e))
				return false;
			
			//Now check for fields that are supposed to be dates
			if(!isblank(e) && e.isDate && !checkdate(e))
				return false;
			
			//Now check for fields that are supposed to be emails
			if(!isblank(e) && e.isEmail && !checkmail(e))
				return false;
			
			//Now check for fields that are supposed not to have whitespaces
			if(!isblank(e) && e.hasNospaces && !checkwhitespace(e))
				return false;
		}//if(type is text or textarea
	}//for each character in field

	//There were no errors if we got this far
	return true;
}//function ends

function isValidEmail(str) {
  return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);	
}
function isNull(aStr)
{
	var index;		
	for (index=0; index < aStr.length; index++)
		if (aStr.charAt(index) != ' ')
			return false;
	return true;
}
function trim(str)
{
    s = str.replace(/^(\s)*/, '');
    s = s.replace(/(\s)*$/, '');
    return s;
}
