<?php
	$acl=$_SESSION["acl"];
	if (strlen($acl)<1 || !strpos($acl,"<2028>")) {	//没有权限
		ob_clean();
		header("Location: accesserror.html");
		exit;
	}
?>
<?php
	$defaultid="U".strval(time()-strtotime("2005-01-01 01:01:01"));
?>

<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<STYLE>
td.TD1 { text-align:center; width:200 }
td.TD2 { text-align:left; width:420 }
td.TD3 { text-align:center; width:620 }
</STYLE>
<SCRIPT LANGUAGE="javascript">
var typeid=new Array();
var usertype=new Array();
var price=new Array();
var typemode=new Array();
var unit=new Array();
<?php
	$n=0;
	$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD")); 
	mysql_select_db(get_cfg_var("SIPDB"),$db);
	$rs=mysql_query("select * from types",$db);
	if ($row = mysql_fetch_array($rs)) {
       while ( !($row==FALSE) ) {
	       echo("typeid[".strval($n)."]='".$row["typeid"]."';\r\n");
	       echo("usertype[".strval($n)."]='".$row["name"]."';\r\n");
		   $str=strval(round($row["price"],2));
	       echo("price[".strval($n)."]='".$str."';\r\n");
	       echo("typemode[".strval($n)."]='".$row["typemode"]."';\r\n");
	       echo("unit[".strval($n)."]='".strval($row["unit"])."';\r\n");
		   $n=$n+1;
	       $row = mysql_fetch_array($rs);
       }
	}
    echo("var typecount=".strval($n).";\r\n");
	mysql_free_result($rs);
?>

function Inint()
{
    SetBuy(document.form1.usertype);
    document.all.writepass.style.display="none";
}

function InputGP()
{
    cancelBubble=true;
    var n=document.form1.gp_name.selectedIndex;
    if (n>=0) {
	    document.form1.gpname.value=document.form1.gp_name.options(n).text;
    }
}

function SetValue()
{
    cancelBubble=true;
    if (document.form1.active[0].checked)
    {	
        document.all.discount1.style.display="";
        document.all.totalmoney21.style.display="";
        document.all.money21.style.display="";
        SetMoney2();
    }
    else
    {
        document.all.discount1.style.display="none";
        document.all.money21.style.display="none";
        document.all.totalmoney21.style.display="none";
        document.all.money22.innerText="0"; 
        document.form1.money2.value="0"; 
        document.form1.discount.value="100";      
        document.all.totalmoney22.innerText="0";
        document.form1.totalmoney2.value="0";
    }
}

function SetPass()
{
    if (document.form1.passtype[0].checked)
    {	
        document.all.writepass.style.display="none";
    }
    else
    {
        document.all.writepass.style.display="";
    }
}


function SetBuy(obj)
{
	var i=0;
	for (i=0;i<typecount;i++) {
		if (typeid[i]==obj.value) break;
	}
	if (i>=typecount) return;
    if (typemode[i]!="1")
    {
        document.all.buy.style.display="none";
        document.form1.buylength.value="0";
        document.all.present.style.display="none";
        document.form1.otherlength.value="0";
        document.all.money11.innerHTML="<span id='money12'>0</span><INPUT TYPE='hidden' NAME='money1' size='8' VALUE='0'>";
    }
    else
    {
        document.all.buy.style.display="";
        document.all.present.style.display="";
        document.all.money11.innerHTML="<INPUT TYPE='text' NAME='money1' VALUE='0' onkeyup='SetMoney1();'>";
    }
     SetMoney1();
}

function SetMoney1()
{
	var i=0;
	for (i=0;i<typecount;i++) {
		if (typeid[i]==document.form1.usertype.value) break;
	}
	if (i>=typecount) return;
    if (typemode[i]!="1")
    {
        var money1=document.form1.money1.value.toString();
        var totalmoney1=Math.round(parseInt(document.form1.count.value,"10")*parseFloat(money1,"10")*10)/10;
        document.all.totalmoney12.innerText=totalmoney1.toString();
        document.form1.totalmoney1.value=totalmoney1.toString();
    }
    else
    {
        var str=document.form1.usertype.options(document.form1.usertype.selectedIndex).text.toString();
        var n1=str.search(/单价/);
        var n2=str.search(/元/);
        var price=parseFloat(str.substring(n1+2,n2));
        var buylength=document.form1.buylength.value;
        var money1=Math.round(price*parseInt(buylength,"10")*10)/10;
        document.form1.money1.value=money1.toString();
        var totalmoney1=Math.round(parseInt(document.form1.count.value,"10")*money1*10)/10;
        document.all.totalmoney12.innerText=totalmoney1.toString();
        document.form1.totalmoney1.value=totalmoney1.toString();
    }
    SetValue();
}

function SetMoney2()
{
    cancelBubble=true;
    var money1=document.form1.money1.value.toString();
    var money2=Math.round(parseFloat(money1,"10")*parseInt(document.form1.discount.value,"10")/10)/10;
    document.all.money22.innerText=money2.toString();
    document.form1.money2.value=money2.toString();
    var totalmoney2=Math.round(parseFloat(money2)*10*parseInt(document.form1.count.value,"10"))/10;
    document.all.totalmoney22.innerText=totalmoney2.toString();
    document.form1.totalmoney2.value=totalmoney2.toString();   
    
}

function IsNumber(obj)
{   
    var str=obj.value;
    var tmpstr=parseFloat(str,"10");
    if (tmpstr!=str)
        return(false);
    else
    {
        obj.value=tmpstr;
        return(true);
    }
}

function IsInt(obj)
{   
    var str=obj.value;
    var tmpstr=parseInt(str,"10");
    if (tmpstr!=str)
        return(false);
    else
    {
        obj.value=tmpstr;
        return(true);
    }
}

function checkData()
{
     var startname=document.form1.startname.value;
     var len=startname.length;
     var tmpstr=startname.substring(len-1,len);
     if (tmpstr.search(/\d/)==-1||len==0)
     {
         alert("起始用户ID必须以数字结尾且不能为空");
         document.form1.startname.focus();
         document.form1.startname.select();
     } 
     else
     {
         var obj=document.form1.count;
         var cansub=IsInt(obj);
         if (cansub&&obj.value>=1)
         {
             obj=document.form1.onlines;
             cansub=IsInt(obj);
             if (cansub&&obj.value>=1)
             {
                 obj=document.form1.buylength;
                 cansub=IsInt(obj);
                 if (cansub&&obj.value>=0)
                 {
                     obj=document.form1.otherlength;
                     cansub=IsInt(obj);
                     if (cansub&&obj.value>=0)
                     {
                         obj=document.form1.discount;
                         cansub=IsInt(obj);
                         if (cansub&&obj.value>0)
                         {
							var i=0;
							for (i=0;i<typecount;i++) {
								if (typeid[i]==document.form1.usertype.value) break;
							}
							if (typemode[i]=="1")
                             {
                                 obj=document.form1.money1;
                                 cansub=IsNumber(obj);
                                 if (cansub&&obj.value>=0)
                                     document.form1.submit();
                                 else
                                 { 
                                     alert("每用户应收金额只能是数字且大于等于0");
                                     document.form1.money1.focus();
                                     document.form1.money1.select();
                                 }
                             }
                             else
                                 document.form1.submit();
                         }
                         else
                     	  { 
                             alert("折扣只能是整数且大于0");
                             document.form1.discount.focus();
                             document.form1.discount.select();
                         }
                     }
                     else
                     { 
                         alert("每用户赠送数量只能是整数且大于等于0");
                         document.form1.otherlength.focus();
                         document.form1.otherlength.select();
                      } 
                 }
                 else
                 {
                     alert("每用户购买数量只能是整数且大于等于0");
                     document.form1.buylength.focus();
                     document.form1.buylength.select();
                 } 
             }
             else
             {
                 alert("每用户允许同时在线人数只能是整数且大于等于1");
                 document.form1.onlines.focus();
                 document.form1.onlines.select();
             } 

         }
         else
         {
             alert("批次用户数只能是整数且大于等于1");
             document.form1.count.focus();
             document.form1.count.select();
         } 
     }
}
</SCRIPT>

<title>批量用户注册</title>
</head>

<body background="bkg.bmp" onload="Inint();">

<p align="center"><font size="5"><b>批量用户注册</b></font></p>
<hr color="#999966">
<div align="center">
<form method="POST" name="form1" action="addbatch_result.php">
  <table id="table1" border="1" bordercolor="#008080" cellspacing="0" cellpadding="0">
    <tr>
      <td class="TD1">起始用户ID</td> 
      <td class="TD2">&nbsp;
          <input type="text" name="startname" size="20" maxlength="20" value="<?php echo $defaultid; ?>"><br>
		  <font size="2">(起始用户ID必须以数字结尾。例如起始ID为U101，数量为10，将生成U101到U110的用户。U10A、U00100等起始ID无效。)</font>
      </td>
    </tr>
    <tr>
      <td class="TD1">该批次用户密码</td>
      <td class="TD2">&nbsp; <input type="radio" name="passtype" checked value="1" onclick="SetPass(this);">自动随机生成&nbsp;&nbsp;&nbsp;  
        <input type="radio" name="passtype" value="2" onclick="SetPass(this);">全部指定相同的密码
        <p id="writepass">&nbsp; 指定密码<input type="text" name="setword" size="20"></p>
      </td>
    </tr>
    <tr>
      <td class="TD1">该批次用户数量</td>
      <td class="TD2">&nbsp;
          <input type="text" name="count" size="8" value="1" onkeyup="SetMoney1();">
      </td>
    </tr>
    <tr>
      <td class="TD1">该批次用户类型</td>
      <td class="TD2">&nbsp;                                                                                 
          <select size="1" name="usertype" onclick="SetBuy(this);">
<?php
		$rs = mysql_query("select typeid,name,price from types order by name",$db);		//读取结帐类型列表
		if ($row = mysql_fetch_array($rs)) {
           echo("<option selected value=".chr(34).$row["typeid"].chr(34).">".$row["name"]."，单价".strval($row["price"])."元</option>");	//缺省选择第一项
           while ($row = mysql_fetch_array($rs)) {
	            echo("<option value=".chr(34).$row["typeid"].chr(34).">".$row["name"]."，单价".strval($row["price"])."元</option>");
           }
		}
		mysql_free_result($rs);
?>
          </select>
      </td>
    </tr>
    <tr>
      <td class="TD1">每用户允许同时在线人数</td>
      <td class="TD2">&nbsp;
          <input type="text" name="onlines" size="3" value="1">
      </td>
    </tr>
    <tr>
      <td class="TD1">该批次用户所属组别</td>
      <td id="groupinfo" class="TD2">&nbsp;
      <input type="text" name="gpname" size="8" value="" maxlength="50">&nbsp;&nbsp;
          <select size="1" name="gp_name" onclick="InputGP();"> 
<?php
        $rs = mysql_query("select gpname from groups order by gpid",$db);
       while ($row = mysql_fetch_array($rs)) {
	       echo("<option value=".chr(34).$row["gpname"].chr(34).">".$row["gpname"]."</option>");
       }
		mysql_free_result($rs);
		mysql_close($db);
?>
          </select><br>&nbsp;&nbsp;<font size="2">新用户组请直接输入,为空为无组别<br>&nbsp&nbsp已有的用户组请从右边的下拉菜单中选择,<font>
      </td>
    </tr>
    <tr id="buy">
      <td class="TD1">每用户购买数量</td>
      <td class="TD2">&nbsp;
          <input type="text" name="buylength" size="8" value="0" onkeyup="SetMoney1();">                        
      </td>
    </tr>
    <tr id="present">
      <td class="TD1">每用户赠送数量</td>
      <td class="TD2">&nbsp;
          <input type="text" name="otherlength" size="8" value="0">
           <font size="2"><br>     
          ·“包年”类型赠送数量是以月为单位；<br>
          ·“计流量(G)”类型赠送数量是以M为单位；<br>
          ·其它类型赠送数量的单位和该类型续费数量单位相同。   
          </font>
      </td>
    </tr>
    <tr>
      <td class="TD1">每用户应收金额</td>
      <td class="TD2">&nbsp;                                                              
          <span id="money11"></span>元
      </td>
    </tr>
    <tr id="totalmoney11">
      <td class="TD1">该批次应收金额</td>	
      <td class="TD2">&nbsp;
      <span id="totalmoney12"></span>元
      <input type="hidden" name="totalmoney1" value="0">
      </td>
    </tr>
	<tr>
      <td class="TD1">备注说明</td>
      <td class="TD2">&nbsp;
          <input type="text" name="comment" size="50" maxlength="100">
        </td>
    </tr>
	<tr>
      <td class="TD1">立即激活该批次</td>	
      <td class="TD2">&nbsp;
          <input type="radio" name="active" value="y" onclick="SetValue(this);">激活
          <input type="radio" name="active" value="n" checked onclick="SetValue(this);">不激活
      </td>
    </tr>
    <tr id="discount1">
      <td class="TD1">折扣</td>	
      <td class="TD2">&nbsp;
          <input type="text" name="discount" value="100" size="3" onkeyup="SetMoney2();">%
      </td>
    </tr>
    <tr id="money21">
      <td class="TD1">每用户实收金额</td>
      <td class="TD2">&nbsp;
      <span id="money22"></span>元
      <input type="hidden" name="money2" value="0">
      </td>
    </tr>
    <tr id="totalmoney21">
      <td class="TD1">该批次实收金额</td>	
      <td class="TD2">&nbsp;
      <span id="totalmoney22"></span>元
      <input type="hidden" name="totalmoney2" value="0">
      </td>
    </tr>
    <tr>
      <td colspan="2" class="TD3"><input type="button" value="提交" name="B1" onclick="checkData();"></td>
    </tr>
  </table>
  </form>
</div>
<hr>
<p align="center">SIP宽带上网计费管理系统</p>
  <p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>
</body>

</html>
