var objAjax=new JAjax();
function createNewObject(){
	return function(){
		this.loading.apply(this,arguments);	
	}
}
var intObject=null;
var wxfileid=-1;
function loadIntObject(obj){	
     intObject=new createIntObejct(obj);
}
var createIntObejct=new createNewObject();

createIntObejct.prototype.loading=function(obj){
	 this.cBodyTitle=new Array("返点方式","购物获得积分","购物获得幸运币","购物获得购物劵","购物获得购物劵有效时间","购物介绍人获得积分","购物介绍人获得幸运币","购物介绍人获得购物劵","介绍人获得购物劵有效时间");
	 this.cBodyKey=new Array("ratioType","hisIntegral","hisMoney","hisShopShould","hisTime","inIntegral","inMoney","inShopShould","inTime");
	 this.addDivObject=obj;
	 this.cTitle=this.createBody(this.addDivObject,"比例名称","99%");
	 this.cBody=this.createBody(this.addDivObject,"比例设置","99%");
	 this.createSel();
	 this.createBodyMethod();
	 try{
		if(fileid>0){wxfileid=fileid;}
		else{wxfileid=0;	}
	 }catch(e){
		if(pType=="modi"){wxfileid=-2;}
		else{wxfileid=-1;}
	 }
}

//创建外围框
createIntObejct.prototype.createBody=function(obj,title, width){
	 var tempObj=document.createElement("div");
	 var div=obj.appendChild(tempObj);
	 div.className="PowerList";
	 if (width!=null){div.style.width=width;}		
	 tempObj=document.createElement("fieldset");
	 var addObj=div.appendChild(tempObj);
	 tempObj=document.createElement("legend");
	 var addText=addObj.appendChild(tempObj);
	 addText.innerText=title;
	 tempObj=document.createElement("div");
	 var addDiv=addObj.appendChild(tempObj);
	 addDiv.innerHTML="";
	 addDiv.className="PowerListOption";
	 return addDiv;
}

createIntObejct.prototype.getNowRatioName=function(menuid,typeid){
	var objResponse=new JHttpResponse();
	objResponse.write("menuid",menuid);
	objResponse.write("isMenu",wxfileid);
	var out=objAjax.getText("post","../../../wxProgram/server/wx_intRatio.aspx?type=readIntName",objResponse,null);
	var selRatioNameLst=$("selRatioNameLst");
	if(out=="默认配置"||out.indexOf("_")>-1||out==""||out==null){
	if(out==null||out==""){
		selRatioNameLst.options[0].selected="selected";
		this.loadInt("-1");
	  }else{
		for(var i=0;i<selRatioNameLst.options.length;i++){
			if(selRatioNameLst.options[i].value==out){
				selRatioNameLst.options[i].selected="selected";	
				break;
			}
		}
		this.loadInt(out);
	  }
	  $("intNameChange").value=selRatioNameLst.options[selRatioNameLst.selectedIndex].innerHTML;
	}
}

//创建选项设计器
createIntObejct.prototype.createSel=function(){
	var tempObj=document.createElement("span");
	tempObj.innerHTML="加载配置:";
	this.cTitle.appendChild(tempObj);
	var out=objAjax.getText("get","../../../wxProgram/server/wx_intRatio.aspx?type=readUserIntLst",null,null);
	if(out==""||out==null||out.indexOf(c1)>-1||out.indexOf("_")>-1){
		tempObj=document.createElement("select");
		tempObj.setAttribute("id","selRatioNameLst")
		tempObj.options.add(new Option("默认配置","-1"));
		if(out.indexOf("_")>-1){
			out=out.split(c1);
			for (var i=0;i<out.length;i++){
				 tempObj.options.add(new Option(out[i],out[i]));
			}
			tempObj.onchange=function(){
				intObject.loadInt(this.options[this.selectedIndex].value);
				$("intNameChange").value=this.options[this.selectedIndex].innerHTML;
			}
		}
		this.cTitle.appendChild(tempObj);
		tempObj=document.createElement("input");
		tempObj.setAttribute("name","intNameChange");
		tempObj.setAttribute("id","intNameChange");
		tempObj.setAttribute("isV","1");
		tempObj.setAttribute("notNull","1");
		tempObj.setAttribute("max","100");
		tempObj.setAttribute("min","2");
		tempObj.setAttribute("title","积分规则配置名称");
		tempObj.setAttribute("value","默认配置");
		tempObj.setAttribute("readOnly","true");
		this.cTitle.appendChild(tempObj);	
		tempObj=document.createElement("span");
		var html="";
		try{
			if(fileid>0){html="";}
		}catch(e){
			html="<input type='checkbox' checked value='1' id='setDownMenuInt' name='setDownMenuInt'><label for ='setDownMenuInt'>应用到子栏目</label>";		
			html=html+"<span style='width:15px'></span><input title='设置当前栏目下的数据使用当前选择的返点比例' onclick='intObject.downInt()' type='button' value='应用到当前栏目下数据'>"	
		}
		tempObj.innerHTML=html;	
		this.cTitle.appendChild(tempObj);
	}
}
createIntObejct.prototype.downInt=function(){
	var v=$("selRatioNameLst").options[$("selRatioNameLst").selectedIndex].innerHTML;
	if(parseInt(wxfileid)==-2){
		var objResponse=new JHttpResponse();
		objResponse.write("menuid",menuid);
		objResponse.write("v",v);
		var out=objAjax.getText("post","../../../wxProgram/server/wx_intRatio.aspx?type=dwnIntMsg",objResponse,null);
		if(out=="ok"){
			alert("设置成功");	
		}else{
			alert(out);	
		}
	}else{
		alert("你当前正在创建新的栏目,当前栏目下还没有任何数据");
	}
}

//载入当前选择的内容加载
createIntObejct.prototype.loadInt=function(obj){
	if(obj==-1){
		this.createBodyMethod();
	}else{
		var objResponse=new JHttpResponse();
		objResponse.write("vName",obj);
		var out=objAjax.getText("post","../../../wxProgram/server/wx_intRatio.aspx?type=readIntMsg",objResponse,null);
		if(out.indexOf(c1)>-1){
			this.createBodyMethod(out.split(c1));
		}else{
			if(out==null||out==""){
				this.loadInt("-1");
			}
		}
	}
}

//创建要显示的控件,并且填充值
createIntObejct.prototype.createBodyMethod=function(){
	var vArray=new Array("0","0","0","0","24","0","0","0","24");
	if(arguments.length==1){
		vArray=arguments[0];
	}	
	var tab=document.createElement("table");
	tab.setAttribute("border","0");
	tab.setAttribute("cellPadding","0");
	tab.setAttribute("cellSpacing","0");
	tab.style.cssText="border-top:solid 1px #c0c0c0;margin-bottom:5px;"
	for(var i=0;i<vArray.length;i++){
		var tr=tab.insertRow(-1);
		var td=tr.insertCell(-1);
		td.style.cssText="width:180px;text-align:right;border:solid 1px #c0c0c0;border-top:none;";
		td.innerHTML=this.cBodyTitle[i]+"：";
		var cTxt=document.createElement("input"); 
		td=tr.insertCell(-1);
		td.style.cssText="border:solid 1px #c0c0c0;border-left:none;width:300px;border-top:none;"
		if(i==0){
			if(vArray[0]==1){
				td.innerHTML="<input type='radio' name='ratioType' value='0' id='r-1'><label for='r-1'>价格比例关系</label><input type='radio' checked name='ratioType' id='r-2' value='1'><label for='r-2'>实际设置值</label>";
			}else{
				td.innerHTML="<input type='radio' checked name='ratioType' id='r-1' value='0'><label for='r-1'>价格比例关系</label><input type='radio' name='ratioType' id='r-2' value='1'><label for='r-2'>实际设置值</label>";
			}
		}else{
			cTxt.setAttribute("type","text");
			cTxt.setAttribute("value",vArray[i]);
			cTxt.setAttribute("name",this.cBodyKey[i]);
			cTxt.setAttribute("id",this.cBodyKey[i]);
			cTxt.setAttribute("isV","1");
			cTxt.setAttribute("isNum","1");
			cTxt.setAttribute("notNull","1");
			cTxt.setAttribute("title",this.cBodyTitle[i]);
			cTxt.setAttribute("size","3");
			cTxt.style.cssText="border:solid 1px #cad6ea;width:180px;";
			cTxt.onchange=function(){
				if(!isNaN(this.value)){
					try{
						if(fileid){
							$("intNameChange").value=menuid+"_"+fileid+"_"+asTab;
						}
					}catch(e){
						$("intNameChange").value=menuid+"_"+asTab;
					}
				}else{
					this.value="0";	
					this.focus();
	        		alert("只能填写数字");
				}
			}
			td.appendChild(cTxt);
		}
	}
	var cForm=document.createElement("form");
	cForm.setAttribute("id","cForm");
	cForm.appendChild(tab);
	this.cBody.innerHTML="";
	this.cBody.appendChild(cForm);
}

//保存当前的数据
createIntObejct.prototype.saveDate=function(){//保存数据
	var objResponse=new JHttpResponse();
	var cf=cForm;
	for(var i=0;i<cf.elements.length;i++){
		if(cf.elements[i].type.toLowerCase()=="text"){
			var v=cf.elements[i].value;
			if(!isNaN(v)){
				objResponse.write(cf.elements[i].id,v);	
			}else{
				alert("请填写数字");
				cf.elements[i].focus();
				return;
			}
		}
	}
	var ratioType=document.getElementsByName("ratioType");
	for(var j=0;j<ratioType.length;j++){
		if(ratioType[j].checked){
			objResponse.write("ratioType",ratioType[j].value);
		}
	}
	try{
	if($("setDownMenuInt").checked){objResponse.write("setDownMenuInt","1");}else{objResponse.write("setDownMenuInt","0");}
	}catch(e){objResponse.write("setDownMenuInt","0");}
	if($("intNameChange").value==null||$("intNameChange").value==""){
		alert("请设置返点比例");
		return ;
	}
	var hisTime=$("hisTime").value
	var inTime=$("inTime").value
	var reg=/[1-9][0-9]*/
	if(!reg.test(hisTime)){
		alert($("hisTime").title+"格式不正确,请填写大于0的整数");
		$("hisTime").focus();
		return;
	}
	if(!reg.test(inTime)){
		alert($("inTime").title+"格式不正确,请填写大于0的整数");
		$("inTime").focus();
		return;
	}
	objResponse.write("ratioName",$("intNameChange").value);
	objResponse.write("menuid",menuid);
	objResponse.write("isMenu",wxfileid);		
	var out=objAjax.getText("post","../../../wxProgram/server/wx_intRatio.aspx?type=saveIntMsg",objResponse,null);
	switch(out){
		case "-1":alert("非法参数");break;
	}
	return true;
}