﻿/*=========================
* 全站通用Javascript
=========================*/


//根据url后的参数，指定key，获取值，传入格式为Key1=Value1&Key2=Value2 ，前面不带问号
function SplitKeyValue(s, key) {
    var sValue = ("?" + s).match(new RegExp("[\?\&]" + key + "=([^\&]*)(\&?)", "i"))
    return sValue ? sValue[1] : sValue
}

//在targetEl前插入newEl DOM对象
function insertAfter(newEl, targetEl) {
    var parentEl = targetEl.parentNode;

    if (parentEl.lastChild == targetEl) {
        parentEl.appendChild(newEl);
    } else {
        parentEl.insertBefore(newEl, targetEl.nextSibling);
    }
}


//获取URL中指定参数的值
function Request(item) {
    var sValue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"))
    return sValue ? sValue[1] : sValue
}


//根据radio的name获取选中radio的值value
function getRadioBoxValue(radioName) {
    var obj = document.getElementsByName(radioName);
    for (i = 0; i < obj.length; i++) {

        if (obj[i].checked) {
            return obj[i].value;
        }
    }
    return "undefined";
}


//获取checkbox值
function getCheckedValue(radioObj) {
    if (!radioObj)
        return "";
    var radioLength = radioObj.length;
    if (radioLength == undefined)
        if (radioObj.checked)
        return radioObj.value;
    else
        return "";
    for (var i = 0; i < radioLength; i++) {
        if (radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}


//显示和隐藏指定ID行
function ShowHideRow(id, showIDPrefix, hideIDPrefix) {
    document.getElementById(hideIDPrefix + id).style.display = "none";
    document.getElementById(showIDPrefix + id).style.display = "";
}

//显示和隐藏ID容器
function ShowHideObject(id, isShow) {
    if (isShow)
        document.getElementById(id).style.display = "";
    else
        document.getElementById(id).style.display = "none";
}



//隐藏或显示指定ID对象
function ShowHideSwitch(id) {
    if (document.getElementById(id).style.display == "none")
    { document.getElementById(id).style.display = ""; }
    else
    { document.getElementById(id).style.display = "none"; }
}


//加入收藏
function AddFavorite(title, url) {
    if (document.all) {
        window.external.addFavorite(url, title);
    }
    else if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    }
}

//普通打开窗口
function OpenWindow(url, type) {
    switch (type) {
        case 1: //_top窗口
            window.open(url, "_top");
            break;
        case 2: //_self窗口
            window.open(url, "_self");
            break;
        case 3: //_blank窗口
            window.open(url, "_blank");
            break;
    }
}




//判断是否为url
function IsUrl(urlString) {
    regExp = /(http[s]?|ftp):\/\/[^\/\.\,\s\'\"\;]+?\.[^\,\s\'\"\;]+?[\w\/]$/i;
    if (urlString.match(regExp)) return true;
    else return false;
}



//从用逗号分隔的组合字符串中移除指定字符串
function RemoveStringFromStringCombination(s, deleteS) {
    var tempArray = s.split(",");
    var outArray = new Array();
    var j = 0;

    for (var i = 0; i < tempArray.length; i++) {
        if (tempArray[i] != deleteS) {
            outArray[j] = tempArray[i];
            j++;
        }
    }

    return outArray.join(",");
}



//检查指定字符串是否存在于用逗号分隔的组合字符串
function CheckIsStringExistsInStringCombination(s, checkS) {
    var tempArray = s.split(",");
	var result=false;

    for (var i = 0; i < tempArray.length; i++) {
        if (tempArray[i] == checkS) {
            result=true;
        }
    }
	return result;
}

//日期间隔
function dateDiff(interval, date1, date2) {
    var objInterval = { 'D': 1000 * 60 * 60 * 24, 'H': 1000 * 60 * 60, 'M': 1000 * 60, 'S': 1000, 'T': 1 };
    interval = interval.toUpperCase();
    var dt1 = Date.parse(date1.replace(/-/g, '/'));
    var dt2 = Date.parse(date2.replace(/-/g, '/'));
    try {
        return Math.round((dt2 - dt1) / eval('(objInterval.' + interval + ')'));
    }
    catch (e) {
        return e.message;
    }
}

//返回今天年-月-日
function getTodayDate() {
    var myDate = new Date();
    return myDate.getFullYear().toString() + "-" + (myDate.getMonth() + 1).toString() + "-" + myDate.getDate().toString();
}

//生成伪Guid，不带横线，32位
function GenerateSimGuid() {
    var guid = "";
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        //if((i==8)||(i==12)||(i==16)||(i==20))
        //  guid += "-";
    }
    return guid;
}

//切断文本
//s 原始文本
//lengthLimit 限制长度
//suffix 超过长度时使用的后缀
function CutText(s,lengthLimit,suffix) {
	if (s=="")
	{
		return "";
	}
	if (s.length>lengthLimit)
	{
		return s.substring(0,lengthLimit) + suffix;
	}
	else
	{
		return s;	
	}
}

//去除最后一个字符
function CutLastChar(s) {
	if (s=="")
	{
		return "";
	}
	return s.substring(0,s.length-1)
}

//解析为绝对路径，支持~标记，需要先引用含全局变量定义的js文件
function ResolveUrl(s) {
	return s.replace("~",GLOBAL_RootPath);
}

//删除左右两端的空格 
function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, ""); 
}
//删除左边的空格 
function ltrim(str){
	return str.replace(/(^\s*)/g,""); 
}
//删除右边的空格 
function rtrim(str){
	return str.replace(/(\s*$)/g,""); 
}

//跳转到锚点
function JumpToAnchor(anchorName) {
  location = "#" + anchorName;
}


//秒转换为时分秒
function SecondsTohhmmss(seconds){
   var hh;
   var mm;
   var ss;
   //传入的时间为空或小于0
   if(seconds==null||seconds<0){
       return;
   }
   //得到小时
   hh=seconds/3600|0;
   seconds=parseInt(seconds)-hh*3600;
   if(parseInt(hh)<10){
          hh="0"+hh;
   }
   //得到分
   mm=seconds/60|0;
   //得到秒
   ss=parseInt(seconds)-mm*60;
   if(parseInt(mm)<10){
         mm="0"+mm;    
   }
   if(ss<10){
       ss="0"+ss;      
   }
   return hh+":"+mm+":"+ss;
}



//显示IP来源
function ShowIPLocation(ip,ipAddressSpanID,ipLocationSpanID) {
        setTimeout(function () {
            if (trim($('#'+ipLocationSpanID).text()) == "") { $('#'+ipLocationSpanID).html('抱歉，暂时无法查询，请稍后再试。'); }
        }, 10000);

        $(function () {
            if (ip == "") {
                $('#'+ipAddressSpanID).text("IP信息错误");
                $('#'+ipLocationSpanID).html('/');
                return;
            }

            $('#'+ipAddressSpanID).text(ip);
            var url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=" + ip;
            $.ajax({
                type: "GET",
                url: url,
                timeout: 15000,
                dataType: "script",
                success: function () {
                    if (remote_ip_info != undefined && remote_ip_info.ret == '1') {
                        var country = remote_ip_info.country;
                        var province = remote_ip_info.province;
                        var city = remote_ip_info.city;
                        var isp = remote_ip_info.isp;
                        var ipLocation = "";
                        if (country != "")
                            ipLocation += country;
                        if (province != "")
                            ipLocation += " - " + province;
                        if (city != "")
                            ipLocation += " - " + city;
                        if (isp != "")
                            ipLocation += " （" + isp + "）";

                        $('#'+ipLocationSpanID).text(ipLocation);
                    }
                },
                error: function () { $('#'+ipLocationSpanID).html('抱歉，暂时无法查询，请稍后再试。'); }
            });
        });
}



