
// 根据鼠标或焦点事件，设置文本框和按钮样式
function IEvent(sType,oInput){
	var onStyle,offStyle,eleType;
	eleType = oInput.type;
	if("button" == eleType || "submit" == eleType || "reset" == eleType || "textarea" == oInput.tagName){
		onStyle = "button_on";
		offStyle = "button_off";
	}else{
		onStyle = "input_on";
		offStyle = "input_off";
	}
	switch (sType){
		case "focus" :
			oInput.isfocus = true;
		case "mouseover" :
			oInput.className = onStyle;
			break;
		case "blur" :
			oInput.isfocus = false;
		case "mouseout" :
			if(!oInput.isfocus){
				oInput.className = offStyle;
			}
			break;
	}
}

// 保存与获取Cookies
function setCookie(name,value){
	var Days = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function getCookie(name){
	var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
	if(arr != null) return unescape(arr[2]); return null;
}

// 展开与关闭对象
function collapse(img, objName){	
	var cateid = objName.replace("cate", "");
	var cateids= getCookie("closedcateids");
	if(cateids == null){
		cateids = "";
	}
	if (objName == 'loopedreplies'){
		collapseTables(img);
		return;
	}
	var obj = $(objName);
	if (!obj){return;}
	if (obj.style.display!='none'){
		obj.style.display = 'none';
		img.src = img.src.replace('open', 'close');
		setCookie("closedcateids",cateids.replace("[" + cateid + "]", "") + "[" + cateid + "]");
	}else{
		obj.style.display = 'block';
		img.src = img.src.replace('close', 'open');
		setCookie("closedcateids",cateids.replace("[" + cateid + "]", ""));
	}
}

function collapseTables(img){
	var tables = document.getElementsByTagName("table");
	if (img.src.indexOf('open') != -1){
		img.src = img.src.replace('open', 'close');
		for (var i=0; i<tables.length; i++){
			if (tables[i].name != 'loopedreplies') continue;
			tables[i].style.display = 'none';
		}
	}else{
		img.src = img.src.replace('close', 'open');
		for (var i=0; i<tables.length; i++){
			if (tables[i].name != 'loopedreplies') continue;
			tables[i].style.display = '';
		}
	}
}

// 层切换
function setTab(name, cursel, n){
	for(var i = 1; i <= n; i++){
		$(name + i).className = (i==cursel) ? "hover" : "";
		$("con_" + name + "_" + i).style.display = (i==cursel) ? "block" : "none";
	}
}

// 使PNG图片透明
function alphaImage(){
	for(var i=0; i<document.images.length; i++){
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
			img.outerHTML = strNewHTML;
			i = i - 1;
		};
	};
};

// 设置字体大小
function setFontSize(uid, n){	
	var element = $(uid);
	var defaultSize = 14;
	var defaultLineheight = 20;
	var currentSize = "";
	var cSize = 0;
	var currentLineheight = "";
	var cLineheight = 0;
	if(null != element){
		currentSize = element.style.fontSize;
		defaultLineheight = element.style.lineHeight;
		cSize = parseInt(currentSize.replace("px",""));
		cLineheight = parseInt(defaultLineheight.replace("px",""));
		if(0 == n){
			cSize = cSize - 2;
			cLineheight = cLineheight - 2;
		}else if(1 == n){
			cSize = cSize + 2;
			cLineheight = cLineheight + 2;		
		}
		if(cSize > 6 && cSize<50){
			try{
				element.style.fontSize = cSize + "px";
				element.style.lineHeight = cLineheight + "px";	
			}catch(e){
				element.style.fontSize = defaultSize + "px";
				element.style.lineHeight = defaultLineheight + "px";	
			}
		}
	}else{
		return;
	}	
}

// 图像不存在时指定默认图像
function ImageOnError(obj, url){
	if(typeof(obj.getAttribute('FirstErr',1))=='undefined' || obj.getAttribute('FirstErr',1)==null){
		obj.setAttribute('FirstErr','NotFirst',1);
		obj.src = url;
	}else{
		return;
	}
}

function ResizeImage(obj){
	var w = obj.width, h = obj.height; 
	var maxHeight = 760, maxWidth = 760;
	if (h > maxHeight){    
		obj.height = maxHeight;    
		obj.width = (maxHeight / h) * w;    
		w = obj.width;   
		h = obj.height;   
	}    
	if (w > maxWidth){    
		obj.width = maxWidth;    
		obj.height = (maxWidth / w) * h;    
	}    
}

function ZoomImage(obj){
	var zoom = (parseInt(obj.style.zoom, 10) || 100);
	zoom += event.wheelDelta / 12;
	if (zoom>0){
		obj.style.zoom = zoom + '%';
	}
	return false;
}

// 全选复选框
function selectallcheckbox(sForm, sObj) {
    var form = eval('document.' + sForm);
    var obj = eval('document.' + sForm + '.' + sObj);
    for (var i = 0; i < form.elements.length; i++) {
        var e = form.elements[i];
        if ( e.name.toLowerCase() == 'id' && e.type.toLowerCase() == 'checkbox') {
            e.checked = obj.checked;
        }
    }
    if(obj.checked) {
        obj.title = "取消全选";
    } else {
        obj.title = "全选";
    }
}

// 分页
var sUrl = null;

function GotoPage(){
	if (window.event){
		if(window.event.keyCode == 13){
			var page = $("iPage").value;
			if (isNaN(page)) page = 1;
			if(sUrl == 'undefined' || sUrl == null || sUrl == '') sUrl = $("sUrl").value;
			if(arguments.length == 1 && arguments[0] != '_self' && arguments[0] != '_target' && arguments[0] != '_parent' && arguments[0] != '_top'){
				var w = eval(arguments[0]);
				w.location.href = SetPage(sUrl, page);
			}else{
				location.href = SetPage(sUrl, page);
			}
			event.returnValue = false;
		}
	}
}

function OnGoto(){
	var page = $("iPage").value;
	if (isNaN(page)) page = 1;
	if(sUrl=='undefined' || sUrl==null || sUrl=='') sUrl = $("sUrl").value;
	if(arguments.length == 1 && arguments[0] != '_self' && arguments[0] != '_target' && arguments[0] != '_parent' && arguments[0] != '_top'){
		var w = eval(arguments[0]);
		w.location.href = SetPage(sUrl, page);
	}else{
		location.href = SetPage(sUrl, page);
	}
}

function SetPage(s, i){
	return s.replace('{p}', i.toString());
}

function AjaxPage(obj, innerdiv){
    var url = obj.href;
    if(url.indexOf('?') == -1){
        url += "?nocache=" + Math.random();
    }else{
        url += "&nocache=" + Math.random();
    }
    $(innerdiv).innerHTML = ' 正在加载中，请稍候...';
    var ajax = new Ajax();
    ajax.Request('get', url, '', function (xmlHttp){
        try{
            $(innerdiv).innerHTML = xmlHttp.responseText;
        }
        finally{
            xmlHttp.abort();
        }
    }, true);
    return false;
}

//过滤HTML标记
function HTMLDecode(str){
	str = str.replace(/&lt;/g, "<");
	str = str.replace(/&gt;/g, ">");
	str = str.replace(/&amp;/g, "&");
	str = str.replace(/&gt;/g, "\"");
	str = str.replace(/&#39;/g, "'");
	str = str.replace(/&nbsp;/g, " ");
	return str;
}

// 显示当前时间
function currenttime(){
    var now = new Date();
    var year = now.getYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var dayofweek = now.getDay();
    var hour = now.getHours();
    var min = now.getMinutes();
    var sec = now.getSeconds();
    if(month < 10) month = "0" + month;
    if(day < 10) day = "0" + day;
    if(hour < 10) hour = "0" + hour;
    if(min < 10) min = "0" + min;
    if(sec < 10) sec = "0" + sec;
    var arr_week = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
    var nowtime = month + "月" + day + "日 " + arr_week[dayofweek] + " " + hour + ":" + min + ":" + sec;
    return nowtime;
}

function showtime(){
    var showtime = $('showtime');
    if(showtime != null){
        showtime.innerHTML = currenttime();
        setInterval("showtime();", 1000);
    }
}


// 根据ID获取页面元素
function $(obj){
	var element = null;
	if(document.getElementById){
		element = document.getElementById(obj);
	}else if(document.all){
		element = document.all[obj];
	}else if(document.layers){
		element = document.layers[obj];
	} 
	return element;
} 

// 获取元素相关值
function getOffsetTop(elm) {
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent){
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetTop;
}

function getOffsetLeft(elm) {
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent) {
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}

function getBodyWidth(){ 
	var w = 0;
	if (!document.all) { 
		w = document.body.clientWidth > document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth; 
	}else{ 
		w = document.documentElement.clientWidth == 0 ? document.body.clientWidth : document.documentElement.clientWidth; 
	}
	return parseInt(w, 10);
}

function getBodyHeight(){
	var h = 0;
	if (!document.all) { 
		h = document.body.clientHeight > document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight; 
	}else{ 	
		h = document.documentElement.clientHeight == 0 ? document.body.clientHeight : document.documentElement.clientHeight; 
	}
	return parseInt(h, 10);
}

function getOffsetWidth(obj){
	var w = parseInt(obj.style.width, 10);
	if(!w && document.all){ 
		w = parseInt(obj.offsetWidth, 10); 
	}
	if(!w && document.all){ 
		w = parseInt(obj.currentStyle.width, 10); 
	} 
	if(!w){ 
		w = 150; 
	}
	return w;
}

function getOffsetHeight(obj){
	var h = parseInt(obj.style.height, 10);
	if(!h && document.all){ 
		h = parseInt(obj.offsetHeight, 10); 
	} 
	if(!h && document.all){ 
		h = parseInt(obj.currentStyle.height, 10); 
	} 
	if(!h){ 
		h = 280; 
	}
	return h;
}
