$(function(){
	$(".chk_usr").click(function(){
		var ut = /[\W]/g;
		if(ut.test($("#userloginname").val()) || $("#userloginname").val().length<4 || $("#userloginname").val().length>50){
				$("#userloginname").prev("span").text("กรุณากรอก Username ใหม่").removeClass().addClass("sps_warning");
		}else{
				checkUsername($("#userloginname").val());
		}
	});
	$(":text, textarea, :password").prev("span").addClass("hps_warning");
	$("span + :text, span + textarea,  span + :password").focus(function(){
		if(this.id == 'userloginname'){
			if($(this).prev("span").text()!='Username นี้ใช้ได้'){ $(this).prev("span").removeClass().addClass("hps_warning"); }
		}else{ $(this).prev("span").removeClass().addClass("hps_warning");}
		$("#sps_summary").hide();
	});
	$("#ProfileForm").submit(function(event){
		event.preventDefault();
		chkdat();
		if($(".sps_warning").size()>0){
			$("#sps_summary").html("มี "+$(".sps_warning").size()+" ฟิลด์ที่ยังไม่สมบูรณ์<br /><br />").show();
		}else{
			$("#ProfileForm").ajaxSubmit({
			beforeSubmit: function(){
			$("#ProfileForm").hide();
			$("#contact_list").height(150).append("<img src='/images/spin.gif' style='position:absolute; top:65px; right:370px;' />");
			$("html, body").animate({scrollTop:0}, 500);
			},
			success: function(msg){
				if(msg.substring(0, 2)=='OK'){ window.location.href='/profile/index'; }else{ ps_error(msg.substring(0, 2)); } 
			},
			error:function(msg){ps_error(msg.substring(0, 2));} 
			});
		}
	});
	$("#reloadimg a").click(function(event){
		event.preventDefault();
		getCaptchar();
	});
	getCaptchar();
});
function chkdat(){
	var ut = /[\W]/g;
	var ut3 = /\s/g;
	var rm = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	$("span + :text, span + textarea,  span + :password").each(function(){								 
		var flag = false;
		if(this.id=='userloginname' && (ut.test($(this).val()) || this.value.length<4 || this.value.length>50)){
			$(this).prev("span").text("กรุณากรอก Username ใหม่");
			flag = true;
		}else if(this.id=='userpwd' && (ut3.test(this.value) || this.value.length<4 || this.value.length>20)){
				flag = true;
		}else if(this.id=='repwd' && ($.trim($(this).val()).length==0 || ($(this).val())!=($("#userpwd").val()))){
				flag = true;
		}else if(this.id=='seekerage' && (this.value.length==0 || this.value<2 || this.value>90)){
			flag = true;
		}else if(this.id=='seekerweight' && (this.value.length==0 || this.value<20 || this.value>400)){
			flag = true;
		}else if(this.id=='seekerheight' && (this.value.length==0 || this.value<50 || this.value>250)){
			flag = true;
		}else if(this.id=='seekeraddress' && ($.trim($(this).val()).length<4)){
			flag = true;
		}else if(this.id=='seekerpostalcode' && (isNaN($(this).val()) || $.trim($(this).val()).length!=5)){
			flag = true;
		}else if(this.id=='seekeremail' && (!rm.test(this.value))){
			flag = true;
		}else if(this.id=='seekerccn' && (this.value.length==0 || this.value<1000 || this.value>9999)){
			flag = true;
		}else{
			if($.trim($(this).val()).length==0){
				flag = true;
			}
		}
		if(flag){
			$(this).prev("span").removeClass().addClass("sps_warning");
		}else{
			if(this.id == 'userloginname'){
				if($(this).prev("span").text()!='Username นี้ใช้ได้'){
					$(this).prev("span").removeClass().addClass("hps_warning"); 
				}
			}else{ 
					$(this).prev("span").removeClass().addClass("hps_warning");
			}
		}
	});
}
function ps_error(msg){
	$("#contact_list > img[src=/images/spin.gif]").remove();
	$("#contact_list").height("");
	$("#ProfileForm").show();
	if(msg=='CA'){
		$("#seekerccn").prev("span").removeClass().addClass("sps_warning");
		$("#esp_submit").focus();
	}else{
		$("#userloginname").prev("span").removeClass().addClass("sps_warning").text("ไม่สามารถใช้ Username นี้ได้");
	}
	$("#ProfileForm ~ img").remove();
}
function checkUsername(uname){
	$.ajax({
		type: "GET",
		url: "/profile/checkusername/"+uname,
		data: "rand="+Math.random(),
		beforeSend: function(){
			$('.chk_usr').hide().after('<img src="/images/reload.gif" />');
		},
		success: function(msg){
			$(".chk_usr ~ img").remove();
			$('.chk_usr').show();
			if(msg.substring(0, 2)=='OK'){
				$("#userloginname").prev("span").removeClass().addClass("sps_message").text("Username นี้ใช้ได้");
			}else{
				$("#userloginname").prev("span").removeClass().addClass("sps_warning").text("ไม่สามารถใช้ Username นี้ได้");
			}
		}
	});
}
function getCaptchar(){
	$("#ccimg").html('<img src="/captchar/index/'+Math.random()+'" class="captchar_number" align="middle" />');
}


