var sex = 'male';
var marriage = 'single';
var worktime = 'immediately';
var iTimerID;

$(document).ready(function() {
	$("#selfcomment").focus(function() {
		iTimerID = setInterval("overOneThousandSelfcomment()",100);
	});
	$("#selfcomment").blur(function() {
		clearInterval(iTimerID);
		$("#countCheck_selfcomment").attr("style", "display: none;");
	});
	
	$("#experience").focus(function() {
		iTimerID = setInterval("overOneThousandExperience()",100);
	});
	$("#experience").blur(function() {
		clearInterval(iTimerID);
		$("#countCheck_experience").attr("style", "display: none;");
	});

	$("#certificate").focus(function() {
		iTimerID = setInterval("overOneThousandCertificate()",100);
	});
	$("#certificate").blur(function() {
		clearInterval(iTimerID);
		$("#countCheck_certificate").attr("style", "display: none;");
	});
	SelectJob();
});

function overOneThousandSelfcomment() {
	var counts = $("#selfcomment").val().length;
	switch(counts) {
		case 0:
			$("#countCheck_selfcomment").attr("style", "display: block;");
			$("#count_selfcomment").html("0");
			break;
		default:
			$("#countCheck_selfcomment").attr("style", "display: block;");
			$("#count_selfcomment").html(counts);
	}
}

function overOneThousandExperience() {
	var counts = $("#experience").val().length;
	switch(counts) {
		case 0:
			$("#countCheck_experience").attr("style", "display: block;");
			$("#count_experience").html("0");
			break;
		default:
			$("#countCheck_experience").attr("style", "display: block;");
			$("#count_experience").html(counts);
	}
}

function overOneThousandCertificate() {
	var counts = $("#certificate").val().length;
	switch(counts) {
		case 0:
			$("#countCheck_certificate").attr("style", "display: block;");
			$("#count_certificate").html("0");
			break;
		default:
			$("#countCheck_certificate").attr("style", "display: block;");
			$("#count_certificate").html(counts);
	}
}

function setSex(obj){
	sex = obj.value;
}

function setMarriage(obj){
	marriage = obj.value;
}

function setWorktime(obj){
	worktime = obj.value;
}

function postData(){
	var sndName = $("#sndName").val();
	var birthday = $("#birthday").val();
	var where = $("#where").val();
	var education = $("#education").val();
	var phonenum = $("#phonenum").val();
	var email = $("#email").val();
	var job = $("#job").val();
	var money = $("#money").val();
	var sMsg = "";

	//检查是否为空
	//姓名
	if(isNull(sndName)){
		sMsg += "Name is required.\n";
	}
	//出生年月
	if(isNull(birthday)){
		sMsg += "BirthDate is required.\n";
	}
	//户籍
	if(isNull(where)){
		sMsg += "Residence is required.\n";
	}
	//学历
	if(isNull(education)){
		sMsg += "Education is required.\n";
	}
	//联系电话
	if(isNull(phonenum)){
		sMsg += "Telephone number is required.\n";
	}
	//E-mail
	if(isNull(email)){
		sMsg += "E-mail is required.\n";
	} else {
		//检查Email
		if(!isMail(email)){
			sMsg += "E-mail is incorrectly formatted.\n";
		}
	}
	//应聘岗位
	if(isNull(job)){
		sMsg += "The position what you apply for is required.\n";
	}
	//期望工资
	if(isNull(money)){
		sMsg += "Wage expectations is required.\n";
	}
	
	
	//检查字数selfcomment内容是否超过1000
	if(isOverOneThousand($("#selfcomment").val().length)){
		sMsg += "The length of Self introduction surpasses 1000.\n";
	}
	
	//检查字数experience内容是否超过1000
	if(isOverOneThousand($("#experience").val().length)){
		sMsg += "The length of Work experience surpasses 1000.\n";
	}
	
	//检查字数certificate内容是否超过1000
	if(isOverOneThousand($("#certificate").val().length)){
		sMsg += "The length of Certification surpasses 1000.\n";
	}
	
	if (sMsg != "") {
		alert(sMsg);
		return false;
	}
	
	var Maildata = "sndName=" + escape(sndName)
		+ "&sex=" + escape(sex)
		+ "&nation=" + escape($("#nation").val())
		+ "&birthday=" + escape(birthday)
		+ "&where=" + escape(where)
		+ "&marriage=" + escape(marriage)
		+ "&education=" + escape(education)
		+ "&computer=" + escape($("#computer").val())
		+ "&japanese=" + escape($("#japanese").val())
		+ "&english=" + escape($("#english").val())
		+ "&school=" + escape($("#school").val())
		+ "&live=" + escape($("#live").val())
		+ "&phonenum=" + escape(phonenum)
		+ "&postnum=" + escape($("#postnum").val())
		+ "&email=" + escape(email)
		+ "&job=" + escape(job)
		+ "&money=" + escape(money)
		+ "&worktime=" + escape(worktime)
		+ "&mailAddr=" + escape($("#mailAddr").val())
		+ "&selfcomment=" + escape($("#selfcomment").val())
		+ "&experience=" + escape($("#experience").val())
		+ "&certificate=" + escape($("#certificate").val());
		
		$.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["apply"].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;
}

function SelectJob() {
	var sJob = "";
	var sComp = "0";
	var url=location.search;
	if(url.indexOf("?")!=-1){
		var   str   =   url.substr(1);
		var   Request   =   new   Object();
		strs   =   str.split("&");
		for(var i=0;i<strs.length;i++){
			switch(strs[i].split("=")[0]) {
			case "job":
				sJob = unescape(strs[i].split("=")[1]);
				break;
			case "comp":
				sComp = unescape(strs[i].split("=")[1]);
				break;
			default:
				break;
			}
		}
	}
	document.getElementById("job").value = sJob;
	document.getElementById("mailAddr").value = sComp;
}
