
/*****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****
 ** 
 **字符串处理函数
 ** htmlspecialchars 过滤客户端特殊的字符 &"'<>

 *****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****/
function htmlspecialchars(text) {
	if(typeof(text)=='undefined'||!text.toString ) {
		return '';
	}
   return text.toString ().replace(/&/g,'&amp;').replace(/"/g,'&quot;').replace(/'/g,'&#039;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}

function escape_js_quotes(text) {
	if(typeof(text)=='undefined'||!text.toString ) {
		return '';
	}
    return text.toString ().replace(/\\/g,'\\\\').replace(/\n/g,'\\n').replace(/\r/g,'\\r').replace(/"/g,'\\x22').replace(/'/g,'\\\'').replace(/</g,'\\x3c').replace(/>/g,'\\x3e').replace(/&/g,'\\x26');
}
function trim(text) {
	if(typeof(text)=='undefined'||!text.toString ) {
		return '';
	}
	return text.toString ().replace(/^\s*|\s*$/g,'');
}
function nltobr(text) {
	if(typeof(text)=='undefined'||!text.toString ) {
		return ''; 
	}
	return text.toString ().replace(/\n/g,'<br />');
}
function escapeURI(u)
{
    if(encodeURIComponent){return encodeURIComponent(u);}
    if(escape){return escape(u);}
}
function goURI(href){window.location.href=href;}



function ge()
{
   var ea;
   for(var i = 0; i < arguments.length; i ++ )
   {
      var e = arguments[i];
      if(typeof e == 'string')
      e = document.getElementById(e);
      if(arguments.length == 1)
      {
         return e;
      }
      if( ! ea)
      ea = new Array();
      ea[ea.length] = e;
   }
   return ea;
}

//得到最新的验证码
function getimgcode(){
    var numkey = Math.random();
    numkey = Math.round(numkey*10000);

    var getimagecode = ge("getcode");
    getimagecode.src = "/inc/Image.aspx?k=" + numkey;
}


String.prototype.trim = function()
{
	// 用正则表达式将前后空格
	// 用空字符串替代。
	return this.replace(/(^\s*)|(\s*$)/g, "");
}


/**
 * 统计字符串字节数
 * return	integer
 */
String.prototype.ByteCount = function()
{
	txt = this.replace(/(<.*?>)/ig,'');
	txt = txt.replace(/([\u0391-\uFFE5])/ig, '11');
	var count = txt.length;
	return count;
}






function remove_node(node)
{if(node.removeNode)
node.removeNode(true);else{for(var i=node.childNodes.length-1;i>=0;i--)
remove_node(node.childNodes[i]);node.parentNode.removeChild(node);}
return null;}


function elementX(obj)
{
   var curleft = 0;
   if(obj.offsetParent)
   {
      while(obj.offsetParent)
      {
         curleft += obj.offsetLeft;
         obj = obj.offsetParent;
      }
   }
   else if(obj.x)
   curleft += obj.x;
   return curleft;
}
function elementY(obj)
{
   var curtop = 0;
   if(obj.offsetParent)
   {
      while(obj.offsetParent)
      {
         curtop += obj.offsetTop;
         obj = obj.offsetParent;
      }
   }
   else if(obj.y)
   curtop += obj.y;
   return curtop;
}


function set_inner_html(obj, html)
{
   obj.innerHTML = html;
   var scripts = obj.getElementsByTagName('script');
   for(var i = 0; i < scripts.length; i ++ )
   {
      if(scripts[i].src)
      {
         var script = document.createElement('script');
         script.type = 'text/javascript';
         script.src = scripts[i].src;
         document.body.appendChild(script);
      }
      else
      {
         eval(scripts[i].innerHTML);
      }
   }
}


function set_opacity(obj, opacity)
{
   try
   {
      obj.style.opacity = (opacity == 1 ? '' : opacity);
      obj.style.filter = (opacity == 1 ? '' : 'alpha(opacity=' + opacity * 100 + ')');
   }
   catch(e)
   {
   }
   obj.setAttribute('opacity', opacity);
}
function get_opacity(obj)
{
   return obj.opacity ? obj.opacity : 1;
}

function getTableRowShownDisplayProperty()
{
   if(ua.ie())
   {
      return'inline';
   }
   else
   {
      return'table-row';
   }
}
function showTableRow()
{
   for(var i = 0; i < arguments.length; i ++ )
   {
      var element = ge(arguments[i]);
      if(element && element.style)element.style.display = getTableRowShownDisplayProperty();
   }
   return false;
}

function hide()
{
   for(var i = 0; i < arguments.length; i ++ )
   {
      var element = ge(arguments[i]);
      if(element && element.style)element.style.display = 'none';
   }
   return false;
}
function shown(el)
{
   el = ge(el);
   return(el.style.display != 'none');
}


function array_indexOf(arr, val, index)
{
   if( ! index)
   {
      index = 0;
   }
   for(var i = index; i < arr.length; i ++ )
   {
      if(arr[i] == val)
      {
         return i;
      }
   }
   return - 1;
}
var ua =
{
   populate : function()
   {
      var agent = /(?:MSIE.(\d+\.\d+))|(?:Firefox.(\d+\.\d+))|(?:Opera.(\d+\.\d+))|(?:AppleWebKit.(\d+.\d+))/.exec(navigator.userAgent);
      if( ! agent)
      {
         this._ie = this._firefox = this._opera = this._safari = 0;
      }
      this._ie = parseFloat(agent[1] ? agent[1] : 0);
      this._firefox = parseFloat(agent[2] ? agent[2] : 0);
      this._opera = parseFloat(agent[3] ? agent[3] : 0);
      this._safari = parseFloat(agent[4] ? agent[4] : 0);
      this.populated = true;
   }
   , populated : false, ie : function()
   {
      if( ! this.populated)this.populate();
      return this._ie;
   }
   , firefox : function()
   {
      if( ! this.populated)this.populate();
      return this._firefox;
   }
   , opera : function()
   {
      if( ! this.populated)this.populate();
      return this._opera;
   }
   , safari : function()
   {
      if( ! this.populated)this.populate();
      return this._safari;
   }
}
Function.prototype.extend = function(superclass)
{
   var superprototype = __metaprototype(superclass, 0);
   var subprototype = __metaprototype(this, superprototype.prototype.__level + 1);
   subprototype.parent = superprototype;
}
function __metaprototype(obj, level)
{
   if(obj.__metaprototype)
   {
      return obj.__metaprototype;
   }
   var metaprototype = new Function();
   metaprototype.construct = __metaprototype_construct;
   metaprototype.prototype.construct = __metaprototype_wrap(obj, level);
   metaprototype.prototype.__level = level;
   metaprototype.base = obj;
   obj.prototype.parent = metaprototype;
   obj.__metaprototype = metaprototype;
   return metaprototype;
}
function __metaprototype_construct(instance)
{
   __metaprototype_init(instance.parent);
   var parents = [];
   var obj = instance;
   while(obj.parent)
   {
      parents.push(new_obj = new obj.parent());
      new_obj.__instance = instance;
      obj = obj.parent;
   }
   instance.parent = parents[1];
   parents.reverse();
   parents.pop();
   instance.__parents = parents;
   instance.__instance = instance;
   var args = [];
   for(var i = 1; i < arguments.length; i ++ )
   {
      args.push(arguments[i]);
   }
   return instance.parent.construct.apply(instance.parent, args);
}
function __metaprototype_init(metaprototype)
{
   if(metaprototype.initialized)return;
   var base = metaprototype.base.prototype;
   if(metaprototype.parent)
   {
      __metaprototype_init(metaprototype.parent);
      var parent_prototype = metaprototype.parent.prototype;
      for(i in parent_prototype)
      {
         if(i != '__level' && i != 'construct' && base[i] === undefined)
         {
            base[i] = metaprototype.prototype[i] = parent_prototype[i]
         }
      }
   }
   metaprototype.initialized = true;
   var level = metaprototype.prototype.__level;
   for(i in base)
   {
      if(i != 'parent')
      {
         base[i] = metaprototype.prototype[i] = __metaprototype_wrap(base[i], level);
      }
   }
}
function __metaprototype_wrap(method, level)
{
   if(typeof method != 'function' || method.__prototyped)
   {
      return method;
   }
   var func = function()
   {
      var instance = this.__instance;
      if(instance)
      {
         var old_parent = instance.parent;
         instance.parent = level ? instance.__parents[level - 1] : null;
         var ret = method.apply(instance, arguments);
         instance.parent = old_parent;
         return ret;
      }
      else
      {
         return method.apply(this, arguments);
      }
   }
   func.__prototyped = true;
   return func;
}
Function.prototype.bind = function(context)
{
   var method = this;
   return function()
   {
      return method.apply(context, arguments);
   }
}



/*****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****
 ** 
 **得到动态加载等待的图标和传入的文本
 ** 
 *****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****/
function getloadingtext(objhtml)
{
    return '<img src="/images/loading.gif" /> '+objhtml;
};



/*****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****
 ** 
 **显示右上角的操作提示  isshow true 显示 false 隐藏 objhtml 显示的内容
 ** 
 *****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****/
function showloading(isshow,objhtml)
{
//var  dialog = new pop_dialog('pop_dialog');
//    dialog.show_prompt('ddd','ffff');
   var  obj=document.getElementById('showloading');
    if(obj==null)
   { 
       obj = document.createElement('div');
       obj.id = 'showloading';          //这个我觉的重复跟第一句(SMP)
       document.body.appendChild(obj); 
      $('#showloading').addClass("ShowLoading"); 
   } 
   if (objhtml=='undefined'||objhtml==''||objhtml==undefined) {
        obj.innerHTML='Loading . . .'; 
   }
   else
   {
        obj.innerHTML=objhtml; 
   } 
   obj.style.top = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)+'px';
   obj.style.right=0;
   if (isshow) {
        obj.style.display = 'block';
        clock=setTimeout("showloading(true,'"+objhtml+"');",1)   //这句我objhtml如果是showloading(true,'');那objhtml不是为空的???(SMP)

   }
   else
   {
        obj.style.display = 'none';
        obj=null;
        clearTimeout(clock);
   }

};


/*****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****
 ** 
 ** 显示信息提示框
 ** 
 *****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****/
function showdivmessage(objhtml)
{
       if (objhtml=='undefined'||objhtml=='') {
            $('#divmessages').hide();
       }
       else
       {
            $('#divmessages').show(); 
            $('#divmessages').html('<p>'+objhtml+'</p>');
       } 
}



/*****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****
 ** 
 ** 主要处理AJAX操作中用户状态实效的问题 do by lqs  
 ** 
 *****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****/
function checkloginstatus()
{
    $.ajaxSettings.async=false;
   var re=false; 
   koollege.homepage.CheckLoginStatus(function(res)
                    {
                            $.ajaxSettings.async=true;
                           if (res=='fail') {
                                var  dialog = new pop_dialog('pop_dialog');
                                dialog.show_choice("It requires another authentication again. Please log in again?", "OK", function(){comm_dialog.get_dialog(this).fade_out(100);LoginAgain()}, "Cancel", function()
                               {
                                  comm_dialog.get_dialog(this).fade_out(100);
                                
                               });         
                                function LoginAgain()
                                {
                                      //document.location.href = "/passport/login.aspx?FromUrl="+escape(document.location.href);
                                      //escapeURI
                                     goURI("/passport/login.aspx?FromUrl="+escapeURI(document.location.href));
                                };   
                             re= false;      
                           }   
                           else
                           {
                            re= true;
                           }
                    });
   return re;
}


/*****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****
 ** 
 **  截取字符串
 ** 
 *****^*****^*****^*****^*****^*****^*****^*****^*****^*****^*****/
function substr(str, len) {
   if(!str || !len) {
      return '';     
   }
   var strtemp=str, a = 0 , i = 0 , temp = '';
   for (i=0;i<str.length;i++)
   {
      if (str.charCodeAt(i)>255)
      {   
         a+=2;
      }
      else
      {
         a++;
      } 
      if(a > len-3) {
         return temp+"...";
      } 
      temp += str.charAt(i);
   }
   return str;
}



function showsubmenu(ss,ii,openimg,closeimg)
{
    var menuobjedt=document.getElementById(ss);
    if (menuobjedt)
    {
	     if (menuobjedt.style.display=="none") 
		    {menuobjedt.style.display="";
	       document.getElementById(ii).src="/images/open.gif";
	       document.getElementById(ii).title="";
		    }
	     else
	      {menuobjedt.style.display="none"; 
		    document.getElementById(ii).src="/images/close.gif";
		    document.getElementById(ii).title="";
	       }
    }


    for (var i=0;i<12;i++)
     {
        if (('submenu'+i)!=ss) {
            var menuobjedttemp=document.getElementById('submenu'+i);
            if (menuobjedttemp)
            {
                    if (menuobjedttemp.style.display!="none") 
		           {
                        menuobjedttemp.style.display="none"; 
                    }
                    document.getElementById('subimg'+i).src="/images/close.gif";
             }
        }  
    }
}

