﻿// 全局通用函数
/*
页可见区域宽： document.body.clientWidth
网页可见区域高： document.body.clientHeight
网页可见区域宽： document.body.offsetWidth (包括边线的宽)
网页可见区域高： document.body.offsetHeight (包括边线的高)
网页正文全文宽： document.body.scrollWidth
网页正文全文高： document.body.scrollHeight
网页被卷去的高： document.body.scrollTop
网页被卷去的左： document.body.scrollLeft
网页正文部分上： window.screenTop
网页正文部分左： window.screenLeft
屏幕分辨率的高： window.screen.height
屏幕分辨率的宽： window.screen.width
屏幕可用工作区高度： window.screen.availHeight
屏幕可用工作区宽度： window.screen.availWidth 

*/
var host = "http://www.rushangdao.com/";

//引号转义 replace(/\"/g,'&quot;')

//清空file控件的值 
function clearFile(fileId)
{
    document.getElementById(fileId).focus(); 
    document.execCommand("selectall"); 
    document.execCommand("Delete"); 
}

$(document).ready(function(){
    $(document.body).append("<link rel=\"shortcut icon\" href=\"http://www.rushangdao.com/image/favicon.ico\" type=\"image/x-icon\" /> ");
})

//快捷获得HTML元素
function _$( name ) 
{
    if (document.getElementById)
        return document.getElementById(name);
    else if (document.all)
        return document.all[name];
    else
        return null;
}

///调用类构造函数
///target:类对象
function _InitElement( target )
{
    if( target !== null )
        target._initElement();    
}

/**
 * 事件回调全局通用函数(最多支持5个参数)
 * @param {String} callback 被调用函数指针(函数型)
 */
//用于:
//  1.触发指定对象的指定方法 callback为宿主事件实现函数
//  2.调用基类的指定方法     callback为保存的基类方2法
function _DoCallBack( callback )
{
    if( callback != null )
    {
        return callback( arguments[1], arguments[2], arguments[3], arguments[4], arguments[5] );   
    }
}

//DOM元素创建
function _CrtEle( type )
{
    return document.createElement( type );
}

//将HTML/Text字串转换成String字串
function _makeHtmlToString( strText )
{
    strText = strText.replace( /^\s+/, "" );
    strText = strText.replace( /\s+$/, "" );
    strText = strText.replace( /&lt;/g, "<" );
    strText = strText.replace( /&gt;/g, ">" );
    strText = strText.replace( /&nbsp;/g, " " );
    
    return strText;
}

//将String字串转换成HTML/Text字串
function makeStringToHtml( strString )
{
    strString = strString.replace( /\s+/g, "&nbsp;" );
    strString = strString.replace( /</g,   "&lt;"   );
    strString = strString.replace( />/g,   "&gt;"   );
   
    return strString;
}

//event 
// ie-srcElement ff-target
function getEvent() 
{   
    return window.event || arguments.callee.caller.arguments[0];
}
//获取事件源对象的绝对位置左,右
function GetPosition()
{
    var e = getEvent().srcElement || getEvent().target;
    var x = e.offsetLeft,y = e.offsetTop;
    while(e = e.offsetParent) 
    { 
        x += e.offsetLeft;   
        y += e.offsetTop;
    }
    ar = new Array(x, y);
    return ar;
}
//--返回IE高宽,显示器宽高
function getPageSize(){
var xScroll,yScroll;

if (window.innerHeight && window.scrollMaxY){
xScroll = document.body.scrollWidth;
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){
sScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else {
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}

var windowWidth,windowHeight;
//var pageHeight,pageWidth; 
if (self.innerHeight) {
windowWidth = self.innerWidth;
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) {
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) {
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}

var pageWidth,pageHeight
if(yScroll < windowHeight){
pageHeight = windowHeight;
} else {
pageHeight = yScroll;
}
if(xScroll < windowWidth) {
pageWidth = windowWidth;
} else {
pageWidth = xScroll;
}
arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
return arrayPageSize;
}


/*
 * 功能：键盘事件
 * 参数说明： params.keycode 键盘按键的keyCode值 ，params.callback 回调函数，params.data callback的参数
 * keyCode 说明：
    字母和数字键的键码值(keyCode)    
    A 65 J 74 S 83 1 49 
    B 66 K 75 T 84 2 50 
    C 67 L 76 U 85 3 51 
    D 68 M 77 V 86 4 52 
    E 69 N 78 W 87 5 53 
    F 70 O 79 X 88 6 54 
    G 71 P 80 Y 89 7 55 
    H 72 Q 81 Z 90 8 56 
    I 73 R 82 0 48 9 57 
    数字键盘上的键的键码值(keyCode) 功能键键码值(keyCode）
    0 96 8 104 F1 112 F7 118 
    1 97 9 105 F2 113 F8 119 
    2 98 * 106 F3 114 F9 120 
    3 99 + 107 F4 115 F10 121 
    4 100 Enter 108 F5 116 F11 122 
    5 101 - 109 F6 117 F12 123 
    6 102 . 110         
    7 103 / 111   
    控制键键码值(keyCode)
    BackSpace 8 Esc 27 Right Arrow 39 -_ 189 
    Tab 9 Spacebar 32 Dw Arrow 40 .> 190 
    Clear 12 Page Up 33 Insert 45 /? 191 
    Enter 13 Page Down 34 Delete 46 `~ 192 
    Shift 16 End 35 Num Lock 144 [{ 219 
    Control 17 Home 36 ;: 186 \| 220 
    Alt 18 Left Arrow 37 =+ 187 ]} 221 
    Cape Lock 20 Up Arrow 38 ,< 188 ’" 222 
 */
function _keyboardEvent( params )
{
    if(document.addEventListener) //ff
        document.addEventListener("keydown",_keydown,true);
    else 
        document.attachEvent("onkeydown",_keydown);  
        
    function _keydown(evt)
    {
        if(evt.keyCode == params.keycode)
        {              
            if(params.callback)
            {                 
                if(!params.data)
                    params.data = {};
                params.callback( params.data );
            }
            event.returnValue = false;
        }
    }            
}

/*
 *JS 的request值
 *调用方法:var abc = new QueryString();
 *         alert(abc.id);//id是假如参数名称中有这个变量名
 */
function QueryString()
{
    //构造参数对象并初始化 
    var name,value,i; 
    var str= location.href;//获得浏览器地址栏URL串 
    var num=str.indexOf("?"); 
    str=str.substr(num+1);//截取“?”后面的参数串 
    var arrtmp=str.split("&");//将各参数分离形成参数数组 
    for(i=0;i < arrtmp.length;i++)
    { 
        num=arrtmp[i].indexOf("="); 
        if(num>0)
        { 
            name=arrtmp[i].substring(0,num);//取得参数名称 
            value=arrtmp[i].substr(num+1);//取得参数值 
            this[name]=value;//定义对象属性并初始化 
            var json = "{ key:'"+name+"',value: '"+value+"' }";   
            this["_" + name]= eval( '(' + json + ')' );//定义对象属性并初始化 
       } 
    } 
}

/*
 *iframe自动高度
 *调用方法 iframe.onload = "_frameAutoHeight(this)"
 */
function _frameAutoHeight(This){
var bobo=This; //iframe id
if (document.getElementById){
if (bobo && !window.opera){
if (bobo.contentDocument && bobo.contentDocument.body.offsetHeight){
bobo.height = bobo.contentDocument.body.offsetHeight;
}else if(bobo.Document && bobo.Document.body.scrollHeight){
bobo.height = bobo.Document.body.scrollHeight;
}
}
}
}

//特殊字符判断
function _boolReg(str)
{
    return /^[\$|\%|\&|\#|\*|\~|\@|\^|\(|\)|\-|\+|\[|\]|\{|\}|\;|\:|\||\'|\"|\,|\<|\.|\>|\?|\/|\`]/.test(str);
}

//判断输入的是否是英文
function _isLetter(str)
{ 
    if(str.lenth ==0)
    { 
        return true; 
    } 
    for(var i=0;i<str.length;i++)
    {
        var c = str.charAt(i); 
        if((c<"a"||c>"z")&&(c<"A"||c>"Z"))
        { 
            return false; 
        } 
    } 
    return true; 
} 



function _changeQueryString(url,key,value)
{
    var index = url.indexOf(key);
    if(index > 0)
    {
        var lastUrl = url.substring(index, url.length);
        var endIndex = index　+ lastUrl.indexOf("&");
        if(lastUrl.indexOf("&") == -1) 
            endIndex = url.length;
        return url.replace(url.substring(index,endIndex),key + "=" + value);
    }
    else
    {
        if(url.indexOf("?") > 0)
        {
            return url + "&" + key + "=" + value;
        }
        else
        {
            return url + "?" + key + "=" + value;
        }
    }
}




//--随机值len:取几位以"-"隔开的数,len2:每个数长几位，例：_getRandCode(2,3)返回：***-***,(3,4)则返回：****-****-****
function _getRandCode(len,len2)
{
    var str="ABCDEFGHIGKLMNOPQRSTUVWXYZ1234567890" 
    function getRandomNum(lbound, ubound) 
    { 
        return (Math.floor(Math.random() * (ubound - lbound)) + lbound); 
    } 
    var s=str.split(""); 
    var t=""; 
    for (var j=0;j<len;j++)
    {
        for (var i=0;i<len2;i++)
        { 
            t+=s[getRandomNum(1,36)]; 
        }
        if (j != len -1)
            t +="-";
    }
    return t;
}
/*
* 方法:Array.baoremove(dx)
* 功能:删除数组元素.
* 参数:dx删除元素的下标.
* 返回:在原数组上修改数组.
*/

Array.prototype.myRemove = function(dx)
{
    if(isNaN(dx)||dx>this.length){return false;}
    this.splice(dx,1);
}



//截断字符串显示省略号 指定长度len
function espLen( str, len )
{
    if(str == undefined && len == undefined)
        throw "error";
    var espLen = str;
    if(str.length >= len)
        espLen = espLen.substr(0, len) + "...";
    return espLen;
}

//限制textarea的长度
var textarea_maxlen = {  
    isMax : function (id){
        var textarea =   document.getElementById(id);
        var max_length = textarea.maxLength;
        if(textarea.value.length > max_length){   
            textarea.value =   textarea.value.substring(0, max_length);
        }  
    },  
    disabledRightMouse:function (){
        document.oncontextmenu = function (){ return false; }   
    },  
    enabledRightMouse : function (){
        document.oncontextmenu = null;   
    }
};

//聚焦到字符串的末尾
function endfocus( id )
{
    var obj = document.getElementById(id);
    var text = obj.createTextRange();   
    text.collapse(false);   
    text.select();
}

function onfocus( obj )
{
    var text = obj.createTextRange();   
    text.collapse(false);   
    text.select();
}

//表格tr换位置
function _changeNode(tr1,tr2){ 
var target= (tr1.rowIndex == tr2.rowIndex + 1) ? tr2 : tr2.nextSibling;//(tr1.rowIndex<tr2.rowIndex)?tr2.nextSibling:tr2;    
var tBody=tr1.parentNode    
tBody.replaceChild(tr2,tr1);    
tBody.insertBefore(tr1,target); 
}

//区分IE6和IE7。如果是IE6则返回 6 IE7返回 7
function _GetIEVersion()
{
    if(navigator.appName.indexOf('Internet Explorer')>0)
    {
        temp=navigator.appVersion.split("MSIE");

        version=parseFloat(temp[1]);
       
    }
    return version;
}

//判断输入的字符长度
function _isLength( str )
{
    if(str.length > 1)
        return true;
    else
        return false;
}