String.prototype.trim=function(){return this.replace(/^\s+|\s+$/ig, "");};
String.prototype.realLength=function(){return this.replace(/[^\x00-\xff]/ig,"aa").trim().length;};
String.prototype.isEmail = function(){ return /^([-\w\.]+)@([-\w]+\.){1,3}[a-z]{2,4}$/i.test(this); };	 // 是否为正确的Email
String.prototype.format = function(){
	var tmpStr = this;
	for(var i=0;i<arguments.length;i++){tmpStr = tmpStr.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);}
	return tmpStr;
}
Number.prototype.prependZero=function(len){var tmp=this.toString(); for(var i=tmp.length; i<len; i++){tmp="0"+tmp;} return tmp;};
Date.prototype.toString=function(f){if(f){return f.replace("yyyy", this.getYear()).replace("MM", this.getMonth().prependZero(2)).replace("dd", this.getDate().prependZero(2)).replace("HH", this.getHours().prependZero(2)).replace("mm", this.getMinutes().prependZero(2)).replace("ss", this.getSeconds().prependZero(2));}else{return this.toString("yyyy-MM-dd HH:mm:ss");}};

$.fn.offset=function(){
	var x=0, y=0;
	for(var obj = this[0]; obj; obj = obj.offsetParent)
	{
		x += parseInt(obj.offsetLeft);
		y += parseInt(obj.offsetTop);
	}
	return {top:y, left:x};
};

$.fn.center=function(){
	return this.each(function(){
		if($.browser.msie)
			$(this).css({left:($("html")[0].offsetWidth-this.offsetWidth)/2, top:($("html")[0].offsetHeight-this.offsetHeight)/2+document.documentElement.scrollTop});
		else
			$(this).css({left:($("html")[0].clientWidth-this.offsetWidth)/2, top:($("html")[0].clientHeight-this.offsetHeight)/2+document.documentElement.scrollTop});
	});
};

$.scroll=function(){
	return {left:document.documentElement.scrollLeft, top:document.documentElement.scrollTop};
};

// eg: $.dialog.pop(.....);
$.extend({
	dialog:{
		_getDialog:function(){
			var _dialog={};
			_dialog.init=function(opt){
				if($.browser.version==6.0) $("select").css("visibility", "hidden");
				if($("#dialogCSS").length==0){
					var style="<style id='dialogCss'>";
					style+="#dlgMask{position:absolute; background:#000; z-index:100; left:0px; top:0px; filter:alpha(opacity=50); opacity:0.5;}";
					style+="#dialog{position:"+($.browser.version==6.0?"absolute":"fixed")+"; z-index:101; border:solid 10px #fff; background:#F5F5F5; visibility:hidden;}";
					style+="	#dialog_header{width:100%; position:absolute; top:-28px; color:#fff;}";
					style+="	#dialog_header i{float:left; font-style:normal; font-size:13px; font-family:verdana;}";
					style+="	#dialog_header i.close{float:right; cursor:pointer} #dialog_btns input{margin:auto 10px; cursor:pointer; background:#000000; color:#fff; border:solid 2px #6A6A6A; border-width:2px 1px 1px 2px; height:20px; line-height:18px; font-size:12px;}";
					style+="</style>";
					$("head").append(style);
				}
				if(opt.block||opt.block==undefined){
					$("<div id='dlgMask'></div>").appendTo($("body")).css("width", document.documentElement.scrollWidth).css("height", Math.max($("html")[0].offsetHeight, document.documentElement.scrollHeight));
					this.mask=$("#dlgMask").mousedown(function(){
						var flashCount=0, oldBgcolor=$("#dialog_header").css("background-color");
						var flashBg=function(){
							$("#dialog_header").css("background-color", [oldBgcolor, "red"][flashCount%2]);
							if(++flashCount<7) setTimeout(flashBg, 50);
						};
						flashBg();
					}).bind("contextmenu", function(e){window.event?window.event.returnValue=false:e.preventDefault();});
				}
				if($("#dialog").length>0) this.close();
				var winStr="<div id='dialog'><div id='dialog_header'><i>弹窗标题</i><i class='close'> 关闭 </i></div></div>";
				($("form").length!=0&&$("@name=__VIEWSTATE").length==0) ? $("form:eq(0)").append(winStr) : $("body").append(winStr);
				this.win=$("#dialog");
				if($.drag) $("#dialog_header").drag(true, this.win[0]);
				this.applyCallbackOnClose=true;  //默认执行回调
				this.restoredElement=null;
				this.closeEl=$("#dialog_header i:eq(1)");
			};
			_dialog.setTitle=function(title){$("#dialog_header i:eq(0)").html(title);};
			_dialog.setClose=function(opt){
				this.closeEl.click(function(){
					if($.browser.version==6.0) $("select").css("visibility", "visible");
					if(_dialog.restoredElement) $("body").append($(_dialog.restoredElement).hide());
					$("#dialog, #dlgMask").remove(); 
					if(opt.applyCallbackOnClose!=null) _dialog.applyCallbackOnClose=opt.applyCallbackOnClose;
					if(_dialog.applyCallbackOnClose && opt.callback!=null) opt.callback.apply(null, [false]);
					_dialog=null;//CollectGarbage();
				});
			};
			_dialog.setContent=function(opt){
				if(typeof(opt.content)=="object") this.restoredElement=opt.content;
				if(opt.url) opt.content="<iframe id='dialog_frame' width="+opt.width+" height="+opt.height+" frameborder=0 scrolling=auto></iframe>";
				if(typeof(opt.content)=="string")
					opt.content=$("body").prepend("<div style='display:none' id='dialog_body'>"+opt.content+"</div>").find("#dialog_body");
				if(opt.url) $("#dialog_frame").attr("src", opt.url);
				$(opt.content).show().css("visibility", "hidden");
				this.win.prepend(opt.content);
				var size={width:this.win[0].offsetWidth, height:this.win[0].offsetHeight};
				this.win.css({visibility:"visible", width:size.width*0.2+"px", height:size.height*0.2+"px"});
				if(opt.speed!=0) 
					this.win.prepend("<div id='ajax-loading' style='font-size:12px; padding:5px;'><img src='/images/ajax-loading.gif' height=22 align=absmiddle />&nbsp;Loading...</div>");
				return size;
			};	
			_dialog.show=function(opt){
				this.init(opt);
				this.setTitle(opt.title || "");
				var winSize=this.setContent(opt);
				this.setClose(opt);
				var width=winSize.width, height=winSize.height, htmlEl=$("html")[0],
				lrBorderWidth=parseInt($(this.win).css("border-left-width"))+parseInt($(this.win).css("border-right-width")),
				tbBorderWidth=parseInt($(this.win).css("border-top-width"))+parseInt($(this.win).css("border-bottom-width")),
				htmlWidth=htmlEl.offsetWidth, htmlHeight=htmlEl.offsetHeight;
				if(!$.browser.msie){ htmlWidth=htmlEl.clientWidth; htmlHeight=htmlEl.clientHeight; }
				if(opt.speed==0){
					this.win.css({width:width-lrBorderWidth, left:(htmlWidth-width)/2, height:height-tbBorderWidth, top:(htmlHeight-height)/2+document.documentElement.scrollTop}).find('> * ').css("visibility","visible").end().css("height", "auto").find("select").css("visibility", "visible");
					if(opt.onload) opt.onload();
				}else
					this.win.animate({width:width-lrBorderWidth, left:(htmlWidth-width)/2}, opt.speed||300).animate({height:height-tbBorderWidth, top:(htmlHeight-height)/2+document.documentElement.scrollTop}, opt.speed||300, function(){$("#ajax-loading").remove(); $(this).find('> * ').hide().css("visibility", "visible").fadeIn().end().css("height", "auto").find("select").css("visibility", "visible"); if(opt.onload) opt.onload();});			
			};
			_dialog.close=function(b){if(b!=null) this.applyCallbackOnClose=b; this.closeEl.click();};
			return _dialog;
		},
		show : function(opt){
			this.dlg=this._getDialog();
			this.dlg.show(opt);
		},
		close : function(b){this.dlg.close(b);},
		alert : function(title, msg, callback){
			var o;
			if(arguments.length==3){o={title:title,content:msg,callback:callback};}
			else if(arguments.length==2){if(typeof(arguments[1])=="function"){o={content:title,callback:msg};}else{o={title:title,content:msg};}}
			else if(arguments.length==1){o={content:title};}
			o.speed=0;
			this.show(o);
		},
		confirm : function(title, msg, callback){
			var o;
			if(arguments.length==3){o={title:title,content:msg,callback:callback};}
			else if(arguments.length==2){if(typeof(arguments[1])=="function"){o={content:title,callback:msg};}else{o={title:title,content:msg};}}
			else if(arguments.length==1){o={content:title};}
			o.speed=0;
			this.show(o);
		},
		prompt : function(title, inputType, callback){//inputType:text, textarea, password
			var o={speed:0, applyCallbackOnClose:false}, arg=arguments;
			if(arg.length==1){o.title=arg[0];}
			else if(arg.length==2){o.title=arg[0]; if(typeof(arg[1])=="boolean"){o.multiline=arg[1];}else{o.callback=arg[1];}}
			else if(arg.length==3){o.title=title; o.inputType=inputType; o.callback=callback;}
			o.content="<div id='dialog_prompt' style='text-align:center; padding:5px;'>{$textbox}<div id='dialog_btns'><input type='button' value=' 确 定 ' /><input type='button' value=' 取 消 ' /><div></div>";
			if(o.inputType==undefined) o.inputType="text";
			if(o.inputType=="textarea") o.content=o.content.replace("{$textbox}", "<textarea id='dialog_input' style='height:80px; width:200px;'></textarea>");
			else o.content=o.content.replace("{$textbox}", "<input type='"+o.inputType+"' id='dialog_input' style='width:200px; margin:5px auto;' />");
			this.show(o); $("#dialog_input").keydown(function(event){if(event.keyCode==13){$("#dialog_btns input:first").click();}}).focus();
			if(o.callback) $("#dialog_btns input:first").click(function(){if(o.callback($("#dialog_input").val())!=false) $.dialog.close(false);});
			$("#dialog_btns input:last").click(function(){$.dialog.close(false);});
		},
		pop : function(title, content, block, callback){
			var o={}, arg=arguments;
			if(arg.length==1){arg[0].content ? o=arg[0] : o={content:arg[0]};}
			else if(arg.length==2){
				var type=typeof(arg[1]);
				if(type=="string"){o={title:arg[0], content:arg[1]};}
				else if(type=="boolean"){o={content:arg[0], block:arg[1]};}
				else if(type=="object"){o={title:arg[0], content:arg[1]};}
				else if(type=="function"){o={content:arg[0], callback:arg[1]};}
			}else if(arg.length==3){
				var type=typeof(arg[1]);
				if(type=="string" || type=="object"){o={title:arg[0], content:arg[1]}; typeof(arg[2])=="boolean" ? o.block=arg[2] : o.callback=arg[2];}
				else if(type=="boolean"){o={content:arg[0], block:arg[1], callback:arg[2]};}
			}else o={title:title, content:content, block:block, callback:callback};
			if(typeof(o.content)=="string" && o.content.indexOf("url=")==0){o.url=o.content.substr(4); o.width=arg[2]; o.height=arg[3]; o.callback=null;}
			this.show(o);
		}
	}
});

function getLength(str){ return str.replace(/[^\x00-\xff]/ig,"aa").trim().length; }

function validateField(field) {
	field=$(field);
	var validateInfo=field.data("validateInfo"), error = false, msg="";
	// required fields
	if(validateInfo.required){ error=!field.val().length; msg=validateInfo.required; }
	// numeric fields
	if(validateInfo.numeric && !error){ error=!/^[0-9]+$/.test(field.val()); msg=validateInfo.numeric; }
	// characters (letters)
	if(validateInfo.character && !error){ error=!/^[a-zA-Z]+$/.test(field.val()); msg=validateInfo.character; }
	// word
	if(validateInfo.word && !error){ error=!/^\w+$/.test(field.val()); msg=validateInfo.word; }
	// email
	if(validateInfo.email && !error){ error=!/^([-\w\.]+)@([-\w]+\.){1,3}[a-z]{2,4}$/i.test(field.val()); msg=validateInfo.email; }
	// 中文
	if(validateInfo.chinese && !error){ error=!/^[\u4e00-\u9fa5]+$/.test(field.val()); msg=validateInfo.chinese; }
	// 中文和英文字符、数字
	if(validateInfo.safecharacter && !error){ error=!/^[\u4e00-\u9fa5a-zA-z_0-9]+$/.test(field.val()); msg=validateInfo.safecharacter; }
	// 长度
	if(validateInfo.length && !error){ error=getLength(field.val())!=validateInfo.length; msg="长度为"+validateInfo.length; }
	// 最大长度
	if(validateInfo.maxLength && !error){ error=getLength(field.val())>validateInfo.maxLength; msg="最大长度为"+validateInfo.maxLength; }
	// 最小长度
	if(validateInfo.minLength && !error){ error=getLength(field.val())<validateInfo.minLength; msg="最小长度为"+validateInfo.minLength; }
	// 正则判断
	if(validateInfo.regex && !error){ error=!new RegExp(validateInfo.regex.split('，')[0], 'ig').test(field.val()); msg=validateInfo.regex.split('，')[1]; }
	// 函数判断
	if(validateInfo.method && !error){ error=!eval(validateInfo.method.split('，')[0]+"('"+field.val()+"')"); msg=validateInfo.method.split('，')[1]; }
	if (error)
		field.addClass("error").next().text( msg ).removeClass("tipMsg").addClass("errorMsg");
	else
		field.removeClass("error").next().text( validateInfo.tip ).removeClass("errorMsg").html(" ").addClass("okMsg");	

	return !error;
}

function doFormValidate(){
	$("label.tipMsg, label.errorMsg, label..okMsg").remove();
	window.hasDoFormValidate=true;
	
	$("form").each( function(){
		$(this).submit(function (){
			var validationError = false;
			$("input[class*='}'], select[class*='}'], textarea[class*='}']", this).each( function() {
					if(!validateField(this)){if(!validationError){try{this.focus();}catch(err){}} validationError = true;}
			});
			return !validationError;
		}).find("input[class*='}'], select[class*='}'], textarea[class*='}']").each( function() {
			var validateInfo;
			if($(this).data("validateInfo")==null){
				validateInfo=eval("("+unescape($(this).attr("class").substring($(this).attr("class").indexOf("{"), $(this).attr("class").lastIndexOf("}")+1))+")");
				if(!validateInfo.tip) validateInfo.tip="";
				$(this).data("validateInfo", validateInfo);
			}else
				validateInfo=$(this).data("validateInfo");
			$(this).after("<label class='tipMsg'>"+validateInfo.tip+"</label>").blur(function(){
				$(this).removeClass("tip"); 
				validateField(this);
			}).focus( function(){
				if(validateInfo.tip){$(this).addClass("tip").removeClass("error").next().text(validateInfo.tip).removeClass().addClass("tipMsg");}
			});
		});
	});
}

$(function(){
	if(!window.hasDoFormValidate){
		doFormValidate();
	}

	//无缝滚动
	$(".mymarquee").each(function(){
		$(this).wrapInner("<div class='mymarquee_inner'></div>").attr("stop", "0").hover(function(){this.stop="1";}, function(){this.stop="0"});
		var inner_height=$(".mymarquee_inner", this)[0].offsetHeight;
		if(inner_height>this.offsetHeight){
			$(".mymarquee_inner").after("<div>"+$(".mymarquee_inner").html()+"</div>");
		}else
			return;
		var _scrollTop=0;
		var _parent=this;
		var run=function(){
			if(_parent.stop=="0"){
				with(_parent){
					if(scrollTop >= lastChild.offsetTop) scrollTop -= firstChild.offsetHeight;
					else scrollTop += 1;
				}
			}
			setTimeout(run, 80);
		}
		run();
	});
});