Validator.registerElement("FCKElement", {
	options : {
		min : 1,
		max : 0
	},
	validate :function(){
		syncFckValue(this.element.id);
		return this.parent();
	},
	process : function(){
		var len=this.value.length;
		// alert(len);
		// alert(this.options.min);
		return (this.options.min==0 || this.options.min<=len) && (this.options.max==0 || this.options.max>=len) 
	}
});
Validator.registerElement("UserNameElement", {
	options : {
		badword : 'admin,root,oleva'
	},
	process : function(){
		return /^[a-z]\w{3,19}$/.test(this.value) 
			&&  this.options.badword != null 
			&& !new RegExp(this.options.badword.split(/\s*[,\uff0c]\s*/).join("|"), "gi").test(this.value);
	}
});
function validateForm(formID){
	return Validator.setup({
		form : formID,
		// configs : 'attribute,tag',
		warns : 'tips, class',
		step : true,
		// color : {warn :'black', pass:'white'},
		// summary : { id : 'summary'},
		tips : {position:'right', offset:{x:0, y:-10}}
	})
}
// //////////////////////////////////////////////////////
function sure(title,content,callback){
	if(!$I('__confirmDiv')){
		var oWrapper=document.createElement('DIV');
		oWrapper.innerHTML='<div class="tcbox" id="__confirmDiv" style="display:none;" >\
							  <div class="tcline">\
								<div class="tctitle">\
								  <p id="__confirmTitle" class="p2">确认操作</p>\
								  <p class="p1"><a href="javascript:TSPopup.closeDiv()"><img src="../images/share/x_06.jpg" /></a></p>\
								</div>\
								<div class="tccon">\
								  <div id="__confirmContent" class="tcbig">\
									<p>确定继续？</p>\
								  </div>\
								  <div class="tcbtbox">\
									<input class="okbtn" type="button" id="__confirmOK" value="确定" />\
									&nbsp;&nbsp;&nbsp;&nbsp;\
									<input class="cancelbtn" type="button" value="取消" onclick="TSPopup.closeDiv();"/>\
								  </div>\
								</div>\
							  </div>\
							</div>';
		document.body.appendChild(oWrapper);
	}
	title = title==null?'确认操作':title;
	$I('__confirmTitle').innerHTML=title;
	$I('__confirmContent').innerHTML=content;
	$I('__confirmOK').onclick=function(){
		var rs=callback();
		if(rs==null || rs)
			TSPopup.closeDiv();
	};
	
	TSPopup.showDiv('__confirmDiv');
}

function info(title, content, callback){
	if(!$I('__alertDiv')){
		var oWrapper=document.createElement('DIV');
		oWrapper.innerHTML='<div class="tcbox" id="__alertDiv" style="display:none;" >\
							  <div class="tcline">\
								<div class="tctitle">\
								  <p id="__alertTitle" class="p2">提示</p>\
								  <p class="p1"><a href="#" id="__alertClose"><img src="../images/share/x_06.jpg" /></a></p>\
								</div>\
								<div class="tccon">\
								  <div id="__alertContent" class="tcbig">\
									<p>点击确定</p>\
								  </div>\
								  <div class="tcbtbox">\
									<input class="okbtn" type="button" id="__alertOK" value="确定" />\
								  </div>\
								</div>\
							  </div>\
							</div>';
		document.body.appendChild(oWrapper);
	}
	title = title==null?'提示':title;
	$I('__alertTitle').innerHTML=title;
	$I('__alertContent').innerHTML=content;
	$I('__alertClose').onclick = $I('__alertOK').onclick = function(){
		TSPopup.closeDiv();
		if(callback) callback();
	};
	
	TSPopup.showDiv('__alertDiv');
}
function gologin(id){
	if(id==1)
		location.href = '../member/login.shtml';
	else
		location.href = '../member/register.shtml';
}
function gotologin(title, content, callback){
	if(!$I('__alertDiv')){
		var oWrapper=document.createElement('DIV');
		oWrapper.innerHTML='<div class="tcbox" id="__alertDiv" style="display:none;" >\
							  <div class="tcline">\
								<div class="tctitle">\
								  <p id="__alertTitle" class="p2">提示</p>\
								  <p class="p1"><a href="#" id="__alertClose"><img src="../images/share/x_06.jpg" /></a></p>\
								</div>\
								<div class="tccon">\
								  <div id="__alertContent" class="tcbig">\
									<p>点击确定</p>\
								  </div>\
								  <div class="tcbtbox">\
									<input class="okbtn" type="button" id="__alertOKl" value="会员登录" />\
									<input class="okbtn" type="button" id="__alertOKr" value="会员注册" />\
								  </div>\
								</div>\
							  </div>\
							</div>';
		document.body.appendChild(oWrapper);
	}
	title = title==null?'提示':title;
	$I('__alertTitle').innerHTML=title;
	$I('__alertContent').innerHTML=content;
	$I('__alertOKl').onclick = function(){
		TSPopup.closeDiv();	
		gologin(1);
	};
	$I('__alertOKr').onclick = function(){
		TSPopup.closeDiv();
		gologin(2);
	};
	$I('__alertClose').onclick = function(){
		TSPopup.closeDiv();	
	};
	
	TSPopup.showDiv('__alertDiv');
}

// //////////////////////////////////////////////////////
String.prototype.trim = function() {
	var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);
	return (m == null) ? '' : m[1];
}

function $I(id){
	var obj = document.getElementById(id);
	if(obj==null){
		var objs=$N(id);
		if(objs.length==1)
		obj=objs[0];
	}
	return obj;
}

function $V(id){
	if($I(id))
		return $I(id).value;
	else 
		return null;
}

function $N(name){
	return document.getElementsByName(name);
}
// 将txt复制到剪贴板
function copyToClipboard(text2copy) {
    try{
    	if(window.clipboardData){
    		 window.clipboardData.setData("Text",text2copy);
    	}else{
	 		var baseURL = getBaseURL();
		    var flashcopier = 'flashcopier';
		    if(!document.getElementById(flashcopier)){
		        var divholder = document.createElement('div');
		        divholder.id = flashcopier;
		        document.body.appendChild(divholder);
		    }
		    document.getElementById(flashcopier).innerHTML = '';
		    var divinfo = '<embed src="' + baseURL + '_clipboard.swf" FlashVars="clipboard='
		    	+text2copy+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';// 这里是关键
		    document.getElementById(flashcopier).innerHTML = divinfo;
    	}
	    return true;
    }catch(e){return false;}
}
function openWindow(src,name,width,height,left,top){
	if(!left){
		left=(screen.width-width)/2;
	}
	if(!top){
		top=(screen.height-height)/2;
	}	
	return window.open(src,encodeURIComponent(name),"menubar=no,toolbar=no,location=no,directories=no,"
		+"status=no,scrollbar=no,resizable=yes,width="+width
		+",height="+height+",left="+left+",top="+top);
}
function getBaseURL() {
	var scripts = document.getElementsByTagName('script');
	var thisFile='util.js';
	for ( var i = 0; i < scripts.length; i++) {
		if (eval('/' + thisFile.replace('.', '\\.') + '$/').test(
				scripts[i].src)) {
			return scripts[i].src.substring(0, scripts[i].src.length
					- thisFile.length);
		}
	}
}
// ///////////////////////////////////////////////////
function setRadioValue(name, value){
    var objs = $N(name);
    for(var i=0;i<objs.length;i++){
		if(objs[i].value==value){
			objs[i].checked=true;
			break;
		}
	}
}
function setCheckboxValue(name, values){
	if(typeof(values)=='string')
		eval('values='+values);
		
    var objs = $N(name);
    for(var i=0;i<objs.length;i++){
		for(var j=0;j<values.length;j++){
			if(objs[i].value==values[j]){
				objs[i].checked=true;
				break;
			}
		}
	}
}
function remote(url, data, callback, async, onFailure){
	if(async == null)
		async=true;
	
	var myReq=new Request({
		url : url,
		evalScripts : true,
		async : async
		});
	if (callback) {
		myReq.onSuccess = callback;
	}
	if (onFailure) {
		myReq.onFailure = onFailure;	
	}
	myReq.send(data);
}
// ////////////////////////////////////////////////////////////
function checkRadio(name, cautionName){
    var objs = $N(name);
    for(var i=0;i<objs.length;i++){
		if(objs[i].checked)
			return true;
	}
	alert("请选择 " + cautionName);
	return false;
}
function getRadioValue(name){
    var objs = $N(name);
    for(var i=0;i<objs.length;i++){
		if(objs[i].checked)
			return objs[i].value;
	}
	return null;
}	
function checkTextarea(id, cautionName, maxLen){
	if($V(id).length > maxLen){
		alert(cautionName+" 长度请勿超过 "+maxLen+" 个字符");
		$I(id).focus();
		return false;
	}
	return true;
}

function countTextArea(textId,showId,max){
	var oText=$I(textId);
	oText._oShow=$I(showId);
	oText._max=max;
	oText._reCount=function(){
		var len=this.value.length;
		var left=(this._max > len ? (this._max - len) : 0);
		this._oShow.innerHTML=left;
	}
	oText.onkeyup=oText._reCount;
	oText.onchange=oText._reCount;
	oText._reCount();
}

function checkNull(id, cautionName){
	if(!$V(id) || $V(id).trim()==''){
		alert("请填写 " + cautionName);
		$I(id).value='';
		$I(id).focus();
		return false;
	}
	return true;
}

function isNull(id){
	if(!$V(id) || $V(id).trim()==''){
		return true;
	}
	return false;
}

function checkAccount(id, cautionName){
	if(/^[a-zA-Z0-9\-_]+$/.test($V(id)))
		return true;
	else{
		alert(cautionName + " 格式不正确");
		$I(id).focus();
		return false;
	}
}

function checkPwd(id, cautionName){
	if(/^\w{4,}$/.test($V(id)))
		return true;
	else{
		alert(cautionName + " 格式不正确");
		$I(id).focus();
		return false;
	}
}

function checkEqual(id1, id2, cautionName1, cautionName2){
	if(checkPwd(id1, cautionName1) && checkPwd(id2, cautionName2)){
		if($V(id1)!=$V(id2)){
			alert(cautionName1 + " 与 " + cautionName2 +" 内容不一致");
			$I(id1).focus();
			return false;
		}
		return true;
	}
	return false;
}

function checkEmail(id, cautionName){
	if(/^[a-z0-9_]{1}[a-z0-9\-_]*(\.[a-z0-9\-_]+)*@[a-z0-9]{1}[a-z0-9\-_]*(\.[a-z0-9\-_]+)*\.[a-z]{2,4}$/.test($V(id)))
		return true;
	else{
		alert(cautionName + " 格式不正确");
		$I(id).focus();
		return false;
	}
}

function checkURL(id, cautionName){
	if(/^http(s)?:\/\/([a-z0-9\-_]+\.)+[a-z]{2,4}$/.test($V(id)))
		return true;
	else{
		alert(cautionName + " 格式不正确");
		$I(id).focus();
		return false;
	}
}

function checkNumber(id, cautionName){
	if(/^[0-9]+$/.test($V(id)))
		return true;
	else{
		alert(cautionName + " 格式不正确");
		$I(id).focus();
		return false;
	}
}
function checkNumberRange(id, cautionName, minValue, maxValue){
	if(!/^-?\d+$/.test($V(id))){
		alert(cautionName + " 格式不正确");
		$I(id).focus();
		return false;
	}
	if(minValue!=null && maxValue!=null ){
		if($V(id)<minValue || $V(id)>maxValue){
			alert(cautionName + " 取值应当在 "+minValue+" 和 "+maxValue+" 之间");
			$I(id).focus();
			return false;
		}
		return true;
	}else{
		if(minValue!=null){
			if($V(id)<minValue){
				alert(cautionName + " 取值应当不小于 "+minValue);
				$I(id).focus();
				return false;
			}
			return true;
		}else{
			if($V(id)>maxValue){
				alert(cautionName + " 取值应当不大于 "+maxValue);
				$I(id).focus();
				return false;
			}
			return true;
		}
	}
}

function checkFixedLength(id, cautionName, value){
  if(value!=null){
	if($V(id).length<value||$V(id).length<value){
	  alert(cautionName + " 长度应当为 "+value+"位");
	  $I(id).focus();
	  return false;
	}
  }
  return true;
}

function checkLength(id, cautionName, minValue, maxValue){
	if(minValue!=null && maxValue!=null ){
		if($V(id).length<minValue || $V(id).length>maxValue){
			alert(cautionName + " 长度应当在 "+minValue+"位 和 "+maxValue+"位 之间");
			$I(id).focus();
			return false;
		}
		return true;
	}else{
		if(minValue!=null){
			if($V(id).length<minValue){
				alert(cautionName + " 长度应当不小于 "+minValue+"位");
				$I(id).focus();
				return false;
			}
			return true;
		}else{
			if($V(id).length>maxValue){
				alert(cautionName + " 长度应当不大于 "+maxValue+"位");
				$I(id).focus();
				return false;
			}
			return true;
		}
	}
}

function checkTel(id, cautionName){
	if(/^[0-9\-,]+$/.test($V(id)))
		return true;
	else{
		alert(cautionName + " 格式不正确");
		$I(id).focus();
		return false;
	}
}

function checkMobile(id, cautionName){
	if(/^1[358]\d{9}$/.test($V(id)))
		return true;
	else{
		alert(cautionName + " 格式不正确");
		$I(id).focus();
		return false;
	}
}
function checkText(id, cautionName){
	if(/^[\w\d\-\u0391-\uFFE5]+$/.test($V(id)))
		return true;
	else{
		alert(cautionName + " 格式不正确");
		$I(id).focus();
		return false;
	}
}
function checkFile(id, cautionName, allowedFile){
	if(!allowedFile)
		allowedFile='jpg,jpeg,gif,png,bmp,doc,xls,ppt,mp3,wma,avi,wmv,mpg';
	allowedFile=','+allowedFile.toLowerCase()+',';
	
	if(isNull(id)){
		alert("请选择需要上传的 " + cautionName);
		$I(id).focus();
		return false;
	}
	var path=$V(id);
	var ext='';
	if(path.lastIndexOf('.')!=-1)
		ext=path.substring(path.lastIndexOf('.')+1).toLowerCase();
	if(allowedFile.indexOf(','+ext+',')==-1){
		alert("不支持的文件类型，请重新选择需要上传的 " + cautionName);
		$I(id).focus();
		return false;
	}
	return true;
}
