var http_request = false;
function get_httpRequest()
	{
		var http_request=null;
		if (navigator.userAgent.indexOf("MSIE")>=0)
			{ 
				var strName="Msxml2.XMLHTTP"
				if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
					{strName="Microsoft.XMLHTTP"} 
				try
					{ 
						http_request=new ActiveXObject(strName)
						http_request.onreadystatechange=step_three;
						return http_request
					} 
				catch(e)
					{ 
						alert("Error. Scripting for ActiveX might be disabled") 
						return 
					} 
			} 
		if (navigator.userAgent.indexOf("Mozilla")>=0)
			{
				http_request = new XMLHttpRequest()
				http_request.onload		=	step_three;
				http_request.onerror	=	step_three;	
				return http_request;			
			}
	}
function step_two(postpage, postmethod, parameters)
	{		
		http_request = get_httpRequest();
		if(postmethod == "POST")
			{
				http_request.open(postmethod, postpage, false);
				http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				http_request.setRequestHeader("Content-length", parameters.length);
				http_request.setRequestHeader("Connection", "close");
				http_request.send(parameters);
			}
		else
			{
				http_request.open(postmethod, postpage + "?" + parameters, false);
				http_request.send(null);
			}
	}
function step_three()
	{
		
		var div = document.getElementById("which_div").value;
		var use_class = document.getElementById("use_class").value;
		if(http_request.readyState == 4 || http_request.readyState == "complete")
			{   
			
				if(http_request.status == 200)
					{
						result = http_request.responseText;

						/* If we're in a blog post, clear the form, show the success, update comment count, fade in and DON'T update class */
						if(document.getElementById("add_comment").value == "1")
							{								
								document.getElementById("name").value="";
								document.getElementById("email").value="";
								document.getElementById("link").value="";
								document.getElementById("comment").value="";
								document.getElementById("comment_info").innerHTML = "<p class=\"success\">Comment Posted Successfully!</p>";
								document.getElementById("comment_count").innerHTML = ((document.getElementById("comment_count").innerHTML/1) + 1);
								document.getElementById(div).className = "no_display";
								document.getElementById(div).innerHTML = document.getElementById(div).innerHTML + result;
								fadeMe(div, 0, 100, 2500, use_class);
							}
						else
							{
								document.getElementById(div).className = use_class;
								document.getElementById(div).innerHTML = result;
								document.getElementById("comment_info").innerHTML = "";
							}
					}
				else
					{alert("There was a problem with the request.");}
			
			}
		
		else{}
	}
   
function post_comment()
	{
		var err = "";
		var e_value = document.getElementById("email").value;
		if(document.getElementById("name").value == "")
			{err = err + "\n - Fill in a name in the name field.";}
		if(e_value !== "" && e_value.indexOf("@") !== -1 && e_value.indexOf("@.") == -1 && e_value.indexOf("@@") == -1 && ( e_value.indexOf(",") == -1 && e_value.indexOf("/") == -1 && e_value.indexOf("'") == -1 && e_value.indexOf("&") == -1 && e_value.indexOf("%") == -1 ))
			{}
		else
			{err = err + "\n - Fill in a valid e-mail address.";}
		if(document.getElementById("comment").value == "")
			{err = err + "\n - Fill in a comment in the comment field.";}
			
		if(err !== "")
			{err = "You comment was not posted, please correct the following: \n" + err; alert(err);}
		else
			{				
				var use_comment = document.getElementById("comment").value.replace(/'/g, "-apos-")
				use_comment = use_comment.replace(/&/g, "-amp-");
				use_comment = use_comment.replace(/\"/g, "-quot-");
				var postPage = "refresh_comments.cfm";
				var postMethod = "POST";
				var postParameters = "menuId="+encodeURI(document.getElementById("menuid").value)+
										"&name="+encodeURI(document.getElementById("name").value.replace(/'/g, ""))+
										"&email="+encodeURI(document.getElementById("email").value)+
										"&link="+encodeURI(document.getElementById("link").value)+
										"&comment="+encodeURI(use_comment)+
										"&insert=1";
				if(document.getElementById("check_subscribe").checked == true)
					{postParameters = postParameters +"&check_subscribe="+encodeURI("1");}
				
				//Check which div we're updating AND which class we're applying to said div
				document.getElementById("which_div").value = "new_comments";
				document.getElementById("use_class").value = "comments_container";
				
				//Show some kind of loading text/image
				document.getElementById("comment_info").innerHTML = "<p class=\"note\">Posting your comment...</p>";
				
				//Apply Ajax
				setTimeout("step_two('"+postPage+"', '"+postMethod+"', '"+postParameters+"');", 500);
			}
   }
 
function delete_comment(comment_id)
	{
		var confirm_delete = confirm("Are you sure you want to delete this comment?");
		if(!confirm_delete)
			{}
		else
			{
				var postPage = "refresh_comments.cfm";
				var postMethod = "POST";
				var postParameters = "menuId="+encodeURI(document.getElementById("menuid").value)+
									"&id="+encodeURI(comment_id)+"&delete=1";
			}
		document.getElementById("which_div").value = "page_comments";
		document.getElementById("use_class").value = "comments_container";
		//Apply Ajax
		setTimeout("step_two('"+postPage+"', '"+postMethod+"', '"+postParameters+"');", 500);
   }
/*****************/
/* Comment Fading */
function fadeMe(id, opacStart, opacEnd, millisec, classTo)
	{
		 //Speed for each frame
	    var speed = Math.round(millisec / 100);
	    var timer = 0;
	    //Determine the direction for the blending, if start and end are the same nothing happens
	  	for(i = opacStart; i <= opacEnd; i++)
			{
				if(i == 5)
					{setTimeout("changeOpac(" + i + ",'" + id + "', '"+classTo+"')",(timer * speed));}
				else
					{setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));}
				timer++;
			}
	}

/**************************/
/* Competition Open/Close */

function expand_div(div, max_height, min_height)
	{
		try{
				var upordown = document.getElementById("upordown").value;
				var speed = 0.8;
				var current_height = document.getElementById(div).offsetHeight;
			}
		catch(e)
			{false}
		/* Open = upordown = 1*/
		if(upordown && upordown == 1)
			{
				for (var i = current_height; i < (max_height/1 + 1); i++)
					{setTimeout("set_height('" + div + "','" + i + "')", (i * speed));}
				document.getElementById("upordown").value = 0;
			}
		/* Close = upordown = 0*/
		else if(upordown && upordown == 0)
			{
				document.getElementById(div).style.height = min_height+"px";
				document.getElementById("upordown").value = 1;
				/*
					for (var j = current_height; j > 0; j--)
						{setTimeout("set_height('" + div + "','" + j + "')", (j * speed));}
					
				*/
			}
	}

function set_height(div, height)
	{document.getElementById(div).style.height = height+"px";}

// Change the Opacity of the object
function changeOpac(opacity, id, classTo)
	{
		if(classTo)
			{document.getElementById(id).className = classTo;}
	    var object = document.getElementById(id).style;
	    object.opacity = (opacity / 100);
	    object.MozOpacity = (opacity / 100);
	    object.KhtmlOpacity = (opacity / 100);
	    object.filter = "alpha(opacity=" + opacity + ")";		
	}
/*************************************/
