﻿// (C) Azurant, LLC 2007
//
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, electronic, mechanical or otherwise, is prohibited
// without the prior written permission of the copyright owner.
//
// Filename: Email.js
//
// DATE     NAME    COMMENT
// 01/26/07 MIJ     Created file.
//
// This file contains general purpose E-Mail functions for use with the Eclarix system.
//
//////////////////////////////////////////////////////////////////////////////

// validates the domain for an e-mail list. The allowedExt parameter is an array of string domain names.
// email: E-mail value to validate.
// allowedExt: Domain restrictions
// Returns: boolean
function ValidEmailListDomain(email, allowedExt) 
{
	var valid = true;

	if (email.length > 0) 
	{
		email = Trim(email);
		var emailString = new String(email);
		var emails = emailString.split(",");
		for (var i = 0; i < emails.length; i++) 
		{
			var emailToken = Trim(emails[i]);
			valid &= ValidateEmailDomain(emailToken, allowedExt);
		}
	}

	return valid;
}

// validates the domain for a single e-mail. The allowedExt parameter is an array of string domain names.
// email: E-mail value to validate.
// allowedExt: Domain restrictions
// Returns: boolean
function ValidateEmailDomain(email, allowedExt) 
{
	var valid = true;

	var atIdx = email.indexOf("@");
	var ext = email.substring(atIdx + 1).toLowerCase(); // e-mail has been validated at this point, so we know there is a domain
	var extLength = ext.length;
	
	if (allowedExt.length > 0) 
	{
		var foundMatch = false;
		for (var i = 0; i < allowedExt.length; i++) 
		{		
			if (allowedExt[i].length > 0) 
			{
				allowedExt[i] = Trim(allowedExt[i]);
				var allowedExtLength = allowedExt[i].length;
				var allowedExtIdx = ext.indexOf(allowedExt[i]);
				if (allowedExtIdx != -1 && allowedExtIdx == (extLength - allowedExtLength)) 
				{
					foundMatch = true;
					break;
				}
			}
		}

		if (!foundMatch)
		{
			valid = false;
		}
	}
	else
	{
		valid = true;
	}

	return valid;
}

// Validates a single e-mail
// email: E-mail address to validate.
// Returns: boolean
function ValidEmail(email) 
{
	var result = false;

	var illegalChars = ",<>/?\";:\\|]}[{=+)(*&^%$#!`~ ";

	if (email.length > 0) 
	{
		email = Trim(email);
		var theStr = new String(email);

	    if (email.indexOf("<ecx:") == 0)
	    {
	        return true;
	    }
	
		for (var i = 0; i < illegalChars.length; i++) 
		{
			var illegalIdx = theStr.indexOf(illegalChars.substring(i, i + 1));
			if (illegalIdx >= 0)
				return false;
		}

		var index = theStr.indexOf("@");
		if (index > 0)
		{
			var pindex = theStr.indexOf(".", index);
			if ((pindex > index + 1) && (theStr.length > pindex + 1)) 
			{
				if (theStr.indexOf(",") == -1)
				{
					result = true;
				}
			}
		}
	}

	return result;
}

// validates a list of emails separated by commas
// emailString: Comma-separated e-mail string.
// Returns: boolean
function ValidEmailList(emailString) 
{
	var valid = true;

	if (emailString.length > 0) 
	{
		emailString = Trim(emailString);
		emailString = new String(emailString);
		var emails = emailString.split(",");
		for (var i = 0; i < emails.length; i++) 
		{
			var emailToken = Trim(emails[i]);
			if (!ValidEmail(emailToken)) 
			{
				valid = false;
				break;
			}
		}
	}

	return valid;
}

// Returns TRUE if the web address is valid (currently, this function alllows all non-null address through)
// address: web address
// Returns: boolean
function ValidWebAddress(address)
{
    var valid = false;
    
    // apply LOOSE validation - any value will work (this allows http://localhost, localhost, 127.0.0.1, test.com, test.com?param=value, etc...)
    valid = address != null && address.length > 0 && address.indexOf(" ") == -1;   
    
    return valid;
}
