﻿// JScript 文件
Number.prototype.NaN0=function(){return isNaN(this)?0:this;}
//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, "");
}
var nowaddStyle;
function addGlobalStyle(css) {
    var head;
    head = document.getElementsByTagName("head")[0];
    if (!head) { return; }
    nowaddStyle = document.createElement("style");
    nowaddStyle.type = 'text/css';
    nowaddStyle.innerHTML = css;
    head.appendChild(nowaddStyle);
}
//判断是否是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; 
}
function pageNoSelect(bool){
	if (bool){
		if (ie){
			document.body.onselectstart = function(){return false};
		}else{
			addGlobalStyle('body { -moz-user-select : none !important; }');
		}
	}
	else
	{
		if (ie){
			document.body.onselectstart = function(){return true};
		}else{
			nowaddStyle.innerHTML = "";
		}
	}
}
//格式化时间
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 + "=" + encodeURIComponent(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 decodeURIComponent(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 mousePos(ev){
    ev=ev || window.event
    if(ev.pageX || ev.pageY){
        return {x:ev.pageX, y:ev.pageY};
    }
    return {
        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y:ev.clientY + document.body.scrollTop - document.body.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.body.scrollLeft - document.body.clientLeft,
        y:ev.layerY + document.body.scrollTop - document.body.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, "?", "my@*@my");
    sreturn = replaceAll(sreturn, "&", "my@@@my");
    sreturn = replaceAll(sreturn, "&amp;", "my@@1@my");
    sreturn = replaceAll(sreturn, "%", "my@5@my");
    sreturn = replaceAll(sreturn, "+", "my@0@my");
    sreturn = replaceAll(sreturn, "=", "my@9@my");
    sreturn = replaceAll(sreturn, "/", "my@7@my");
    sreturn = replaceAll(sreturn, "\\", "my@6@my");
    return sreturn;
}
///   <summary>
///   将URL地址解码
///   </summary>
function decodeUrl(text)
{
    var sreturn = text;
    sreturn = replaceAll(sreturn, "my@*@my", "?");
    sreturn = replaceAll(sreturn, "my@@@my", "&");
	sreturn = replaceAll(sreturn, "my@@1@my", "&amp;");
    sreturn = replaceAll(sreturn, "my@5@my", "%");
    sreturn = replaceAll(sreturn, "my@0@my", "+");
    sreturn = replaceAll(sreturn, "my@9@my", "=");
    sreturn = replaceAll(sreturn, "my@7@my", "/");
    sreturn = replaceAll(sreturn, "my@6@my", "\\");
    return sreturn;
}

function getDDlValue(selectObject){
	var cursel = selectObject.selectedIndex;
	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 == String(values)){
			selectObject.selectedIndex = 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
    }
}
function copy_clip(meintext, message)
{
    if (window.clipboardData) 
    {
        window.clipboardData.setData("Text", meintext);
    }
    else if (window.netscape) 
    {
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
        var clip = Components.classes['@mozilla.org/widget/clipboard;1']
                  .createInstance(Components.interfaces.nsIClipboard);
        if (!clip) return;
        var trans = Components.classes['@mozilla.org/widget/transferable;1']
                   .createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;
        trans.addDataFlavor('text/unicode');
        var str = new Object();
        var len = new Object();
        var str = Components.classes["@mozilla.org/supports-string;1"]
                 .createInstance(Components.interfaces.nsISupportsString);
        var copytext=meintext;
        str.data=copytext;
        trans.setTransferData("text/unicode",str,copytext.length*2);
        var clipid=Components.interfaces.nsIClipboard;
        if (!clip) return false;
        clip.setData(trans,null,clipid.kGlobalClipboard);
    }
    alert(message);
    return false;
}

