var xmlHttp;

function GetXmlHttpObject() {
	var objXMLHttp = null;
	if (window.XMLHttpRequest) { objXMLHttp = new XMLHttpRequest(); }
	else if (window.ActiveXObject) { objXMLHttp = new ActiveXObject('Microsoft.XMLHTTP'); }
	return objXMLHttp;
}

function noAjax() {
	alert('Browser does not support HTTP Request');
	return;
}

/* processes a request to send an article to a friend */
function sendFriendG() {
	var name1 = document.getElementById('sf-your-name').value;
	var email1 = document.getElementById('sf-your-email').value;
	var name2 = document.getElementById('sf-friend-name').value;
	var email2 = document.getElementById('sf-friend-email').value;
	var msg = escape(document.getElementById('sf-message').value);
	var id = document.getElementById('sf-id').value;
  var uri = document.getElementById('sf-uri').value;
	
	if (name1 == '' || email1 == '' || name2 == '' || email2 == '') { alert('Please fill out all fields.'); }
	else {
		var url = "/func/sendfriendG.php?";
		url += "name1=" + name1 + "&email1=" + email1;
		url += "&name2=" + name2 + "&email2=" + email2;
		url += "&id=" + id;
    url += "&uri=" + uri;
		url += "&msg=" + msg;
		
		xmlHttp = GetXmlHttpObject();
		if (xmlHttp == null) { noAjax(); }
		else {
			xmlHttp.onreadystatechange = sendFriendGResult;
			xmlHttp.open('GET',url,true);
			xmlHttp.send(null);
		}
	}
	return;
}

function sendFriendGResult() {
	if ((xmlHttp.readyState == 4) || (xmlHttp.readyState == 'complete')) {
		var response = xmlHttp.responseText;
		//document.getElementById('send-friend').style.display = 'none';
		var msg = '';
		if (response == '1') {
      msg = 'This page was successfully sent. You will now be taken back to the top of this page.';
      location.href = location.href.replace(/#sendfriend/, '');
    }
		else { msg = 'There was a problem sending this page.  Please try again'; }
		alert(msg);
	}
	return;
}


/* processes a request to send an article to a friend */
function sendFriend() {
	var name1 = document.getElementById('sf-your-name').value;
	var email1 = document.getElementById('sf-your-email').value;
	var name2 = document.getElementById('sf-friend-name').value;
	var email2 = document.getElementById('sf-friend-email').value;
	var msg = escape(document.getElementById('sf-message').value);
	var y = document.getElementById('sf-y').value;
	var m = document.getElementById('sf-m').value;
	var d = document.getElementById('sf-d').value;
	//var article = escape(document.getElementById('article').innerHTML).slice(0, 1900);
	
	if (name1 == '' || email1 == '' || name2 == '' || email2 == '') { alert('Please fill out all fields.'); }
	else {
		var url = "/func/sendfriend.php?";
		url += "name1=" + name1 + "&email1=" + email1;
		url += "&name2=" + name2 + "&email2=" + email2;
		url += "&y=" + y + "&m=" + m + "&d=" + d;
		url += "&msg=" + msg;
		
		xmlHttp = GetXmlHttpObject();
		if (xmlHttp == null) { noAjax(); }
		else {
			xmlHttp.onreadystatechange = sendFriendResult;
			xmlHttp.open('GET',url,true);
			xmlHttp.send(null);
		}
	}
	return;
}

function sendFriendResult() {
	if ((xmlHttp.readyState == 4) || (xmlHttp.readyState == 'complete')) {
		var response = xmlHttp.responseText;
		//document.getElementById('send-friend').style.display = 'none';
		var msg = '';
		if (response == '1') {
      msg = 'This journal was successfully sent. You will now be taken back to the top of this page.';
      location.href = location.href.replace(/#sendfriend/, '');
    }
		else { msg = 'There was a problem sending this journal.  Please try again'; }
		alert(msg);
	}
	return;
}

/* processes a request to send an article to a friend */
function sendFriendInfo() {
	var name1 = document.getElementById('sf-your-name').value;
	var email1 = document.getElementById('sf-your-email').value;
	var name2 = document.getElementById('sf-friend-name').value;
	var email2 = document.getElementById('sf-friend-email').value;
	var msg = escape(document.getElementById('sf-message').value);
  var cat = document.getElementById('sf-cat').value;
	var title = escape(document.getElementById('sf-title').value);
	
	if (name1 == '' || email1 == '' || name2 == '' || email2 == '') { alert('Please fill out all fields.'); }
	else {
		var url = "/func/sendfriendinfo.php?";
		url += "name1=" + name1 + "&email1=" + email1;
		url += "&name2=" + name2 + "&email2=" + email2;
		url += "&msg=" + msg + "&cat=" + cat + "&title=" + title;
		
		xmlHttp = GetXmlHttpObject();
		if (xmlHttp == null) { noAjax(); }
		else {
			xmlHttp.onreadystatechange = sendFriendResultInfo;
			xmlHttp.open('GET',url,true);
			xmlHttp.send(null);
		}
	}
	return;
}

function sendFriendResultInfo() {
	if ((xmlHttp.readyState == 4) || (xmlHttp.readyState == 'complete')) {
		var response = xmlHttp.responseText;
		//document.getElementById('send-friend').style.display = 'none';
		var msg = '';
		if (response == '1') {
      msg = 'This page was successfully sent. You will now be taken back to the top of this page.';
      location.href = location.href.replace(/#sendfriend/, '');
    }
		else { msg = 'There was a problem sending this journal.  Please try again'; }
		alert(msg);
	}
	return;
}

function sendFriendHome() {
	var name1 = document.getElementById('sf-your-name').value;
	var email1 = document.getElementById('sf-your-email').value;
	var name2 = document.getElementById('sf-friend-name').value;
	var email2 = document.getElementById('sf-friend-email').value;
	var msg = escape(document.getElementById('sf-message').value);
	//var article = escape(document.getElementById('article').innerHTML).slice(0, 1900);
	
	if (name1 == '' || email1 == '' || name2 == '' || email2 == '') { alert('Please fill out all fields.'); }
	else {
		var url = "/func/sendfriendhome.php?";
		url += "name1=" + name1 + "&email1=" + email1;
		url += "&name2=" + name2 + "&email2=" + email2;
		url += "&msg=" + msg;
		
		xmlHttp = GetXmlHttpObject();
		if (xmlHttp == null) { noAjax(); }
		else {
			xmlHttp.onreadystatechange = sendFriendHomeResult;
			xmlHttp.open('GET',url,true);
			xmlHttp.send(null);
		}
	}
	return;
}

function sendFriendHomeResult() {
	if ((xmlHttp.readyState == 4) || (xmlHttp.readyState == 'complete')) {
		var response = xmlHttp.responseText;
		//document.getElementById('send-friend').style.display = 'none';
		var msg = '';
		if (response == '1') {
      msg = 'This was successfully sent. You will now be taken back to the top of this page.';
      location.href = '/';
    }
		else { msg = 'There was a problem sending this.  Please try again'; }
		alert(msg);
	}
	return;
}