var checkvalidCodeFlag = false; //检验验证码标识符
var realNameFlag = true; //真实名称验证标识符
var telePhoneFlag = true; //联系电话标识符
var maxlimit = 500;//字数限制

$(document).ready(function(){
	if (typeof window['DWRUtil'] == 'undefined') 
        window.DWRUtil = dwr.util; 
    if (typeof window['DWREngine'] == 'undefined') 
        window.DWREngine = dwr.engine;
});

//初始化提示信息
function initUsableNum(){
	var usableNum = maxlimit - $("#message").val().length;
	if(usableNum > 0) {
		$("#usableNum").html(usableNum);
	} else {
		$("#usableNum").html('0');
	}
}

//文本域字数限制
function wordLimit() {
	var content = $("#message").val();
	if (content.length >= maxlimit) {
		if(content.length > maxlimit) {
			$("#message").val(content.substring(0, maxlimit));
		}
		$("#usableNum").html('0');
	} else {
		$("#usableNum").html(maxlimit - content.length);
	}
}

function changeValidCode() {
	$('#geneValidCode').attr("src", "../valid_code.jsp?id=" + Math.random());
	$('#validCode').val('');
}

//验证码有效性验证
function checkValidCode(){
	var validCode = $('#validCode').val();
	$.ajax({url:'../checkValidCode.do',type:'post',dataType:'json',data:{'rUser.validCode':$('#validCode').val()},
		success:function(data){
			if(data.error  == "S"){
				checkvalidCodeFlag = true;
				$('#validCodeInfo').attr('style','color:red;font-size:12px;text-align:left;');
        		$('#validCodeInfo').html('<img src="http://png.gc73.com.cn/website_img/right_icon.gif" width="15" height="14" style=" vertical-align:bottom;"/>');
			}else{
				checkvalidCodeFlag = false;
				$('#validCodeInfo').attr('style','color:red;font-size:12px;text-align:left;');
        		$('#validCodeInfo').html('<img src="http://png.gc73.com.cn/website_img/error_icon.gif" width="15" height="14" style=" vertical-align:bottom;"/>');
			}
		}
	})
}

//验证用户联系电话
function checkTelePhone(telePhone) {
	var telStr = telePhone;
 	var regm= /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
	var reg=/^1[3458]\d{9}$/;
//	if(telStr == ''){
//		$('#telePhoneInfo').html('');
//		$('#telePhoneInfo').attr('style', 'color:#FF0000;font-size:12px;');
//		$('#telePhoneInfo').html('<img src="http://png.gc73.com.cn/website_img/error_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />'
//								+ '请输入您的联系电话');
//		telePhoneFlag = false;
//		return;
//	}
	
	if(telStr != '' && telStr.length > 0) {
	    if(!reg.test(telStr) && !regm.test(telStr)){
	    	$('#telePhoneInfo').attr('style', 'color:#FF0000;font-size:12px;');
			$('#telePhoneInfo').html('<img src="http://png.gc73.com.cn/website_img/error_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />'
									+ '请输入正确的联系电话');
			telePhoneFlag = false;
			return;
		}
		$('#telePhoneInfo').attr('style', 'color:#009900;font-size:12px;');
		$('#telePhoneInfo').html('<img src="http://png.gc73.com.cn/website_img/right_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />');
		telePhoneFlag = true;
	}
}

//验证用户真实姓名
function checkRealName(name) {
	if (name == null || name.length == 0) {
		realNameFlag = false;
		$('#realNameInfo').attr('style', 'color:#FF0000;font-size:12px;');
		$('#realNameInfo').html('<img src="http://png.gc73.com.cn/website_img/error_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />'
						+ '请输入您的真实姓名');
		return;
	}
	RegisterInfoService.checkRealName(name, {
		callback : function(str) {
			if (str == 'S') {
				realNameFlag = true;
				$('#realNameInfo').attr('style', 'color:#009900;font-size:12px;');
				$('#realNameInfo').html('<img src="http://png.gc73.com.cn/website_img/right_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />');
				return;
			} else {
				realNameFlag = false;
				$('#realNameInfo').attr('style', 'color:#FF0000;font-size:12px;');
				$('#realNameInfo').html('<img src="http://png.gc73.com.cn/website_img/error_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />'
								+ str);
				return;
			}
		},
		timeout : 5000,
		errorHandler : function(message){
			realNameFlag = false;
			alert("系统运行出现问题" + message);
			return;
		}
	});
}

//输入验证码后回车提交
function validateCodeSubmit(event){
	if(event == undefined){
		setTimeout(storeMessage, 500);
	}else if(event.keyCode == 13){
		setTimeout(storeMessage, 500);
	}	
}

//保存用户留言
function storeMessage() {
	var content = $("#message").val();
	var realName = $("#realName").val();
	var telePhone = $("#telePhone").val();
	var questionOneCount = $('input[@name=questionOne][@checked]').size();
	var questionTwoCount = $('input[@name=questionTwo][@checked]').size();
	var r = document.getElementsByName("questionTwo"); 
	var questTwo = new Array();
	var j = 0;
	for(var i = 0; i < r.length; i++){
		if(r[i].checked){
			questTwo[j++] = r[i].value;
		}
	} 
	if(questionOneCount == 0){
		alert("请填写问题一");
		return ;
	}
	if(questionTwoCount == 0){
		alert("请填写问题二");
		return ;
	}
	if("" == realName || !realNameFlag) {
		alert("请正确填写您的姓名");
		return;
	}
	if(!telePhoneFlag) {
		alert("请正确填写您的联系电话");
		return;
	}
	if("" == content || content.length < 1) {
		alert("留言内容不能为空");
		return;
	}
	if(!checkvalidCodeFlag) {
		alert("验证码输入错误");
		return;
	}
	var _href = document.getElementById('submitHref');
	_href.href = 'javascript:void(0);';
	$.ajax({url:"storeMessage.do",type:"post",dataType:"json",
		data:{'questionOne':$('#questionOne').val(),'questionTwo':questTwo.join('|'),
			'realName':$('#realName').val(),'telePhone':$('#telePhone').val(),
			'messageContent':$('#message').val(),'validCode':$('#validCode').val()},
		success:function(data){
			if(data.message == "S"){
				alert("衷心感谢您对波克城市提出的宝贵建议，我们会尽快查看您的建议。");
				window.location.href="../index.html";
				return ;
			}else{
				alert(data.message);
				_href.href = 'javascript:storeMessage();';
				return;
			}
		},
		error:function(data){
			alert("网络异常，请稍候重试");
			_href.href = 'javascript:storeMessage();';
			return ;
		}
		
	});
	//$("#messageFrm").submit();
}
