var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
  try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) {
    xmlhttp = false;
   }
  }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}

function sendRequest(url) {
    var result;
	xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4) {
		result = String(xmlhttp.responseText);
	  }
    }
    xmlhttp.send(null);

    return 1;
}

var currentlyEditing = new Array();
var currentlyEditingTopics = new Array();

/*****
	FOR TOPICS
*****/

function editTopic(topicid) {
	for(i in currentlyEditingTopics) {
		if(currentlyEditingTopics[i] == topicid) {
			return;
		}
	}
	currentlyEditingTopics.push(topicid);
	var elem = $('topic_' + topicid);
	convertEditableTopic(topicid);
	return;
}

function convertEditableTopic(topicid) {
	
	new ajax ('/forum/ajax/topic_fetch.php' , {
		postBody: 'id='+ topicid + '&mode=bbcode', 
		update: '', 
		onComplete: ajaxMakeEditableTopic
	});
		
	return;
}

function ajaxMakeEditableTopic(request) {
	
	var php = new PHP_Serializer(true);
	if(!request || request.responseText != '-1') {
	
		if(request.responseText) {
			info = php.unserialize(request.responseText);
			fieldName = 'topic_' + info['id'];
			
			if(info['mode'] == 'bbcode')
				$(fieldName).innerHTML = "<input type='text' size='40' name='description' id='edit_" + info['id'] + "' value='" + info['body'] + "'>\n" +
				"<br />" +
				"\n<input type='button' value='Update Topic' onClick='editTopicCommit(\"" + info['id'] + "\")' /> <input type='button' value='Never Mind' onClick='unsetEditableTopic(\"" + info['id'] + "\")' /> ";
			else
				$(fieldName).innerHTML = info['body'];
		}

	} else {
		//Not logged in, send them to the login page
		document.location = '/login.php';
	}
	
	return;
}

function unsetEditableTopic(topicid) {
	
	for(i in currentlyEditingTopics) {
		if(currentlyEditingTopics[i] == topicid) {
			currentlyEditingTopics.splice(i,1);
			break;
		}
	}
	
	new ajax ('/forum/ajax/topic_fetch.php' , {
		postBody: 'id='+ topicid + '&mode=html', 
		update: '', 
		onComplete: ajaxMakeEditableTopic
	});

	return;
}

function editTopicCommit(topicid) {
	var copy = $('edit_' + topicid).value;
	
	new ajax ('/forum/ajax/topic_edit.php' , {
		postBody: 'id='+ topicid + '&body=' + encodeURI(copy), 
		update: '', 
		onComplete: ajaxEditTopicCommit
	});
	
	
	for(i in currentlyEditingTopics) {
		if(currentlyEditingTopics[i] == topicid) {
			currentlyEditingTopics.splice(i,1);
			break;
		}
	}
	
	return;
}

function ajaxEditTopicCommit(request) {
	var php = new PHP_Serializer(true);
	
	if(!request || request.responseText != '-1') {
		if(request.responseText) {
			info = php.unserialize(request.responseText);
			fieldName = 'topic_' + info['id'];
			$(fieldName).innerHTML = info['body'];
		}		
	}
	
	return;
}


/*****
	POSTS
*****/

function editPost(postID) {
	for(i in currentlyEditing) {
		if(currentlyEditing[i] == postID) {
			return;
		}
	}
	currentlyEditing.push(postID);
	convertEditable2(postID);
	return;
}

function convertEditable2(postid) {
	
	new ajax ('/forum/ajax/post_fetch.php' , {
		postBody: 'id='+ postid + '&mode=bbcode', 
		update: '', 
		onComplete: ajaxMakeEditable
	});
		
	return;
}

function ajaxMakeEditable(request) {
	
	var php = new PHP_Serializer(true);
	if(!request || request.responseText != '-1') {
	
		if(request.responseText) {
			info = php.unserialize(request.responseText);
			fieldName = 'body_' + info['id'];
			if(info['mode'] == 'bbcode')
				$(fieldName).innerHTML = "<form action='' method='get'>" +
						"\n<textarea name='body' rows='8' cols='40' id='edit_" + info['id'] + "'>\n" +
						info['body'] +	"\n</textarea><br />" +
						"\n<input type='hidden' id='hd_" + info['id'] + "' value='" + info['body'] + "' />" +
						"\n<input type='button' value='Update Post' onClick='editPostCommit(\"" + info['id'] + "\")' /> <input type='button' value='Never Mind' onClick='unsetEditable(\"" + info['id'] + "\")' /> " +
						"\n</form>";
			else
				$(fieldName).innerHTML = info['body'];
		}

	} else {
		//Not logged in, send them to the login page
		document.location = '/login.php';
	}
	
	return;
}

function unsetEditable(postid) {
	
	for(i in currentlyEditing) {
		if(currentlyEditing[i] == postid) {
			currentlyEditing.splice(i,1);
			break;
		}
	}
	
	new ajax ('/forum/ajax/post_fetch.php' , {
		postBody: 'id='+ postid + '&mode=html', 
		update: '', 
		onComplete: ajaxMakeEditable
	});

	return;
}


function editPostCommit(postid) {
	var copy = encodeURI(document.getElementById('edit_' + postid).value);
	
	new ajax ('/forum/ajax/post_edit.php' , {
		postBody: 'id='+ postid + '&body=' + copy, 
		update: '', 
		onComplete: ajaxEditPostCommit
	});
	
	
	for(i in currentlyEditing) {
		if(currentlyEditing[i] == postid) {
			currentlyEditing.splice(i,1);
			break;
		}
	}
	return;
}

function ajaxEditPostCommit(request) {
	var php = new PHP_Serializer(true);
	if(!request || request.responseText != '-1') {
		if(request.responseText) {
			info = php.unserialize(request.responseText);
			fieldName = 'body_' + info['id'];
			$(fieldName).innerHTML = info['body'];
		}		
	}
	
	return;
}


function convertEditable2(postid) {
	
	new ajax ('/forum/ajax/post_fetch.php' , {
		postBody: 'id='+ postid + '&mode=bbcode', 
		update: '', 
		onComplete: ajaxMakeEditable
	});
		
	return;
}


function deletePost(postid) {
	
	new ajax ('/forum/ajax/post_delete.php' , {
		postBody: 'id='+ postid, 
		update: '', 
		onComplete: ajaxDeletePost
	});
	
	return;
}

function ajaxDeletePost(request) {
	
	if(!request || request.responseText != '-1') {
		if(request.responseText) {
			bodyDiv = $('body_' + request.responseText);
			parentDiv = $('parent_' + request.responseText);
			bodyDiv.innerHTML = '<span style="color:#AA0000">This post been deleted.</span>';
			var divUpdate = function() {
				parentDiv.innerHTML = '';
			}
			
			setTimeout(divUpdate,1000);
		}
		
	}
	
	return;
}

function deleteTopic(elemid) {
	var conf = confirm("Delete this topic?");
	if(conf) {
		var elem = document.getElementById('topic_' + elemid);
		var url = "ajax/topic_delete.php?t=" + elemid;
		var result = sendRequest(url);
		var divUpdate = function() {
			elem.innerHTML = '<span style="color:#AA0000">(this topic will disappear when you refresh :) )</span>';
		}
		if(result) {
			elem.innerHTML = '<span style="color:#AA0000">This topic been deleted.</span>';
			setTimeout(divUpdate,1000);
		}
	}
	return;
}

function switchViews(on,off) {
	var elemOn = document.getElementById(on);
	var elemOff = document.getElementById(off);
	
	elemOff.style.display = 'none';
	elemOn.style.display = 'block';
	
	return;
}

function getMods(topicid) {
	var url = 'ajax/get_moderators.php?topic=' + topicid;
	xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4) {
		result = String(xmlhttp.responseText);
	  	document.getElementById('current_mods').innerHTML = result;
	  }
    }
    xmlhttp.send(null);

    return 1;
}

function remMod(modid, topicid) {
	var url = 'ajax/rem_moderator.php?topic=' + topicid + '&modid' + modid;
	xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4) {
		result = String(xmlhttp.responseText);
	  }
    }
    xmlhttp.send(null);
    return 1;
}

function displayULWindow() {
	window.open('/forum/img_upload.php','','scrollbars=no,menubar=no,height=400,width=600,resizable=yes,toolbar=no,location=no,status=no');
	return;
}
