// 去空格
function BaseTrim(str)
{
	  lIdx=0;rIdx=str.length;
	  if (BaseTrim.arguments.length==2)
	    act=BaseTrim.arguments[1].toLowerCase()
	  else
	    act="all"
      for(var i=0;i<str.length;i++)
	  {
	  	thelStr=str.substring(lIdx,lIdx+1)
		therStr=str.substring(rIdx,rIdx-1)
        if ((act=="all" || act=="left") && thelStr==" ")
		{
			lIdx++
        }
        if ((act=="all" || act=="right") && therStr==" ")
		{
			rIdx--
        }
      }
	  str=str.slice(lIdx,rIdx)
      return str
}

// 图片按比例缩放
// 调用：<img src="图片" onload="javascript:DrawImage(this,100,100)">
function DrawImage(ImgD,w,h)
{
	var image = new Image();
	image.src = ImgD.src;
	var iwidth  = w; //定义允许图片宽度，当宽度大于这个值时等比例缩小
	var iheight = h; //定义允许图片高度，当宽度大于这个值时等比例缩小
	
	if(image.width>0 && image.height>0)
	{
		ImgD.width =image.width; 
		ImgD.height=image.height;
		if(image.width/image.height >= iwidth/iheight)
		{
			if(image.width>iwidth)
			{ 
				ImgD.width =iwidth;
				ImgD.height=(image.height*iwidth)/image.width;
			}
		}
		else
		{
			if(image.height>iheight)
			{ 
				ImgD.height=iheight;
				ImgD.width =(image.width*iheight)/image.height; 
			}
		}
	}
}

// 友情链接  
// 调用：onchange="QLink(this)"
function QLink(obj)
{
	if(obj.value!="")
	{
		window.open(obj.value);
		obj.selectedIndex=0;
	}
}

// 加入收藏
// 调用： <a href="#" onclick="AddFavorite(window.location,document.title);return false;">加入收藏</a> 
function AddFavorite(sURL, sTitle)
{
    try{
        window.external.addFavorite(sURL, sTitle);
    }catch(e){
		try{
			window.sidebar.addPanel(sTitle, sURL, "");
		}catch(e){
			alert("加入收藏失败，请使用Ctrl+D进行添加");
		}
    }
}

// 设为首页
// 调用： <a href="#" onclick="SetHome(this,window.location);return false;">设为首页</a>
function SetHome(obj,vrl)
{
	try
	{
		obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
	}
	catch(e)
	{
		if(window.netscape)
		{
			try
			{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}
			catch(e)
			{
				alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");
			}
			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',vrl);
		 }
	}
}

//===============================
//      ===【表单验证】===
//===============================

// 【表单验证】是否为空
function ChTxt(obj,Msg)
{
	if(obj.value=="")
	{
		alert(Msg);
		if(obj.type!="hidden") obj.focus();
		return true;
	}return false;
}

// 【表单验证】两次输入密码是否相同
function ChPass(obj,obj1,Msg)
{
	if(obj.value!=obj1.value)
	{
		alert(Msg);
		obj.focus();
		return true;
	}return false;
}

// 【表单验证】身份证号
function ChIDCARD(obj,Msg1,Msg2)
{
	var txt = BaseTrim(obj.value);
	if(txt=="")
	{
		alert(Msg1);
		obj.focus();
		return true;
	}
	if(txt.length!=18)
	{
		alert(Msg2);
		obj.focus();
		return true;
	}
	return false;
}

// 【表单验证】根据 bol 判断是否 显示 Msg
function ChBoll(bol,Msg)
{
	if(bol)
	{
		alert(Msg);
		return true;
	}return false;
}

//===============================
//      ===【表单赋值】===
//===============================

// 【表单赋值】Select
function SetValueToSelect(obj,Txt)
{
	for(i=0;i<obj.options.length;i++)
	{
		if(obj.options[i].value==Txt)
		{
			obj.options[i].selected = true;
			return;
		}
	}
}

// 【表单赋值】时间写入下拉菜单
function SetInputDate(y,m,d,obj)
{
	if(obj.value!="") 
	{
		dstr = obj.value.split("-");
		SetValueToSelect(y,dstr[0]);
		SetValueToSelect(m,dstr[1]);
		SetValueToSelect(d,dstr[2]);
	}
}

// 【表单赋值】时间写入隐藏域
function GetInputDate(y,m,d,obj)
{
	obj.value = y.value + "-" + m.value + "-" + d.value;
}

// 【表单赋值】其他选项 Radio
function SetPsToRadio(obj,obj1)
{
	if(obj.checked)
	{
		obj1.style.display = "";
		obj.value = obj1.value;
	}
	else
	{
		obj1.style.display = "none";
	}
}

// 【表单赋值】其他选项 Select
function SetPsToSelect(obj,obj1,QTTxt)
{	
	if(obj.options[obj.selectedIndex].text==QTTxt)
	{
		obj1.style.display = "";
		obj.options[obj.selectedIndex].value = obj1.value;
	}
	else
	{
		obj1.style.display = "none";
	}
}

//===============================
//      ===【表单输出】===
//===============================

// 【表单输出】Option 数字型
function WriteOption(SNum,ENum)
{
	for(var i=SNum;i<=ENum;i++)
	{
		document.write("<OPTION value="+i+">"+i+"</OPTION>");
	}
}
