// Mail article to friend script

window.onload = function() {
	initMailFriend();
	
}

function initMailFriend() {
	var oldBody = document.body.innerHTML;
	document.body.innerHTML = oldBody + '<form id="mailtofriendform" method="post" action="javascript:sendMailToFriend()" style="display:none"><input id="articleLoc" name="articleLoc" type="hidden" value="' + document.URL + '" /><input id="articleTitle" name="articleTitle" type="hidden" value="' + document.getElementById('column_lead').getElementsByTagName('h1')[0].innerHTML + '" /><div class="closeBtn"><a href="javascript:closeMailFriend()">Close X</a></div><h2>Mail This Article to a Friend</h2><table><tr><td colspan="2">To send this article to a friend, just fill out the following fields and click "send."</td></tr><tr><th>Your Name</th><td><input id="mailname1" name="mailname1" type="text" /></td></tr><tr><th>Your eMail</th><td><input id="mailadd1" name="mailadd1" type="text" /></td></tr><tr><th>Friend\'s Name</th><td><input id="mailname2" name="mailname2" type="text" /></td></tr><tr><th>Friend\'s eMail</th><td><input id="mailadd2" name="mailadd2" type="text" /></td></tr><tr><th>Personal Message</th><td><textarea id="emailbody" name="emailbody" rows="4" cols="15">I was reading this article, and thought you would be interested in it as well.</textarea></td></tr><tr><td colspan="2"><input type="submit" id="mailsend" value="Send" /></td></tr></table></form>';
}

function mailToFriend() {
	document.getElementById('mailtofriendform').style.display='block';
}

function closeMailFriend() {
	document.getElementById('mailtofriendform').style.display='none';
}

function sendMailToFriend() {
	//get email character positions
	var emailAddress1 = document.getElementById('mailadd1').value;
		atPos1 = emailAddress1.indexOf('@');
		dotPos1 = emailAddress1.lastIndexOf('.');
	var emailAddress2 = document.getElementById('mailadd2').value;
		atPos2 = emailAddress2.indexOf('@');
		dotPos2 = emailAddress2.lastIndexOf('.');

	//make sure no elements are empty
	if ((document.getElementById('mailtofriendform').mailname1.value == '') || 
	(document.getElementById('mailtofriendform').mailadd1.value == '') || 
	(document.getElementById('mailtofriendform').mailname2.value == '') || 
	(document.getElementById('mailtofriendform').mailadd2.value == '') ||
	(document.getElementById('mailtofriendform').emailbody.value == '')) {
		alert('Please complete all fields before clicking "send."');
	//make sure email address is valid
	} else if (((atPos1 <= 0) || (dotPos1 <= atPos1)) || ((atPos2 <= 0) || (dotPos2 <= atPos2))) {
			alert('Please enter valid email addresses before clicking send.');
	} else {
		document.getElementById('mailtofriendform').action = "mailtofriend_send.php";
		document.getElementById('mailtofriendform').submit();
	}
}