﻿function toQuery(S){
	var Str=[];
	for(var k in S)
		Str.push(k+'='+S[k]);
	return Str.join("&");
}
function trim(t){
	var str=/(^\s*)|(\s*$)/g;
	return t.replace(str,"");   
}
function empty(t){ 
	return t==null||t==undefined||t.length<=0;
}
function isNum(t){
	var str=/^[-\+]?[\d]+\.?\d*$/;
	return str.test(t);
}
function isInt(t){ 
	var str=/^[-\+]?\d+$/;
	return str.test(t);
}
function isFloat(t){ 
	var str=/^[-\+]?\d+(\.\d+)?$/;
	return str.test(t);
}
function isMail(mstr){
	var str=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	return str.test(mstr);
}
function isIP(t){
	var str=/^((\d{1,2}|1\d{1,2}|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d{1,2}|2[0-4]\d|25[0-5])$/;
	return str.test(t);
}
var hash="";
function forhash(url){
	Parse={
		value:'',
		CallBack:function(){
			hash=arguments[0];
		}
	}
	mAjax(url,"forhash",Parse);
}
var fmchk=Object;
var chk_box=Object;
fmchk={
	chk_showid:false,
	arr:null,//arr(names,items,ops:array())
	blurs:false,
	db_tb:"",
	func:function(){},
	ajaxurl:"ajax/check.php",
	ajaxok:true,
	chkfiled:function(obj,errs,filed,srt){
		regchk={value:'db_tb='+fmchk.db_tb+'&fileds='+filed+'&values='+srt,
				callBack:function(t){
					if(parseInt(t)!=1){
						fmchk.ajaxok=false;
						fmchk.shor_err(obj,'s',errs);
					}else{
						fmchk.ajaxok=true;
						fmchk.shor_err(obj,'y');
					}
				}
		}
		mAjax(fmchk.ajaxurl,filed,regchk);
	},
	shor_err:function(obj,key,errs){
		if(fmchk.chk_showid){
			if($("chk_"+obj.id)==null){
				var chk_err=document.createElement("span");
				chk_err.setAttribute('id',"chk_"+obj.id);
				chk_err.style.color="#ccc";
				chk_err.style.fontSize="12px";
				obj.parentNode.appendChild(chk_err);
			}
		}
		if(!fmchk.chk_showid){
			alert(errs);
		}else{
			if(key==='n')$("chk_"+obj.id).innerHTML="<img src='/inc_js/img/fail.gif' />";
			else if(key==='y') $("chk_"+obj.id).innerHTML="<img src='/inc_js/img/valid.gif' />";
			else $("chk_"+obj.id).innerHTML="<img src='/inc_js/img/attention.gif' />"+errs;
		}
		try{
		if(!fmchk.blurs)obj.focus();
		}catch(e){}
		return false;
	},
	init:function(fm){
		for(var k in fmchk.arr){
			if($(fmchk.arr[k]['id'])==null)continue;
			$(fmchk.arr[k]['id']).setAttribute("pno",k);
			$(fmchk.arr[k]['id']).onfocus=function(){
				if(this.value.length<=0)fmchk.shor_err(this,'n');
			}
			$(fmchk.arr[k]['id']).onkeypress=function(){
				if(this.value.length<=0)fmchk.shor_err(this,'y');
			}
			$(fmchk.arr[k]['id']).onblur=function(){
				fmchk.blurs=true;
				if(fmchk.arr[this.getAttribute('pno')]['items']=='str'||fmchk.arr[this.getAttribute('pno')]['items']=='mail')
					return fmchk[fmchk.arr[this.getAttribute('pno')]['items']](this,fmchk.arr[this.getAttribute('pno')]['pars'],fmchk.arr[this.getAttribute('pno')]['ckajax']);
				else return fmchk[fmchk.arr[this.getAttribute('pno')]['items']](this,fmchk.arr[this.getAttribute('pno')]['pars']);
			}
		}
		chk_box[fm]=function(){
			fmchk.blurs=false;
			arr=fmchk.arr;
			for(var k in arr){
				if($(fmchk.arr[k]['id'])==null)continue;
				if(!fmchk[arr[k]['items']]($(arr[k]['id']),arr[k]['pars']))return false;
			}
			return true;
		}
		$(fm).onsubmit=function(){
			fmchk.func();
			if(fmchk.ajaxok) return chk_box[this.id]();
			else return false;
		}
	},
	str:function(obj,arr,ckajax){
		$(obj).value=trim(obj.value);
		if(empty($(obj).value)) return this.shor_err($(obj),'s',arr[2]+string_t1);//不能為空!
		else this.shor_err($(obj),'y');
		if($(obj).value.length<arr[0]&&arr[0]!=0) return this.shor_err($(obj),'s',arr[2]+string_t2+arr[0]+string_t4);//不得少於個字符
		else this.shor_err($(obj),'y');
		if($(obj).value.length>arr[1]&&arr[1]!=0) return this.shor_err($(obj),'s',arr[2]+string_t3+arr[1]+string_t4);
		else this.shor_err($(obj),'y');
		if(ckajax) this.chkfiled($(obj),string_t12,$(obj).name,$(obj).value);
		return true;
	},
	slt:function(obj,arr){
		$(obj).value=trim($(obj).value);
		if($(obj).value==arr[1]) return this.shor_err($(obj),'s',string_t5+arr[0]);//請選擇正的
		else this.shor_err($(obj),'y');
		return true;
	},
	list:function(obj,arr){
		if($(obj).options.length<=0) return this.shor_err($(obj),'s',string_t11+arr[0]);//請選擇正的
		else this.shor_err($(obj),'y');
		return true;
	},
	pwd:function(obj,arr){
		$(obj).value=trim($(obj).value);
		$(arr[0]).value=trim($(arr[0]).value);
		if(arr[5]||!empty($(obj).value)){
			if(empty($(obj).value)) return this.shor_err($(obj),'s',arr[3]+string_t1);//不能為空!
			else this.shor_err($(obj),'y');
			
			if($(obj).value.length<arr[1]&&arr[1]!=0) return this.shor_err($(obj),'s',arr[3]+string_t2+arr[1]+string_t4);//不得少於個字符
			else this.shor_err($(obj),'y');
			
			if($(obj).value.length>arr[2]&&arr[2]!=0) return this.shor_err($(obj),'s',arr[3]+string_t3+arr[2]+string_t4);
			else this.shor_err($(obj),'y');
			
			if(empty($(arr[0]).value))  return this.shor_err($(arr[0]),'s',arr[4]+string_t1);//不能為空
			else this.shor_err($(arr[0]));
			
			if($(obj).value!=$(arr[0]).value) return this.shor_err($(arr[0]),'s',string_t6);//兩次密碼不一至
			else this.shor_err($(arr[0]));
		}
		return true;
	},
	mail:function(obj,arr,ckajax){
		$(obj).value=trim($(obj).value);
		if(empty($(obj).value)) return this.shor_err($(obj),'s',arr[0]+string_t1);//不能為空
		else this.shor_err($(obj),'y');
		if(!isMail($(obj).value)) return this.shor_err($(obj),'s',arr[0]+string_t7);//不正確
		else this.shor_err($(obj),'y');
		if(ckajax) this.chkfiled($(obj),string_t12,$(obj).name,$(obj).value);
		return true;
	},
	num:function(obj,arr){
		$(obj).value=trim($(obj).value);
		if(empty($(obj).value)) return this.shor_err($(obj),'s',arr[1]+string_t7);//不正確
		else this.shor_err($(obj),'y');
		if(arr[0]=='int'&&!isInt($(obj).value)) return this.shor_err($(obj),'s',arr[1]+string_t8);//必須為整數
		else if(arr[0]=='float'&&!isFloat($(obj).value)) return this.shor_err($(obj),'s',arr[1]+string_t9);//必須為小數
		else if(!isNum($(obj).value)) return this.shor_err($(obj),'s',arr[1]+string_t10);//必須為數字
		else this.shor_err($(obj),'y');
		return true;
	}
}





