﻿//window.onerror = function() { return true; }
String.prototype.trim = function(){
	return this.replace(/(^\s+)|(\s+$)/g, '');
}

function HttpUrl(){  
    return window.location.pathname + window.location.search;
}

function EscapeUrl(){
	return escape(HttpUrl());
}

function ReGobackUrl(url){
    return url.indexOf('?') == -1 ? '?url=' + EscapeUrl() : '&url=' + EscapeUrl();
}

function ReWriteUrl(url){
	window.location.href = url + ReGobackUrl(url);
}

function ReWriteHref(o){
	o.href = o.href + ReGobackUrl(o.href);
}

function ReWriteForm(f, u){
    f.method = 'GET';
    f.__VIEWSTATE.disabled = true;
    if(u){
        f.action = u;
    }
}


function Sure2Delete(url, msg){
	if(confirm(msg)){
	    window.location.href = url + '&url=' + EscapeUrl();
	}
}

function ValidateIsMoreThenLength(e, l, m){
    if(e.value.trim().length > l){
        alert(m);
        e.select();
        e.focus();
        
        return false;
    }
    
	return true;
}

function ValidateIsEqualTwoLength(e, l, h, m){
    if(e.value.trim().length != l && e.value.trim().length != h){
        alert(m);
        e.select();
        e.focus();
        
        return false;
    }
    
	return true;
}

function ValidateIsEqualLength(e, l, m){
    if(e.value.trim().length != l){
        alert(m);
        e.select();
        e.focus();
        
        return false;
    }
    
	return true;
}

function ValidateIsNotNull(e, m){
    if(e.value.trim().length == 0){
        alert(m);
        e.focus();
        
        return false;
    }
    
	return true;
}

function ValidateIsTinyMceNull(id, m){
    var e = tinyMCE.get(id);
    if(e.getContent().trim().length == 0){
        alert(m);
        e.focus();
        
        return false;
    }
    
	return true;
}

function ValidateIsZero(e, m){
    if(e.value == '0'){
        alert(m);
        e.focus();
        return false;
    }
    
	return true;
}

function ValidateIsZero2(e, m){
    if(e.value == '2'){
        alert(m);
        e.focus();
        return false;
    }
    
	return true;
}

function ValidateIsNumber(e, m){
    var p = /^-?\d+$/gi;
    if(!p.test(e.value)){
        alert(m);
        e.select();
        e.focus();
        return false;
    }
    
	return true;
}

function ValidateIsPlusCharNumber(e, m){
    var p = /^[\d]*$/gi;
    if(!p.test(e.value)){
        alert(m);
        e.select();
        e.focus();
        return false;
    }
    
	return true;
}

function ValidateIsTimeNumber(e, m){
    var p = /^[0-59]\d\:[0-59]\d$/gi;
    if(!p.test(e.value)){
        alert(m);
        e.select();
        e.focus();

        return false;
    }
    
	return true;
}

function ValidateIsFloat(e, m){
    var p = /^\d+\.\d+/gi;
    if(!p.test(e.value)){
        alert(m);
        e.select();
        e.focus();
        return false;
    }
    
	return true;
}

function ValidateIsPlusNumber(e, m){
    var p = /^[1-9]{1}[\d]*$/gi;
    if(!p.test(e.value)){
        alert(m);
        e.select();
        e.focus();
        return false;
    }
    
	return true;
}

//验证网址,空字符串则通过
function CheckeUrl(url,e){    
        var str=url;
        if(str.value.trim().length == 0){      
            return true;
        }else{
            var Expression=/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/; 
            var objExp=new RegExp(Expression);
            if(objExp.test(str.value)==true){
                return true;
            }else{
                alert(e);
                return false;
            }
        }
} 

function ValidateIsEmail(vEMail){
	var regInvalid=/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var regValid = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	
	return (!regInvalid.test(vEMail)&&regValid.test(vEMail));
}

function alertMessage(e, m){
    alert(m);
    if(e){
        e.select();
        e.focus();
    }
}

function DisplayNormalThumbnail(d, e, src, w, h){
    var obj = document.getElementById(e);
    if(obj.value.length > 0){
        var html = '<img src="'+ src + obj.value + '"';
        if(w){
            html += ' style="width: ' + w + ';';
            if(h){
                html += ' height: ' + h + ';';
            }
            html += '"';
        }
        html += ' />';
        
        var divThumbnail = document.getElementById(d);
        divThumbnail.style.visibility = 'visible';
        divThumbnail.innerHTML = html;
    }
}

function DisplayThumbnail(e, src, act){
    var hdval = document.getElementById(e);
    if(hdval.value.length > 0){
        var html = '<div style="position:absolute;"><input type="button" value="X" onclick="Sure2Delete(\''+ act +'\',\'确定删除吗?\')" /></div>';
        html += '<img src="'+ src + hdval.value +'" />';
        var divPic = document.getElementById('divThumbPic');       
        divPic.style.visibility = 'visible';
        divPic.innerHTML = html;
    }
}

/* display attachment */
function DisplayAttach(src, nam, divAttachment, downSrc, delSrc){
    if(src.length > 0){
        var pos =  src.lastIndexOf('.');        
        var extend = src.substring(pos + 1);
        var html = '<img src="/images/file/'+ extend +'.gif" align="absmiddle" /> ';
        html += '<a href="'+ downSrc +'">'+ nam +'</a> ';
        
        if(delSrc){
            html += '<img src="/images/del.gif" align="absmiddle" style="cursor:pointer;" onclick="Sure2Delete(\''+ delSrc +'\', \'确认删除？\')" />';
        }

        var att = document.getElementById(divAttachment);
        att.innerHTML = html;
    }
}

/*创建可拖动层*/
function Closes(){
    divWindow = document.getElementById('divWindow');
    divWindow.style.visibility = 'hidden';
    document.getElementById('frmWindow').style.width = '0px';
    document.getElementById('frmWindow').style.height = '0px';
    document.getElementById('divWindowMark').style.display = 'none';
}

var divWindow;
var px = 0;
var py = 0;
var begin = false;
var topDiv;

function CreateWindow(width, height, src, scroll) {
    divWindow = document.getElementById('divWindow');
    if (!divWindow) {
        var frmWindow = document.createElement('iframe');
        frmWindow.id = 'frmWindow';
        frmWindow.setAttribute('name', 'frmWindow', 0);
        frmWindow.className = 'frmWindow';
        frmWindow.frameBorder = '0px';

        /*创建可拖动嵌套头*/
        var divWindowMove = document.createElement('div');
        divWindowMove.id = 'divWindowMove';
        divWindowMove.className = 'divWindowMove';
        divWindowMove.style.width = (width - 17) + 'px';
        divWindowMove.style.height = '24px';

        /*创建可拖动嵌套关闭头*/
        var divWindowClose = document.createElement('div');
        divWindowClose.id = 'divWindowClose';
        divWindowClose.className = 'divWindowClose';
        if (document.all) {
            divWindowClose.attachEvent('onclick', function() { Closes() });
        } else {
            divWindowClose.addEventListener('click', function() { Closes() }, false);
        }
        aWindowClose = document.createElement('a');
        if (document.all) {
            aWindowClose.innerText = 'X';
        } else {
            aWindowClose.textContent = 'X';
        }
        aWindowClose.id = 'aWindowClose';
        aWindowClose.className = 'aWindowClose';
        divWindowClose.appendChild(aWindowClose);

        /*创建可拖动主层*/
        divWindow = document.createElement('div');
        divWindow.id = 'divWindow';
        divWindow.className = 'divWindow';
        divWindow.style.width = width + 'px';
        divWindow.style.height = height + 'px';
        divWindow.appendChild(divWindowMove);
        divWindow.appendChild(divWindowClose);
        divWindow.appendChild(frmWindow);
        window.document.body.appendChild(divWindow);

        /*创建Mark层*/
        divWindowMark = document.createElement('div');
        divWindowMark.id = 'divWindowMark';
        divWindowMark.className = 'divWindowMark';
        window.document.body.appendChild(divWindowMark);
    }
    
    /*初始化弹出层窗口位置*/
    var clientHeight = document.documentElement.clientHeight;
    var clientWidth = document.body.clientWidth;
    var left = (clientWidth - width) / 2;
    var top = document.documentElement.clientHeight;
    var topscroll = document.body.clientHeight;
    if (topscroll > top) {
        top = topscroll;
    }
    
    top = (top - height) / 2;
    if (top < 20) {
        top = 20;
    }
    
    divWindow.style.width = width + 'px';
    divWindow.style.height = height + 'px';
    divWindow.style.left = left + 'px';
    divWindow.style.top = top + 'px';

    var act = 'width=' + width + '&height=' + height;
    if(!scroll) scroll = 'auto';
    act += '&scroll=' + scroll;
    if(src.indexOf('?') > -1){
        src += '&' + act;
    }else{
        src += '?' + act;
    }
    document.getElementById('frmWindow').scrolling = scroll;
    document.getElementById('frmWindow').src = src;
    document.getElementById('frmWindow').style.width = (width - 2) + 'px';
    document.getElementById('frmWindow').style.height = (height - 25) + 'px';

    document.getElementById('divWindowMove').style.width = (width - 17) + 'px';

    divWindowMark.style.display = 'block';
    divWindowMark.style.width = document.body.clientWidth + 20;
    divWindowMark.style.height = document.body.clientHeight + 20;
    divWindow.style.display = 'block';
    divWindow.style.visibility = 'visible';

    try{
        new DragObj("divWindow", "divWindowMove");
    }catch(err){}
}
/*drag div end*/

function tokenKey(){
	var hex = new Array('0','1','2','3','4','5','6','7','8', '9','a','b','c','d','e','f');
	var t = '';
	for(var i = 0; i<32; i++){
		t += hex[Math.floor(Math.random() * 16)];
	}
	return t.toUpperCase();
}

function ReloadValicode(o){
    o.src = '/GetValiCode.aspx?' + tokenKey();;
}

/*菜单选择*/
function OnMenuChkClicked(o){
	var subMenu = document.getElementsByName('cbSubMenu' + o.value);
	var len = subMenu.length;
	for(var i = 0; i < len; ++i){
		subMenu[i].checked = o.checked;
	}
}

function OnSubmenuChkClicked(o){
	var menuId = o.id.substr(9, 2);
	var subMenu = document.getElementsByName('cbSubMenu' + menuId);
	var len = subMenu.length;
	for(var i = 0; i < len; ++i){
		if(subMenu[i].id.length == 15 && subMenu[i].id.substr(0, 13) == o.id){
			subMenu[i].checked = o.checked;
		}
	}
}

function IsMemberHasLogin(){
    var memberId = HttpUrlCommand('/Js/MemberId.aspx?', null, null);
    if(memberId == '4492A54C3CFC1E57' || memberId.length > 16){
        alert('尚未登录，请先登录');
        
        return false;
    }
    
    return true;
}

var disabledSelectArray = null;
function DisableSelect(left, top, width, height){
	var x_bt = left + width;
	var y_bt = top + height;
	var sl_pos, sl_x_top, sl_y_top, sl_x_bt, sl_y_bt;
	var sls = document.getElementsByTagName('SELECT');
	disabledSelectArray = new Array();
	for(var i=0; i<sls.length; i++){
		sl_pos = AbsPos(sls[i]);
		sl_x_top = sl_pos.x;
		sl_y_top = sl_pos.y;
		sl_x_bt = sl_x_top + sls[i].clientWidth;
		sl_y_bt = sl_y_top + sls[i].clientHeight;
		
		if(((sl_x_bt > left) && (sl_x_top < x_bt)) && ((sl_y_bt > top) && (sl_y_top < y_bt))){
		    sls[i].disabled = true;
		    disabledSelectArray[disabledSelectArray.length] = sls[i];
		}
	}
}
function ShowMaskLayer(left, top, width, height){
	var divMaskLayer = document.getElementById('divMaskLayer');
    if(divMaskLayer == null){
        divMaskLayer = document.createElement("div");
        divMaskLayer.id = 'divMaskLayer';
        divMaskLayer.className = 'divMaskLayer';
        window.document.body.appendChild(divMaskLayer);
    }
	divMaskLayer.style.left = left +'px';
	divMaskLayer.style.top = top +'px';
	divMaskLayer.style.width = width +'px';
	divMaskLayer.style.height = height +'px';
	divMaskLayer.style.visibility = 'visible';
	
	DisableSelect(left, top, width, height);
}
function ResetMaskLayerPos(){
	var eHeight = document.documentElement.clientHeight;
	var bHeight = document.body.clientHeight;
	var clientWidth = document.body.clientWidth;
	var clientHeight = (eHeight > bHeight) ? eHeight : bHeight;
	ShowMaskLayer(0, 0, clientWidth, clientHeight);
}
function HideMaskLayer(){
	var divMask = document.getElementById('divMaskLayer');
	if(divMask){
	    divMask.style.left = '0px';
	    divMask.style.top = '0px';
	    divMask.style.width = '0px';
	    divMask.style.height = '0px';
	    divMask.style.visibility = 'hidden';
	}
	if(disabledSelectArray != null){
	    for(var i=0; i<disabledSelectArray.length; i++){
	        disabledSelectArray[i].disabled = false;
	    }
	    disabledSelectArray = null;
	}
}

function AbsPos(el){
	for(var lx=0,ly=0;el!=null;lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
	return {x:lx,y:ly}
}

//无刷新分页,异步方式
function AjaxPager(url){
    if(url.indexOf('?') == -1){
        url += '?';
    }
    
    var html = HttpUrlCommand(url, GetAjaxHtml, null);
	document.getElementById('divPager').innerHTML = '<center><img src="/images/loading.gif" /></center>';
}

function GetAjaxHtml(val){
    if(val != null){
        var tagBegin = '<!--divPagerBegin-->';
        var tagEnd = '<!--divPagerEnd-->';
        var tagBeginIndex = val.indexOf(tagBegin);
        var tagEndIndex = val.indexOf(tagEnd);
        if(tagEndIndex != -1 && tagBeginIndex != -1 && tagEndIndex > tagBeginIndex){
            var tagBeginLen = tagBegin.length;
            val = val.substr(tagBeginIndex + tagBeginLen, tagEndIndex - tagBeginIndex - tagBeginLen);
            document.getElementById('divPager').innerHTML = val;
        }else{
            document.getElementById('divPager').innerHTML = 'load data error........';
        }
    }
}

/*树形CheckBox*/
function TreeCheck(o) {
    var oid = o.id;
    if (!o.checked) {
        var idLen = oid.length;

        var chks = document.getElementsByName('cbTreeNode');
        var chksLen = chks.length;
        
        // 父级不选中子级不能选中
        for (var i = 0; i < chksLen; i++) {
            if (chks[i].id.indexOf(oid + '_') != -1) {
                chks[i].checked = o.checked;
            }
        }
    } else {
        //子级选中，父级必须选中
        var parentId = TreeParentId(oid);
        var parentCb;
        while (parentId != '') {
            parentCb = $(parentId);
            parentCb.checked = true;
            parentId = TreeParentId(parentId);
        }
    }
}

function TreeParentId(id) {
    var cb = $(id);
    var index = id.indexOf('_' + cb.value);
    if (index != -1) {
        return id.substr(0, index);
    }
    
    return ''
}

function GetTreeValue() {
    var subjectIds = '';
    var categoryIds = '';
    var chks = document.getElementsByName('cbTreeNode');
    var chksLen = chks.length;
    for (i = 0; i < chksLen; i++) {
        if (chks[i].checked) {
            if (chks[i].id.indexOf('_') == -1) {    //科目
                subjectIds += ',' + chks[i].value;
            } else {    //知识点
                categoryIds += ',' + chks[i].value;
            }
        }
    }

    return {subjectIds: subjectIds, categoryIds: categoryIds };
}

/*列表CheckBox*/
function ChkboxAllClick(e){
    var chks = document.getElementsByName('chkbox');
    for(i=0; i<chks.length; i++){
        chks[i].checked = e.checked;
    }
}

function ChkboxSigleClick(){
    var chkall = document.getElementById('chkbox_all');
    chkall.checked = IsAllChecked();
    
    function IsAllChecked(){
        var isallChecked = true;
        var chks = document.getElementsByName('chkbox');
        for(i=0; i<chks.length; i++){
            if(!chks[i].checked){
                isallChecked = false;
                break;
            }
        }
        return isallChecked;
    }
}

function ChkBoxValues(){
    var names = '';
    var chks = document.getElementsByName('chkbox');
    if(chks){
        for(i=0; i<chks.length; i++){
            if(chks[i].checked){
                names += ','+ chks[i].value;
            }
        }
    }
    return names;
}

function ChangeKnowledgeCategory(o, id, cid){
    document.getElementById("divCategory").style.display='';
    var cmd = new AjaxCommand('Knowledge', 'GetTreeCategoryDropDownList');
    if(typeof(o)!="object"){
        cmd.SetParameter('subjectId', o);
    }
    else{
        cmd.SetParameter('subjectId', o.value);
    }
    cmd.SetParameter('categoryId', cid);
    
    var rval = cmd.Execute().ReturnValue;
    if(rval != null){
        document.getElementById(id).innerHTML = rval;
    }
}

function GetSubjectDropDownList(o, id,subjectid) {
    var cmd = new AjaxCommand('Knowledge', 'GetSubjectDropDownList');
    cmd.SetParameter('moduleId', o.value);
    cmd.SetParameter('subjectId', subjectid);
    var rval = cmd.Execute().ReturnValue;
    if (rval != null) {
        document.getElementById(id).innerHTML = rval;
        if(subjectid!=0){
            ChangeKnowledgeCategory(subjectid, 'divCategory', categoryId);
        }else{
            document.getElementById("divCategory").style.display='none';
        }
    }
}

/*
prototype_begin
*/
var isIE = (document.all) ? true : false;

var $ = function(id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
    create: function() {
        return function() { this.initialize.apply(this, arguments); }  //用apply应用下面的initialize初始化属性
    }
}

var Extend = function(destination, source) {
    //继承
    for (var property in source) {
        destination[property] = source[property];
    }
}

var Bind = function(object, fun) {
    return function() {
        return fun.apply(object, arguments);
    }
}

/*
用object对象调用 fun参数是event||window event
*/
var BindAsEventListener = function(object, fun) {
    return function(event) {
        return fun.call(object, (event || window.event));
    }
}

/*
添加帧听器事件
@oTarget 目标对象
@sEventType 事件类型
@fnHandler 目标触发事件对象
*/
function addEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.addEventListener) {
        oTarget.addEventListener(sEventType, fnHandler, false);
    } else if (oTarget.attachEvent) {
        oTarget.attachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = fnHandler;
    }
}

function removeEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = null;
    }
}
/*prototype_end*/

/*改变字体大小*/
function ZoomDoc(oid, size) {
    var pContent = $(oid);
    pContent.style.fontSize = size + 'px';
    var childs = pContent.childNodes;
    for (var i = 0; i < childs.length; i++) {
        if (childs[i].nodeType == 1) {
            childs[i].style.fontSize = size + 'px';
        }
    }
}

function PlayConfirm(money) {
    if (money > 0) {
        return confirm("温馨提示：该视频需要花费 " + money + " E币,您确定要继续观看吗? 十二小时内不重复扣币![报班学员免费]");
    }
}

function ExamConfirm(money) {
    if (money > 0) {
        return confirm("温馨提示：该测试需要花费 " + money + " E币,您确定要继续测试吗? 十二小时内不重复扣币![报班学员免费]");
    }
}

function IsInFrame(){
    var url = EscapeUrl();
    var index = url.toLowerCase().indexOf('/membercenter/');
    if(index != -1){
        if(self == top){
            var module = url.substr(index + 14, url.length - index);
            module = module.substr(0, module.indexOf('/'));
            window.location.href = '/MemberCenter/Default.aspx?lurl=/MemberCenter/' + module + '/Left.aspx&rurl=' + url;
        }
    }
}
