//alert ("went in to validation");
var validate;

function validate(obj)
{
	//define objects to validate
	var allElements = obj.elements;
	var coCount = 0;
	var errors = "";

	for( i=0 ; i<allElements.length; i++)
	{
		//CHECK FULLNAME NOT EMPTY
		if (allElements[i].type=="text" && allElements[i].name == "fullname")
		{
			var oFullName = allElements[i].value;
			if ( oFullName.length <= 0 )
			{
				errors += "-> Fullname is a Mandatory field, please enter your Fullname\n";
			}			
		}		
		
		//CHECK EMAIL ADDRESS NOT EMPTY AND VALID
		else if (allElements[i].type == "text" && allElements[i].name == "email")
		{
			var oEmail = allElements[i].value;
			var chkDot = true;
			var result = false;
			var ndxAt = ndxDot =  0;
			var ndxAt = ndxDot =  0;

			if (oEmail.length <= 0 )
			{
				errors += "-> Email Address is a Mandatory field, please enter your Email Address\n";
			}
			else //not empty start email address validation
			{
				ndxAt  = oEmail.indexOf("@");
				ndxDot = oEmail.indexOf(".");
				ndxDot2 = oEmail.lastIndexOf(".");
									
				var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
				
			        if (oEmail.match(illegalChars)) { 
			                errors += "The Email Address contains illegal characters\n";
			        }
				else if ( (ndxDot < 0) ) //check for a '.' sign
				{
					errors += "->The Email Address lacks '.'\n\tThe format is 'you@domain.com'";
				}
				else if ( (ndxAt < 0) ) //check for an '@' sign
				{
					errors += "->The Email Address lacks an '@'\n\tThe format is 'you@domain.com'";
				}
				else if (ndxDot2 - 3 <= ndxAt)
				{
					errors += "->You may be missing your domain name\n\tThe format is 'you@domain.com'";
				}
			}
			
	
		}
		
		//CHECK CONTACT NUMBER NOT EMPTY
		else if (allElements[i].type=="text" && allElements[i].name == "number")
		{
			var oFullName = allElements[i].value;
			if ( oFullName.length <= 0 )
			{
				errors += "-> Contact Number is a Mandatory field, please enter your area code and Contact Number\n";
			}			
		}				
		

		//CHECK BRANCH DROPDOWN TITLE LIST
		if (allElements[i].name == "branch")
		{
			var otitle = allElements[i].value;
			if  ( ( otitle.length <= 0 ) || (allElements[i].selectedIndex == 0) )
			{
				errors += "-> Branch is a Mandatory field, please select the Branch you would like to contact\n";
			}
		}
		
		//CHECK QUERY DROPDOWN TITLE LIST
		if (allElements[i].name == "query")
		{
			var otitle = allElements[i].value;
			if  ( ( otitle.length <= 0 ) || (allElements[i].selectedIndex == 0) )
			{
				errors += "-> Select Query is a Mandatory field, please select the Query you would like to post\n";
			}
		}

		//CHECK BRANCH DROPDOWN TITLE LIST
		if (allElements[i].name == "province")
		{
			var otitle = allElements[i].value;
			if  ( ( otitle.length <= 0 ) || (allElements[i].selectedIndex == 0) )
			{
				errors += "-> Province is a Mandatory field, please select the Province you would like to contact\n";
			}
		}
		
		if (allElements[i].name == "comments")
		{
			var ocomments = allElements[i].value;
			if ( ocomments.length <= 0 )
			{
				errors += "-> Comments is a Mandatory field, please enter your Comments\n";
			}	
		}
		
		
		//CHECK PASS NUMBER NOT EMPTY
		else if (allElements[i].type=="text" && allElements[i].name == "code")
		{
			var oFullName = allElements[i].value;
			if ( oFullName.length <= 0 )
			{
				errors += "-> Code Number is a Mandatory field, please enter the code displayed below\n";
			}			
		}		
		
		
	}
	if (coCount == 2) 
	{
			
		errors += "-> Please fill in the neccesary country and area code in the Contact Number\n";
	}
	
	if(errors)
	{
		alert("The following errors were found\n" + errors);
		errors = "";
		returnVal = false;
	}
	else
	{
		returnVal = true;
	}
	return returnVal; 
}
