//问答-部门筛选
function searchDept(id) {
  Zjport.doSubmit('','govConsult!searchQus.jspa?deptId=' + id, { 
  	     receiver: 'question_list_div'
   });
}
//问答-行业筛选
function searchIndustry(id) {
  Zjport.doSubmit('','govConsult!searchQus.jspa?industryId=' + id, { 
  	     receiver: 'question_list_div'
   });
}
//全文检索：问答-部门筛选
function ftSearchDept(id) {
  Zjport.doSubmit('keyWordForm','govConsult!ftSearchQus.jspa?type=search&deptId=' + id, { 
  	     receiver: 'question_list_div'
   });
}
//全文检索：问答-行业筛选
function ftSearchIndustry(id) {
  Zjport.doSubmit('keyWordForm','govConsult!ftSearchQus.jspa?type=search&industryId=' + id, { 
  	     receiver: 'question_list_div'
   });
}
//热门标签
function searchHotTag(id) {
  Zjport.doSubmit('','govConsult!hotTagList.jspa?tagId=' + id, { 
  	     receiver: 'tag_search_div'
   });
}
function searchHotTag2(id) {
  window.location.href = Context.base + '/ask/govConsult!hotTag.jspa?tagId=' + id + '&type=search';
}
//关务专家
function showExpert(id) {
  window.location.href = Context.base + '/ask/govConsult!showExpert.jspa?customsExpert.id=' + id;
}
function showExpert2(id) {
  Zjport.doSubmit('','govConsult!showExpert2.jspa?customsExpert.id=' + id, { 
  	     receiver: 'expert_detail_div'
   });
}
//办事指南-行业筛选
function actionGuide(id) {
  window.location.href = Context.base + '/ask/govConsult!guideList.jspa?industryId=' + id;
}

function searchGuide(id) {
  Zjport.doSubmit('','govConsult!searchGuide.jspa?industryId=' + id, { 
  	     receiver: 'guide_list_div'
   });
}
function searchGuideDept(id) {
  Zjport.doSubmit('','govConsult!searchGuide.jspa?deptId=' + id, { 
  	     receiver: 'guide_list_div'
   });
}
function doGuide(id) {
  window.location.href = Context.base + '/ask/govConsult!guideDetail.jspa?actionGuide.id=' + id;
}

//检索答案
function doSearch() {

  if(jQuery.trim($("#gcKeywords").val())!="") {
	    Zjport.Table.loadingPic('result_div');
	    var keyWordForm = jQuery('#keyWordForm')[0];
	    // keyWordForm.action = Context.base + '/ask/govConsult!searchKeyWord.jspa?type=search';
	    keyWordForm.action = Context.base + '/ask/govConsultSearch!ftSearchHeader.jspa?type=search';
	    keyWordForm.submit();
  } else {
	    alert("请输入关键字再搜索答案！");
	    return;
  }
}

//我要提问
function doAsk() {
	window.location.href = Context.base + '/ask/govConsult!searchBox.jspa?type=ask';
}

function doBoxSave() {
  if(document.getElementById("queTitle").value != "") {
    boxForm.action = Context.base + '/ask/govConsult!searchBox.jspa?type=ask';
    boxForm.submit();
    jQuery.nyroModalRemove();
  } else {
    alert('请输入您的提问标题！');
  }
}

//满意度评价--遮罩层显示
function showScore(fid) {
  jQuery.nyroModalManual({
     url: Context.base + '/ask/govConsult!score.jspa?fid=' + fid
   });
}
//评分
function doScore(fid, score) {
	window.location.href = Context.base + '/ask/govConsult2!scoreSave.jspa?fid=' + fid + '&score=' + score;
}
//差评
function doScore1(fid, score) {
  if(chechReason()) {
    jQuery('#scoreForm').submit();
  }
}
function doClose() {
  jQuery.nyroModalRemove();
}
function doUserQus() {
  window.location.href = Context.base + '/ask/govConsult!userQus.jspa';
}
///**部门、行业联动*/
function changeDept() {
  var deptId = document.getElementById('deptId');
  Zjport.doSubmit('','govConsult!changeDept.jspa?deptId=' + deptId.value, { 
      receiver: 'industry_div'
      });
}

//继续提问
function doSave(){
  if(check()) {
    if($("#uploadResourceId1").val()!="") {
	  $("[id=resourceId]").val($("#uploadResourceId1").val());
	}
    Zjport.doSubmit('askForm','govConsult2!save.jspa',{ 
  	  afterReceive : function(){
  	    document.getElementById("uploadResourceId1").value = "";
  	    document.getElementById("question.title").value = "";
  	    document.getElementById("question.question").value = "";
  	    document.getElementById("deptId").value = "";
  	    document.getElementById("industryId").value = "";
  	    document.getElementById("question.telphone").value = "";
  	    //window.location="govConsult.jspa";
  	    jQuery.nyroModalManual({
	      url: Context.base + '/ask/govConsult!resuccess.jspa'
	    });
      }
    });
  }
}

//继续提问-CHECK
function check() {
  var title = document.getElementById('question.title').value;
  if(title == null || title == "") {
    alert('请添加标题！');
    return false;
  }
  var deptId = document.getElementById('deptId').value;
  if(deptId == null || deptId == "") {
    alert('请选择部门！');
    return false;
  } else {
    document.getElementById('question.deptId').value = deptId;
  }
  var industryId = document.getElementById('industryId').value;
  if(industryId == null || industryId == "") {
    alert('请选择业务分类！');
    return false;
  } else {
    document.getElementById('question.industryId').value = industryId;
  }
  var telphone = document.getElementById('question.telphone').value;
  if(telphone != "") {
    if(!/^(13[0-9]|15[0-9]|18[0-9])\d{8}$/.test(telphone)) {
      if(!/^(\d{3,4}-?)?\d{7,9}$/.test(telphone)) {
        alert('请填写正确的联系方式！');
        return false;
      }
    }
  }
  return true;
}
//差评-CHECK
function chechReason() {
  var satisReason = document.getElementById('question.satisReason').value;
  if(satisReason == null || satisReason == "") {
    alert('请您添加评价原因，以便我们工作改进！');
    return false;
  }
  var telphone = document.getElementById('question.telphone').value;
  if(telphone == null || telphone == "") {
    alert('请添加您的联系方式！');
    return false;
  } else {
    if(!/^(13[0-9]|15[0-9]|18[0-9])\d{8}$/.test(telphone)) {
      if(!/^(\d{3,4}-?)?\d{7,9}$/.test(telphone)) {
        alert('请填写正确的联系方式！');
        return false;
      }
    }
  }
  return true;
}

//问答详情
function showDetail(id) {
  window.location.href = Context.base + '/ask/questionDetailt.jspa?fid=' + id;
  jQuery.nyroModalRemove();
}

//登陆
function doLogin() {
  window.location.href = Context.base + '/protected/redirect.jsp?url=' + encodeURIComponent(window.location.href);
}

function doNewSave() {
	if(validateAskForm()){
		jQuery('#askForm').submit();
	}
}

function validateAskForm() {
	var deptId = jQuery('#deptId option:selected').val();
	if(deptId == null || deptId == "") {
		alert('请选择部门！');
		return false;
	}
	
	var industryId = jQuery('#industryId option:selected').val();
	if(industryId == null || industryId == "") {
		alert('请选择业务分类！');
    	return false;
	}
  
	var title = jQuery('#questionTitle').val();
	if(title == null || title == "") {
    	alert('请添加标题！');
    	return false;
	}
	
	var fileName = jQuery('#uploadFile').val();
	if(fileName != null && fileName != ""){
		var extensionName = fileName.substr(fileName.lastIndexOf('\.') + 1);
		var format = /(png|gif|jpeg|jpg|bmp)$/;
		if(!format.test(extensionName.toLowerCase())){
			alert('文件类型不符合要求！');
	    	return false;
		}
	}
	
	return true;
}

// 错误提示弹出框：关闭
function closePromptBox() {
	jQuery.nyroModalRemove();
	jQuery('#promptBox').hide();
	return false;
}

// 登录弹出框：关闭
function closePromptBox1() {
	jQuery.nyroModalRemove();
	jQuery('#promptBox1').hide();
	return false;
}

// 登录弹出框：关闭,返回政务咨询首页
function closePromptBox2() {
	jQuery.nyroModalRemove();
	jQuery('#promptBox2').hide();
	window.location.href = Context.base + '/portal-staticview/govConsult.jspa';
}

// 登录弹出框：电子口岸用户直接登录
function registedUserLogin(){
	// window.location.href = '${base}/protected/redirect.jsp?url=' + encodeURIComponent(window.location.href);
	window.location.href = Context.base + '/protected/redirect.jsp?url=' + encodeURIComponent(Context.base + '/ask/govConsult!saveQuestion.jspa');
}

// 登录弹出框：匿名用户提交
function anonymousLogin(){
	var anonymousEmail = jQuery('#anonymousEmail').val();

	if(jQuery('#anonyEmailCB').attr('checked') == true){
		if(!(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(anonymousEmail))){
			alert('Email格式不正确！');
			return false;
		}
	}

	jQuery.post(Context.base + '/ask/govConsult!anonymousAsk.jspa',
		jQuery('#anonymousLoginForm').serialize(),
		function(data){
			jQuery('#acceptId').append(data);
			jQuery('#promptBox1').toggle();
			jQuery('#promptBox2').toggle();
		});
}

// 点击匿名提问按钮
function clickAnonymous(){
	if(jQuery('#anonymous').attr('checked') == true){
		// jQuery('#dl2').removeClass('mail_disable');
		// jQuery('#anonyEmailCB').removeAttr('disabled');
		jQuery('#submitBtn').removeAttr('disabled');
	}else{
		// jQuery('#dl2').addClass('mail_disable');
		// jQuery('#anonyEmailCB').attr('disabled', true);
		jQuery('#submitBtn').attr('disabled', true);
	}
}

function toggleEmail(){
	if(jQuery('#anonyEmailCB').attr('checked') == true){
		jQuery('#anonymousEmail').removeAttr('disabled');
	}else{
		jQuery('#anonymousEmail').attr('disabled', true);
	}
}

// 政务咨询首页按编号查询
function checkByCode(){
	var questionCode = jQuery('#questionCode').val();
	questionCode = jQuery.trim(questionCode);
	var verifyCode = jQuery('#verifyCode').val();
	if(questionCode == null || questionCode == ''){
		alert('请输入编号！');
		return false;
	}else if(questionCode.length != 12){
		alert('编号长度不正确，请输入12位编号！');
		return false;
	}
	if(verifyCode == null || verifyCode == ''){
		alert('请输入验证码！');
		return false;
	}
	jQuery.get(Context.base + '/ask/govConsult!checkQuestionByCode.jspa',
		{questionCode: questionCode, verifyCode: verifyCode},
		function(data){
			data = eval('(' + data + ')');
			if(data.code == -1){
				alert('验证码验证失败，请重新输入验证码！');
				// 重刷验证码
				jQuery('#verifyCodeImg').attr('src', Context.base + '/captcha/createCaptcha.jspa?tmp='+Math.random());
			}else if(data.code == 0){
				alert('很抱歉，编号为' + questionCode + '的问题已失效！');
			}else if(data.code == 1){// 编号对应的问题存在
				if(data.acceptState == 2){// 不受理
					alert('很抱歉，编号为'+ questionCode +'的问题未审核通过，详情请拨打967209！');
				}else if(data.acceptState == 0 || data.acceptState == 1){// 未受理或已受理
					if(data.user != null && data.anonymous == 0 && data.requester == data.user){
						window.location.href = Context.base + '/ask/questionDetailt.jspa?fid=' + questionCode;
					} else {
						alert('您好，编号为'+ questionCode +'的问题正在受理中，请稍后再查询，如有问题可拨打967209！');
					}
				}else{// 已答复
					if(data.checkState == 0){// 审核未通过
						if(data.user != null && data.anonymous == 0 && data.requester == data.user){
							if(data.display == 0){// 不显示
								window.location.href = Context.base + '/ask/questionDetailt.jspa?fid=' + questionCode;
							}
						} else {
							if(data.display == 0){// 不显示
								alert('您好，编号为'+ questionCode +'的问题正在审核中，请稍后再查询，如有问题可拨打967209！');
							}
						}
					}else{// 审核通过
						window.location.href = Context.base + '/ask/questionDetailt.jspa?fid=' + questionCode;
					}
				}
			}
		});
}

function _changePic(){
	jQuery('#verifyCodeImg').attr('src', Context.base + '/captcha/captcha.jspa?rd=' + Math.random());
}

// 更改验证码
function doChangePic(){
	_changePic();
	jQuery('#verifyCodeImgDiv').show();
}

// 显示验证码
function showVerifyDiv(){
	// doChangePic();
	jQuery('#verifyCodeImgDiv').show();
	var verifyCodeImgSrc = jQuery('#verifyCodeImg').attr('src');
	if(typeof(verifyCodeImgSrc) == 'undefined' || verifyCodeImgSrc == ''){
		_changePic();
	}	
}

// 隐藏验证码
function hideVerifyDiv(){
	jQuery('#verifyCodeImgDiv').hide();
}

//按部门检索所有问题tag
function qusByDept(){
  var deptId = document.getElementById('deptId').value;
  Zjport.doSubmit('','govConsult!userQusList.jspa?deptId='+deptId, { 
      receiver: 'use_page_div'
  });
}
//按部门检索已答问题tag
function qusAnsByDept(){
  var deptId = document.getElementById('deptId').value;
  Zjport.doSubmit('','govConsult!userQusAnsList.jspa?deptId='+deptId, { 
      receiver: 'use_page_div'
  });
}
//按部门检索待答问题tag
function qusNoAnsByDept(){
  var deptId = document.getElementById('deptId').value;
  Zjport.doSubmit('','govConsult!userQusNoAnsList.jspa?deptId='+deptId, { 
      receiver: 'use_page_div'
  });
}
//按部门检索催答问题tag
function qusHurryByDept(){
  var deptId = document.getElementById('deptId').value;
  Zjport.doSubmit('','govConsult!userQusHurryList.jspa?deptId='+deptId, { 
      receiver: 'use_page_div'
  });
}
//小喇叭催答
function qusRemindAns(acceptId){
  Zjport.doSubmit('','govConsult!questionRemind.jspa?acceptId='+acceptId, { 
      receiver: 'user_remind_'+acceptId,
      afterReceive : function(){
	      if(jQuery('#message_'+acceptId).val()!=""){
	  	      alert(jQuery('#message_'+acceptId).val());
	  	  }
      }
  });
}
// 点击左侧分类菜单的大类
function clickDept(obj, id){
	var parentDiv = jQuery(obj).parents('.zx_wtfl');
	if(parentDiv != null && parentDiv.length > 0){
		jQuery('.zx_wtfl_sel').attr('class', 'zx_wtfl');
		jQuery(parentDiv).attr('class', 'zx_wtfl_sel');
	}
	searchDept(id);
	scroller('new_ques', 1);
}
// 点击左侧分类菜单的小类
function clickIndustry(obj, id){
	var parentDiv = jQuery(obj).parents('.zx_wtfl');
	if(parentDiv != null && parentDiv.length > 0){
		jQuery('.zx_wtfl_sel').attr('class', 'zx_wtfl');
		jQuery(parentDiv).attr('class', 'zx_wtfl_sel');
	}
	jQuery('.sel').removeAttr('class');
	jQuery(obj).attr('class', 'sel');
	searchIndustry(id);
	scroller('new_ques', 1);
}

//基于全文检索的顶部搜索框自动提示
jQuery(document).ready(function(){
    jQuery("#gcKeywords").autocomplete({
        minLength: 2,
        source: function(request, response){
            jQuery.ajax({
                url: Context.base + "/ask/govConsultAutocomplete.jspa",              
                type: "POST",
                dataType: "json",
                data: request,
                success: function(data){ 
                    response(data);
                }
            });
        },
        select: function(event, ui){
			jQuery("#gcKeywords").val(ui.item.value);
			doSearch();
		}
    });  
});

//基于全文检索的提问搜索提示
jQuery(document).ready(function(){
    jQuery("#questionTitle").autocomplete({
        minLength: 0,
        source: function(request, response){
        	var questionTitle = jQuery('#questionTitle').val();
        	if(questionTitle.length > 1){
        		jQuery("#relevant_answer_none").hide();
        		jQuery("#relevant_answer_list").show();
	        	jQuery('#term').val(questionTitle);
				Zjport.doSubmit('ftsearch4askForm','ftSearch4Ask!ftSearch4Ask.jspa', { 
			  	     receiver: 'relevant_answer_list'
			   	});
		   	}else{
		   		jQuery("#relevant_answer_none").show();
        		jQuery("#relevant_answer_list").hide();
		   	}
        }
    });  
});

// 转换为数字 
function intval(v) 
{ 
    v = parseInt(v); 
    return isNaN(v) ? 0 : v; 
} 

// 获取元素信息 
function getPos(e) 
{ 
    var l = 0; 
    var t  = 0; 
    var w = intval(e.style.width); 
    var h = intval(e.style.height); 
    var wb = e.offsetWidth; 
    var hb = e.offsetHeight; 
    while (e.offsetParent){ 
        l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0); 
        t += e.offsetTop  + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0); 
        e = e.offsetParent; 
    } 
    l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0); 
    t  += e.offsetTop  + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0); 
    return {x:l, y:t, w:w, h:h, wb:wb, hb:hb}; 
} 

// 获取滚动条信息 
function getScroll()  
{ 
    var t, l, w, h; 
     
    if (document.documentElement && document.documentElement.scrollTop) { 
        t = document.documentElement.scrollTop; 
        l = document.documentElement.scrollLeft; 
        w = document.documentElement.scrollWidth; 
        h = document.documentElement.scrollHeight; 
    } else if (document.body) { 
        t = document.body.scrollTop; 
        l = document.body.scrollLeft; 
        w = document.body.scrollWidth; 
        h = document.body.scrollHeight; 
    } 
    return { t: t, l: l, w: w, h: h }; 
} 

// 锚点(Anchor)间平滑跳转 
function scroller(el, duration) 
{ 
    if(typeof el != 'object') { el = document.getElementById(el); } 

    if(!el) return; 

    var z = this; 
    z.el = el; 
    z.p = getPos(el); 
    z.s = getScroll(); 
    z.clear = function(){window.clearInterval(z.timer);z.timer=null}; 
    z.t=(new Date).getTime(); 

    z.step = function(){ 
        var t = (new Date).getTime(); 
        var p = (t - z.t) / duration; 
        if (t >= duration + z.t) { 
            z.clear(); 
            window.setTimeout(function(){z.scroll(z.p.y, z.p.x)},13); 
        } else { 
            st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t; 
            sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l; 
            z.scroll(st, sl); 
        } 
    }; 
    z.scroll = function (t, l){window.scrollTo(l, t)}; 
    z.timer = window.setInterval(function(){z.step();},13); 
}

