W3CDOM = (document.createElement && document.getElementsByTagName);
ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;
ie = (navigator.appName=="Microsoft Internet Explorer") ? true : false;
is_firefox = false;
var is_show = 1;

if (ie4) {
	if (navigator.userAgent.indexOf('MSIE 5')>0) {
		ie5 = true;
	} else {
		ie5 = false; 
	}
} else {
	ie5 = false;
}
if(!ns4 && !ie4){is_firefox=true;}
function trim(str){
	str = str.replace(/^[ ]+/i,'');
	str = str.replace(/[ ]+$/i,'');
	return str;
}
function strlen(str){
	var strlen = 0;
	var v = trim(str);
	for(var i=0; i<v.length; i++){
		var tc = v.charCodeAt(i);
		if(tc > 128){ strlen += 2;}else{strlen++;}
	}
	return strlen;
}

function getPosition(el){
	var l = el.offset().left, t = el.offset().top;
	return {"left": l,"top": t}
}

function remove_div(div_id){
	if($('#'+div_id)!=null){
		$('#'+div_id).remove();
	}
}

function check_email(email){
	var ms = /^[_.0-9a-z-]+@([0-9a-z-]+.)+[a-z]{2,3}$/i;
	return email.match(ms)==false ? false : true;
}

//复制地址
function copystr(text2copy,hint){
	if (window.clipboardData) {   
		window.clipboardData.setData("Text",text2copy);
	} else {   
		var flashcopier = 'flashcopier';
		if(!document.getElementById(flashcopier)) {
			var divholder = document.createElement('div');
			divholder.id = flashcopier;
			document.body.appendChild(divholder);
		}
		document.getElementById(flashcopier).innerHTML = '';
		var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		document.getElementById(flashcopier).innerHTML = divinfo;
	}
	if(hint){
		var msg = global_lang['copy_msg']+"\n"+hint;
		alert(msg);
	}else{
		alert(global_lang['copy_msg']);
	}
}


// 关闭div
function close_fancybox(){
	$.fn.fancybox.close();
}

// 防止被iframe
if (self != top) {
	top.location.replace(location.href);
}

// 阻止链接跳转
function stopDefault( e ) {
	if ( e && e.preventDefault ) e.preventDefault();
	else window.event.returnValue = false;
	return false;
}

// cookie操作
var Cookies={
	// 创建cookie ,d=过期天数,dm=域名
    createCookie:function(cn,cv,d,dm){
          var now=new Date();
          var expire=new Date();
          if(d==null||d==0)d=1;
           
          expire.setTime(now.getTime()+3600000*24*d);
          document.cookie=cn+"="+escape(cv)+";expires="+expire.toGMTString()+";domain="+dm+"; path=/";
    },
    // 删除cookie
    deleteCookie:function(cn,dm){
          if(this.getCookie(name)){
         document.cookie=cn+"="+((domain)?"; domain="+dm:"")+"; expires=Thu, 01-Jan-70 00:00:01 GMT";
          }
    },
    // 读取cookie
    readCookie:function(cn){
          var dc=document.cookie;
          var prefix=cn+"=";
          var begin=dc.indexOf("; "+prefix);
           
          if(begin==-1){
               begin=dc.indexOf(prefix);
               if(begin!=0)return null;
          }
         else{
        begin+=2;
         }
    
         var end=document.cookie.indexOf(";",begin);    
         if(end==-1){end=dc.length;}        
         return unescape(dc.substring(begin+prefix.length,end));
    }         
}

// 某元素是否存在
var is_exist = function(selector) {
	return $(selector)&&$(selector).size()>0 ? $(selector) : false;
};

// 用户向导
var guide = {
	config: {
		pop_id: 1,
		related_wrap: '#cnt',
		close_class: 'close',
		pop_class: 'guide-pop'
	},

	init: function(no, msg, el, l, t, r, b) {// No.,message, related element, left, top, right, bottom
		l = l || 0;
		r = r || 0;
		t = t || 0;
		b = b || 0;
		no = no || 0;
		var _me = this;
		setTimeout(function(){
					var pop_id = _me.create(no, msg, l, t, r, b);
					if(el) {
						$(el).bind('mousedown', function(){guide.close(pop_id)});
					}
			}, 0);
		
	},
	
	create: function(no, msg, l, t, r, b) {
		var pop_id = 'pop_'+ this.config.pop_id;
		var pop_str = '#'+ pop_id;
		var style = '';
		if(l) style += 'left:'+ l +'px;';
		if(t) style += 'top:'+ t +'px;';
		if(r) style += 'right:'+ r +'px;';
		if(b) style += 'bottom:'+ b +'px;';

		// 增加序号
		if(no) {
			msg = '<span class="no no'+no+'"></span>' + msg;
		}
		$(this.config.related_wrap).css('position','relative').append('<div id="'+pop_id+'" class="'+this.config.pop_class+'" style="'+style+'"><span class="'+this.config.close_class+'">x</span><div class="pop-msg">'+msg+'</div></div>');
		$(pop_str+' .'+ this.config.close_class).click(function(){guide.close(pop_str)});
		this.config.pop_id ++;
		return pop_str;
	},

	close: function(id) {
		if(! id) {
			id = $(this).parent().attr('id');
		}
		$(id).fadeOut();
	}
};

var get_time = function() {
	var now = new Date();
	if(!now) return false;
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var section = 'am';
	if(hours > 12) {
		hours -= 12;
		section = 'pm';
	}
	if(minutes <= 9) {
		minutes = '0' + minutes;
	}
	return {
			time: hours +':'+ minutes +' '+ section,
			seconds: now.getSeconds(),
			zone:(now.getTimezoneOffset()/60)*(-1),
			date: now.getFullYear +'-'+ now.getMonth() +'-'+ now.getDate()
	};
};

var set_time = function(time_str) {
	var time = time_str ? time_str : get_time().time;
	if($.trim(time).length > 6) {
		$('#cur_time').html(time);
	}
};

var auto_time = function() {
	setInterval(set_time, 60000);
};

// 斑马线
var zebra = function(el) {
	$(function(){
		$('table.nozebra:children table').each(function(){
			$(this).addClass('nozebra');
		});
		var tbl = $('table:not(.nozebra)>tbody>tr');
		//if(tbl.size() > 2) {
			tbl.bind('mouseenter', function(){$(this).addClass('mson')}).bind('mouseleave', function(){$(this).removeClass('mson')}).filter('tr:odd').addClass('zebra');
		//}
		
		
	});
}
//zebra();

$(function(){
	var seconds = (60 - get_time().seconds) * 1000;
	set_time();
	setTimeout(function(){set_time();auto_time();}, seconds);  // 先同步时间，后计时

	var tabs = $('.tabs,.qtabs');
	if(tabs && tabs.size == 0) return;
	tabs.children('a').not('.on').bind('mouseenter', function(){
		$(this).addClass('hover');
	}).bind('mouseleave', function(){
		$(this).removeClass('hover');
	});
	tabs.children('a.on').css('cursor', 'text').click(function(){
		return false;
	});
});

/**
 *扩展jQuery
*/
// 禁用表单
$.fn.disable = function(){
	return this.each(function(){
		if(typeof this.disabled != 'undefined') this.disabled = true;
	});
}

// 启用表单
$.fn.enabled = function(){
	return this.each(function(){
		if(typeof this.disabled != 'undefined') this.disabled = '';
	});
}

//日期选择  @日期选择指定类型
function SelectDate(obj){
	new WdatePicker({el:obj});
}

// check all/uncheck all
function checkall(n,t){
	$(":checkbox").each(function(){
		if(t == 0){	
			this.checked = false;
		}else if(t == 1){
			this.checked = true;
		}
	});
}

// 系统表单执行函数
function text_value_select(obj,value_id){
	var str_s= $('#'+value_id).val().substr(0,1);
	var input_s  = (obj.value)?obj.title:".";
	if ($('#'+value_id).val() =='' || str_s =='.' ){
		$('#'+value_id).attr('value',input_s+obj.options[obj.selectedIndex].text);
		$('#'+value_id).attr('class','input2 italic');
	} 
}
	
function text_value_default(obj){
	if (obj.value.substr(0,1)=="."){
		obj.value="";
		obj.className = 'input2';
	}else{
		obj.className = 'input2';
	}
}

// search 
function express_search(tipobj){
	var status = $('#'+tipobj).css('display');
	if (status =='block'){
		$('#'+tipobj).css('display','none');
	} else if (status =='none'){
		$('#'+tipobj).css('display','block');
	}
}

// page input
function poppage(url,obj){
	var page = parseInt($('#'+obj).val());

	if (page > 0 && typeof(page) =='number'){
		var pop_url = url+page;
		location.href = pop_url;
	}
}
