// JavaScript Document
index_list = new Array(8);
function default_value()
{
	for(i=1;i<=8;i++){
		index_list[i-1] = document.getElementById("user_right"+i).value;
	}
}
function restore_default()
{
	for(i=1;i<=8;i++){
		document.getElementById("user_right"+i).value = index_list[i-1];
		check_time_scale(i);
	}
}
function valcheck()
{
	var bg = 'user_right';
	var name = 'user_name';
	var pass = 'user_pass';
	var no_acc=0,view=0,oper=0,admin=0;
	var v_space=0,o_space=0,a_space=0;
	var pv_space=0,po_space=0,pa_space=0;
	for(i=1;i<=8;i++){
	    switch(document.getElementById(bg+i).value){
		case '0':
			no_acc++; break;
		case '1':
			if(document.getElementById(name+i).value == "")
				v_space++;
			if(document.getElementById(pass+i).value == "")
				pv_space++;
			//chiao: add the follow
			if((checkusernamelen(i) == false) ||(checkuserpasslen(i) == false) )
				return false;
			view++; break;
		case '2':
			if(document.getElementById(name+i).value == "")
				o_space++;
			if(document.getElementById(pass+i).value == "")
				po_space++;	
			//chiao: add the follow
			if((checkusernamelen(i) == false) ||(checkuserpasslen(i) == false) )
				return false;
			oper++; break;
		case '4':
			if(document.getElementById(name+i).value == "")
				a_space++;
			if(document.getElementById(pass+i).value == "")
				pa_space++;	
			//chiao: add the follow
			if((checkusernamelen(i) == false) ||(checkuserpasslen(i) == false) )
				return false;
			admin++; break;
	   }
	}
	if(admin==0){
		alert("帐号中至少应有一个具有管理员的权限");
		document.getElementById(bg+1).focus();
		//restore_default();
		return false;
	}
	
	if((admin>0)&&(no_acc<=7)){
		if((a_space>0)||(o_space>0)||(v_space>0)) {
			alert("帐户的用户名称不能为空。");
			//restore_default();
			return false;
		}
		if((pa_space>0)||(po_space>0)||(pv_space>0)) {
			alert("帐户的用户密码不能为空。");
			//restore_default();
			return false;
		}
	}
 
	if((admin==1)&&(no_acc==7))
		return true;
	
	for(i=1;i<=7;i++)
	  for(j=i+1;j<=8;j++){
		if ((document.getElementById("user_name"+i).value==document.getElementById("user_name"+j).value)&&
			(document.getElementById("user_right"+i).value!="0") &&
			(document.getElementById("user_right"+j).value!="0") ) {
			alert("存在重复的用户名称。");
			//restore_default();
			return false;				
		}
	  }
	
	return true;
}
 
function check_user()
{
	for (i=1;i<=8;i++) {
		check_time_scale(i);
	}
	EnableSIPService_change();
}
 
function check_time_scale(i)
{
	switch(document.getElementById("user_right"+i).value){
		case '0':
			document.getElementById("user_name"+i).disabled = 1;
			document.getElementById("user_pass"+i).disabled = 1;
			break;
		case '1':
		case '2':
		case '4':
			document.getElementById("user_name"+i).disabled = 0;
			document.getElementById("user_pass"+i).disabled = 0;
			break;
		}
}
 
function reset_all()
{
	document.form1.reset();
	check_user();
}
 
function EnableSIPService_change() {
  var myform = document.form1;
  if(!document.getElementById("EnableSIPService_alian").checked)  {
	myform.SIPUsername.disabled = 1;
	myform.SIPPassword.disabled = 1;
	myform.SIPServerIP.disabled = 1;
	myform.SIPServerPort.disabled = 1;
  }
  else {
	myform.SIPUsername.disabled = 0;
	myform.SIPPassword.disabled = 0;
	myform.SIPServerIP.disabled = 0;
	myform.SIPServerPort.disabled = 0;  	
  }
}
 
//chiao
function checkusernamelen(i)
{
	if(document.getElementById("user_name"+i).value.length > 15)
	{
		alert("用户名长度应该小于或等于15个字符。");
		document.getElementById("user_name"+i).focus();
		return false;
	}
	return true;
}
 
function checkuserpasslen(i)
{
	if(document.getElementById("user_pass"+i).value.length > 15)
	{
		alert("口令长度应该小于或等于15个字符。");
		document.getElementById("user_pass"+i).focus();
		return false;
	}
 
	return true;
}