/**********************************************************\
 * 常用正则表达式的定义
 *---------------------------------------------------------
 * 
\**********************************************************/
var	Require	= /.+/;
var	Email	= /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
var	Phone	= /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/;
var	Mobile	= /^((\(\d{2,3}\))|(\d{3}\-))?1\d{10}$/;
var	Url		= /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
var	Currency= /^\d+(\.\d+)?$/;
var	Number	= /^\d+$/;
var	Zip		= /^[1-9]\d{5}$/;
var	QQ		= /^[1-9]\d{4,8}$/;
var	Integer	= /^[-\+]?\d+$/;
var	Double	= /^[-\+]?\d+(\.\d+)?$/;
var	English	= /^[A-Za-z]+$/;
var	Chinese	= /^[\u0391-\uFFE5]+$/;
var RealName= /^[\u0391-\uFFE5]{2,10}$/;
var	Username= /^\w{5,16}$/;
var	Password= /^\w{5,16}$/;
var	UnSafe	= /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/;


/**********************************************************\
 * 对相关数据类型的扩展
 *---------------------------------------------------------
 * 
\**********************************************************/

/**
 * 给String类型添加一个trim方法,去掉字符串两边的空字符
 */
String.prototype.trim	= function(){
	return this.replace(/(^\s*)|(\s*$)/g, '');
};
/**
 * 将字符串以UTF-8字符集编码
 * 
 * @param val	待编码的字符串
 * @return 按UTF-8编码后的字符串,例如: 你->%E4%BD%A0
 */
function URLEncode(val){
	return encodeURIComponent(encodeURIComponent(val));
}
/**
 * 检查身份证号码是否符合规则
 * 
 * @param _card_num	要检查的身份证号码
 * @return ok 或 错误信息
 */
function checkIDCard(_card_num){
	//check null
	if(_card_num == null){
		return "Null ID Card number";
	}
	//check length
	if(!(_card_num.length==15 || _card_num.length==18)){
		return "ID Card number length not match";
	}	
	//check ID Card if contains illegal char or not
	if(!/(^\d{15}$)|(^\d{17}[0-9|x|X]{1}$)/.test(_card_num)){
		return "Contain illegal char";
	}
	//check birth date
	var	birth	= _card_num.length==15 ? "19"+_card_num.substring(6,12) : _card_num.substring(6,14);
	var	year	= birth.substring(0,4);
	var	month	= birth.substring(4,6);
	var	date	= birth.substring(6,8);
	var	MONTH_NAME	= {'1':'January', '2':'February', '3':'March', '4':'April', '5':'May', '6':'June', '7':'July', '8':'August', '9':'September', '10':'October', '11':'November', '12':'December'};
	if(month>12){ return 'Month out of range'; }
	if(month==2){
		if((year%4==0 && year%100!=0) || year%400==0){
			if(date>29){
				return 'The date of '+MONTH_NAME[month]+' greater than 29';
			}
		}else{
			if(date>28){
				return 'The date of '+MONTH_NAME[month]+' greater than 28';
			}
		}
	}else{
		if(month==4 || month==6 || month==9 || month==11){
			if(date > 30){
				return 'The date of '+MONTH_NAME[month]+' greater than 30';
			}
		}else{
			if(date > 31){
				return 'The date of '+MONTH_NAME[month]+' greater than 31';
			}
		}
	}
	//verify check bit
	if(_card_num.length == 18){
    	var	wi	= new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
    	var	ai	= "10x98765432";
    	var	sum	= 0;
    	for(var i=0; i<18-1; i++){
    		sum	+= wi[i] * parseInt(_card_num.substring(i, i+1));
    	}
    	var	position	= sum % 11;
    	if(ai.substring(position, position+1) != _card_num.toLowerCase().substring(17)){
    		return "Invalid check bit";
    	}
	}
	//check area
	this.areaIndex	= -1;
	var	PCODE_LIST	= ['11','12','13','14','15','21','22','23','31','32','33','34','35','36','37','41','42','43','44','45','46','50','51','52','53','54','61','62','63','64','65','71','81','82'];
	var	pcode		= _card_num.substring(0,2);
	for(var i=0; i<PCODE_LIST.length; i++){
		if(PCODE_LIST[i] == pcode){
			this.areaIndex	= i;
			break;
		}
	}
	if(this.areaIndex == -1){
		return "ID Card illegal area";
	}
	return 'ok';
}

/**
 * URL处理, 获取URL中的参数
 * <pre>
 * e.g.
 * var	jsr	= new JSRequest("https://sq.qidian.com/wwww/ShowClub.aspx?id=23432&f=567");
 * 
 * alert("path: "+myurl.Path);
 * alert("pathandQuery: "+myurl.PathAndQuery);
 * alert("queryString: "+myurl.QueryString);
 * alert("id="+myurl.getParameter("id")); //获取查询参数的值
 * alert("f="+myurl.getParameter("f"));
 * </pre>
 * 
 * @param _url	utlde
 * @return
 */
function JSRequest(_url){
	if(_url){
        this.url	= _url;
	}else{
		this.url	= document.URL.indexOf('#')<=0 ? document.URL : document.URL.substring(0, document.URL.indexOf('#'));
	}

    this.reg	= new RegExp("(http|https|ftp)://([-a-z0-9_.]+)(/[-a-z0-9_.!/@&=\+,.~%\$\?]*)","gmi");
    this.reg.test(this.url);

    this.HostName=RegExp.$2; //域名
    this.Protocal=RegExp.$1; //url协议
    this.PathAndQuery=RegExp.$3; //路径和查询参数
    this.Path=this.url.indexOf('?')>0?RegExp.$3.substring(0,RegExp.$3.indexOf('?')):RegExp.$3; //路径
    this.QueryString=this.url.indexOf('?')>0?RegExp.$3.substring(RegExp.$3.indexOf('?')+1):''; //查询参数
}
//获取Url的查询参数值
JSRequest.prototype.getParameter = function (name){
	if(/\?(.+)$/.test(this.url)){
		var ta=RegExp.$1;
		var r = new RegExp(name+"=([^&]+)","gmi");
		if(r.test(ta)){
			return unescape(RegExp.$1);
		}else{
			return '';
		}
	}else{
		return '';
	}
}
/**********************************************************\
 * 闪烁的文本框
 *---------------------------------------------------------
 * 使文本框在鼠标进入和移出时分别显示不同的颜色
 * 
\**********************************************************/
function BLINK_BORDER(cMouseOver, cMouseOut){
	var	objs	= document.getElementsByTagName("input");
	for(var i=0; i<objs.length; i++){
		if(objs[i].type=="text" || objs[i].type=="password"){
			objs[i].style.border	= "1px solid "+cMouseOut;
			objs[i].style.width	= 160;
			objs[i].onmouseover	= function(){
				this.style.border	= "1px solid "+cMouseOver;
			};
			objs[i].onmouseout	= function(){
				this.style.border	= "1px solid "+cMouseOut;
			};
		}
	}
}

/**
 * 向head中添加一个style标签
 * 
 * @param cssStr	css样式
 * @return
 */
function addStyle(cssStr){
	try{//IE
		var	style	= document.createStyleSheet();
		style.cssText = cssStr;
	}catch(e){	//Firefox,Opera,Safari,Chrome
		var	style	= document.createElement("style");
		style.type	= "text/css";
		style.textContent	= cssStr;
		var	_head_	= document.getElementsByTagName("HEAD");
		_head_.item(0).appendChild(style);
	}
}

/**********************************************************************\
 * __☆★| 锁屏功能 |★☆__
 *---------------------------------------------------------------------
 * 说明:
 *      当要执行摸个操作时先调用 lock() 方法, 束时时在调用 unlock() 方法.
 *      LOCK_IMGAGE_URL 为loading图片的位置,可根据实际情况更改
 * 作者: Alex Zhao
 * 时间: 2008-11-13 14:51
\**********************************************************************/
var	LOCK_IMGAGE_URL	= "http://image.guayou520.com/cmn/clz.gif";
function lock(){
	var	oImg	= document.createElement("img");
	oImg.setAttribute("src", LOCK_IMGAGE_URL);
	oImg.setAttribute("id", "_close");

	var	oInfo	= document.createElement("div");
	oInfo.setAttribute("id", "_lock_info_div");
	oInfo.setAttribute("style", "border: 0px solid #369; width:100px; height:100px; background:transparent; z-index:1000; position:absolute; display:none;");
	oInfo.style.display = "block";
	oInfo.style.position = "absolute";
	oInfo.style.top = "50%";
	oInfo.style.left = "50%";
	oInfo.style.marginTop = "-75px";
	oInfo.style.marginLeft = "-50px";

	var	oWall = document.createElement("div");
	
	oWall.setAttribute("id","_lock_wall_div");
	oWall.style.background = "#000";
	oWall.style.width = "100%";
	oWall.style.height = "100%";
	/* 如果是IE6将采取其他方式处理高度 */
	if(navigator.appVersion.search("MSIE 6.0")>=0){
		oWall.style.height = "1050px";
	}
	oWall.style.position = "absolute";
	oWall.style.top = "0";
	oWall.style.left = "0";
	oWall.style.zIndex = "999";
	oWall.style.opacity = "0.61";
	oWall.style.filter = "Alpha(opacity=61)";

	oInfo.appendChild(oImg);

	document.body.appendChild(oInfo);

	document.body.appendChild(oWall);
	
	document.body.style.overflow = "hidden";
}

function unlock(){
	document.body.removeChild(document.getElementById('_lock_info_div'));
	document.body.removeChild(document.getElementById('_lock_wall_div'));
	document.body.style.overflow = "auto";
}
//-- 锁屏功能结束 ---------------------------------------------------------


/**
 * 将null或undefined转换为空字符串
 * @param val
 * @return
 */
function ne(val){
	return (val==null || val==undefined) ? "" : val;
}

/**
 * 检查对象是否是函数
 * @param obj
 * @return
 */
function isFunction(obj){
	return obj!=null && (typeof obj=='function') && obj.constructor==Function;
} 


/**
 * 一个简单的Map实现
 * 
 * @return
 */
function Map(){
	this.entrys	= new Array();
	
	this.size	= function(){ return this.entrys.length; };
	
	this.keys	= function(){
		var	_keys	= new Array();
		for(var i=0; i<this.entrys.length; i++){
			_keys.push(this.entrys[i].key);
		}
		return _keys;
	};
	
	this.put	= function(key, value){
		for(var i=0; i<this.entrys.length; i++){
			if(key === this.entrys[i].key){
				return;
			}
		}
		this.entrys.push({"key":key, "value":value});
	};
	
	this.get	= function (key){
		for(var i=0; i<this.entrys.length; i++){
			if(key === this.entrys[i].key){
				return this.entrys[i].value;
			}
		}
		return null;
	};
	
	this.remove	= function (key){
		for(var i=0; i<this.entrys.length; i++){
			if(key === this.entrys[i].key){
				for(var j=i; j<this.entrys.length-1; j++){
					this.entrys[j]	= this.entrys[j+1];
				}
				this.entrys.length	= this.entrys.length-1;
			}
		}
	};
}

/**
 * 数据处理器配置函数
 * 
 * @return
 */
function DataProcessor(){
	this._field_processor_	= new Map();
	this._value_processor_	= new Map();
	
	/**
	 * 添加字段处理器
	 * 
	 * @param _field_		字段名称
	 * @param _processor_	值处理器对象,值处理器接受一个待处理的值对象作为参数 
	 */
	this.addFieldProcessor	= function(_field_, _processor_){
		if(isFunction(_processor_))
			this._field_processor_.put(_field_, _processor_);
	};
	
	/**
	 * 添加特殊值处理器
	 * 
	 * @param _regexp_		正则表达式字符串,用于匹配要处理的特殊值
	 * @param _processor_	值处理器对象,值处理器接受一个待处理的值对象作为参数
	 */
	this.addValueProcessor	= function(_regexp_, _processor_){
		if(isFunction(_processor_))
			this._value_processor_.put(_regexp_, _processor_);
	};
	
	/**
	 * 调用处理器处理要输入的数据
	 * 
	 * @param _field_	待处理字段
	 * @param _value_	待处理值
	 */
	this.process	= function(_field_, _value_){
		var	tresult	= _value_;
		var	_t_fp_value	= this._field_processor_.get(_field_);
		if(_t_fp_value!=null && isFunction(_t_fp_value)){
			tresult	= _t_fp_value(tresult);
		}
		
		var	_vp_keys	= this._value_processor_.keys();
		for(var i=0; i<_vp_keys.length; i++){
			var	tregexp	= new RegExp(_vp_keys[i]);
			if(tregexp.test(tresult)){
				var	tfunction	= this._value_processor_.get(_vp_keys[i]);
				if(isFunction(tfunction)){
					tresult	= tfunction(tresult);
					continue;
				}
			}
		}
		return tresult;
	};
}

/**
 * 一个简单的对象填充函数,可以按对象中的字段将值填充到模板中
 * 
 * @param obj		要填充的对象
 * @param tmplate	模板字符串
 * @param dp		DataProcessor对象
 * @return	填充数据后的字符串
 */
function rplc(obj, tmplate, dp){
	for(var i in obj){
		if(i!=undefined && i!=""){
			if(dp && dp.process){
				tmplate	= tmplate.replace(new RegExp("\\{\\$"+i+"}", 'g'), dp.process(i, obj[i]));
			}else{
				tmplate	= tmplate.replace(new RegExp("\\{\\$"+i+"}", 'g'), obj[i]);
			}
		}
	}
	return tmplate;
}

/**
 * 使用指定的字符串覆盖原始串中的部分字符
 * 
 * @param tstr	原始串
 * @param tstart	开始字符,从0开始
 * @param tend	结束字符,不能超过tstr.length
 * @param tchar	用于做掩盖用的字符串
 * @return	覆盖后的字符串
 */
function mask(tstr, tstart, tend, tchar){
	var	rs	= '';
	for(var i=0; i<tstr.length; i++){
		rs	+= (tstart<=i && i<tend) ? tchar : tstr.charAt(i);
	}
	return rs;
}

/**
 * 接收两个或三个参数如果,如果是两个参数分别为turl和tfun,如果是三个参数则分别是turl、
 * tdata和tfun
 * 
 * @return
 */
function ajax(){
	if(arguments.length >= 2){
		var	tdata	= arguments[1];
		var	tfun	= arguments[1];
		if(arguments.length = 2){
			tdata	= {r: Math.random()};
		}
		$.ajax({url: arguments[0], type: 'POST', dataType: 'json', data: tdata, success: tfun});
	}
}

/**
 * 同步加在一个文件
 */
function load(turl){
	var	rs	= null;
	$.ajax({async: false,type: 'post',url: turl,success: function(res){return rs=res;}});
	return rs;
}

/**
 * 加载一个类似view_[tname].html的文件
 * 
 * @param tname	加载文件的内容
 * @return
 */
function view(tname){
	var	rs	= null;
	$.ajax({async: false,type: 'post',url: 'view_'+tname+'.html',success: function(res){return rs = res;}});
	return rs;
}
