﻿function LTrim(str)
{
    var whitespace = new String(" \t\n\r");
    var s = new String(str);    
    if (whitespace.indexOf(s.charAt(0)) != -1){
        var j=0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1){
            j++;
        }
        s = s.substring(j, i);
    }
    return s;
}
function RTrim(str){
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1)
    {
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
        {
            i--;
        }
        s = s.substring(0, i+1);
    }
    return s;
}
function Trim(str){
    return RTrim(LTrim(str));
}

function IsMail(mail){
    return(new RegExp(/^[_a-zA-Z0-9]+@([_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/).test(mail));
}

function IsVadABC(code){
   return new RegExp(/^[A-Za-z]+$/).test(code);
}
function IsVadCode(code){
   return new RegExp(/^[A-Za-z0-9]+$/).test(code);
}
function IsVadNum(code){
   return new RegExp(/^[0-9]+$/).test(code);
}
function IsVadNC(code){
   return new RegExp(/^[a-zA-Z0-9\u4e00-\u9fa5]+$/).test(code);
}
function IsIP(code){
    return new RegExp(/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/).test(code);
}
function IsMob(code){
    return new RegExp(/^(130|131|132|133|134|135|136|137|138|139|150|151|152|153|154|155|156|157|158|159|180|181|182|183|184|185|186|187|188|189)\d{8}$/).test(code);
}

function openWin(url,name,w,h){
    var x=(window.screen.width-w)/2;
	var y=(window.screen.height-h)/2;
	var win=window.open(url,name,'toolbar=0,location=0,status=0,top='+y+',left='+x+',width='+w+',height='+h );
	win.focus();
	
	return win;
 }
 
function InitXMLHTTP(){
    var oXMLDoc;
    if(window.XMLHttpRequest) {
        oXMLDoc = new XMLHttpRequest();
        if (oXMLDoc.overrideMimeType) {
            oXMLDoc.overrideMimeType("text/xml");
        }
    }
    else if(window.ActiveXObject) {
        try{
                oXMLDoc = new ActiveXObject("Msxml2.XMLHTTP");
            }catch (e){
        try{
                oXMLDoc = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (e) {}
        }
    }
    if (!oXMLDoc) {
        window.alert("不能创建XMLHttpRequest对象实例.");
        oXMLDoc = null;
    }
    return oXMLDoc;
}
function $$(_id) { return document.getElementById(_id); }
function IsStringNull(_id, _alert, _isFocus) {
    if (Trim($$(_id).value) == "") {
        if (_alert) alert(_alert);
        if (_isFocus) $$(_id).focus();
        return true;
    }
    else {
        return false;
    }
}
function IsStringLen(_id, _start, _end, _alert, _isFocus) {
    if ($$(_id).value.length < _start || $$(_id).value.length > _end) {
        if (_alert) alert(_alert);
        if (_isFocus) $$(_id).focus();
        return false;
    }
    else {
        return true;
    }
}
function IsStringNum(_id, _alert, _isFocus) {
    if (!IsVadNum($$(_id).value)) {
        if (_alert) alert(_alert);
        if (_isFocus) $$(_id).focus();
        return false;
    }
    else {
        return true;
    }
}
function IsStringEmail(_id, _alert, _isFocus) {
    if (!IsMail($$(_id).value)) {
        if (_alert) alert(_alert);
        if (_isFocus) $$(_id).focus();
        return false;
    }
    else {
        return true;
    }
}
function IsStringNumABC(_id, _alert, _isFocus) {
    if (!IsVadCode($$(_id).value)) {
        if (_alert) alert(_alert);
        if (_isFocus) $$(_id).focus();
        return false;
    }
    else {
        return true;
    }
}

function IsStringNumABCLine(_id, _alert, _isFocus) {
    if (!IsVadABCNumLine($$(_id).value)) {
        if (_alert) alert(_alert);
        if (_isFocus) $$(_id).focus();
        return false;
    }
    else {
        return true;
    }
}

function PopDialogWin(_url, _width, _height) {
    var sWidth, sHeight;
    sWidth = document.documentElement.scrollWidth;
    sHeight = document.documentElement.scrollHeight;
    var win = document.createElement("div");
    win.id = "DialogWinA";
    win.style.position = "absolute";
    win.style.background = "#bbb";
    win.style.filter = "alpha(style=3,opacity=25,finishOpacity=75)";
    win.style.opacity = "0.6";
    win.style.left = "0";
    win.style.top = "0";
    win.style.width = sWidth + "px";
    win.style.height = sHeight + "px";
    win.style.zIndex = "9000";
    document.body.appendChild(win);

    var bTop, bLeft;
    bTop = document.documentElement.scrollTop + document.documentElement.clientHeight / 2 - _height / 2;
    bLeft = document.documentElement.scrollLeft + document.documentElement.clientWidth / 2 - _width / 2;
    var box = document.createElement("div");
    box.id = "DialogWinB";
    box.style.position = "absolute";
    box.style.backgroundColor = "#fff";
    box.style.top = bTop;
    box.style.left = bLeft;
    box.style.width = _width + "px";
    box.style.height = _height + "px";
    box.style.zIndex = "9001";
    document.body.appendChild(box);

    box.innerHTML = "<iframe src='" + _url + "' frameborder='no' scrolling='no' width='100%' height='100%'>";
}
function CloseDialogWin() {
    if ($$("DialogWinA") && $$("DialogWinB")) {
        document.body.removeChild($$("DialogWinA"));
        document.body.removeChild($$("DialogWinB"));
    }
}
function PopDialogWinSelect(_text, _width, _height) {
    var sWidth, sHeight;
    sWidth = document.documentElement.scrollWidth;
    sHeight = document.documentElement.scrollHeight;
    var win = document.createElement("div");
    win.id = "DialogWinA";
    win.style.position = "absolute";
    win.style.background = "#bbb";
    win.style.filter = "alpha(style=3,opacity=25,finishOpacity=75)";
    win.style.opacity = "0.6";
    win.style.left = "0";
    win.style.top = "0";
    win.style.width = sWidth + "px";
    win.style.height = (sHeight+3000) + "px";
    win.style.zIndex = "9000";
    document.body.appendChild(win);

    var bTop, bLeft;
    bTop = document.documentElement.scrollTop + document.documentElement.clientHeight / 2 - _height / 2;
    bLeft = document.documentElement.scrollLeft + document.documentElement.clientWidth / 2 - _width / 2;
    var box = document.createElement("div");
    box.id = "DialogWinB";
    box.style.position = "absolute";
    box.style.backgroundColor = "#fff";
    box.style.top = bTop;
    box.style.left = bLeft;
    box.style.width = _width + "px";
    box.style.height = _height + "px";
    box.style.zIndex = "9001";
    document.body.appendChild(box);
    
    box.innerHTML = "<iframe style='position:absolute;z-index:-1;width:100%;height:100%;top:0;left:0;scrolling:no;' frameborder='0' src='about:blank'></iframe>";
    box.appendChild(_text);
}

function CloseDialogWinSelect(_obj) {
    if ($$("DialogWinA") && $$("DialogWinB")) {
        document.body.appendChild(_obj);
        _obj.style.display = "none";
        document.body.removeChild($$("DialogWinA"));
        document.body.removeChild($$("DialogWinB"));
    }
}

function $XX(_id) { return document.getElementById(_id); }
function create(elm, parent, fn) {
    var element = document.createElement(elm);
    parent.appendChild(element);
    if (fn) fn(element);
    return element;
}
function Css(e, o) {
    if (typeof o == "string") {
        e.style.cssText = o;
        return;
    }
    for (var i in o)
        e.style[i] = o[i];
}
function addListener(element, e, fn) {
    element.addEventListener ? element.addEventListener(e, fn, false) : element.attachEvent("on" + e, fn);
}
function BindAsEventListener(object, fun) {
    var args = Array.prototype.slice.call(arguments).slice(2);
    return function(event) {
        return fun.apply(object, [event || window.event].concat(args));
    };
}
