﻿/*
	jQuery Extention Functions
	jquery-kk-lib.js
	Copyright (c) 2009 Kang Hanqing
	Date: 2009-04-18 
*/
//////////////////////////////////////////////////////////////

//获取页面宽高尺寸
function _DocSize(){
	return {
		w:document.documentElement.clientWidth||document.body.clientWidth, //$().width()
		h:document.documentElement.clientHeight||document.body.clientHeight  //$().height()
	};
}

//同时兼容ie和ff的 event对象写法 
function _getEvent(){
	if(document.all)return window.event;
	var func = _getEvent.caller;
	while(func!=null){
		var arg0=func.arguments[0];
		if(arg0){ 
			if((arg0.constructor==Event)||(typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){
				return arg0;
			} 
		}
		func=func.caller; 
	}
	return null; 
}


function _kEvent(){
	var e=null;
	if(typeof(event)=="object"){
		e=event;
	}else{
		var func = _kEvent.caller;
		while(func!=null){
			var arg0 = func.arguments[0];
			if(arg0){
				if(typeof(arg0)=="object"){
					e = arg0;
					break;
				}
			}
			func=func.caller;
		}
	}
	return e==null?null:{oE:e.target||e.srcElement,x:e.pageX||e.x,y:e.pageY||e.y};
}

function kFmtIs(t,x){
	if (typeof x!='string')
	{
		alert('kFmtIs函数出错！不能对非字符串进行判断。'); return false;
	}
	if(x==''){alert('kFmtIs函数出错！字符串为空。');return false;}
	if(typeof t!='string'){alert('kFmtIs函数出错！格式名不在允许的列表中。');return false;}
	if(t==''){alert('kFmtIs函数出错！格式名未知。');return false;}
	if(t=='number'){
		return !(x.search(new RegExp("^[0-9]+$"))==-1);
	}
	if(t=='email'){
		return !(x.search(new RegExp("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"))==-1);
	}
	if(t=='postcode'){
		return !(x.search(new RegExp("^[0-9]{6}$"))==-1);
	}
	if(t=='mobile'){
		return !(x.search(new RegExp("^1[0-9]{10}$"))==-1);
	}
	if(t=='phone'){
		return !(x.search(new RegExp("^[0-9]+-?[0-9]+$"))==-1);
	}
	if(t=='phonearea'){
		return !(x.search(new RegExp("^0[0-9]{2,3}$"))==-1);
	}
	if(t=='date'){
		return !(x.search(new RegExp("^(19[0-9][0-9]|20[0-9][0-9])-([0-9]|0[0-9]|1[0-2])-([0-9]|[0-2][0-9]|3[0-1]) *( ([0-9]|[01][0-9]|2[0-4]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9]))$"))==-1);
	}
	if(t=='idcard'){
		return !(x.search(new RegExp("^[0-9]{18}$"))==-1);
	}
	return !(x.search(new RegExp(t))==-1);
}

function kToArray(str,sp){
    var r=[];
    if(str!=undefined&&str!=''){
        r=str.toString().split(',');
    }
    return r;
}

function kChecked(obj){
    var r = false;
    if(obj.type=='checkbox'||obj.type=='radio')
    {
		for(var i=0;i<obj.length;i++)
		{
			if(obj[i].checked)
			{
			    r = true;
				break;
			}
		}
    }
    return r;
}

function kFormReset(obj,val){
	if (obj.length>0)
	{
		if(obj.type=='select-one')
		{
			var c=obj.options.length;
			for(var i=0;i<c;i++)
			{
				if(obj.options[i].value==val) break;
			}
			obj.selectedIndex=i;
		}
		else if(obj[0].type=='radio')
		{
			for(var i=0;i<obj.length;i++)
			{
				if (obj[i].value==val)
				{
					obj[i].checked=true;
					break;
				}
				
			}
		}
		else if(obj[0].type=='checkbox')
		{
		    if(val!=undefined){
			    for(var i=0;i<obj.length;i++)
			    {
				    obj[i].checked=false;
			        if(typeof(val)=='string'){
			            if(val!=undefined&&val!=''){
			                if(val.toString().indexOf(obj[i].value)>=0){
			                    obj[i].checked=true;
			                }
			            }
			        }else{
				        for (var j=0; j<val.length; j++)
				        {
					        if (val[j]==obj[i].value) 
					        {
						        obj[i].checked=true;
						        break;
					        }
				        }
			        }
			    }
			}
		}
	}
	else
	{
		if(obj.type=='text'||obj.type=='textarea'||obj.type=='hidden')
		{
			obj.value=(val==undefined?"":val);
		}
		else if(obj.type=='checkbox')
		{
			if (typeof val=='boolean')
			{
				obj.checked=val;
			}
			if (typeof val=='string'||typeof val=='number')
			{
				obj.checked=(obj.value==val);
			}
		}
	}
}

// 时间字符串
function kDateStr(){
    var p0 = arguments.length>0?arguments[0]:0;
    var p1 = arguments.length>1?arguments[1]:(new Date());
    var r='',d=p1;
    if(p0==1){
        r=''+d.getYear()+'-'+d.getMonth()+'-'+d.getDate()+' '+d.getHours()+':'+d.getMinutes();
    }else{
        r=''+d.getYear()+'-'+d.getMonth()+'-'+d.getDate();
    }
    return r;
}

// jQuery 扩展功能
$.fn.extend({

	//对话框拖动
	kEnmove:function(t){
		$(this).bind("mousedown",t==undefined?$(this):(typeof t=="string"?$(t):(typeof t=="object"?$(t):$(this))),function(e){
			var ob=e.data;
			var evt=_getEvent();
			var evp={x:evt.pageX||evt.x,y:evt.pageY||evt.y}
			var originData={
				obj:ob,objsize:{w:ob.width(),h:ob.height()},
				boxpos:{x:ob.offset().left,y:ob.offset().top},
				moupos:{x:evp.x,y:evp.y}
			};
			ob.css({position:"absolute"/*,opacity:0.7*/}).find("iframe").css({visibility:"hidden"});
			ob.find("select").css({visibility:"hidden"});
			$().bind("mouseup",ob,function(xb){
				xb.data.find("iframe").css({visibility:"visible"});
				xb.data.find("select").css({visibility:"visible"});
				$().unbind("mousemove").unbind("mouseup");
			});
			$().bind("mousemove",originData,function(ori){
				var evt=_getEvent();
				var evp={x:evt.pageX||evt.x,y:evt.pageY||evt.y}
				var _dx,_dy,_maxLeft,_maxTop,_ds={w:$(window).width(),h:$(window).height()};
				_maxLeft=_ds.w-ori.data.objsize.w-10;
				_maxTop=_ds.h-ori.data.objsize.h-10;
				_dx=ori.data.boxpos.x+(evp.x-ori.data.moupos.x);
				_dy=ori.data.boxpos.y+(evp.y-ori.data.moupos.y);
				_dx=_dx<10?10:_dx;_dy=_dy<10?10:_dy;
				_dx=_dx>_maxLeft?_maxLeft:_dx;_dy=_dy>_maxTop?_maxTop:_dy;
				ori.data.obj.css({left:_dx+"px",top:(_dy)+"px"});
			});
		});
		return this;
	},
	kMoveby:function(h){
		var $o=(typeof h=="object")?h:((h==undefined||typeof h!="string")?$(this):$(h))
		$o.kEnmove(this);
		return this;
	},

	//将表单值进行escape编码
	kEscSeria:function(){
		var a=this.serializeArray();
		var s = [ ];
		function add( key, value ){
			s[ s.length ] = encodeURIComponent(key) + '=' + escape(value);
		};
		if ( jQuery.isArray(a) || a.jquery )
			jQuery.each( a, function(){
				add( this.name, this.value );
			});
		else
			for ( var j in a )
				if ( jQuery.isArray(a[j]) )
					jQuery.each( a[j], function(){
						add( j, this );
					});
				else
					add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );
		return s.join("&").replace(/\+/g, "%2B");
	},

	// 打开对话框
	kDiaOpen:function(){
		odia=this[0];
		var $dialog	= odia.$0; //对话框
		var $shadow	= odia.$1; //对话框阴影
		var $box	= odia.$2; //对话框框体
		var $handle	= odia.$3; //对话框拖动手柄
		var $bg	= odia.$4; //背景
		if($dialog!=undefined&&(typeof $dialog=="object")){
			$dialog.css({width:"100%"}).fadeIn("fast");
			if($bg!=undefined&&(typeof $bg=="object")){
				$bg.fadeIn("fast");
			}
			if($box!=undefined&&(typeof $box=="object")){
				$box.fadeIn("fast");
				if($shadow!=undefined&&(typeof $shadow=="object")){
					$shadow.width($box.width()).height($box.height()).fadeIn("fast");
				}
				$dialog.width($box.width()).height($box.height());
			}
			if($handle!=undefined&&(typeof $handle=="object")){
				$dialog.kMoveby($handle);
			}
		}
		return $dialog;
	},
	// 关闭对话框
	kDiaClose:function(){
		odia=this[0];
		var $dialog	= odia.$0;
		var $shadow	= odia.$1;
		var $box	= odia.$2;
		var $handle	= odia.$3;
		var $bg	= odia.$4;
		if($dialog!=undefined&&(typeof $dialog=="object")){
			if($bg!=undefined&&(typeof $bg=="object")){
				$bg.fadeOut("fast");
			}
			if($box!=undefined&&(typeof $box=="object")){
				if($shadow!=undefined&&(typeof $shadow=="object")){
					$shadow.fadeOut("fast");
				}
				$box.fadeOut("fast");
			}
			if($handle!=undefined&&(typeof $handle=="object")){
				$handle.unbind();
			}
			$dialog.fadeOut("fast");
		}
	},

	// 获取 checkbox 值
	kChkValue:function(){
		var s=arguments.length>0?arguments[0]:"";
		var r="";
		jQuery(this).each(function(){r += (this.checked ? (r!="" ? s : "")+this.value : "");});
		return r;
	}
});

