// JavaScript Document
var checkIDRequestObject = createXMLHttpRequestObject();
function checkAllName()
{
	var regubtn = document.getElementById('regubtn'); 
	regubtn.disabled="disabled";
		
	var myform = document.userinfo; 

	var uname = myform.uname.value;
	var email = myform.email.value;
	var mobile = myform.mobile.value;
	if(uname =='' || uname.length<4 || IsIDPW(uname)==0){
		window.alert("帳號格式不正確"); 
		myform.uname.focus();
		regubtn.disabled=""; 
		return false;
	}
	if(email =='' || IsEmail(email) ==0){
		window.alert("email格式不正確"); 
		myform.email.focus();
		regubtn.disabled=""; 
		return false;
	}
	
	//if ( myform.mobile.value == "" || IsNumber(myform.mobile.value)==0 || myform.mobile.value.length!=10) { window.alert("手機號碼格式不正確 "); myform.pass.focus(); return false; }
	if(mobile =='' ||  IsNumber(mobile)==0 || mobile.length!=10){
		window.alert("手機號碼格式不正確"); 
		myform.mobile.focus();
		regubtn.disabled=""; 
		return false;
	}
			if(checkIDRequestObject)
  				{

					dataSource = "include/checkuserRepeat.php?op=c&u="+uname+"&e="+email+"&m="+mobile+"&";    
    				checkIDRequestObject.open("POST", dataSource);
 					checkIDRequestObject.setRequestHeader("Method", "POST " + dataSource + " HTTP/1.1");
	 				checkIDRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    				checkIDRequestObject.onreadystatechange = function()
    				{
     					try
      					{
       		 				if (checkIDRequestObject.readyState == 4 && checkIDRequestObject.status == 200)
        					{
		  						if(checkIDRequestObject.responseText == 1){
									//window.alert('ok'); 
			 						myform.submit(); 
		  						}else{
									window.alert(checkIDRequestObject.responseText); 
									regubtn.disabled=""; 
									return false;

		  						}
        					}
							else
							{
          						//window.alert(checkIDRequestObject.responseText);	
							}
      					}
      					catch(e){}
    				}
					try
    				{
     					checkIDRequestObject.send(null);
    				}
    				catch(e){}
  				}
}


function checkAllNameByEDIT()
{
	var myform = document.userinfo;
	var uid =myform.uid.value;
	var email = myform.email.value;
	var mobile = myform.mobile.value;
	if(mobile =='' ||  IsNumber(mobile)==0 || mobile.length!=10){
		window.alert("手機號碼格式不正確"); 
		myform.mobile.focus();
		regubtn.disabled=""; 
		return false;
	}
			if(checkIDRequestObject)
  				{

					dataSource = "include/checkuserRepeat.php?op=e&u="+uid+"&e="+email+"&m="+mobile+"&";    
    				checkIDRequestObject.open("POST", dataSource);
 					checkIDRequestObject.setRequestHeader("Method", "POST " + dataSource + " HTTP/1.1");
	 				checkIDRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    				checkIDRequestObject.onreadystatechange = function()
    				{
     					try
      					{
       		 				if (checkIDRequestObject.readyState == 4 && checkIDRequestObject.status == 200)
        					{
		  						if(checkIDRequestObject.responseText == 1){
									//window.alert('ok'); 
			 						myform.submit(); 
		  						}else{
									window.alert(checkIDRequestObject.responseText); 
									regubtn.disabled=""; 
									return false;

		  						}
        					}
							else
							{
          						//window.alert(checkIDRequestObject.responseText);	
							}
      					}
      					catch(e){}
    				}
					try
    				{
     					checkIDRequestObject.send(null);
    				}
    				catch(e){}
  				}
}

function createXMLHttpRequestObject()
{
  var XMLHttpRequestObject = false;
  
  try
  {
    XMLHttpRequestObject = new XMLHttpRequest();
  }
  catch(e)
  {
    var aryXmlHttp = new Array(
                               "MSXML2.XMLHTTP",
                               "Microsoft.XMLHTTP",
                               "MSXML2.XMLHTTP.6.0",
                               "MSXML2.XMLHTTP.5.0",
                               "MSXML2.XMLHTTP.4.0",
                               "MSXML2.XMLHTTP.3.0"
                               );
    for (var i=0; i<aryXmlHttp.length && !XMLHttpRequestObject; i++)
    {
      try
      {
        XMLHttpRequestObject = new ActiveXObject(aryXmlHttp[i]);
      } 
      catch (e) {}
    }
  }

  
  if (!XMLHttpRequestObject)
  {
    alert("Error: failed to create the XMLHttpRequest object.");
  }
  else 
  {
    return XMLHttpRequestObject;
  }
}
function checkBannerForm(){
	
}
//rname,zipcode,address
function xoopsFormValidate_userinfo_first() {
	
	var regubtn = document.getElementById('regubtn'); 
		regubtn.disabled="disabled";
		// regubtn.style.display="none"; 
myform = window.document.userinfo; 
if ( myform.rname.value == "" ) { window.alert("請輸入真實姓名"); myform.email.focus();regubtn.disabled=""; return false; }
if ( myform.pass.value == "" || myform.pass.value.length<6) { window.alert("密碼要多於6字元"); myform.pass.focus();regubtn.disabled="";  return false; }
//if ( myform.zipcode.value.length != 3 || IsNumber(myform.zipcode.value)==0) { window.alert("郵遞區號格式不正確"); myform.zipcode.focus();regubtn.disabled="";  return false; }



if ( myform.address.value == "" ) { window.alert("請輸入住址"); myform.pass.focus();regubtn.disabled="";  return false; }
if ( myform.vpass.value == "" || IsIDPW(myform.vpass.value)==0) { window.alert("核對密碼格式不正確，請使用英文及/或數字組合。"); myform.vpass.focus();regubtn.disabled="";  return false; }
if ( myform.agree_disc.checked == false ) { window.alert("請輸入我同意上述聲明"); myform.agree_disc.focus();regubtn.disabled="";  return false; }
checkAllName();
return true;
}
//rname,zipcode,address,birthday,idno
function xoopsFormValidate_userinfo() {
	
	var regubtn = document.getElementById('regubtn'); 
		regubtn.disabled="disabled";
		// regubtn.style.display="none"; 
myform = window.document.userinfo; 
if ( myform.rname.value == "" ) { window.alert("請輸入真實姓名"); myform.email.focus();regubtn.disabled=""; return false; }
//if ( myform.idno.value == "" || checkID(myform.idno.value)==false ) { window.alert("身分證號碼格式錯誤"); myform.pass.focus();regubtn.disabled="";  return false; }
if ( myform.pass.value == "" || myform.pass.value.length<6) { window.alert("密碼要多於6字元"); myform.pass.focus();regubtn.disabled="";  return false; }
//if ( myform.zipcode.value.length != 3 || IsNumber(myform.zipcode.value)==0) { window.alert("郵遞區號格式不正確"); myform.zipcode.focus();regubtn.disabled="";  return false; }


if ( myform.tel.value.length >0 && IsNumber(myform.tel.value)==0){ window.alert("電話號碼格式不正確 "); myform.tel.focus();regubtn.disabled="";  return false; }
if ( myform.address.value == "" ) { window.alert("請輸入住址"); myform.pass.focus();regubtn.disabled="";  return false; }
if ( myform.vpass.value == "" || IsIDPW(myform.vpass.value)==0) { window.alert("核對密碼格式不正確，請使用英文及/或數字組合。"); myform.vpass.focus();regubtn.disabled="";  return false; }
//if ( myform.agree_disc.checked == false ) { window.alert("請輸入我同意上述聲明"); myform.agree_disc.focus();regubtn.disabled="";  return false; }
checkAllName();
return true;
}
function xoopsFormValidate_userinfo_two() {
	
	var regubtn = document.getElementById('regubtn'); 
		regubtn.disabled="disabled";
		// regubtn.style.display="none"; 
myform = window.document.userinfo; 

if ( myform.idno.value != "" && checkID(myform.idno.value)==false ) { window.alert("身分證號碼格式錯誤"); myform.idno.focus();regubtn.disabled="";  return false; }

if ( myform.tel.value.length >0 && IsNumber(myform.tel.value)==0){ window.alert("電話號碼格式不正確 "); myform.tel.focus();regubtn.disabled="";  return false; }
//checkAllName();
myform.submit();
return true;
}

function xoopsFormValidate_userinfo_edit() { 
	var regubtn = document.getElementById('regubtn'); 
	//regubtn.disabled="disabled";
	//regubtn.disabled=true;
		
myform = window.document.userinfo; 
if ( myform.name.value == "" ) { window.alert("請輸入真實姓名"); myform.email.focus();regubtn.disabled=""; return false; }
//if ( myform.idno.value == "" || checkID(myform.idno.value)==false ) { window.alert("身分證號碼格式錯誤"); myform.pass.focus();regubtn.disabled=""; return false; }
//if ( myform.zipcode.value.length != 3 || IsNumber(myform.zipcode.value)==0) { window.alert("郵遞區號格式不正確"); myform.zipcode.focus(); regubtn.disabled="";return false; }
if ( myform.tel.value.length >0 && IsNumber(myform.tel.value)==0){ window.alert("電話號碼格式不正確 "); myform.tel.focus();regubtn.disabled=""; return false; }
if ( myform.address.value == "" ) { window.alert("請輸入住址"); myform.pass.focus();regubtn.disabled=""; return false; }
if ( myform.vpass.value != myform.pass.value ) { window.alert("兩次輸入密碼不正確。"); myform.vpass.focus();regubtn.disabled=""; return false; }
checkAllNameByEDIT();
return true;
}
function IsIDPW(strVal){
	var objRe = /[a-zA-Z0-9]$/;
	if(objRe.test(strVal)) {
		return 1;
	}else{
		return 0;
	}
}
//function IsNumber(strVal){
//	var objRe = /[0-9]$/;
//	if(objRe.test(strVal)) {
//		return 1;
//	}else{
//		return 0;
//	}
//}
function IsNumber(s)
{
	var patrn=/^[0-9]{1,20}$/;
	if (!patrn.exec(s)) return 0;
	return 1;
}
function IsEmail(strEmail) {
var objRe = /^[\-_.a-z0-9]+@([\-_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
	if(objRe.test(strEmail)) {
		return 1;
	}else{
		return 0;
	}
}
function checkID(idStr){
  var letters = new Array('A', 'B', 'C', 'D',
      'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
      'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
      'X', 'Y', 'W', 'Z', 'I', 'O');
  var multiply = new Array(1, 9, 8, 7, 6, 5,
                           4, 3, 2, 1);
  var nums = new Array(2);
  var firstChar;
  var firstNum;
  var lastNum;
  var total = 0;
  var regExpID=/^[a-z](1|2)\d{8}$/i;
  if (idStr.search(regExpID)==-1) {
   return false;
  } else {
	firstChar = idStr.charAt(0).toUpperCase();
	lastNum = idStr.charAt(9);
  }
  for (var i=0; i<26; i++) {
	if (firstChar == letters[i]) {
	  firstNum = i + 10;
	  nums[0] = Math.floor(firstNum / 10);
	  nums[1] = firstNum - (nums[0] * 10);
	  break;
	}
  }
  for(var i=0; i<multiply.length; i++){
    if (i<2) {
      total += nums[i] * multiply[i];
    } else {
      total += parseInt(idStr.charAt(i-1)) *
               multiply[i];
    }
  }
  if ((10 - (total % 10))!= lastNum) {
	return false;
  }
  return true;
}