﻿//计算字符串长度
function getStrLen(sTargetStr)
{
    var sTmpStr, sTmpChar;
    var nOriginLen = 0;
    var nStrLength = 0;

    sTmpStr = new String(sTargetStr);
    nOriginLen = sTmpStr.length;

    for ( var i=0 ; i < nOriginLen ; i++ ) 
    {
        sTmpChar = sTmpStr.charAt(i);

        if (escape(sTmpChar).length > 4) 
        {
            nStrLength += 2;
        } else if (sTmpChar!='\r') 
        {
            nStrLength ++;
        }
    }

    return nStrLength; 
}
//选中表单复选框的所有的值
function CheckAll(form)
{
    for (var i=0;i<form.elements.length;i++)
    {
        var e = form.elements[i];
        if (e.type=="checkbox" && e.id != 'chkall') e.checked = form.chkall.checked;
    }
}
//是否已经选中操作行
function isSelected(form)
{
    flag=false;
    for (var i=0;i<form.elements.length;i++)
    {
        var e = form.elements[i];
        if (e.type=="checkbox" && e.id != 'chkall')
        {
            if(e.checked) flag=true;
        } 
    }
    return flag;
}
//打开窗口
function OpenWindowAndSetValue(theURL,Width,Height,winName)
{
	theTop = (window.screen.height-Height)/2;
    theLeft = (window.screen.width-Width)/2;
    var features = "toolbar=0,scrollbars=no,resizable=no,status=no,scroll=no,left=" + theLeft + ",top=" + theTop + ",width=" + Width + ",height=" + Height;
    
    window.SubWin = window.open(theURL,winName,features);
}

/**
* 客户端预览图片
* 页面中必须包含<div id="previewImage"></div>的标签
*/
function preview(id)
{
    var pic = document.getElementById(id);
    if(!pic || !pic.value)
    {
        document.getElementById('previewImage').innerHTML = '没有图片';
        return;
    }
    var patn = /\.jpg$|\.jpeg$|\.bmp$|\.png$|\.gif$/i;
    if(patn.test(pic.value.toLowerCase()))
    {
        document.getElementById('previewImage').innerHTML = '<img src=\''+pic.value+'\' width=100 height=100  onload=\'ResizeImage(this, 100, 100,true);\' align=absmiddle>';
    }
    else
    {    
        alert("对不起，图片格式不正确，请重新浏览选择！"); 
        pic.outerHTML=pic.outerHTML;
        pic.value='';
    }
}
//调整图片大小
var imageObject;
function ResizeImage(obj,MaxW,MaxH,flag)
{
    if(obj!=null) imageObject=obj;
    var state=imageObject.readyState;
    if(state!='complete')
    {
        setTimeout("ResizeImage(null,"+MaxW+","+MaxH+")",50);
        return;
    }
    var oldImage=new Image();
    oldImage.src=imageObject.src;
    var dW=oldImage.width;
    var dH=oldImage.height;
    if(dW>MaxW||dH>MaxH)
    {
        a=dW/MaxW;
        b=dH/MaxH;
        if(b>a)a=b;dW=dW/a;dH=dH/a;
    }
    if(dW>0&&dH>0)
    {
        imageObject.width=dW;
        imageObject.height=dH;
    }
    var dtop=MaxH/2-dH;
    if(dH!=MaxH && flag)
    {
        //if(dtop<=0) imageObject.style.marginTop='10px';
        //else 
        imageObject.style.marginTop=(MaxH/2-dH/2)+'px';
    }
}
/**
* 按图片大小打开合适的窗口
*/
var imgObj;
function checkImg(theURL,winName)
{
    if (typeof(imgObj) == "object")
    {
        if ((imgObj.width != 0) && (imgObj.height != 0))
        OpenFullSizeWindow(theURL,winName, ",width=" + (imgObj.width+20) + ",height=" + (imgObj.height+30));
    else
        setTimeout("checkImg('" + theURL + "','" + winName + "')", 100)
    }
}
function OpenFullSizeWindow(theURL,winName,features) 
{
    var aNewWin, sBaseCmd;
    sBaseCmd = "top=0,left=0,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,";
    if (features == null || features == ""){
        imgObj = new Image();
        imgObj.src = theURL;
        checkImg(theURL, winName)
    }
    else{
        aNewWin = window.open(theURL,winName, sBaseCmd + features);
        aNewWin.focus();
    }
}
/**
* 在val的下边显示obj层
* val 参考的层
* obj 显示的层
*/
function showDiv(val, obj, evt)
{
	if(obj)
	{
		obj.style.left = getOffsetLeft(val) + "px";
		obj.style.top = (getOffsetTop(val) + val.offsetHeight) + "px";
		if (obj.style.display=="")
		{
			obj.style.display="none";
		}else {
		    document.onclick=function(event){
		        event = event ? event:(window.event ? window.event : null);
		        if((event.target==undefined ? event.srcElement : event.target)!=val) obj.style.display="none";
		    }
			obj.style.display="";
		}
	}
}

//取层Top
function getOffsetTop(elm)
{
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent)
	{
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetTop;
}
//取层Left
function getOffsetLeft(elm)
{
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent)
	{
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}
//搜索下拉框值与指定值匹配，并选择匹配项
function SearchSelectValue(o_Select, s_Value)
{
	for (var i=0;i<o_Select.length;i++)
	{
		if (o_Select.options[i].value == s_Value){
		
			o_Select.selectedIndex = i;
			return true;
		}
	}
	return false;
}
//Flash输出
function insertFlash(elm,url,w,h)
{
	if(!document.getElementById(elm))return;
	var str='';
	str+='<object width="'+w+'" height="'+h+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0">';
	str+='<param name="movie" value="'+url+'">';
	str+='<param name="wmode" value="transparent">';
	str+='<param name="quality" value="autohigh">';
	str+='<param name="allowScriptAccess" value="always" > ';
	str+='<embed width="'+w+'" height="'+h+'" src="'+url+'" quality="autohigh" wmode="opaque" type="application/x-shockwave-flash" plugspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>';
	str+='</object>';
	document.getElementById(elm).innerHTML=str;
}
