function getHTTPObject()
{
	if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) return new XMLHttpRequest();
	else {
		alert("Your browser does not support AJAX.");
		return null;
	}
} 

$(document).ready(function() 
{

	$('#login-box').fadeIn('normal');

	$('#newsletter_btn').click(function ()
	{ 
		saveNewsLetter();
    });
	$('#user_email').blur(function ()
	{
		$(this).value = '';
	});
	
	$('#answer-it').click(function()
	{
		showAnswerBox();
	});
	
	$('#hide_comments').click(function()
	{
		
		$('#buzz_comment_display').fadeOut('normal');
		$('#comment-display').html('<a id="show_comments">Show Comments</a>');
		
	});
	
	showOverlayBox();
	
	
	$('#forgot').click(function()
	{
		$('#forgot-pass-box').fadeIn('normal');
	});	
	
	$('#remembered').click(function()
	{
		$('#forgot-pass-box').fadeOut('normal');
	});		
	
});

$('#show_comments').click(function()
{
	$('#buzz_comment_display').fadeIn('normal');
	$('#comment-display').html('<a id="hide_comments">Hide Comments</a>');
});	


function showOverlayBox()
{
	$('#video-overlay-box').fadeIn('normal');
}


function showAnswerBox()
{
		$('#answer-box').fadeIn('normal');
}

/* BUZZ SECTION COMMENTS */
function saveBuzzComments(user_id)
{
	httpObject = getHTTPObject();
	uid = user_id;
	var cid = document.getElementById('buzz_content_id').value;
	var comm = document.getElementById('buzz_comment_box').value;
	
	if (httpObject != null) 
	{
		httpObject.open("GET", "/_base/static/ajax/save_buzz_comments.php?uid="+uid+'&cid='+cid+'&comm='+comm, true);
		httpObject.send(null);
		httpObject.onreadystatechange = function()
		{
			saveBuzzCommentResults();
	
		}
	}
}
function saveBuzzCommentResults()
{
	if(httpObject.readyState == 4)
	{
		document.getElementById('buzz_comment_box').value = '';
		$('.user_comments').fadeIn('normal');
		$('#answer-box').toggle('fast');				
		$('#buzz_comment_display').before(httpObject.responseText);
	
	}
}

/* VOTING SYSTEM */
function recycleIt(id, user_id)
{
	httpObject = getHTTPObject();
	obj = id;
	uid = user_id;
	if (httpObject != null) 
	{
		httpObject.open("GET", "/_base/static/ajax/recycleit.php?id="+obj+'&uid='+uid, true);
		httpObject.send(null);
		httpObject.onreadystatechange = function()
		{
			recycleItResults(obj);
		}
	}
}
function recycleItResults(my_id)
{
	if(httpObject.readyState == 4)
	{
		document.getElementById('recycle_count_'+my_id).innerHTML =  httpObject.responseText;
		document.getElementById('recycle_'+my_id).innerHTML =  '<img src="/_base/static/img/recycle_icon.png" border="0"> Recycled';		
	}
	
}

/* NEWSLETTER SYSTEM */
function saveNewsLetter()
{
	httpObject = getHTTPObject();
	var email = document.getElementById('user_email').value;

	if (httpObject != null) 
	{
		httpObject.open("GET", "/_base/static/ajax/newsletter.php?email="+email, true);
		httpObject.send(null);
		httpObject.onreadystatechange = function()
		{
			saveNewsLetterResults();
		}
	}
}

function saveNewsLetterResults(my_id)
{
	if(httpObject.readyState == 4)
	{
		document.getElementById('newsletter').innerHTML =  httpObject.responseText;
	}
	
}


/* ANSWERS*/
function saveAnswers(user_id)
{
	httpObject = getHTTPObject();
	uid = user_id;
	var qid = document.getElementById('answer_content_id').value;
	var comm = document.getElementById('answer_comment_box').value;
	
	if (httpObject != null) 
	{
		httpObject.open("GET", "/_base/static/ajax/save_answers.php?uid="+uid+'&qid='+qid+'&comm='+comm, true);
		httpObject.send(null);
		httpObject.onreadystatechange = function()
		{
			saveAnswerResults();
		}
	}
}
function saveAnswerResults()
{
	if(httpObject.readyState == 4)
	{
		document.getElementById('answer_comment_box').value = '';
		$('.user_comments').fadeIn('normal');
		$('#buzz_comment_display').before(httpObject.responseText);
	
	}
	
}



/* BLOG COMMENTS */
function saveBlabComments(user_id)
{
	httpObject = getHTTPObject();
	uid = user_id;
	var cid = document.getElementById('blab_content_id').value;
	var comm = document.getElementById('blab_comment_box').value;
	
	if (httpObject != null) 
	{
		httpObject.open("GET", "/_base/static/ajax/save_blab_comments.php?uid="+uid+'&cid='+cid+'&comm='+comm, true);
		httpObject.send(null);
		httpObject.onreadystatechange = function()
		{
			saveBlabCommentResults();
		}
	}
}
function saveBlabCommentResults()
{
	if(httpObject.readyState == 4)
	{
		document.getElementById('blab_comment_box').value = '';
		$('.user_comments').fadeIn('normal');
		$('#blab_comment_display').before(httpObject.responseText);
	
	}
	
}

/* VIDEO SECTION COMMENTS */
function saveVideoComments(user_id)
{
	httpObject = getHTTPObject();
	uid = user_id;
	var cid = document.getElementById('video_content_id').value;
	var comm = document.getElementById('video_comments_box').value;
	
	if (httpObject != null) 
	{
		httpObject.open("GET", "/_base/static/ajax/save_video_comments.php?uid="+uid+'&cid='+cid+'&comm='+comm, true);
		httpObject.send(null);
		httpObject.onreadystatechange = function()
		{
			saveVideoCommentResults();
	
		}
	}
}
function saveVideoCommentResults()
{
	if(httpObject.readyState == 4)
	{
		document.getElementById('video_comments_box').value = '';
		$('.user_comments').fadeIn('normal');
		$('#answer-box').toggle('fast');				
		$('#buzz_comment_display').before(httpObject.responseText);
	
	}
}

function incrementVideoView(id)
{
	httpObject = getHTTPObject();
	vid = id;
	uip = document.getElementById('user_ip').value;
	if (httpObject != null) 
	{
		httpObject.open("GET", "/_base/static/ajax/update_video_views.php?vid="+vid+'&uip='+uip, true);
		httpObject.send(null);
		httpObject.onreadystatechange = function()
		{
			userIP();
	
		}		
	}
}

function userIP()
{
	if(httpObject.readyState == 4)
	{
	
	}
}




function votePoll()
{
	var answers = document.poll_form.poll_answer;
	var pass = 'no';
	var answer_id = "";
	for (x=0;x<answers.length;x++)
	{
		if (answers[x].checked)
		{
			 pass = 'yes';
			 answer_id = answers[x].value;
		}
	}
	
	
	if (pass=='yes')
	{
		saveVote(answer_id);
	}
}





function saveVote(answer_id)
{
	httpObject = getHTTPObject();
	var ajaxDisplay = document.getElementById("poll_container");
	
	
	if (httpObject != null) 
	{
		var queryString = "?paid="+answer_id+"&pqid="+document.getElementById("poll_question_id").value+"&do=submit";
		httpObject.open("GET", "/_base/static/ajax/poll_submit.php" + queryString, true);
		httpObject.send(null);
		httpObject.onreadystatechange = function()
		{
			if(httpObject.readyState == 4)
			{
				getPollResults(document.getElementById("poll_question_id").value);	
			}		
	
		}		
	}	

}

function getPollResults(pqid)
{
	httpObject = getHTTPObject();
	
	var ajaxDisplay = document.getElementById("poll_container");
	
	if (httpObject != null) 
	{
		var queryString = "?pqid="+pqid;
		httpObject.open("GET", "/_base/static/ajax/poll_results.php" + queryString, true);
		httpObject.send(null);
		httpObject.onreadystatechange = function()
		{
			if(httpObject.readyState == 4)
			{
				ajaxDisplay.innerHTML = httpObject.responseText;
				document.getElementById("poll_option_links").innerHTML = "";
			}		
	
		}		
	}	

}
