// JavaScript Document
function doddnsreg()
{
  var form = document.formddnsreg;
  if (confirm("现在将连接到vipcam.cn，注册一个DDNS。\n请确认您现在可以访问互联网络。"))
  {
    //form.submit()
	window.open("http://www.vipcam.cn/","New_window");
  }
}
 
function dhcp_enable(dhcp)
{
	if (dhcp=="static")
	{
		document.form1.StaticIP.disabled = 0;
		document.form1.SubnetMask.disabled = 0;
		document.form1.DefaultGatewayIP.disabled = 0;
		document.form1.DNSSvr1.disabled = 0;
		document.form1.DNSSvr2.disabled = 0;
		document.form1.DNS_AUTO.value = 0;
	}
	else
	{
		document.form1.StaticIP.disabled = 1;
		document.form1.SubnetMask.disabled = 1;
		document.form1.DefaultGatewayIP.disabled = 1;
		if(document.form1.DNS_AUTO.value == 1)
		{
			document.form1.DNSSvr1.disabled = 1;
			document.form1.DNSSvr2.disabled = 1;
		}
	}
}
 
function dns_auto(auto)
{
	if(auto == 0)
	{
		document.form1.DNSSvr1.disabled = 0;
		document.form1.DNSSvr2.disabled = 0;
	}
	else
	{
		document.form1.DNSSvr1.disabled = 1;
		document.form1.DNSSvr2.disabled = 1;
		document.form1.ObtainIP_method.value = "dynamic";
		dhcp_enable("dynamic");
	}
}
 
function enable_pppoe(enable)
{
	if(enable == 1)
	{
		document.form1.PPPoE_UserAccount.disabled = 0;
		document.form1.PPPoE_Passwd.disabled = 0;
		document.form1.PPPOE_SERVICENAME.disabled = 0;
		document.form1.PPPOE_CLAMPMSS.disabled = 0;
		document.form1.PPPOE_MAX_IDLE_TIME.disabled = 0;
		document.getElementById("id_PPPOE_AUTO_REDIAL").disabled = 0;
		document.getElementById("id_PPPOE_AUTO_DNS").disabled = 0;	
	}
	else if(enable > 1)
	{
		document.form1.PPPoE_UserAccount.disabled = 0;
		document.form1.PPPoE_Passwd.disabled = 0;
		document.form1.PPPOE_SERVICENAME.disabled = 1;
		document.form1.PPPOE_CLAMPMSS.disabled = 1;
		document.form1.PPPOE_MAX_IDLE_TIME.disabled = 0;
		document.getElementById("id_PPPOE_AUTO_REDIAL").disabled = 0;
		document.getElementById("id_PPPOE_AUTO_DNS").disabled = 0;
	}
	else
	{
		document.form1.PPPoE_UserAccount.disabled = 1;
		document.form1.PPPoE_Passwd.disabled = 1;
		document.form1.PPPOE_SERVICENAME.disabled = 1;
		document.form1.PPPOE_CLAMPMSS.disabled = 1;
		document.form1.PPPOE_MAX_IDLE_TIME.disabled = 1;
		document.getElementById("id_PPPOE_AUTO_REDIAL").disabled = 1;
		document.getElementById("id_PPPOE_AUTO_DNS").disabled = 1;
	}
}
function reset_all()
{
	document.form1.reset();
	dhcp_enable(document.form1.ObtainIP_method.value);
	enable_pppoe(document.getElementById('idPPPoE_enable').value);
	ddns_change(document.getElementById('ddns').value);
	onNetworkMode(document.getElementById('Wifi_enable').value);//hrt add
}
function ddns_change(value)
{
	if(value == 0)
	{
		//document.getElementById('ddns_button').disabled = 1;
		document.getElementById('dd_domain').disabled = 1;
		document.getElementById('dd_loginname').disabled = 1;
		document.getElementById('dd_pass').disabled= 1;
		document.getElementById('useoutip').disabled= 1;
	}
	else
	{
		//document.getElementById('ddns_button').disabled = 0;
		//if (document.getElementById('ddns').value=="2")	 {	//VIPCAM.CN
			//document.getElementById('dd_domain').disabled = 1;
			//dd_loginname_change(document.getElementById('dd_loginname').value);
		//} else
		document.getElementById('dd_domain').disabled = 0;
		document.getElementById('dd_loginname').disabled = 0;
		document.getElementById('dd_pass').disabled= 0;
		if (document.getElementById('ddns').value=="2")
		{
			document.getElementById('useoutip').disabled= 0;
		}
		else
		{
			if (document.getElementById('ddns').value=="1")
				document.getElementById('useoutip').value= "1";
			document.getElementById('useoutip').disabled= 1;			
		}
	}
}
 
function dd_loginname_change(value)
{
	if (document.getElementById('ddns').value=="2")
	{	//VIPCAM.CN
		document.getElementById('dd_domain').value = value+".vipcam.cn";
	}
}
 
function checkessid()
{
	if(document.getElementById('Wifi_ApEssid').value.length > 32 )
	{
		alert("essid长度应该小于或等于32个字符。");
		return 0;
	}
	return 1;
}
 
function settcpip()
{
	var element;
 
	//chiao:check for ip and port valid
	element = document.getElementById('StaticIP');
	if(checinetaddrvalid(element.value) == false)
	{
		element.focus();
		return false;
	}
	
	element = document.getElementById('SubnetMask');
	if(checinetaddrvalid(element.value) == false)
	{
		element.focus();
		return false;
	}
 
	
	element = document.getElementById('DefaultGatewayIP');
	if(checinetaddrvalid(element.value) == false)
	{
		element.focus();
		return false;
	}
 
	
	element = document.getElementById('DNSSvr1');
	if(checinetaddrvalid(element.value) == false)
	{
		element.focus();
		return false;
	}
		
	
	element = document.getElementById('DNSSvr2');
	if(checinetaddrvalid(element.value) == false)
	{
		element.focus();
		return false;
	}
 
	element = document.getElementById('HTTP_Port');
	if(checinetportvalid(element.value) == false)
	{
		element.focus();
		return false;
	}
 
 
  if (confirm("重启IPCamera需要大概60秒钟，\n您是否需要重启？")) {
	var enAutoRedial = !document.getElementById("id_PPPOE_AUTO_REDIAL").disabled;
	var enAudoDns = !document.getElementById("id_PPPOE_AUTO_DNS").disabled;
	if (!enAutoRedial) {
		document.getElementById("id_PPPOE_AUTO_REDIAL").disabled = 0;
	}
	if (document.getElementById("id_PPPOE_AUTO_REDIAL").checked) {
		document.getElementById("hide_PPPOE_AUTO_REDIAL").value = 1;
	} else {
		document.getElementById("hide_PPPOE_AUTO_REDIAL").value = 0;
	}
	if (!enAutoRedial) {
		document.getElementById("id_PPPOE_AUTO_REDIAL").disabled = 1;
	}
	
	if (!enAudoDns) {
		document.getElementById("id_PPPOE_AUTO_DNS").disabled = 0;
	}
	if (document.getElementById("id_PPPOE_AUTO_DNS").checked) {
		document.getElementById("hide_PPPOE_AUTO_DNS").value = 1;
	} else {
		document.getElementById("hide_PPPOE_AUTO_DNS").value = 0;
	}
	if (!enAudoDns){
		document.getElementById("id_PPPOE_AUTO_DNS").disabled = 1;
	}
 
	document.form1.submit()
  }
}
function updateddns()
{
  //wopen('../cgi-bin/ddnsupdate.cgi','_blank',400,300);
  //alert("enter ddns");
  var dns_provider;
  dns_provider = document.getElementById("ddns").value;
  if(dns_provider == 0)
  {
  	alert("请选择域名提供商");
  	return;
  }
  else if(dns_provider == 1)
  {
  	dns_provider = "3322.org";
  }
  else if(dns_provider == 2)
  {
  	dns_provider = "vipcam.cn";
  }
  
  var ddns_domain;
  ddns_domain = document.getElementById("dd_domain").value;
  if(ddns_domain == "")
  {
  	alert("请输入域名");
  	return;
  }
  var ddns_user;
  ddns_user = document.getElementById("dd_loginname").value;
  if(ddns_user == "")
  {
  	alert("请输入用户名称");
  	return;
  }
  var ddns_pass;
  ddns_pass = document.getElementById("dd_pass").value;
  if(ddns_pass == "")
  {
  	alert("请输入密码");
  	return;
  }
  var outip;
  outip = document.getElementById("useoutip").value;
  
  var s="/cgi-bin/action?lang=chs&action=ddnsupdate&dns_provider="+dns_provider +
  	"&domain="+ddns_domain+"&user="+ddns_user+"&ddns_pass="+ddns_pass + "&outip="+outip;
  //alert(s);
  parent.frames["msgFrame"].location.replace(s) ; //need parameters
  //alert("exit ddns");
}
 
//chiao:
function   checinetaddrvalid(inetaddr)
{  
  //var   ipstr = document.getElementById(IDstr).value;
  var   reg=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/  
  if(arr=inetaddr.match(reg)){  
	for(i=1;i<arr.length;i++) if(parseInt(arr[i])>255) { alert("非法输入，"+arr[i]+"不在[0,255]内。"); return false; }  
 
  }  
  else  {
	alert("非法输入，"+inetaddr+"格式不正确。");
	return false;
  }  
 
  return true;
 }   
 
 function fucCheckNUM(NUM)
{
	var i,j,strTemp;
 
	strTemp="0123456789";
 
	if ( NUM.length== 0)
		return false;
 
	for (i=0;i<=(NUM.length-1);i++)
	{
		j=strTemp.indexOf(NUM.charAt(i));
		if (j==-1)
		{
			return false;
		}
	}
 
	return true;
}
 
 function checinetportvalid(inetport)
 {
	if(fucCheckNUM(inetport) == false)
	{
		alert("非法的端口格式。");
		return false;
	}
 
	var portval = parseInt(inetport);
 
	if(portval > 65535 || portval < 0)
	{
		alert("端口值应大于0，小于65535。");
		return false;
	}
	return true;
 }
 
 function en_radio_adv_cfg(en)
 {
	if (en == 1)
	{
		document.getElementById('Radio_DialNum').disabled= 0;
		document.getElementById('Radio_APN').disabled= 0;
	}
	else
	{
		document.getElementById('Radio_DialNum').disabled= 1;
		document.getElementById('Radio_APN').disabled= 1;
	}
 }
 