﻿var __ServiceUrl = '/services.aspx?method=';
function CallService(serviceid, data, callback) {
	jQuery.post(__ServiceUrl + serviceid, data, function(data) {
		var value = __findException(data);
		var err = false;
		if (value != '' && value != null) {
			alert(value);
			err = true;
		}
		if (callback != null) {
			callback(data,err);
		}
	}, 'xml');

}
function __findException(data) {
	var value = $(data).find('Exception').text();
	return value;


}
function Validator(){}
Validator.prototype.Success = true;
Validator.prototype.Execute = null;
Validator.prototype.Message = null;
//值不为空
Validator.prototype.NotNull=function(value) {
	this.Success= value != null && value != '' && value != undefined;
}
//正则匹配
Validator.prototype.MatchRegex= function(value, parent) {
	var reg =  new RegExp(parent);
	reg.ignoreCase = true;
	if (value.match(reg) == null) {
		this.Success= false;
	}
	else{
		this.Success= true;
	}
}
//执行验证
Validator.prototype.Do=function(call)
{
	this.Success = true;
	if(this.Execute !=null)
	  this.Execute();
}
//字符长度区间
Validator.prototype.LengthRegion= function(value,min,max)
{
	if(!value)
	{
		 this.Success= true;
	}
	else
	{
		 var lvalue = value.toString().length;
		 var min = __ValidatorConvert(min,"number");
		 var max = __ValidatorConvert(max,"number");
		 if(min !=null && max !=null)
			 this.Success= __CompareValue(lvalue,min,"req")&& __CompareValue(lvalue,max,"leq");
		  else
			 this.Success= __CompareValue(lvalue,min,"req")|| __CompareValue(lvalue,max,"leq");
	}
}
//数字大小区间
Validator.prototype.NumberRegion =function(value,min,max) {
	if(!value)
	{
		this.Success= true;
	}
	else
	{
		var lvalue = __ValidatorConvert(value,"number");
		if(!lvalue)
		{
		   this.Success= false;
		}
		else
		{
			
			var min = __ValidatorConvert(min,"number");
			var max = __ValidatorConvert(max,"number");
			
			if(min !=null && max !=null)
				this.Success= __CompareValue(lvalue,min,"req")&& __CompareValue(lvalue,max,"leq");
			else
				this.Success= __CompareValue(lvalue,min,"req")|| __CompareValue(lvalue,max,"leq");
		}
	}
}
//比较数字
Validator.prototype.NumberCompare = function(leftvalue,rightvalue,type){
	var lvalue = __ValidatorConvert(leftvalue,"number");
	var rvalue = __ValidatorConvert(rightvalue,"number");
	this.Success= __CompareValue(lvalue,rvalue,type);
}

//日期大小区间
Validator.prototype.DataRegion = function(value,min,max)
{
	if(!value)
	{
		this.Success= true;
	}
	else
	{
		var lvalue = __ValidatorConvert(value,"date");
		if(!lvalue)
		{
			this.Success = false;
		}
		else
		{
			var min = __ValidatorConvert(min,"date");
			var max = __ValidatorConvert(max,"date");
			if(min !=null && max !=null)
				this.Success= __CompareValue(lvalue,min,"req")&& __CompareValue(lvalue,max,"leq");
			else
				this.Success= __CompareValue(lvalue,min,"req")|| __CompareValue(lvalue,max,"leq");
		}
	}
}
//比较日期
Validator.prototype.DateCompare = function(leftvalue,rightvalue,type){
		var lvalue = __ValidatorConvert(leftvalue,"Date");
		var rvalue = __ValidatorConvert(rightvalue,"Date");
		this.Success= __CompareValue(lvalue,rvalue,type);
}
//匹配邮件
Validator.prototype.EMail = function(value){
	this.MatchRegex(value,'^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$');
}
//身份证
Validator.prototype.IDCard = function(value){
	this.MatchRegex(value,'^\\d{15}$)|(^\\d{17}([0-9]|X|x)$');
}
Validator.prototype.Chinese = function(value){
	this.MatchRegex(value,'^[\\u4e00-\\u9fa5]*$');
}
Validator.prototype.Mobile = function(value)
{
	this.MatchRegex(value,'^[1][0-9]{10}$');
}
//字符比较
Validator.prototype.StringCompare = function(leftvalue,rightvalue,type){
	this.Success= __CompareValue(leftvalue,rightvalue,type);
}

//选择项
Validator.prototype.SelectCheckbox= function(controls,selectcount)
{
	var selects =0;
	for(var i =0;i<controls.length;i++)
	{
	   if($('#'+controls[i]).attr('checked'))
	   {
			selects++;
	   }
	}
	this.Success= selects>= selectcount;
}
//选择项
Validator.prototype.SelectCheckboxFromTo=function(control,from,to,selectcount)
{
	var selects =0;
	for(var i =from;i<to;i++)
	{
	   if($('#'+controls[i]).attr('checked'))
	   {
			selects++;
	   }
	}
	this.Success= selects>= selectcount;
}
Validator.prototype.Ajax = function(url,params)
{
	 var result = $.ajax({
		 url: url,
		 async: false,
		 data:__CreatePostData(params)
	 }).responseText;
	 if($(result).find('Exception').text())
		this.Success = false;
	 else
		this.Success = true;
}
function __CreatePostData(expression)
{

		var param = new Object();
		if(expression != null && expression !='')
		{
				var properties = expression.split('&');
				var namevalue,execute;
				for(i =0;i<properties.length;i++)
				{
					namevalue = properties[i].split('=');
					if(namevalue.length ==2)
					{
						if(namevalue[1].indexOf('#') >=0)
						{
							execute ='param.' + namevalue[0]+'=$(\"'+ namevalue[1]+'\").val()';
						}
						else
						{
							execute ='param.' + namevalue[0]+'=\"'+ namevalue[1]+'\"';
						}
						eval(execute);
					}
				}
		}
		param.PostTime = new Date().toString();
		return param;
}
   

//值是否为null
function __IsNull(value)
{
 
	if(!value)
		return true;
	return false;
}
//比较值函数
function __CompareValue(leftvalue,rightvalue,compareType)
{
	if(__IsNull(leftvalue)  || __IsNull(rightvalue))
		return false;
	if(compareType=="eq")
	{
		return leftvalue == rightvalue;
	}
	else if(compareType =="neq")
	{
		return leftvalue != rightvalue;
	}
	else if(compareType =="le")
	{
		return leftvalue < rightvalue;
	}
	else if(compareType =="leq")
	{
		return leftvalue <= rightvalue;
	}
	else if(compareType =="ri")
	{
		return leftvalue > rightvalue;
	}
	else if(compareType =="req")
	{
		return leftvalue >= rightvalue;
	}
	else
	{
		return false;
	}
	
}
//转换值函数
function __ValidatorConvert(value, dataType) {
	var num,exp,m;
	var year,month,day
	if(value == null || value =="")
		return null;
	if(dataType=="int")
	{
		exp=/^[-\+]?\d+$/;
		if (value.match(exp) == null)
			return null;
		num = parseInt(value, 10);
		return (isNaN(num) ? null : num);
	}
	else if(dataType =="number")
	{
		exp=/^[-\+]?((\d+)|(\d+\.\d+))$/;
		if (value.match(exp) == null)
			return null;
		num = parseFloat(value);
		return (isNaN(num) ? null : num);
	}
	else if(dataType =="date")
	{
		exp=/^(\d{4})([-/]?)(\d{1,2})([-/]?)(\d{1,2})$/
		m = value.match(exp);
		if (m == null)
		{
			exp=/^(\d{1,2})([-/]?)(\d{1,2})([-/]?)(\d{4})$/
			m = value.match(exp);
			if(m== null)
				return null;
			year = m[5];
			month = m[1];
			day =m[3];
			
		}
		else
		{
			year = m[1];
			month =m[3];
			day = m[5];
		}
		try
		{
			num = new Date(year,month,day);
		}
		catch(e)
		{
			return null;
		}
		return num;
	}
	else
	{
		return value.toString();
	}
}
function _StringFormatInline()
{
	var txt = this;
	for(var i=0;i<arguments.length;i++)
	{
		var exp = new RegExp('\\{' + (i) + '\\}','gm');
		txt = txt.replace(exp,arguments[i]);
	}
	return txt;
}

function _StringFormatStatic()
{
	for(var i=1;i<arguments.length;i++)
	{
		var exp = new RegExp('\\{' + (i-1) + '\\}','gm');
		arguments[0] = arguments[0].replace(exp,arguments[i]);
	}
	return arguments[0];
}

if(!String.prototype.format)
{
	String.prototype.format = _StringFormatInline;
}

if(!String.format)
{
	String.format = _StringFormatStatic;
}
