function delete_wiki(wiki_id,id){
	orig_name = document.getElementById('name'+id).value; 
	if (confirm("Are you sure you want to delete "+orig_name+"?")) {
		document.getElementById("del"+id).innerHTML = "<img src=\"images/loading.gif\"><br/>deleting...";
		delete_xmlhttpPost("ajax/delete.php",wiki_id,id);

		colorTween = new ColorTween(document.getElementById("tr"+id).style, 'backgroundColor', Tween.regularEaseOut , 'AACF8E', 'ffffff', 15);
		colorTween.start();
		setTimeout('document.getElementById("tr'+id+'").style.display = "none"', 3000);
	}
}

function delete_xmlhttpPost(strURL,wiki_id,id) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
// 	document.getElementById(elementID).innerHTML = outputString;	
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById('del'+id).innerHTML = self.xmlHttpReq.responseText;
			}
		}
		self.xmlHttpReq.send('input= ' + wiki_id);
		
}

function allow_edit(wiki_id, id, orig_name, orig_desc){
	document.getElementById("name"+id).readOnly = false;
	document.getElementById("desc"+id).readOnly = false;
 	document.getElementById("name"+id).style.backgroundColor = "#fff";
 	document.getElementById("desc"+id).style.backgroundColor = "#fff";
	document.getElementById("edit"+id).innerHTML = "<a href=\"javascript:cancel_edit('"+wiki_id+"','"+id+"','"+orig_name+"','"+orig_desc+"');\">cancel</a> <a href=\"javascript:save_edit('"+id+"','"+wiki_id+"');\">save</a>";
}

function cancel_edit(wiki_id,id, orig_name, orig_desc){
	document.getElementById("name"+id).value = orig_name;
	document.getElementById("desc"+id).value = orig_desc;
	document.getElementById("name"+id).readOnly = true;
	document.getElementById("desc"+id).readOnly = true;
 	document.getElementById("name"+id).style.backgroundColor = "#E5EFD1";
 	document.getElementById("desc"+id).style.backgroundColor = "#E5EFD1";
	document.getElementById("edit"+id).innerHTML = "<img src=\"images/edit.gif\" onclick=\"allow_edit('"+wiki_id+"','"+id+"','"+orig_name+"','"+orig_desc+"')\">";
}

function save_edit(id,wiki_id){
	document.getElementById("name"+id).readOnly = true;
	document.getElementById("desc"+id).readOnly = true;
 	document.getElementById("name"+id).style.backgroundColor = "#E5EFD1";
 	document.getElementById("desc"+id).style.backgroundColor = "#E5EFD1";
	document.getElementById("edit"+id).innerHTML = "<img src=\"images/loading.gif\"><br/>saving...";
	save_xmlhttpPost("ajax/save.php",wiki_id,id);	
}

function save_xmlhttpPost(strURL,wiki_id,id) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
// 	document.getElementById(elementID).innerHTML = outputString;	
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
					document.getElementById('edit'+id).innerHTML = self.xmlHttpReq.responseText;
				 	orig_name = document.getElementById('name'+id).value;
				 	orig_desc = document.getElementById('desc'+id).value;

					document.getElementById("edit"+id).innerHTML = "<img src=\"images/edit.gif\" onclick=\"allow_edit('"+wiki_id+"','"+id+"','"+orig_name+"','"+orig_desc+"')\">";
			}
		}
		self.xmlHttpReq.send(save_getQueryString(wiki_id,id));
		
}

function save_getQueryString(wiki_id,id) {
	inputString = wiki_id +"|"
				 +document.getElementById('name'+id).value +"|"
				 +document.getElementById('desc'+id).value +"|"

	qstr = 'input= ' + inputString;
	return qstr;
}

function logout(){
		logout_xmlhttpPost("ajax/logout.php");
}

function logout_xmlhttpPost(strURL) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
					window.location = "index.php";
			}
		}
		self.xmlHttpReq.send(0);
		
}

function login_xmlhttpPost(strURL,elementID,outputString) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	document.getElementById(elementID).innerHTML = outputString;	
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
					document.getElementById(elementID).innerHTML = self.xmlHttpReq.responseText;
					if(self.xmlHttpReq.responseText == "Login <span style=\"color:#AF0017;\">SUCCESSFUL</span>."){
						window.location = "admin.php";
					}
			}
		}
		self.xmlHttpReq.send(login_getQueryString());
		
}

function login_getQueryString() {
	inputString = document.getElementById('Username').value +"|"
				 +document.getElementById('password').value+"|"

	qstr = 'input= ' + inputString;
	return qstr;
}

function login_form(){	
	document.getElementById('logindiv').style.display = "block";
	addForm = ''+
	'<div id="loginform">'+
		'<table>'+
			'<tr>'+
				'<td></td>'+
				'<td><a href="javascript: close_loginform();" id="close">close</a></td>'+
			'</tr>'+
			'<tr>'+
				'<td>Username:</td>'+
				'<td><input type="text" size="15" id="Username"></td>'+
			'</tr>'+
			'<tr>'+
				'<td>Password:</td>'+
				'<td><input type="password" size="15" id="password"></td>'+
			'</tr>'+
			'<tr>'+
				'<td></td>'+
				'<td align="center"><input type="image" src="images/login.gif" onclick="login()"></td>'+
			'</tr>'+
		'</table>'+
		'<div id="login_message"></div>'+
	'</div>'+
	'';
	document.getElementById('logindiv').innerHTML = addForm;
	
}

function close_loginform(){
	document.getElementById('logindiv').style.display = "none";
}

function check_login(){
	if(trimAll(document.getElementById('Username').value)  == ""){
		return "Please enter a <span style=\"color:#AF0017;\">USERNAME</span>.";
	}	
	if(document.getElementById('password').value  == ""){
		return "Please enter your <span style=\"color:#AF0017;\">PASSWORD</span>.";
	}
	else{
		return true;
	}
}

function login(){
	if(check_login() == true){
		login_xmlhttpPost("ajax/login.php","login_message","<img src=\"images/loading.gif\">");
	}
	else{
		document.getElementById('login_message').innerHTML = check_login();
	}
}