// JavaScript Document
//For subscription text box. Send data to PHP page as well.
function subscribe(){
	//target and declare text field
	var x = document.getElementById('subscriptionmail');
	//send data
	var bodyofrequest = encodeVariable("email",x.value);
	var client = new XMLHttpRequest();
	client.open("POST", "../../Scripts/BlogSubscribe.php", true);
	client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	client.onreadystatechange = onRecieve;
	client.send(bodyofrequest);
	//change textfield color and set message
	x.style.color = "#FF0000"
	x.value = "Subscribing.."
}

function clearSubscribeField(){
	var x = document.getElementById('subscriptionmail');
	x.value="";
}


function onRecieve() {
	var x = document.getElementById('subscriptionmail');
	
	if(this.readyState ==4 && this.responseText == "DONE") {
		x.style.color = "#4e4c4c"
		x.value = "Your@email.here"
		alert("Subscription Successful!\n Thank you for subscribing to daggerdyamics.com! An email will be sent to you whenever DaggerDynamics is updated.\n\n If you wish to unsubscribe to daggerdynamics, feel free to contact me at dagger@daggerdynamics.com.");
	}else if(this.readyState ==4 && this.responseText != "DONE"){
		x.style.color = "#4e4c4c"
		x.value = "Your@email.here"
		alert("ERROR!\n There seems to be an error with your subscription. Make sure you entered a valid email address, or try again later.");
	}
}

//for XMLHttpRequest POST encoding of variables
function encodeVariable(variable, value){
	return variable+"="+ encodeURIComponent(value);
}