function validateThisEmailAddress(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
	{
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
	    return false
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		return false
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		return false
	}
		
	if (str.indexOf(" ")!=-1)
	{
	    return false
	}

	return true					
}

function validateInputTypeText(stringValue)
{
	//window.alert("******* "+stringValue)
	if ((stringValue==null)||(stringValue==""))
	{
		return false
	}

	return true					
}


function validateThisForm( startsWithString ) 
{
	var thisField = '';
	var brokenUpIdValuesArray = new Array(); // to store the textbox objects
	var allInputTypesInDom = document.getElementsByTagName( 'input' ) // store collection of all<input/> elements
	var allTextAreasInDom = document.getElementsByTagName( 'textarea' ) // store collection of all<textarea/> elements	
	var i = 0;
	var j = 0;	
	var thisInputTypeName = "";
	var thisInputTypeValue = "";	


	for ( i = 0; i < allInputTypesInDom.length; i++ )  // loop through and find <input type="text"/>
	{ 
			thisInputTypeName = allInputTypesInDom[i].id;			



			if(thisInputTypeName.substring(0,startsWithString.length) == startsWithString)
			{
				brokenUpIdValuesArray = new Array();
				brokenUpIdValuesArray = thisInputTypeName.split("_");

				if ( allInputTypesInDom[i].type == 'text' ) 
				{
					thisInputTypeValue = allInputTypesInDom[i].value;											
					//window.alert("---\n\n* " + thisInputTypeName +" >"+  thisInputTypeValue +"<");					
					
					if(brokenUpIdValuesArray[1] == 'email')
					{
						thisField = '';
						for ( j = 2; j < brokenUpIdValuesArray.length; j++ )
						{ 	
							thisField += brokenUpIdValuesArray[j] + ' ';
						}

						if (validateThisEmailAddress(thisInputTypeValue)==false)
						{
							alert("The email address '" + thisInputTypeValue + "' \n specified in '" + thisField + "' is invalid");
							return false	
						}
						
						if (validateInputTypeText(thisInputTypeValue) == false)
						{
							window.alert("The following text field is invalid\n\n* " + thisField);
							return false	
						}						
					}
					else
					{
						thisField = '';
						for ( j = 1; j < brokenUpIdValuesArray.length; j++ )  // loop through and find <input type="text"/>
						{ 	
							thisField += brokenUpIdValuesArray[j] + ' ';
						}
					
						if (validateInputTypeText(thisInputTypeValue) == false)
						{
							window.alert("The following text field is invalid\n\n* " + thisField);
							return false	
						}
					}				
				}
				else if (allInputTypesInDom[i].type == 'file' ) 
				{
					thisInputTypeValue = allInputTypesInDom[i].value;											
					//window.alert("---\n\n* " + thisInputTypeName +" >"+  thisInputTypeValue +"<");					

					thisField = '';
					for ( j = 1; j < brokenUpIdValuesArray.length; j++ )  // loop through and find <input type="text"/>
					{ 	
						thisField += brokenUpIdValuesArray[j] + ' ';
					}
				
					if (validateInputTypeText(thisInputTypeValue) == false)
					{
						window.alert("A file needs to be uploaded in the following field:\n\n* " + thisField);
						return false	
					}				
				}
				else if (allInputTypesInDom[i].type == 'checkbox' ) 
				{
					thisInputTypeChecked = allInputTypesInDom[i].checked;				
					//window.alert("---\n\n* " + thisInputTypeName +" >"+  thisInputTypeChecked +"<");					

					thisField = '';										
					for ( j = 1; j < brokenUpIdValuesArray.length; j++ )  // loop through and find <input type="text"/>
					{ 	
						thisField += brokenUpIdValuesArray[j] + ' ';
					}

					if(thisInputTypeChecked == false)
					{
						window.alert("The following checkbox needs to be selected:\n\n* " + thisField);
						return false
					}
				}				
			}
	}

	for ( i = 0; i < allTextAreasInDom.length; i++ )  // loop through and find <input type="text"/>
	{ 
		thisTextAreaName = allTextAreasInDom[i].id;			

		if(thisTextAreaName.substring(0,startsWithString.length) == startsWithString)
		{
			brokenUpIdValuesArray = new Array();
			brokenUpIdValuesArray = thisTextAreaName.split("_");
			thisTextAreaValue = allTextAreasInDom[i].value;	
			
			thisField = '';
			for ( j = 1; j < brokenUpIdValuesArray.length; j++ )  // loop through and find <input type="text"/>
			{ 	
				thisField += brokenUpIdValuesArray[j] + ' ';
			}
	   		
			if (validateInputTypeText(thisTextAreaValue) == false)
			{
				window.alert("The following field is invalid\n\n* " + thisField);			
				return false;	
			}
		}		
	}

	return true;
}
