﻿// JScript 文件
Number.prototype.NaN0=function(){return isNaN(this)?0:this;}
String.prototype.Trim = function() {return this.replace(/(^\s*)|(\s*$)/g, "");}
String.prototype.LTrim = function() { return this.replace(/(^\s*)/g, "");}
String.prototype.RTrim = function() { return this.replace(/(\s*$)/g, "");}

//array = array.deleteItem(1);
Array.prototype.deleteItem = function(n) {
  if(n<0)
    return this;
  else
    return this.slice(0,n).concat(this.slice(n+1,this.length));
}

function request(strname)
{
    var hrefstr,pos,parastr,para,tempstr;
    hrefstr = window.location.href;
    pos = hrefstr.indexOf("?")
    parastr = hrefstr.substring(pos+1);
    para = parastr.split("&");
    tempstr="";
    for(i=0;i<para.length;i++)
    {
        tempstr = para[i];
        pos = tempstr.indexOf("=");
        if(tempstr.substring(0,pos) == strname)
        {
            return tempstr.substring(pos+1);
        }
    }
   return null;
}
String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

//判断是否是IE
var agt=navigator.userAgent.toLowerCase();
var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));
var tBodyNode;
if (ie) {tBodyNode = 0;}else{tBodyNode = 1};

function replaceAll(s,y,t) 
{ 
     var i; 
     var s2=s; 
     var y2=y;
     var t2=t;
     
     while(s2.indexOf(y2)>=0)
     {
         i = s2.indexOf(y2); 
         s2 = s2.substring(0, i) + t2 + s2.substring(i + y2.length, s2.length); 
     } 
     return s2; 
}

//静止选择
var nowaddStyle = document.createElement("style");
function addGlobalStyle(css) {
    var head;
    head = document.getElementsByTagName("head")[0];
    if (!head) { return; }
    nowaddStyle.type = 'text/css';
    nowaddStyle.innerHTML = css;
    head.appendChild(nowaddStyle);
}
function pageNoSelect(bool){
    if (bool){
        if (typeof document.onselectstart != "undefined")
            document.onselectstart = new Function ("return false");
        else{
            addGlobalStyle('body { -moz-user-select : none !important; }');
        }
    }else{
        if (typeof document.onselectstart != "undefined")
            document.onselectstart = new Function ("return true");
        else{
            nowaddStyle.innerHTML = "";
        }
    }
}
//静止右键
function norightclick(bool){
    if (ie){
        document.oncontextmenu = function(){event.cancelBubble = true; event.returnValue = false; return false;}
    }else{
        document.oncontextmenu = function(e){return   false;}
    }
}


//格式化时间
function formatTime(cell){
    if (cell.toString().length == 1){
        cell = "0"+cell;
    }
    return cell
}
String.prototype.trim= function()  
{
    return this.replace(/(^\s*)|(\s*$)/g, "");  
}
function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure){
    var sCookie = sName + "=" + encodeURI(sValue);
    if (oExpires){
        sCookie += "; expiress=" + oExpires.toGMTString();
    }
    if (sPath){
        sCookie += "; path=" + sPath;
    }
    if (sDomain){
        sCookie += "; domain=" + sDomain;
    }
    if (bSecure){
        sCookie += "; secure";
    }
    document.cookie = sCookie;
}
function getCookie(sName){
    var sRE = "(?:; )?" + sName + "=([^;]*);?";
    var oRE = new RegExp(sRE);
    if (oRE.test(document.cookie)){
        return decodeURI(RegExp["$1"]);
    }else{
        return null;
    }
}
function deleteCookie(sName, sPath, sDomain){
    setCookie(sName, "", new Date(0), sPath, sDomain);
}
function ReplaceTB (str, space){
    str = String(str).trim();
    str = replaceAll(str, "\'", "");
    str = replaceAll(str, "\"", "");
    str = replaceAll(str, "&lt;", "");
    str = replaceAll(str, "&gt;", "");
    if (space){
        str = replaceAll(str, " ", "");
    }
    return str;
}

function getEvent(ev){
    ev=ev || window.event;
    return ev;
}
//鼠标坐标
function mousePos(ev){
    ev=ev || window.event
    if(ev.pageX || ev.pageY){
        return {x:ev.pageX, y:ev.pageY};
    }
    return {
        x:ev.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft,
        y:ev.clientY + document.documentElement.scrollTop - document.documentElement.clientTop
    };
}
//鼠标坐标相对位置
function mousePosRelate(ev){
    ev=ev || window.event
    if(ev.offsetX || ev.offsetY){
        return {x:ev.offsetX, y:ev.offsetY};
    }
    return {
        x:ev.layerX + document.documentElement.scrollLeft - document.documentElement.clientLeft,
        y:ev.layerY + document.documentElement.scrollTop - document.documentElement.clientTop
    };
}
//对象跟随移动 obj移动的对象  mPos 移动的坐标   Center 是否居中bool型   max 最大移动范围数组 {x:0,y:0,w:200,h:150}  obj必须有widht height属性
function startDrag(obj, mPos, center, max){
    if (center){
        obj.style.left = (mPos.x - parseInt(obj.style.width)/2) + "px";
        obj.style.top = (mPos.y - parseInt(obj.style.height)/2) + "px";
    }else{
        obj.style.left = mPos.x + "px";
        obj.style.top = mPos.y + "px";
    }
    if (max){
        if (parseInt(obj.style.left) < max.x){
            obj.style.left = max.x + "px";
        }
        if (parseInt(obj.style.left) + parseInt(obj.style.width) > max.x + max.w){
            obj.style.left = (max.x + max.w - parseInt(obj.style.width)) + "px";
        }
        if (parseInt(obj.style.top) < max.y){
            obj.style.top = max.x + "px";
        }
        if (parseInt(obj.style.top) + parseInt(obj.style.height) > max.y + max.h){
            obj.style.top = (max.y + max.h - parseInt(obj.style.height)) + "px";
        }
    }
}
///   <summary>
///   将URL地址编码
///   </summary>
function encodeUrl(text)
{
    var sreturn = text;
    sreturn = replaceAll(sreturn, "?", "mym@*@mym");
    sreturn = replaceAll(sreturn, "&", "mym@@@mym");
    sreturn = replaceAll(sreturn, "&amp;", "mym@@1@mym");
    sreturn = replaceAll(sreturn, "%", "mym@5@mym");
    sreturn = replaceAll(sreturn, "+", "mym@0@mym");
    sreturn = replaceAll(sreturn, "=", "mym@9@mym");
    sreturn = replaceAll(sreturn, "/", "mym@7@mym");
    sreturn = replaceAll(sreturn, "\\", "mym@6@mym");
    sreturn = replaceAll(sreturn, "\"", "mym@4@mym");
    sreturn = replaceAll(sreturn, "<", "mym@8@mym");
    sreturn = replaceAll(sreturn, ">", "mym@3@mym");
    sreturn = replaceAll(sreturn, "#", "mym@2@mym");
    sreturn = replaceAll(sreturn, "'", "mym@-@mym");
    return sreturn;
}
///   <summary>
///   将URL地址解码
///   </summary>
function decodeUrl(text)
{
    var sreturn = text;
    sreturn = replaceAll(sreturn, "mym@*@mym", "?");
    sreturn = replaceAll(sreturn, "mym@@@mym", "&");
	sreturn = replaceAll(sreturn, "mym@@1@mym", "&amp;");
    sreturn = replaceAll(sreturn, "mym@5@mym", "%");
    sreturn = replaceAll(sreturn, "mym@0@mym", "+");
    sreturn = replaceAll(sreturn, "mym@9@mym", "=");
    sreturn = replaceAll(sreturn, "mym@7@mym", "/");
    sreturn = replaceAll(sreturn, "mym@6@mym", "\\");
    sreturn = replaceAll(sreturn, "mym@4@mym", "\"");
    sreturn = replaceAll(sreturn, "mym@8@mym", "<");
    sreturn = replaceAll(sreturn, "mym@3@mym", ">");
    sreturn = replaceAll(sreturn, "mym@2@mym", "#");
    sreturn = replaceAll(sreturn, "mym@-@mym", "'");
    return sreturn;
}

function getDDlValue(selectObject){
	var cursel = selectObject.selectedIndex;
	if(cursel == undefined){
	    return "";
	}
	var selectValue = selectObject.options[cursel].value;
	selectValue=encodeURI(selectValue);
	if (selectValue == "defaultValue"){
		selectValue = "";
	}
	return selectValue;
	
}
function setDDlValue(selectObject, values){
	values = decodeURI(values);
	for (var i=0; i<selectObject.options.length; i++){
		if (selectObject.options[i].value.Trim() == String(values).Trim()){
			selectObject.selectedIndex = i;
			return i;
		}
	}
	return null;
}
function clearDDlValue(selectObject){
	while (selectObject.options.length != 0){
	    selectObject.remove(0);
	}
}
//sOption 格式为 1(')课程|'|2(')学习|'|
function createDDlFromString(sOption, oSelect){
    var select;
    if (oSelect){
        select = oSelect;
        clearDDlValue(select);
    }else{
        select = document.createElement("select");
    }
    var arrOption = sOption.split("|'|");
    for (var i=0; i<arrOption.length; i++){
        var value = arrOption[i].split("(')")[0];
        var text = arrOption[i].split("(')")[1];
        var option = document.createElement("option");
        option.value = value;
        option.text = text;
        select.options.add(option);
    }
    
    return select;
}
//将selectT 中的 options 置换到 selectY 中
function replaceDDlOption(selectY, selectT){
    clearDDlValue(selectY);
    for (var i=0; i<selectT.options.length; i++){
        var option = document.createElement("option");
        option.value = selectT.options[i].value;
        option.text = selectT.options[i].text;
        selectY.options.add(option);
    }
}
function requestBool(value){
	if (request(value) != null && request(value) != ""){
		return true;	
	}
	return false;	
}
function flashObject(movieName) {
    if (ie) {
        return document[movieName]
    }
    else {
        return document[movieName]
    }
}
function objectDivSwatchHeight(main, left, right){
	var div_main = document.getElementById(main);
	var div_left = document.getElementById(left);
	var div_right = document.getElementById(right);
	setInterval(function (){
		if (parseInt(div_left.style.height) != div_main.scrollHeight){
			div_left.style.height = div_main.scrollHeight + "px";
		}
		if (parseInt(div_right.style.height) != div_main.scrollHeight){
			div_right.style.height = div_main.scrollHeight + "px";
		}
	}, 10);
}
//objectDivSwatchHeight("div_main","div_left","div_right");启动说明 // 记得加<div style="clear: both;"></div>

//对象必须有width height属性
function setScreenCenter(object){
    if (object.style.width && object.style.height){
        var screenH = parseInt(document.documentElement.clientHeight);
	    var screenW = parseInt(document.documentElement.clientWidth);
	    var nowleft = (screenW-parseInt(object.style.width))/2 + document.documentElement.scrollLeft;
	    var nowtop = (screenH-parseInt(object.style.height))/3 + document.documentElement.scrollTop;;
	    if (nowleft<0){
		    object.style.left = "0px";
	    }else{
		    object.style.left = nowleft + "px";
	    }
	    if (nowtop< 0){
		    object.style.top = "0px";
	    }else{
		    object.style.top = nowtop + "px";
	    }
	}else{
	    alert("对象缺乏width或height属性");
	}
}
//日期比较 返回两个时间月之间的差
function dateMonthBetween(date1, date2){
    var all1 = date1.getFullYear() * 12 + date1.getMonth() + 1;
    var all2 = date2.getFullYear() * 12 + date2.getMonth() + 1;
    return (all1-all2);
}
//返回子项在数组中的位置
function getArrayI(value, array){
    for(var i in array){
       if(array[i]==value){return i;break;}
    }
}
function setHomepage(surl)
{
    if (document.all)
    {
        document.body.style.behavior='url(#default#homepage)';
        document.body.setHomePage(surl);

    }
    else if (window.sidebar)
    {
        if(window.netscape)
        {
            try
            {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e)
            {
                alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");
            }
        }
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
        prefs.setCharPref('browser.startup.homepage',surl);
    }
}

function addThisPage(surl, txt) {
    if (document.all)
        window.external.AddFavorite(surl, txt);
    else if (window.sidebar)
        window.sidebar.addPanel(txt, surl, "")
}

//校验日期
function isReg(s, patrn) {
    //格式 /表达式/
    if (!patrn.exec(s)){
        return false
    }else{
        return true
    }
}
//获取上级对象 tagName 标签名 需小写
function getParentNode(node, tagName, className){
    var parent = node.parentNode;
    while(parent.tagName.toLowerCase() != "body"){
        if (className){
            if (parent.tagName.toLowerCase() == tagName && parent.className == className){
                return parent;
            }
        }else{
            if (parent.tagName.toLowerCase() == tagName){
                return parent;
            }
        }
        parent = parent.parentNode;
    }
    return null;
}
