function chkCustomize(id){
	var objs = new Array();
	objs[0] = "first";
	objs[1] = "second";
	objs[2] = "third";
	var selectedObj=document.getElementById(id);
	for(var i=0; i<objs.length; i++){
		if(id==objs[i])
			continue;
		else
		{
			var obj=document.getElementById(objs[i]);
			if(obj.value==selectedObj.value&&selectedObj.value!=''){
				selectedObj.value='';
				alert('此選項已選過.');
				return false;
			}
		}
	}
}

function chkCharacterNum(id){
	var obj=document.getElementsByName(id)[0].value;
	re = /\W/;
	if (re.test(obj))
	{
	    alert("只允許輸入英文,數字以及底線");
	    //document.getElementsByName(id)[0].value="";
	    document.getElementById(id).value="";
	    //document.getElementById(id).focus();
	    window.setTimeout( function(){ document.getElementById(id).focus(); }, 0);
	    return false;
	}	
}

function chkPwd(id, pwdId){
	chkCharacterNum(id);
	var pwdConfirm=document.getElementsByName(id)[0].value;
	var pwd=document.getElementsByName(pwdId)[0].value;
	if(pwdConfirm!=pwd.substring(0,pwdConfirm.length)){
		alert("請確認密碼");
		document.getElementsByName(id)[0].value="";
		//document.getElementById(id).focus();
		window.setTimeout( function(){ document.getElementById(id).focus(); }, 0);
		return false;
	}
	else
		return true;
}

function chkPwdFormat(id){	
	chkCharacterNum(id);
	var obj=document.getElementsByName(id)[0];
	var objValue=obj.value;
	if(objValue.length>0&&objValue.length<4){
		alert("密碼長度至少4碼");
		//document.getElementById(id).focus();
		window.setTimeout( function(){ document.getElementById(id).focus(); }, 0);
		return false;
	}
}

//flag: true (show alert immidiately), false (show later)
function chkEmpty(id,fieldName,flag){
	var errorString="";
	var ids=id.split("#");
	var names=fieldName.split("#");
	//alert(ids.length);
	for(var i=0;i<ids.length;i++){		
		var idValue=document.getElementsByName(ids[i])[0].value;
		//var idValue=document.getElementById(ids[i]).value;
		if(idValue.length==0){		
			errorString=errorString+names[i]+"不能為空白\n";								
		}				
	}
	
	if(errorString.length!=0){		
		alert(errorString);		
		return false;
	}
	else{
		return true;
	}
}

function removeBad(id) { 
	var strTemp=document.getElementsByName(id)[0].value;
	//var strTemp=document.getElementById(id).value;
    strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g,""); 
    document.getElementsByName(id)[0].value=strTemp; 
    //document.getElementById(id).value=strTemp;   
} 

function chkEmail(id) {
	 var emailObj=document.getElementsByName(id)[0];
	 var email=emailObj.value;
	 if(email.length!=0)
	  {
	    if (email.charAt(0)=="." ||        
	         email.charAt(0)=="@"||       
	         email.indexOf('@', 0) == -1 || 
	         email.indexOf('.', 0) == -1 || 
	         email.lastIndexOf("@")==email.length-1 || 
	         email.lastIndexOf(".")==email.length-1)
	     {
	      alert("Email的格式不正確!");
	      //emailObj.focus();
	      window.setTimeout( function(){ document.getElementById(id).focus(); }, 0);
	      return false;
	      }
	   }
	   email=removeBad(id);
	 	 
	 return false;
} 

function chkNUM(id)
{
	var numObj=document.getElementsByName(id)[0];
	var num=document.getElementsByName(id)[0].value;
	if(num!='')
	{
		for(var i=0;i<num.length;i++){
			if(isNaN(num.charAt(i))){
				alert("需要是數字格式");
				//numObj.focus();
				window.setTimeout( function(){ document.getElementById(id).focus(); }, 0);
				return false;			
			}
		}	
		return true;
	}
	else
		return true;
}

function chkAddr(id)
{
	var addrObj=document.getElementsByName(id)[0];
	var addr=document.getElementsByName(id)[0].value;
	for(var i=0;i<addr.length;i++){
		if(isNaN(addr.charAt(i))&&addr.charAt(i)!="-"){
			alert("只能是數字或符號-");
			//addrObj.focus();
			window.setTimeout( function(){ document.getElementById(id).focus(); }, 0);
			return false;			
		}
	}	
}

function chkPhone(id)
{
	var phoneObj=document.getElementsByName(id)[0];
	var phone=document.getElementsByName(id)[0].value;
	for(var i=0;i<phone.length;i++){
		if(isNaN(phone.charAt(i))&&phone.charAt(i)!="-"&&phone.charAt(i)!="#"&&phone.charAt(i)!="("&&phone.charAt(i)!=")"){
			alert("不符合電話號碼格式");
			//phoneObj.focus();
			window.setTimeout( function(){ document.getElementById(id).focus(); }, 0);
			return false;			
		}
	}	
}

function chkRegisterInput(id,fieldName){
	if(!document.getElementById('policy').checked){
		alert("請同意使用條款內容");
		return false;
	}	
	else if(chkEmpty(id,fieldName,false)){
		document.registerForm.submit();
	}			
}

function chkPostInput(id,fieldName){
	if(chkEmpty(id,fieldName,false)){
		document.postForm.submit();
	}			
}

function chkPostInfoInput(lat,lng,id,fieldName){
	if(chkLatLng(lat, lng)&&chkEmpty(id,fieldName,false)){
		document.forms.postForm.submit();
	}			
}

function chkPostBizInput(lat,lng,id,fieldName){
	if(chkLatLng(lat, lng)&&chkEmpty(id,fieldName,false)){
		document.postForm.submit();
	}			
}

function chkLatLng(lat, lng){
	if(document.getElementById(lat).value==''||document.getElementById(lng).value=='')
	{
		alert('您必須按"確認Map地址是否存在"');
		return false;
	}
	else
		return true;
}
/*
function chkUsername(id,url){
	var obj=document.getElementById(id);
	var objValue=obj.value;
	if(objValue==""){
		alert("請輸入帳號");
		obj.focus();
		return false;
	}	
}
*/
