var winfm1 = null;
var showCustomForm = function(innerTxt,periodcount,x,y){
	if(winfm1 && $('dialogBoxBG1').style.display != 'none')
	{
		winfm1.close();
		winfm1 = null;
		return;
	}
	winfm1 = new winForm1();
	winfm1.custom1('自定义选号', innerTxt,null,periodcount*25,x,y);
}
var getCustom = function(form_name,type){

        var sBG='<div id="dialogBoxBG1" style="position:absolute;width:100%;top:0px;left:0px;opacity:0.5;filter:Alpha(Opacity=50);z-index:5;"><iframe src="" frameborder="0" style="position:absolute;  top:0px; left:0px; width:100%; height:500px; " ></iframe></div>'
		var oDiv = document.createElement('div');
		oDiv.id = "dialogCase1";
		oDiv.innerHTML = sBG;

		
		if($("dialogBoxBG1")){
		$("dialogBoxBG1").show();
		}else{
		      document.body.appendChild(oDiv);
		    $('dialogBoxBG1').style.height = document.body.scrollHeight;
		}

	var maxPeriodNum;
	var theform = $(form_name);
	var txtPerNum	= theform.periodscount;
	//var startMuti = parseInt(theform.startmultiple1.value);//起始倍数

	if(Config.surperiods<parseInt(txtPerNum.value))
	{
		alert("超过当天的生意运营期数，请重新选择");
		return;
	}
	else
		maxPeriodNum = parseInt(txtPerNum.value);
	
	var thisHTML = "";
	thisHTML = "\
	<div style=\"padding:5px 20px;text-align:left\">追号期数: <input size=\"10\" name=\"periodscount\"  id=\"periodscount\" onkeyup=\"bindData(1);\" onkeypress=\"if(event.keyCode<48||event.keyCode>57)return false;\"  type=\"text\" value=\""+txtPerNum.value+"\"></div>\
	<table width='420' border='0' align='center' cellpadding='0' cellspacing='1' id=\"customform\" bgcolor='#CCCCCC'>\
	<tr bgcolor='#E1E1E1'>\
	<td width='16%' height='17' align=center>中奖出现</td>\
	<td width='13%' align=center>投注倍数</td>\
	<td width='20%' align=center>本期投入</td>\
	<td width='13%' align=center>累积投入</td>"
	if(type){
	thisHTML+="\
		<td width='13%' align=center>中奖奖金</td>\
	<td width='12%' align=center>收益值</td>\
	"
	}
	thisHTML+="</tr>";
	;
	
	var _maxPerNum = 1;
	var perNum=new Array;
	if($("MulitSelfInput").value!=""){
	    perNum=$("MulitSelfInput").value.split(",");
	}
	else{
	    perNum=1;
	}
	//MulitSelfInput
	var totalChipinMoney = 0;
	var agenttype=($("agenttype").value==4?4:1);
	for(var i=0;i<(maxPeriodNum);i++)
	{	
		var currentChipinMoney =  (perNum==1?1:perNum[i]) * 2;
		totalChipinMoney += currentChipinMoney;
		thisHTML += "\
			<tr bgcolor='#FFFFFF'>\
			<td height='20' valign='top' align='center'>第 " + (i+1) + " 期</td>\
			<td valign='top' align='right'><input name=\"pernum\" id=\"pernum_"+i+"\" onkeyup=\"if(isNaN(this.value)) this.value='1';\" onblur=\" bindData("+type+",this)\"  maxlength=\"6\" style='border:0px;width:40px; text-align:right; border-bottom:1px solid #000' type=\"text\"  value='" + (perNum==1?"1":perNum[i]) + "' /></td>\
			<td valign='top' align='right'>" + currentChipinMoney + "</td>\
			<td valign='top' align='right'>" + totalChipinMoney + "</td>";
				if(type){
	                thisHTML+="\
		                <td width='13%' align=center>"+currentChipinMoney*2*agenttype +"</td>\
	                <td width='12%' align=center>"+ (currentChipinMoney*2*agenttype - totalChipinMoney) +"</td>\
	                "
	            }
	            thisHTML+="</tr>";
	}
	thisHTML +=  "</table>";
	thisHTML +="<div style=\"padding:5px;text-align:center\"><input type=\"hidden\" name=\"totalChipinMoney\" id=\"totalChipinMoney\" value=\""+totalChipinMoney+"\"><input type=\"hidden\" name=\"agentnum\" id=\"agentnum\" value=\""+agenttype+"\"><input type=\"button\" value=\"确定\" onclick=\"postData()\"> <input type=\"button\" value=\"放弃\"  onclick=\"QuitData()\"> <input type=\"button\" value=\"关闭\"  onclick=\"closeData()\"></div>"
	return thisHTML;
}
function bindData(type,obj){
    var id="0";
    if(obj) id=obj.id 
   bindHtml(type,id); 
    //setTimeout("bindHtml("+type+",'"+id+"')",1000)
}
function bindHtml(type,obj){
	var inputs=$("customform").getElementsByTagName("INPUT");
	var endnum=inputs.length-1;
    if(obj!="0"){
        var num=$(obj).value;
        if(num.indexOf('.')!=-1)  {
           $(obj).value='1';
       }
       	if(obj.split("pernum_")[1]=="0"||obj.split("pernum_")[1]==endnum){
	    if(num==0) {
	        $(obj).value='1';
	   }
	    }
   }
	var perNum=new Array;
	var totalChipinMoney=0
	var txtPerNum=$("periodscount").value;
    for(var i=0;i<txtPerNum;i++){
        if(i<inputs.length){ 
        perNum.push(inputs[i].value)
       }
       else{
         perNum.push(1);
       } 
   } 
   	var agentnum=$("agentnum").value;
	var thisHTML = "";
	thisHTML = "\
	<div style=\"padding:5px 20px;text-align:left\">追号期数: <input size=\"10\" name=\"periodscount\"  id=\"periodscount\"  onkeyup=\"bindData(1);\" onkeypress=\"if(event.keyCode<48||event.keyCode>57)return false;\"  type=\"text\" value=\""+txtPerNum+"\"></div>\
	<table width='420' border='0' align='center' cellpadding='0' cellspacing='1' id=\"customform\" bgcolor='#CCCCCC'>\
	<tr bgcolor='#E1E1E1'>\
	<td width='16%' height='17' align=center>中奖出现</td>\
	<td width='13%' align=center>投注倍数</td>\
	<td width='20%' align=center>本期投入</td>\
	<td width='13%' align=center>累积投入</td>";
    if(type){
	thisHTML+="\
		<td width='13%' align=center>中奖奖金</td>\
	<td width='12%' align=center>收益值</td>\
	"
	}
	thisHTML+="</tr>";
	for(var i=0;i<txtPerNum;i++)
	{	
		var currentChipinMoney = perNum[i] * 2;
		totalChipinMoney += currentChipinMoney;
		thisHTML += "\
			<tr bgcolor='#FFFFFF'>\
			<td height='20' valign='top' align='center'>第 " + (i+1) + " 期</td>\
			<td valign='top' align='right'><input name=\"pernum\" id=\"pernum_"+i+"\" onkeyup=\"if(isNaN(this.value)) this.value='1';\"  onblur=\" bindData("+type+",this)\"  maxlength=\"6\"  style='border:0px;width:40px; text-align:right; border-bottom:1px solid #000' type=\"text\"  value='" + perNum[i] + "' /></td>\
			<td valign='top' align='right'>" + currentChipinMoney + "</td>\
			<td valign='top' align='right'>" + totalChipinMoney + "</td>"
			if(type){
	                thisHTML+="\
		                <td width='13%' align=center>"+currentChipinMoney*2*agentnum +"</td>\
	                <td width='12%' align=center>"+ (currentChipinMoney*2*agentnum - totalChipinMoney) +"</td>\
	                "
	        }
	        thisHTML+="</tr>";

	}
	thisHTML +=  "</table>";
	thisHTML +="<div style=\"padding:5px;text-align:center\"><input type=\"hidden\" name=\"totalChipinMoney\" id=\"totalChipinMoney\" value=\""+totalChipinMoney+"\"><input type=\"hidden\" name=\"agentnum\" id=\"agentnum\" value=\""+agentnum+"\"><input type=\"button\" value=\"确定\" onclick=\"postData()\"> <input type=\"button\" value=\"放弃\"  onclick=\"QuitData()\"> <input type=\"button\" value=\"关闭\"  onclick=\"closeData()\"></div>"
	$('dialogMsg1').innerHTML = thisHTML;
	//$('dialogMsg1').scrollTop = $('dialogMsg1').scrollHeight;
}
function postData(){
    var inputs=$("customform").getElementsByTagName("INPUT");
    var str=""; 
    for(var i=0;i<inputs.length;i++){
        str+=inputs[i].value+"|";
   }
   var iframe;
   if ($("expert_dif"))
   {
        iframe=$("expert_dif").contentWindow.$("ifmPlan").contentWindow;
   }
   else
   {
        iframe=$("ifmH8Chipin").contentWindow.$("ifmPlan").contentWindow;
   }
   
   var totalPay=iframe.$("ckbFeiPan").checked ? ($("totalChipinMoney").value*2) :$("totalChipinMoney").value ;
   //alert(iframe.$("ckbFeiPan").checked);
   
   if (iframe.$("agenttype").value=="0" || iframe.$("agenttype").value=="5")
   {
     iframe.$('dqzh_ChipinMoney').innerHTML=totalPay;
     iframe.$('dqzh_spanProNum').innerHTML=$("periodscount").value;
   }
   else if (iframe.$("agenttype").value=="3")
   {
     iframe.$('rhzz_ChipinMoney').innerHTML=totalPay;
     iframe.$('rhzz_spanProNum').innerHTML=$("periodscount").value;
   }
   else
   {
     iframe.$('hxfa_ChipinMoney').innerHTML=totalPay;
     iframe.$('hxfa_spanProNum').innerHTML=$("periodscount").value;
   } 
     
   iframe.$('periodscount').value=$("periodscount").value;	
   iframe.$('MulitSelfInput').value=str.substring(0,str.length-1).replace(/\|/g,',');
   iframe.$("dialogBoxBG1").style.display="none";
   winfm1.close();
}
function QuitData(){
    var CloseFram;
    var DoJob;
    if ($("expert_dif"))
    {
        CloseFram=$("expert_dif").contentWindow.$("ifmPlan").contentWindow;
        DoJob = $("expert_dif").contentWindow.document.getElementById("ifmPlan").contentWindow.SetEscStatus.bind($("expert_dif").contentWindow.document.getElementById("ifmPlan").contentWindow)
    }
    else
    {
        CloseFram=$("ifmH8Chipin").contentWindow.$("ifmPlan").contentWindow;
        DoJob = $("ifmH8Chipin").contentWindow.document.getElementById("ifmPlan").contentWindow.SetEscStatus.bind($("ifmH8Chipin").contentWindow.document.getElementById("ifmPlan").contentWindow)
    }

     CloseFram.$('MulitSelfInput').value="";
     //alert(1);
    // alert($("expert_dif").contentWindow.document.getElementById("ifmPlan") );
     //var zzz = $("expert_dif").contentWindow.document.getElementById("ifmPlan").contentWindow.uuu.bind($("expert_dif").contentWindow.document.getElementById("ifmPlan").contentWindow)
     DoJob();
     //zzz("0");
     //MutiTypeChange("0",$("expert_dif").contentWindow.document.getElementById("ifmPlan").contentWindow);
    //alert("ok")
      winfm1.close();
      CloseFram.$("dialogBoxBG1").style.display="none"; 
     
}

function closeData()
{
     var CloseFram;
    if ($("expert_dif"))
    {
        CloseFram=$("expert_dif").contentWindow.$("ifmPlan").contentWindow;
    }
    else
    {
        CloseFram=$("ifmH8Chipin").contentWindow.$("ifmPlan").contentWindow;
    }
    winfm1.close();
    CloseFram.$("dialogBoxBG1").style.display="none"; 
     
}