var errorMessage = document.getElementById("blogFormMessage");
errorMessage.className = "noVis";
var checkbox = document.getElementById("blogTermsCheck");
var titleLabel = document.getElementById("blogTitleLabel");
var textLabel = document.getElementById("blogTextLabel");
var roleLabel = document.getElementById("blogRoleLabel");
var locLabel = document.getElementById("blogLocLabel");
var checkLabel = document.getElementById("blogCheckLabel");
var titleConfirm = false;
var storyConfirm = false;
var roleConfirm = false;
var locationConfirm = false;
var emailConfirm = false;


function confirmInput(field){
	if (field == "title"){
		var titleInput = document.getElementById("storyTitle").value;
		if (titleInput !== "")
			titleConfirm = true;
		else
			titleConfirm = false;
	} else if (field == "story"){
		var storyInput = document.getElementById("storyText").value;
		if (storyInput !== "")
			storyConfirm = true;
		else
			storyConfirm = false;
	} else if (field == "role"){
		var roleInput = document.getElementById("storyRole").value;
		if (roleInput !== "")
			roleConfirm = true;
		else
			roleConfirm = false;
	} else if (field == "location"){
		var locationInput = document.getElementById("storyLoc").value;
		if (locationInput !== "")
			locationConfirm = true;
		else
			locationConfirm = false;
	}
}

function confirmEmail(){
	var emailInput = document.getElementById("storyEmail").value;
	if (request){
		var url = "emailValid.php";
		request.open("POST", url, true);
		request.onreadystatechange = emailReturn;
		params = "email=" + emailInput;
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Content-length", params.length);
		request.setRequestHeader("Connection", "close");
		request.send(params);
	}
}

function emailReturn(){
	if ((request.readyState == 4) && (request.status == 200)){
		if (request.responseText == 1)
			emailConfirm = true;
		else
			emailConfirm = false;
	}
}

function submitStory(){
	errorMessage.className = "noVis";
	var message = "";
	var cleared = true;
	
	titleLabel.style.color = "#000";
	textLabel.style.color = "#000";
	roleLabel.style.color = "#000";
	locLabel.style.color = "#000";
	checkLabel.style.color = "#000";
	
	if ((titleConfirm == false) || (storyConfirm == false) || (roleConfirm == false) || (locationConfirm == false) || (checkbox.checked == false)){
		cleared = false;
	}
		
	if (titleConfirm == false)
		titleLabel.style.color="#FF0000";
	
	if (storyConfirm == false)
		textLabel.style.color="#FF0000";
	
	if (roleConfirm == false)
		roleLabel.style.color="#FF0000";
	
	if (locationConfirm == false)
		locLabel.style.color="#FF0000";
		
	if (checkbox.checked == false)
		checkLabel.style.color="#FF0000";
	
	if (cleared == false){
		errorMessage.className="";
	}else if (cleared == true) {
		if (request){
		  var url = "mail.php";
		  request.open("POST", url ,true);
		  if (navigator.appName=="Microsoft Internet Explorer"){
		    request.onreadystatechange = function(){submitReturnIE();};
		  }else{ 
		    request.onreadystatechange=function(){
		      if ((request.readyState == 4) && (request.status == 200)){
		          if (request.responseText == 'sent'){
		            form = document.getElementById("blogForm");
		            errorMessage.innerHTML = "Story submitted successfully";
		            errorMessage.className = "";
		            form.reset();
		          }else if (request.responseText == "failed") {
		            errorMessage.innerHTML = "Story was not submitted!<br />Please try again.";
		            errorMessage.className="";
		          }
		      }
		    }
		  }  
		
		  var title = document.getElementById("storyTitle").value;
		  var story = document.getElementById("storyText").value;
		  var role = document.getElementById("storyRole").value;
		  var location = document.getElementById("storyLoc").value;
		  
		  if (emailConfirm == true)
			  	var email = document.getElementById("storyEmail").value;
		  else if (emailConfirm == false)
				var email = "noEmail";
		  	
		  var params = "title=" + title + "&story=" + story + "&role=" + role + "&location=" + location + "&email=" + email;
		  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		  request.setRequestHeader("Content-length", params.length);
		  request.setRequestHeader("Connection", "close");
		  request.send(params);
		}
	}
}

function submitReturnIE(){
	if ((request.readyState == 4) && (request.status == 200)){
		if (request.responseText == 'sent'){
         form = document.getElementById("blogForm");
         errorMessage.innerHTML = "Story submitted successfully";
         errorMessage.className = "";
         form.reset();
       }else if (request.responseText == "failed") {
         errorMessage.innerHTML = "Story was not submitted!<br />Please try again.";
         errorMessage.className="";
       }
  }
}

function insertTag(tagStart, tagEnd){
	var storyText = document.getElementById("storyText");
	if (storyText.setSelectionRange){
		storyText.value = storyText.value.substring(0,storyText.selectionStart) + tagStart + storyText.value.substring(storyText.selectionStart, storyText.selectionEnd) + tagEnd + storyText.value.substring(storyText.selectionEnd, storyText.value.length);
	} else if (document.selection){
		var range = document.selection.createRange(); 
		var stored_range = range.duplicate(); 
		stored_range.moveToElementText(storyText); 
		stored_range.setEndPoint( 'EndToEnd', range ); 
		storyText.selectionStart = stored_range.text.length - range.text.length; 
		storyText.selectionEnd = storyText.selectionStart + range.text.length;
			storyText.value = storyText.value.substring(0,storyText.selectionStart) + tagStart + storyText.value.substring(storyText.selectionStart,storyText.selectionEnd) + tagEnd + storyText.value.substring(storyText.selectionEnd,storyText.value.length) ; 
	} else if (document.selection && document.selection.createRange) { 
			alert("#2");
			storyText.focus(); 
			var range = document.selection.createRange(); 
			range.text = tagStart + tagEnd + range.text; 
		}
}
