﻿$(document).ready(function() {
	commentVhomeValidator();
});
function InsertWord(word) {
	var word = word + ' '; 
	field = document.getElementById('txtComment');
	if (document.selection) {
		field.focus();
		var sel = document.selection.createRange();
		sel.text = word;
		sel.select();
	} else if (field.selectionStart || field.selectionStart == '0') {
		var start = field.selectionStart;
		var end = field.selectionEnd;
		var scroll = field.scrollTop;
		field.value = field.value.substring(0, start) + word + field.value.substring(end, field.value.length);
		field.focus();
		field.selectionStart = start + word.length;
		field.selectionEnd = start + word.length;
		field.scrollTop = scroll;
	} else {
	field.value += word;
	field.focus();
	}
}


uiemoticon = function(pdomain) {	
	$.getJSON(
		HOST_API + 'uiemoticon?callback=?',
		{domain:pdomain},
		function(response) {
			$.blockUI({
			message: response,
			css: {
				background: 'none',
				border: 'none',
				top:  ($(window).height() - 300)/2 + 'px',
				left: ($(window).width() - 400)/2 + 'px',
				cursor: 'pointer'
				}
			});
		}
	);
}

function commentVhomeValidator() {
	$("#frmVhomeComment").validate({
		errorClass		: 'ErrorMessage',
		errorElement	: 'span',
		onfocusout		: false,
		onkeyup			: false,
		rules: {
			txtComment:{
				checkVhomeComment: true
			},
			txtVcommentCaptcha:{
				validateVhomeCommentCaptcha: true
			}
		},
		messages: {
			txtComment:{
				checkVhomeComment : com_msg_invalidcontent
			},
			txtVcommentCaptcha:{
				validateVhomeCommentCaptcha: com_msg_invalidcaptcha
			}
		}
	});
	$.validator.addMethod('checkVhomeComment', function() {
		//var strIcon = $('#cbEmoticon option:selected').val();strIcon == '' &&
		var strComment = stripTags($('#txtComment').val(), 'comment_vhome');
		if(  strComment == '' ) return false;
		return true;
	});
	$.validator.addMethod('validateVhomeCommentCaptcha', function() {
		var strCaptchaInput = $('#txtVcommentCaptcha').val();
		return validateCaptcha(strCaptchaInput);
	});
}
function showUIGift(username) {
	if(username) {
		$.get(
			HOST_HAT + 'ajax/uigift?username=' + username,
			function(html){
				$.blockUI({
					message: html,
					css: {
						border: 'none',
						top:  ($(window).height() - 400)/2 + 'px',
						left: ($(window).width() - 600)/2 + 'px',
						cursor: 'pointer'
					}
				});
			}
		);
	}
}
function uiRecorder() {
	$.getJSON (
		HOST_HAT + 'ajax/uirecorder',
		function(html) {
			$.blockUI({
				message: html,
				css: {
					background: 'none',
					border: 'none',
					top:  ($(window).height() - 300)/2 + 'px',
					left: ($(window).width() - 500)/2 + 'px',
					cursor: 'pointer'
				}
			});
		}
	);
}
uimfquestion = function(pusername) {
	$.get (
		HOST_HAT + 'ajax/uimfquestion?username='+pusername,
		function(html) {
			$.blockUI({
				message: html,
				css: {
					background: 'none',
					border: 'none',
					top:  ($(window).height() - 500)/2 + 'px',
					left: ($(window).width() - 500)/2 + 'px',
					cursor: 'pointer'
				}
			});
		}
	);
}
uimfanswerresult = function(pquestionid) {
	$.get (
		HOST_HAT + 'ajax/uimfanswerresult?questionid='+pquestionid,
		function(html) {
			$.blockUI({
				message: html,
				css: {
					background: 'none',
					border: 'none',
					top:  ($(window).height() - 500)/2 + 'px',
					left: ($(window).width() - 640)/2 + 'px',
					cursor: 'pointer'
				}
			});
		}
	);
}
uimfanswer = function(pusername, pquestionid, pparentid) {
	$.get (
		HOST_HAT + 'ajax/uimfquestion?username='+pusername + '&questionid='+pquestionid + '&parentid='+pparentid,
		function(html) {
			$.blockUI({
				message: html,
				css: {
					background: 'none',
					border: 'none',
					top:  ($(window).height() - 500)/2 + 'px',
					left: ($(window).width() - 500)/2 + 'px',
					cursor: 'pointer'
				}
			});
		}
	);
}
deletemfquestion = function(pquestionid, page) {
	if (confirm(vhome_msg_confirmdeletemfquestion)) {
		$.post (
			HOST_HAT + 'ajax/deletemfquestion?isajax=1&page='+page,
			{questionid: pquestionid},
			function (response) {
				$('#questionItems').html(response);
			}
		);
	}
}
showmfanswer = function (username, ausername, questionid, isread, parentid, type, isshowresult) {
	if (parentid == 0) {
		var display = $("#questionAnswerItems_"+questionid).css('display');
		if ($("#questionAnswerItems_" + questionid).html()=='' || isshowresult == 1) {
			$("#questionAnswerItems_"+questionid).css('display','block');
			$.get(HOST_HAT + 'ajax/mfanswer?username='+username+'&ausername='+ausername + '&questionid='+questionid + '&isread='+isread + '&parentid='+parentid + '&type='+type + '&isshowresult='+isshowresult, function(result){$("#questionAnswerItems_" + questionid).html(result);});
		} else if(display == 'none') {
			$("#questionAnswerItems_"+questionid).css('display','block');
		} else {
			$("#questionAnswerItems_"+questionid).css('display','none');
		}
	} else {
		var display = $("#subAnswer_"+parentid).css('display');
		if ($("#subAnswer_"+parentid).html() == '') {
			$("#updown_"+parentid).attr('src', HOST_STATIC+'img/icon/makefriend/bnt_hide.gif');
			$("#updown_"+parentid).attr('title', 'Thu gọn');			
			$.get(
				HOST_HAT + 'ajax/mfanswer?username='+username+'&ausername='+ausername+'&questionid='+questionid + '&isread='+isread + '&parentid='+parentid + '&type='+type + '&isshowresult='+isshowresult,
				function(result){
					$("#subAnswer_"+parentid).html(result);
				}
			);
		} else if(display == 'none') {
			$("#updown_"+parentid).attr('src', HOST_STATIC+'img/icon/makefriend/bnt_hide.gif');
			$("#updown_"+parentid).attr('title', 'Thu gọn');			
			$("#subAnswer_"+parentid).css('display','block');
		} else {
			$("#updown_"+parentid).attr('src', HOST_STATIC+'img/icon/makefriend/bnt_unhide.gif');
			$("#updown_"+parentid).attr('title', 'Xem chi tiết');			
			$("#subAnswer_"+parentid).css('display','none');
		}
	}
}
deletemfanswer = function (pownerusername, pusername, panswerid, pparentid, pquestionid, ppage, ppagename) {
	if (confirm(vhome_msg_confirmdeletemfanswer)) {
		$.post(
			HOST_HAT + 'ajax/deletemfanswer',
			{username:pownerusername, questionid:pquestionid, answerid:panswerid, parentid:pparentid, page:ppage, pagename:ppagename},
			function(response) {				
				if (ppagename == '') {
					var numAnswer = parseInt($("#spanNumAnswer_"+pquestionid).html());
					if (pownerusername != pusername) {
						$("#spanNumAnswer_"+pquestionid).html(numAnswer-1);
					}
					if (pparentid == 0) {						
						$("#questionAnswerItems_"+pquestionid).html(response);					
					} else {
						$('#subAnswer_'+pparentid).html(response);
					}
				} else {					
					location.reload(); 
				}
			}
		);
	}
}
votemfanswer = function(pusername, panswerid, pparentid, pquestionid, ptype, newpoint, ppage, ppagename) {
	$.getJSON (
		HOST_HAT + 'ajax/votemfanswer?callback=?',
		{username:pusername, answerid:panswerid, parentid:pparentid, questionid:pquestionid, type:ptype, page:ppage, pagename:ppagename},
		function (response) {
			if (ppagename == '') {
				var error = response['error'];
				var msg = response['msg'];
				if (error == 0) {
					if (pparentid == 0)
						$('#answerPoint_'+panswerid).html(newpoint);
					else
						$('#subAnswerPoint_'+panswerid).html(newpoint);
					showUIMessage(msg);
				} else {
					alert(msg);
				}
			} else {				
				location.reload(); 
			}
		}
	);
}
votemfquestion = function(pusername, pquestionid, ptype, newpoint) {
	$.getJSON (
		HOST_HAT + 'ajax/votemfquestion?callback=?',
		{username:pusername,questionid:pquestionid,type:ptype},
		function (response) {
			var error = response['error'];
			var msg = response['msg'];
			if (error == 0) {
				$('#questionPoint_'+pquestionid).html(newpoint);
				showUIMessage(msg);
			} else {
				alert(msg);
			}
		}
	);
}
replyanswer = function(pparentid) {
	$("#replyanswer_"+pparentid).css('display','block');
	$("#txtSubAnswer_"+pparentid).focus();
}