#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html;charset=gb2312
Cache-Control: no-cache

"
echo -n "";
. ../common/common.sh 
myself="/cgi-bin/Pppoe/`basename $0`"
if [ "${CGI_where}" = "pool" ]; then
	returl="/cgi-bin/Pppoe/ippool_list"
elif [ "${CGI_where}" = "online" ]; then
	returl="/cgi-bin/Pppoe/pppoe_online"
else
	returl="/cgi-bin/Pppoe/pppoe_account"
fi

echo -n "
<link rel=\"stylesheet\" type=\"text/css\" href=\"/img/cal.css\" />
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script type=\"text/javascript\" src=\"/img/jq.js\"></script>
<script type=\"text/javascript\" src=\"/img/cal.js\"></script>
<script language=\"javascript\">
function beforeCommit(frm)
{
	var account = document.getElementsByName(\"account\")[0];
	var expire  = document.getElementsByName(\"expire\")[0];
	var passwd = document.getElementsByName(\"passwd\")[0];
	var bindip  = document.getElementsByName(\"bindip\")[0];
	var maxonline = document.getElementsByName(\"maxonline\")[0];
	account.value = TrimAll(account.value);
	bindip.value = TrimAll(bindip.value);
	maxonline.value = TrimAll(maxonline.value);
	if (account.value == \"\") {
		alert(\"˺!\");
		account.select();
		return false;
	}
	passwd.value = TrimAll(passwd.value);
	if (passwd.value == \"\") {
		alert(\"!\");
		passwd.select();
		return false;
	}
	expire.value = TrimAll(expire.value);
	if (expire.value == \"\") {
		alert(\"Ч\");
		expire.select();
		return false;
	}
	var cname  = document.getElementsByName(\"cname\")[0];
	var phone  = document.getElementsByName(\"phone\")[0];
	var card   = document.getElementsByName(\"card\")[0];
	var other  = document.getElementsByName(\"other\")[0];
	cname.value = TrimAll(cname.value);
	card.value  = TrimAll(card.value);
	phone.value = TrimAll(phone.value);
	other.value = TrimAll(other.value);
	return true;
}
function onCancel() 
{
	window.location.href = \"${returl}\";
}
String.prototype.trim = function () {
    var $ = /^\s+|\s+$/g;
    return function () {
        return this.replace($, \"\")
    }
}();
jQuery(document).ready(function () {
	\$('#expire').Cal({enddate:2100});
	\$('#start').Cal({enddate:2100});
	Date.prototype.Format = function(format){
		var o = {
		\"M+\" : this.getMonth()+1, //month
		\"d+\" : this.getDate(),    //day
		\"h+\" : this.getHours(),   //hour
		\"m+\" : this.getMinutes(), //minute
		\"s+\" : this.getSeconds(), //second
		\"q+\" : Math.floor((this.getMonth()+3)/3),  //quarter
		\"S\" : this.getMilliseconds() //millisecond
		};
		if(/(y+)/.test(format)){
			format=format.replace(RegExp.\$1,(this.getFullYear()+\"\").substr(4 - RegExp.\$1.length));
		}
		for(var k in o){
			if(new RegExp(\"(\"+ k +\")\").test(format)){
				format = format.replace(RegExp.\$1,RegExp.\$1.length==1 ? o[k] :(\"00\"+ o[k]).substr((\"\"+ o[k]).length));
			}
		}
		return format;
	};
	\$('#link_add_month').bind({
		'mouseover':function(e){
			\$('#box_choose_month').show();
		},
		'mouseout':function(e){
			\$('#box_choose_month').hide();
		}
	});
	
	\$('span',\$('#box_choose_month')).bind({
		'click':function(e){
			var ctl = \$('#expire');
			var d1;
			d1 = ctl.val();
			if(d1 == '') return;
			
			var addm = parseInt(\$(this).attr('m')),d2=0,nowTime = (new Date()).Format('yyyy-MM-dd');
			d1 = d1.trim();
			if(/^\d{4}-\d{1,2}-\d{1,2}(\s+\d{2}:\d{1,2})?\$/.test(d1)){
				if(/1970-01-01/.test(d1)){
					d1 = nowTime;
				}
			}else{
				d1 = nowTime;
			}
			
			var t,pos,y,m,om,d,od,pd,pt,md = [31,28,31,30,31,30,31,31,30,31,30,31];
			if((pos = d1.indexOf(' ')) !== -1){
				pt = d1.substr(pos+1);
				pd = d1.substr(0,pos);
			}else{
				pt = '23:59';
				pd = d1;
			}
			pd = pd.split('-');
			
			y = parseInt(pd[0],10);
			om = m = parseInt(pd[1],10);
			od = d = parseInt(pd[2],10);
				
			if(addm + m > 12){
				y++;
				m = addm+m-12;
			}else{
				m = m + addm;
			}
			
			if(y % 4 == 0)md[1] = 29;
			if(md[om-1] == od){
				d = md[m-1];
			}
			m = '0' + m;
			d = '0' + d;
			d2 = y+'-'+m.substr(m.length-2)+'-'+d.substr(d.length-2);
			
			ctl.val(d2);
		}
	});
});
</script>
";
if [ "${REQUEST_METHOD}" = "POST" ]; then
	operator_check "${myself}" pppoeman
	cmdargs="poolid=${CGI_poolid} name=${CGI_account} password=${CGI_passwd} start=${CGI_start} expire=${CGI_expire}"
	[ "${CGI_maxonline}" = "" ] && CGI_maxonline="1"
	cmdargs="${cmdargs} maxonline=${CGI_maxonline}"
	[ "${CGI_bindmac}" = "" ] && CGI_bindmac="00-00-00-00-00-00"
	cmdargs="${cmdargs} bindmac=${CGI_bindmac}"
	[ "${CGI_bindip}" = "" ] && CGI_bindip="0.0.0.0"
	[ "${CGI_bindip}" != "0.0.0.0" ] && cmdargs="${cmdargs} bindip=${CGI_bindip}"
	[ "${CGI_cname}" != "" ] && cmdargs="${cmdargs} cname=${CGI_cname}"
	[ "${CGI_phone}" != "" ] && cmdargs="${cmdargs} phone=${CGI_phone}"
	[ "${CGI_card}"  != "" ] && cmdargs="${cmdargs} card=${CGI_card}"
	[ "${CGI_other}" != "" ] && cmdargs="${cmdargs} other=${CGI_other}"
	errmsg=`${FLOWEYE} pppoeacct set ${cmdargs}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	else
		afm_load_page 0 "/cgi-bin/Pppoe/pppoe_account"
		exit 0
	fi
else
	for nameval in `${FLOWEYE} pppoeacct get name=${CGI_account}`
	do
		name=`echo ${nameval} | cut -d'=' -f1`
		val=`echo ${nameval} | cut -d'=' -f2-`
		export CGI_${name}="${val}"
	done
	[ "${CGI_bindmac}" = "00-00-00-00-00-00" ] && CGI_bindmac=""
	[ "${CGI_bindip}" = "0.0.0.0" ] && CGI_bindip=""
fi

echo -n "
<body>
";
cgi_show_title "PPPOE->޸˺" 

echo -n "
<br>
<form method=post onsubmit=\"return beforeCommit(this)\" action=\"${myself}\">
<table style=\"font:14px\" width=700 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td width=40></td>
        <td width=80 align=left>˺</td>
	<td width=* align=left><b>${CGI_account}</b></td>
</tr>
<tr id=row1>
	<td width=40></td>
        <td width=80 align=left>ʱ</td>
	<td width=* align=left><b>${CGI_birth}</b></td>
</tr>
"; if [ "${CGI_lastofftime}" != "1970-01-01/08:00:00" ]; then 
echo -n "
<tr id=row1>
	<td width=40></td>
        <td width=80 align=left>ʱ</td>
	<td width=* align=left><b>${CGI_lastofftime}</b></td>
</tr>
"; fi 
echo -n "
<tr id=row1>
	<td></td>
        <td align=left></td>
        <td align=left>
		<select name=poolid value=\"${CGI_poolid}\" style=\"width:150;height:21\">
";
		${FLOWEYE} pppoeippool list | while read gid name theothers
		do
			[ "${name}" = "NULL" ] && name="${gid}"
			if [ "${gid}" = "${CGI_poolid}" ]; then
				echo "<option value=${gid} selected>${name}</option>"
			else
				echo "<option value=${gid}>${name}</option>"
			fi
		done

echo -n "
		</select>
	</td>
</tr>
<tr id=row1>
	<td width=40></td>
        <td width=80 align=left>˺</td>
	<td width=* align=left>
		<input type=text name=passwd style=\"width:150px\" value=\"${CGI_passwd}\"></input>
		&nbsp;(30Ӣַ)
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ͨ</td>
        <td align=left>
		<input type=\"text\" size=\"6\" name=\"start\" id=\"start\" validate=\"Date\" filter=\"filterDate\" setfn=\"setfnDate\" style=\"width:150px\" value=\"${CGI_start}\"/>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>Ч</td>
        <td align=left>
		<input type=\"text\" size=\"6\" name=\"expire\" id=\"expire\" validate=\"Date\" filter=\"filterDate\" setfn=\"setfnDate\" style=\"width:150px\" value=\"${CGI_expire}\"/>
                <span id=\"link_add_month\"> +ʱ
                    <div id=\"box_choose_month\">
                        <span m=\"1\">1</span>
                        <span m=\"3\">3</span>
                        <span m=\"6\">Ӱ</span>
                        <span m=\"12\">1</span> 
                    </div>
                </span>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>MAC</td>
        <td align=left><input type=text class=text name=bindmac style=\"width:150px\" value=\"${CGI_bindmac}\"></input>
		&nbsp;(ʽxx-xx-xx-xx-xx-xx, 00-00-00-00-00-00ʾ)
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>IP</td>
        <td align=left><input type=text class=text name=bindip style=\"width:150px\" value=\"${CGI_bindip}\"></input>
		&nbsp;(0.0.0.0Ϊձʾ)
	</td>
</tr>
"; [ "${CGI_maxonline}" = "" ] && CGI_maxonline=1 
echo -n "
<tr id=row1>
	<td></td>
        <td align=left>û</td>
        <td align=left><input type=text class=text name=maxonline style=\"width:150px\" value=\"${CGI_maxonline}\"></input></td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left></td>
        <td align=left><input type=text class=text name=cname style=\"width:150px\" value=\"${CGI_cname}\"></input>&nbsp;(7ַ)
</tr>
<tr id=row1>
	<td></td>
        <td align=left>֤</td>
        <td align=left><input type=text class=text name=card style=\"width:150px\" value=\"${CGI_card}\"></input>&nbsp;(18ַ)
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ϵ绰</td>
        <td align=left><input type=text class=text name=phone style=\"width:150px\" value=\"${CGI_phone}\"></input>&nbsp;(12ַ)
</tr>
<tr id=row1>
	<td></td>
        <td align=left>Ϣ</td>
        <td align=left><input type=text class=text name=other style=\"width:450\" value=\"${CGI_other}\"></input>&nbsp;(40ַ)
</tr>
</table>
<table style=\"width:700; border-bottom:1px #787882 solid; color:#0000ff\">
<tr><td align=right>&nbsp;</td></tr>
</table>
<table style=\"width:700\"> 
<tr>
        <td align=right>
		<input type=hidden name=where value=\"${CGI_where}\" />
		<input type=hidden name=dbid value=\"${CGI_dbid}\" />
		<input type=hidden name=account value=\"${CGI_account}\" />
		<input type=submit style=\"width:80\" value=\"ύ\" />
		<input type=button style=\"width:80\" value=\"ȡ\" onclick=\"onCancel()\" />
	</td>
</tr>
</table>
</form>
</body>
</html>
";