function isEmail(str) {
	
	var supported = 0;
  
	if (window.RegExp) {
    
		var tempStr = "a";
   		var tempReg = new RegExp(tempStr);
    
		if (tempReg.test(tempStr))
			supported = 1;
  	}
  
 	if (!supported) 
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  
  	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  
  	return (!r1.test(str) && r2.test(str));
}


function parseForMultiple(friendsEmail, commaLocation) {
	
	if(isEmail(friendsEmail.substring(0, commaLocation))) {
		//Start email is valid, check next
		var newString = friendsEmail.substring(commaLocation + 1, friendsEmail.length);
		
		var commaLocation = newString.indexOf(",");
		if(commaLocation == -1) {
			//Last entry
			if(isEmail(newString))
				return true;
			else
				return false;
		}
		else {
			return parseForMultiple(newString, commaLocation);
		}
	}
	else {
		//Email not valid
		return false;
	}
}

function checkEmail() {
	
	var isValid = false;
	
	var friendsEmail = document.email.emailTo.value;
	var commaLocation = friendsEmail.indexOf(",");
	
	if(commaLocation != -1) {
		//Parse for multiple addresses
		isValid = parseForMultiple(friendsEmail, commaLocation);
		if(!isValid)
			alert("Invalid email address (Friend's Email). Please re-type...");
		
	}
	else if(isEmail(document.email.emailTo.value)) {
		//Only one address
		isValid = true;
	}
	else {
		//Bad Individual Address
		alert("Invalid email address (Friend's Email). Please re-type...");
		isValid = false;
	}
	
	if(isValid) {
		if(!isEmail(document.email.emailFrom.value)) {
			alert("Invalid email address (Your Email Address). Please re-type...");
			isValid = false;
		}
	}
	
	return isValid;
}
