// JavaScript Document
var CMS=new Object();//CMS对象
//选择自身
CMS.select=function(el){
    var boxs=el.getElementsByTagName("INPUT"); 
	for(var i=0;i<boxs.length;i++){
        if(boxs[i].type!='checkbox')continue; 
	    if(!boxs[i].checked)boxs[i].checked=true;
	    else boxs[i].checked=false;
    }
};
//选择所有
CMS.selectAll=function(el){
    var boxs=document.getElementsByName(el.name); 
	for(var i=0;i<boxs.length;i++){
		if(el.checked)boxs[i].checked=true;
	    if(!el.checked)boxs[i].checked=false;
    }
};
//CMS获取请求信息
CMS.getRequest=function(){
   var request={};var href=location.href;
   href=href.substring(href.lastIndexOf("/")+1,href.lastIndexOf("."));
   var start=href.indexOf("_");
   if(start==-1)return request;
   var ps=href.split("_");
   for(var i=0;i<ps.length;i++){
      var s=ps[i].indexOf("-");
	  if(s==-1)continue;
      var n=ps[i].substring(0,s);
	  var v=ps[i].substring(s+1);
	  n=n.toLowerCase();
	  request[n]=v;
   }
   return request;
};
//获取CheckBox的值
CMS.getCheckBoxs=function(name,el){
   if(!el)el=document;el=$L.el(el); var boxs=el.getElementsByTagName("INPUT"); var values="";
   for(var i=0;i<boxs.length;i++){
      if(boxs[i].type!='checkbox')continue;
	  if(boxs[i].name!=name)continue;
	  if(!boxs[i].checked)continue;
      if(values.length>0)values+=",";
	  values+=boxs[i].value;
   }
   return values;
}





//打开一个链接
CMS.openUrl=function(url){
   var win=$L.ui.getWindow(url,true);
};
//保存信息
CMS.saveInfo=function(action,ps,callBack){
	if(!action){alert("请设置action！");return;}ps.action=action;  
	$L.Ajax.getResult("com.ogw.cms.core.SaveInfo",ps,function(msg){    
		 if(msg=='error.500'){alert("对不起，您没有权限操作!");return;}
		 if(msg=='ok')callBack(true);
	});
};
//修改信息
CMS.updateInfo=function(action,ps,callBack){ 
    if(!action){alert("请设置action！");return;}ps.action=action;  
	$L.Ajax.getResult("com.ogw.cms.core.UpdateInfo",ps,function(msg){   
		 if(msg=='error.500'){alert("对不起，您没有权限操作!");return;}
		 if(msg=='ok')callBack(true); 
	});
};
//获取信息
CMS.getInfo=function(key,action,callback){
	 if(!action){alert("请设置action！");return;} 
     var ps={'key':key,'action':action};
	 $L.Ajax.getResult("com.ogw.cms.core.GetInfo",ps,function(msg){
		 if(msg=='error.500'){alert("您没有该权限!");return;}
		 if(msg=='error.db'){alert("数据库查询错误!");return;} 
		 callback($L.tranceStore(msg));
	 });
}; 
//删除新闻
CMS.deleteInfo=function(key,action,callback){
     var ps={'key':key,'action':action};
	 $L.Ajax.getResult("com.ogw.cms.core.DeleteInfo",ps,function(msg){
		 if(msg=='error.500'){alert("您没有该权限!");return;}
		 if(msg!='ok'){alert(msg);return;}
		 if(callback)callback();else{$L.refresh();}
	 });
};
//删除信息
CMS.del=function (name,label,action,maxLen){
	   if(!maxLen)maxLen=5;
	   var values=CMS.getCheckBoxs(name);
	   if(values==''){alert("请选择您要删除的"+label+"!");return;}
	   if(values.split(",").length>maxLen){alert("对不起，一次最多只能删除"+maxLen+"条数据!");return;}
	   var rs=confirm("您确定要删除这些数据吗？");if(!rs)return;
	   CMS.deleteInfo(values,action,function(){
	       $L.refresh();
	   });
}; 
//修改信息
CMS.execute=function(action,keys,handle){
     if(!action){alert("请传入控制器!");return;}
	 if(!keys){alert("请输入关键字!");return;}
	 $L.Ajax.getResult("com.ogw.cms.core.Execute",{'action':action,'keys':keys},function(msg){
		 if(msg=='error.500'){alert("您没有该权限!");return;}
		 if(msg=='error.db'){alert("数据库异常错误!");return;}
		 if(msg=='ok'){if(handle){handle();return;};$L.refresh();return;}
		 alert("对不起修改失败!【"+msg+"】");
	 });
};
//改变状态
CMS.updateStatu=function(name,label,action,maxLen,handle){
     if(!maxLen)maxLen=5;
	 var values=CMS.getCheckBoxs(name);
	 if(values==''){alert("请选择您要修改的"+label+"!");return;}
	 if(values.split(",").length>maxLen){alert("对不起，一次最多只能修改"+maxLen+"条数据!");return;}
	 this.execute(action,values,handle);
}
//跳转修改页面
CMS.goToUpdate=function(name,url,isSelf){
     var values=this.getCheckBoxs(name);
	 if(values==''){alert("请选择您要修改的数据!");return;}
	 if(values.split(",").length>1){alert("对不起，一次最多只能1条数据!");return;}
	 url+=values+".htm";if(!isSelf)this.openUrl(url);else location.href=url;
}; 
//用户注册
CMS.reg=function(form,handle){
    var form=$L.getForm(form);
	if(!/^[a-zA-Z0-9_]{5,16}$/.test(form.account)){if(handle){handle('account');return;}alert("请正确输入用户帐号!");return;}
	if(!/^[a-zA-Z0-9]{5,20}$/.test(form.psw)){if(handle){handle('psw');return;}alert("请正确输入密码!");return;}
	if(form.psw1!=form.psw){if(handle){handle('psw1');return;}alert("密码两次输入不相等!");return;}
	if(!/^[a-zA-Z0-9_]{4,5}$/.test(form.code)){if(handle){handle('code');return;}alert("请输入验证码4位数!");return;}
    $L.Ajax.getResult("com.ogw.cms.ajax.user.RegUser",form,function(msg){    
		  if(msg=='error.psw1'){if(handle){handle('psw1');return;}alert("密码两次不一样!");return;}
		  if(msg=='error.psw'){if(handle){handle('psw');return;}alert("密码错误!");return;}
		  if(msg=='error.account'){if(handle){handle('account');return;}alert("用户名不合法!");return;}
		  if(msg=='error.code'){if(handle){handle('code');return;}alert("验证码不正确!");return;}
		  if(msg=='error.db'){alert("数据库错误!");return;}
		  if(msg=='error.has'){alert("用户名已经存在!");return;}
		  location.href="login.htm";
	});
}
//用户登录
CMS.login=function(formId){
    var form=$L.getForm(formId);
    if(!/^[a-zA-Z0-9_]{5,16}/.test(form.account)){alert("请正确输入登录帐号!");return;}
	if(!form.psw){alert("请输入登录密码!");return;}
    if(!form.code){alert("请输入验证码!");return;}
	$L.Ajax.getResult("com.ogw.cms.ajax.user.LoginAjax",form,function(msg){
		if(msg=='error.out'){alert("外部访问，参数缺少!");return;}
		if(msg=='error.code'){alert("验证码不正确!");return;}
		if(msg=='error.login'){alert("请不要重复登陆!");return;}
		if(msg=='error.404'){alert("登录帐号不存在!");return;}
		if(msg=='error.fb'){alert("对不起该帐号被禁止!");return;}
		if(msg=='error.psw'){alert("对不起，您的密码不对!");return;}
		if(msg=='ok'){$L.refresh();return;}
	});
};
//退出登录
CMS.out=function(){
   $L.Ajax.getResult("com.ogw.cms.ajax.user.OutAjax",{},function(msg){
		 if(msg=='ok'){$L.sendRedirect('/index.htm'); return;}
	});
}; 
//刷新验证码
CMS.refreshCode=function(obj,url){
     url+="&t"+$L.random(10000);
	 obj.src=url;
}
//购买商品
CMS.buy=function(id){
	$L.Ajax.getResult("com.ogw.cms.ajax.user.BuyAjax",{'id':id},function(msg){
		  if(msg=='nologin'){alert('没有登录不能进行操作');$L.sendRedirect('/user/login.htm');return;}
		  if(msg=='error.404'){alert("请不要从外部访问！");return;}
          if(msg=='error.has'){alert("您已经购买该商品！");return;}
          if(msg=='error.no'){alert("对不起，该商品不存在！");return;}
		  if(msg=='error.db'){alert("对不起，数据库操作错误！");return;}
		  alert("产品成功放入购物车！");
    }); 
};
//搜藏商品
CMS.soucang=function(id){
	$L.Ajax.getResult("com.ogw.cms.ajax.user.SouCang",{'id':id},function(msg){
		  if(msg=='nologin'){alert('没有登录不能进行操作');$L.sendRedirect('/user/login.htm');return;}
		  if(msg=='error.404'){alert("请不要从外部访问！");return;}
          if(msg=='error.has'){alert("您已经搜藏该商品！");return;}
          if(msg=='error.no'){alert("对不起，该商品不存在！");return;}
		  if(msg=='error.db'){alert("对不起，数据库操作错误！");return;}
		  alert("收藏成功！");
    }); 
};
//删除购物车
CMS.delCar=function(id){
    var rs=confirm("您确定要从购物车删除该商品吗?");
	if(!rs)return;
	$L.Ajax.getResult("com.ogw.cms.ajax.user.DelCar",{'id':id},function(msg){
		  if(msg=='error.404'){alert("请不要从外部访问！");return;} 
          if(msg=='error.no'){alert("对不起，购物车不存在！");return;}
		  if(msg=='ok'){$L.refresh();}
    });
}
//删除搜藏
CMS.delSoucang=function(id){
    var rs=confirm("您确定要从购物车删除该商品吗?");
	if(!rs)return;
	$L.Ajax.getResult("com.ogw.cms.ajax.user.DelSouCang",{'id':id},function(msg){
		  if(msg=='error.404'){alert("请不要从外部访问！");return;} 
          if(msg=='error.no'){alert("对不起，购物车不存在！");return;}
		  if(msg=='ok'){$L.refresh();}
    });
}








 
