﻿//提取Cookies变量的值
function GetCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}

//保存Cookies变量的值
function SetCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}



// 根据鼠标或焦点事件，设置文本框和按钮样式
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";
	}else{
		onStyle = "input_on";
		offStyle = "input";
	}
	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;
	}
}

// 展开与关闭对象
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++){
	   if ($(name+i).className!="hidden"){
		$(name + i).className = (i==cursel) ? "hover" : "";
		$("con_" + name + "_" + i).style.display = (i==cursel) ? "block" : "none";
		}
	}
}

// 全选复选框
function Selectallcheckbox(sForm, sObj, sId, sText) {
    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 (sId == "" || sId == null) sId = 'id';
        if ( e.name.toLowerCase() == sId && e.type.toLowerCase() == 'checkbox') e.checked = obj.checked;
    }
    if(obj.checked) {
        obj.title = "取消全选";
    } else {
        obj.title = "全选";
    }
    if (sText == "" || sText == null) sText = 'selectalltext';
    if($(sText)){
        if(obj.checked) {
            $(sText).innerText = "取消全选";
        } else {
            $(sText).innerText = "全选";
        }
    }
}

// 分页
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;
}

// 后台主页菜单
function ShowAdminMenu(ts,div,off,on,id,tag){
    var ts=ts.id;
    var num=ts.match(/\d+/ig).toString();
    var btn=ts.replace(num,'');
	var div=div;
	var off=off;
	var on=on;
	var id=id;
	var tag=tag;
	var total=1;			
	var c = $(id).getElementsByTagName(tag);
    for(var i = 0;i < c.length;i++){
      total++;
    }	
    var b = document.getElementsByTagName("*")
	for (j=1; j<=total; j++){
	    if($(btn+j)!=null){
	        b[btn+j].className=off;
	    }
        if(div!=""){
	        if($(div+j)!=null){
                b[div+j].style.display="none";
            }
        }
        if (j==num){
            if($(btn+j)!=null){
    		    b[btn+j].className=on;
            }
            if(div!=""){
                if($(div+j)!=null){
                    b[div+j].style.display="block";
                }
            }
        }
	}			
}

function ShowMainMenu(ts){
	ShowAdminMenu(ts,'','','menu_on','menu','li');
}

function ShowLeftMenu(ts){
	 ShowAdminMenu(ts,'lm','','sidebar_on','sidebar','ul');
}

// 上传文件
function Uploadfile(id, type){
	//Xl.ShowMsg({ Type : "iframe", Title : "上传文件", Content : { IframeName : "Uploadfile", Url : "../system/upload.aspx?ctrlid=" + id + "&type=" + type, Style : "width:338px;height:68px" }, IsClose : true,IsModeless:false });
    Comment("上传文件","../system/upload.aspx?ctrlid=" + id + "&type=" + type,400,100)
}

//浮动显示图片 pic:图片的路径,width:图片宽度,height:图片高度
//function ShowPIC(pic,width,height){
//	Xl.ShowMsg({ Type : "iframe", Title : "上传文件", Content : { IframeName : "Uploadfile", Url : pic, Style : "width:"+width+"px;height:"+height+"px" }, IsClose : true });
//}

// 浮动提示  title:标题  ShowUrl:提示页面
function ShowMessage(title,ShowUrl,width,height){
    ShowUrl=encodeURI(ShowUrl);
    
	//Xl.ShowMsg({ Type : "iframe", Title :""+title, Content : { IframeName : "ShowMessage", Url:""+ShowUrl ,Style: "width:"+width+"px;height:"+height+"px" }, IsCancel : true});
    Comment(title,ShowUrl,width,height);

}

// 浮动提示  title:标题  ShowUrl:提示页面
//function AlertMessage(title,Message,width,height){
//	Xl.ShowMsg({ Type : "prompt", Title :""+title, Content : { IframeName : "ShowMessage", Url:""+ShowUrl ,Style: "width:"+width+"px;height:"+height+"px" }, IsCancel : true});
//}


// 显示邮件内容替换帮助信息
function EmailHelp(){
	Xl.ShowMsg({ Type : "iframe", Title : "邮件内容替换帮助信息", Content : { IframeName : "EmailHelp", Url : "help.htm", Style : "width:350px;height:195px" }, IsClose : true });
}

// 检测文件上传
function chkPost_upload(sForm){
    var allowTypes = $('allowTypes').value;
    var filepath = $('uploadfile').value;
    if (filepath == ""){
        alert("请选择要上传的文件！");
        return false;
    }
    var extension = filepath.substr(filepath.lastIndexOf(".")).toLowerCase();
    if (extension == ".exe"){
        alert("请不要上传可执行程序文件！");
        return false;
    }
    var typeArray = allowTypes.split("|");
    var isFoundType = false;
    for (var i = 0; i < typeArray.length; i++){ 
        if (extension == typeArray[i].toLowerCase()){
            isFoundType = true;
            break;
        }
    }
    if (!isFoundType){
        alert("允许上传的文件类型为：" + typeArray.join(" ") + "！");
        return false;
    }
    return true;
}

// 检测论坛版面的提交表单
function chkPost_bbs_board(){
    var f = document.forms[0];
    if (f.Name.value.replace(/(^\s*)|(\s*$)/g, "") == ""){
        alert("没有填写版面名称！");
        f.Name.focus();
        return false;
    }
    if (f.Description && f.Description.value.length > 250){
        alert("说明内容长度不能大于250字节，请截去一部分再试！");
        f.Description.focus();
        return false;
    }
    if ($('allowbid').value!="" && $('allowbid').value.indexOf(f.Parentid.options[f.Parentid.selectedIndex].value) < 0){
    	alert("所属版面只能选择一级版面或留空！");
        f.Parentid.options[0].selected = true;
        return false;
    }
}

// 检测论坛广告的提交表单
function chkPost_bbs_ads(){
    var f = document.forms[0];
    for (var i = 0; i < f.elements.length; i++){
        var element = f.elements[i];
        if(element.value.toLowerCase().indexOf("swf") != -1){
            if (element.value.substr(element.value.lastIndexOf(".") + 1).toLowerCase() == "swf"){
                var num = element.name.substr(element.name.length - 1);
                if($('adlinkurl' + num).value != "-"){
                    $('adlinkurl' + num).value = "-";
                }
            }
        }
    }
    return confirm('您确定要修改论坛的头部广告设置吗？');
}

// 检测论坛公告的提交表单
function chkPost_bbs_addannounce(){
    var f = document.forms[0];
    if (f.subject.value.replace(/(^\s*)|(\s*$)/g, "") == ""){
        alert("没有填写公告名称！");
        f.subject.focus();
        return false;
    }
    if (f.issuer.value.replace(/(^\s*)|(\s*$)/g, "") == ""){
        alert("没有填写公告发布人！");
        f.issuer.focus();
        return false;
    }
    if (f.content.value.replace(/(^\s*)|(\s*$)/g, "") == ""){
        alert("没有填写公告内容！");
        f.content.focus();
        return false;
    }
    if (f.content && f.content.value.length > 1000){
        alert("公告内容长度不能大于1000字节，请截去一部分再试！");
        f.content.focus();
        return false;
    }
}

// 处理列表数据
function processing_datalist(obj, form, url){
    var action = obj.options[obj.selectedIndex].value;
    if (action == ''){
        obj.options[0].selected = true;
        return false;
    }
    url = url + "?action=" + action;
    if(action.indexOf('sel') != -1){
	    var f = document.forms[form];
        if(!f) f = eval('document.' + form);
        var idList = '';
        for (var i = f.elements.length; i > 0; i--){
            if (f.elements[i-1].type.toLowerCase() == "checkbox" && f.elements[i-1].name.toLowerCase() != "selectall"){
	            if (f.elements[i-1].checked){
		            idList += f.elements[i-1].value.toString() + ",";
	            }
            }
        }
        if (idList == ''){
            alert('没有选择任何记录行！');
            obj.options[0].selected = true;
            return false;
        }
        idList = idList.substring(0, idList.length - 1);
        url += "&id=" + escape(idList);            
    }
    if(!confirm('你确定执行此项操作吗？')){
        obj.options[0].selected = true;
        return false;
    }else{
        location.href = url;
    }
}

// 检测答题管理中的添加题目表单
function chkPost_answer_addsubject() {
    var sForm = document.forms[0];
    var o = 0;
    var s = 0;
    for (var i = 0; i < sForm.elements.length; i++) {
        var e = sForm.elements[i];
        if (e.name.indexOf('S_Option') != -1 && e.type == 'text' && e.value != '') {
            o = o + 1;
        }
        if (e.name.indexOf('S_Score') != -1 && e.type == 'text' && e.value != '') {
            s = s + 1;
        }
    }
    if(sForm.S_Description.value!="" && sForm.S_Option1.value!="" && sForm.S_Option2.value!="") {
        if( s != 0 && s != o ) {
            alert("请对应填写好选项内容与分数，分数可为空！");
            return false;
        }else{
            if(sForm.S_ID.value=="") {
                sForm.action="?action=add_subject&sid="+sForm.S_ClassID.value;
            }else{
                sForm.action="?action=update_subject&sid="+sForm.S_ClassID.value;
            }
            sForm.submit();
        }
    }else{
        alert("请至少填写题目内容与两个选项！");
        sForm.S_Description.focus();
        return false;
    }
}

// 检测答题管理中的添加试题组表单
function chkPost_answer_addtest() {
    var sForm = document.forms[0];
    if(sForm.A_Name.value!="") {
        if(sForm.A_ID.value=="") {
            sForm.action="?action=add_test";
        }else{
            sForm.action="?action=update_test";
        }
        sForm.submit();
    }else{
        alert("请把试题名称与试题答案填写完善！");
        sForm.A_Name.focus();
        return false;
    }
}

// 执行答题管理中删除选择的项目
function exec_answer_delsel(url, sName) {
    var sForm = document.forms[0];
    var j = 0;
    for (var i = 0; i < sForm.elements.length; i++) {
        var e = sForm.elements[i];
        if ( e.name == 'id' && e.type == 'checkbox' && e.checked) {
            j = j + 1;
        }
    }
    if(j > 0) {
        if(confirm('你确认要删除所选择的项目吗？'))
        {
            sForm.action = url;
            sForm.submit();
        }else{
            return false;
        }
    }else{
        alert("请至少选择一项你需要删除的项目！");
        return false;
    }
}

// 会员与交易统计的URL跳转
function gotoUrl_meStat(obj){
    var url = document.location.href;
    if(url.indexOf('&uid=') == -1){
        url += "?show=day&uid=" + obj.options[obj.selectedIndex].value;
    }else{
        url = url.substring(0, url.lastIndexOf("&uid="));
        url += "&uid=" + obj.options[obj.selectedIndex].value;
    }
    window.location.href = url;
}

// 检测检测基本广告分组管理中的表单
function chkPost_adgroup() {
    var sForm = document.forms[0];
    if(sForm.gname.value!="") {
        if(sForm.gid.value=="") {
            sForm.action="?action=saveaddgroup";
        }else{
            sForm.action="?action=saveeditgroup";
        }
        sForm.submit();
    }else{
        alert("表单填写不完善，请重示！");
        sForm.gname.focus();
        return false;
    }
}

// 检测检测会员等级管理中的表单
function chkPost_usergrade(){
    var f = document.forms[0];
    if (f.name.value.replace(/(^\s*)|(\s*$)/g, "") == ""){
        alert("没有填写等级称呼！");
        f.name.focus();
        return false;
    }
    if (f.integral.value.search(/^\d+$/gi) < 0){
        alert("没有填写等级所需积分！");
        f.integral.focus();
        return false;
    }
}

// 通过Ajax获取数据或执行程序
// 参数:存放结果的span,发送的url,参数类别,参数,其他参数,返结果回方式,返回参数,状态span,完成结果（即spanId对应多少才表示完成），完成显示
function GetOfAjax(spanId,sendUrl,paramType,param,otherParam,returnType,returnParam,stateSpanId,finishResult,finishMessage){ 
	var xmlhttp;
	var si=document.getElementById(spanId);
	var stateSpan=document.getElementById(stateSpanId);
	try{
   		xmlhttp=new XMLHttpRequest();
   	}
	catch(e){
   		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   	}
	xmlhttp.onreadystatechange=function(){
	if (xmlhttp.readyState==4){
   		if (xmlhttp.status==200){
		switch(returnType){
			case "addnum"://加returnParam中的值
				si.innerHTML=parseInt(si.innerHTML)+parseInt(returnParam);break;
			case "adddata"://加目标网页返回的数据
				si.innerHTML=parseInt(si.innerHTML)+parseInt(xmlhttp.responseText);break;
			case "data"://返回目标网页返回的数据
				si.innerHTML=xmlhttp.responseText;break;
		}
		if (finishResult==si.innerHTML) stateSpan.innerHTML=finishMessage;
		
    	}
   		else{
    		si.innerHTML="Error ("+xmlhttp.status+"):"+ paramType;// +"<br>"+ xmlhttp.responseText;
		//return false;
    	}
   	}
	else{
   		stateSpan.innerHTML="<img src=\"/images/loadingCircle.gif\" border=\"0\">";
   	}
	
	}
	xmlhttp.open("get",sendUrl+"?ParamType="+paramType+"&param="+param+"&"+otherParam);
	xmlhttp.send(null);
}


//ajax:读取数据页的Ajax函数,提供给与Web.DataAjax一起使用
function DataOfAjax(spanId,paramType,param,otherParam){ 
	var xmlhttp;
	var si=document.getElementById(spanId);
	try{
   		xmlhttp=new XMLHttpRequest();
   	}
	catch(e){
   		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   	}
	xmlhttp.onreadystatechange=function(){
	if (xmlhttp.readyState==4){
   		if (xmlhttp.status==200){
    		si.innerHTML=xmlhttp.responseText;
    	}
   		else{
    		si.innerHTML="Error ("+xmlhttp.status+"):"+ paramType;// +"<br>"+ xmlhttp.responseText;
    	}
   	}
	else{
   		si.innerHTML="<img src=\"/images/loadingCircle.gif\" border=\"0\">";
   	}
	
	}
	xmlhttp.open("get", "/dataOfAjax.aspx?ParamType="+paramType+"&param="+param+"&"+otherParam);
	xmlhttp.send(null);
	//xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	//xmlhttp.send("paramType=product.pv"+escape(paramType)+"&param="+param);
}

//网页加载后执行
function OnLoadEvent(func) 
{ 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
		window.onload = func; 
	} else { 
		window.onload = function() { 
		oldonload(); 
		func(); 
		} 
	} 
} 

//从价格组中提取销售价格
function SalesPrice(priceGroup)
{
	var strs= new Array(); 
	strs=priceGroup.split("|"); 
	if (GetCookie("MemberGrade")==null || GetCookie("MemberGrade")=="")
	return strs[0];
	else return strs[GetCookie("MemberGrade")];
}



/**正侧验证,WanCome.Web.SubInput中使用**/
function VerifyRegExpObj(obj,RegExp){
    //if (obj.value.replace(RegExp, "") == ""){
    //    return false;
    //}else return true;
    return RegExp.test(obj.value);
}

/**验证,WanCome.Web.SubInput中使用**/
function VerifyObj(obj,VerifyType){
	recheck=false;
	switch(VerifyType)
	{
 	  case "email":{                     
		recheck=VerifyRegExpObj(obj,/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/);
		break;}
	  case "float":{
		recheck=VerifyRegExpObj(obj,/^[1-9][0-9]*\.[0-9]+$/);
		break;}
	  case "notnull":{
		if (obj.value.replace("(^\s*)|(\s*$)/g","")=="") recheck=false;
		else recheck=true;
		break;}
	  case "chinese":
		recheck=VerifyRegExpObj(obj,/^[\u0391-\uFFE5]+$/);
		break;
	  case "qq":
		recheck=VerifyRegExpObj(obj,/^[1-9]\d{4,9}$/);
		break;
	  case "zip":
		recheck=VerifyRegExpObj(obj,/^\d{6}$/);
		break;
	  case "id":
		recheck=VerifyRegExpObj(obj,/^\d{15}|\d{18}$/);
		break;
	  case "mobile":
		recheck=VerifyRegExpObj(obj,/^((\+86)|(86))?(13[0-9]|15[0|2|3|6|7|8|9]|18[0-9])\d{8}$/);
		break;
	  case "tel":
		recheck=VerifyRegExpObj(obj,/^[0-9]{3,4}\-\d{7,8}(\(\d{1,6}\))?$/);
		break;
	  case "url":
		recheck=VerifyRegExpObj(obj,/^(http\:\/\/)?([a-z0-9][a-z0-9\-]+\.)?[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+(\/[a-z0-9\.\,\-\_\%\?\=\&]?)?$/i);
		break;
	}
	return recheck;
} 


/**** 获取鼠标当前XY *******/
var mousePos;
document.onmousemove = mouseMove;
function mouseMove(ev){
	ev=ev || window.event;
	mousePos = mouseCoords(ev);  
}   
function mouseCoords(ev){ 
      var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0; 

      if(ev.pageX || ev.pageY){
          return {x:ev.pageX, y:ev.pageY};
       }
      return {
        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
	y:ev.clientY +scrollTop - document.body.clientTop
      };
}  
