$(document).ready(function(){
	check_account();
	$("#email").blur(function(){
		check_account();
	});
	$("#resend_confirm_letter").click(function(){
		
		var addr=prompt("please enter your email address then press ok!","");

		if(addr !=null){
			if(checkMail(addr))
			{	
				$.post("resendconfirm.php", { email: addr } ,function(data)
				{
					if(data=='send')
					{
						alert("The confirm letter send!")
					}
					if(data=='empty')
					{
						alert("This account doesn't exsists!!")
					}
					if(data=='sent')
					{
						alert("This account already passed confirm!")
					}
				});
			}
		}
	});
	$("input[name='pcode[]']").each(function(){
    	var no=$(this).val();
    	if(no!=''){
        	var nopare=no.substr(0,3);
        	if($(this).attr("checked")==true){$("#"+nopare).css("display","block")}
	    }
	})
	$(".insert").mousedown(function(){
		$(".insert").attr("bgcolor","");
		$(this).attr("bgcolor","#FFFCDF");
	});

});
function check_account(){
	if($("#email").val()!=''){
		$.post("/Ajax/check_account.php",{email:$("#email").val()},function(data){
			var stat=$("stat",data).text();
			var msg=$("login_msg",data).text();
			if(stat=="true"){
				$("#check_email_img").attr("src","/images/check_right.gif");
			}else{
				$("#check_email_img").attr("src","/images/check_error.gif");
			}
			$("#check_email_img").attr({width:"13px",height:"13px"});
			$("#check_email").html(msg);
		});
	}
}
function send1(){
	var email=$("#email").val();
		var error_stat=false;
		if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){
            $("#check_email_img").attr("src","/images/check_error.gif");
            $("#check_email").html("Email format error! Example:test@test.com.tw");
            $("#check_email_img").attr({width:"13px",height:"13px"});
            error_stat=true;
    	}
    	if($("#passwd").val().length<6){
    		$("#check_passwd_img").attr("src","/images/check_error.gif");
            $("#check_passwd").html("Password is too short!  It has to be more than 6 digits.");
            $("#check_passwd_img").attr({width:"13px",height:"13px"});
            error_stat=true;
    	}else{
    		if($("#passwd2").val()!=$("#passwd").val()){
    			$("#check_passwd_img2").attr("src","/images/check_error.gif");
            	$("#check_passwd2").html("Password isn't the same!");
            	$("#check_passwd_img2").attr({width:"13px",height:"13px"});
            	error_stat=true;
    		}
    	}
		if(error_stat==true){
			return false;
		}else{
			return true;
		}
}

function checkMail(email)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)){alert('error email format!!');return false;}else{return true;}
}
function CheckPassword(TheForm)
        {
          if ( TheForm.email.value =="" || TheForm.email.value !="" &&  ! /^[_\.\d\w\-]+@([\d\w][\d\w\-]+\.)+[\w]{2,3}$/.test(TheForm.email.value)) 
            { 
              alert("Email address error! Please check your Email again."); 
              TheForm.email.focus();
              return (false); 
            } 

          if ((TheForm.password1.value != TheForm.password2.value) ||
               TheForm.password1.value =='' ||
               TheForm.password2.value =='')
            {
              alert ("Your password and confirm password are not the same.");
              return(false);
            }
          else
            {
              return(true);
            }
        }

function CheckMyEmail(TheForm)
        {
          if ( TheForm.email.value =="" || TheForm.email.value !="" &&  ! /^[_\.\d\w\-]+@([\d\w][\d\w\-]+\.)+[\w]{2,3}$/.test(TheForm.email.value)) 
            { 
              alert("Email address error! Please check your Email again."); 
              TheForm.email.focus();
              return (false); 
            } 
          else
            {
              return(true);
            }
        }


function CheckPassword1(TheForm)
        {
          if (TheForm.email.value =='')
            {
              alert ("Please Input your email !");
              return(false);
            }

          if (TheForm.password.value =='')
            {
              alert ("Please Input your password !");
              return(false);
            }
          else
            {
              return(true);
            }

         
        }


function CheckInfo(TheForm)
{
	var error_img='<img src="/images/check_error.gif">';
	var error=0;
	var checked_btype=0;
	var checked_bcate=0;
	var emsg="";
    if($("input[name='password1']").val().length<6){
    	$("#password1").css("display","block");
    	error=1;
    	emsg+="\nYour password is too short, it has to be more than 6 digits.";
    }else{
    	if($("input[name='password1']").val()!=$("input[name='password2']").val()){
    		$("#password2").css("display","block");
    		error=1;
    		emsg+="\nPasswords are not the same";
    	}else{
    		$("#password1").hide();
    		$("#password2").hide();
    	}
    }
    if($("input[name='address']").val()=='' ){
    	$("#address").css("display","block");
    	error=1;
    	emsg+="\nPlease check your address!";
    }else{
    	$("#address").hide();
    }
    if($("input[name='tel_country']").val()=='' || $("input[name='tel_area']").val()=='' || $("input[name='tel_number']").val()==''){
    	$("#phone").css("display","block");
    	error=1;
    	emsg+="\nPlease check your phone!";
    }else{
    	$("#phone").hide();
    }
     if($("input[name='company']").val()=='' ){
    	$("#company").css("display","block");
    	error=1;
    	emsg+="\nPlease check your company name!";
    }else{
    	$("#company").hide();
    }
    $("input[name^='btype']").each(function(){
    	if($(this).attr("checked")==true){
    		checked_btype=1;
    	}
    });
    $("input[name='pcode[]']").each(function(){
    	if($(this).attr("checked")==true){
    		checked_bcate=1;
    	}
    });
    if(checked_btype==0){
    	$("#btype").css("display","block");
    	error=1;
    	emsg+="\nPlease at least select a business type!";
    }else{
    	$("#btype").hide();
    }
    
	if(checked_bcate==0){
    	$("#bcate").css("display","block");
    	error=1;
    	emsg+="\nPlease at least select a business category!";
    	
    }else{
    	$("#bcate").hide();
    }
    if($("input[name='agree']").attr("checked")!=true){
    	$("#agree").css("display","block");
    	error=1;
    	emsg+="\nPlease read Terms of Use and agree Privacy Policy!";
    }else{
    	$("#agree").hide();
    }
    if(error==1){
    	document.form1.password1.focus();
    	alert("Please modify the information according to the message below.\n"+emsg);
    	return false;
    }else{
    	$("input[name='Submit']").attr("disabled",true);
    }
}
function display(id){
	var display=$("#chk"+id).css("display");
	var disable=$("#chk"+id).attr("disabled");
	if($("#"+id).css('display')=='none'){
		$("#"+id).css("display","block");
		$("#chk"+id).attr("checked",true);
	}else{
		$("#"+id).css("display","none");
		$("#chk"+id).attr("checked",false);
	}
}