jQuery.fn.autolink = function () {
	return this.each(function(){
		var re = /((http|https|ftp|www):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>)+(![=]*))/g;
		$(this).html( $(this).html().replace(re, '<a href="$1" target="_blank">$1</a>') );
	});
}
jQuery.fn.exists = function(){ return ($(this).length > 0); }

function iweb_common() {
	$(".cnt_image").fancybox({ 'autoScale': false });
	$("img.fancybox").each(function(){ $("img.fancybox").fancybox({ 'href': $(this).attr("ref"), 'autoScale': false }); });

	$(".iweb_ads").css("z-index", "0");
	$(".iweb_ads div").each(function(){
		$(this).css("z-index", "0");
		if($(this).css("display") == "inline") {
			$(this).css("display", "");
		}
	});
	
	$("object").attr("wmode", "opaque");
	$("object param[name='wmode']").attr("value", "opaque");
	$("object embed").attr("wmode", "opaque");

	$(".a_print").bind("click", function(event) { print_click(event); });
	$(".a_abuse").bind("click", function(event) { if(iweb_umode == "0") { err_event_unauthorized(event); } else { abuse_click(event); } });
	$(".a_sendarticle").bind("click", function(event) { if(iweb_umode == "0") { err_event_unauthorized(event); } else { sendarticle_click(event); } });
	$(".sendarticle_btn_submit").bind("click", function(event) { sendarticle_submit(event); });
	
	$("#iweb_bt_search").bind("click", function(event) { var keyword = $("#iweb_textbox_search").val(); var pageno = 1; iweb_search(event, keyword, pageno, false); });
	$("#iweb_textbox_search").keypress(function(event) { if(event.which == 13) { var keyword = $("#iweb_textbox_search").val(); var pageno = 1; iweb_search(event, keyword, pageno, false); } });
	
	if(iweb_umode == "0") {
		$("#bt_write_article").bind("click", function(event) { err_event_unauthorized(event); });
		$(".a_download").bind("click", function(event) { err_event_unauthorized(event); });
	}

	$.nyroModalSettings({
		 debug: false
		,modal: false
		,closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close">Close</a>'
		,processHandler: function(settings) {
			var url = settings.url;
			if(url && url.indexOf('/write') == 0) {
				$.nyroModalSettings({
					 height: 580
					,width: 820
					,modal: true
				});
			}
		}
		,endShowContent: function(elts, settings) {
			$('.resizeLink', elts.contentWrapper).click(function(e) {
				e.preventDefault();
				$.nyroModalSettings({
					width: Math.random()*1000,
					height: Math.random()*1000
				});
				return false;
			});
			$('.bgLink', elts.contentWrapper).click(function(e) {
				e.preventDefault();
				$.nyroModalSettings({
					bgColor: '#'+parseInt(255*Math.random()).toString(16)+parseInt(255*Math.random()).toString(16)+parseInt(255*Math.random()).toString(16)
				});
				return false;
			});
		}
	});
}

function iweb_featuretext() {
	$(".featuretext object").each(function(){
		var id = uniqid();
		$(this).attr("id", id);
		
		var src = $("#" + id + " param[name='src']").attr("value");
		var type = $(this).attr("type");
		var width = $(this).attr("width");
		var height = $(this).attr("height");
		
		if(!$("#" + id + " embed").exists() && type == "application/x-shockwave-flash") {
			var embedElem = document.createElement("embed");
			$(this).append($(embedElem).attr("src", src)
				.attr("type", type)
				.attr("allowscriptaccess", "always")
				.attr("allowfullscreen", "true")
				.attr("width", width)
				.attr("height", height)
				.attr("wmode", "opaque")
			);
			$(this).removeAttr("type");
			$(this).removeAttr("data");
		}
	});
	
	$(".featuretext").autolink();
	$(".featuretext a img").addClass("a_img");
	$(".featuretext a").each(function(){ $(this).attr("target", "_blank"); });

	$(".featuretext img").each(function(){
		if(!$(this).hasClass("a_img")
			&& $(this).attr("alt") != "#emoticon"
			&& $(this).attr("src").indexOf("tiny_mce") < 0) {
			$(this).removeAttr("width");
			$(this).removeAttr("height");
			$(this).addClass("cursor_pointer");
			$(this).attr("title", "คลิกเพื่อดูขนาดจริง");
			$(this).fancybox({ 'href': $(this).attr("src"), 'autoScale': false, 'titleShow': false });
		}
	});
}

function print_click(event) {
	event.preventDefault();
	window.print();
}

function fbs_click(event, u, t) {
	event.preventDefault();
	var url = 'http://www.facebook.com/sharer.php?u='+ u +'&t='+ t;
	var params = 'toolbar=0,status=0,width=626,height=436,share_url=' + u
	window.open(url,'fshare', params);
}

function rt_click(event, url) {
	event.preventDefault();
	window.open(url,'retweet','toolbar=0,status=0,width=626,height=436,scrollbars=1');
}

function nyromodal_parent_onclose(event) {
	 event.preventDefault();
	 parent.$.nyroModalRemove();
	 return false;
}

function sendarticle_click(event) {
	event.preventDefault();
	$(".div_sendarticle_msg").hide();
	$(".div_sendarticle").show();
}

function abuse_click(event) {
	event.preventDefault();
}

function cp_page_delete(event, page_id) {
	event.preventDefault();
	var elem = $(".a_page_delete");
	var text = $(elem).text();

	if($(elem).hasClass("page_cp_waiting")) {
		return false;
	}

	if (!confirm("Are you sure you want to " + text.toLowerCase() + " this post?")) {
		return false;
	}

	$(elem).addClass("page_cp_waiting");
	$(elem).text("");

	var plugin = "page_write";
	var action = "do_delete";
	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'page_id', value : page_id }
	];

	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK"){
				$(elem).removeClass("page_cp_waiting");
				$(elem).text(text);
				alert(iweb_lang.get(result.code));
			}
			else {
				window.location.href = (text.toLowerCase() == "reject")?"/waiting":result.cat_path;
			}
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			alert(iweb_lang.get("ERR_AJAX_PROCESS_FAILED"));
			window.location.reload();
	    }
	});
}

function cp_page_toggle_publish(event, page_id) {
	event.preventDefault();
	var elem = $(".a_page_publish");
	var text = $(elem).text();

	if($(elem).hasClass("page_cp_waiting")) {
		return false;
	}

	$(elem).addClass("page_cp_waiting");
	$(elem).text("");

	var plugin = "page_write";
	var action = "do_publish";
	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'page_id', value : page_id }
	];

	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK"){
				$(elem).removeClass("page_cp_waiting");
				$(elem).text(text);
				alert(iweb_lang.get(result.code));
			}
			window.location.reload();
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			alert(iweb_lang.get("ERR_AJAX_PROCESS_FAILED"));
			window.location.reload();
	    }
	});
}

function cp_page_toggle_featured(event, page_id) {
	event.preventDefault();
	var elem = $(".a_page_featured");
	var text = $(elem).text();

	if($(elem).hasClass("page_cp_waiting")) {
		return false;
	}

	$(elem).addClass("page_cp_waiting");
	$(elem).text("");

	var plugin = "page_write";
	var action = "do_featured";
	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'page_id', value : page_id }
	];

	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK"){
				$(elem).removeClass("page_cp_waiting");
				$(elem).text(text);
				alert(iweb_lang.get(result.code));
			}
			window.location.reload();
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			alert(iweb_lang.get("ERR_AJAX_PROCESS_FAILED"));
			window.location.reload();
	    }
	});
}

function cp_page_gallery_pic_click(event, thumb_id, rp) {
	event.preventDefault();
	cp_page_gallery_pic_show(thumb_id, rp);
}

function cp_page_gallery_pic_prev(event, page_gallery_ids, thumb_id, page_gallery_pic_rp) {
	event.preventDefault();
	for(var i=0; i < page_gallery_ids.length; i++) {
		if(page_gallery_ids[i] == thumb_id) {
			thumb_id =  (i-1 > 0)?page_gallery_ids[i-1]:page_gallery_ids[page_gallery_ids.length - 1];
			break;
		}
	}
	cp_page_gallery_pic_show(thumb_id, page_gallery_pic_rp);
}

function cp_page_gallery_pic_next(event, page_gallery_ids, thumb_id, page_gallery_pic_rp) {
	event.preventDefault();
	for(var i=0; i < page_gallery_ids.length; i++) {
		if(page_gallery_ids[i] == thumb_id) {
			thumb_id =  (i+1 < page_gallery_ids.length)?page_gallery_ids[i+1]:page_gallery_ids[0];
			break;
		}
	}
	cp_page_gallery_pic_show(thumb_id, page_gallery_pic_rp);
}

function cp_page_gallery_pic_show(thumb_id, rp) {
	var plugin = "page_tool";
	var action = "get_gallery_thumb";
	var url = "/tpl/cp/cp_gallery_pic?thumb_id=" + thumb_id;

	$(".show_big_image_thumb").empty().append("<div class=\"loading\"></div>");
	$(".show_big_image_desc").empty().hide();

	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'url', value : url }
		,{ name : 'rp', value : rp }
		,{ name : 'thumb_id', value : thumb_id }
	];

	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK"){
				alert(iweb_lang.get(result.code));
			}
			else {
				page_gallery_pic_id = result.thumb_id;
				if(result.pageno != page_gallery_pic_pageno) {
					page_gallery_pic_pageno = result.pageno;
					cp_page_gallery_pic_list_show(result.thumb_id, result.page_id, result.pageno, result.rp)
				}
				$(".show_big_image_thumb").empty().append(result.html).fadeIn(1000);
				if(result.thumb_desc != "") {
					$(".show_big_image_desc").empty().append(result.thumb_desc).show();
				}

				$(".pic_item").each(function() { $(this).removeClass("pic_item_selected"); });
				$("#pic_item_" + page_gallery_pic_id).addClass("pic_item_selected");
			}
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) { }
	});
}

function cp_page_gallery_pic_list_show(thumb_id, page_id, pageno, rp) {
	var plugin = "page_tool";
	var action = "get_html";
	var url = "/tpl/cp/cp_gallery_pic_list?thumb_id=" + thumb_id + "&page_id=" + page_id + "&pageno=" + pageno + "&rp=" + rp;

	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'url', value : url }
	];

	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK"){
				alert(iweb_lang.get(result.code));
			}
			else {
				$(".list_show").empty().append(result.html).fadeIn(1000);
			}
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) { }
	});
}

function cp_page_write_get_categories(cat_id, cat_ids) {
	var plugin = "page_write";
	var action = "get_categories";

	$(".page_write_cat_loading").show();
	$(".page_write_cat_menu").hide();

	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'cat_ids', value : cat_ids }
		,{ name : 'cat_id', value: cat_id }
	];

	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK"){
				alert(iweb_lang.get(result.code));
			}
			else {
				$("#categorymenu").empty().append("<li class=\"mc_root\" rel=\"0\">กรุณาเลือก</li>").append(result.html);
				var dd = $("#cat_id").mcDropdown();
				dd.setValue(result.cat_id);

				$(".page_write_cat_loading").hide();
				$(".page_write_cat_menu").show();
			}
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			alert(iweb_lang.get("ERR_AJAX_PROCESS_FAILED"));
	    }
	});
}

function cp_page_event_comment_list(event, req_uid, page_id, pageno, rp, qorder, scroll) {
	event.preventDefault();
	cp_page_comment_list(req_uid, page_id, pageno, rp, qorder, scroll);
}

function cp_page_comment_list(req_uid, page_id, pageno, rp, qorder, scroll) {
	var plugin = "page_tool";
	var action = "get_html";
	var url = "/tpl/cp/cp_comment_list?req_uid=" + req_uid + "&page_id=" + page_id + "&pageno=" + pageno + "&rp=" + rp + "&qorder=" + qorder;

	$(".comment_form").hide();
	$(".comment_items").empty().addClass("page_cp_waiting");
	if(scroll) { $(window).scrollTo($("#comment_article"), 0); }

	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'url', value : url }
	];

	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK"){ $(".comment_items").empty().removeClass("page_cp_waiting").append(iweb_lang.get(result.code)); }
			else { $(".comment_items").empty().removeClass("page_cp_waiting").append(result.html).fadeIn(1000); }

			$(".comment_form").show();
			if(scroll) { $(window).scrollTo($("#comment_article"), 0); }
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) { }
	});
}

function cp_comment_submit(event, pageno, rp, qorder) {
	event.preventDefault();
	comment_dosubmit(pageno, rp, qorder);
}

function cp_comment_edit(event, elem) {
	event.preventDefault();
	comment_doedit(elem);
}

function cp_comment_voteup(event, elem, comment_id) {
	event.preventDefault();
	comment_dovote(elem, comment_id, "comment_voteup");
}

function cp_comment_votedown(event, elem, comment_id) {
	event.preventDefault();
	comment_dovote(elem, comment_id, "comment_votedown");
}

function cp_comment_edit(event, elem, pageno, rp, qorder) {
	event.preventDefault();
	comment_doedit(elem, pageno, rp, qorder);
}

function cp_comment_delete(event, elem, pageno, rp, qorder) {
	event.preventDefault();
	comment_dodelete(elem, pageno, rp, qorder);
}

function cp_comment_hide(event, elem, pageno, rp, qorder) {
	event.preventDefault();
	comment_dohide(elem, pageno, rp, qorder);
}

function cp_comment_abuse(event, elem) {
	event.preventDefault();
	comment_doabuse(elem);
}

function sendarticle_submit(event) {
	event.preventDefault();
	sendarticle_dosubmit();
}

function iweb_search(event, keyword, pageno, parent) {
	event.preventDefault();
	iweb_dosearch(keyword, pageno, parent);
}

function err_event_unauthorized(event) {
	event.preventDefault();
	err_unauthorized();
}

function err_unauthorized() {
	alert(iweb_lang.get("ERR_UNAUTHORIZED"));
	window.location.href = login_url;
}

function comment_dosubmit(pageno, rp, qorder) {
	var plugin = "page_comment";
	var action = "post_comment";
	var comment = tinyMCE.get('comment_txt').getContent();
	var comment_id = $.trim($('#comment_id').val());
	var section = $.trim($('#comment_section').val());
	var page_id = $.trim($('#comment_page_id').val());
	var parent_id = $.trim($('#comment_parent_id').val());

	$(window).scrollTo($("#comment_article"), 0);
	$(".comment_form").hide();
	$(".comment_items").empty().addClass("page_cp_waiting");

	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'comment', value : comment }
		,{ name : 'comment_id', value : comment_id}
		,{ name : 'section', value: section }
		,{ name : 'page_id', value: page_id }
		,{ name : 'parent_id', value: parent_id }
		,{ name : 'rp', value: rp }
		,{ name : 'pageno', value: pageno }
		,{ name : 'qorder', value: qorder }
	];
	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK") {
				if(result.code == "ERR_UNAUTHORIZED") {
					err_unauthorized();
					return false;
				}
				else {
					alert(iweb_lang.get(result.code));
				}
			}
			else {
				var ed = tinyMCE.get('comment_txt');
				ed.setContent("");
			}
			cp_page_comment_list(result.uid, result.page_id, result.pageno, result.rp, result.qorder, true);
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			alert(iweb_lang.get("ERR_AJAX_PROCESS_FAILED"));
		}
	});
}

function comment_doabuse(elem) {
	if($(elem).hasClass("page_cp_waiting")) return false;
	var comment_id = $(elem).attr("href").replace(/#comment_/, "");
	var comment_no = $(elem).attr("ref").replace(/#comment_no_/, "");

	if (!confirm("คุณต้องการแจ้งลบความคิดเห็น #" + comment_no + " ใช่หรือไม่?")) {
		return false;
	}

	$(elem).empty().addClass("page_cp_waiting");
	var plugin = "page_comment";
	var action = "abuse_comment";
	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'comment_id', value : comment_id }
	];

	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK") {
				if(result.code == "ERR_UNAUTHORIZED") {
					err_unauthorized();
					return false;
				}
				else {
					alert(iweb_lang.get(result.code));
				}
			}
			else {
				alert("ขอบคุณค่ะ ระบบบันทึกการแจ้งลบความคิดเห็น #" + comment_no + " เรียบร้อยแล้ว");
			}
			$(elem).empty().append("Report abuse").removeClass("page_cp_waiting");
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			alert(iweb_lang.get("ERR_AJAX_PROCESS_FAILED"));
		}
	});
}

function comment_doedit(elem, pageno, rp, qorder) {
	var comment_id = $(elem).attr("href").replace(/#comment_/, "");
	var comment_no = $(elem).attr("ref").replace(/#comment_no_/, "");
	$(window).scrollTo($("#comment_article"), 0);

	var plugin = "page_comment";
	var action = "edit_comment";
	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'comment_id', value : comment_id }
		,{ name : 'pageno', value : pageno }
		,{ name : 'rp', value : rp }
		,{ name : 'qorder', value : qorder }
	];

	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK") {
				if(result.code == "ERR_UNAUTHORIZED") { err_unauthorized(); return false; }
				else { alert(iweb_lang.get(result.code)); }
			}
			if(result.page_id == 0) return false;
			var ed = tinyMCE.get('comment_txt');
			ed.setContent(result.comment);
			$("#comment_id").val(result.comment_id);
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			alert(iweb_lang.get("ERR_AJAX_PROCESS_FAILED"));
		}
	});
}

function comment_dovote(elem, comment_id, action) {
	var a_elem_voteup = $("#a_comment_voteup_" + comment_id);
	var a_elem_votedown = $("#a_comment_votedown_" + comment_id);

	if(!$(a_elem_voteup).hasClass("cursor_pointer") || !$(a_elem_votedown).hasClass("cursor_pointer")) {
		return false;
	}

	$(a_elem_voteup).removeClass("cursor_pointer");
	$(a_elem_voteup).removeClass("a_comment_voteup");

	$(a_elem_votedown).removeClass("cursor_pointer");
	$(a_elem_voteup).removeClass("a_comment_votedown");

	var elem_givescore = $("#comment_give_score_" + comment_id);
	var elem_innerbox = $("#comment_give_score_" + comment_id + " .givescore_innerbox");

	$(elem_innerbox).hide();
	$(elem_givescore).addClass("give_score_waiting");

	var comment_no = $(elem).attr("ref").replace(/#comment_no_/, "");
	var plugin = "page_comment";
	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'comment_id', value : comment_id }
	];

	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK") {
				if(result.code == "ERR_UNAUTHORIZED") { err_unauthorized(); return false; }
				else { alert(iweb_lang.get(result.code)); }
			}
			else {
				var elem_voteup = $("#comment_voteup_" + comment_id);
				var elem_votedown = $("#comment_votedown_" + comment_id);
				var elem_votetotal = $("#comment_votetotal_" + comment_id);

				$(elem_voteup).empty().append(result.vote_up);
				$(elem_votedown).empty().append(result.vote_down);
				$(elem_votetotal).empty().append(result.vote_total);
			}

			$(elem_innerbox).show();
			$(elem_givescore).removeClass("give_score_waiting");
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			alert(iweb_lang.get("ERR_AJAX_PROCESS_FAILED"));
		}
	});
}

function comment_dodelete(elem, pageno, rp, qorder) {
	var comment_id = $(elem).attr("href").replace(/#comment_/, "");
	var comment_no = $(elem).attr("ref").replace(/#comment_no_/, "");

	if (!confirm("คุณต้องการลบความคิดเห็น #" + comment_no + " ใช่หรือไม่?")) {
		return false;
	}

	$(window).scrollTo($("#comment_article"), 0);
	$(".comment_form").hide();
	$(".comment_items").empty().addClass("page_cp_waiting");

	var plugin = "page_comment";
	var action = "delete_comment";
	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'comment_id', value : comment_id }
		,{ name : 'pageno', value : pageno }
		,{ name : 'rp', value : rp }
		,{ name : 'qorder', value : qorder }
	];

	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK") {
				if(result.code == "ERR_UNAUTHORIZED") { err_unauthorized(); return false; }
				else { alert(iweb_lang.get(result.code)); }
			}
			cp_page_comment_list(result.uid, result.page_id, result.pageno, result.rp, result.qorder, true);
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			alert(iweb_lang.get("ERR_AJAX_PROCESS_FAILED"));
		}
	});
}

function comment_dohide(elem, pageno, rp, qorder) {
	var comment_id = $(elem).attr("href").replace(/#comment_/, "");
	var comment_no = $(elem).attr("ref").replace(/#comment_no_/, "");

	if (!confirm("คุณต้องการซ่อนความคิดเห็น #" + comment_no + " ใช่หรือไม่?")) {
		return false;
	}

	$(window).scrollTo($("#comment_article"), 0);
	$(".comment_form").hide();
	$(".comment_items").empty().addClass("page_cp_waiting");

	var plugin = "page_comment";
	var action = "hide_comment";
	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'comment_id', value : comment_id }
		,{ name : 'pageno', value : pageno }
		,{ name : 'rp', value : rp }
		,{ name : 'qorder', value : qorder }
	];
	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK") {
				if(result.code == "ERR_UNAUTHORIZED") { err_unauthorized(); return false; }
				else { alert(iweb_lang.get(result.code)); }
			}
			cp_page_comment_list(result.uid, result.page_id, result.pageno, result.rp, result.qorder, true);
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			alert(iweb_lang.get("ERR_AJAX_PROCESS_FAILED"));
		}
	});
}

function sendarticle_dosubmit() {
	var plugin = "page_tool";
	var action = "sendarticle";
	var to_email = $("#sendarticle_to_email").val();

	$(".div_sendarticle").hide();
	$(".div_sendarticle_msg").empty().append(iweb_lang.get("SEND_ARTICLE")).show();

	var params = [
		 { name : 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'to_email', value : to_email }
		,{ name : 'title', value : page_title }
		,{ name : 'path', value : page_path }
	];
	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(result.code != "OK") {
				var msg = iweb_lang.get("ERR_PROCESS_FAILED");
				$(".div_sendarticle").show();
				$(".div_sendarticle_msg").empty().hide();
				if(result.code == "ERR_UNAUTHORIZED") { err_unauthorized(); }
				else if(result.code == "ERR_REQUIRED_TO_EMAIL") { alert(iweb_lang.get("ERR_REQUIRED_EMAIL")); $("#sendarticle_to_email").focus(); }
				else if(result.code == "ERR_INVALID_TO_EMAIL") { alert(iweb_lang.get("ERR_INVALID_EMAIL")); $("#sendarticle_to_email").focus(); }
				else if(result.code == "ERR_INVALID_AJAX_SESSION") { $(".div_sendarticle").hide(); $(".div_sendarticle_msg").empty().append(iweb_lang.get("ERR_INVALID_AJAX_SESSION")).show(); }
				else { $(".div_sendarticle").hide(); $(".div_sendarticle_msg").empty().append(msg + " Error with message " + result.code).show(); }
				return false;
			}
			$("#sendarticle_to_email").val("");
			$(".div_sendarticle").hide(); $(".div_sendarticle_msg").empty().append(iweb_lang.get("SEND_ARTICLE_SUCCESS")).show();
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			$(".div_sendarticle").hide(); $(".div_sendarticle_msg").empty().append(iweb_lang.get("ERR_AJAX_PROCESS_FAILED")).hide();
		}
	});
}

function uc_login(data) {
	var iweb_lang = data.iweb_lang;
	var formhash = data.formhash;
	var loginfield = data.loginfield;
	var questionid = data.questionid;
	var username = data.username;
	var password = data.password;
	var cookietime = data.cookietime;
	var ajax_session = data.ajax_session;
	var ajax_url = data.ajax_url;
	var base_url = data.base_url;
	var referer_url= data.referer_url;
	var login_url = data.login_url;
	var home_url = data.home_url;
	var params = [
		 { name : 'plugin', value : 'ucenter' }
		,{ name : 'action', value : 'login' }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'formhash', value : formhash }
		,{ name : 'loginfield', value : loginfield }
		,{ name : 'questionid', value : questionid }
		,{ name : 'username', value : username }
		,{ name : 'password', value : password }
		,{ name : 'cookietime', value : cookietime }
		,{ name : 'referer_url', value : referer_url }
	];
	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			$("#login_error").empty().hide();
			if(result.code != "OK") {
				var msg = result.code;
				$("#msg_username").empty().hide();
				$("#msg_password").empty().hide();
				if(result.code == "ERR_USERNAME_REQUIRED") {
					msg = iweb_lang.get("ERR_REQUIRED");
					$("#username").focus();
					$("#msg_username").empty().append(iweb_lang.get("ERR_REQUIRED_USERNAME")).show();
				}
				else if(result.code == "ERR_PASSWORD_REQUIRED") {
					msg = iweb_lang.get("ERR_REQUIRED");
					$("#password").focus();
					$("#msg_password").empty().append(iweb_lang.get("ERR_REQUIRED_PASSWORD")).show();
				}
				else if(result.code == "ERR_UNAUTHORIZED") {
					msg = iweb_lang.get("ERR_LOGIN_FAILED");
					$("#username").focus(); $("#password").val("");
				}
				$("#login_error").empty().append(msg).show(); return;
			}
			$("#login_innerbox").empty().addClass("txt_info").append(iweb_lang.get("LOGIN_SUCCESS"));
			window.location.href = result.url;
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			var msg = iweb_lang.get("ERR_AJAX_PROCESS_FAILED");
			$("#username").val(""); $("#username").focus(); $("#login_error").empty().append(msg).show();
		}
	});
}

function iweb_dosearch(keyword, pageno, parent) {
	var keyword = $.trim(urlencode(keyword.replace(/'/g, '').replace(/"/g, '')));
	var url = "/search?q=" + keyword;
	if(pageno > 1) $url = url +  "&p=" + pageno;

	var plugin = "page_search";
	var action = "set_pagesession";
	var params = [
		 { name: 'plugin', value : plugin }
		,{ name : 'action', value : action }
		,{ name : 'ajax_session', value : ajax_session }
		,{ name : 'keyword', value : keyword }
		,{ name : 'pageno', value : pageno }
	];
	$.ajax({
		type: "POST",
		dataType: "json",
		url: ajax_url + "?r=" + uniqid(),
		data: params,
		success: function(result){
			if(parent){ window.parent.location.href = url; }
			else { window.location.href = url; }
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) { }
	});
}
