var iTimerID;

$(document).ready(function() {
	$("#ask").focus(function() {
		iTimerID = setInterval("overOneThousand()",100);
	});
	$("#ask").blur(function() {
		clearInterval(iTimerID);
		$("#countCheck").attr("style", "display: none;");
	});
});

function overOneThousand() {
	var counts = $("#ask").val().length;
	switch(counts) {
		case 0:
			$("#countCheck").attr("style", "display: block;");
			$("#count").html("0");
			break;
		default:
			$("#countCheck").attr("style", "display: block;");
			$("#count").html(counts);
	}
}

function postData(){
	var name = $("#name").val();
	var email = $("#email").val();
	var ask = $("#ask").val();
	var sMsg = "";
	//检查是否为空
	//姓名
	if(isNull(name)){
		sMsg += "Name is required.\n";
	}
	//Email
	if(isNull(email)){
		sMsg += "E-mail is required.\n";
	} else {
		//检查Email
		if(!isMail(email)){
			sMsg += "E-mail is incorrectly formatted.\n";
		}
	}
	//咨询内容
	if(isNull(ask)){
		sMsg += "Advisory content is required.\n";
	}
	
	//检查字数ask内容是否超过1000
	if(isOverOneThousand($("#ask").val().length)){
		sMsg += "The length of Advisory content surpasses 1000.\n";
	}
	
	if (sMsg != "") {
		alert(sMsg);
		return false;
	}
	
	var Maildata = "address=" + escape($("#address").val())
		+ "&name=" + escape(name)
		+ "&phone=" + escape($("#phone").val())
		+ "&email=" + escape(email)
		+ "&company=" + escape($("#company").val())
		+ "&department=" + escape($("#department").val())
		+ "&ask=" + escape(ask);

		$.ajax({
		   type: "POST",
		   url: "mailsender.asp",
		   data: Maildata,
		   dataType: "text",
		   beforeSend: function(){
			$("#info").html("");
		  	$("#btn_submit").attr("disabled","true");
		   	$("#progressMsg").attr("style","display: block;");
			location.href = "#progressMsg";
		   },
		   success: function(msg){
		  	$("#btn_submit").attr("disabled","");
			msg = msg.substring(0,7);
			if(msg == "success"){
				alert("Success.");
				document.forms["gbook"].reset();
			}else{
				alert("Failure.");
			}
			$("#progressMsg").attr("style","display: none;");
		   },
		   error: function(){
		   	$("#btn_submit").attr("disabled","");
		   	alert("The operation failed because of the failure of server’s response, please retry the operation.");
		   	$("#progressMsg").attr("style","display: none;");
		   }
		}); 
				
	return false;
}
