/* Ver 2.1 by Arrack */
var isclick=0;
function checkForm(formname){
	var errormsg='';
	var currentTime = new Date();
	var year = currentTime.getFullYear();
	//檢查性別跟身分證字號關係
	//checkField(str1,msg,type,str2,notaddstr,minlength,maxlength)
	try{
		var c1=document.getElementById("ischeck1").value;
		var c2=document.getElementById("ischeck2").value;
		//var c3=document.getElementById("ischeck3").value;
		var c3='1';

		if(c1!="" &&c2!=""&c3!=""){
		}else{
			errormsg+="尚有錯誤，請確認畫面上紅色字，或是必填項目未填寫\n";
		}		
	} catch (e){
		errormsg+="尚有錯誤，請確認畫面上紅色字，或是必填項目未填寫\n";
	}
	//
	errormsg+=checkField('username','姓名未填寫或是長度錯誤(2-4)','chstr','',1,2,4);
	//errormsg+=checkField('twid','身分證號未填寫','twid','',1);
	errormsg+=checkField('sex','性別未選擇','','',1);
	errormsg+=checkField('by','生日未填寫','','',1);
	errormsg+=checkField('address2','地址未填寫','chstr','',1);
	errormsg+=checkField('reg_account','帳號只接受小寫英文和數字','enstr','',1,4,12);
	errormsg+=checkField('reg_password','兩次輸入的密碼不相同','equal','reg_password2',1);
	errormsg+=checkField('reg_password','密碼長度限制在4-24','','',1,4,24);
	errormsg+=checkField('email','EMAIL未輸入或是格式錯誤','email','',1);
	errormsg+=checkField('tel','電話未輸入或是格式錯誤','numeric','',1);
	if($("#tel").val()==""){
		errormsg+=checkField('cellphone','電話未輸入或是格式錯誤','numeric','',1);
	}
	errormsg+=checkField('isargee','條款未同意','','',1);

	if(errormsg==''){
		if(isclick==0){isclick=1;document.getElementById(formname).submit();		
		}else{alert('please wait submit()');isclick=0;}	
	}else{alert(errormsg);isclick=0;}
}
function getxmlhttp(){
	var xmlhttp
	if (window.XMLHttpRequest) {xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}}}
	return xmlhttp;
}
function getContents(xmlhttp,msgname) {
	var msg=document.getElementById(msgname);
	if (xmlhttp.readyState == 4) {
		if(xmlhttp.status == 200) {
			//alert(xmlhttp.responseText);
			msg.innerHTML=xmlhttp.responseText;
		} else {
			//alert('error');
		}
	}
}
function AccountCheck() {
	if(!enstr(a$('reg_account').value))
	{a$('msg_reg_account').innerHTML='帳號只接受4-12位小寫英文和數字\n';return false;}
	
	var xmlhttp = getxmlhttp();
	var account = document.getElementById("reg_account").value;
	if (!xmlhttp) {alert('Error');return false;}
    xmlhttp.onreadystatechange = function() { 
		getContents(xmlhttp,"msg_reg_account");
	};
	url = "check.php?kind=1&account=" + encodeURI(account);
    xmlhttp.open('GET', url, true);
	xmlhttp.send(null);
}
function EmailCheck() {
	if(checkField('email','EMAIL未輸入或是格式錯誤','email','',1)!=''){
		a$('msg_email').innerHTML='格式錯誤';
		return false;
	}
	var xmlhttp = getxmlhttp();
	var email = document.getElementById("email").value;

	if(email!=""){
		if (!xmlhttp) {alert('Error');return false;}
		xmlhttp.onreadystatechange = function() { 
			getContents(xmlhttp,"msg_email");
		};
		url = "check.php?kind=2&email=" + encodeURI(email);
		xmlhttp.open('GET', url, true);
		xmlhttp.send(null);
	}
}

function twidCheck() {
	var xmlhttp = getxmlhttp();
	var twid = document.getElementById("twid").value;

	t=checkTWID(document.getElementById('twid').value);
	if(t!=''){document.getElementById("msg_twid").innerHTML="<span style='color:red'>"+t+"</span>"; return false;}
	else{document.getElementById("msg_twid").innerHTML='';}
	
	if(twid!=""){
		if (!xmlhttp) {alert('Error');return false;}
		xmlhttp.onreadystatechange = function() { 
			getContents(xmlhttp,"msg_twid");
		};
		url = "check.php?kind=3&twid=" + encodeURI(twid);
		xmlhttp.open('GET', url, true);
		xmlhttp.send(null);
	}
}

function copy_address_function(){
	if($('#copy_address').attr('checked')){
		$('#zipcode2').val($('#zipcode').val());
		showZone(document.getElementById('city2'),document.getElementById('town2'), document.getElementById('zipcode2').value);
	}
}
