/**

 *  build an arry of email items from a newline and or comma separated list.

 *

 */



function extractEmailItems(string) {

	// break email on newlines.

	var regExp = /.*/g

	var matchArray = string.match(regExp);



	// break on commas next

	var emailList = new Array();

	var ii;

	for (ii=0; ii<matchArray.length; ii++) {

		var commabreak = /[^,]+/g

		var subgroup = matchArray[ii].match(commabreak);

		var jj;

		if (subgroup == null)

			continue;

		// now regroup when the comma is imbedded in "'s or <>.

		for (jj=0; jj<subgroup.length; jj++) {

			part = subgroup[jj];



			var firstQuote = part.indexOf("\"");

			var lastQuote = part.lastIndexOf("\"");

			//  parsed a comma in the name field

			if (firstQuote != -1 && (firstQuote == lastQuote)) {

				while ((firstQuote == lastQuote) && (jj < (subgroup.length-1))) {

					jj++;

					part += subgroup[jj];

					lastQuote = part.lastIndexOf("\"");

				}

			}

			emailList.push(part);

		}

	}

	return emailList;

}



/**
 *  return true if email_address is valid otherwise return false.
 */

function isValidAddress(email_address) {
	if(email_address.replace(/\s/g,"") == ""){
		return false;
	}

	var verify = new RegExp("^([-a-zA-Z0-9._]+@[-a-zA-Z0-9.]+(\.[-a-zA-Z0-9]+)+)*$", "");
	return verify.test(email_address);
}


/**

 *  return the parts array extracted if the email address has a . and @ with no spaces.

 *	if this is not the case return an empty array.

 */



function verify_email(parts) {

	return (isValidAddress(parts[0])) ? parts : new Array();

}



	/** 

	* return an array of email address parts.  It can be of the format

	*		email_address

	*		"first a b last"<email_address>

	*		first a b last <email_address>

	*

	* @param $email_string the original string.

	* @return array of parts.  each part is stored in the array 

	* 0	email_address the email address - always exists

	* 1	firstname the first string found outside the email address - optional

	* 2	lastname the last string found outside the email address - optional

	* 3-x 1 - x other strings found outside the email address - optional.

	*	

	*/





function extractEmailParts(email_string) {

	var result = new Array("","","");

	result[0] = email_string;



	// search for an email addres blocked by <>

	var start = email_string.indexOf("<");

	var end = email_string.indexOf(">");

	// terminators not found so return the full string.

	if (start < 0 || end < 0) {

		return verify_email(result);

	}

	// terminators found so extract the email address.

	result[0] = email_string.substr(start+1, (end - start) - 1);

	// get the remaining part if any exists.

	if (start < 1)

		return verify_email(result);



	// extract "'s if they exist.

	var first_quote = email_string.indexOf('"');

	var remain;

	if (first_quote < 0) {

		remain = " " + email_string.substr(0, start-1);

	} else {

		// first quote found then locate a second quote.

		var second_quote = email_string.indexOf('"', first_quote+1);

		if (second_quote < 0) {

			// malformed quotes so leave the name parts undefined.

			return verify_email(result);

		}



		// extract the name parts.

		remain = " "+email_string.substr(first_quote+1, (second_quote - first_quote) - 1);

	}

	var regExp = /\s\S*/g;

	var matchArray = remain.match(regExp);

	if (matchArray != null) {

		// must have a firstname if you reach here.

		result[1] = matchArray[0];



		if (matchArray.length > 1) {

			// lastname exists

			result[2] = matchArray[matchArray.length-1];

			var len = matchArray.length;

			if ( len > 2) {

				var ii;

				for (ii=1; ii < len-1; ii ++) {

					result[ii+2] = matchArray[ii];

				}

			}

		}



	}



	return verify_email(result);



}




