﻿function getValue(ctrlId){
    var _ctrl = document.getElementById(ctrlId);
    if (_ctrl) {
        if (_ctrl.tagName == "SPAN") 
            return _ctrl.innerText;
        else if(_ctrl.tagName == "select-one")
            return _ctrl.options[_ctrl.selectedIndex].value;
        else 
            if (_ctrl.tagName == "DIV") 
                return _ctrl.innerHTML;
            else 
                return _ctrl.value;
    }
    else {
        return "";
    }
}

function setValue(ctrlId, ctrlValue){
    var _ctrl = document.getElementById(ctrlId);
    if (_ctrl == null) 
        return;
    
    if (_ctrl.tagName == "A" || _ctrl.tagName == "SPAN" || _ctrl.tagName == "DIV" || _ctrl.tagName.indexOf("H") > -1 || _ctrl.tagName == "TD" || _ctrl.tagName == "LABEL") 
        _ctrl.innerHTML = ctrlValue;
    else
    {
        if (_ctrl.type == "select-one") 
        {
            var values = ctrlValue.split('$');
            if (values.length == 2) 
                _ctrl.options.add(new Option(values[0], values[1]));
            else 
                _ctrl.options.add(new Option(controlValue, controlValue));
        }
        else 
        {
            if (_ctrl.type == "checkbox") {
                if (controlValue == "true") 
                    _ctrl.checked = true;
                else 
                    _ctrl.checked = false;
            }
            else 
                _ctrl.value = ctrlValue;
        }
    }
}

function lockScreen(){
    var lockDiv = document.getElementById("__lockDiv__");
    if (lockDiv == null) {
        lockDiv = document.createElement("DIV");
        lockDiv.id = "__lockDiv__";
        lockDiv.innerHTML = "<iframe src='../mark.htm' id='LockIFrame' width='100%' height='100%'></iframe>";
    }
    lockDiv.className = "DisableStyle";
    lockDiv.style.width = screen.width + "px";
    lockDiv.style.height = document.body.style.height + document.body.scrollHeight + "px";
    lockDiv.style.display = "block";
    document.body.appendChild(lockDiv);
}

function unlockScreen(){
    var lockDiv = document.getElementById("__lockDiv__");
    if (lockDiv) {
        lockDiv.className = "";
        lockDiv.style.display = "none";
    }
}

/*将某个元素移动到屏幕中央 */
function resetControlToCenter(controlId, controlWidth, controlHeight){
    if (!document.getElementById(controlId)) 
        return;
    
    if (controlWidth) 
        document.getElementById(controlId).style.width = controlWidth;
    if (controlHeight) 
        document.getElementById(controlId).style.height = controlHeight;
    document.getElementById(controlId).style.top = (document.documentElement.scrollTop +
    (document.documentElement.clientHeight - document.getElementById(controlId).offsetHeight) / 2) +
    "px";
    
    document.getElementById(controlId).style.left = (document.documentElement.scrollLeft +
    (document.documentElement.clientWidth - document.getElementById(controlId).offsetWidth) / 2) +
    "px";
}

/*字符串是否是日期格式 */
function isDateTime(sDate){
    var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    var iaDate = new Array(3);
    var year, month, day;
    
    if (arguments.length != 1) 
        return false;
    iaDate = sDate.toString().split("/");
    if (iaDate.length != 3) 
        return false;
    if (iaDate[0].length > 2 || iaDate[1].length > 2) 
        return false;
    
    year = parseFloat(iaDate[2]);
    month = parseFloat(iaDate[0]);
    day = parseFloat(iaDate[1]);
    
    if (year < 1900 || year > 2100) 
        return false;
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) 
        iaMonthDays[1] = 29;
    if (month < 1 || month > 12) 
        return false;
    if (day < 1 || day > iaMonthDays[month - 1]) 
        return false;
    return true;
}

function isTime(sTime)
{
    var hs = sTime.split(":");
    if(hs.length > 2)
        return false;
    if(hs.lenght = 1)
    {
        try
        {
            if(!isNumber(hs[0]))
                return false;
            var hour = parseInt(hs[0]);
            if(hour < 0 || hour > 24)
                return false;
            return true;
        }catch(e){return false;}
    }
    else
    {
        try
        {
            if(!isNumber(hs[0]) || !isNumber(hs[1]))
                return false;
            var hour = parseInt(hs[0]);
            var minute = parseInt(hs[1]);
            if(hour < 0 || minute < 0 || hour > 24 || minute > 60)
                return false;
            return true;
        }
        catch(e){return false;}
    }
}

/*字符串是否可以转换成Email*/
function isEmail(str){
    var reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    return reg.test(str);
}

/*========================判断字符串是否可以转换成数字(isNumber)======================================*/
function isNumber(str){
    var i, j, strTemp;
    strTemp = "0123456789";
    if (str.length == 0) 
        return false;
    for (i = 0; i < str.length; i++) {
        j = strTemp.indexOf(str.charAt(i));
        if (j == -1) {
            /*说明有字符不是数*/
            return false;
        }
    }
    /*说明是数*/
    return true;
}


/**Array的自定义操作
 是否包含Item**/
Array.prototype.contains = function(varElement){
    var result = false;
    for (var i = 0; i < this.length; i++) {
        if (this[i] == varElement) {
            result = true;
            break;
        }
    }
    return result;
}

/**删除Item **/
Array.prototype.deleteElement = function(varElement){
    var numDeleteIndex = -1;
    for (var i = 0; i < this.length; i++) {
        /**严格比较，即类型与数值必须同时相等**/
        if (this[i] === varElement) {
            this.splice(i, 1);
            numDeleteIndex = i;
            break;
        }
    }
    return numDeleteIndex;
}

/**返回字符串：xxx,xxx,xxx格式**/
Array.prototype.toStr = function(){
    var result = "";
    for (var i = 0; i < this.length; i++) {
        result += this[i] + ",";
    }
    // if(result != "")
    // result = result.substr(0,
    return result;
}

Array.prototype.clear = function(varElement){
    for (var i = 0; i < this.length; i++) 
    {
        this.splice(i, 1);
    }
}

function trim(str){
    return str.replace(/(^\s*)|(\s*$)/g, "");
}

function ltrim(str){
    return str.replace(/(^\s*)/g, "");
}

function rtrim(str){
    return str.replace(/(\s*$)/g, "");
}

function showCtrl(ctrlId){
    document.getElementById(ctrlId).style.display = "block";
}

function lockControl(id)
{
    var c = document.getElementById(id);
    if(c)
        c.disabled = "disabled";
}

function unlockControl(id)
{
    var c = document.getElementById(id);
    if(c)
        c.disabled = "";
}

/*消除A标签鼠标点上去的虚线框*/
onload=function()
{
	var a=document.links;
	for(var i=0;i<a.length;i++)a[i].onfocus=function(){this.blur();}
}

/*function $id(id)
{
	return document.getElementById(id);
}
function $(source)
{
	if(typeof(source) == "string")
	{
		source = $id(source);
	}
	if(!source)
	{
		return null;
	}
	if(window.isIE && !source.expand)
	{
		for(var item in HTMLElement.prototype) 
		{
			source[item] = HTMLElement.prototype[item];
		}
		var prototype = source.getType().prototype;
		for(var item in prototype)
		{
			source[item] = prototype[item]
		}
		source.expand = true;
	}
	return source;
}*/

function getObj(source)
{
	if(typeof(source) == "string")
	{
		source = document.getElementById(source);
	}
	if(!source)
	{
		return null;
	}
	if(window.isIE && !source.expand)
	{
		for(var item in HTMLElement.prototype) 
		{
			source[item] = HTMLElement.prototype[item];
		}
		var prototype = source.getType().prototype;
		for(var item in prototype)
		{
			source[item] = prototype[item]
		}
		source.expand = true;
	}
	return source;
}

/*
window.onerror = function(msg)
{
    alert("there'are some errors occured: " + msg);
    return false;
}
*/

function ItemNum_KeyPress()
{
  if(!((window.event.keyCode>=48) && (window.event.keyCode<=57)))
   {
		window.event.keyCode=0;
        window.event.returnvalue = false;
           
   }
}

/*****************************随机数函数********************************/
rnd.today = new Date(); 

rnd.seed=rnd.today.getTime(); 

function rnd() 
{
    rnd.seed = (rnd.seed*9301+49297) % 233280; 

    return rnd.seed/(233280.0); 
}

function rand(number) 
{ 
    return Math.ceil(rnd()*number); 
}

function showHeaderNoPermission(event)
{
    var mydiv = getObj("DivNoPermission");
    mydiv.style.left=event.clientX+document.body.scrollLeft-mydiv.style.width-5+"px";
    mydiv.style.top=event.clientY+document.body.scrollTop+"px";
    mydiv.className='';
}

function hideHeaderNoPermission()
{
    getObj("DivNoPermission").className="hide";
}

String.prototype.trim     =   function(){return   this.replace(/(^\s*)|(\s*$)/g,   "");} 

function IsEmpty(str,otherinput)
{
    if(str.trim()==""||str.trim()==otherinput)
        return true;
    return false;
}

function CheckIsEmpty(objid,otherinput,msg)
{
    if(objid==null||objid=="") return false;
    var obj=getObj(objid);
    if(obj==null) return false;
    if(!IsEmpty(obj.value.trim(),otherinput)) return false;
    AlertAndFocus(obj,msg);
    return true;
}

function CheckValueLength(objid,maxlength,msg)
{
    if(objid==null||objid=="") return false;
    var obj=getObj(objid);
    if(obj==null) return false;
    if(obj.value.length<=maxlength) return true;
    AlertAndFocus(obj,msg);
    return false;
}

function CheckEmail(objid,msg)
{
    if(objid==null||objid=="") return false;
    var obj=getObj(objid);
    if(obj==null) return false;
    if(isEmail(obj.value)) return true;
    AlertAndFocus(obj,msg);
    return false;
}

function AlertAndFocus(obj,msg)
{
    if(obj==null) return;
    //alert(msg);
    showDialog(msg);
    try
    {
        if(obj.disabled=="")
            obj.focus();
    }
    catch(e){}
}

function addCss(ctrlId,cssName)
{
    if(containsCss(ctrlId,cssName)) return false;
    getObj(ctrlId).className+=" "+cssName; return true;
}

function removeCss(ctrlId,cssName)
{
    var newCssName="";
    var t=getObj(ctrlId).className.trim().split(" ");
    for(i=0;i<t.length;i++)
    {
        if(t[i]=="" || t[i]==cssName)
            continue;
        newCssName+=t[i]+" ";
    }
    setCss(ctrlId,newCssName);
}

function containsCss(ctrlId,cssName)
{
    var t=getObj(ctrlId).className.trim().split(" ");
    for(i=0;i<t.length;i++)
    {
        if(t[i]==cssName)
            return true;
    }
    return false;
}

function getCss(ctrlId)
{
    return getObj(ctrlId).className;
}

function setCss(controlId, cssName){
    var ctrl = document.getElementById(controlId);
    if (ctrl) {
        ctrl.className = cssName;
    }
}

function replaceCss(controlId, oldCssName, newCssName)
{
    var css=" "+getCss(controlId)+" ";
    css=css.replace(oldCssName,newCssName);
    setCss(controlId,css);
}

function HTMLEncode (str)
{
    var div = document.createElement("div");
    if(div.innerText==null)//ff
    {
        var text = document.createTextNode(str);
        div.appendChild(text);
        return div.innerHTML.replace(/\n/g,"<br>");
    }
    else//ie
    {
        div.innerText=str;
        return div.innerHTML;
    }
}

function clearDropdownList(id,title)
{
    var dropdownList = document.getElementById(id);
    if(dropdownList == null)
        return;
    while(dropdownList.options.length >0)
    {
        dropdownList.remove(dropdownList.options[0]);
    }
    if(title)
        dropdownList.options.add(new Option(title,""));
}

/*values 格式：xxxxx#aaaaaa|yyyyyy#bbbbbb   其中|可以自定义，使用参数valueSeparator*/
function fillDropdownList(id,values,valueSeparator,title,needClear)
{
    var dropdownList = document.getElementById(id);
    if(dropdownList == null)
        return;
    if(needClear)
    {
        clearDropdownList(id);
        dropdownList.options.add(new Option(title,"0"));
    }
    var arrValue = values.split(valueSeparator);
    for(i=0;i<arrValue.length;i++)
    {
        if(!arrValue[i] || arrValue[i] == "")
            continue;
        var nameId = arrValue[i].split('#');
        dropdownList.options.add(new Option(nameId[0],nameId[1]));
    }
}

//兼容firefox的outerHTML属性
if (window.HTMLElement) {
  HTMLElement.prototype.__defineSetter__("outerHTML",function(sHTML) {
        var r=this.ownerDocument.createRange();
        r.setStartBefore(this);
        var df=r.createContextualFragment(sHTML);
        this.parentNode.replaceChild(df,this);
        return sHTML;
    });

    HTMLElement.prototype.__defineGetter__("outerHTML",function() {
     var attr;
        var attrs=this.attributes;
        var str="<"+this.tagName.toLowerCase();
        for (var i=0;i<attrs.length;i++) {
            attr=attrs[i];
            if(attr.specified)
                str+=" "+attr.name+'="'+attr.value+'"';
        }
        if(!this.canHaveChildren)
            return str+">";
        return str+">"+this.innerHTML+"</"+this.tagName.toLowerCase()+">";
        });

   HTMLElement.prototype.__defineGetter__("canHaveChildren",function() {
     switch(this.tagName.toLowerCase()) {
         case "area":
         case "base":
         case "basefont":
         case "col":
         case "frame":
         case "hr":
         case "img":
         case "br":
         case "input":
         case "isindex":
         case "link":
         case "meta":
         case "param":
         return false;
     }
     return true;
   });
}

function showDialog(content,title,closeAction)
{
    var container=getObj("dialog-box");
    if(container==null)
    {
        container=document.createElement("div");
        document.body.appendChild(container);
    }
    if(title==null||title=="") title="Notice";
    title=title.replace(/'/g,"''");
    content=content.replace(/\n/g, "<br>").replace(/'/g,"''");
    var html="";
    html+="<div id='dialog-box' style='margin:0 auto;width:304px;border:1px solid #366293;text-align:center;line-height:150%;position:absolute;z-index:999999999'>";
    html+=" <div class='box-top' style='width:289px;height:20px;background-color:#82a5cc;font:bold 14px Arial;color:#fff;margin:0;padding-top:5px;padding-left:15px;text-align:left;'>"+title+"</div>";
    html+=" <div class='box-main' style='margin:0;padding:20px 15px 10px;background-color:#e6ebea;border:2px solid #82a5cc;width:270px;font:13px Arial, Helvetica, sans-serif;color:#333;'>";
    html+="     <p style='margin:10px 0;padding:0;'>"+content+"</p>";
    html+="     <p style='margin:10px 0;padding:0;'><input type='button' onclick='closeDialog();"+closeAction+"' value='ok' style='width:80px;height:25px;background-color:#f5f5f5;border:1px solid #82a5cc;font:12px ''Trebuchet MS'', Arial, Helvetica, sans-serif;color:#82a5cc;' /></p>";
    html+=" </div>";
    html+="</div>";
    container.outerHTML=html;
    resetControlToCenter("dialog-box")
}

function closeDialog()
{
    var container=getObj("dialog-box");
    if(container!=null)
        container.style.display="none";
}

function moveDialog()
{
    var container=getObj("dialog-box")
    if(container!=null)
    {
        container.style.top=(document.documentElement.scrollTop+(document.documentElement.clientHeight-container.offsetHeight)/2)+"px";
        container.style.left=(document.documentElement.scrollLeft+(document.documentElement.clientWidth-container.offsetWidth)/2)+"px";
    }
}

function showCtrl(ctrlId){
    var c = document.getElementById(ctrlId);
    if(c)
        c.style.display="block";
}
function hideCtrl(ctrlId){
    var c = document.getElementById(ctrlId);
    if(c)
        c.style.display="none";
}

function setInnerText(id,value)
{
    if(document.all)
        getObj(id).innerText=value;
    else
        getObj(id).textContent=value;
}

function copyText(value)
{
    window.clipboardData.setData('text',value);
}

function resizeImg(me,width,height)
{
    if(me.width>width) me.width=width;
    if(me.height>height) me.height=height;
}

function replaceSymbol(value)
{
    //value=value.replace(/[\s~!@#$%^&*()_+`=\[\]\{\}|:;\"'<>?,\.\/]/g,"-");
    value=value.replace(/[\W]/g,"-");
    return value;
}

function Typesetting(){var D=document; F(D.body); function F(n){var u,r,c,x; if(n.nodeType==3){ u=n.data.search(/\S{10}/); if(u>=0) { r=n.splitText(u+10); n.parentNode.insertBefore(D.createElement("WBR"),r); } }else if(n.tagName!="STYLE" && n.tagName!="SCRIPT"){for (c=0;x=n.childNodes[c];++c){F(x);}} } }

window.onscroll=moveDialog;
window.onresize=moveDialog;