function getFlashMovie(movieName) { 
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName]; 
} 

function bkb_updatePolls(){
/*	var poll_pie = document.getElementById("poll_pie").GetVariable("var1");
	var poll_bar = document.getElementById("poll_bar").GetVariable("var1");

	if(poll_pie==""){
		bkb_submitPoll("137", "0");
	} else if (poll_bar==""){
		bkb_submitPoll("141", "0");
	}
		
	if(poll_pie=="" || poll_bar==""){
		window.setTimeout('bkb_updatePolls()', 250);
	}	
	*/	
}

function bkb_submitPoll(poll_id, answer_id){
	var url = "assets/p_polls.php";
	parameters = "P=" + poll_id;

	if (poll_id == "141"){
		var answer_index = document.getElementById('poll_bar_select').selectedIndex;
		var answer_value = document.getElementById('poll_bar_select').options[answer_index].value;
		if (answer_value == "0"){
			return false;
		} else {
			parameters += "&A=" + answer_value;
		}
	} else {
		parameters += "&A=" + answer_id;
	}

/*	test for existence of request object
 * if (request == null) {
		alert("Error creating request object!");
	}*/

	createRequest();
	request.onreadystatechange = bkb_updatePoll;
	request.open('POST', url, true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", parameters.length);
	request.setRequestHeader("Connection", "close");
	request.send(parameters);
}

function bkb_updatePoll(){
	if (request.readyState == 4) {
		if (request.status == 200) {
		/* Get the response from the server */
			var response 	= request.responseText;
			AR_response = response.split('|');
			
			var poll_id = AR_response[0];
			
			if (poll_id == "137"){
				getFlashMovie("poll_pie").sendTextToFlash(AR_response[1],AR_response[2],AR_response[3]); 
				document.getElementById("poll_pie").SetVariable("var1", AR_response[1]);
				document.getElementById("poll_pie").SetVariable("var2", AR_response[2]);
				document.getElementById("poll_pie").SetVariable("var3", AR_response[3]);

//				document.getElementById("poll_buttons_pie").style.display = "none";
				$("#poll_buttons_pie").fadeOut();
				
			} else if (poll_id == "141"){
				getFlashMovie("poll_bar").sendTextToFlash(AR_response[1],AR_response[2],AR_response[3],AR_response[4],AR_response[5],AR_response[6]); 
				document.getElementById("poll_bar").SetVariable("var1", AR_response[1]);
				document.getElementById("poll_bar").SetVariable("var2", AR_response[2]);
				document.getElementById("poll_bar").SetVariable("var3", AR_response[3]);
				document.getElementById("poll_bar").SetVariable("var4", AR_response[4]);
				document.getElementById("poll_bar").SetVariable("var5", AR_response[5]);
				document.getElementById("poll_bar").SetVariable("var6", AR_response[6]);
				
//				document.getElementById("poll_buttons_bar").style.display = "none";
				$("#poll_buttons_bar").fadeOut();
			} 
			

		} else {
//			alert("Error: status = " + request.status);
		}
	}
}

function bkb_ValidateComment() {

	var Comment = document.getElementById("Comment").value;
	
	if (Comment == "") {
		alert("Please enter your comments.");
		
	} else if (Comment == "Comment") {
		alert("Please enter your comments.");
		
	} else {
		bkb_comment_store();
	}
}

function bkb_ValidateReply(RID) {
	
	var Comment = document.getElementById("Comment"+RID).value;

	if (Comment == "") {
		alert("Please enter your comments.");
		
	} else if (Comment == "Comment") {
		alert("Please enter your comments.");
		
	} else {
		bkb_reply_store(RID);
	}
}

function player_pause(){
	getFlashMovie("playback").tellFlashPause();
}

function player_play(){
	getFlashMovie("playback").tellFlashPlay();
}

function comment_reply(RID, YID){

	$(".reply").remove();
	

	reply = '<div id="reply_'+RID+'" class="reply">';
	reply += '<form name="reply" action="assets/p_comments.php" method="post">';
	reply += '<input type="hidden" id="YID" name="YID" value="'+YID+'"/>';
	reply += '<input type="hidden" id="RID" name="RID" value="'+RID+'"/>';
	reply += '<textarea id="Comment'+RID+'" name="Comment'+RID+'" rows="2" cols="20" onfocus="ClearDefault(this);">Comment</textarea>';
	reply += '<p><a href="#" onclick="bkb_ValidateReply('+RID+'); return false;" onkeypress="bkb_ValidateReply('+RID+'); return false;">SUBMIT COMMENT</a></p>';
	reply += '</form>';
	reply += '<div class="clear"></div>';
	reply += '</div>';
	
	$("#comment_"+RID).after(reply);
	$("#Comment"+RID).focus();
}

function toggle_replies(RID, ReplyCount){

	$("#replies_for_"+RID).toggle("normal");
	
	if($("#show_replies_"+RID).text()=="Hide replies "){
		$("#show_replies_"+RID).text("Show replies ");
	}else{
		$("#show_replies_"+RID).text("Hide replies ");
	}
}

function more_info_toggle(){
	$("#info_copy").toggle();

	if($("#more_info").html()=="see less info"){
		$("#more_info").html("see more info");
	}else{
		$("#more_info").html("see less info");
	}

	if($("#more_info_plus").html()=="-"){
		$("#more_info_plus").html("+");
	}else{
		$("#more_info_plus").html("-");
	}
}

function bkb_comment_store(){
	UserID = $("#UID").val();
	YourSayID = $("#YID").val();
	Comment = $("#Comment").val();
	RID = "0";

	$.ajax({
		type: "POST",
		url: "assets/p_comments.php",
		data: "RID="+RID+"&UID="+UserID+"&YID="+YourSayID+"&Comment="+Comment,
		success: function(msg){
			AR_response = msg.split('|[:');
			YSMID = AR_response[0];
			Username = AR_response[1];
			Date_time = AR_response[2];
			Comment = AR_response[3];
			add_new_comment(RID, YSMID, Username, Date_time, Comment);
			$(".comments").html("<div class=\"button_big thankyou\"><p>THANK YOU.<br />YOUR COMMENT HAS <br />BEEN ADDED.</p></div>");
		}
	});
}

function bkb_reply_store(RID){
	UserID = $("#UID").val();
	YourSayID = $("#YID").val();
	Comment = $("#Comment"+RID).val();

	$.ajax({
		type: "POST",
		url: "assets/p_comments.php",
		data: "RID="+RID+"&UID="+UserID+"&YID="+YourSayID+"&Comment="+Comment,
		success: function(msg){
			// hide reply comment
			$(".reply").remove();
		
			old_count = ($("#reply_count_"+RID).text());
			old_count = old_count.substring(1,old_count.length-1)
			if (old_count == ""){old_count = 0};
			new_count = parseInt(old_count) + 1;
			
			$("#reply_count_"+RID).text("("+new_count+")");
			
			AR_response = msg.split('|[:');
			YSMID = AR_response[0];
			Username = AR_response[1];
			Date_time = AR_response[2];
			Comment = AR_response[3];
			add_new_reply(RID, YSMID, Username, Date_time, Comment);
			$("#replies_for_"+RID).show("normal");
			$("#show_replies_"+RID).text("Hide replies ");
		}
	});
}

function add_new_comment(RID, YSMID, Username, Date_time, Comment){
	output = '<div id="comment_'+YSMID+'>" class="bkb_message" >';
	output += '<p class="username">'+Username+'</p>';
	output += '<p class="datetime">'+Date_time+'</p>';
	output += '<div class="clear"></div><br />';
	output += '<p>'+Comment+'</p>';
	output += '<div class="clear"></div><br />';
	output += '<div class="clear"></div>';
	output += '</div>';
	
	$(".bkb_messages").prepend(output);
}

function add_new_reply(RID, YSMID, Username, Date_time, Comment){
	output = '<div id="comment_'+YSMID+'>" class="bkb_message_reply" style="margin-left: 20px">';
	output += '<p class="username">'+Username+'</p>';
	output += '<p class="datetime">'+Date_time+'</p>';
	output += '<div class="clear"></div><br />';
	output += '<p>'+Comment+'</p>';
	output += '<div class="clear"></div><br />';
	output += '<div class="clear"></div>';
	output += '</div>';
	
	$("#replies_for_"+RID).prepend(output);							
}