﻿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 is_mac = userAgent.indexOf('mac') != -1;

String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function trim(str) {
	return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}

function strlen(str) {
	return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}

/*----------------------验证相关方法----------------------------------*/
//返回是否为邮件地址
function isEmail(email)
{
    var reEmail = /^([A-Za-z0-9])(\w)+@(\w)+(\.)(com|com\.cn|net|cn|net\.cn|org|biz|info|gov|gov\.cn|edu|edu\.cn)/;
    return email.match(reEmail);
}
//返回是否为正整数
function isInt(str)
{
    var re = /^\d+/;
    return str.match(re);
}

function in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
					return true;
			}
		}
	}
	return false;
}

function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}
/*----------------------checkbox相关方法----------------------------------*/
//返回当前选中的ID数组
function getCheckedId(containerId) {
	var ary = new Array();
	$("#"+containerId+" input[name='chkid']").each(function(){
		    if(this.checked)
				ary.push($(this).val());
		});
	return ary;
}

//全选(checked=false)/全不选(checked=true)
function setAllChecked(containerId, checked) {
	$("#"+containerId+" input[name='chkid']").each(function(){
		    this.checked = checked;
		});
}

//反选
function setRevChecked(containerId) {
	$("#"+containerId+" input[name='chkid']").each(function(){
		    this.checked = !this.checked;
		});
}

/*----------------------浏览器功能相关方法----------------------------------*/
//复制到剪粘板
function setcopy(text, alertmsg){
	if(is_ie) {
		clipboardData.setData('Text', text);
		if(alertmsg) {
			alert(alertmsg);
		}
	} 
}

//收藏本站
function addfavorite(title, url)
{
	if (document.all) {
	   window.external.addFavorite(url,title);  
	} else if (window.sidebar) {
	   window.sidebar.addPanel(title, url, ""); 
	}
}

//获取cookie值
function getcookie(name) {
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}
//设置cookie值
function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
	var expires = new Date();
	expires.setTime(expires.getTime() + seconds * 1000);
	domain = !domain ? cookiedomain : domain;
	path = !path ? cookiepath : path;
	document.cookie = escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}


//文本框获取光标,文字消失
function txtfocus(obj)
{
    if(obj.value==obj.title)
    {
         obj.value="";
    }
}

//文本框失去光标,文字重现
function txtblur(obj)
{
    if(obj.value=="")
    {
        obj.value=obj.title;
    }
}

//返回表格中指定列中的值数组。checked=false时，返回所有行中该列的值数组
function getCellValue(table, col, checked) {
	var ary = new Array();
	$('#'+table+" tr").each(function(){
			if(!checked || (checked && $(this).find("input[name='chkid']").attr('checked') ) ) {
				var cell = $(this).find('td:eq('+ col +')');
				if(cell)
					ary.push(cell.text());
			}
		});
		
	return ary;
}

//获取对象obj选定的行中的控件里name值为str的值, Create by liuzj 2009-5-6
//返回一个逗号相隔的字符串，如 "001,002,003"
//obj=容器对象,type=控件类型,str=控件name值。
//haschk表示记录前是否有复选框,isonce表示相同的值是否只出现一次
function getControlValue(obj, type, str, haschk, isonce){
    if(obj == null || obj == "")
        obj = document;
    else if(typeof(obj) == "string")
        obj = document.getElementById(obj);
    if(type == null || type == "") type = 'input';
    if(haschk == undefined) haschk = true;
    if(isonce == undefined) isonce = true;
        
    var arr = new Array();
    var j = 0;
    var chks = obj.getElementsByTagName('input');
    var trs = obj.getElementsByTagName('tr');
    for(var i=0; i<trs.length; i++){
        var chks = trs[i].getElementsByTagName('input');
        var chk = null;
        for(var b=0;b<chks.length;b++){
            if((chks[b].type == "checkbox"||chks[b].type == "radio") && chks[b].id.toLowerCase().indexOf("chkid")>-1){
                chk = chks[b];
            }
        }
        if((chk!=null && chk.checked) || !haschk){
            var controls = trs[i].getElementsByTagName(type);
            for(var k=0; k<controls.length; k++){
                var MM_key = controls[k].id ? controls[k].id : controls[k].name;
                var MM_value = controls[k].value ? controls[k].value : controls[k].innerText;
                if(MM_value.indexOf(',') != -1) MM_value = MM_value.split(',')[0].trim(); //如果值中有","字符，仅取该字符分割的第一段
                if(MM_key.indexOf(str) != -1 && MM_value != ""){
                    var isInArr = false;
                    for(var l=0; l<arr.length; l++){
                        if(arr[l] == MM_value && isonce){
                            isInArr = true;
                        }
                    }
                    if(!isInArr){
                        arr[j] = MM_value;
                        j++;
                    }
                }
            }
        }
    }    
    return arr.toString();
}

//格式化分割字符串，如果源字符串分割符是","可以只传入第一个参数  Create by liuzj 2009-5-13
//返回一个逗号相隔的字符串，如 "'001','002','003'"
//举例：var inCodeString = formatSplitString("001*002*003", "*")
//      var inCodeString = formatSplitString("001,002,003", ",")
//      var inCodeString = formatSplitString("001,002,003")  
function formatSplitString(str, s){
    if(s == null) s = ',';
    var arr = str.split(s);
    var revalue = "";
    for(var i=0; i<arr.length; i++){
        if(arr[i].trim() != "")
            revalue +=("'"+ arr[i].trim() +"',");
    }
    if(revalue.substr(revalue.length-1, 1) == ',')
        revalue = revalue.substring(0, revalue.length-1);
    return revalue;
}

function limitInput(obj, type){
    var key = window.event.keyCode;
    if(type == 'int'){
        if(((key>=48) && (key<=57)) || (key==45 && obj.value==''))
            return true;
        else
            return false;
    }else if(type == 'double'){
        if((key>=48)&&(key<=57) || key==46){
            if(obj.value.indexOf('.')!=-1 && key==46)
                return false;
            else
                return true;
         }else if(key==45 && obj.value==''){
            return true;
         }else{
            //alert(key);
            return false;
        }
    }
}

//PageScroll
function pagescroll_class(obj, pagewidth, pageheight) {
	this.ctrlobj = document.getElementById(obj);
	this.speed = 2;
	this.pagewidth = pagewidth;
	this.times = 1;
	this.pageheight = pageheight;
	this.running = 0;
	this.defaultleft = 0;
	this.defaulttop = 0;
	this.script = '';
	this.start = function(times) {
		if(this.running) return 0;
		this.times = !times ? 1 : times;
		this.scrollpx = 0;
		return this.running = 1;
	}
	this.left = function(times, script) {
		if(!this.start(times)) return;
		this.stepv = -(this.step = this.pagewidth * this.times / this.speed);
		this.script = !script ? '' : script;
		setTimeout('pagescroll.h()', 1);
	}
	this.right = function(times, script) {
		if(!this.start(times)) return;
		this.stepv = this.step = this.pagewidth * this.times / this.speed;
		this.script = !script ? '' : script;
		setTimeout('pagescroll.h()', 1);
	}
	this.up = function(times, script) {
		if(!this.start(times)) return;
		this.stepv = -(this.step = this.pageheight * this.times / this.speed);
		this.script = !script ? '' : script;
		setTimeout('pagescroll.v()', 1);
	}
	this.down = function(times, script) {
		if(!this.start(times)) return;
		this.stepv = this.step = this.pageheight * this.times / this.speed;
		this.script = !script ? '' : script;
		setTimeout('pagescroll.v()', 1);
	}
	this.h = function() {
		if(this.scrollpx <= this.pagewidth * this.times) {
			this.scrollpx += Math.abs(this.stepv);
			patch = this.scrollpx > this.pagewidth * this.times ? this.scrollpx - this.pagewidth * this.times : 0;
			patch = patch > 0 && this.stepv < 0 ? -patch : patch;
			oldscrollLeft = this.ctrlobj.scrollLeft;
			this.ctrlobj.scrollLeft = this.ctrlobj.scrollLeft + this.stepv - patch;
			if(oldscrollLeft != this.ctrlobj.scrollLeft) {
				setTimeout('pagescroll.h()', 1);
				return;
			}
		}
		if(this.script) {
			eval(this.script);
		}
		this.running = 0;
	}
	this.v = function() {
		if(this.scrollpx <= this.pageheight * this.times) {
			this.scrollpx += Math.abs(this.stepv);
			patch = this.scrollpx > this.pageheight * this.times ? this.scrollpx - this.pageheight * this.times : 0;
			patch = patch > 0 && this.stepv < 0 ? -patch : patch;
			oldscrollTop = this.ctrlobj.scrollTop;
			this.ctrlobj.scrollTop = this.ctrlobj.scrollTop + this.stepv - patch;
			if(oldscrollTop != this.ctrlobj.scrollTop) {
				setTimeout('pagescroll.v()', 1);
				return;
			}
		}
		if(this.script) {
			eval(this.script);
		}
		this.running = 0;
	}
	this.init = function() {
		this.ctrlobj.scrollLeft = this.defaultleft;
		this.ctrlobj.scrollTop = this.defaulttop;
	}

}
