/**
 * common.js
 *
 * 常用js函数库
 *
 */
var lang = new Array();
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;
var ajaxdebug = 0;
var codecount = '-1';
var codehtml = new Array();

//FixPrototypeForGecko
if(is_moz && window.HTMLElement) {
	HTMLElement.prototype.__defineSetter__('outerHTML', function(sHTML) {
        	var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var df = r.createContextualFragment(sHTML);
		this.parentNode.replaceChild(df,this);
		return sHTML;
	});

	HTMLElement.prototype.__defineGetter__('outerHTML', function() {
		var attr;
		var attrs = this.attributes;
		var str = '<' + this.tagName.toLowerCase();
		for(var i = 0;i < attrs.length;i++){
			attr = attrs[i];
			if(attr.specified)
			str += ' ' + attr.name + '="' + attr.value + '"';
		}
		if(!this.canHaveChildren) {
			return str + '>';
		}
		return str + '>' + this.innerHTML + '</' + this.tagName.toLowerCase() + '>';
        });

	HTMLElement.prototype.__defineGetter__('canHaveChildren', function() {
		switch(this.tagName.toLowerCase()) {
			case 'area':case 'base':case 'basefont':case 'col':case 'frame':case 'hr':case 'img':case 'br':case 'input':case 'isindex':case 'link':case 'meta':case 'param':
			return false;
        	}
		return true;
	});
	HTMLElement.prototype.click = function(){
		var evt = this.ownerDocument.createEvent('MouseEvents');
		evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
		this.dispatchEvent(evt);
	}
}

Array.prototype.push = function(value) {
	this[this.length] = value;
	return this.length;
}
function $$(id) {
	return document.getElementById(id);
}

// 适合大部分浏览器的getElementById = G(et) E(lement)
function GE(id) {
	if (typeof(id) != "string" || id == "") return null;
	if (document.getElementById && document.getElementById(id)) {
		return document.getElementById(id);
	} else if (document.all && document.all[id]) {
		return document.all[id];
	} else if (document.layers && document.layers[id]) {
		return document.layers[id];
	} else {
		return null;
	}
}

function resize_img(img, w, h) {
	if(typeof(img) == 'object') {
		// 计算当前图片比例
		img.style.width='';
		img.style.height='';
		var oldimgw = img.width;
		var oldimgh = img.height;
		if(oldimgw > oldimgh && oldimgw > w) {
			img.style.width=w+'px';
			img.style.height=w*oldimgh/oldimgw+'px';
		} else if(oldimgw <= oldimgh && oldimgh > h) {
			img.style.height=h+'px';
			img.style.width=h*oldimgw/oldimgh+'px';
		}
	}
}



var curlab = {};
function load_lab_content(omouseon) {
	if(typeof(omouseon) != 'object') return false;
	var oli = omouseon.parentNode;

	if(typeof(oli) == 'object' && oli.tagName == 'LI') {
		var content_id = lab_id = oli.id;
		var tmp = content_id.split('_');
		var content_id = tmp[0] + '_' + 'content' + '_' + tmp[2];
		var odiv = GE(content_id);
		if(typeof(odiv) == 'object' && odiv != null) {
			odiv.style.display = 'block';
			oli.className = 'checked';
			// 把当前的标签设置为未选中，并把当前标签对应的内容设置为不显示
			
			var ocurlab = GE(curlab[tmp[0]]);
			if(typeof(ocurlab) == 'object' && ocurlab != null) {
				var curcontent_id = ocurlab.id;
				curcontent_id = curcontent_id.replace('lab', 'content');
				var ocurcontent = GE(curcontent_id);
				ocurcontent.style.display = 'none';
				ocurlab.className = '';
			}
			// 重新记录当前标签
			curlab[tmp[0]] = lab_id;
		}
	}
}
function reg_lab(lab_name, default_lab) {
	curlab[lab_name] = default_lab;
}

function file_img_onchange(ofile, id) {
	if(typeof(ofile) == 'object') {
		var img = GE(id);
		img.src= ofile.value;
	}
}

// 高级搜索显示关闭
function ad_search_onclick(form, oid)
{
    var o_search_form   = document.forms[form];
    var o_div_ad_search = document.getElementById(oid);


    if(o_search_form.is_ad.value != 1)
    {
        o_div_ad_search.style.display = 'block';
        o_search_form.is_ad.value = 1;
    }
    else
    {
        o_div_ad_search.style.display = 'none';
        o_search_form.is_ad.value = 0;
    }
}


function dsp_chkbox2div(obj, div_id) {
	if(typeof(obj) == 'object') {
		var div = GE(div_id);
		if(typeof(div) == 'object') {
			if(obj.checked) {
				div.style.display = 'none';
			} else {
				div.style.display = 'block';
			}
		}
	}
}

function dsp_chkbox2div2(obj, div_id) {
	if(typeof(obj) == 'object') {
		var div = GE(div_id);
		if(typeof(div) == 'object') {
			if(obj.checked) {
				div.style.display = 'block';
			} else {
				div.style.display = 'none';
			}
		}
	}
}

// +-------------------------------------------------------+
// 字符串操作
// +-------------------------------------------------------+
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) {
	regExp1 = /^ */;
	regExp2 = / *$/;
	return str.replace(regExp1,'').replace(regExp2,'');
}
function unhtmlspecialchars(str){
	f = new Array(/&lt;/g, /&gt;/g, /&quot;/g, /&amp;/g);
	r = new Array('<', '>', '"', '&');
	for (var i = 0; i < f.length; i++){
		str = str.replace(f[i], r[i]);
	}
	return str;
}
// 同php的 htmlspecialchars
function htmlspecialchars(str){
	var f = new Array(new RegExp('&', 'g'),new RegExp('<', 'g'),new RegExp('>', 'g'),new RegExp('"', 'g'));
	var r = new Array('&amp;','&lt;','&gt;','&quot;');
	for (var i = 0; i < f.length; i++){
		str = str.replace(f[i], r[i]);
	}
	return str;
}
// +-------------------------------------------------------+


// 从URL参数中获取值
function URLParam(param_name) {
	if(typeof(window.document.common_url_params) != 'object') {
		var common_url_params = new Object();
		// file:/// 分割符为26%
		// http://  分割符为&
		switch(document.location.protocol) {
			case 'http:':
				str_split = '&';
				break;
			case 'file:':
				str_split = '%26';
				break;
			default:
				str_split = '&';
				break;
		}

		var aParams = document.location.search.substr(1).split(str_split);
		for (i=0;i<aParams.length;i++) {
			var aParam      = aParams[i].split('=');
			var sParamName  = aParam[0];
			var sParamValue = aParam[1];
			common_url_params[sParamName] = sParamValue;
		}
		window.document.common_url_params = common_url_params;
	} else {
		var common_url_params = window.document.common_url_params;
	}
	return (typeof(common_url_params[param_name])=='string')?common_url_params[param_name]:'';
}

// +-------------------------------------------------------+
// Cookie 操作
// +-------------------------------------------------------+
// Cookie 操作：设置Cookie的值
setCookie = function ( sName, sValue, nDays )
{
	var expires = "";
	if ( nDays )
	{
		var d = new Date();
		d.setTime( d.getTime() + nDays * 24 * 60 * 60 * 1000 );
		expires = "; expires=" + d.toGMTString();
	}

	document.cookie = sName + "=" + sValue + expires + "; path=/";
};
// Cookie 操作：得到Cookie的值
getCookie = function (sName)
{
	var re = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
	var res = re.exec( document.cookie );
	return res != null ? res[3] : null;
};
// Cookie 操作：从Cookie中删除某值
removeCookie = function ( name )
{
	setCookie( name, "", -1 );
};
// +-------------------------------------------------------+





// 直接改style
var TrOrigBgColor   = '';		//列表原始背景
var TrOrigColor     = '';		//列表原始字符颜色
var CurTrBgColor    = '#FFE';	//hover的背景
var CurTrColor      = '#417abd';//hover的字符颜色
// 直接改class
var TrOrigClassName = '';
var TrClassName     = 'tr_hover';


function chgBg(objRef, state, className) {
	if(objRef.tagName == "TR") {
		TrOrigClassName     = (1 == state) ? objRef.className : TrOrigClassName;
		objRef.className    = (1 == state) ? TrClassName : TrOrigClassName;
	} else {
		TrOrigBgColor = (1 == state) ? objRef.style.backgroundColor : TrOrigBgColor;
		TrOrigColor = (1 == state) ? objRef.style.color : TrOrigColor;

		objRef.style.backgroundColor = (1 == state) ? CurTrBgColor : TrOrigBgColor;
		objRef.style.color = (1 == state) ? CurTrColor : TrOrigColor;
	}
	return;
}

function CancelSelect() {
	event.cancelBubble = true;
	return false;
}

/**
 * non_num_filter 
 * 屏蔽键盘输入的非数字型字符
 *
 *
 */
function non_num_filter(e){ 
	//alert(event.altKey);
	// 8 退格
	// 37 - 38 - 39 - 40 上下左右
	// 45 36 33 Insert Home Pageup
	// 46 35 34 Delete End Pagedown
	// 96-105 110
	// 48-57 190

	var key = event.keyCode;
	if(
		(key == 8) || 
		(key>=33 && key<=40) ||
		(key>=45 && key<=46) ||

		((key>=48 && key <=57) || key == 46) 
		/*||
		((key>=96 && key <=105) || key == 110)*/
	)
		return true;
	else
		return false;
}




// +------------------------------------------------------------------------+ //
// 表单操作
/**
 * chkbox_sel_all 全选
 * parameter:
 * form 当前表单的名称
 * obj_name 需要选择的checkbox的名称
 *
 * 不传递点击按钮或对象的this主要是因为可能该对象不在form中，或者不是一个input
 * 可能是一个<A>的onclick事件，或者一个<DIV>的onclick事件
 * 如果该对象是合法的<INPUT>并且在form中，可以用如下的方法使用该函数
 * chkbox_sel_all(this.form, '需要选取的对象的name')
 *
 * 
 */
function chkbox_sel_all(form, obj_name, checked) {
	var this_form = document.forms[form];
	if(typeof(this_form) != 'object' && this_form == null) return false;
	var obj = this_form.elements[obj_name];

	if(typeof(obj) == 'object' && obj != null) {
		if(obj.length > 0) {
			for(var i=0; i<obj.length; i++) obj[i].checked = checked;
		} else {
			obj.checked = checked;
		}
	}
}
/**
 * chkbox_sel_reverse 反选
 * 参数说明参照 chkbox_sel_reverse
 */
function chkbox_sel_reverse(form, obj_name) {
	var this_form = document.forms[form];
	if(typeof(this_form) != 'object' && this_form == null) return false;
	var obj = this_form.elements[obj_name];

	if(typeof(obj) == 'object' && obj != null) {
		if(obj.length > 0) {
			for(var i=0; i<obj.length; i++) obj[i].checked = !obj[i].checked;
		} else {
			obj.checked = !obj.checked;
		}
	}
}

// 删除一条记录
function del_this() {
	return confirm('您是否真的要删除当前的记录？\n\n点击“确定”删除，“取消”取消删除操作。');
}

// 检测多选框中是否选择了至少一条记录
function check_at_least_one(form, obj_name) {
	
	var this_form = document.forms[form];
	if(typeof(this_form) != 'object' && this_form == null) return false;
	var obj = this_form.elements[obj_name];
	if(typeof(obj) == 'object' && obj != null) {
		if(obj.length > 0) {
			for(var i=0; i<obj.length; i++) {
				if(obj[i].checked) {
					return true;
					break;
				}
			}
		} else {
			if(obj.checked) return true;
		}
	}
	return false;
}

// 提交，默认是批量删除
// 根据后两个参数确定
function submit_select_items(form, obj_name, op_lang, op_action) {
	if(typeof(op_lang) == 'undefined') op_lang = '删除';
	if(typeof(op_action) == 'undefined') op_action = 1;

	if(check_at_least_one(form, obj_name)) {
		if(confirm('您是否真的要['+op_lang+']当前选定的记录？\n\n点击“确定”'+op_lang+'，“取消”取消'+op_lang+'操作。')) {
			if(op_action == 1) {
				return true;
			} else {
				// 不提交默认表单中的action
				var oform	= document.forms[form];
				oform.action = op_action;
				oform.submit();
				return true;
			}
		} else {
			return false;
		}
	} else {
		alert('请至少选择一条要['+op_lang+']的记录！');
		return false;
	}
}

//首页搜索
function nTabs(thisObj,Num){
	if(thisObj.className == "findy")return;
	var tabObj = thisObj.parentNode.id;
	var tabList = document.getElementById(tabObj).getElementsByTagName("li");

	for(i=0; i <tabList.length; i++)
	{
	  if (i == Num)
	  {
		  thisObj.className = "findy"; 
		  document.getElementById(tabObj+"_Content"+i).style.display = "block";
	  }else{
		   tabList[i].className = "findw"; 
		   document.getElementById(tabObj+"_Content"+i).style.display = "none";
	  }
	} 

}

//判断是否选中check,改变obj的值勤
//ifcheck(this.checked, '#a1');
function ifcheck(check, obj,obj_a,obj_b,obj_hid) { 
	
	if (check) {
		$(obj).attr("disabled",false);
		$(obj_a).attr("disabled",false);
		//$(obj_hid).val($(obj_hid).val() +"," + $(obj).val());
	} else {
		$(obj).attr("disabled",true);		
		$(obj).attr("checked",false);		
		$(obj_a).attr("disabled",true);
		$(obj_b).attr("disabled",true);
		$(obj_b).val("0");				
	}	
	check_hid("subject_listid","#hid_subject_listid");
}
function ifcheck1(check, obj) { 
	if (check) {
		$(obj).attr("disabled",false);
		$(obj).val("50");
	} else {
		$(obj).attr("disabled",true);		
		$(obj).val("0");
	}
	check_hid("ab_click","#hid_ab_listid");
}

function check_hid(checkname,hidname) {
	var a = ""; 
	for (var i=0;i<form1.elements.length;i++) {
			var e = form1.elements[i];
			if (e.name.indexOf(checkname) != -1) {
				if(e.checked) {
					a = a + e.value + ","
				}				
			}
	}	
	$(hidname).val(a);
//alert(hidname + "," + checkname);
}
