// JavaScript Document
var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
var px = document.layers ? "" : "px";
function JSFX_FloatDiv(id, sx, sy)
{
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};
	el.flt=function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.flt()", 40);
	}
	return el;
}

	$(document).ready(function(){
		JSFX_FloatDiv("float_comment", 	  0,   0).flt();

	/*	$("#txtUsername").focus(function(){
			show_comment('username_comment');
		});		
		$("#txtUsername").blur(function(){
			hide_comment('username_comment');
			check_username();
		});
		$("#check_username_button").click(function(){
			check_ajax_user();
		});
		$("#txtUsername").keyup(function(){
			var str = $("input#txtUsername").val() + "@tech24.vn";
			$("#tech24mail_example").html(str);
		});*/
		
		$("#txtPassword").focus(function(){
			show_comment('pass_comment');
		});
		$("#txtPassword").blur(function(){
			hide_comment('pass_comment');
			check_pass();
		});
		
		$("#txtRepass").focus(function(){
			show_comment('pass_comment');
		});
		$("#txtRepass").blur(function(){
			hide_comment('pass_comment');
			check_repass();
		});
		
		$("#txtEmail").focus(function(){
			show_comment('email_comment');
		});
		$("#txtEmail").blur(function(){
			hide_comment('email_comment');
			check_email();
		});
		
		$("#txtReemail").blur(function(){
			check_reemail();
		});
		
		$("#txtFullname").blur(function(){
			check_fullname();
		});
		$("#txtGraduation").blur(function(){
			check_graduation();
		});
		$("#txtJob").blur(function(){
			check_job();
		});
		$("#txtAnswer").blur(function(){
			check_answer();
		});		
		
		$("#choose_avatar").click(function(){
			check_choose_avatar();
		});
		
		$("#txtBirthday").blur(function(){
			check_birthday();
		});
		$("#cbBirthmonth").change(function(){
			check_birthmonth();
		});
		$("#cbBirthyear").change(function(){
			check_birthyear();
		});
		$("#cbProvince").change(function(){
			check_province();
		});		
		
		$("#cbCountry").change(function(){
			var country_id = $("select#cbCountry").val();
			$.ajax({
			   type:"POST",
			   url:"/index.php?mod=user&task=ajax_change_country&ajax",
			   data:"country_id="+country_id,
			   success: function(response_text){
				   $("#cbProvince").html(response_text);
			   }
		   });
		});
	});
	function onchange() {
			//alert('ffdfd');
		check_ajax_user();
		return false;
		}
	function check_ajax_user() {
			
			if(check_username()){
				var username = $("input#txtUsername").val();
				var username_available_span = '<span style="color:#2586F3;">'+username_available+'</span>';
				
				$.ajax({
					type:"POST",
					url:"/index.php?mod=user&task=ajax_check_username&ajax",
					data:"username="+username,
					beforeSend: function(){
						$("#alert_username").html('<img src="/images/ajax_check_username.gif" align="left" />');
					},
					success: function(response_text){
						if(response_text == '2')
						{
							check_correct('username');
							$("#error_username").html(username_available_span);
							$("#abc").html('change');

						}
						else
							{
							    check_fail('username',alert_username_exist);		
								$("input#check_ajax_exit").val(1);
							  
							}
					}
				});
			}
		}
	function validate_form(){
	
		if (check_email() & check_reemail() & check_pass() & check_repass() &  check_fullname() &  check_answer() & (check_birthday() && check_birthmonth() && check_birthyear()) & check_province() & check_agree())
		{
			$("#loading_image").css("display","block");
			return true;			
		}
		else
		{
			if (!check_birthday())
				$("#txtBirthday").focus();
			if (!check_answer())
				$("#txtAnswer").focus();	
			if (!check_fullname())
				$("#txtFullname").focus();				
			if (!check_repass())
				$("#txtRepass").focus();	
			if (!check_pass())
				$("#txtPassword").focus();	
			if (!check_reemail())
				$("#txtReemail").focus();	
			if (!check_email())
				$("#txtEmail").focus();		
			
			return false;
		}
	}

	function show_block(id)
	{
		document.getElementById(id).style.display='block';	
	}
	function hide_block(id)
	{
		document.getElementById(id).style.display='none';	
	}
	
	function check_fail(id,content)
	{
		document.getElementById("alert_"+id).innerHTML ='<img src="/themes/default/images/icon/error_icon.png" align="left" />';
		document.getElementById("error_"+id).innerHTML = content;
	}
	function check_correct(id)
	{
		var str = '<img src="/themes/default/images/icon/correct_icon.png" align="left" />';
		document.getElementById("alert_"+id).innerHTML = str;
		document.getElementById("error_"+id).innerHTML = '';
	}
	
	function show_comment(id)
	{
		document.getElementById(id).className = "hightlight_comment";
	}
	function hide_comment(id)
	{
		document.getElementById(id).className = "";
	}
	function onFocus()
		{
			if($("input#txtUsername").val()!='')
			{
				$("input#check_ajax_exit").val('0');
				check_ajax_user();
				return false;
			}
		}
	function check_username()
	{
		var re = /^[a-z0-9\.\_]+$/;
		var pos = $("input#txtUsername").val().search(re);
		
		if($("input#check_ajax_exit").val()==1)
			return false;
			
		if($("input#txtUsername").val() == '')
		{
			check_fail('username',alert_username_blank);
			return false;
		}
		else if($("input#txtUsername").val().length < 6 || $("input#txtUsername").val().length > 24)
		{
			check_fail('username',alert_username_length);
			return false;
		}
		else if(pos == -1)
		{
			check_fail('username',alert_username_character);
			return false;
		}
		else
		{
			check_correct('username');
			return true;
		}
		
	}
	function check_pass()
	{
		if($("input#txtPassword").val() == '')
		{
			check_fail('pass',alert_password_blank);
			return false;
		}
		else if($("input#txtPassword").val().length < 6)
		{
			check_fail('pass',alert_password_length);
			return false;
		}
		else
		{
			check_correct('pass');
			return true;
		}
	}
	function check_repass()
	{
		if($("input#txtRepass").val() == '')
		{
			check_fail('repass',alert_repass_blank);
			return false;
		}
		else if($("input#txtRepass").val() != $("input#txtPassword").val())
		{
			check_fail('repass',alert_repass_wrong);
			return false;
		}	
		else{
			check_correct('repass');
			return true;
		}
	}
	function check_email(){
		var email = $("input#txtEmail").val();
		var apos=email.indexOf("@");
		var dotpos=email.lastIndexOf(".");
		
		if(email == '')
		{
			check_fail('email',alert_email_blank);
			return false;
		}
		else if (apos<1||dotpos-apos<2){
			check_fail('email',alert_email_format);
			return false;
		}
		else{
			check_correct('email');
			return true;
		}
	}
	function check_reemail()
	{
		if($("input#txtReemail").val() == '')
		{
			check_fail('reemail',alert_reemail_blank);
			return false;
		}
		else if($("input#txtReemail").val() != $("input#txtEmail").val())
		{
			check_fail('reemail',alert_reemail_wrong);
			return false;
		}	
		else{
			check_correct('reemail');
			return true;
		}
	}
	function check_fullname()
	{
		if($("input#txtFullname").val() == '')
		{
			check_fail('fullname',alert_fullname_blank);
			return false;
		}
		else{
			check_correct('fullname');
			return true;
		}
	}
	function check_graduation()
	{
		if($("input#txtGraduation").val() == '')
		{
			check_fail('graduation',alert_graduation_blank);
			return false;
		}
		else{
			check_correct('graduation');
			return true;
		}
	}
	function check_job()
	{
		if($("input#txtJob").val() == '')
		{
			check_fail('job',alert_job_blank);
			return false;
		}
		else{
			check_correct('job');
			return true;
		}
	}
	function check_answer()
	{
		if($("input#txtAnswer").val() == '')
		{
			check_fail('answer',alert_answer_blank);
			return false;
		}
	/*	else if(isNaN($("input#txtAnswer").val())){
			check_fail('answer',alert_answer_format);
			return false;
		}*/
		else{
			check_correct('answer');
			return true;
		}
	}
	function check_agree()
	{
		if ($('form #chkAgree').is(':checked'))
		{
			check_correct('agree');
			return true;
		}
		else{
			check_fail('agree',alert_agree_check);
			return false;
		}
	}
	function check_choose_avatar(){
		var username = $("input#txtUsername").val();
		if( username == ''){
			check_fail('avatar',alert_avatar_choose);
		}
		else{
			$("#alert_avatar").html("");
			$("#error_avatar").html("");
			var opener=window.open ("/lib/popup_avatar/popup_avatar.php?file_name="+username, "sendtofriend", "location=1,status=1,scrollbars=1,width=520,height=400")
		}
	}
	function check_birthday(){
		var birthday = $("input#txtBirthday").val();
		if( birthday == ''){
			check_fail('birthday',alert_birthday_blank);
			return false;
		}
		else if (isNaN(birthday))
		{
			check_fail('birthday',alert_birthday_number);
			return false;
		}
		else if(birthday >31 || birthday <0){
			check_fail('birthday',alert_birthday_wrong);
			return false;
		}
		else{
			$("#alert_birthday").html("");
			$("#error_birthday").html("");
			return true;
		}
	}
	
	function check_birthmonth(){
		var birthmonth = $("select#cbBirthmonth").val();
		if (birthmonth == ''){
			check_fail('birthday',alert_birthmonth_select);
			return false;
		}
		else{
			$("#alert_birthday").html("");
			$("#error_birthday").html("");
			return true;
		}
	}
	function check_birthyear(){
		var birthyear = $("select#cbBirthyear").val();
		if (birthyear == ''){
			check_fail('birthday',alert_birthyear_select);
			return false;
		}
		else{
			$("#alert_birthday").html("");
			$("#error_birthday").html("");
			return true;
		}
	}
	function check_province(){
		var province = $("select#cbProvince").val();
		if (province == ''){
			check_fail('address',alert_province_select);
			return false;
		}
		else{
			check_correct('address');
			return true;
		}
	}
