/*cleanValidator v1.0 improve by 910edu TeamWork
--2009-09-0-23--*/
var t = s = 0; //the tag for clearTimeout();
var cleanValidator = {
	init: function (settings) {
	    //settings is also an array.
		this.settings = settings;
		this.trigger = $("#" + this.settings["trigger"]);
		this.trigger.click(function(){
		    var error = cleanValidator.validate();
		    if(error[0].length < 1) {
				return true;
			} else {
				cleanValidator.fadeError(error[0],error[1]);
				return false;
			}
		});
	},
	validate: function () {
	    //declare an array which contains an errorMessage'id an element that unvalidate.
	    var errorMsg ="";
		var errorArr = new Array();
		errorArr[0] = errorMsg;
		validationTypes = new Array("isRequired", "isEmail", "isNumeric");
		for(n=0; n < validationTypes.length; n++) {
		    //anyway,x is an array which contains some html element such as input\select and so on
			var x = this.settings[validationTypes[n]];
			if(x != null) {
			    //traverse the element in x
				for(i=0; i<x.length; i++) 
				{
					var inputField = $("#" + x[i]);
					switch (validationTypes[n]) {
						case "isRequired" :
						valid = !isRequired(inputField.val());
						errorMsg = "此项必须填写.";
						break;
						case "isEmail" :
						valid = isEmail(inputField.val());
						errorMsg = "邮件地址无效.";
						break;
						case "isNumeric" :
						valid = isNumeric(inputField.val());
						errorMsg = "只能是数字.";
						break;
					}
					if(!valid){
					    errorArr[0] = errorMsg;
					    errorArr[1] = x[i];
					    return errorArr;
					}
					else
					    continue;
				}    
			}
		}
	    return errorArr;
	},
	fadeError: function (errorMsg,eleid) {
	window.clearTimeout(t);
	if($("div.tip-wrapper").size() > 0){
	    $("div.tip-wrapper").remove();
	}

	    $("<div></div>").prependTo("body")
	                    .addClass("tip-wrapper")
	                    .css("opacity","0");
	    //the div contains some tips.
		$("<div></div>").appendTo("div.tip-wrapper")
		                .append(errorMsg)
		                .append($("<img src='/include/jquery/cleanValidator/img/close.png' alt='关闭' style='margin-left:8px;cursor:pointer;'/>"))
		                .addClass("tip-info");
		$("<div></div>").css({"text-align":"left","background-color":"transparent","padding-left":"20px"})
		                .append($("<img src='/include/jquery/cleanValidator/img/arrow.png'alt=''/>"))
		                .appendTo("div.tip-wrapper");
		$("div.tip-wrapper img").click(function(){dispearOut();});
	    $(".tip-info").corner("5px");
	    var height = $("div.tip-wrapper").height() + parseInt($("div.tip-wrapper").css("padding-bottom"),10)
	    var width =  $("div.tip-wrapper").width();
	    var element = $("#" + eleid);
	    //the tooptip position,setted by the program not by the user.center is default.
	    var top  = element.offset().top - height;
		var left = element.offset().left + element.width()/2;
		$("div.tip-wrapper").animate({"opacity":"1"}).css("left", left).css("top", top);
		t = window.setTimeout("dispearOut()",2000);
	}
};

// returns true if the string is not empty
function isRequired(str){
    str = $.trim(str);
	return (str== null) || (str.length == 0);
}
// returns true if the string is a valid email
function isEmail(str){
	if(isRequired(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}
// returns true if the string only contains characters 0-9 and is not null
function isNumeric(str){
	if(isRequired(str)) return false;
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}
function clearTip(){
   $("div.tip-wrapper").remove();
}
function dispearOut(){
    $("div.tip-wrapper").fadeOut(800);
    window.clearTimeout(s);
    s = window.setTimeout("clearTip()",801);
}