//requires JavaScript variables defined in Validate.asp/apg.

function Verify(CurrentForm, Password) 
{
	var Msg = missinginformation + "\n\n";
	var EmptyFields = false;
	var PasswordIncorrect = false;
	var EmailPresent = false;
	var EmailInvalid = false;
	var Missing = "";

	for(var i = 0; i < CurrentForm.length; i++) 
	{
		var Element = CurrentForm.elements[i];
		
		if (!Element.optional)
		{
			switch(Element.type)
			{
				case "text":
					if (Element.value == null || IsBlank(Element.type, Element.value))
					{
						if (Element.name == email_address || Element.name == email)
						{
							EmailPresent = true;
							var EmailName = Element;
							EmptyFields = true;
							Missing = Element.name;
							Missing = Missing.replace(/_/g, " ");
							Msg = Msg + eaddress  + "\n";
						}
						else if (Element.name == friendemail )
						{
							EmailPresent = true;
							var EmailName = Element;
							EmptyFields = true;
							Missing = Element.name;
							Missing = Missing.replace(/_/g, " ");
							Msg = Msg + friendemailaddress  + "\n";
						}
						else if (Element.name == friends_name )
						{
							EmailPresent = true;
							var EmailName = Element;
							EmptyFields = true;
							Missing = Element.name;
							Missing = Missing.replace(/_/g, " ");
							Msg = Msg + friendname + "\n";
						}
						else
						{
							EmptyFields = true;
							Missing = Element.name;
							Missing = Missing.replace(/_/g, " ");
							Msg = Msg + Missing + "\n";
						}
					}
					if (Element.name == email_address ||  Element.name == friendemail)
					{
						EmailPresent = true;
						var EmailName = Element;
					}
					

					break;	

				case "textarea":
					if (Element.value == null || IsBlank(Element.type, Element.value))
					{
						EmptyFields = true;
						Missing = Element.name;
						Missing = Missing.replace(/_/g, " ");
						Msg = Msg + Missing + "\n";
					}
					break;						 

				case "password":
					if (Element.value == null || IsBlank(Element.type, Element.value))
					{
						EmptyFields = true;
						Missing = Element.name;
						Missing = Missing.replace(/_/g, " ");
						Msg = Msg + Missing + "\n";
					}
					break;						 

				case "select-one":
					if (Element.value == "" || Element.value == "0" || Element.selectedIndex == -1 || (Element.selectedIndex == 0 && ((Element.options[Element.selectedIndex].value == 0) || (Element.options[Element.selectedIndex].value == null) || (Element.options[Element.selectedIndex].value == ""))))
					{
						EmptyFields = true;
						Missing = Element.name;
						Missing = Missing.replace(/_/g, " ");
						Msg = Msg + Missing + "\n";
					}
					break;							 

				case "select-multiple":
					if (Element.value == "" || Element.value == "0" || Element.selectedIndex == -1 || (Element.selectedIndex == 0 && ((Element.options[Element.selectedIndex].value == 0) || (Element.options[Element.selectedIndex].value == null) || (Element.options[Element.selectedIndex].value == ""))))
					{
						EmptyFields = true;
						Missing = Element.name;
						Missing = Missing.replace(/_/g, " ");
						Msg = Msg + Missing + "\n";
					}
					break;							 

  				case "radio":
  					var FormName = CurrentForm.name
  					var RadioName = eval("document." + FormName + "." + Element.name);
  					var RadioStatus = false;
					var counter = 0;
  					for (counter = 0; counter < (eval("CurrentForm." + Element.name + ".length")); counter++)
  					{
  						if (eval("CurrentForm." + Element.name + "[" + counter + "].checked") == true)
  							{
  								RadioStatus = true;
  							}
  					}

 						Missing = Element.name;
 						Missing = Missing.replace(/_/g, " ");						
 						
						if (!RadioStatus && (Msg.search(Missing) == -1))
 							{
 								EmptyFields = true;
 								Msg = Msg + Missing + "\n";
 							}
 							break;	
			}
		}
	}



	if (!EmptyFields) 
	{
		if (Password == true) 
		{
			if(ConfirmPassword(CurrentForm))
			{
				PasswordIncorrect = true;
			}
		}

		if (EmailPresent == true && PasswordIncorrect != true)
		{
			if(ValidateEmail(CurrentForm, EmailName))
			{
				EmailInvalid = true;
			}
		}

	}

	if (EmptyFields) 
	{
		alert(Msg);
	} 

	if (!EmptyFields && !PasswordIncorrect && !EmailInvalid)
	{
		return true;
	}
	else
	{
		return false;
	}
	
}





function IsBlank(ElementType, ElementValue) 
{
	if (ElementValue != null)
	{
		for(var i = 0; i < ElementValue.length; i++) 
		{
			var c = ElementValue.charAt(i);
			if ((c != ' ') && (c != '\n') && (c != '\t')) 
			{
				return false;
			}
		}
		return true;
	}
	else 
	{
		if (ElementType != "select-one" && ElementType != "select-multiple")
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}





function ValidateEmail(CurrentForm, Email)
{
	var Pattern = /^(.+)@(.+)$/;
	var SpecialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
       var ValidChars = "\[^\\s" + SpecialChars + "\]";
	var QuotedUser = "(\"[^\"]*\")";
	var Piece = ValidChars + '+';
	var Word = "(" + Piece + "|" + QuotedUser + ")";
	var UserPattern = new RegExp("^" + Word + "(\\." + Word + ")*$");
	var KnownDomains = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var IPDomain = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var NormalDomain = new RegExp("^" + Piece + "(\\." + Piece + ")*$");
	var MatchArray = Email.value.match(Pattern);
	var InvalidChars ="'#";
	var checkStr = Email.value; 
	
	if (MatchArray == null) 
	{
		alert(missinginvalidemail);
		//      "The e-mail address you entered does not follow the accepted pattern.");
		return true;
       }
	
	var User = MatchArray[1];
	var Domain = MatchArray[2];

	for (i = 0; i < User.length; i++) 
	{
		if (User.charCodeAt(i) > 127) 
		{
			alert(missinginvalidemail);
		//      "The e-mail address you entered contains invalid characters in the username.");
			return true;
		}
	}

	if (User.match(UserPattern) == null) 
	{
		alert(missinginvalidemail);
		//      "The e-mail address you entered contains an invalid username.");
		return true;
	}


	
	for (i = 0; i < Domain.length; i++) 
	{
		if (Domain.charCodeAt(i) > 127) 
		{
		alert(missinginvalidemail);
		//      "The e-mail address you entered contains invalid characters in the domain name.");
			return true;
  			}
	}
	
	var IPArray = Domain.match(IPDomain);
	if (IPArray!=null) 
	{
		for (var i=1; i <= 4; i++) 
		{
			if (IPArray[i] > 255) 
				{
					alert(missinginvalidemail);
		     		//	  "The e-mail address you entered contains an invalid destination IP address.");
					return true;
  					}
		}
		return false;
	}

	var PiecePattern = new RegExp("^" + Piece + "$");
	var DomainArray = Domain.split(".");
	var Len = DomainArray.length;
	for (i=0; i < Len; i++) 
	{
		if (DomainArray[i].search(PiecePattern) == -1) 
		{
			alert(missinginvalidemail);
     		//	  "The e-mail address you entered contains an invalid domain name.");
			return true;
	   	}
	}

	if (DomainArray[DomainArray.length - 1].length != 2 && DomainArray[DomainArray.length - 1].search(KnownDomains) == -1) 
	{
		alert(missinginvalidemail);
     	//	  "The e-mail address you entered does not end in a well-known domain or two letter country.");
		return true;
	}		
	
	if (Len < 2) 
	{
		alert(missinginvalidemail);
		//	  "The e-mail address you entered is missing a host name.");
		return true;
	}
	
	for (i = 0;  i < checkStr.length;  i++) 
	{ 
       	ch = checkStr.charAt(i); 
           for (j = 0;  j < InvalidChars.length;  j++) 
           	if (ch == InvalidChars.charAt(j)) 
			{
         			alert(missinginvalidemail);
				return true; 
              }                       
       } 
	
	return false;
}





function ConfirmPassword(CurrentForm) 
{
	if (CurrentForm.Password.value != CurrentForm.Confirm.value) 
	{
		alert(missingpasswordnotmatch);
		CurrentForm.Password.value = '';
		CurrentForm.Confirm.value = '';
		CurrentForm.Password.focus();
		return true;
	}
	else
	{
		if (CurrentForm.Password.value.length < 4)
		{
			alert(missingpassmustbe4char);
			CurrentForm.Password.value = '';
			CurrentForm.Confirm.value = '';
			CurrentForm.Password.focus();
			return true;
		}
	}
	return false;
}
