var ServiceStatus = 1;
var encryptStatus = 0;
//var CurrentKeyType = keytype.selectedIndex;
//alert("c4");
var autochannelEnable = "1";
var Run_Wizard = document.getElementById("Run_Wizard");

var channel = document.getElementById("channel");
  
function WizardChange_Auto_Channel_Scan(){
if(AutoChannelScan!=null){
	if(AutoChannelScan.checked==true)
   		channel.disabled = true;
   	else
   		channel.disabled = false;
   	}	
}
function check_autochannelScanEnable()
{
if(channel!=null)
{
	if(autochannelEnable==1)
    		channel.disabled = true;
    	else
    		channel.disabled = false;
    //alert(autochannelEnable);	
}    
}
function InitialSettings()
{
    Change_Device_Name();
    CheckMaxLength();
    if(cipher != null)
    {
        cipher.value = 3;
        cipher.disabled = true;
    } 
    WizardChange_Auto_Channel_Scan();   
}


function WizardExit(i)
{
    if (confirm("Quit setup wizard without saving settings ?"))
        location.href = "HomeWizard.html";
    else
        formSubmit(i);    
}


function Check_WPAPSK()
{
   if(passphrase != null)
	{
		if(passphrase.value.length < 8 || passphrase.value.length > 63)
		{
			alert( "The length of 'PassPhrase' must be 8 to 63.");
			passphrase.select();
			return false;
		}
	}
			
	if(groupkey != null)
	{
		if(isNaN(groupkey.value) || groupkey.value < 300 || groupkey.value > 9999999)
		{
			alert( "The range of 'Group Key Update Interval' is 300 to 9999999.");
			groupkey.select();
			return false;
		}
	}
	
	if(passphrase != null || groupkey != null)
	  return true;
	else 
	  return false;   
}    
function CheckMaxLength()
{
    //if(keytype != null)  
    //CurrentKeyType = keytype.selectedIndex;    
     
    //alert("KeySize1="+KeySize1+"KeySize2="+KeySize2+"KeySize3="+KeySize3+"CurrentKeyType="+CurrentKeyType);    
    if((KeySize1 != null) && (KeySize2 != null) && (KeySize3 != null))
	{	
        if(keytype.selectedIndex == 0)
        {	    
	        if(KeySize1.checked==true)//64 bits
            {
	            if(key.value.length!=10)
	                key.value="**********"; 
		        key.maxLength=10;
	        }
	        if(KeySize2.checked==true)//128 bits
	        {
	            if(key.value.length!=26)
	                key.value="**************************";     
		        key.maxLength=26;
	        }
	        if(KeySize3.checked==true)//152 bits
	        {
	            if(key.value.length!=32)
	                key.value="********************************";	        
		        key.maxLength=32;
	        }
	     }   
	      if(keytype.selectedIndex == 1)
	      {
	        if(KeySize1.checked==true)//64 bits
            {
	            key.value=""; 
		        key.maxLength=5;
	        }
	        if(KeySize2.checked==true)//128 bits
	        {
	            key.value="";     
		        key.maxLength=13;
	        }
	        if(KeySize3.checked==true)//152 bits
	        {
	            key.value="";	        
		        key.maxLength=16;
	        }
	      }
	}
}

function Key_Check()
{
    //if(keytype != null)
    //CurrentKeyType = keytype.selectedIndex;
    
  	if((key != null) && (KeySize1 != null) && (KeySize2 != null) &&(KeySize3 != null))
  	{
	    if(key.value == "" )
    	{
	        alert(" No Key values ! ");
	    	return false;
    	}
    	
        if(keytype.selectedIndex == 0)
        {
	        if(KeySize1.checked==true)//64 bits
    	    {
	       	    if(key.value.length!=0 && key.value!="**********" )
         	    {
            	    if(key.value.length!=10 )
        	        {
			            alert("Please input 10 Hexadecimal digits!");
          		        key.select();
         	            return false;
         	        }
			        if(!HexCheck2(key.value))
		            {
                	    alert("Please input hexadecimal digits!");
                	    key.select();
         	            return false;
         	        }
       	        }
	        }

      	    if(KeySize2.checked==true)//128 bits
	        {
	  	        if(key.value.length!=0 && key.value!="**************************" )
       	        {
        	     if(key.value.length!=26 )
        	        {
         	            alert("Please input 26 Hexadecimal digits!");
         	            key.select();
        	            return false;
        	        }
          	        if(!HexCheck2(key.value))
         	        {
            	        alert("Please input hexadecimal digits!");
                        key.select();
         	            return false;
         	        }
                }
	        }
		
	        if(KeySize3.checked==true)//152 bits
	        {
	            if(key.value.length!=0 && key.value!="********************************" )
      	        {
            	    if(key.value.length!=32 )
        	        {
        	            alert("Please input 32 Hexadecimal digits!");
        	            key.select();
         	            return false;
         	        }
         	        if(!HexCheck2(key.value))
        	        {
           	            alert("Please input hexadecimal digits!");
           	            key.select();
        	            return false;
        	        }
     	        }
	        }
	    }
	    if(keytype.selectedIndex == 1) //ASCII
	    {
	        if(KeySize1.checked==true)//64 bits
	        {
			    if(key.value.length != 0 && key.value != "")
			    {
			        if(key.value.length != 5)
                    {
                        alert("Please input 5 characters!");
                        key.select();
                        return false;
                    }
			    }	            
	        }
	        if(KeySize2.checked==true)//128 bits
	        {
			    if(key.value.length != 0 && key.value != "")
			    {
			        if(key.value.length != 13)
                    {
                        alert("Please input 13 characters!");
                        key.select();
                        return false;
                    }
			    }		            
	        }
	        if(KeySize3.checked==true)//152 bits
	        {
			    if(key.value.length != 0 && key.value != "")
			    {
			        if(key.value.length != 16)
                    {
                        alert("Please input 16 characters!");
                        key.select();
                        return false;
                    }
			    }		            
	        }	
	        
			if(!ASCIICheck(key.value))
			{
			    alert("The String has invalid character.Please input again!");
			    return false;
			}
			else
			{
			  key.maxLength = 32;
			  key.value = ASCIIToHex(key.value);
			}	                	        	        
	    
	    }   
	}     		  
   return true;  
}  
 
function formSubmit(arg)
{
    
  if(arg == 12) 
  {
    //if(hide)
    //    hide.value = 0;      
    if(Verify_Entry())
		  document.forms[0].submit();      
  }
  else if(arg == 10) //Click SiteSurvey
  {
    //if(hide)
    //    hide.value = 1;
    document.forms[0].submit();
  }
  else if(arg == 9)//Security Level set
  {
    if(no.checked) 
        formSubmit(7);
    if(wpa.checked) 
        formSubmit(5);
    if(psk.checked) 
        formSubmit(6);                     
  }  
  else if(arg == 8) //reboot page back
  {
    if(encryptStatus == 0) 
        formSubmit(4);
    if(encryptStatus == 1) 
        formSubmit(5);
    if(encryptStatus == 2) 
        formSubmit(6);                     
  }   
  else  
  {
    //if(hide)
    //    hide.value = 0;      
    Run_Wizard.value = arg;
    document.forms[0].submit();
  }    
}

function Verify_Entry()
{
  Run_Wizard.value = 12; 
  return true;
}

function CheckPwd() 
{
    if(NewPwd != null && CfmNewPwd != null)
    {
 	    if(NewPwd.value!= CfmNewPwd.value)
 	    {
 		    alert("\n The Password is not match!"+
 		        "\n Please Enter Again.");
 		    CfmNewPwd.select();
 		    return false;
 	    }
    }
    
    return true;  
    
 }
 
function Wireless_Mode_Check()
{
    if(WizardRootSsid != null)
    {
	    if(WizardRootSsid.value=="")
	    {
		    alert("Please input SSID.");
		    WizardRootSsid.value="";
		    WizardRootSsid.focus();
		    return false;   
	    }
	    if (WizardRootSsid.value != "")
        {
  	        var ssid_string = WizardRootSsid.value;
            var ssid_index = ssid_string.indexOf(" ");
     
            if (ssid_index == 0)
            {
        	    alert("The first character can't be blank.");
    	        WizardRootSsid.value="";
    	        WizardRootSsid.focus();
    	        return false;
            }
        }
    }  
	return true;  
}

function Change_Wizard_Key_Type_Or_Size()
{
    //if(keytype != null)
    //CurrentKeyType = keytype.selectedIndex;
    
    if((KeySize1 != null) && (KeySize2 != null) && (KeySize3 != null))
	{    
      // alert("CurrentKeyType"+CurrentKeyType);
        if(KeySize1.checked==true)
        {
            if(keytype.selectedIndex == 0) //HEX
	  		{
	  		    key.value = "**********";
	  		    key.maxLength = 10;
	  		}    

	  	    else
	  	    {
	            key.value = "";
	            key.maxLength = 5;
	        } 
	    }
        if(KeySize2.checked==true)
        {
            if(keytype.selectedIndex == 0) //HEX
	  		{
	  		    key.value = "**************************";
	  		    key.maxLength = 26;
	  		}    

	  	    else
	  	    {
	            key.value = "";
	            key.maxLength = 13;
	        } 
	    }	  
	    
        if(KeySize3.checked==true)
        {
            if(keytype.selectedIndex == 0) //HEX
	  		{            
	  		    key.value = "********************************";
	  		    key.maxLength = 32;
	  		}    

	  	    else
	  	    {
	            key.value = "";
	            key.maxLength = 16;
	        } 
	    }      
    }
}
