/*!
 * djwlutil
 * by huxiao,likun,qijichao
 * http://www.dijiuwangluo.com
 */

var djwlutil = {};

/**
 * 判断字符串是否为空，空格也算空
 */
djwlutil.isBank = function(str) {
    return /^\s*$/.test(str);
}
/**
 * 判断字符串是否为空，空格不算空
 */
djwlutil.isEmpty = function(str) {
    return "" == str;
}
/**
 * 去掉前后空格
 */
djwlutil.trim = function(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}
/**
 * 是否包含特定字符(可接受正则表达式)
 */
djwlutil.contains = function(str, substr) {
    return eval("/" + substr + "/").test(str);
}

/**
 * 取得字符串长度，一个汉字两个字节
 */
djwlutil.getLength = function(str) {
    return str.replace(/[^\x00-\xff]/g, 'xx').length;
}

//---------------------------------------------------------------------------------------//

/**
 * 选择全部(反选)
 * @author huxiao kskr@qq.com
 */
djwlutil.selectAll = function(name){
	$("input[name="+name+"]").each(function(){
		this.checked == true ? this.checked = false : this.checked = true;
	});
}
/**
 * 选择至少几个
 * @author yangkai
 */		
djwlutil.selectSome = function (name,count){
	var i = 0;			
	$("input[name="+name+"]").each(function(){
		this.checked == true ? i++ : "";
	});
	return i < parseInt(count) ;
}

/**
 * 在表单提交前，禁用btn，防止重复提交
 * @param {Object} id
 * @param {Object} value
 * @author yangkai
 */
djwlutil.disBtn = function(id,value){
	$("#"+id+"").hide();
	var buttonValue = value || "操作中...";         
	$("#"+id+"").after('<input style="width:57px; height:20px;"  type="button" value="'+ buttonValue +'" disabled="disabled"');
}

/**
 * 防止重复点击链接提交信息，点击之后，链接会变成“请稍后...”
 * @param {Object} ele
 * @author huxiao kskr@qq.com
 */
djwlutil.confirms = function(ele){
	if(!confirm('确认操作？')){
		return false;
	}else{
		$(ele).after("请稍后...");
		$(ele).hide();
		return true;
	}
}

/**
 * 转向某个页面，跟window.location.href不同的是，这个是点击链接过去的，所以能获取referrer
 * @author huxiao kskr@qq.com
 */
djwlutil.gotourl = function(url){
	var id = Math.random;
	$("body").append("<a href=\""+ url +"\" style=\"display:none\" id=\""+id+"\">"+url+"</a>");
	document.getElementById(id).click();
}

//---------------------------------------------------------------------------------------//

/**
 * Return a string with &, < and > replaced with their entities
 * @param {Object} original
 */
djwlutil.escapeHtml = function(original) {
    return original.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

/**
 * 和escapeHtml(str)作用相反
 * @param {Object} str
 */
djwlutil.unescapeHtml = function(str) {
    return original.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
}

/**
 * Replace characters dangerous for XSS reasons with visually similar characters
 * @see TODO
 */
djwlutil.replaceXmlCharacters = function(original) {
    original = original.replace("&", "+");
    original = original.replace("<", "\u2039");
    original = original.replace(">", "\u203A");
    original = original.replace("\'", "\u2018");
    original = original.replace("\"", "\u201C");
    return original;
};

/**
 * Return true iff the input string contains any XSS dangerous characters
 * @see TODO
 */
djwlutil.containsXssRiskyCharacters = function(original) {
    return (original.indexOf('&') != -1 || original.indexOf('<') != -1 || original.indexOf('>') != -1 || original.indexOf('\'') != -1 || original.indexOf('\"') != -1);
};

/**
 * Enables you to react to return being pressed in an input
 */
djwlutil.onReturn = function(event, action) {
    if (!event) event = window.event;
    if (event && event.keyCode && event.keyCode == 13) action();
};

/**
 * Select a specific range in a text box. Useful for 'google suggest' type functions.
 */
djwlutil.selectRange = function(ele, start, end) {
    if (typeof ele == "string") {
        ele = document.getElementById(ele);
    }
    if (ele == null) return;
    if (ele.setSelectionRange) {
        ele.setSelectionRange(start, end);
    } else if (ele.createTextRange) {
        var range = ele.createTextRange();
        range.moveStart("character", start);
        range.moveEnd("character", end - ele.value.length);
        range.select();
    }
    ele.focus();
};

/**
 * @private Is the given node an HTML element (optionally of a given type)?
 * @param ele The element to test
 * @param nodeName eg "input", "textarea" - check for node name (optional)
 *         if nodeName is an array then check all for a match.
 * 整合了StringUtils
 */
djwlutil._isHTMLElement = function(ele, nodeName) {
    if (ele == null || typeof ele != "object" || ele.nodeName == null) {
        return false;
    }
    if (nodeName != null) {
        var test = ele.nodeName.toLowerCase();
        if (typeof nodeName == "string") {
            return test == nodeName.toLowerCase();
        }
        if (djwlutil._isArray(nodeName)) {
            var match = false;
            for (var i = 0; i < nodeName.length && !match; i++) {
                if (test == nodeName[i].toLowerCase()) {
                    match = true;
                }
            }
            return match;
        }
        djwlutil._debug("djwlutil._isHTMLElement was passed test node name that is neither a string or array of strings");
        return false;
    }
    return true;
};

/**
 * @private Like typeOf except that more information for an object is returned other than "object"
 */
djwlutil._detailedTypeOf = function(x) {
    var reply = typeof x;
    if (reply == "object") {
        reply = Object.prototype.toString.apply(x); // Returns "[object class]"
        reply = reply.substring(8, reply.length - 1); // Just get the class bit
    }
    return reply;
};

/**
 * @private Object detector. Excluding null from objects.
 */
djwlutil._isObject = function(data) {
    return (data && typeof data == "object");
};

/**
 * @private Array detector. Note: instanceof doesn't work with multiple frames.
 */
djwlutil._isArray = function(data) {
    return (data && data.join);
};

/**
 * @private Date detector. Note: instanceof doesn't work with multiple frames.
 */
djwlutil._isDate = function(data) {
    return (data && data.toUTCString) ? true: false;
};

/** @private Used internally when some message needs to get to the programmer */
djwlutil._debug = function(message, stacktrace) {
    var written = false;
    try {
        if (window.console) {
            if (stacktrace && window.console.trace) window.console.trace();
            window.console.log(message);
            written = true;
        } else if (window.opera && window.opera.postError) {
            window.opera.postError(message);
            written = true;
        }
    } catch(ex) {
        /* ignore */
    }

    if (!written) {
        var debug = document.getElementById("dwr-debug");
        if (debug) {
            var contents = message + "<br/>" + debug.innerHTML;
            if (contents.length > 2048) contents = contents.substring(0, 2048);
            debug.innerHTML = contents;
        }
    }
};

djwlutil.logout = function(){
	$("body").append('<iframe src="/testlogout.htm" style="display:none;"></iframe>');
}