// Javascript file for Email Validation for misaves.org



//Use this function for forms that only have an 'email' field
function verifyEmail(){
	form = document.forms[1];
	error = "";
	//form variables from parameters
		email = form.email.value;

	//email may not be required but must be of a valid format

	if(email != ""){
			var eAtSym    = email.indexOf('@');
			var ePeriod   = email.lastIndexOf('.');
			var eSpace    = email.indexOf(' ');
			var eLength   = email.length - 1;   // Array is from 0 to length-1
			if ((eAtSym < 1) ||                     // '@' cannot be in first position
				(ePeriod <= eAtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
				(ePeriod == eLength ) ||             // Must be atleast one valid char after '.'
				(eSpace  != -1))                    // No empty spaces permitted
			   {  
				  error += "Please enter a valid Contact e-mail address\n";
			   }
	}

	//output error or submit form
	if (error == ""){
		form.submit();
        form.submit.disabled = true;
	}
	else{
		alert(error);
		return;
	}
}



// Use this function when the form has a myEmail and friendEmail Field
function verifyMFEmail(){
	form = document.forms[1];
	error = "";
	//form variables from parameters
		postedByEmail = form.myEmail.value;
		targetEmail = form.friendEmail.value;
	
	//email may not be required but must be of a valid format


	if(postedByEmail != ""){
			var AtSym    = postedByEmail.indexOf('@');
			var Period   = postedByEmail.lastIndexOf('.');
			var Space    = postedByEmail.indexOf(' ');
			var Length   = postedByEmail.length - 1;   // Array is from 0 to length-1
			if ((AtSym < 1) ||                     // '@' cannot be in first position
				(Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
				(Period == Length ) ||             // Must be atleast one valid char after '.'
				(Space  != -1))                    // No empty spaces permitted
			   {  
				  error += "Please enter a valid Contact e-mail address\n";
			   }
	}
	
	if(targetEmail != ""){
			var fAtSym    = targetEmail.indexOf('@');
			var fPeriod   = targetEmail.lastIndexOf('.');
			var fSpace    = targetEmail.indexOf(' ');
			var fLength   = targetEmail.length - 1;   // Array is from 0 to length-1
			if ((fAtSym < 1) ||                     // '@' cannot be in first position
				(fPeriod <= fAtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
				(fPeriod == fLength ) ||             // Must be atleast one valid char after '.'
				(fSpace  != -1))                    // No empty spaces permitted
			   {  
				  error += "Please enter a valid Contact e-mail address\n";
			   }
	}

	//output error or submit form
	if (error == ""){
		form.submit();
        form.submit.disabled = true;
	}
	else{
		alert(error);
		return;
	}
}




function verifyMyEmail(){
	form = document.forms[1];
	error = "";
	//form variables from parameters
		postedByEmail = form.myEmail.value;
	
	//email may not be required but must be of a valid format
	if(postedByEmail != ""){
			var AtSym    = postedByEmail.indexOf('@');
			var Period   = postedByEmail.lastIndexOf('.');
			var Space    = postedByEmail.indexOf(' ');
			var Length   = postedByEmail.length - 1;   // Array is from 0 to length-1
			if ((AtSym < 1) ||                     // '@' cannot be in first position
				(Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
				(Period == Length ) ||             // Must be atleast one valid char after '.'
				(Space  != -1))                    // No empty spaces permitted
			   {  
				  error += "Please enter a valid Contact e-mail address\n";
			   }
	}

	//output error or submit form
	if (error == ""){
		form.submit();
        form.submit.disabled = true;
	}
	else{
		alert(error);
		return;
	}
}



// Function that clears the search field onfocus
function clearDefault(el) {
	if (el.defaultValue==el.value) el.value = ""
}
