/**
 * Created on 2008-03-20 
 * FC114 常用JS方法
 * @since FC114_V2008
 * @author jctr<jc@cdrj.net.cn>
 * ChengDu CandorSoft Co., Ltd.
 * @version $Id: jquery.utils.js,v 1.7 2008/04/15 09:35:05 jc Exp $
 */


/**
 * 设置页面高度
 * 
 */
function setPageHeight(num){
	if(!num) num = 0;
	var aPageSize = getPageSize();
	//alert(aPageSize);
	var iHeight = aPageSize[3] - 15;
	$("#container").height(iHeight - num);
}


function dumpRegExp(event,tRe){
	var event = jQuery.event.fix( event || window.event); 
	var tVal = event.target.value;	
	tRe = new RegExp(tRe,"ig");
	var tRet = tVal.match(tRe);
	event.target.value = (tRet==null?"":tRet[0]);
	event.returnValue=false;
}

function dumpInt(event,len){
	len=!len?"":(len-=1);
	var tRe="([1-9][0-9]{0,"+len+"})|0";
	return dumpRegExp(event,tRe);
}

function dumpfloat(event,m,n,fs){
	m=!m?"":(m-=1);
	n=!n?"":n;
	var tRe=(!fs?"":"(-)?");
	tRe+="(0|[1-9][0-9]{0,"+m+"})?([\.][0-9]{0,"+n+"})?";
	return dumpRegExp(event,tRe);
}

function dumpChar(event,len){
	/*
	len=!len?"":(len-=1);
	var tRe="\w{0,"+len+"}";
	return dumpRegExp(event,tRe);
	*/
	return true;
}

function dumpSpace(event,len){
	len=!len?"":(len);
	var tRe="([0-9]{0,"+len+"})?(([\,][0-9]{0,"+len+"})*)?";
	return dumpRegExp(event,tRe);
}


function isEmailAddr(obj,eMsg){
	if(!obj.value) return true;
	if(!(/^([\.a-zA-Z0-9_-]){2,}@([a-zA-Z0-9_-]){2,}(\.([a-zA-Z0-9]){2,4}){1,2}$/.test(obj.value))){
		alert(eMsg+"输入不正确!");
		obj.select();
		obj.focus();
		return false;
	}
	return true;
}

function isUrlAddr(obj,eMsg){
	if(!obj.value) return true;
	if(!(/^http:\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?$/.test(obj.value))){
		alert(eMsg+'输入不正确!');
		obj.select();
		obj.focus();
		return false;
	}
	return true;
}

	function dumpkey(e){
		var event = e || window.event;
		if(event.keyCode==13)
			if(event.srcElement.type!="button"&&event.srcElement.type!="textarea")
				event.keyCode = 9;
		return event.returnValue;
	}

// 得到radio 或 checkBox的值, 返回数组
function getRdoOrCkbValue(a){
	if(!a) return; 
	if(typeof a == "string") a = document.getElementsByName(a);
	var ar = new Array();
	jQuery.each(a,function(f,v){
		if(v.checked) ar.push(v.value);
	});
	return ar;
}

// 数据效验
function fnViefData(a){
	if((typeof a != "object") || (a.constructor != Array)) return false;
	var iLen = a.length;
	var sf, sv, v;

	for(var i = 0; i < a.length; i++ ){
		v = a[i];
		sf = v.field;
		if(typeof sf == "string") sf = jQuery("#" + sf);
		if(!sf) return false;
		sv = jQuery.trim(sf.val());
		if(sv == ""){
			alert("『×』["+ v.tips +"]不能为空!");
			sf.focus();
			return false;
		}
	}
	return true;
}


//设置按钮状态
function setButtonState(aBtn, isDis){
	if ((typeof aBtn != "object") || (aBtn.constructor != Array)) return;
	//isDis = (!!isDis) ? isDis : true;
	var iBtnLen = aBtn.length; 
	var ob;
	for(var i=0;i<iBtnLen;i++){
		ob = document.getElementById(aBtn[i]);
		if(ob)  ob.disabled = isDis;
	}
}



//打开一个新的窗口
function openWindow(sUrl,sName,iWidth,iHeight, fea) {
	var aWin = getPageSize();
	var iWidth = iWidth || 650, iHeight = iHeight || 500, iLeft = 0, iTop = 0;
	iLeft = Math.ceil((aWin[2] - iWidth) / 2);
	iTop =  Math.ceil((aWin[3] - iHeight) / 2);
	var sFea = "height="+ iHeight +",width="+ iWidth +",left="+ iLeft +",top="+ iTop +",location=0,menubar=0,status=0,toolbar=0,scrollbars=1";
	if(fea) sFea += "," + fea;
	return window.open(sUrl,sName,sFea);
}



// 附件能接受的参数
function efficacyAccept(obj){
	var sFileType,aAccetpType;
	var sAccetpType = obj.accept.toLowerCase();
	if(sAccetpType.length > 0){
		aAccetpType = sAccetpType.split(",");
	}

	if(obj.value != "" && aAccetpType.length > 0){
		sFileType = obj.value.replace(/.*\./g,"").toLowerCase();
		var isAccept = $.inArray(sFileType,aAccetpType);
		if(isAccept < 0) {

			alert('『×』不支持的文件类型');

			if(obj.outerHTML) obj.outerHTML = obj.outerHTML;
			else obj.value = "";
		}
	}	
}


function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight, scrollTop;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
		scrollTop = self.pageYOffset ;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
		scrollTop = document.documentElement.scrollTop;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
		scrollTop = document.body.scrollTop;
	}	


	pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;

	arrayPageSize = [pageWidth,pageHeight,windowWidth,windowHeight,scrollTop];
	return arrayPageSize;
}


// =============================================================

jQuery.iUtil = {
	getPosition : function(e) {
		var x = 0, y = 0;
		var es = e.style;
		var restoreStyles = false;
		if (jQuery(e).css('display') == 'none') {
			var oldVisibility = es.visibility;
			var oldPosition = es.position;
			restoreStyles = true;
			es.visibility = 'hidden';
			es.display = 'block';
			es.position = 'absolute';
		}
		var el = e;
		while (el){
			x += el.offsetLeft + (el.currentStyle && !jQuery.browser.opera ?parseInt(el.currentStyle.borderLeftWidth)||0:0);
			y += el.offsetTop + (el.currentStyle && !jQuery.browser.opera ?parseInt(el.currentStyle.borderTopWidth)||0:0);
			el = el.offsetParent;
		}
		el = e;
		while (el && el.tagName  && el.tagName.toLowerCase() != 'body')	{
			x -= el.scrollLeft||0;
			y -= el.scrollTop||0;
			el = el.parentNode;
		}
		if (restoreStyles == true) {
			es.display = 'none';
			es.position = oldPosition;
			es.visibility = oldVisibility;
		}
		return {x:x, y:y};
	},

	getPositionLite : function(el) {
		var x = 0, y = 0;
		while(el) {
			x += el.offsetLeft || 0;
			y += el.offsetTop || 0;
			el = el.offsetParent;
		}
		return {x:x, y:y};
	},

	getSize : function(e) {
		var w = jQuery.css(e,'width');
		var h = jQuery.css(e,'height');
		var wb = 0;
		var hb = 0;
		var es = e.style;
		if (jQuery(e).css('display') != 'none') {
			wb = e.offsetWidth;
			hb = e.offsetHeight;
		} else {
			var oldVisibility = es.visibility;
			var oldPosition = es.position;
			es.visibility = 'hidden';
			es.display = 'block';
			es.position = 'absolute';
			wb = e.offsetWidth;
			hb = e.offsetHeight;
			es.display = 'none';
			es.position = oldPosition;
			es.visibility = oldVisibility;
		}
		return {w:w, h:h, wb:wb, hb:hb};
	},

	getSizeLite : function(el)	{
		return {
			wb:el.offsetWidth||0,
			hb:el.offsetHeight||0
		};
	},

	getClient : function(e)	{
		var h, w, de;
		if (e) {
			w = e.clientWidth;
			h = e.clientHeight;
		} else {
			de = document.documentElement;
			w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
			h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		}
		return {w:w,h:h};
	},

	getScroll : function (e) {
		var t=0, l=0, w=0, h=0, iw=0, ih=0;
		if (e && e.nodeName.toLowerCase() != 'body') {
			t = e.scrollTop;
			l = e.scrollLeft;
			w = e.scrollWidth;
			h = e.scrollHeight;
			iw = 0;
			ih = 0;
		} else  {
			if (document.documentElement) {
				t = document.documentElement.scrollTop;
				l = document.documentElement.scrollLeft;
				w = document.documentElement.scrollWidth;
				h = document.documentElement.scrollHeight;
			} else if (document.body) {
				t = document.body.scrollTop;
				l = document.body.scrollLeft;
				w = document.body.scrollWidth;
				h = document.body.scrollHeight;
			}
			iw = self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0;
			ih = self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0;
		}
		return { t: t, l: l, w: w, h: h, iw: iw, ih: ih };
	},

	getMargins : function(e, toInteger)	{
		var el = jQuery(e);
		var t = el.css('marginTop') || '';
		var r = el.css('marginRight') || '';
		var b = el.css('marginBottom') || '';
		var l = el.css('marginLeft') || '';
		if (toInteger)
			return {
				t: parseInt(t)||0,
				r: parseInt(r)||0,
				b: parseInt(b)||0,
				l: parseInt(l)
			};
		else
			return {t: t, r: r,	b: b, l: l};
	},

	getPadding : function(e, toInteger)	{
		var el = jQuery(e);
		var t = el.css('paddingTop') || '';
		var r = el.css('paddingRight') || '';
		var b = el.css('paddingBottom') || '';
		var l = el.css('paddingLeft') || '';
		if (toInteger)
			return {
				t: parseInt(t)||0,
				r: parseInt(r)||0,
				b: parseInt(b)||0,
				l: parseInt(l)
			};
		else
			return {t: t, r: r,	b: b, l: l};
	},

	getBorder : function(e, toInteger)	{
		var el = jQuery(e);
		var t = el.css('borderTopWidth') || '';
		var r = el.css('borderRightWidth') || '';
		var b = el.css('borderBottomWidth') || '';
		var l = el.css('borderLeftWidth') || '';
		if (toInteger)
			return {
				t: parseInt(t)||0,
				r: parseInt(r)||0,
				b: parseInt(b)||0,
				l: parseInt(l)||0
			};
		else
			return {t: t, r: r,	b: b, l: l};
	},

	getPointer : function(event) {
		var x = event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) || 0;
		var y = event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop)) || 0;
		return {x:x, y:y};
	},

	traverseDOM : function(nodeEl, func) {
		func(nodeEl);
		nodeEl = nodeEl.firstChild;
		while(nodeEl){
			jQuery.iUtil.traverseDOM(nodeEl, func);
			nodeEl = nodeEl.nextSibling;
		}
	},

	purgeEvents : function(nodeEl)	{
		jQuery.iUtil.traverseDOM(
			nodeEl,
			function(el) {
				for(var attr in el){
					if(typeof el[attr] === 'function') {
						el[attr] = null;
					}
				}
			}
		);
	},

	centerEl : function(el, axis) {
		var clientScroll = jQuery.iUtil.getScroll();
		var windowSize = jQuery.iUtil.getSize(el);
		if (!axis || axis == 'vertically')
			jQuery(el).css({
				top: clientScroll.t + ((Math.max(clientScroll.h,clientScroll.ih) - clientScroll.t - windowSize.hb)/2) + 'px'
			});

		if (!axis || axis == 'horizontally')
			jQuery(el).css({
				left:	clientScroll.l + ((Math.max(clientScroll.w,clientScroll.iw) - clientScroll.l - windowSize.wb)/2) + 'px'
			});
	},

	fixPNG : function (el, emptyGIF) {
		var images = jQuery('img[@src*="png"]', el||document), png;
		images.each( function() {
			png = this.src;				
			this.src = emptyGIF;
			this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + png + "')";
		});
	}
};


[].indexOf || (Array.prototype.indexOf = function(v, n){
	n = (n == null) ? 0 : n;
	var m = this.length;
	for (var i=n; i<m; i++)
		if (this[i] == v)
			return i;
	return -1;
});



(function($) {

/**
 * 城市联动下拉框
 * @name     regionSelection
 * @param    url xml 的路径
 * @param    city 另一个框的名字
 */
$.fn.regionSelection = function(url, city){
	var obj = jQuery(this), sltVal = obj.val();
	if($.trim(url) == "") return;
	
	// 运行就设置城市
	$(setCityOption(url, sltVal, city));

	obj.change(function(){
		sltVal = this.value;
		if($.trim(sltVal) == "" || $.trim(url) == "") return;
		setCityOption(url, sltVal, city);
	});

	function setCityOption(url, area, city){
		if(typeof city == "string") city = document.getElementById(city);
		if(!city) return;
		city.length = 0; // 清空原来的内容

		var attr, col;

		$.ajax({
			url: url, 
			async:false, 
			dataType: "xml", 
			success:function(xml){
				$("area[id="+ sltVal +"]", xml).children().each(function(i){
					attr = this.attributes;
					city.options[i] = new Option(attr[1].nodeValue, attr[0].nodeValue);
					col = (i % 2 == 0) ? "#FFDFFF" : "white"
					city.options[i].style.backgroundColor = col;
				});
			}
		});
	}
}

/**
 * 为表格鼠标移动着色
 * @name     mouseColor
 * @param    cm 移动上去的样式表
 * @param    fn 回调函数
 */
$.fn.mouseColor = function(cm, fn){
	var css, oj;
	this.each(function(){ //选中了多少个表格
		oj = jQuery("tr",jQuery(this));
		
		if(cm){ 
			oj.hover(function(){
				jQuery(this).addClass(cm);
			},
			function(){
				jQuery(this).removeClass(cm);
			});
		}
		
		if(fn && "function" == typeof fn){ //用来处理点击事件的回调函数
			oj.click(function(event){
				event = jQuery.event.fix( event || window.event || {} ); 
				var cn = event.target;
				if( cn && cn.nodeName.toLowerCase() == "td"){
					cn = $(cn).parent();
					fn.call(this,cn); //返回点击的那一行的HTML
				}
			});
		}
	});
}

})(jQuery);
 




/**
 * 对表格加样式表(使表格各行颜色交替变色)
 *
 * @name     cycle
 * @param    c1 偶数行显示的颜色 0,2,4,6...
 * @param    c2 奇数行显示的颜色 1,3,5,7...
 * @param    cm 鼠标移动上去时的颜色
 * @param    fn 点击某个TD时的回调函数
 * @example  $(function(){$(".tableLine").cycle("csstr1","csstr2","csstr3",function(obj){var ck = jQuery("td",obj).get(5).firstChild; ck.checked = !ck.checked;})});
 */
jQuery.fn.cycle = function(c1, c2, cm, fn){
	if(!(c1 && c2)) return; //判断有没得参数传入
	var css, oj;
	this.each(function(){ //选中了多少个表格
		oj = jQuery("tr",jQuery(this));
		oj.each(function(i){ // 选中表格中的 TR 加 CSS
			css = i % 2 == 0 ? c1 : c2;
			jQuery(this).addClass(css);
		});
		
		if(cm){ //如果有第三个参数
			oj.hover(function(){
				jQuery(this).addClass(cm);
			},
			function(){
				jQuery(this).removeClass(cm);
			});
		}
		
		if(fn && "function" == typeof fn){ //用来处理点击事件的回调函数
			oj.click(function(event){
				event = jQuery.event.fix( event || window.event || {} ); 
				var cn = event.target;
				if( cn && cn.nodeName.toLowerCase() == "td"){
					cn = jQuery(cn).parent();
					fn.call(this,cn); //返回点击的那一行的HTML
				}
			});
		}
	});
} 

// 显示提示信息(进行高亮显示)
function showMsg(o){
	if(typeof o != "object") o = jQuery("#" + o);
	if(o) {
		if(jQuery.trim(o.html()) == "") o.hide();
		else o.highlightFade({
			speed:2000, 
			complete:function() { 
				o.hide();
				o.html("");
			}
		});
	}

}
