﻿// JScript 文件

function $(aID)
{
    var obj = document.getElementById(aID);
    if (obj == null)
    {
        obj = document.getElementsByName(aID);
        if (obj != null && obj.length > 0)
        {
            obj = obj[0];
        }
    }
    return obj;
}

// swg，2007-9-18: find frame object
function getFrameNode(aFrameName, aDocument)
{
   var doc = aDocument;
   if (doc == null) doc = document;
   return doc.frames ? doc.frames[aFrameName] : doc.getElementById(aFrameName).contentWindow;
}

function ViewObj(obj)
{
    s = new Array();
	s.push(obj + " \n");
	try
	{
	    for(i in obj)
	    {
	        if(typeof(eval("obj." + i)) == "function")
		        s.push("\n" + i + ": function");
            else
  		        s.push("\n" + i + ": " + eval("obj." + i));
        }
    }
    catch(e)
    {
      s.push("\n" + e);
    }
    return s.toString();
}

function PrintObj(obj)
{    
    alert(ViewObj(obj));
}

function BoundTextByWidth(text, width){

    var newtext = '<div style="white-space:nowrap; width:'+width+';overflow:hidden; text-overflow: ellipsis;">' + text +  '</div>';

    return newtext;
}


function BoundTextByTagWidth(text, tagName, width){

    var newtext = '<'+tagName+' style="white-space:nowrap; width:'+width+';overflow:hidden; text-overflow: ellipsis;">' + text +  '</'+tagName+'>';

    return newtext;
}

function BoundTagByWidth(obj, width){

    if(obj){
        obj.style.whiteSpace = "nowrap";
        obj.style.overflow = "hidden";
        obj.style.textOverflow = "ellipsis";
        obj.style.width = width;    
    }
}


function TruncateText(text, length){
    if(text!=null&&text!=''&&text.length>length){
        return text.substring(0,length)+'...';
    
    }else{
        return text;        
    }
}

function   nocontextmenu(evt)     
{   
    evt.cancelBubble   =   true   
    evt.returnValue   =   false;   
        
    return   false;   
}   
    


function GenerateGUID(){
    var guid = "";
    for(var   i = 1; i <= 32; i++)
    {
      var n = Math.floor(Math.random() * 16.0).toString(16);
      guid += n;
    }
    return guid;
}



function ResizePageDiv(namePrefix, indexList, width)
{
    var s = indexList.split(',');
    var i;
    for(i=0; i<s.length; i++)
    {     
        var divObj = document.getElementById(namePrefix + s[i]);
        if(divObj)
        {
            divObj.style.width = width;
        }
    }
}


function DownloadFile(aURL, aIsViewMode)
{
    if (!aIsViewMode)
    {
        var id = 0;
        while (document.getElementById("_downloadFrame" + id) != null)
        {
            id += 1;
            if (id > 100)
            {
                // 防止死循环
                return;
            }
        }

        var frm = document.createElement("iframe");
        frm.id = "_downloadFrame" + id;
        frm.style.position = "absolute";
        frm.style.width = "0px";
        frm.style.height = "0px";
        frm.style.visibility = "hidden";
        // 不能加这句，在opera中设置成none后就不能弹出下载对话框了
        //frm.style.display = "none";
        // 不能在onload的时候删除自己，会出现不下载的情况
        //frm.onload = function() { document.body.removeChild(frm); };
        frm.src = aURL;
        document.body.appendChild(frm);
    }
    else
    {
        window.open(aURL, "_blank");
    }
}

function getEvtClientX(evt)
{
    evt = evt ? evt : (window.event ? window.event : null);
    if(null == evt) return 0;
    var temp = 0;
    if(null != window.scrollMaxY)  //是firefox
    {
        temp = evt.pageX;
    }
    else  // 是Opera和IE
    {
        temp = evt.clientX;
    }
	return temp;
}

function getEvtClientY(evt)
{
    evt = evt ? evt : (window.event ? window.event : null);
    if(null == evt) return 0;
    var temp = 0;
    if(null != window.scrollMaxY)  //是firefox
    {
        temp = evt.pageY;
    }
    else  // 是Opera和IE
    {
        temp = evt.clientY;
    }
	return temp;
}

//获得单选框的值
function GetRadioValue(radioName)
{
    var radioInputs = document.getElementsByName(radioName);
    for (var i = 0; i < radioInputs.length; i++)
    {
        if ((radioInputs[i].type == "radio" || radioInputs[i].type == "check") && radioInputs[i].checked)
        {
            return radioInputs[i].value;
        }
    }
    return "";
}

//验证控件是否为空
function CheckInputEmpty(aControl, aHint)
{
    var control = $(aControl);
    if (control && control.value == "")
    {
        alert(aHint);
        control.focus();
        return false;
    }
    return true;
}

//检查是否是数字
function CheckInputNumberValid(aControl, aHint, aMin, aMax)
{
    var control = $(aControl);
    var value = parseInt(control.value);
    if (isNaN(value))
    {
        alert(aHint);
        control.focus();
        return false;
    }

    if (aMin != null && value < aMin)
    {
        alert(aHint);
        control.focus();
        return false;
    }

    if (aMax != null && value > aMax)
    {
        alert(aHint);
        control.focus();
        return false;
    }

    return true;
}

// 检查浮点数是否正确
function CheckInputFloatValid(aControl, aHint, aMin, aMax)
{
    var control = $(aControl);
    var value = parseFloat(control.value);
    if (isNaN(value))
    {
        alert(aHint);
        control.focus();
        return false;
    }

    if (aMin != null && value < aMin)
    {
        alert(aHint);
        control.focus();
        return false;
    }

    if (aMax != null && value > aMax)
    {
        alert(aHint);
        control.focus();
        return false;
    }

    return true;
}
