/**
 * @author ANTZ
 */
function $(id){
    return document.getElementById(id);
}

// Ajax-- 实例getRequestObject("https://login.sina.com.cn/cgi/trigger_regmail.php", {method:'GET', onError: fun, onCompletedListener : fun});
function getRequestObject(url, opt){
    var req = false;
    if (window.XMLHttpRequest) {
         req = new window.XMLHttpRequest();
    } else 
        if (window.ActiveXObject) {
           req = new window.ActiveXObject("Microsoft.XMLHTTP");
        }
    if (!req) {
		return false;
	}
    
    opt = opt || {};
    opt.method = opt.method || 'GET';
    
    opt.data = opt.data || null;
    req.onreadystatechange = function(){
        //alert(req.readyState + ":" + req.status);
        if (req.readyState == 4) {
			if (req.status && req.status == 200) {
				if (opt.onCompletedListener) {
					opt.onCompletedListener.call(req, req.responseText);
				}
			} else {
				if(opt.onError){
					opt.onError.call(req, req.responseText);
				}
			}	
		}
    };
    try {
        req.open(opt.method, url, true);
        opt.method == "POST" ? req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") : null;
    } catch (e) {
        alert("Ajax_error:" + e);
    }
    req.send(opt.data);
    return req;
}

function strLen(key){
    var l = escape(key), len;
    len = l.length - (l.length - l.replace(/\%u/g, "u").length) * 4;
    l = l.replace(/\%u/g, "uu");
    len = len - (l.length - l.replace(/\%/g, "").length) * 2;
    return len;
}

//- navigator
var scope = scope || {};
scope._ua = navigator.userAgent.toLowerCase();
scope.$IE = /msie/.test(scope._ua);
scope.$OPERA = /opera/.test(scope._ua);
scope.$MOZ = /gecko/.test(scope._ua);
scope.$IE5 = /msie 5 /.test(scope._ua);
scope.$IE55 = /msie 5.5/.test(scope._ua);
scope.$IE6 = /msie 6/.test(scope._ua);
scope.$IE7 = /msie 7/.test(scope._ua);
scope.$SAFARI = /safari/.test(scope._ua);
//--------------------------------------------------------------------------------------------Check--------------------------------------------------------------------------------------------
var Check = {};
//驗證郵箱名格式
Check.checkEmail = function(val){
    if (!val || val == "") {
        return "请输入邮箱名";
    }
    if (val.length < 4 || val.length > 16) {
        return "邮箱名必须是4-16位之间（包含4位及16位）";
    }
    if (/[A-Z]/.test(val)) {
        return "不能有大写字母";
    }
    if (val.indexOf(" ") > -1) {
        return "不能有空格";
    }
    if ((/^[0-9]+$/g).test(val)) {
        return "不能全是数字";
    }
    if ((/^[_]+$/g).test(val)) {
        return "不能全是下划线";
    }
    if (val.slice(val.length - 1) == "_") {
        return "下划线不能在结尾";
    }
    for (var i = 0; i < val.length; i++) {
        var c_gname = val.charCodeAt(i);
        if (c_gname > 65248 || c_gname == 12288) {
            return "不能有全角字符";
        }
    }
    if (/^[\u4e00-\u9fa5]/.test(val)) {
        return "不能有中文";
    }
    if ((/>|<|,|\[|\]|\{|\}|\/|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\?|\+|\-|\*|\$|\%|\^|\&|\(|\)|`/i).test(val)) {
        return "邮箱名仅允许使用小写英文、数字或下划线";
    }
    if (!/^[0-9a-z_]+$/.test(val)) {
        return "限用英文小写、数字、下划线. 不能全是数字或下划线。 下划线不能在结尾。";
    }
    return "ok";
};

//密码检查
Check.checkPassword = function(ppassword){
    if (!ppassword || ppassword == "") {
        return "请填写密码";
    }
    if (ppassword.length < 6 || ppassword.length > 16) {
        return "密码的长度应该在6-16个字符之间";
    }
    if (ppassword.indexOf(" ") > -1) {
        return "密码中不允许包含空格";
    }
    if ((/>|<|\+|,|\[|\]|\{|\}|\/|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i).test(ppassword)) {
        return "请勿使用特殊字符";
    }
    for (var i = 0; i < ppassword.length; i++) {
        var c_gname = ppassword.charCodeAt(i);
        if (c_gname > 65248 || c_gname == 12288) {
            return "请勿使用全角字符";
        }
    }
    if (/^[\u4e00-\u9fa5]/.test(ppassword)) {
        return "请勿使用中文";
    }
    if (ppassword.slice(ppassword.length - 1) == "_" || ppassword.slice(0, 1) == "_") {
        return "下划线不能在开头或结尾";
    }
    //!/^[a-zA-Z0-9/\-/.?_]{6,16}$/.test(ppassword)
    //	if (!/^[a-zA-Z0-9/\-/.?_]{6,16}$/.test(ppassword)){
    //		ShowError(opbg,oPP,"6-16位字符（包括6、16），限用英文、数字、半角“.”、“-”、“?”以及下划线，区分大小写。",1);
    //		return false;
    //	}
    return "ok";
};

// 密码确认检查
Check.checkPasswordAgain = function(password, passwordAgain){
	if (!passwordAgain || passwordAgain == "") {
		return "请填写密码";
	}
	if (passwordAgain != password) {
		return "您两次输入的密码不一致";
	}
	if (!Check.checkPassword(passwordAgain)) {
		return "您输入的密码格式不正确";
	}
	
	return "ok";
};

//密码提示答案
Check.checkAnswer = function(val){
    if (!val || val == "") {
        return "请输入密码查询答案";
    }
    if (strLen(val) < 6 || strLen(val) > 16) {
        return "长度应在6-16个字符或3-8个汉字之间";
    }
    if ((/>|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i).test(val)) {
        return "请勿使用特殊字符";
    }
    if (/^\s/.test(val) || /\s$/.test(val)) {
        return "第一位和最后一位不允许使用空格";
    }
    if (val.slice(val.length - 1) == "_") {
        return "下划线不能在最后";
    }
    if (/^[\uFF00-\uFFFF]/.test(val)) {
        return "不能有全角字符";
    }
    
    return "ok";
};

//密码提示自填写问题检查
Check.checkCustomQuestion = function (val){
	if(!val || val==""){
		return "请输入密码查询问题";
	}
	if(strLen(val)<4 || strLen(val)>32){
		return "长度应该在4-32个字符或2-16个汉字之间";
	}
	if((/>|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i).test(val)){
		return "请勿使用特殊字符";
	}
	if(/^\s/.test(val) || /\s$/.test(val)){
		return "第一位和最后一位不允许使用空格";
	}
	if(val.slice(val.length-1)=="_"){
		return "下划线不能在最后";
	}
	if(/^[\uFF00-\uFFFF]/.test(val)){
		return "不能有全角字符";
	}
	return "ok";
};

//手机号码检查
Check.checkMobile = function(mobile){
    return /^13\d{9}$/.test(mobile) | /^15\d{9}$/.test(mobile);
};
//-----------------------------------------------------------------cookie---------------------------------------------------------------------------------------------------------------------------------------------------
var Cookie = {};
Cookie.setCookie = function(cookieName, cookieValue){
    var cookie_expires = new Date();
    cookie_expires.setTime(cookie_expires.getTime() + 10 * 60 * 1000);
    document.cookie = cookieName + "=" + cookieValue + ";expires=" + cookie_expires.toGMTString();
};
Cookie.get_Cookie = function(cookieName){
    var cookieString = document.cookie;
    var start = cookieString.indexOf(cookieName + '=');
    if (start == -1) {
		return null;
	}
    start += cookieName.length + 1;
    var end = cookieString.indexOf(';', start);
    if (end == -1) {
		return unescape(cookieString.substring(start));
	}
    return unescape(cookieString.substring(start, end));
};

//-----------------------------------------------------------------formUtils---------------------------------------------------------------------------------------------------------------
var FormUtil = {};
FormUtil.focuseOnFirst = function(){
    if (document.forms.length > 0) {
        for (var i = 0; i < document.forms[0].length; i++) {
            var temp = document.forms[0].elements[i];
            if (temp.type != "hidden") {
                temp.focus();
                return;
            }
        }
    }
};

FormUtil.addSelectOption = function(id, optArr){
    for (var i = 0; i < optArr.length; i++) {
        var opt = document.createElement("option");
        var optText = document.createTextNode(optArr[i]);
        opt.appendChild(optText);
        $(id).appendChild(opt);
    }
};
//---------------------------------------------------------------------------EventUtils---------------------------------------------------------------------------------
EventUtils = {};
EventUtils.addEventListener = function(oTarget, sEventType, funHandler){
    if (oTarget.addEventListener) {
        oTarget.addEventListener(sEventType, funHandler, false);
    } else 
        if (oTarget.attachEvent) {
            oTarget.attachEvent("on" + sEventType, funHandler);
        } else {
            oTarget["on" + sEventType] = funHandler;
        }
};

EventUtils.removeEventListener = function(oTarget, sEventType, funHandler){
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, funHandler, false);
    } else if (oTarget.detachEvent) {
            oTarget.detachEvent("on" + sEventType, funHandler);
        } else {
            oTarget["on" + sEventType] = null;
        }
};

EventUtils.formatEvent = function(oEvent){
    //如果是windows
    if (document.all) {
        //alert("this is window");
        oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
        oEvent.eventPhase = 2;
        oEvent.isChar = (oEvent.charCode > 0);
        oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
        oEvent.pageY = oEvent.clientY + document.body.scrollTop;
        oEvent.preventDefault = function(){
            this.returnvalue = false;
        };
        
        if (oEvent.type == "mouseout") {
            oEvent.relatedTarget = oEvent.toElement;
        } else 
            if (oEvent.type == "mouseover") {
                oEvent.relatedTarget = oEvent.fromEvent;
            }
        
        oEvent.stopPropagation = function(){
            this.cancelBubble = true;
        };
        
        oEvent.target = oEvent.srcElement;
        oEvent.time = (new Date()).getTime();
    }
    //alert("ss" + oEvent);
    
    return oEvent;
};

EventUtils.getEvent = function(){
    if (window.event) {
        //alert(11 + window.event);
        return this.formatEvent(window.event);
    } else {
        return EventUtils.getEvent.caller.arguments[0];
    }
    
};
//---------------------------------------------------------------------------passwordStrength---------------------------------------------------------------------
// 密码强弱检查相关js
function PasswordStrength(password){
	var _pw = password;
	
	function CharMode(iN){
		if (iN >= 65 && iN <= 90) { //大写字母
			return 2;
		}
		if (iN >= 97 && iN <= 122) { //小写
			return 4;
		} else {
			return 1;
		}//数字
	}
	//bitTotal函数：计算出当前密码当中一共有多少种模式
	function bitTotal(num){
		modes=0;
		for (i=0;i<3;i++){
			if (num & 1) {
				modes++;
			}
			num>>>=1;
		}
		return modes;
	}
	
	//1弱 2中 3强 其余是强
	this.checkStrong = function (){
		Modes=0;
		for (i=0;i<_pw.length;i++){
			//测试每一个字符的类别并统计一共有多少种模式.
			Modes|=CharMode(_pw.charCodeAt(i));
		}
		var btotal = bitTotal(Modes);
		if (_pw.length >= 10) {
			btotal++;
		}
		return btotal;
	};
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function getDim(e){
	return {
		top : e.offsetTop,
		left : e.offsetLeft,
		width : e.offsetWidth,
		height : e.offsetHeight
	};
}
function getWindowSize(){    
    if (self.innerHeight) {
        // Most common
        return { 'width':self.innerWidth,'height':self.innerHeight };
    } else if (document.documentElement 
        && document.documentElement.clientHeight) {
        // MSIE strict
        return {
            'width':document.documentElement.clientWidth, 
            'height':document.documentElement.clientHeight
        };
    } else if (document.body) {
        // MSIE quirks
        return {
            'width':document.body.clientWidth,
            'height':document.body.clientHeight
        };
    }   
}


