var url = "getFortune.php?id="; // The server-side script
var pick_url = "pickFortune.php?uniqueid=";
var email_url = "send-fortune.php?uniqueid=";

function handleHttpResponse() {
  if (http.readyState == 4) {
    document.getElementById('fortune').innerHTML = http.responseText;
  }
}
function updateFortune() {
  var fortuneValue = document.getElementById("random_f_id").value;
  http.open("GET", url + escape(fortuneValue), true);
  http.onreadystatechange = handleHttpResponse;
  http.send(null);
}

function clearFortune() {
  if (document.getElementById("uniqueid")) {
		var uniqueid = document.getElementById("uniqueid").value;
		http.open("GET", "pickFortune.php?uniqueid=" + uniqueid, true);
	} else {
			if (document.getElementById("back_uniqueid")) {
				var backId = document.getElementById("back_uniqueid").value;
				http.open("GET", "pickFortune.php?backid=" + backId, true);
			} else {
				http.open("GET", "pickFortune.php" + uniqueid, true);
			}
	}
  http.onreadystatechange = handleHttpResponse;
  http.send(null);
}

function dailyMail() {
  http.open("GET", "daily-fortune-signup.php", true);
  http.onreadystatechange = handleHttpResponse;
  http.send(null);
}

function sendDailyMail() {
	var email = document.getElementById("email").value;
	if (isValidEmail(email)) { 
		var name = document.getElementById("name").value;
		var name = name.replace(/[^a-zA-Z 0-9]+/g,'');
		http.open("GET", "daily-signup-mail.php?email=" + escape(email) + "&name=" + escape(name), true);
		http.onreadystatechange = handleHttpResponse;
		http.send(null);
	} else {
		alert('Please enter a valid email address');
	}
}

function openSendMail() {
  var fortuneValue = document.getElementById("this_uid").value;
  http.open("GET", email_url + escape(fortuneValue), true);
  http.onreadystatechange = handleHttpResponse;
  http.send(null);
}

function sendMyMail() {
	var email = document.getElementById("email").value;
	if (isValidEmail(email)) {
		var comments = document.getElementById("comments").value;
		//var comments = comments.replace(/[^a-zA-Z 0-9]+/g,'');
		var from = document.getElementById("from").value;
		//var from = from.replace(/[^a-zA-Z 0-9]+/g,'');
		var uniqueid = document.getElementById("uniqueid").value;
		http.open("GET", "send-mail.php?uniqueid=" + escape(uniqueid) + "&email=" + escape(email) + "&from=" + escape(from) + "&comments=" + escape(comments), true);
		http.onreadystatechange = handleHttpResponse;
		http.send(null);
	} else {
		alert('Please enter a valid email address');
	}
}
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object