var blnAdded = false;
var firstclick = true;

//FUNCTION TO REMOVE EMAILS FROM THE LIST BOX
function delEmailIdsFromList() {
	var intLength = document.form1.cmbEmails.length;
	var sCheckFlag = false;
	for(index = intLength - 1; index >= 0; index--) {
		if(document.form1.cmbEmails.options[index].selected == true) {
			document.form1.cmbEmails.options[index] = null;
			sCheckFlag = true;
		}
	}
	if (sCheckFlag == false) {
		alert("Please select the e-mail address you wish to delete.\n");
		document.form1.cmbEmails.focus();
		return;
	}
}

function addEmailIdsToList() {
	if(firstclick == true) {
		document.form1.cmbEmails.options[0] = null;
		firstclick = false;
	}
	var strEmailId;
	strEmailId = document.form1.vchrFriendEmailA.value;
	if (strEmailId == "") {
		alert("Please enter an e-mail address to add.\n");
		document.form1.vchrFriendEmailA.focus();
		return false;
	}
	else if (!validateEmail(strEmailId)) {
		alert("Please Enter A Valid Email Address.\n");
		document.form1.vchrFriendEmailA.value = "";
		document.form1.vchrFriendEmailA.focus();
		return false;
	}
	else {	
		//CHECK IF THERE IS ALREADY AN IDENTICAL ENTRY IN THE LIST BOX
		intIndex = document.form1.cmbEmails.length;
		for(i = 0; i < intIndex; i++) {
			if(strEmailId.toUpperCase() == document.form1.cmbEmails.options[i].value.toUpperCase()) {
				alert("This e-mail address is already in your recipient list.\n");
				document.form1.vchrFriendEmailA.value = "";
				document.form1.vchrFriendEmailA.focus();
				return false;		
			}
		}
		blnAdded = true;
		document.form1.vchrFriendEmailA.value = "";
		document.form1.vchrFriendEmailA.focus();
		document.form1.cmbEmails.options[intIndex] = new Option(strEmailId,strEmailId);
		document.form1.cmbEmails.selectedIndex = -1;
		return true;
	}
}

function checkForm() {
	var errStr = "";
	if (!validateReal(document.form1.vchrSenderEmail.value)) {errStr = errStr + "Please enter your e-mail address.\n"; document.form1.vchrSenderEmail.focus();}
	if (!validateReal(document.form1.vchrSenderName.value)) {errStr = errStr + "Please enter your name.\n"; document.form1.vchrSenderName.focus();}
	if (!validateReal(document.form1.vchrSubject.value)) {errStr = errStr + "Please enter a subject.\n"; document.form1.vchrSubject.focus();}
	if (!validateReal(document.form1.vchrFriendEmailA.value) && blnAdded == false) {
		errStr = errStr + "The e-mail needs at least one vchrFriendEmail.\n"; 
		document.form1.vchrFriendEmailA.focus();
	}
	if(errStr != "") {
		alert(errStr);
		return false;
	}

	// POPULATE THE HIDDEN FORM FIELD WITH A TILDA-DELIMITED 
	//LIST OF EMAIL RECIPIENTS
	if(document.form1.vchrFriendEmailA.value != "") {
		document.form1.vchrFriendEmail.value = document.form1.vchrFriendEmailA.value + "~";
	}
	else {
		document.form1.vchrFriendEmail.value = "";
	}
	var iNoEmailids = document.form1.cmbEmails.length;
	for (i = 0; i < iNoEmailids; i++) {	
		strEmailId = document.form1.cmbEmails.options[i].value;
		document.form1.vchrFriendEmail.value += strEmailId +"~";
	}
	return true;	
}

function validateEmail(email) {
	var filter = /^[A-Za-z0-9\_\-\.]+@+[A-Za-z0-9\_\-\.]+\.+[A-Za-z]{2,3}$/;
	return (filter.test(email));
}

function validateReal(stuff) {
	if (stuff.replace(/ /g,'').length == 0)
		return false;
	return true;
}
