﻿
function $(objId)
{
	return document.getElementById(objId);	
}

function P$(objId)
{
	var obj;
	if(parent)
	{
		obj=parent.document.getElementById(objId);
	}
	else
		obj=null;
	return obj;
}

// 注册事件
function AddEventHandler(oTarget,sEventType,fHandler)
{
	if(oTarget.addEventListener)
	{
		//alert('eDo1');
		oTarget.addEventListener(sEventType,fHandler,false);
	}
	else if(oTarget.attachEvent)
	{
		//alert('eDo2');
		oTarget.attachEvent('on'+sEventType,fHandler);
	}
	else
	{
		//alert('eDo3');
		oTarget['on'+sEventType]=fHandler;
	}
}

function RadioSwitchDo(elem,display,container)
{
	var ctn=document.getElementById(container);
	if(!ctn)
	{
		alert('元素'+container+'不存在');
		return;
	}
	//alert(ctn.style.display);
	if(elem.checked)
		ctn.style.display=display?"":"none";
}

function CheckBoxSwitchDo(elem,container)
{
	var ctn=document.getElementById(container);
	if(!ctn)
	{
		alert('元素'+container+'不存在');
		return;
	}
	//alert(ctn.style.display);
	ctn.style.display=elem.checked?"":"none";
}

function ImageResize(img,width,height)
{
	//alert('in');
	var oW,oH;
	oW=img.width;
	oH=img.height;
	if(img.width>width)
	{
		img.width=width;
		//alert('img.width='+img.width+'\nimg.height='+img.height);
		img.height=oH*width/oW;
		//alert('height='+img.height);
		oW=img.width;
		oH=img.height;
	}
	if(img.height>height)
	{
		img.height=height;
		img.width=oW*height/oH;
	}
//	img.width=destW;
//	img.height=destH;
}

function SelectAll(obj,name)
{
	var arrCtl=document.getElementsByName(name);
	for(var i=0;i<arrCtl.length;i++)
		arrCtl[i].checked=obj.checked;
}

function SelectOption(objId,value)
{
	var obj=document.getElementById(objId);
	if(!obj)
		{alert('SelectOption:'+objId+'不是有效的控件ID');return;}
	if(!value) return;
	for(var i=0;i<obj.options.length;i++)
	{
		if(obj.options[i].value==value)
		{
			obj.selectedIndex=i;
			return;
		}
	}
}

function DeleteConfirm(msg)
{
	if(!msg)
		msg='是否确认删除?';
	if(window.confirm(msg))
		return true;
	else 
		return false;
}



//取元素相对于body的坐标
function ZB() 
{
 this.left=0,
 this.top=0,
 this.width=0,
 this.height=0
 };
 
function getCurrentStyle(o,style)
{
   var number=parseInt(o.currentStyle[style]);
   return isNaN(number)?0:number;
}
function getComputedStyle(o,style)
{
   return parseInt(document.defaultView.getComputedStyle(o,null).getPropertyValue(style));
}
 
function GetZB(obj)
{
	var o=obj;
	var oLTWH=new ZB();
	oLTWH.width=o.offsetWidth;
	oLTWH.height=o.offsetHeight;
	oLTWH.left=o.offsetLeft;
	oLTWH.top=o.offsetTop;
	while(true)
    {
       o=o.offsetParent;
       if(o==(document.body&&null))break;
       oLTWH.left+=o.offsetLeft;
       oLTWH.top+=o.offsetTop;
       /*
       if(document.all)
       {
           oLTWH.left+=getCurrentStyle(o,"borderLeftWidth");
           oLTWH.top+=getCurrentStyle(o,"borderTopWidth");
       }
       else
       {
		oLTWH.left+=getComputedStyle(o,"border-left-width");
        oLTWH.top+=getComputedStyle(o,"border-top-width");
       }
      */
    }
    return oLTWH;
}

function LTrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1)
	{
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
		{j++;}
		s = s.substring(j, i);
	}
	return s;
}

function RTrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1)
	{
		var i = s.length - 1;
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
		{i--;}
		s = s.substring(0, i+1);
	}
	return s;
}

function Trim(str)
{
	return RTrim(LTrim(str));
}

function IsSingleByteChar(chr)
{
	var code=chr.charCodeAt(0);
	if(code<256)
		return true;
	return false;
}
function GetStrLen(str)
{
	var len=0;
	for(var i=0;i<str.length;i++)
		if(IsSingleByteChar(str.charAt(i)))
			len++;
		else
			len=len+2;
	return len;
}

//设置Cookie   
function setCookie(name,value,exp)   
{   
	document.cookie = name + "=" + escape (value) + (exp?(";expires=" + exp.toGMTString()):"");   
}   
  
//读取cookies的值   
function getCookie(name)   
{   
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");   
    if(arr=document.cookie.match(reg)) return unescape(arr[2]);   
    else return null;   
}   
  
//删除cookies   
function delCookie(name)   
{   
    var exp = new Date();   
    exp.setTime(exp.getTime() - 1000);   
    var cval=getCookie(name);   
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();       
}

function shareFB(title)
{
    var url = "http://www.facebook.com/sharer.php?u=" +  encodeURIComponent(window.location.href) +"&t=" + encodeURIComponent(title);
    window.open(url,"name1","width=600,height=400,toolbar=no,scrollbars=no,menubar=no,screenX=100,screenY=100"); 
}

function shareTwitter(title)
{
    var url = "http://twitter.com/home?status=" +  encodeURIComponent(title) + "%3A+" +  encodeURIComponent(window.location.href) ;
    window.open(url,"name1","width=600,height=800,toolbar=no,scrollbars=no,menubar=no,screenX=100,screenY=100");   
}
