// JavaScript Document

function CheckEmpty(lc_Value)
{
	var ln_Valid = 0;
	if (lc_Value.length == 0)
	{
		ln_Valid = 1;
	}
	
	return ln_Valid;
}

function CheckFile(lc_FieldValue,lc_ValidateVars)
{
	
	var ln_Valid = 0;
	
	if (lc_FieldValue.length !=0)
	{
	
		lc_ValidateVars = lc_ValidateVars.toLowerCase();
		
		ln_Dot = lc_FieldValue.lastIndexOf(".");
		
		lc_FileType = lc_FieldValue.substring(ln_Dot);
		lc_FileType = lc_FileType.toLowerCase();
		//alert('Uploaded file type = ' + lc_FileType);
		
		var la_TypeArray = new Array();
		la_TypeArray = lc_ValidateVars.split("_");
		
		var ln_MatchTypes = 0;
		
		for (ln_Count=0; ln_Count<la_TypeArray.length; ln_Count++)
		{
			//alert('type = ' + la_TypeArray[ln_Count]);
			
			if (lc_FileType == la_TypeArray[ln_Count])
			{
				ln_MatchTypes = 1;
			}
			
		}
		
		//alert('match types = ' + ln_MatchTypes);
		
		if (ln_MatchTypes == 0)
		{
			ln_Valid = 1;
		}
		
	}
	//alert('valid now = ' + ln_Valid);
	
	return ln_Valid;
}


function CheckFormat(lc_Value,lc_Type)
{
	var ln_Valid = 0;
	
	if (lc_Type == "email")
	{
		//alert('need to check email format');
		ln_Result = CheckEmailFormat(lc_Value);
		ln_Valid = ln_Valid + ln_Result;
	}
	else if (lc_Type == "phone")
	{
		alert('need to check phone format');
	}
	else if (lc_Type == "postcode")
	{
		alert('need to check postcode format');
	}
	
	return ln_Valid;
}

function CheckTelephone(lc_Value)
{
	var ln_Valid = 0;
	
	var lc_Allowed = '0123456789+ ';				
	var lc_CheckChar = "";
	
	for (ln_Count=0;ln_Count<lc_Value.length;ln_Count++)
	{
		lc_CheckChar = lc_Value.charAt(ln_Count)
		
		ln_CharPos = lc_Allowed.indexOf(lc_CheckChar)
		
		if (ln_CharPos == -1)
		{
			ln_Valid = ln_Valid + 1;
		}
	}
	
	return ln_Valid;
}

function CheckEmailFormat(lc_Email)
{
	var ln_Valid = 0;
	var lc_ErrMessage = "";
	var lc_ErrName = "";
	var lc_CheckStdEmailAddress = "";
	var ln_Chars = lc_Email.length;
	if (ln_Chars > 0)
	{
		var ln_AtSignPos = lc_Email.indexOf('@');
		if (ln_AtSignPos > -1)
		{
			var lc_Before = lc_Email.substring(0,ln_AtSignPos);
			if (lc_Before.charAt(0) == '.')
			{
				//cannot start with a "."				
				ln_Valid = ln_Valid + 1;
			}
			var ln_Before_FS = lc_Before.lastIndexOf(".");
			if (ln_Before_FS > -1 && ln_Before_FS == lc_Before.length-1)
			{
				//must have at least 2 characters after the "." and before the "@"
				ln_Valid = ln_Valid + 1;
			}
				
		}
		else
		{
			//address must contain @ sign
			ln_Valid = ln_Valid + 1;
		}
		if (ln_AtSignPos < lc_Email.length-1)
		{
			var lc_After = lc_Email.substring(ln_AtSignPos+1);
			if (lc_After.charAt(0) == '.')
			{
				//cannot have a "." immediately after the "@"			
				ln_Valid = ln_Valid + 1;
			}
			var ln_After_FS = lc_After.lastIndexOf(".");
			if (ln_After_FS > 0)
			{
				var ln_After_FS2 = lc_After.indexOf(".");
				if (ln_After_FS2 > 0 && ln_After_FS2 != ln_After_FS)
				{
					if (ln_After_FS2 < 2)
					{
						//must have at least 2 characters after the "@" and before the "."
						ln_Valid = ln_Valid + 1;
					}
					if (ln_After_FS2 + 2 >= ln_After_FS)
					{
						//must have at least 2 characters between the "." characters
						ln_Valid = ln_Valid + 1;
					}					
				}
				else
				{
					if (ln_After_FS < 2)
					{
						//must have at least 2 characters after the "@" and before the "."
						ln_Valid = ln_Valid + 1;
					}
				}
			}
			else
			{
				//must contain at least one "." character after the "@"'
				ln_Valid = ln_Valid + 1;
			}
			if (ln_After_FS > -1 && ln_After_FS == lc_After.length-1)
			{
				//cannot end with a "." character
				ln_Valid = ln_Valid + 1;
			}
			if (ln_After_FS > -1 && ln_After_FS >= lc_After.length-2)
			{
				//must end with at least 2 characters after the last "."
				ln_Valid = ln_Valid + 1;
			}			
		}
		else
		{
			//address cannot end with an @ sign
			ln_Valid = ln_Valid + 1;
		}	
	}
	else
	{
		//Please enter a valid ' + lc_ErrName + ' address'
		ln_Valid = ln_Valid + 1;
	}
	
	lc_CheckStdEmailAddress=lc_ErrMessage;
	
	//alert('email valid = ' + ln_Valid);
	
	return ln_Valid;
}
