<%+cbi/valueheader%>
<script type="text/javascript">//<![CDATA[
	function dler_login(btn,option)
	{
		btn.disabled = true;
		if (option == "dler_login") {
			var s = document.getElementById(option+'-status');
			var e = document.getElementsByName('cbid.openclash.config.dler_email');
			var p = document.getElementsByName('cbid.openclash.config.dler_passwd');
			var c = document.getElementsByName('cbid.openclash.config.dler_checkin');
			if (!e[0].value || !p[0].value) {
				btn.disabled = false;
				s.innerHTML ="<font color='red'><strong>"+"<%:Error Login Info%>"+"</strong></font>";
				return false;
			};
			if (c[0] && c[0].checked) {
				c = "1";
				var i = document.getElementsByName('cbid.openclash.config.dler_checkin_interval');
				var m = document.getElementsByName('cbid.openclash.config.dler_checkin_multiple');
				if (!i[0].value || !(/(^[1-9]\d*$)/.test(i[0].value))) { i = "1"} else {i = i[0].value};
				if (!m[0].value || !(/(^[1-9]\d*$)/.test(m[0].value)))
					{
						btn.disabled = false;
						s.innerHTML ="<font color='red'><strong>"+"<%:Multiple Must Be a Positive Integer and No More Than 50%>"+"</strong></font>";
						return false;
					}
				else if (m[0].value < 1)
					{
						btn.disabled = false;
						s.innerHTML ="<font color='red'><strong>"+"<%:Multiple Must Be a Positive Integer and No More Than 50%>"+"</strong></font>";
						return false;
					}
				else if (m[0].value > 50)
					{
						btn.disabled = false;
						s.innerHTML ="<font color='red'><strong>"+"<%:Multiple Must Be a Positive Integer and No More Than 50%>"+"</strong></font>";
						return false;
					}
				else {
					m = m[0].value;
				};
			}
			else {
				c = "0";
				var i = "1";
				var m = "1";
			};
			btn.value = '<%:Login...%>';
			XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "dler_login_info_save")%>', {email: e[0].value, passwd : p[0].value, checkin: c, interval: i, multiple: m}, function(x, status) {
				if (x && x.status == 200) {
					XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "dler_login")%>', null, function(x, status) {
					if (s)
					{
						if (x && x.status == 200 && status.dler_login == 200) {
							s.innerHTML ="<font color='green'><strong>"+"<%:Dler Cloud Login Successful%>"+"</strong></font>";
							window.location.href='<%="settings?tab.openclash.config=dlercloud"%>';
						}
						else {
							s.innerHTML ="<font color='red'><strong>"+"<%:Dler Cloud Login Faild%>"+"</strong></font>";
							window.location.href='<%="settings?tab.openclash.config=dlercloud"%>';
						}
					}
					btn.disabled = false;
					btn.value = '<%:Login Account%>';
					});
				}
			});
		};
		
		if (option == "dler_logout") {
			var s = document.getElementById('dler_login-status');
			btn.value = '<%:Logout...%>';
			XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "dler_logout")%>', null, function(x, status) {
				if (s)
				{
					if (x && x.status == 200 && status.dler_logout == 200) {
						s.innerHTML ="<font color='green'><strong>"+"<%:Dler Cloud Logout Successful%>"+"</strong></font>";
						window.location.href='<%="settings?tab.openclash.config=dlercloud"%>';
					}
					else {
						s.innerHTML ="<font color='red'><strong>"+"<%:Dler Cloud Logout Faild%>"+"</strong></font>";
					}
				}
				btn.disabled = false;
				btn.value = '<%:Logout Account%>';
				}
			);
		};
		return false;
	}
//]]></script>
<input type="button" class="btn cbi-button cbi-button-apply" value="<%:Login Account%>" onclick="return dler_login(this,'dler_login')" />
<input type="button" class="btn cbi-button cbi-button-remove" value="<%:Logout Account%>" onclick="return dler_login(this,'dler_logout')" />
<span id="<%=self.option%>-status"><%=self.value%></span>
<%+cbi/valuefooter%>
