<?php
	$acl=$_SESSION["acl"];
	if (strlen($acl)<1 || !strpos($acl,"<2003>")) {	//没有权限
		ob_clean();
		header("Location: accesserror.html");
		exit;
	}
?>


<?php
    $errno=0;             //响应信息号
	$userid="";
	$usertype="";
	$price=0;
	$username="";
	$usertypename="";
	$loginname=trim($_REQUEST["loginname"]);
	if ($loginname!="") {
		$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 users.userid,users.type,users.username,types.name,types.price,types.typemode from users,types where users.type=types.typeid and users.checked='n' and users.loginname='".$loginname."'",$db);		//读取结帐类型列表
		if ($row = mysql_fetch_array($rs)) {
			$userid=$row[0];
			$usertype=$row[1];
			$username=$row[2];
			if ($username=="") {
			   $username="&nbsp;";
			}
			$usertypename=$row[3];
			$price=strval($row[4]);
			$typemode=$row[5];
			$errno=1;
		}
	    else {
	        $errno=2;
		}
		mysql_free_result($rs);
		if ($typemode!="1") {
		    $errno=3;
		}
		else {
		    $rs = mysql_query("select viptype,givelength,discount from vipuser where  userid='".$userid."'",$db);		//读取优惠等级列表
	    	if ($row = mysql_fetch_array($rs)) {
			   $viptype=$row[0];
			   $givelength=$row[1];
			   $discount=$row[2];
			}
		    else {
		       $viptype="";
		       $givelength=0;
		       $discount=100;
		    }
		    mysql_free_result($rs);
		}
		mysql_close($db);
	}
    else {
       $errno=4;
	}
?>

<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">

<SCRIPT LANGUAGE="javascript">
function setmoney1()
{
    cancelBubble=true;
    var price=document.form1.price.value;
    var buylength=document.form1.buylength.value;
    var money1=Math.round(parseFloat(price,"10")*parseInt(buylength,"10")*10)/10;
	document.all.money11.innerText=" "+money1.toString()+"元";
	document.form1.money1.value=money1.toString();
	setmoney2()
}

function setmoney2()
{
    cancelBubble=true;
	var money2=Math.round(parseInt(document.form1.discount.value,"10")*parseFloat(document.form1.money1.value,"10")/10)/10;
	document.all.money21.innerText=" "+money2.toString()+"元";
	document.form1.money2.value=money2.toString();
}


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()
{

    obj=document.form1.buylength;
    cansub=IsInt(obj);
    if (cansub&&obj.value>=1)
       {
        obj=document.form1.otherlength;
        cansub=IsInt(obj);
        if (cansub&&obj.value>=0)
           {
            obj=document.form1.discount;
            cansub=IsInt(obj);
            if (cansub&&obj.value>0)
                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("购买数量只能是整数且大于等于1");
        document.form1.buylength.focus();
        document.form1.buylength.select();
       } 
}
</SCRIPT>


<title>用户上网续费</title>
</head>

<body background="bkg.bmp">

<p align="center"><font size="5"><b>用户上网续费</b></font></p>
<hr color="#999966">
<div id='info' align="center">
<center>
<?php if ($errno==1) { ?>
<form name=form1 method="POST" action="addtime_result.php">
  <input type="hidden" name="userid" value="<?php echo $userid; ?>">
  <input type="hidden" name="usertype" value="<?php echo $usertype; ?>">
  <input type="hidden" name="price" value="<?php echo $price; ?>">
<table border="1" width="524" height="183" bordercolor="#008080" cellspacing="0" cellpadding="0">
    <tr>
      <td width="124" align="center" height="50">用户ID</td>
      <td width="386" align="left" height="50">&nbsp;&nbsp;<?php echo $loginname; ?>
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">用户名称</td>
      <td width="386" align="left" height="50">&nbsp;&nbsp;<?php echo $username; ?>
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">用户类型</td>
      <td width="386" align="left" height="50">&nbsp;&nbsp;<?php echo $usertypename; ?>
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">续费数量</td>
      <td width="386" align="left" height="50">&nbsp;
          <input type="text" name="buylength" size="8" value="0" onkeyup="setmoney1();">
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">赠送数量</td>
      <td width="386" align="left" height="50">&nbsp;
          <input type="text" name="otherlength" value="<?php echo $givelength; ?>" size="6"><br>
          <font size="2"><br>     
          ·“包年”类型赠送数量是以月为单位；<br>
          ·“计流量(G)”类型赠送数量是以M为单位；<br>
          ·其它类型赠送数量的单位和该类型续费数量单位相同。   
          </font>
        </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">合计费用</td>
      <td width="386" align="left" height="50">&nbsp;
          <span id="money11">0元</span>
           <input type="hidden" name="money1" value="0">         
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">优惠折扣率</td>
      <td width="386" align="left" height="50">&nbsp;
	      <input type="text" name="discount" size="3" value="<?php echo $discount; ?>" onkeyup="setmoney2();">% 
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">实收费用</td>
      <td width="386" align="left" height="50">&nbsp;
           <span id="money21">0元</span> 
           <input type="hidden" name="money2" value="0">
      </td>
    </tr>
<?php if ($viptype!="") {?>
   <tr>
      <td align="center" height="50" colspan=2>
       该用户优惠等级为：<?php echo $viptype; ?>,赠送<?php echo $givelength; ?>,折扣<?php echo $discount; ?>%
        </td>
    </tr>
<?php } ?>
    <tr>
      <td colspan="2" width="464" align="center" height="51">
          <input type="button" value="提交" name="B1" onclick="checkData();">
      </td>
    </tr>
</table>
</form>
<?php } elseif ($errno==2) {?>
<p>输入的用户ID无效</p>
<?php } elseif ($errno==3) {?>
<p>该用户类型为：<?php echo $usertypename; ?>,不是预付费类型</p>
<?php } elseif ($errno==4) {?>
<p>请输入需要续费的用户ID</p>
<?php } ?>
  </center>
</div>
<hr>
<p align="center">SIP宽带上网计费管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>

</body>

</html>
