﻿// JScript 文件

function cls(){ 
//捕获触发事件的对象，并设置为以下语句的默认对象 
with(event.srcElement) 
    //如果当前值为默认值，则清空 
if(value==defaultValue) value="" 
} 

function res(){ 
//捕获触发事件的对象，并设置为以下语句的默认对象 
with(event.srcElement) 
//如果当前值为空，则重置为默认值 
if(value=="") value=defaultValue 
} 

//弹出HTML编辑器
function eWebEditorPopUp(form, field, width, height) {
	window.open("/Tools/WebEditor/popup.htm?style=popup&form="+form+"&field="+field, "", "width="+width+",height="+height+",toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no");
}

//在屏幕中间打开指定大小的窗口
function openwin(turl,wd,hd) {
   tt=(screen.height-hd)/2;
   ll=(screen.width-wd)/2;
   winn=window.open(turl,"","scrollbars=yes,resizable=yes,status=no,width="+wd+",height="+hd+",top="+tt+",left="+ll);
   winn.focus();
}

//插入Flash
function insertFlash(elm, url, w, h) 
{
if (!document.getElementById(elm)) return;
var str = '';
str += '<object width="'+ w +'" height="'+ h +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0">';
str += '<param name="movie" value="'+ url +'">';
str += '<param name="wmode" value="opaque">';
str += '<param name="quality" value="autohigh">';
str += '<embed width="'+ w +'" height="'+ h +'" src="'+ url +'" quality="autohigh" wmode="transparent" type="application/x-shockwave-flash" plugspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>';
str += '</object>';
document.getElementById(elm).innerHTML = str;
}

//选择全部复选框
function CheckAll(oCheckBox,form)  {
  for (var i=0;i<form.elements.length;i++)    {
    var e = form.elements[i];
    if (e.name != oCheckBox.name)       e.checked = oCheckBox.checked; 
   }
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//屏蔽回车键
function event_down()
{
if(event.keyCode==13)event.returnValue =false;
}

//在屏幕中间打开一个网页窗口
function OpenWindow(URL,winwidth,winheight){
   wintop=(screen.height-winheight)/2;
   winleft=(screen.width-winwidth)/2;
   winn=window.open(URL,"","scrollbars=yes,resizable=yes,status=no,width="+winwidth+",height="+winheight+",top="+wintop+",left="+winleft);
   winn.focus();
}

function myBrowser(){
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    document.write(userAgent);
    var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera ; //判断是否IE浏览器 
    var isFF = userAgent.indexOf("Firefox") > -1 ; //判断是否Firefox浏览器
    var isSafari = userAgent.indexOf("Safari") > -1 ; //判断是否Safari浏览器

    if(isIE){ 
       var IE5 = IE55 = IE6 = IE7 = IE8 = false;
       var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
       reIE.test(userAgent);
       var fIEVersion = parseFloat(RegExp["$1"]);

       IE55 = fIEVersion == 5.5 ;
       IE6 = fIEVersion == 6.0 ;
       IE7 = fIEVersion == 7.0 ;
       IE8 = fIEVersion == 8.0 ;
      
       if(IE55){ return "IE55"; }
       if(IE6){ return "IE6"; }
       if(IE7){ return "IE7"; }
       if(IE8){ return "IE8"; }
     }
    if(isFF){ return "FF"; }
    if(isOpera){ return "Opera"; }
}

//在屏幕中间打开指定大小的窗口
function showModalWin(url,wd,hd) {
    var tt=(screen.height-hd)/2;
    var ll=(screen.width-wd)/2;
    var rnd=Math.random();
    var paraRnd="rnd=" + rnd;
    if(url.indexOf('?')>-1)
      paraRnd = "&" + paraRnd
    else
      paraRnd = "?" + paraRnd
    if(navigator.userAgent.indexOf("MSIE")>0)
    {
        if(window.XMLHttpRequest)
        {
            hd -= 21+3+3;
            wd -= 6;
        }
       winn=window.showModalDialog(url + paraRnd,window,"scrollbars=yes;resizable=no;help=no;status=no;dialogHeight=" + hd +"px;dialogWidth=" + wd + "px");
    }
    else
       winn=window.open(url,"","scrollbars=yes,resizable=yes,status=no,width="+wd+",height="+hd+",top="+tt+",left="+ll);
    return winn;
}

//往新窗口中写入自适应窗口宽度的图片
function ShowImgInWin(imgurl)
{
   var strHtml = "<html><title>照片---中小学辅导网</title><body>";
   strHtml += "<table width=\"100%\" height=\"100%\"><tr><td style=\"text-align:center; vertical-align:middle;\"><img src=\"" + imgurl + "\" onload=\"if(this.width>document.body.clientWidth){this.width=document.body.clientWidth-10};\"></td></tr></table>";
   strHtml += "<style>body{margin:5px;text-align:center;}</style>";
   //strHtml += "<style>div img {width:expression(document.body.clientWidth>window.screen.availWidth?\"600\":\"auto\"); overflow:hidden;}</style>";
   strHtml += "</body></html>";
   var imgWin = window.open("","newWindow","scrollbars=yes,toolbar=no,status=no,resizeable=no");
   imgWin.document.write(strHtml);  //   写html文本   
}

function closeWindow()
{
    window.opener=null;window.open('','_self');window.close();
}


//格式化字符串
String.format = function() {
    if (arguments.length == 0) {
        return null;
    }
    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}

//输出固定长度的字符串
function substring(input, len) {
    if (input.length <= len)
        return input;
    else
        return input.substr(0, len) + "...";
}

//设置控件焦点进入时隐藏,移出时显示控件的初始值(需Jquery库支持)
function toggleControlValue(control, initValue) {
    if (initValue == null) initValue = control.val();
    control.focus(function() {
        if ($(this).val() == initValue)
            $(this).val("");
    });

    control.blur(function() {
        if ($(this).val() == "")
            $(this).val(initValue);
    });
}

//显示确认对话框    
function showConfirmDialog(title, content, callback) {
    var handle = new SNS.sys.Popup({
        width: 450,
        // 对话框宽度
        title: title,
        // 模态框标题
        hideMask: false,
        // 是否显示遮层
        content: content,
        // 模态框文案，支持 HTML
        type: 'error',
        // 模块框额外样式
        focus: 1,
        // 焦点的按钮位置
        //buttons: [{text: "确认", func: function() { callback;}}
        buttons: [{ text: "确定", func: callback }
	    , { text: "取消", func: function() { this.hide(); } }
	    ],
        autoShow: false,
        // 是否立即弹出
        useAnim: true,
        // 是否显示动画
        onShow: function() { }
	    ,
        onHide: function() { }
    }
    );
    handle.show();
}

///显示提示对话框
function showMessageDialog(content) {
    new SNS.sys.Popup({
        width: 450, // 对话框宽度
        title: '提示信息',  // 模态框标题
        hideMask: false, // 是否显示遮层
        content: '<span>' + content + '</span>', // 模态框文案,支持 HTML
        type: 'error', // 模块框额外样式
        focus: 1, // 焦点的按钮位置
        buttons: [
        { text: "确定", func: function() { this.hide(); } }
    ],
        autoShow: true, // 是否立即弹出
        useAnim: true, // 是否显示动画
        onShow: function() { },
        onHide: function() { }
    });
}

///显示提示对话框
function showMessageDialogAndGoUrl(title,content,width,url) {
    new SNS.sys.Popup({
        width: width, // 对话框宽度
        title: title,  // 模态框标题
        hideMask: false, // 是否显示遮层
        content: '<span>' + content + '</span>', // 模态框文案,支持 HTML
        type: 'error', // 模块框额外样式
        focus: 1, // 焦点的按钮位置
        buttons: [
        { text: "确定", func: function () {
            if (url.length > 0) {
                if (url == "-1")
                    history.go(-1);
                else
                    self.location = url;
            }
            this.hide();
        }
        }
    ],
        autoShow: true, // 是否立即弹出
        useAnim: true, // 是否显示动画
        onShow: function () { },
        onHide: function () { }
    });
}

Date.prototype.pattern = function(fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份         
        "d+": this.getDate(), //日         
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时         
        "H+": this.getHours(), //小时         
        "m+": this.getMinutes(), //分         
        "s+": this.getSeconds(), //秒         
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度         
        "S": this.getMilliseconds() //毫秒         
    };
    var week = {
        "0": "\u65e5",
        "1": "\u4e00",
        "2": "\u4e8c",
        "3": "\u4e09",
        "4": "\u56db",
        "5": "\u4e94",
        "6": "\u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

//获取url参数
function request(paras) {
    var url = location.href;
    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
    var paraObj = {}
    for (i = 0; j = paraString[i]; i++) {
        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
    }
    var returnValue = paraObj[paras.toLowerCase()];
    if (typeof (returnValue) == "undefined") {
        return "";
    } else {
        return returnValue;
    }
}

function createTipsScript(selector, method, msg, cssIcon, cssTips) {
    if (cssTips == "") cssTips = "sns-msg msg-full";
    var htmlInfo = String.format("<div class=\\\"{0}\\\"><p class=\\\"{1}\\\">{2}</p></div>", cssTips, cssIcon, msg);
    return String.format("{0}.{1}(\"{2}\");", selector, method, htmlInfo);
}
