//获取ID对象
function getElm(name) {
	return document.getElementById(name);
}

//显示模式窗口
function popObjectSelectInfo(url) {
	window.open(url,'newWin','modal=yes,width=550,height=330,resizable=no,scrollbars=no');
}

/********** 定义COOKIE 类 start ***************/
var Cookie =
{
	get : function(name) {
		var value = "";
		var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
		if(arr != null) { value = unescape(arr[2]); }
		return value;
	},
	set : function(name,value,days){
		if(isNaN(days)){
			document.cookie = name+"="+escape(value)+";";
		}else{
			document.cookie = name+"="+escape(value)+";expires="+new Date(new Date().getTime()+days*86400000).toGMTString();
		}
	},
	del : function(name){
		document.cookie = name+"=;expires="+new Date().toGMTString();
	}
}
/********** 定义COOKIE 类 end ***************/



document.onkeydown = function() {  
	var k = window.event.keyCode;  
	if (k == 116 || (window.event.ctrlKey && window.event.keyCode==82)) //屏蔽   F5   刷新键  和CTRL+R
	{  
		window.event.keyCode = 0;  
		window.event.returnValue = false;  
		chkrefresh();
	} 
}
var refreshtime = 5000;//5秒
function chkrefresh()//检测是否刷新过快
{
	var oldtime = Cookie.get('refreshtime');
	var nowtime = new Date().getTime();
	oldtime = parseInt(oldtime);
	if(oldtime+refreshtime<nowtime) {
		window.location.reload();
		Cookie.set('refreshtime',nowtime);
	} else {
		alert("刷新过快，请稍候");
	}
}


/********** Ajax start ***************/
var xmlHttp;
var obj;

//接口
function myRequest(tempobj,url) {
	if (tempobj==null) return;
	obj=tempobj;
	var re = /.*\//gi;
	startRequestGet(this.location.href.match(re)+url);
}
//get请求
function startRequestGet(url) {
    try {
        createobj();
        if(xmlHttp) {
        	//xmlHttp.setTimeouts(15000, 15000, 15000, 15000);
            xmlHttp.onreadystatechange=function() {
                //客户端请求成功
                if(xmlHttp.readyState==4) {
                    //服务端返回成功
                    if(xmlHttp.status==200) {
                        doResult();
                    }
                }    
            }
            xmlHttp.open("GET",url,true);
            xmlHttp.setRequestHeader("Content-Type","text/html;charset=utf-8");
            xmlHttp.send(null);
        }
    } catch(e) {
		// alert('object null');
    }   
}
//post请求
function startRequestPost(url,value) {
    try {
		// createXmlHttpRequest();
        if(xmlHttp!=null) {
            xmlHttp.onreadystatechange=function() {
				if(xmlHttp.readyState==4) {
					if(xmlHttp.status==200) {
                        doResult();
                    }
                }
            }
            xmlHttp.open("POST",url,true);
            //http_request.setRequestHeader("Content-Length",value.length); 
            xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlHttp.send(value);
        }
    } catch(e) {
        return false;
    }
}
//创建ajax对象
function createobj() {
	if (window.XMLHttpRequest) { 
		xmlHttp= new XMLHttpRequest();
	} else if(window.ActiveXObject) {
        try {
			// 尝试按新版InternetExplorer方法创建
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) { // 创建请求的ActiveX对象失败
			try {
				// 尝试按老版InternetExplorer方法创建
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				// 不能通过ActiveX创建XMLHttpRequest
			}
		}
	}
}
//返回执行结果
function doResult() {
	var content=xmlHttp.responseText;
	obj.innerHTML=content;
}
//播放器:video 视频文件; pic 代替的图片文件; width 宽带; height 高度
function play(video, pic, width, height) {
	var str;
	if (video=="") {
		str = "<img src='"+pic+"' onload='changeRate(this,"+width+","+height+")'>";
	} else str = getPlayer(video, width, height + 50);
	
	document.write(str);
}
//播放器:video 视频文件; width 宽带; height 高度
function getPlayer(video, width, height) {
	var str;
	//视频文件类型
	var type = video.replace(/.+[\/\.]/g,'').toLowerCase();
	if (type=="flv") str = getFLVPlayer(video, width, height);
	else if (type=="rm" || type=="rmvb") str = getRMPlayer(video, width, height);
	else str = getWMVPlayer(video, width, height);
	
	return str;
}
//播放RM
function getRMPlayer(url, width, height) {
	return "<embed width='"+width+"' height='"+height+"' src='"+url
		+"' autostart='true' type='audio/x-pn-realaudio-plugin' controls='imagewindow,ControlPanel'>";
} 
//播放WMV,mp3
function getWMVPlayer(url, width, height) {
	return "<object align=middle classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95' class=OBJECT id=MediaPlayer width="
		+width+" height="+height+"><param name=ShowStatusBar value=0><param name=Filename value='"
		+url
		+"'><embed type=application/x-oleobject codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715'></embed></object>";
}
//播放flv
function getFLVPlayer(url, width, height) {
	return "<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 width="
		+width+" height="+height+" classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000>"
		+"<PARAM NAME='quality' VALUE='high'><PARAM NAME='allowFullScreen' VALUE='true'><PARAM NAME='movie' VALUE='http://www.luie.cn/flash/flv.swf'>"
		+"<PARAM NAME='FlashVars' VALUE='vcastr_file="+url+"&BarColor=0xFF6600&BarPosition=1&IsShowBar=1&IsAutoPlay=1'>"
		+"<embed src='http://www.luie.cn/flash/flv.swf' flashvars='vcastr_file="+url+"&BarColor=0xFF6600&BarPosition=1&IsShowBar=1&IsAutoPlay=1' width='"
		+width+"' height='"+height
		+"' pluginspage='http://www.macromedia.com/go/getflashplayer' quality='high' allowfullscreen='true' type='application/x-shockwave-flash'></embed></OBJECT>"; 
}
/********** Ajax end ***************/
/**
 * 调整图片比例
 */
function changeRate(obj,width,height){
	var img=new Image();
	img.src = obj.src;
	var rate = height/width;
	var imgRate = img.height/img.width;
	if(img.height<height && img.width<width){
		obj.width = img.width;
		obj.height = img.height;
		return;
	}
	if(rate>imgRate){
		obj.width = width;
		return;
	}
	if(rate<imgRate){
		obj.height = height;
		return;
	}

}
function changeCss(obj,width,height){
	alert(width+"\t"+height);
	obj.style.width = "'"+parseInt(width)+20+"'";
	obj.style.height = "'"+parseInt(height)+40+"'";
}
/**
 * 创建iframe
 * @param src
 * @param width
 * @param height
 * @return
 */
function addIframe(src,pic,width,height){
	if (src=="") {
		str = "<img src='"+pic+"' onload='changeRate(this,"+width+","+height+")' >";
		document.write(str);
		return;
	}
	src = src + "&dateb="+new Date();
	var oIframe = "<iframe src='"+src+"' width ='"+width+"' height='"+height+"' frameborder='0'></iframe>";
	//var oIframe = document.createElement("iframe");
//	oIframe.src = src;
//	oIframe.width = width;
//	oIframe.height = height;
//	oIframe.setAttribute("frameborder", "0", 0);
//	document.body.appendChild(oIframe);
	document.write(oIframe);
}
