#!/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/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 passwd1 = document.getElementsByName(\"passwd1\")[0];
	var memo    = document.getElementsByName(\"memo\")[0];
	account.value = TrimAll(account.value);
	if (account.value == \"\") {
		alert(\"˺!\");
		account.select();
		return false;
	}
	passwd1.value = TrimAll(passwd1.value);
	if (passwd1.value == \"\") {
		alert(\"!\");
		passwd1.select();
		return false;
	}
	expire.value = TrimAll(expire.value);
	if (expire.value == \"\") {
		alert(\"Ч\");
		expire.select();
		return false;
	}
	var fullname = document.getElementsByName(\"fullname\")[0];
	var identitycard  = document.getElementsByName(\"identitycard\")[0];
	var telephone = document.getElementsByName(\"telephone\")[0];
	var remark = document.getElementsByName(\"remark\")[0];
	
	fullname.value = TrimAll(fullname.value);
	identitycard.value = TrimAll(identitycard.value);
	telephone.value = TrimAll(telephone.value);
	remark.value = TrimAll(remark.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});
	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>
";
analytical_memo_description_mini()
{
		# int arg like: 1. enabled;maxlogin;uplimit;downlimit;createtm;fullname;identitycard;telephone;address;remark
		#				2. remark dbid:
		# out like this: enabled= maxlogin= uplimit= downlimit= createtm= fullname= identitycard= telephone= address= remark=
		echo "${1}" | \
		awk -F';' 'BEGIN{ \
			split("dbid;fullname;identitycard;telephone;remark",columns,";");\
		}{ \
			gsub(/ /,"+",$0); \
			if($0 ~ /^[^;|.]*;[^;|.]*;[^;|.]*;[^;|.]*;[.]*/) { \
				for(i=6;i<=NF;i++) \
					$5=$5"\\;"$i;\
			} else { \
				$5=$0; \
				for(i=1;i<5;i++) \
					$i=""; \
			} \
			for(i=1;i<5;i++) \
				printf columns[i]"="$i" "; \
			print columns[i]"="$i; \
		}'
}
if [ "${REQUEST_METHOD}" = "POST" ]; then
	operator_check "${myself}" pppoeman
	cmdargs="poolid=${CGI_poolid} name=${CGI_account} password=${CGI_passwd1} expire=${CGI_expire}"
	[ "${CGI_bindmac}" != "" ] && cmdargs="${cmdargs} bindmac=${CGI_bindmac}"
	[ "${CGI_multiplay}" = "1" ] && cmdargs="${cmdargs} multiplay=1"
	cmdargs="${cmdargs} memo=${CGI_dbid};${CGI_fullname};${CGI_identitycard};${CGI_telephone};${CGI_remark}"
	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
		dbid=0
		fullname=""
		identitycard=""
		telephone=""
		remark=""
		nameval=`echo ${nameval} | awk '{gsub(/;/,"\\\\;",$0); gsub(/\x27/,"\\\\\\\\\\\\\x27",$0 );\
			gsub(/ /, "_", $0); \
			gsub(/\&|\<|\>|\"/, "", $0); \
			print $0;}'`
		
		eval "${nameval}" 
		if [ "${memo}" != "" ]; then
			eval "`analytical_memo_description_mini ${memo}`"
		fi
	done
	CGI_passwd1="${passwd}"
	CGI_passwd2="${passwd}"
	CGI_poolid="${poolid}"
	CGI_memo="${memo}"
	CGI_expire="${expire}"
	CGI_dbid="${dbid}"
	if [ "${dbid}" = "" ]; then
		CGI_dbid=0
	fi
	CGI_fullname="${fullname}"
	CGI_identitycard="${identitycard}"
	CGI_telephone="${telephone}"
	CGI_remark="${remark}"
	CGI_bindmac="${bindmac}"
	CGI_multiplay="${multiplay}"
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></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=passwd1 style=\"width:150px\" value=\"${CGI_passwd1}\"></input>
		&nbsp;(30Ӣַ)
	</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)
	</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>ದ</td>
        <td align=left><select name=multiplay style=\"width:120px\" value=\"${CGI_multiplay}\">
	"; if [ "${CGI_multiplay}" = "0" ]; then 
echo -n "
		<option value=0 selected></option>
		<option value=1></option>
	"; else 
echo -n "
		<option value=0></option>
		<option value=1 selected></option>
	"; fi 
echo -n "
	</select>&nbsp;(ದʱһ˺ſͻͬʱ½ʹ)
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left></td>
        <td align=left><input type=text class=text name=fullname style=\"width:150px\" value=\"${CGI_fullname}\"></input>&nbsp;(7ַ)
</tr>
<tr id=row1>
	<td></td>
        <td align=left>֤</td>
        <td align=left><input type=text class=text name=identitycard style=\"width:150px\" value=\"${CGI_identitycard}\"></input>&nbsp;(18ַ)
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ϵ绰</td>
        <td align=left><input type=text class=text name=telephone style=\"width:150px\" value=\"${CGI_telephone}\"></input>&nbsp;(12ַ)
</tr>
<tr id=row1>
	<td></td>
        <td align=left>עϢ</td>
        <td align=left><input type=text class=text name=remark style=\"width:450\" value=\"${CGI_remark}\"></input>&nbsp;(50ַ)
</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>
";