loadscript("mdialog");

function addflower(reviewid, number) {
  if (!is_numeric(reviewid)) {
    alert('无效的点评ID');
    return;
  }
  $.post(getUrl('shop')+'post.php', { action:"addflower",reviewid:reviewid,in_ajax:1 }, function(result) {
    if (result.length > 0) {
		myAlert(result);
    } else {
      $('#flower_'+reviewid).text(number+1);
    }
  });
}

function show_report(reviewId) {
	$.get(getUrl('shop')+'post.php', { action:"report",reviewid:reviewId,in_ajax:1 }, function(result){
		if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
		} else if(result.match(/<form.+onsubmit="return post_report\(\);".*>/)) {
			result += '<iframe style="display:none" src="" name="ifreport" id="ifreport"></iframe>';
			dlgOpen('举报不规范信息', result, 400, 260);
		} else {
			myAlert(result);
		}
	});
}

function post_report() {
	var form = document.frmreport;
	if(form.username && form.username.value.length == 0){
		msgOpen('请填写您的昵称。');
		return false;
	}
	if(form.email && form.email.value.length == 0){
		msgOpen('请输入您的电子邮件地址。');
		return false;
	}else if(form.email && !is_email(form.email.value)) {
		msgOpen('您输入的电子邮件地址格式不正确。');
		return false;
	}
	var sort = getRadio(form, 'sort');
	if(form.sort.value == '') {
		msgOpen('您选择举报类型。');
		return false;
	}
	if(form.content.value.length > 200){
		msgOpen('您填写的举报说明文字过多(200字以内)，请精简一下您的举报说明。');
		return false;
	}
	if(!is_numeric(form.reviewid.value)){
		msgOpen('无法确定举报对象。');
		return false;
	}

	return true;
}

function show_upshop(shopid) {
	$.get(getUrl('shop')+'post.php',{ action:"upshopinfo",shopid:shopid,in_ajax:1 },function(result){
		if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
		} else if(result.match(/<form.+onsubmit="return post_upshop\(\);".*>/)) {
			result += '<iframe style="display:none" src="" name="ifupshopinfo" id="ifupshopinfo"></iframe>';
			dlgOpen('补充商铺信息', result, 380, 230);
		} else {
			myAlert(result);
		}
	});
}

function post_upshop() {

	var form = document.frmupshop;
	var upshopmap = form.target == 'ifupshopmap';

	if(form.username && form.username.value.length == 0) {
		msgOpen('请填写您的昵称。');
		return false;
	}
	if(form.email && form.email.value.length == 0) {
		msgOpen('请输入您的电子邮件地址。');
		return false;
	} else if(form.email && !is_email(form.email.value)) {
		msgOpen('您输入的电子邮件地址格式不正确。');
		return false;
	}
	if(upshopmap && (form.lng.value.length == 0 || form.lat.value.length == 0)) {
		msgOpen('未标注商铺地图。');
		return false;
	} else if(upshopmap && (form.lng.value.trim() == '' || form.lat.value.trim() == '')) {
		msgOpen('未标注商铺地图。');
		return false;
	} else if(upshopmap && (!form.lng.value.match(/^[A-Za-z0-9\.\-]+$/) || !form.lat.value.match(/^[A-Za-z0-9\.\-]+$/))) {
		msgOpen('无效的未标值。');
		return false;
	}
	if(form.content.value.length == 0) {
		msgOpen('请填写补充说明文字。');
		return false;
	} else if(form.content.value.length > 300) {
		msgOpen('您填写的说明文字过多(300字以内)，请精简一下您的说明。');
		return false;
	}
	if(!is_numeric(form.shopid.value)){
		msgOpen('无法确定补充对象(商铺)。');
		return false;
	}
	
	return true;

}

function show_upshopmap (shopid) {
	$.get(getUrl('shop')+'post.php',{ action:"upshopmap",shopid:shopid,in_ajax:1 },function(result){
		if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
		} else if(result.match(/<form.+onsubmit="return post_upshop\(\);".*>/)) {
			result += '<iframe style="display:none" src="" name="ifupshopmap" id="ifupshopmap"></iframe>';
			dlgOpen('地图标注更新', result, 580, 430);
		} else {
			myAlert(result);
		}
	});
}

function user_effect(keyid,idtype,effect,effectaction) {
    if(effectaction == 'getusers' && $('#users_'+effect).html() != "") {
        $('#users_'+effect).html("");
        $('#users_'+effect).css("display:none;");
        return;
    }
    $.post(getUrl()+'ajax.php', { action:"usereffect", keyid:keyid, idtype:idtype, effect:effect, 
        effectaction:effectaction, in_ajax:1 }, 
	  function(result) {
		if (result.match(/^[0-9]+$/)) {
			$('#num_'+effect).html(result);
			if($('#users_'+effect).html()!="") {
				$('#users_'+effect).html("");
				$('#users_'+effect).css("display:none;");
				user_effect(keyid,idtype,effect,'getusers');
			}
		} else if(result.match(/^<a\s+href=\"http/)) {
		    $('#users_'+effect).css("display:;");
			$('#users_'+effect).html(result);
		} else if(result == 'clear') {
			$('#users_'+effect).html('暂无信息。');
		} else if(result != 'keep') {
			myAlert(result);
		}
	});
}

function addfavorite (shopid) {
	if (!is_numeric(shopid)) {
		alert('无效的商铺');
		return;
	}
	$.post(getUrl('shop')+'post.php', { action:"addfavorite",shopid:shopid,in_ajax:1 }, function(result) {
		myAlert(result);
	});
}

/************* 留言 *****************/
function getGuestbook(guestbookid) {
    $.get(getUrl('shop')+'post.php', { action:"guestbook", guestbookid:guestbookid, in_ajax:1 }, function(result) {
        if(result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
        } else {
            var data = result.split("||||");
            $("#guestbook_content_"+guestbookid).html(data[0]);
            if(data[1]) {
                $("#guestbook_reply_"+guestbookid).html(data[1]);
                $("#guestbook_reply_tr1_"+guestbookid).css("display","");
            }
        }
    });
}

function gbReply(guestbookid) {
    if (!is_numeric(guestbookid)) {
        alert('无效的留言ID'); return;
    }
	$.get(getUrl('shop')+'member.php', { ac:"guestbook",op:"reply",guestbookid:guestbookid,in_ajax:1 }, function(result){
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			dlgOpen('回复留言', result, 350, 260);
		}
	});
}

function gbEdit(guestbookid) {
    if (!is_numeric(guestbookid)) {
        alert('无效的留言ID'); return;
    }
	$.get(getUrl('shop')+'member.php', { ac:"guestbook",op:"edit",guestbookid:guestbookid,in_ajax:1 }, function(result){
        if(result == null) {
			alert('信息读取失败，可能网络忙碌，请稍后尝试。');
        } else if (result.match(/\{\s+caption:".*",message:".*".*\s*\}/)) {
            myAlert(result);
		} else {
			dlgOpen('编辑留言', result, 350, 260);
		}
	});
}

function gbDelete(guestbookid) {
    if (!is_numeric(guestbookid)) {
        alert('无效的留言ID'); return;
    }
    if(confirm("确定要删除这条留言信息吗？")) {
        $.post(getUrl('shop')+'member.php', { ac:"guestbook", op:"delete", guestbookid:guestbookid, in_ajax:1 }, function(result) {
            if(result=='') {
                $('#guestbook_'+guestbookid).remove();
            } else {
                myAlert(result);
            }
        });
    }
}

function checkfrmGuestbook(form) {
    if(form.content.value=='') {
        alert('对不起，您未填写留言内容。');
        return false;
    } else if (form.seccode && form.seccode.value=='') {
        alert('对不起，您未填写验证码。');
        return false;
    }
    return true;
}
