function isValidEmail(e)
{
	// assume an email address cannot start with an @ or white space, but it
	// must contain the @ character followed by groups of alphanumerics and '-'
	// followed by the dot character '.'
	// It must end with 2 or 3 alphanumerics.
	
	var alnum="a-zA-Z0-9";
	exp="^[^@\\s]+@(["+alnum+"+\\-]+\\.)+["+alnum+"]["+alnum+"]["+alnum+"]?$";
	emailregexp = new RegExp(exp);

	result = e.match(emailregexp);
	if (result != null)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// ---

function isValidDate(strDate)
{
	var strMatch = /^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/\d{4}/;
	
	return strMatch.test(strDate);
}

// ---

function isValidFileType( strFileName, arrExt )
{
	if (!strFileName.length)
	{
		return( false );
	}
	
	strFileName = strFileName.toLowerCase();
	
	for ( var j=0; j<arrExt.length; j++ )
	{
		if ( strFileName.substring( strFileName.length, strFileName.indexOf( arrExt[j].toLowerCase() ) ) == arrExt[j].toLowerCase() )
		{
			return( true );
		}
	}
	
	return( false );
}

// ---

function getRadioValue(objField)
{
	for (var j=0; j<objField.length; j++)
	{
		if (objField[j].checked)
		{
			return objField[j].value;
		}
	}
	return null;
}

// ---

function checkFormFields(strFormName, arrFields, arrTypes, arrOutput)
{
	/*
	*  arrFields = new Array("txtFirstName", "txtDescription", "txtEmail", "txtPassword");
	*  arrTypes = new Array("string", "longstring(MAX_CHARS)", "email", "password");
	*  arrOutput = new Array("First Name", "Description", "Email Address", "Password");
	*/
	
	var objForm = document.forms[strFormName];
	
	// clear all row colours
	for (var j=0; j<arrFields.length; j++)
	{
		document.getElementById(arrFields[j] + "_row").className = "";
	}
	
	// check all fields
	for (var j=0; j<arrFields.length; j++)
	{
		if (arrTypes[j] == "string")
		{
			if (objForm.elements[arrFields[j]].value.length < 1)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				alert("Please complete the '" + arrOutput[j] + "' field.");
				objForm.elements[arrFields[j]].focus();
				return false;
			}
		}
		else if (arrTypes[j].substring(0, 10) == "longstring")
		{
			if (objForm.elements[arrFields[j]].value.length < 1)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				alert("Please complete the '" + arrOutput[j] + "' field.");
				objForm.elements[arrFields[j]].focus();
				return false;
			}
			else
			{
				// arrTypes[j] is passed in as longstring(x) where x is
				// the maximum character limit. ie longstring(500);
				var strTemp = arrTypes[j].substring(10);
				var lngMaxLength = strTemp.substring(1, strTemp.length-1);
				
				if (objForm.elements[arrFields[j]].value.length > lngMaxLength)
				{
					document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
					
					alert("Please limit the '" + arrOutput[j] + "'\nfield to " + lngMaxLength + " characters or less.\n\nCurrent characters : " + objForm.elements[arrFields[j]].value.length);
					objForm.elements[arrFields[j]].focus();
					return false;
				}
			}
		}
		else if (arrTypes[j] == "email")
		{
			// check valid email address
			
			if (objForm.elements[arrFields[j]].value.length < 1)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				alert("Please complete the '" + arrOutput[j] + "' field.");
				objForm.elements[arrFields[j]].focus();
				return false;
			}
			else
			{
				if (!isValidEmail(objForm.elements[arrFields[j]].value))
				{
					document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
					
					alert("Please enter a valid email address in the '" + arrOutput[j] + "' field.");
					objForm.elements[arrFields[j]].focus();
					return false;
				}
			}
		}
		else if (arrTypes[j] == "password")
		{
			// password (needs to be at least 5 characters)
			if (objForm.elements[arrFields[j]].value.length < 5)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				alert("Please complete the '" + arrOutput[j] + "' field with at least 5 characters.");
				objForm.elements[arrFields[j]].focus();
				return false;
			}
			else
			{
				// make the passwords lower case
				objForm.elements[arrFields[j]].value = objForm.elements[arrFields[j]].value.toLowerCase();
				objForm.elements[arrFields[j] + "_confirm"].value = objForm.elements[arrFields[j] + "_confirm"].value.toLowerCase();
			}
			
			// check passwords match
			if (objForm.elements[arrFields[j]].value != objForm.elements[arrFields[j] + "_confirm"].value)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				alert("Your passwords do not match!\n\nPlease enter the same value in both\nthe '" + arrOutput[j] + "' and 'Re-Type " + arrOutput[j] + "' fields.");
				objForm.elements[arrFields[j]].focus();
				return false;
			}
		
		}
	}
	
	return true;
}

// ---

function countWords(strInput)
{

	var char_count = strInput.length;
	var fullStr = strInput + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length-1;
	
	if (fullStr.length <2)
	{
		word_count = 0;
	}
	
	return word_count;

}

// ---

function openProfile(Uid)
{
	var width = 550;
	var height = 400;
	
	var posHor = (screen.width - width)/2;
	var posVer = (screen.height - height)/2;
	
	var newwindow = window.open("/directory/view.cfm?uid=" + Uid, "profile","left=" + posHor +",top=" + posVer + ",height=" + height + ",width=" + width + ",toolbars=no,resizable=yes,scrollbars=yes,fullscreen=no");

	newwindow.focus();
}
