//
/*
 Require : /.+/,
 Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
 Phone : /^(\+?\d{2,3}\-)?((\(\d{2,3}\))|(\d{2,3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{3,4})?$/,
 Mobile : /^((\(\d{3}\))|(\d{3}\-)|(\+\d{2,3}))?13\d{9}$/,
 Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
 IdCard : /^\d{15}(\d{2}[A-Za-z0-9])?$/,
 Currency : /^\d+(\.\d+)?$/,
 Number : /^\d+$/,
 Zip : /^[1-9]\d{5}$/,
 QQ : /^[1-9]\d{4,8}$/,
 Integer : /^[-\+]?\d+$/,
 Double : /^[-\+]?\d+(\.\d+)?$/,
 English : /^[A-Za-z]+$/,
 Chinese :  /^[\u0391-\uFFE5]+$/,
 UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,
 ymdDate:/^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$/
usage:
in form tagName add onsubmit="return checkForm(this)"
in input tagName add onblur="return check(this,strCheckType,{objMoreInfo})"
strCheckType:notNull,tel,email,date,number,confirm
objMoreInfo:can no use
notNull:{min:a,max:b}
confirm:{matchTo:strComfirmID}
* :{unique:strCheckUrl}// ajax 
* :{require:false} {hideTip:true}

*/
//
function getOs()
{ 
    var OsObject = ""; 
   if(navigator.userAgent.indexOf("MSIE")>0) { 
        return "MSIE"; 
   } 
   if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){ 
        return "Firefox"; 
   } 
   if(isSafari=navigator.userAgent.indexOf("Safari")>0) { 
        return "Safari"; 
   } 
   if(isCamino=navigator.userAgent.indexOf("Camino")>0){ 
        return "Camino"; 
   } 
   if(isMozilla=navigator.userAgent.indexOf("Gecko")>0){ 
        return "Gecko"; 
   } 
   
}
function GetObj(objName){
	if(document.getElementById){
		return eval('document.getElementById("' + objName + '")');
	}else{
		return eval('document.all.' + objName);
	}
}

//trim spaces with regular expression
String.prototype.trim = function()
{	
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

//Count string byte number, return integer
String.prototype.ByteCount = function()
{	
	txt = this.replace(/(<.*?>)/ig,"");
	txt = txt.replace(/([\u0391-\uFFE5])/ig, "11");
	var count = txt.length;
	return count;
}

function checkForm(container) {
	var errMsg='填写有误! 请检查';
	var result=true;
    var items = container;//.all;
    if(items.length != null) {
        for(var i=0 ; i<items.length ; i++) {
            var item = items[i];
			
            if(item.onblur != null) {				
				var resultThis=item.onblur();
				if(!resultThis && result){
					result=resultThis;
					item.focus();
					if(item.lang)errMsg+="\n"+item.lang;
				}                
            }
        }
    }
    if(!result)alert(errMsg);
	
    return result;
}

function check(thisInput,thisType,moreInfo){	
	thisType=thisType.toLowerCase();
	if(thisType==='notnull'){return checkNotNull(thisInput,moreInfo);}
	if(thisType==='date')return checkDate(thisInput,moreInfo);
	if(thisType==='email')return checkEmail(thisInput,moreInfo);	
	if(thisType==='number')return checkNumber(thisInput,moreInfo);	
	if(thisType==='function')return checkFunction(thisInput,moreInfo);
	if(thisType==='confirm')return checkConfirm(thisInput,moreInfo);
	if(thisType==='tel')return checkTel(thisInput,moreInfo);
	if(thisType==='mobile')return checkMobile(thisInput,moreInfo);
	if(thisType==='phone')return checkPhone(thisInput,moreInfo);
	if(thisType==='chinese')return checkChinese(thisInput,moreInfo);
	if(thisType==='zhuanye')return checkZhuanye(thisInput,moreInfo);
	
}
function checkChinese(thisInput,moreInfo){
	var result=true;
	var count = thisInput.value.trim().ByteCount();
	var regChinese=/^[\u0391-\uFFE5 ]+$/ ;
	if(!thisInput.value.trim().match(regChinese))	result=false;	
	if(moreInfo && moreInfo.min){
		if(count<moreInfo.min)result=false;
	}
	if(count==0)result=-1;
	return showInfo(result,moreInfo,thisInput);
}

function checkZhuanye(thisInput,moreInfo){
	var result=true;
	var count = thisInput.value.trim().ByteCount();
	var regZhuanye=/^[\u0391-\uFFE5 ]+$/ ;
	if(!thisInput.value.trim().match(regZhuanye))	result=false;	
	if(moreInfo && moreInfo.min){
		if(count<moreInfo.min)result=false;
	}
	if(count==0)result=-1;
	return showInfo(result,moreInfo,thisInput);
}

function checkPhone(thisInput,moreInfo){
	return checkMobile(thisInput,moreInfo) || checkTel(thisInput,moreInfo);	
}
function checkNotNull(thisInput,moreInfo){
	var result=true;
	var count = thisInput.value.trim().ByteCount();
	if(moreInfo){}
	if (thisInput.value.trim() == "") {
		result=false;
	}	
	if(moreInfo){
		if(moreInfo.reg){//正则验证
			//alert(moreInfo.reg);
			if(!new RegExp(moreInfo.reg,"g").test(thisInput.value)){result=false;}			
		}
		if(typeof moreInfo.min=='number'){
			if(count<moreInfo.min)result=false;
		}
		if(typeof moreInfo.max=='number'){
			if(count>moreInfo.max)result=false;
		}
	}
	if(count==0)result=-1;
	return showInfo(result,moreInfo,thisInput);
	
}

function checkDate(thisInput,moreInfo){
	var result=true;
	var count = thisInput.value.trim().ByteCount();
	
	var DateString=thisInput.value.trim();
	
	var tDateString =DateString.replace('-','/');
	var tempDate = new Date(tDateString);
	if (isNaN(tempDate))result= false;
	if(count==0)result=-1;
	return showInfo(result,moreInfo,thisInput);
	
}

function checkEmail(thisInput,moreInfo){
	var result=true;
	var count = thisInput.value.trim().ByteCount();
	
	var re = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@([\-a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
	if ( (thisInput.value.trim() == "") ) {
		result=false;
	}
	else if (!thisInput.value.match(re))
	{
		result=false;
	}
	if(count==0)result=-1;
	return showInfo(result,moreInfo,thisInput);
	
}

function checkNumber(thisInput,moreInfo)
{	var result=true;
	var count = thisInput.value.trim().ByteCount();
	
	if( count == 0 )
	{
		result=false;
	}
		
	number = new Number(thisInput.value.trim());
    if (isNaN(number)) {
        result=false;
    }
	if(count==0)result=-1;
	return showInfo(result,moreInfo,thisInput);
}

function checkTel(thisInput,moreInfo)
{	var result=true;	
	var count = thisInput.value.trim().ByteCount();
	
	if( count<7 || count>25 ){
		result=false;
	}
	var re = /^(\+?\d{2,3}\-)?((\(\d{2,3}\))|(\d{2,3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{3,4})?$/;
	//var re = /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
	if (!re.test(thisInput.value))	{
		result=false;
	}
	if(count==0)result=-1;
	return showInfo(result,moreInfo,thisInput);
}
function checkMobile(thisInput,moreInfo)
{	var result=true;	
	var count = thisInput.value.trim().ByteCount();
	
	if( count<11 || count>17 ){
		result=false;
	}
	var re =/^((\(\+?\d{2,3}\))|(\+?\d{2,3}\-)|(\+\d{2,3}))?1(3|5|8){1}\d{9}$/;
	//var re = /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
	if (!re.test(thisInput.value))	{
		result=false;
	}
	if(count==0)result=-1;
	return showInfo(result,moreInfo,thisInput);
}
function checkConfirm(thisInput,moreInfo){
	var result=true;	
	var count = thisInput.value.trim().ByteCount();
	if (thisInput.value!==GetObj(moreInfo.matchTo).value)	{
		result=false;
	}
	if(count==0)result=-1;
	return showInfo(result,moreInfo,thisInput);
}

function sendUniqueData(url,objAjaxData){
	var ajax=new Ajax(url,receiveUniqueData,objAjaxData,false);				
	ajax.add(objAjaxData.inputObj.name,objAjaxData.inputObj.value);	
	ajax.send('timeStamp='+new Date());
}
function receiveUniqueData(obj){
	if(obj.status==200){
		if(obj.text.indexOf('check_ok')>=0){
			obj.msg='输入值可以使用!';
		}else{
			obj.msg='(输入值已经被使用,请输入别的值!)';
			obj.result=false;			
		}
	}else{
		obj.msg='(服务器出错,请重试或联系客服!)';
		obj.result=false;
	}
}
function showWord(inputObj,result,msg){
	/*result:
	-2,提示加载,请等
	-1:未填
	-3:清除Tip
	 true:正确
	 false:错误
	*/
	//创建元素
	var oTipLabel=window.document.createElement('label');
	if(inputObj.id!==''){
		oTipLabel.setAttribute('id',inputObj.id+'TipDiv');
		var oldTipDiv=GetObj(inputObj.id+'TipDiv');
		if(oldTipDiv){
			if(getOs()=='Firefox')oldTipDiv.parentNode.replaceChild(oTipLabel,oldTipDiv); 
			else oldTipDiv.replaceNode(oTipLabel);
		}
	}else{
		oTipLabel.setAttribute('id',inputObj.name+'TipDiv');
		var oldTipDiv=GetObj(inputObj.name+'TipDiv');
		if(oldTipDiv)oldTipDiv.replaceNode(oTipLabel);
	}
	if(result==-3){oTipLabel.style.display='none';return;}
	var oIcon=window.document.createElement('font');	
	oTipLabel.appendChild(oIcon);
	var oTipText=window.document.createElement('span');	
	oTipLabel.appendChild(oTipText);
	oIcon.style.fontWeight='bold';	
	oTipLabel.style.padding='2px 0 0 2px';
	oTipLabel.style.margin='0px 2px 0px 2px';
	//创建元素结束
	if(result==true){
				//控制
				oIcon.style.color='green';
				oIcon.innerHTML='√';
				oTipText.innerHTML='正确!';
				oTipText.style.color='green';//颜色
		}else if(result==false){			
				oIcon.style.color='red';
				oIcon.innerHTML='×';
				oTipText.innerHTML='有误!';
				oTipText.innerHTML+=msg;
				oTipText.style.color='#E42E83';//颜色
				
		}else if(result==-1){			
				oIcon.style.color='red';
				oIcon.innerHTML='×';
				oTipText.innerHTML='未填!';
				oTipText.style.color='#E42E83';//颜色
		}else if(result==-2){
				oIcon.style.color='red';
				oIcon.innerHTML='i';
				oTipText.innerHTML=msg;
				oTipText.style.color='green';//颜色
		}
		//添加元素
		if(getOs()=='Firefox'){//fireFox
			inputObj.insertAdjacentElement=function(where,parsedNode){ 
				switch(where){ 
					case "beforeBegin": 
						this.parentNode.insertBefore(parsedNode,this); 
						break; 
					case "afterBegin": 
						this.insertBefore(parsedNode,this.firstChild); 
						break; 
					case "beforeEnd": 
						this.appendChild(parsedNode); 
						break; 
					case "afterEnd": 
						if(this.nextSibling) 
							this.parentNode.insertBefore(parsedNode,this.nextSibling); 
						else 
							this.parentNode.appendChild(parsedNode); 
						break; 
					} 
				}	
		}
		inputObj.insertAdjacentElement('afterEnd',oTipLabel);
}
function showInfo(result,moreInfo,inputObj){
	var objAjaxData=new Object();
	objAjaxData.msg='';
	var hideTip=false;
	if(moreInfo){
		
		if(moreInfo.require===false&&result==-1){showWord(inputObj,-3,'');result=true;return result;}
		if(moreInfo.unique&&result==true){//处理唯一性
			showWord(inputObj,-2,'正在检查数据,请稍候...');
			objAjaxData.result=result;
			objAjaxData.inputObj=inputObj;
			sendUniqueData(moreInfo.unique,objAjaxData);
			showWord(inputObj,-3,'');
			result=objAjaxData.result;
		}	
		if(moreInfo.hideTip==true)hideTip=true;
	}
	if(!hideTip){
		showWord(inputObj,result,(inputObj.lang?inputObj.lang:'')+objAjaxData.msg)
	}	
	return (result==1);
}
//包括Js
function IncludeJS(tag, fileUrl, source){ 
        var oAppendTag = document.getElementsByTagName(tag).item(0); 
        var oScript = document.createElement( "script" ); 
		oScript.language = "javascript"; 
        oScript.type = "text/javascript"; 
        oScript.defer = true; 
        if(fileUrl){oScript.src=fileUrl;}else if(source&&source!==''){oScript.text = source; }
        oAppendTag.appendChild( oScript );
}
function bindEventListener(target,type,handler){		
		if(window.document.all)
			target.attachEvent("on" + type, handler );
		else
			target.addEventListener(type, handler, false);
}
function addAjax(){
	IncludeJS('html', '/ameng/library/js/ajax.js');
}
bindEventListener(window,'load', addAjax);
