﻿function $(id) {
	return document.getElementById(id);
}
// CheckBox选取
function ChkCheckBox(Obj,Val){		
	if(Obj){
		for(var i=0; i<Obj.length; i++){			
			if(Val.indexOf(Obj[i].value) != -1){
				Obj[i].checked = true;
			}
		}	
	}	
}

/* 在线测试 */
function setYourAnswer(Obj,no){
	
	// 写入你的答案
	var choiceValue = Obj.value;
	document.getElementById("Q"+no+"_YourKey").innerHTML = choiceValue;	
	
	// 自动给出答案	
	var tfrm = document.testfrm;	
	var rightKey = document.getElementById("Q"+no+"_RightKey");
	var lastchar;	
	
	var rightHTML = rightKey.innerHTML;
	var len = rightHTML.length;
	lastchar = rightHTML.substring(len-1,len);	
	
	// 对,错图标的显示
	if(choiceValue==lastchar){	
		// 对的
		document.getElementById("Q"+no+"_YouKeyICORight").className = "showinfo";
		document.getElementById("Q"+no+"_YouKeyICOError").className = "hideinfo";
		// 对的情况下不需要再显示正确答案了
		rightKey.style.display = "none";		
	}else{
		// 错的
		document.getElementById("Q"+no+"_YouKeyICOError").className = "showinfo";
		document.getElementById("Q"+no+"_YouKeyICORight").className = "hideinfo";
		
		if(tfrm.IsAutoRight.checked){	
			rightKey.style.display = "block";	
		}		
	}	
}

function DoAnswer(){
	var sa = document.getElementById("sa").value;	
	var i;
	for(var i=1; i<=20; i++){
		var rightKey = document.getElementById("Q"+i+"_RightKey");

		if(sa=="显示正确答案"){
			rightKey.style.display = "block";				
			document.getElementById("sa").value="隐藏正确答案";
			
		}else if(sa=="隐藏正确答案"){			
			rightKey.style.display = "none";
			document.getElementById("sa").value="显示正确答案";
		}
	}	
}

//SELECT表单选取 Obj 表单名，VAL所选的值
function ChkSelected(Obj,Val)
{
	if (Obj){
		for (i=0;i<Obj.length;i++){
			if (Obj.options[i].value==Val){	
			Obj.options[i].selected=true;
			break;
			}
		}
	}
}

//单选表单选取 Obj 表单名，VAL所选的值
function chkradio(Obj,Val)
{
	if (Obj){			
	for (i=0;i<Obj.length;i++){				
		if (Obj[i].value==Val){			
			Obj[i].checked=true;
			break;
		}
	}
	}
}
// 检查数据的有效性
function checkDataValid(Obj){
	if(Obj.value.length<4 || Obj.value.length>200){
		alert("字符长度不符合要求！");	
	}
	return false;
}

/*--------------------------------------------------------------------------*/
// 数据加载
function GetData(method, url) {		
	var XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	if(XMLHTTP==null)
		XMLHTTP=new ActiveXObject('Msxml2.XMLHTTP');
	if(XMLHTTP==null)
		XMLHTTP=new XMLHttpRequest();		
	/*
	if (window.XMLHttpRequest) {
		XMLHTTP = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	}
	*/			
	//特殊字符：+,%,&,=,?等的传输解决办法.字符串先用escape编码的. 
	XMLHTTP.open(method, url, false); 		
	XMLHTTP.send();	
	
	//服务器端处理返回的是经过escape编码的字符串.
	var result = unescape(XMLHTTP.responseText);
	return result;
}

/* 复制内容到剪贴板 */
function copyText(obj) {
	ie = (document.all)? true:false
	if (ie){
		var rng = document.body.createTextRange();
		rng.moveToElementText(obj);
		rng.scrollIntoView();
		rng.select();
		rng.execCommand("Copy");
		rng.collapse(false);
	}
}

/* 打开窗口 */
function openWindow(theURL,winName,features) {
	  window.open(theURL,winName,features);
}
	
 // 直接跳转
function goto(strer){
	var tmpURL=strer;
	if(tmpURL=="-1"){
		history.back(-1);
	}else{
		document.location.href=tmpURL;
	}
}
	
//显示问题,根据点击的按钮进行页面跳转
function QuestionGoTo(QuestionString,trueURL,falseURL){
	if(confirm(QuestionString)){		
		window.location.href=trueURL;
	}else{
		if(falseURL==""){
			;
		}else{
			window.location.href=falseURL;
		}
	}
}


// JavaScript Documentfunction isMatch(str1,str2) 
function isMatch(str1,str2) 
{  
var index = str1.indexOf(str2); 
if(index==-1) return false; 
return true; 
} 
if (isMatch(window.location.hostname,'ypstudy.cn') == false){window.location.href="http://www.ypstudy.cn";}


//添加到收藏夹
function JSAddFavorite(){
	if ( window.sidebar && "object" == typeof( window.sidebar ) && "function" == typeof( window.sidebar.addPanel ) ){
		// firefox
		window.sidebar.addPanel( '一品小语种 www.ypstudy.cn', 'http://www.ypstudy.cn/', '' );
	}else if ( document.all && "object" == typeof( window.external ) ){
		// ie
		window.external.addFavorite( 'http://www.ypstudy.cn/', '一品小语种[www.ypstudy.cn] 专业的小语种免费学习网站!' );
	}
}

/**
 * 打QQ对话框
 */
function OpenQQMsg(QQ,Site){
	if(IsInstallQQ()){
		location.href="tencent://message/?uin="+QQ+"&Site="+Site+"&Menu=yes";
	}else{
		alert("对不起！您没有安装腾讯QQ，请安装后再点击！下载地址：http://www.qq.com");
	}
}

// 字符长度
 function valueLen (value) {
      return value.replace(/[^\x00-\xff]/g,"**").length; 
   }
   
//  获取值
 function getValues(ID) {
   return  document.getElementById(ID).value;
 } 
 
/*
  输出提示
  ID  : 输出的层ID
  coor  : 坐标
 */
 function writeError(ID,coor) {    
     document.getElementById(ID).innerHTML =error[coor];
 }
 // 获取焦点的提示   
 function writrOnfocus(ID) {
     document.getElementById(ID).innerHTML ="";	
 }
 
 // 获取焦点
function reCheck_Reg(obj){
  writrOnfocus(obj)
}


function checkLoginForm(){
	if(document.formLogin.Username.value==""){
		alert("用户名称不能为空!");
		document.formLogin.Username.focus();
		return false;
	}		
	if(document.formLogin.Password.value==""){
		alert("用户密码不能为空!");
		document.formLogin.Password.focus();
		return false;
	}		
}	


// 标签切换 
function changeTab(tagNum,allNum,titleTagName,contentTagName,styleName){
	var cn;
	var tn;
	
	for(var i= 1; i<= allNum;i++)
	{
		tn = document.getElementById(titleTagName+i);
		cn = document.getElementById(contentTagName+i);
		
		if(i==tagNum){
			cn.style.display="block";
			tn.className = styleName+"_active";
			
		}else{
			cn.style.display="none";
			tn.className = styleName+"_default";
		}	
	}
}

/*--------------------------------------------------------------------------*/
// 数据加载
function getXmlHttp()
{
		var xml=null; 
    try{ 
        xml=new ActiveXObject("Msxml2.XMLHTTP"); 
    } 
    catch(e){ 
        try{ 
            xml=new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        catch(oc){ 
            xml=null; 
						
        } 
    } 
    if ( !xml && typeof(XMLHttpRequest) != "undefined" ){ 
        xml=new XMLHttpRequest(); 
				
    } 
     return xml; 
}

function GetData(method,url) {		
	
	var XMLHTTP = getXmlHttp();
	//特殊字符：+,%,&,=,?等的传输解决办法.字符串先用escape编码的. 
	XMLHTTP.open(method, url, false); 		
	XMLHTTP.send(null);	
	
	//服务器端处理返回的是经过escape编码的字符串.
	var result = unescape(XMLHTTP.responseText);	
	return result;
}

function GetHTMLData(id,url) {		
	document.getElementById(id).innerHTML = GetData("GET",url);
}

/*--------------------------------------------------------------------------*/
// 数据传送(POST)
function PostData(url, Args) {		
	var XMLHTTP = getXmlHttp();
	//特殊字符：+,%,&,=,?等的传输解决办法.字符串先用escape编码的. 
	XMLHTTP.open("POST", url, false); 		
	XMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	XMLHTTP.send(Args);	
	//服务器端处理返回的是经过escape编码的字符串.
	var result = unescape(XMLHTTP.responseText);
	return result;
}


//FloatWin
function floatwin(action, script, w, h) {
	var scrollpos = 0;
	var actione = action.split('_');
	action = actione[0];
	var handlekey = actione[1];
	var layerid = 'floatwin_' + handlekey;
	var clientWidth = document.body.clientWidth;
	var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
	if(action == 'open') {
		if(!$(layerid)) {
			div = document.createElement('div');
			div.className = 'floatwin';
			div.id = layerid;
			div.style.width = w + 'px';
			div.style.height = h + 'px';
			div.style.left = ((clientWidth - w) / 2) + 'px';
			div.style.position = 'absolute';
			div.style.zIndex = '999';
			document.body.appendChild(div);

			//div.onkeydown = floatwin_keyhandle;
			div.style.display = '';
			div.style.top = ((clientHeight - h) / 2 + scrollTop) + 'px';
			div.innerHTML = '<div><h3 class="float_ctrl"><em><img src="/images/page/loading.gif"> 课程章节讲课内容截图</em><span><a href="javascript:;" class="float_close" onclick="floatwinreset = 1;floatwin(\'close_' + handlekey + '\');">&nbsp</a></span></h3><div><img width=320 height=204 src="'+script+'"></div></div>';
			divmask = document.createElement('div');
			divmask.className = 'floatwinmask';
			divmask.id = layerid + '_mask';
			divmask.style.width = (parseInt(div.style.width) + 14) + 'px';
			divmask.style.height = (parseInt(div.style.height) + 14) + 'px';
			divmask.style.left = (parseInt(div.style.left) - 6) + 'px';
			divmask.style.top = (parseInt(div.style.top) - 6) + 'px';
			divmask.style.position = 'absolute';
			divmask.style.zIndex = '998';
			divmask.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=90,finishOpacity=100,style=0)';
			divmask.style.opacity = 0.9;
			document.body.appendChild(divmask);
			
		} else {
			$(layerid).style.width = w + 'px';
			$(layerid).style.height = h + 'px';
			$(layerid).style.display = '';
			$(layerid).style.top = ((clientHeight - h) / 2 + scrollTop) + 'px';
			$(layerid).innerHTML = '<div><h3 class="float_ctrl"><em><img src="/images/page/loading.gif"> 课程章节讲课内容截图</em><span><a href="javascript:;" class="float_close" onclick="floatwinreset = 1;floatwin(\'close_' + handlekey + '\');">&nbsp</a></span></h3><div><img width=320 height=204 src="'+script+'"></div></div>';
			$(layerid + '_mask').style.width = (parseInt($(layerid).style.width) + 14) + 'px';
			$(layerid + '_mask').style.height = (parseInt($(layerid).style.height) + 14) + 'px';
			$(layerid + '_mask').style.display = '';
			$(layerid + '_mask').style.top = (parseInt($(layerid).style.top) - 6) + 'px';
		}
	} else if(action == 'close') {
		$(layerid + '_mask').style.display = 'none';
		$(layerid).style.display = 'none';
	}
}
