﻿function ClickLogout() {
	if (window.XMLHttpRequest)
  	{
  		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=function()
		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			window.location.reload();
    		}
  		}
  	
		xmlhttp.open("GET","logout.php",true);
		xmlhttp.send();
	}
}

function OnClickLogin() {
	if (window.XMLHttpRequest)
  	{
  		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=function()
		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			if (xmlhttp.responseText=="Valid") {
    				window.location.reload();
    			} else {
    				//alert("Invalid username or password!");
    				alert(xmlhttp.responseText);	
    			}
    		}
  		}	
	
		var username = document.getElementById('loginname').value;
		var password = SHA1(document.getElementById('loginpassword').value);
		
		var params = "username="+username+"&password="+password;
	
		xmlhttp.open("POST", "login.php", true);

		//Send the proper header information along with the request
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");

		xmlhttp.send(params);
	}	
}

function KeyPress() {
	if (event.keyCode==13) {
		OnClickLogin();	
	}
}


function SubmitPost() {
	if (window.XMLHttpRequest)
  	{
  		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=function()
		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			if (xmlhttp.responseText=="Success") {
    				window.location.reload();
    			} else {
    				alert(xmlhttp.responseText);	
    			}
    		}
  		}	
	
		var msg = encodeURIComponent(document.getElementById('post').value);
		var thread = encodeURIComponent(document.getElementById('thread').value);
		
		var params = "post="+msg+"&thread="+thread;
	
		xmlhttp.open("POST", "submitpost.php", true);

		//Send the proper header information along with the request
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");

		xmlhttp.send(params);
	}		
}


function EditPost() {
	if (window.XMLHttpRequest)
  	{
  		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=function()
		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			if (xmlhttp.responseText=="Success") {
    				//window.location.reload();
    				qs();
    				var tid = qsParm['t'];
    				var page = qsParm['tp'];
    				window.location = "index.php?p=forums&t=" + tid + "&tp=" + page;
    			} else {
    				alert(xmlhttp.responseText);	
    			}
    		}
  		}	
	
		var msg = encodeURIComponent(document.getElementById('post').value);
		var edit = encodeURIComponent(document.getElementById('postid').value);
		
		var params = "post="+msg+"&edit="+edit;
	
		xmlhttp.open("POST", "submitpost.php", true);

		//Send the proper header information along with the request
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");

		xmlhttp.send(params);
	}		
}
/*
Trying some stuff
*/

function PreviewPost(){

/* so what would be good is if this grabbed FormattedBody()
	aka it has to for the idea we want.
	
	so it needs to do a request? through ajax? dunno how to do that.
	
	or we could just change the formatting to be html instead of [] 
	
*/
	//AJAX REQUEST
	var postInput = encodeURIComponent(document.getElementById('post').value);
	
	xmlhttp=new XMLHttpRequest();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
    		document.getElementById('preview').innerHTML=xmlhttp.responseText;
		}	
	}
	
	
	
	xmlhttp.open("GET","engine/PreviewPost.php?p="+postInput,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(postInput);
	//document.getElementById('preview').innerHTML=xmlhttp.responseText;
	
	//document.getElementById('preview').innerHTML = post.value;
	//return true;



}

function CreateThread() {
	if (window.XMLHttpRequest)
  	{
  		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=function()
		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			if (IsNumeric(xmlhttp.responseText)) {
    				window.location = "index.php?p=forums&t=" + xmlhttp.responseText;
    			} else {
    				alert(xmlhttp.responseText);	
    			}
    		}
  		}	
	
		var tle = document.getElementById('title').value;		
		var msg = document.getElementById('post').value;
		var section = document.getElementById('section').value;
		
		
		var params = "title="+tle+"&post="+msg+"&section="+section;
	
		xmlhttp.open("POST", "createthread.php", true);

		//Send the proper header information along with the request
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");

		xmlhttp.send(params);
	}		
}

function IsNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}

function EditorTextBold() {
	EditorUpdateText("[b]","[/b]");
}

function EditorTextItalic() {
	EditorUpdateText("[i]","[/i]");
}

function EditorTextCenter() {
	EditorUpdateText("[center]","[/center]");
}

function EditorTextJustify() {
	EditorUpdateText("[justify]","[/justify]");
}

function EditorTextImage() {
	EditorUpdateText("[img]","[/img]");
}

function EditorTextUrl() {
	EditorUpdateText("[url]","[/url]");
}

function EditorUpdateText(opentag,closetag)
{
	var txtarea = document.getElementById('post');
	
	var data = txtarea.value;

	var begintxt = data.substring(0,txtarea.selectionStart);
	var middletxt = data.substring(txtarea.selectionStart,txtarea.selectionEnd);
	var endtext = data.substring(txtarea.selectionEnd);

	txtarea.value = begintxt + opentag + middletxt + closetag + endtext;
}

var qsParm = new Array();
function qs() {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
}

