﻿//
//注意此文件不要和以前的版本随意覆盖 2010-03-02
//
function $(id){
    return document.getElementById(id);
}

function $V(id){
    return document.getElementById(id).value;
}

function $GetLeft(obj){              //得到对象的左边距
    var left = obj.offsetLeft;
    obj = obj.offsetParent;
    while(obj.tagName.toUpperCase() != "BODY" && obj.tagName.toLowerCase() != "html"){
        left += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    return left;
}

function $GetTop(obj){               //得到对象的上边距
    var top = obj.offsetTop;
    obj = obj.offsetParent;
    while(obj.tagName.toUpperCase() !="BODY" && obj.tagName.toLowerCase() != "html"){ 
        top += obj.offsetTop;
        obj = obj.offsetParent;
    }        
    return top;
}

function $ExpVal(exp, input){
    var express = new RegExp(exp);
    return express.test(input);      
}

function $LTrim(str){
    for(i=0; i<str.length; i++){
        if(str.charAt(i) != " " && str.charAt(i) != "　"){
            str = str.substring(i);
            return str;
        } 
    }
    return "";
}

function $RTrim(str){
    for(i=str.length-1; i>=0; i--){
        if(str.charAt(i) != " " && str.charAt(i) != "　"){
            str = str.substring(0,i + 1);
            return str;
        } 
    }
    return "";
}

function $Trim(str){
    return $RTrim($LTrim(str));
}

function $ReplaceAll(srcStr,replaceStr,targetStr){
    var index = srcStr.indexOf(replaceStr);
    while(index > -1){
        srcStr = srcStr.replace(replaceStr,targetStr);        
        index = srcStr.indexOf(replaceStr);
    }
    return srcStr;
}

function $RemoveSelectOptions(obj,startIndex){
    while(obj.options.length > startIndex){
        obj.options.remove(obj.options.length - 1);
    }
}

function $AddSelectOptions(sltObj, str){
     var temp, index = str.indexOf(";") ,i;
     while(index > 0){
         temp = str.substring(0,index);
         i = temp.indexOf(",");
         sltObj.options.add(new Option(temp.substring(i + 1),temp.substring(0,i)));
         str = str.replace(temp + ";","");
         index = str.indexOf(";");
     } 
}

function $GetFileExtension(filePath){
    var index = filePath.lastIndexOf(".");
    if(index < 0)
        return "";
    return filePath.substring(index);
}

function $CheckImageExtension(imgPath){
    var expandName = $GetFileExtension(imgPath).toLowerCase();
    if(expandName != ".gif" && expandName != ".jpg" && expandName != ".jpeg" && expandName != ".jpe" && expandName != ".bmp" && expandName != ".ico" && expandName != ".png")
        return false;
    return true;
}

function $CheckedGridViewRows(cbx, gvw){
    if(gvw.firstChild.tagName.toUpperCase() == "TBODY")
         gvw = gvw.firstChild;
    for(var i = 1; i < gvw.childNodes.length; i++){
        gvw.childNodes[i].firstChild.firstChild.checked = cbx.checked;
    }
}

function $CheckGridViewRowCheckedState(gvw, startRowIndex){
    if(gvw.firstChild.tagName.toUpperCase() == "TBODY")
    	gvw = gvw.firstChild;
    for(var i = startRowIndex; i < gvw.childNodes.length; i++){
        if(gvw.childNodes[i].firstChild.firstChild.checked){
            return true;
        }
    }
    return false;
}

function $GetQueryString(key){
    key = key.toUpperCase() + "=";
    var hrefs = location.search.substring(1).split('&');
    for(var i=0; i<hrefs.length; i++){
        if(hrefs[i].toUpperCase().indexOf(key) == 0){
            return hrefs[i].substring(key.length);
        }
    }
    return null;
}
