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

"
echo -n "";
. ../common/common.sh 
myself="/cgi-bin/Setup/`basename $0`"

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 memo    = document.getElementsByName(\"memo\")[0];
	account.value = TrimAll(account.value);
	if (account.value == \"\") {
		alert(\"˺!\");
		account.select();
		return false;
	}
	expire.value = TrimAll(expire.value);
	if (expire.value == \"\") {
		alert(\"Ч\");
		expire.select();
		return false;
	}
	memo.value = TrimAll(memo.value);
	return true;
}
function onCancel() 
{
	window.location.href = \"/cgi-bin/Setup/l2bypass_account\";
}
String.prototype.trim = function () {
    var $ = /^\s+|\s+$/g;
    return function () {
        return this.replace($, \"\")
    }
}();
jQuery(document).ready(function () {
	\$('#expire').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'),
			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="account=${CGI_account} expire=${CGI_expire} gid=${CGI_gid}"
	[ "${CGI_memo}" != "" ] && cmdargs="${cmdargs} memo=${CGI_memo}"
	errmsg=`${FLOWEYE} l2route set ${cmdargs}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	else
		afm_load_page 0 "/cgi-bin/Setup/l2bypass_account"
		exit 0
	fi
fi
if [ "${CGI_account}" != "" ]; then
	acctinfo=`${FLOWEYE} l2route list type=account | awk '{print $3,$5,$1,$6}' | grep "^${CGI_account} "`
	CGI_expire=`echo ${acctinfo} | cut -d' ' -f2`
	CGI_gid=`echo ${acctinfo} | cut -d' ' -f3`
	CGI_memo=`echo ${acctinfo} | cut -d' ' -f4`
	[ "${CGI_memo}" = "NULL" ] && CGI_memo=""
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=600 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>
		<input type=text name=account style=\"width:150px\" value=\"${CGI_account}\"></input>
		&nbsp;(30Ӣַ15ַ)
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left></td>
        <td align=left>
		<select name=gid value=\"${CGI_gid}\" style=\"width:150;height:21\">
	";
		${FLOWEYE} l2route list | while read gid name theothers
		do
			[ "${name}" = "NULL" ] && name="${gid}"
			if [ "${gid}" = "${CGI_gid}" ]; 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></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>
		&nbsp;(ʽ2014-05-01/12:00:00)</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>עϢ</td>
        <td align=left><input type=text class=text name=memo style=\"width:250\" value=\"${CGI_memo}\"></input>&nbsp;(50ַ)
</tr>
</table>
<table style=\"width:600; border-bottom:1px #787882 solid; color:#0000ff\">
<tr><td align=right>&nbsp;</td></tr>
</table>
<table style=\"width:600\"> 
<tr>
        <td align=right>
		<input type=submit style=\"width:80\" value=\"ύ\"></input>
		<input type=button style=\"width:80\" value=\"ȡ\" onclick=\"onCancel()\"></input>
	</td>
</tr>
</table>
</form>
</body>
</html>
";