// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//
// Orignal Code by Travis Beckham -  http://www.squidfingers.com
// Changed / Updated to live validation by Cody Lindley - http://www.codylindley.com
//
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// --- version date: 2/9/2006 ---------------------------------------------------------

// returns true if the string is empty
function isEmpty(str) {
	return (str == null) || (str.length == 0);
}
// returns true if the select
function isNoSelect(str) {
	return (str == null) || (str =="0") || (str =="");
}
//检测用户
function chkUserName(str){
	var re=/^[^\s\wu4e00-u9fa5]{2,4}$/;
	return re.test(str);
}
//检测企业
function chkEnterName(str){
	var re=/^[\s\w\u4e00-\u9fa5]{5,20}$/;
	return re.test(str);
}
function chkEnterReg(str) {
		var result=false;
		var params = "uName="+ str;
     	new Ajax.Request('/public/get/chkEnter.asp?'+params,
       {
             method:'get',                //提交方式
			 asynchronous:false,
             onSuccess: function(data){  //回调函数
			 	if(data.responseText=="true")
                 result=true;
			 }
		});
		return result;
}


// returns true if the string is a valid email
function isEmail(str) {
	if (isEmpty(str)) {
		return false;
	}
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i;
	return re.test(str);
}
// returns true if the string is a valid email
function chkEmailReg(str) {
		var result=false;
		var params = "uEmail="+ str;
     	new Ajax.Request('/public/get/chkEmail.asp?'+params,
       {
             method:'get',                //提交方式
			 asynchronous:false,
             onSuccess: function(data){  //回调函数
			 	if(data.responseText=="true")
                 result=true;
			 }
		});
		return result;
}

// returns true if the string only contains characters A-Z or a-z
function isAlpha(str) {
	var re = /[^a-zA-Z]/g;
	if (re.test(str)) {
		return false;
	}
	return true;
}
// returns true if the string only contains characters 0-9
function isNumeric(str) {
	var re = /[\D]/g;
	if (re.test(str)) {
		return false;
	}
	return true;
}
// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str) {
	var re = /[^a-zA-Z0-9-]/g;
	if (re.test(str)) {
		return false;
	}
	return true;
}
// returns true if the string's length equals "len"
function isLength(str, len) {
	return str.length == len;
}
// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max) {
	return (str.length >= min) && (str.length <= max);
}
// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str) {
	var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/;
	return re.test(str);
}
function isTelephone(Tel)
{
	var re=/^((\d{3,4})-(\d{8})|(\d{4})-(\d{7}))$/;
	return re.test(Tel);
} 
function isMobile(m){
 var reg0=/^13\d{9}$/;   //130--139。至少7位
 var reg1=/^15\d{9}$/;  //联通150-159。至少7位
 if (reg0.test(m))return true;
 if (reg1.test(m))return true;
 return false;
}
// returns true if the string is a valid date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate(str) {
	//var re = /^(\d{4})[\s\.\/-](\d{1,2})[\s\.\/-](\d{1,2})$/;
	var re=/^(19|20)\d{2}-(0?\d|1[012])-(0?\d|[12]\d|3[01])$/; 

	if (!re.test(str)) {
		return false;
	}
	return true;
	var result = str.match(re);
	var y = parseInt(result[1]);
	var m = parseInt(result[2]);
	var d = parseInt(result[3]);
	if (m < 1 || m > 12 || y < 1900 || y > 2100) {
		return false;
	}
	if (m == 2) {
		var days = ((y % 4) == 0) ? 29 : 28;
	} else {
		if (m == 4 || m == 6 || m == 9 || m == 11) {
			var days = 30;
		} else {
			var days = 31;
		}
	}
	return (d >= 1 && d <= days);
}
// returns true if "str1" is the same as the "str2"
function isMatch(str1, str2) {
	return str1 == str2;
}
// returns true if the string contains only whitespace
// cannot check a password type input for whitespace
function isWhitespace(str) { // NOT USED IN FORM VALIDATION
	var re = /[\S]/g;
	if (re.test(str)) {
		return false;
	}
	return true;
}
// removes any whitespace from the string and returns the result
// the value of "replacement" will be used to replace the whitespace (optional)
function stripWhitespace(str, replacement) {// NOT USED IN FORM VALIDATION
	if (replacement == null) {
		replacement = "";
	}
	var result = str;
	var re = /\s/g;
	if (str.search(re) != -1) {
		result = str.replace(re, replacement);
	}
	return result;
}
//检测密码函数
function chkPassword(str){
	var re=/^.{4,20}$/;
	return re.test(str);
}
// 验证码
function isCode(str) {
	var re = /[\d]{4}/;
	return re.test(str);
}


/**
*验证超类
*/
var Validation = Class.create();
Validation.prototype = {initialize:function () {
	this.id = null;//要验证的元素
	this.message = null;//提示信息
	this.error = null;//未通过验证提示信息
	this.msginit=true;//验证信息提示是否要初始化
	this.msgid=null;//验证信息显示区ID
	this.nexid=null//联动验证对象ID,主要用于下拉列表验证
	this.matchid = null;//匹配验证对旬ID
	this.valresult = null;//验证结果
	this.needed = true;//
	
}, validate:function () {
	return false;
}};


/**
* 执行用户姓名验证
*/
var UsernameValidation = Class.create();
UsernameValidation.prototype = Object.extend(new Validation(), {initialize:function (id, message, error, needed) {
	this.id = id;
	this.message = message;
	this.error = error;
	if (arguments.length > 3) {
		this.needed = needed;
	}
}, validate:function () {
	if (this.needed) {
		return chkUserName($F(this.id));
	}
	return true;
}});

/**
* 执行企业名称验证
*/
var EnterNameValidation = Class.create();
EnterNameValidation.prototype = Object.extend(new Validation(), {initialize:function (id, message, error, needed) {
	this.id = id;
	this.message = message;
	this.temperror = error;
	this.error = error;
	if (arguments.length > 3) {
		this.needed = needed;
	}
}, validate:function () {
	if (this.needed) {
		var t=chkEnterName($F(this.id));
		if(t){
			this.error="您的公司名称已注册过";
			return chkEnterReg($F(this.id));
		}
		else{
			this.error=this.temperror;
			return false;
		}
	}
	return true;
}});

/**
* 执行邮件地址验证
*/
var EmailValidation = Class.create();
EmailValidation.prototype = Object.extend(new Validation(), {initialize:function (id, message, error, needed) {
	this.id = id;
	this.message = message;
	this.temperror=error;
	this.error = error;
	if (arguments.length > 3) {
		this.needed = needed;
	}
}, validate:function () {
	if (this.needed) {
		var t=isEmail($F(this.id));
		if(t){
			this.error="此邮箱已被注册,请重新输入";
			return chkEmailReg($F(this.id));
		}
		else{
			this.error=this.temperror;
			return false;
		}
	}
	return true;
}});
/**
* 执行匹配验证
*/
var MatchedValidation = Class.create();
MatchedValidation.prototype = Object.extend(new Validation(), {initialize:function (id, message, error, matchedId, needed) {
	this.id = id;
	this.message = message;
	this.error = error;
	this.matchid = matchedId;
	if (arguments.length > 4) {
		this.needed = needed;
	}
}, validate:function () {
	if (this.needed) {		
		return isMatch($F(this.id), $F(this.matchid))&&chkPassword($F(this.id));
	}
	return true;
}});
/**
* 执行密码验证
*/
var PasswordValidation= Class.create();
PasswordValidation.prototype = Object.extend(new Validation(), {initialize:function (id, message, error, needed) {
	this.id = id;
	this.message = message;
	this.error = error;
	if (arguments.length > 3) {
		this.needed = needed;
	}
}, validate:function () {
	if (this.needed) {		
		return chkPassword($F(this.id));
	}
	return true;
}});
/**
* 执行日期验证
*/
var DateValidation= Class.create();
DateValidation.prototype = Object.extend(new Validation(), {initialize:function (id, message, error, needed) {
	this.id = id;
	this.message = message;
	this.error = error;
	if (arguments.length > 3) {
		this.needed = needed;
	}
}, validate:function () {
	if (this.needed) {		
		return isDate($F(this.id));
	}
	return true;
}});
/**
* 执行下拉列表验证
*/
var SelectValidation= Class.create();
SelectValidation.prototype = Object.extend(new Validation(), {initialize:function (id, message, error, needed) {
	this.id = id;
	this.message = message;
	this.error = error;
	if (arguments.length > 3) {
		this.needed = needed;
	}
}, validate:function () {
	if (this.needed) {		
		return !isNoSelect($F(this.id));
	}
	return true;
}});

/**
* 连动下拉菜单验证
*/
var TCBValidation= Class.create();
 TCBValidation.prototype = Object.extend(new Validation(), {initialize:function (id, message, error,msginit,msgid,nextid,needed) {
	this.id = id;
	this.message = message;
	this.error = error;
	if (arguments.length > 3) {
		this.msginit = msginit;//是否要初始化提示信息,默认true
	}
	if (arguments.length >4) {
		this.msgid = msgid;//提示信息id
	}
	if (arguments.length > 5) {
		this.nextid= nextid;//联动元素
	}
	if (arguments.length > 6) {
		this.needed = needed;
	}
}, validate:function () {
	if($(this.id).disabled)return true;
	if (this.needed) {		
		return !isNoSelect($F(this.id));
	}
	return true;
}});
 

/**
* 执行联系电话验证
*/
var PhoneValidation= Class.create();
 PhoneValidation.prototype = Object.extend(new Validation(), {initialize:function (id, message, error, needed) {
	this.id = id;
	this.message = message;
	this.error = error;
	if (arguments.length > 3) {
		this.needed = needed;
	}
}, validate:function () {
	if (this.needed) {		
		return isTelephone($F(this.id)) || isMobile($F(this.id));
	}
	return true;
}});
/**
* 执行联系地址验证
*/
var AddressValidation= Class.create();
 AddressValidation.prototype = Object.extend(new Validation(), {initialize:function (id, message, error, needed) {
	this.id = id;
	this.message = message;
	this.error = error;
	if (arguments.length > 3) {
		this.needed = needed;
	}
}, validate:function () {
	if (this.needed) {		
		return !isEmpty($F(this.id));
	}
	return true;
}});
 
/**
* 执行验证码
*/
var ValCodeValidation= Class.create();
 ValCodeValidation.prototype = Object.extend(new Validation(), {initialize:function (id, message, error, needed) {
	this.id = id;
	this.message = message;
	this.error = error;
	if (arguments.length > 3) {
		this.needed = needed;
	}
}, validate:function () {
	if (this.needed) {		
		return isCode($F(this.id));
	}
	return true;
}});


/**
* 验证的中心入口
*/
var EmapValidator = Class.create();
EmapValidator.prototype = {initialize:function () {
	this.validations = new Array();
	this.valResult=false;
}, addValidation:function (validation) {
	if (validation instanceof Validation) {
		this.validations[validation.id] = validation;
	}
}, handleMessage:function (event) {
	var id = Event.element(event).id;
	var validation = this.validations[id];
	if (validation.message != null) {
		var element = $(this.getTipId(id));
		element.innerHTML = this.getAlertBody(this.getTipId(id), validation.message);
	}
}, getTipId:function (id) {
	var validation = this.validations[id];
	if(validation.msgid)
	return validation.msgid + "Tip";
	else
	return id + "Tip";
}, getMessageBody:function (id, message) {
	return "<span class='validation_msg' id='" + id + "'>" + (message == null ? "" : message) + "</span>";
}, getErrorBody:function (id, error) {
	return "<span class='validation_err' id='" + id + "'>" + error + "</span>";
}, getAlertBody:function (id, error) {
	return "<span class='validation_alert' id='" + id + "'>" + error + "</span>";
}, getPassBody:function (id, error) {
	return "<span class='validation_pass' id='" + id + "'>&nbsp;</span>";
}, handleError:function (event) {
	var id = Event.element(event).id;
	var validation = this.validations[id];
	var element = $(this.getTipId(id));
	if (validation.validate()) {
		if(validation.nextid&&!$(validation.nextid).disabled)
		element.innerHTML = this.getErrorBody(validation.nextid, this.validations[validation.nextid].message);
			else
		element.innerHTML = this.getPassBody(id, validation.message);
	} else {
		element.innerHTML = this.getErrorBody(id, validation.error);
	}
}, validate:function () {
	for (id in this.validations) {
		var validation = this.validations[id];
		var element = $(id);
		if (validation instanceof Validation) {
			element.onblur = this.handleError.bindAsEventListener(this);
			element.onfocus = this.handleMessage.bindAsEventListener(this);
			if(validation.msginit!=false)
			new Insertion.Bottom(element.parentNode, this.getMessageBody(this.getTipId(id), validation.message));
		}
	}
},validateAll:function(){
	for (id in this.validations) {
		var validation = this.validations[id];
		var element = $(this.getTipId(id));
		if (validation instanceof Validation) {
			if(!validation.validate()){
				alert(validation.error);
				$(id).focus();
				return false;
			}
		}
	}
	return true;
}};

