// 框架用JS

/**
 * 框架基础类
 */
$.fw = {
	getLoginInfo : function(id) {
		var n = 'fw_login';
		n += id ? '_'+id : '';
		var info = $.cookie(n+'_info');
		if (info) {
			return eval('('+info+')');
		}
		return null;
	}
};

/**
 * 框架response方法
 */
$.fw.response = {

	popupFrameNames : new Array(),

	/**
	 * 操作成功
	 *
	 * @params msg 成功信息
	 * @params callback 回调函数 如果传入则点击确定后调用
	 */
	success : function(msg, callback, modal) {
		callback = callback ? callback : '';
		modal = modal ? true : false;
		var html = '<div id="fw_success" style="text-indent:2em; display:none;">'+msg+'</div>';
		$('body').append(html);
		$('#fw_success').dialog({ resizable:false, modal:modal, bgiframe:true, buttons:{ "确定": function() { $(this).dialog("close"); } }, close:function() {$(this).dialog("destroy");$(this).remove();eval(callback);} });
	},

	/**
	 * 失败处理
	 * @param el 出错的元素id
	 * @param msg 出错信息 可以为空
	 * @param pos 出现位置 right:右侧 left:左侧 top:上 bottom:下
	 */
	fail : function(el, msg, pos, offset) {
		pos = pos ? pos : 'right';
		offset = offset ? offset : 2;
		switch (pos) {
			case 'left' :
				offset = [-offset, 0];
				break;
			case 'right' :
				offset = [offset, 0];
				break;
			case 'top' :
				offset = [0, -offset];
				break;
			case 'bottom' :
				offset = [offset, 0];
				break;
		}
		
		$('#'+el).effect("highlight", {color:'#ff9999'}, 400);
		if (msg) {
			$('#'+el).simpletip({content:'<span>'+msg+'</span>', baseClass:'tooltip_'+pos, offset:offset, hidden:false, position:pos, fixed:true });
		}
		$('#'+el).focus();
	},

	/**
	 * 显示提示信息
	 *
	 * @param msg 信息内容
	 * @param w 宽度
	 * @param h 高度
	 * @param type 类型: 0-普通 1-错误(红色) 2-提示(黄色) 3-正确(绿色)
	 * @param title 标题 可以为空
	 */
	message : function(msg, type, title, w, h) {
		type  = type ? type : '0';
		title = title ? title : '';
		pname   = new Date().getTime();
		var html = '<div id="fw_message_'+pname+'" title="'+title+'" style="display:none;">'+msg+'</div>';
		$('body').append(html);
		$('#fw_message_'+pname).dialog({ width:w, height:h, resizable:false, bgiframe:true, buttons:{ "确定": function() { $(this).dialog("close"); } }, close:function() {$(this).dialog("destroy");$(this).remove();} });
	},

	/**
	 * 弹出提示信息
	 *
	 * @param msg 信息内容
	 * @param title 标题 可以为空
	 */
	alert : function(msg, title) {
		title = title ? title : '';
		var html = '<div id="fw_alert" title="'+title+'" style="text-indent:2em; display:none;">'+msg+'</div>';
		$('body').append(html);
		$('#fw_alert').dialog({ resizable:false, modal:true, bgiframe:true, buttons:{ "确定": function() { $(this).dialog("close"); } }, close:function() {$(this).dialog("destroy");$(this).remove();} });
	},

	/**
	 * 确认信息
	 *
	 * @param msg 信息内容
	 * @param func 确定后要执行的操作
	 */
	confirm : function(msg, func) {
		if ($('#fw_confirm').length == 0) {
			var html = '<div id="fw_confirm" style="display:none;">'+msg+'</div>';
			$('body').append(html);
			$('#fw_confirm').dialog({ resizable:false, modal:true, bgiframe:true, buttons:{ "取消": function() {$(this).dialog("close");}, "确定": function() {if(typeof(func)=='function'){func();}else{eval(func)};$(this).dialog("close"); } }, close:function() {$(this).dialog("destroy");$(this).remove(); } });
		}
	},

	/**
	 * 弹出窗口
	 * 用于获取用户输入等
	 *
	 * @param el 元素id
	 * @param title 标题
	 * @param modal 是否modal式弹出
	 * @param w 宽度
	 * @param h 高度
	 */
	popup : function(el, title, modal, w, h) {
		title = title ? title : '';
		modal = modal ? true : false;
		w = w ? w : 320;
		h = h ? h : 280;
		$("#"+el).dialog({ title:title, width:w, height:h, resizable:false, modal:modal, bgiframe:true, close:function() {$(this).dialog("destroy");} });
	},

	/**
	 * 弹出窗口 从其他页面获取内容
	 *
	 * @param uri 页面地址
	 * @param w 宽度
	 * @param h 高度
	 * @param title 标题 可以为空
	 * @param modal 是否modal式弹出
	 */
	popupFrame : function(uri, title, modal, w, h) {
		//parent.$('.ui-dialog-content').dialog('close');
		title = title ? title : '';
		modal = modal ? true : false;
		w = w ? w : 320;
		h = h ? h : 280;
		pname   = new Date().getTime();
		var html = '<div id="fw_popup_'+pname+'" title="'+title+'" style="display:none;"><iframe scrolling="no" frameborder="0" style="background:transparent" width="100%" height="98%" src="'+uri+'"></iframe></div>';
		$('body').append(html);
		$('#fw_popup_'+pname).dialog({ width:w, height:h, resizable:false, modal:modal, bgiframe:true,
			//open:function() { $.fw.response.popupFrameNames.push('fw_popup_'+pname); },
			close:function() { $(this).dialog("destroy"); $(this).remove(); }
		});
		//setTimeout("console.log($('#fw_popup_iframe_'+pname).contents().find('#close'))", 1000);
	},

	closePopup : function(id) {
		if (id) {
			$('#fw_popup_'+id).dialog('close');
		}
		else {
			$('.ui-dialog-content').dialog('close');
		}
	}
};

/**
 * 框架validate方法
 * 统一返回值：是-true 否-false
 * 
 * @param string v 通用参数 要验证的字符
 * @return boolean
 */

$.fw.validate = {
		
	/**
	 * 验证是否为空
	 */
	isEmpty : function(v) {
		if ($.trim(v).length == 0) return true;
		return false;
	},
	
	/**
	 * 验证是否为纯数字(0-9)
	 */
	isNum : function(v) {
		
	},
	
	/**
	 * 验证是否为整数(0-9和负号-) 不包含小数点
	 */
	isInt : function(v) {
		
	},
	
	/**
	 * 验证是否为价格(0-9、负号-、逗号和小数点)
	 */
	isPrice : function(v) {
		
	},
	
	/**
	 * 验证是否为电子邮件
	 */
	isEmail : function(v) {
	　	if (!v.match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/)) {
			return true;
		}
		return false; 
	},
	/**
	 * 验证是否为非法字符
	 */
	f_check_ZhOrNumOrLett: function(v) {
		var pat=new RegExp("[^a-zA-Z0-9\_\u4e00-\u9fa5]","i");
		if(pat.test(v))	return true; 
		else 	 		return false;
   },
  	/**
	 * 验证是否为手机号
	 */
	isMobile : function(v) {
		if (!v.match(/^(13|15|18)[0-9]{9}$/)) return true; 
		    return false;
	},
	/**
	 * 含有非数字字符 返回 true 
	 */ 
     IsNumber : function(s){ //适于校验非负整数  
         var reg = /^[01233456789]{1,}$/;  
          if(reg.test(s))  
              return true;  
          else  
              return false;  
      },
	/**
	 * 验证是否为身份证
	 */
	isNumberId : function(v) { 
       if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(v))) return true;
       	   return false;
	},
	/**
	 * 验证是否为中文
	 */ 
	isCn : function(v) { 
	if (!v.match(/^[\u0391-\uFFE5]+$/))return true;  
		return false;
	},
	/**
	 * 验证是否是中文或数字/[\W]/g
	 */ 
	isCnorNumber : function(v) { 
	if (!v.match(/^[\u0391-\uFFE5\d]+$/))return true;  
		
		return false;
	},
	/**
	 * 验证是否是中文或数字
	 */ 
	isEHorNumber : function(v) { 

	if (!v.match(/^[a-zA-Z0-9]+$/))return true;  
		
		return false;
	 
	}
};

/**
 * jquery.simpletip 1.3.1
 */
(function($){function Simpletip(elem,conf){var self=this;elem=jQuery(elem);elemp=elem.parent();var tooltip=jQuery(document.createElement('div')).addClass(conf.baseClass).addClass((conf.fixed)?conf.fixedClass:'').addClass((conf.persistent)?conf.persistentClass:'').html(conf.content).appendTo(elemp);if(!conf.hidden){tooltip.show()}else tooltip.hide();if(!conf.persistent){elem.hover(function(event){self.show(event)},function(){self.hide()});elem.focus(function(event){self.show(event)},function(){self.hide()});if(!conf.fixed){elem.mousemove(function(event){if(tooltip.css('display')!=='none')self.updatePos(event)})}}else{elem.click(function(event){if(event.target===elem.get(0)){if(tooltip.css('display')!=='none')self.hide();else self.show()}});jQuery(window).mousedown(function(event){if(tooltip.css('display')!=='none'){var check=(conf.focus)?jQuery(event.target).parents('.tooltip').andSelf().filter(function(){return this===tooltip.get(0)}).length:0;if(check===0)self.hide()}})};jQuery.extend(self,{getVersion:function(){return[1,2,0]},getParent:function(){return elem},getTooltip:function(){return tooltip},getPos:function(){return tooltip.offset()},setPos:function(posX,posY){var elemPos=elem.offset();if(typeof posX=='string')posX=parseInt(posX)+elemPos.left;if(typeof posY=='string')posY=parseInt(posY)+elemPos.top;tooltip.css({left:posX,top:posY});return self},show:function(event){conf.onBeforeShow.call(self);self.updatePos((conf.fixed)?null:event);switch(conf.showEffect){case'fade':tooltip.fadeIn(conf.showTime);break;case'slide':tooltip.slideDown(conf.showTime,self.updatePos);break;case'custom':conf.showCustom.call(tooltip,conf.showTime);break;default:case'none':tooltip.show();break};tooltip.addClass(conf.activeClass);conf.onShow.call(self);return self},hide:function(){conf.onBeforeHide.call(self);switch(conf.hideEffect){case'fade':tooltip.fadeOut(conf.hideTime);break;case'slide':tooltip.slideUp(conf.hideTime);break;case'custom':conf.hideCustom.call(tooltip,conf.hideTime);break;default:case'none':tooltip.hide();break};tooltip.removeClass(conf.activeClass);conf.onHide.call(self);return self},update:function(content){tooltip.html(content);conf.content=content;return self},load:function(uri,data){conf.beforeContentLoad.call(self);tooltip.load(uri,data,function(){conf.onContentLoad.call(self)});return self},boundryCheck:function(posX,posY){var newX=posX+tooltip.outerWidth();var newY=posY+tooltip.outerHeight();var windowWidth=jQuery(window).width()+jQuery(window).scrollLeft();var windowHeight=jQuery(window).height()+jQuery(window).scrollTop();return[(newX>=windowWidth),(newY>=windowHeight)]},updatePos:function(event){var tooltipWidth=tooltip.outerWidth();var tooltipHeight=tooltip.outerHeight();if(!event&&conf.fixed){if(conf.position.constructor==Array){posX=parseInt(conf.position[0]);posY=parseInt(conf.position[1])}else if(jQuery(conf.position).attr('nodeType')===1){var offset=jQuery(conf.position).offset();posX=offset.left;posY=offset.top}else{var elemPos=elem.offset();var elemWidth=elem.outerWidth();var elemHeight=elem.outerHeight();switch(conf.position){case'top':var posX=elemPos.left-(tooltipWidth/2)+(elemWidth/2);var posY=elemPos.top-tooltipHeight;break;case'bottom':var posX=elemPos.left-(tooltipWidth/2)+(elemWidth/2);var posY=elemPos.top+elemHeight;break;case'left':var posX=elemPos.left-tooltipWidth;var posY=elemPos.top-(tooltipHeight/2)+(elemHeight/2);break;case'right':var posX=elemPos.left+elemWidth;var posY=elemPos.top-(tooltipHeight/2)+(elemHeight/2);break;default:case'default':var posX=(elemWidth/2)+elemPos.left+20;var posY=elemPos.top;break}}}else{var posX=event.pageX;var posY=event.pageY};if(typeof conf.position!='object'){posX=posX+conf.offset[0];posY=posY+conf.offset[1];if(conf.boundryCheck){var overflow=self.boundryCheck(posX,posY);if(overflow[0])posX=posX-(tooltipWidth/2)-(2*conf.offset[0]);if(overflow[1])posY=posY-(tooltipHeight/2)-(2*conf.offset[1])}}else{if(typeof conf.position[0]=="string")posX=String(posX);if(typeof conf.position[1]=="string")posY=String(posY)};self.setPos(posX,posY);return self}})};jQuery.fn.simpletip=function(conf){jQuery('.tooltip_right').remove();jQuery('.tooltip_left').remove();jQuery('.tooltip_top').remove();jQuery('.tooltip_bottom').remove();var defaultConf={content:'',persistent:false,focus:false,hidden:true,position:'default',offset:[2,0],boundryCheck:true,fixed:true,showEffect:'fade',showTime:150,showCustom:null,hideEffect:'fade',hideTime:150,hideCustom:null,baseClass:'tooltip',activeClass:'active',fixedClass:'fixed',persistentClass:'persistent',focusClass:'focus',onBeforeShow:function(){},onShow:function(){},onBeforeHide:function(){},onHide:function(){},beforeContentLoad:function(){},onContentLoad:function(){}};jQuery.extend(defaultConf,conf);this.each(function(){var el=new Simpletip(jQuery(this),defaultConf);jQuery(this).data("simpletip",el)});return this}})();

/**
 * bgiframe 2.1.1
 */
(function($){$.fn.bgIframe=$.fn.bgiframe=function(s){if($.browser.msie&&/6.0/.test(navigator.userAgent)){s=$.extend({top:'auto',left:'auto',width:'auto',height:'auto',opacity:true,src:'javascript:false;'},s||{});var prop=function(n){return n&&n.constructor==Number?n+'px':n},html='<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+'style="display:block;position:absolute;z-index:-1;'+(s.opacity!==false?'filter:Alpha(Opacity=\'0\');':'')+'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+'"/>';return this.each(function(){if($('> iframe.bgiframe',this).length==0)this.insertBefore(document.createElement(html),this.firstChild)})}return this}})(jQuery);
