
//document.write("<>")
//document.write("<input type=hidden name=userid value="+getCookie("userid")+">");


function getCookie(name)
{
	var arg = escape(name) + "=";
	var nameLen = arg.length;
	var cookieLen = document.cookie.length;
	var i = 0;
   
	while (i < cookieLen) {
		var j = i + nameLen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieValueByIndex(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;

}
function getCookieValueByIndex(startIndex) {
	var endIndex = document.cookie.indexOf(";", startIndex);
	if (endIndex == -1) 
		endIndex = document.cookie.length;
	return unescape(document.cookie.substring(startIndex, endIndex));
}


  
function parse_form()
{  //get URL's value
    
   var pairs = getAnsString(document.location.href).split("&");
	var pair;
   for (pair in pairs)
   {
       local = pairs[pair].split("=");
       Config[local[0]] = local[1];
   }
}
function Config(id)
{	
	 this.id = id;   
}

function getAnsString(str)
{  //get value
   var Strn,NumStart,NumLong;
   NumStart = str.lastIndexOf("?") + 1;
   NumLong = str.length;
   Strn = str.substring(NumStart,NumLong);
   return Strn ;
}

function menu_title(index)
{	
	
	var menu_page = new Array()	
	menu_page[0] = "web/nvr/liveview.asp",	
	menu_page[1] = "web/nvr/playback.asp",
	menu_page[2] = "web/main_menu.asp",
	menu_page[3] = "web/advanced/user_account.asp",
	menu_page[4] = "web/nvr/ipcam_status.asp",
	menu_page[5] = "web/help/help.asp"
	

	
	//menu_page[5] = LOGIN_PAGE
	
	var menu_name = new Array()	
	//menu_name[0] = "Live Video",
	menu_name[0] = msg_menu1,
	menu_name[1] = msg_menu2,
	menu_name[2] = msg_menu3,
	menu_name[3] = msg_menu4,
	menu_name[4] = msg_menu5,
	menu_name[5] = msg_menu6
	//menu_name[5] = "LOGOUT"	

	for (i = 0;i<menu_name.length;i++)
	{	
				
		if (index == i )		
		{			
			document.write("<td id='topnavon'><a href=/goform/CfgTimeOut?url="+ menu_page[i] +">"+ menu_name[i] +"</a></td>")
		}	
		else
			{			
			document.write("<td id='topnavoff'><a href=/goform/CfgTimeOut?url="+ menu_page[i] +">"+ menu_name[i] +"</a></td>")		
		}
			
	}	
		
}
function menu_title_guest(index)
{	
	
	var menu_page = new Array()	
	menu_page[0] = "web/nvr/user_liveview.asp",	
	menu_page[1] = "web/nvr/user_playback.asp",
	menu_page[2] = "web/nvr/user_help.asp"	
			
	var menu_name = new Array()	
	menu_name[0] = msg_menu1,
	menu_name[1] = msg_menu2,
	menu_name[2] = msg_menu6
	
	for (i = 0;i<menu_name.length;i++)
	{	
				
		if (index == i )		
		{			
			document.write("<td id='topnavon'><a href=/goform/CfgTimeOutUser?url="+ menu_page[i] +">"+ menu_name[i] +"</a></td>")
		}	
		else
			{			
			document.write("<td id='topnavoff'><a href=/goform/CfgTimeOutUser?url="+ menu_page[i] +">"+ menu_name[i] +"</a></td>")		
		}
			
	}	
	document.write("<TD width='250' colspan='2'>&nbsp;</TD>");
	 
		
}

function menu_setup(index)
{	
	
	var menu_page = new Array("web/main_menu.asp","web/tools/network.asp","web/nvr/ipcam_upnp.asp","web/nvr/ipcam_settings.asp","web/nvr/ipcam_recordsetting.asp","web/nvr/liveserver.asp","web/nvr/ipcam_schedule-beta.asp","web/nvr/ipcam_event.asp","web/nvr/smtp.asp","web/tools/ddns.asp","web/tools/timezone.asp");
	var menu_name = new Array(msg_menu_setup1,msg_menu_setup2,msg_menu_setup3,msg_menu_setup4,msg_menu_setup5,msg_menu_setup6,msg_menu_setup7,msg_menu_setup8,msg_menu_setup9,msg_menu_setup10,msg_menu_setup11);
	

	for (i = 0;i<menu_name.length;i++)
	{								
		if (index == i )		
		{			
			document.write("<LI><DIV id=sidenavoff>"+menu_name[i]+"</DIV>");
		}	
		else
		{
				document.write("<LI><DIV><a href=/goform/CfgTimeOut?url="+menu_page[i]+">" +menu_name[i]+"</a></DIV>");
		}
			
	}
		document.write("<LI><DIV><a href=/goform/formLogout>"+msg_menu_logout+"</a></DIV>");			
}

function menu_maintenance(index)
{	
	
	var menu_page = new Array("web/advanced/user_account.asp","web/formatHD/raid_wizard0.asp","web/tools/backup.asp","web/tools/firmware_upload.asp")		
	var menu_name = new Array(msg_menu_maintenance1,msg_menu_maintenance2,msg_menu_maintenance3,msg_menu_maintenance4)	
	

	for (i = 0;i<menu_name.length;i++)
	{								
		if (index == i )		
		{			
			document.write("<LI><DIV id=sidenavoff>"+menu_name[i]+"</DIV>");
		}	
		else
		{
				document.write("<LI><DIV><a href=/goform/CfgTimeOut?url="+menu_page[i]+">" +menu_name[i]+"</a></DIV>");
		}
			
	}			
				document.write("<LI><DIV><a href=/goform/formLogout>"+msg_menu_logout+"</DIV></a>");
}

function menu_status(index)
{	
	
	var menu_page = new Array("web/nvr/ipcam_status.asp","web/status/device_status.asp","web/status/log.asp","web/nvr/nvrlog.asp");
	var menu_name = new Array(msg_menu_status1,msg_menu_status2,msg_menu_status3,msg_menu_status4);
	

	for (i = 0;i<menu_name.length;i++)
	{								
		if (index == i )					
			document.write("<LI><DIV id=sidenavoff>"+menu_name[i]+"</DIV>");
		else					
				document.write("<LI><DIV><a href=/goform/CfgTimeOut?url="+menu_page[i]+">" +menu_name[i]+"</a></DIV>");					
	}
	document.write("<LI><DIV><a href=/goform/formLogout>"+msg_menu_logout+"</a></DIV>");			
}

function menu_help(index)
{	
	
	var menu_page = new Array("web/help/help.asp","web/help/help_livevideo.asp","web/help/help_playback.asp","web/help/help_setup.asp","web/help/help_maintenance.asp","web/help/help_status.asp");
	var menu_name = new Array(msg_menu_help1,msg_menu_help2,msg_menu_help3,msg_menu_help4,msg_menu_help5,msg_menu_help6);

	for (i = 0;i<menu_name.length;i++)
	{								
		if (index == i )					
			document.write("<LI><DIV id=sidenavoff>"+menu_name[i]+"</DIV>");
		else					
				document.write("<LI><DIV><a href=/goform/CfgTimeOut?url="+menu_page[i]+">" +menu_name[i]+"</a></DIV>");					
	}
	document.write("<LI><DIV><a href=/goform/formLogout>"+msg_menu_logout+"</a></DIV>");			
}

function menu_help_guest(index)
{
	var menu_page = new Array("web/nvr/user_help.asp","web/nvr/user_help_livevideo.asp","web/nvr/user_help_playback.asp");
	var menu_name = new Array(msg_menu_help1,msg_menu_help2,msg_menu_help3);

	for (i = 0;i<menu_name.length;i++)
	{								
		if (index == i )					
			document.write("<LI><DIV id=sidenavoff>"+menu_name[i]+"</DIV>");
		else					
				document.write("<LI><DIV><a href=/goform/CfgTimeOutUser?url="+menu_page[i]+">" +menu_name[i]+"</a></DIV>");					
	}
	document.write("<LI><DIV><a href=/goform/user_logout>"+msg_menu_logout+"</a></DIV>");			
}
function makeRequest(url) 
{	 
	http_request = false;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,IE7...
      
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject)  // IE
    {
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) 
    {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }

    http_request.onreadystatechange = alertContents;
    http_request.open('POST', url, true);
    
    
    http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	http_request.setRequestHeader('Content-Type','text/html');
	http_request.setRequestHeader('Content-Type','charset=UTF-8');
	http_request.setRequestHeader('Pragma','no-cache');
	http_request.setRequestHeader('Cache-Control', 'no-cache');
	
    http_request.send(null);
}
function ChkIPAddress(ipaddr)
{
	if(ipaddr=="127.0.0.1")	
	{
		return 1;
	}
	else
		return 0;

}


var _idle_time_init = true;
function idle_time(time)
{
	
	if ( _idle_time_init == true )
	{
		time = 0;
		_idle_time_init = false;		
	}
	
	
	if (time == _idle_limit)
	{	
		if (typeof(window.opener) != "undefined")
    	{        		        		    		
    		window.close();    		    		
    	}
    	else
			location.replace('/web/login.asp?status=timeout')		
	}	
	time++;		
	timerID = setTimeout("idle_time("+time+")",1000) 		
}

 function check_login(status)
    {    
        if (status==0)
        {   
        	if (typeof(window.opener) != "undefined")
        	{        		        		
        		opener.location.href="login.asp";		
        		window.close();
        		
        	}
            else            	 	
            location.href="../login.asp";                                    
        }
    }    
    
  
function parse_form()
{  //get URL's value
    
   var pairs = getAnsString(document.location.href).split("&");
	var pair;
   for (pair in pairs)
   {
       local = pairs[pair].split("=");
       Config[local[0]] = local[1];
   }
}
function Config(id)
{	
	 this.id = id;   
}

function getAnsString(str)
{  //get value
   var Strn,NumStart,NumLong;
   NumStart = str.lastIndexOf("?") + 1;
   NumLong = str.length;
   Strn = str.substring(NumStart,NumLong);
   return Strn ;
}    

function display(eid,str) {
	if (document.getElementById(eid) == null) return;		
	
	if (document.getElementById(eid).type == "button" || document.getElementById(eid).type == "submit" || document.getElementById(eid).type == "reset" )
		document.getElementById(eid).value = str;
	else	
	document.getElementById(eid).innerHTML = str;
}

function ip_same_net(ip,netmask,gateway)
{
	var mt1 = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	var mt2 = netmask.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	var mt3 = gateway.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	
	if (((mt1[1]&mt2[1]) == (mt2[1]&mt3[1])) && ((mt1[2]&mt2[2]) == (mt2[2]&mt3[2])) && ((mt1[3]&mt2[3]) == (mt2[3]&mt3[3])) && ((mt1[4]&mt2[4]) == (mt2[4]&mt3[4])))
		return 0
	else
		return 1	
		
}
/*
function ip_range(ip,netmask,gateway)
{
	var mt1 = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	var mt2 = netmask.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	var mt3 = gateway.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	
	
	if ( (mt1[1]&mt2[1] == mt3[1] ) && (mt1[2]&mt2[2] == mt3[2] ) && (mt1[3]&mt2[3] == mt3[3] ) && (mt1[4]&mt2[4] == mt3[4] ))
	{
		alert("This is an invalid ip.")
		return 1;
	}
	
	var start_ip = new Array(mt1[1]&mt2[1],mt1[2]&mt2[2],mt1[3]&mt2[3],mt1[4]&mt2[4]);
	var stop_ip = new Array();
	for (i = 0;i<4;i++)
	{	
		if (mt2[i+1] == 0)
		{
			stop_ip[i] = 255;
		}
		else if( mt2[i+1] == 255)
		{
			stop_ip[i] = start_ip[i];
		}

		else
		{
			stop_ip[i] = start_ip[i]*2 -1;
		}
		alert(stop_ip[i]);
	}

	if ( (stop_ip[0] == mt3[1] ) && (stop_ip[1] == mt3[2] ) && (stop_ip[2] == mt3[3] ) && (stop_ip[3] == mt3[4] ))
	{
		alert("This is an invalid ip.")
		return 1
	}
	return 0;

}
*/
var subnet_mask = new Array(0, 128, 192, 224, 240, 248, 252, 254, 255);
function check_mask(my_mask){
	//var temp_mask = my_mask.addr;	
	//var temp_mask = my_mask;						
	var temp_mask = my_mask.split(".")// my_mask.addr;	
	var in_range = false;
	var error;
	
	for (var i = 0; i < temp_mask.length; i++){	
		var mask = parseInt(temp_mask[i],10);
						
		for (var j = 0; j < subnet_mask.length; j++){			
			if (mask == subnet_mask[j]){							
				
				in_range = true;
				break;
			}else{
				
				in_range = false;
			}
		}
		
		if (!in_range){	// when not in the subnet mask range
		
			error = "The subnetmask you input is incorrect.The subnetmask should be continuous."
			alert(error);
			return false;
		}
		
	}
	return true;
}


function ip_range(_ip,_netmask,_gateway)
{
	//var ip = new Array(2,66,74,58);
	//var mask = new Array(255,255,255,224);
	var ip = _ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	var mask = _netmask.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	var stop_range = new Array(0,0,0,0);
	var start_range;


	var mt1 = _ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	var mt2 = _netmask.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	var mt3 = _gateway.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	
	for (var i =0;i<4;i++)
	{
		if (mask[i+1] == 255)
		{
			stop_range[i] = ip[i+1];	
		}
		else
		{
			start_range = mask[i+1]&ip[i+1];
		
			for (var j=0;j<8;j++)
			{			
				if (start_range&1)
				{						
					stop_range[i]+=Math.pow(2,j);
					break;
					
				}
				else
				{
					stop_range[i]+=Math.pow(2,j);
				}		
				start_range>>=1;				
			}	
		}
	}
	//for (i=0;i<4;i++)
	//	alert(stop_range[i]);
		
	if ( (stop_range[0] == mt3[1] ) && (stop_range[1] == mt3[2] ) && (stop_range[2] == mt3[3] ) && (stop_range[3] == mt3[4] ))
	{			
		return 1
	}
					
	var start_ip = new Array(mt1[1]&mt2[1],mt1[2]&mt2[2],mt1[3]&mt2[3],mt1[4]&mt2[4]);
	
	
	if ( (start_ip[0] == mt3[1] ) && (start_ip[1] == mt3[2] ) && (start_ip[2] == mt3[3] ) && (start_ip[3] == mt3[4] ))
	{		
		return 1
	}
	return 0;
}

function w(str)
{
	document.write(str);
}