﻿function apply_ss(o,o1){
	for(var i in o1){
		o[i]=o1[i];
	}
}

apply_ss(Array.prototype,{
		 
	getRange:function(start,end){
        var result = [];
        if(this.length > 0){
			start = start || 0;
			var i,
				len = this.length - 1
				r = [];
				
			end = end == null?len:Math.min(end,len);
			if(start <= end){
				for(i = start; i <= end; i++) {
					result.push(this[i]);
				}
			}else{
				for(i = start; i >= end; i--) {
					result.push(this[i]);
				}
			}
        }
		return result;
    },
	
	append:function(value){
		if(this.indexOf(value)==-1){
			this[this.length]=value;
		}
		return this;
	},
	
	insert:function(value,i){
		if(this.indexOf(value) == -1){
			this.splice(i,0,value);
		}
		return this;
	},
	
	indexOf:function(value){
		for(var i=0,len=this.length;i<len;i++){
			if(this[i]==value){
				return i;
			}
		}
		return -1;
	},
	
	lastIndexOf:function(value){
		for(var i=this.length;i--;){
			if(this[i]==value){
				return i;
			}	
		}
		return -1;
	},
	
	remove:function(value){
		var index=this.indexOf(value);
		if(index!=-1){
			this.splice(index,1);
		}
		return this;
	},
	
	get:function(value){
		return this[this.indexOf(value)] || null;
	},
	
	removeLast:function(value){
		var index=this.lastIndexOf(value);
		if(index!=-1){
			this.splice(index,1);
		}
		return this;
	},
	
	clear:function(){
		this.length=0;
		return this;
	},
	
	getLast:function(){
		return this[this.length-1];
	},
	
	getFirst:function(){
		return this[0];	
	},
	
	indexOfCall:function(fn){
		for(var i=0,len=this.length;i<len;i++){
			if(fn(this[i])){
				return i;
			}
		}
		return -1;
	},
	
	getCall:function(fn){
		return this[this.indexOfCall(fn)] || null;
	}
});






Validate = {
	ErrorItem : [document.forms[0]],
	existentMessage:[],
	ErrorMessage : ["以下原因导致提交失败："],
	appendMessage : [],
	classList:new Array(),
	validate : function(theForm){
		var obj = theForm || event.srcElement;
		if(typeof obj =='string'){
			obj=document.getElementById(obj);
		}
		var count = obj.elements.length;
		this.ErrorMessage.length = 1;
		this.existentMessage.length=0;
		this.ErrorItem.length = 1;
		this.ErrorItem[0] = obj;
		for(var i=0;i<count;i++){
			with(obj[i]){
				var args = getAttribute("dataType");
				if(!args) continue;
				if(args.indexOf('NotNull') == -1 && value=="") continue;
				args=args.split(',');
				var _dataType=args[0];
				if(!_dataType)  continue;
				switch(_dataType){
					case "MT":
						if(!this.Mobile.test(value) && !this.Phone.test(value)){
							this.AddError(i, getAttribute("msg"));
						}
						break;
					case "Date":
					case "Repeat":
					case "Range":
					case "RangeWithin":
					case "Compare":
					case "Custom":
					case "Group": 
					case "Limit":
					case "LimitB":
					case "SafeString":
						if(!eval(this[_dataType])){
							this.AddError(i, getAttribute("msg"));
						}
						break;
					default :
						if(!this[_dataType].test(value)){
							this.AddError(i, getAttribute("msg"));
						}
						break;
				}
			}
		}
		
		if(this.appendMessage.length > 0){
			for(var i=0;i<this.appendMessage.length;i++){
				this.ErrorMessage.push(this.ErrorMessage.length+":"+this.appendMessage[i]);
			}
			this.appendMessage.length = 0;
		}
		
		if(this.ErrorMessage.length > 1){
			var errCount = this.ErrorItem.length;
			alert(this.ErrorMessage.join("\n"));
			try{
				this.ErrorItem[1].focus();
			}catch(e){}
			return false;
		}
		return true;
	},
	limit:function(len,min, max){
		min = min || 0;
		max = max || Number.MAX_VALUE;
		return min <= len && len <= max;
	},
	LenB : function(str){
		return str.replace(/[^\x00-\xff]/g,"**").length;
	},
	addError:function(message){
		this.appendMessage.push(message);
	},
	AddError : function(index, str){
	    if(this.existentMessage.indexOf(str)==-1){
		    this.ErrorItem[this.ErrorItem.length] = this.ErrorItem[0].elements[index];
		    this.ErrorMessage[this.ErrorMessage.length] = this.ErrorMessage.length + ":" + str;
		    this.existentMessage.push(str);
		}
	},
	Exec : function(op, reg){
		return new RegExp(reg,"g").test(op);
	},
	compare : function(op1,operator,op2){
		switch (operator) {
			case "NotEqual":
				return (op1 != op2);
			case "GreaterThan":
				return (op1 > op2);
			case "GreaterThanEqual":
				return (op1 >= op2);
			case "LessThan":
				return (op1 < op2);
			case "LessThanEqual":
				return (op1 <= op2);
			default:
				return (op1 == op2);            
		}
	},
	MustChecked : function(name, min, max){
		var groups = document.getElementsByName(name);
		var hasChecked = 0;
		min = min || 1;
		max = max || groups.length;
		for(var i=groups.length-1;i>=0;i--)
			if(groups[i].checked) hasChecked++;
		return min <= hasChecked && hasChecked <= max;
	},
	IsDate : function(op, formatString){
		formatString = formatString || "ymd";
		var m, year, month, day;
		switch(formatString){
			case "ymd" :
				m = op.match(new RegExp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$"));
				if(m == null ) return false;
				day = m[6];
				month = --m[5];
				year =  (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));
				break;
			case "dmy" :
				m = op.match(new RegExp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$"));
				if(m == null ) return false;
				day = m[1];
				month = --m[3];
				year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));
				break;
			default :
				break;
		}
		month = parseInt(month);
		if(month != 0 && !month){
			return false;
		}
		month = month==12 ?0:month;
		var date = new Date(year, month, day);
        return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate());
		function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;}
	},
	
	//Require : /.+/,
	'NotNull':	/.+/,
	Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
	/*Phone : /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?/,*/
	Phone:/^[-0-9]+$/,
	Mobile : /^((\(\d{3}\))|(\d{3}\-))?1\d{10}$/,
	IdCard : /^\d{15}(\d{2}[A-Za-z0-9])?$/,
	Currency : /^\d+(\.\d+)?$/,
	Number : /^\d+$/,
	F2:/^[0-9]+(.{1}[0-9]{1,2})?$/,
	Zip : /^\d{6}$/,
	QQ : /^[1-9]\d{4,8}$/,
	Integer : /^[-\+]?\d+$/,
	Double : /^[-\+]?\d+(\.\d+)?$/,
	EN:/^[A-Za-z0-9]+$/,
	English : /^[A-Za-z]+$/,
	Chinese :  /^[\u0391-\uFFE5]+$/,
	IsSafe : function(str){return !this.UnSafe.test(str);},
	SafeString : "this.IsSafe(value)",
	Limit : "this.limit(value.length,getAttribute('min'),  getAttribute('max'))",
	LimitB : "this.limit(this.LenB(value), getAttribute('min'), getAttribute('max'))",
	Date : "this.IsDate(value, getAttribute('min'), getAttribute('format'))",
	Repeat : "value == document.getElementsByName(getAttribute('to'))[0].value",
	RangeWithin : "(!getAttribute('min') || parseInt(getAttribute('min')) < value) && (!getAttribute('max') || value < parseInt(getAttribute('max')))",
	Range : "(!getAttribute('min') || parseInt(getAttribute('min')) <= value) && (!getAttribute('max') || value <= parseInt(getAttribute('max')))",
	Compare : "this.compare(value,getAttribute('operator'),getAttribute('to'))",
	Custom : "this.Exec(value, getAttribute('regexp'))",
	Group : "this.MustChecked(getAttribute('name'), getAttribute('min'), getAttribute('max'))",
	UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,
	Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/
 };
