var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);

var siteRootUrl = "/";
var template = "default";
var stylename = "default";
var templateUrl = siteRootUrl + "template/" + template + "/";

function CheckAll(form, prefix, checkall) {
    var checkall = checkall ? checkall : 'chkall';
    for (var i = 0; i < form.elements.length; i++) {
        var e = form.elements[i];
        if (e.name && e.name != checkall && (!prefix || (prefix && e.name.match(prefix)))) {
            e.checked = form.elements[checkall].checked;
        }
    }
}


function FoundInArray(needle, haystack) {
    if (typeof needle == 'string' || typeof needle == 'number') {
        for (var i in haystack) {
            if (haystack[i] == needle) {
                return true;
            }
        }
    }
    return false;
}

function SetCopy(text, alertmsg) {
    if (is_ie) {
        clipboardData.setData('Text', text);
        alert(alertmsg);
    } else if (prompt('Press Ctrl+C Copy to Clipboard', text)) {
        alert(alertmsg);
    }
}

function CopyText(obj) {
    var rng = document.body.createTextRange();
    rng.moveToElementText(obj);
    rng.scrollIntoView();
    rng.select();
    rng.execCommand("Copy");
    rng.collapse(false);
}



function GetQueryString(queryname) {
    var qKeys = {};
    var re = /[?&]([^=]+)(?:=([^&]*))?/g;
    var matchInfo;
    while (matchInfo = re.exec(location.search)) {
        qKeys[matchInfo[1]] = matchInfo[2];
    }
    return typeof (qKeys[queryname]) == 'undefined' ? '' : qKeys[queryname];
}

String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/(^\s*)/g, "");
}
String.prototype.rtrim = function() {
    return this.replace(/(\s*$)/g, "");
}

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;
}



// namespace Common 通用辅助类


//---class stringHelper Begin----------------------------------------------------------------------------


function stringHelper() {
    var _this = this; 

    var init = function() {
    }

    init();
}
stringHelper.Trim = function(str) {
    //删除左右两端的空格
    return str.replace(/(^\s*)|(\s*$)/g, "");
}

stringHelper.LTrim = function(str) {  //删除左边的空格
    return str.replace(/(^\s*)/g, "");
}

stringHelper.RTrim = function(str) {  //删除右边的空格
    return str.replace(/(\s*$)/g, "");
}





//---class stringHelper End----------------------------------------------------------------------------



//---class dataValidator Begin----------------------------------------------------------------------------

/*一些常用的正则表达式
* 
* 
^\d+$　　//匹配非负整数（正整数 + 0） 
^[0-9]*[1-9][0-9]*$　　//匹配正整数 
^((-\d+)|(0+))$　　//匹配非正整数（负整数 + 0） 
^-[0-9]*[1-9][0-9]*$　　//匹配负整数 
^-?\d+$　　　　//匹配整数 
^\d+(\.\d+)?$　　//匹配非负浮点数（正浮点数 + 0） 
^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$　　//匹配正浮点数 
^((-\d+(\.\d+)?)|(0+(\.0+)?))$　　//匹配非正浮点数（负浮点数 + 0） 
^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$　　//匹配负浮点数 
^(-?\d+)(\.\d+)?$　　//匹配浮点数 
^[A-Za-z]+$　　//匹配由26个英文字母组成的字符串 
^[A-Z]+$　　//匹配由26个英文字母的大写组成的字符串 
^[a-z]+$　　//匹配由26个英文字母的小写组成的字符串 
^[A-Za-z0-9]+$　　//匹配由数字和26个英文字母组成的字符串 
^\w+$　　//匹配由数字、26个英文字母或者下划线组成的字符串 
^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$　　　　//匹配email地址 
^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$　　//匹配url 

匹配中文字符的正则表达式： [\u4e00-\u9fa5] 
匹配双字节字符(包括汉字在内)：[^\x00-\xff] 
匹配空行的正则表达式：\n[\s| ]*\r 
匹配HTML标记的正则表达式：/<(.*)>.*<\/>|<(.*) \/>/ 
匹配首尾空格的正则表达式：(^\s*)|(\s*$) 
匹配Email地址的正则表达式：\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 
匹配网址URL的正则表达式：^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$ 
匹配帐号是否合法(字母开头，允许5-16字节，允许字母数字下划线)：^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 
匹配国内电话号码：(\d{3}-|\d{4}-)?(\d{8}|\d{7})? 
匹配腾讯QQ号：^[1-9]*[1-9][0-9]*$ 
* */
/*formValidatorRegex 正则表达式*/
var regexEnum =
{
    intege: "^-?[1-9]\\d*$", 				//整数
    intege1: "^[1-9]\\d*$", 				//正整数
    intege2: "^-[1-9]\\d*$", 				//负整数
    num: "^([+-]?)\\d*\\.?\\d+$", 		//数字
    num1: "^[1-9]\\d*|0$", 				//正数（正整数 + 0）
    num2: "^-[1-9]\\d*|0$", 				//负数（负整数 + 0）
    money: "^\\d{0,}$|^(\\d{0,})\.(\\d{0,})$",
    decmal: "^([+-]?)\\d*\\.\\d+$", 		//浮点数
    decmal1: "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$", //正浮点数
    decmal2: "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$", //负浮点数
    decmal3: "^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$", //浮点数
    decmal4: "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$", //非负浮点数（正浮点数 + 0）
    decmal5: "^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$", //非正浮点数（负浮点数 + 0）
    email: "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", //邮件
    color: "^[a-fA-F0-9]{6}$", 			//颜色
    url: "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$", //url
    chinese: "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$", 				//仅中文
    ascii: "^[\\x00-\\xFF]+$", 			//仅ACSII字符
    zipcode: "^\\d{6}$", 					//邮编
    mobile: "^(13|15|16|18)[0-9]{9}$", 			//手机
    ip4: "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$", //ip地址
    notempty: "^\\S+$", 					//非空
    picture: "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$", //图片
    rar: "(.*)\\.(rar|zip|7zip|tgz)$", 							//压缩文件
    date: "^\\d{4}(\\-|\\/|\.)\\d{1,2}\\1\\d{1,2}$", 				//日期
    qq: "^[1-9]*[1-9][0-9]*$", 			//QQ号码
    tel: "^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$", //电话号码的函数(包括验证国内区号,国际区号,分机号)
    mob: "^1\\d{10}$", //手机号
    //    telmob: "^[1-9]\\d*$", //同时是验证手机及电话
    telmob: "^((([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?)$", //同时是验证手机及电话
    username: "^\\w+$", 					//用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
    letter: "^[A-Za-z]+$", 				//字母
    letter_u: "^[A-Z]+$", 				//大写字母
    letter_l: "^[a-z]+$", 				//小写字母
    idcard: "^[1-9]([0-9]{14}|[0-9]{17})$", //身份证
    website: "^[0-9a-z]([a-z0-9\-]*[a-z0-9])?\.(com|net|org)$" //网站域名
}

function dataValidator() {

}

dataValidator.isEmail = function(str) {
    var re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    return re.test(str);
}


dataValidator.isNumber = function(str) {
    var re = /^\d+$/;
    return re.test(str);
}


dataValidator.isChinese = function(str) {
    var re = /^[\u4e00-\u9fa5]+$/;
    return re.test(str);
}

dataValidator.isMobile = function(str) {
    var re = /^(13|15|16|18)[0-9]{9}$/;
    return re.test(str);
}
dataValidator.isPhone = function(str) {
    var re = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
    return re.test(str);
}
dataValidator.isPostcode = function(str) {
    var re = /^\d{6}$/;
    return re.test(str);
}

dataValidator.isTime = function(str) {
    var re = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;
    return re.test(str);
}



//---class dataValidator End----------------------------------------------------------------------------


//---class DataSecurity Star----------------------------------------------------------------------------

function DataSecurity() {

}

DataSecurity.CorrectNumberUnderMax = function(obj, maxNumber) {
    //当obj的数值大于maxNumber修正为maxNumber
    if (parseFloat(obj.value) > maxNumber) {
        obj.value = maxNumber;
        alert('输入数值过大！');
    }
}
//---class DataSecurity End----------------------------------------------------------------------------








function loadJsFile(filename) {
    //LoadJsCssFile("myscript.js",)
    var fileref = document.createElement('script');
    fileref.setAttribute("type", "text/javascript");
    fileref.setAttribute("src", filename);
    if (typeof (fileref) != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
}


function loadCssFile(filename) {
    //LoadJsCssFile("main.css", "css")
    var fileref = document.createElement("link");
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", filename);
    if (typeof (fileref) != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
}


/**
*判断图片大小  
*obj 文件域ID ，k 上传文大小
*/
function checkImg(obj, k, m) {
    var img = null;
    if (img) img.removeNode(true);
    img = document.createElement("img");
    img.style.position = "absolute";
    img.style.display = "none";
    document.body.insertAdjacentElement("beforeend", img);
    document.all[obj].focus();
    document.execCommand("selectall");
    img.src = document.selection.createRange().text;
    if (img.src.length > 0) {
        var ext = img.src.substr(img.src.length - 4, 4).toLowerCase();
        if (ext == ".jpg" || ext == ".gif" || ext == "jpeg" || ext == ".png") {
        }
        else {
            alert('上传的文件为非图片格式\n\n允许的格式为：jpg、jpeg、gif、png');
            return false;
        }
        //IE7.0 不支持，暂停该判断。
        //if (img.readyState!="complete" && m!="1"){ 
        //	setTimeout("checkImg('"+obj+"',"+k+",1);", 500); 
        //	return false; 
        //}
        if (Math.floor(img.fileSize / 1024 * 100) / 100 > k) {
            alert('允许上传图片的最大尺寸为 ' + k + ' k');
            return false;
        }
    }
    return true;
}

/**
*判断文件后缀 (str长度为4，比如 .swf)  
*/
function check_File_Type(obj, str) {
    var theSrc
    try {
        theSrc = document.all[obj].value;
        if (theSrc.length > 0) {
            var ext = theSrc.substr(theSrc.length - 4, 4).toLowerCase();
            if (str.indexOf(ext) >= 0) {
            }
            else {
                alert('上传的文件格式不对\n\n允许的格式为：' + str);
                document.all[obj].focus();
                document.execCommand("selectall");
                return false;
            }
        }
    } catch (err) { }
    return true;
}




function showDialog(url, w, h) {
    var num = Math.random();
    var sret = window.showModalDialog(url, "", "dialogWidth:" + w + "px;dialogHeight:" + h + "px;edge: Raised; center: Yes;help:no; resizable: Yes; status: no;scroll:yes;")
    if (sret == "refresh") {
        return true;
    }
    else {
        return false;
    }
}

function addFavorite(title, url) {

    if ($.browser.version = "8.0") {
        window.external.addToFavoritesBar(url, title)
        return true;
    }
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (document.all) {
        window.external.AddFavorite(url, title);
    } else if (window.opera && window.print) {
        return true;
    }
}

//JSON To String 
function obj2str(o) {
    var r = [];
    if (typeof o == "string")
        return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
    if (typeof o == "object") {
        if (!o.sort) {
            for (var i in o)
                r.push("\"" + i + "\":" + obj2str(o[i]));
            if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                r.push("toString:" + o.toString.toString());
            }
            r = "{" + r.join() + "}"
        }
        else {
            for (var i = 0; i < o.length; i++)
                r.push(obj2str(o[i]))
            r = "[" + r.join() + "]"
        }
        return r;
    }
    return o.toString();
}



function getParam(param) {
    var uri = window.location.search;
    var re = new RegExp("" + param + "=([^&?]*)", "ig");
    return ((uri.match(re)) ? (decodeURIComponent(uri.match(re)[0].substr(param.length + 1))) : '');
}

//让网页标题闪烁
var step = 0;
var space = '';
var _title = document.title;
for (var i = 0; i <= _title.length; i++) space += '　';

function flashTitle() {
    step++;
    if (step == 3) {
        step = 1
    }
    if (step == 1) {
        document.title = space
    }
    if (step == 2) {
        document.title = _title
    }
    setTimeout("flashTitle()", 500);
}