var PAGE_ITEM=15;
var PAGE_NUM;
var	PREV_ICON="<img border='0' src='../images/prev_page.gif'>" ;
var	NEXT_ICON="<img border='0' src='../images/next_page.gif'>" ;
var	END_ICON="<img border='0' src='../images/end_page.gif'>" ;
var	FIRST_ICON="<img border='0' src='../images/first_page.gif'>" ;
var	PREV_ICON0="<img border='0' src='../images/prev_page0.gif'>" ;
var	NEXT_ICON0="<img border='0' src='../images/next_page0.gif'>" ;
var	END_ICON0="<img border='0' src='../images/end_page0.gif'>" ;
var	FIRST_ICON0="<img border='0' src='../images/first_page0.gif'>" ;
//var PAGE_DETAIL=4;

function makeRequest(url,content,type,row_num,goto,send_value,list_type) 
{	 
	var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        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;
    }
    
    if (content == true)
    {    	
    	if(type==1)
    		http_request.onreadystatechange = function(){show_group_list(http_request,1,goto)};
    	else if(type==2)
    		http_request.onreadystatechange = function(){show_group_list(http_request,2,goto)};
	else if(type==3)
		http_request.onreadystatechange = function(){show_hdd_list(http_request)};
	else if(type==4)
		http_request.onreadystatechange = function(){show_smart_info(http_request)};
    	else if(type==5)
		http_request.onreadystatechange = function(){show_HDD_health(http_request,row_num)};
	else if(type==6)
		http_request.onreadystatechange = function(){show_syslog(http_request,goto)};
	else if(type==7)
		http_request.onreadystatechange = function(){Get_XML_List(http_request,1,goto,list_type)};
	else if(type==8)
		http_request.onreadystatechange = function(){show_ftplog(http_request)};
	else if(type==9)
		http_request.onreadystatechange = function(){show_chg_status(http_request)};
	else if(type==10)
		http_request.onreadystatechange = function(){show_share_list(http_request)};
	else if(type==11)
		http_request.onreadystatechange = function(){show_client_list(http_request)};
	else if(type==12)
		http_request.onreadystatechange = function(){show_chk_pw_status(http_request)};
	else if(type==13)
		http_request.onreadystatechange = function(){show_raid_info(http_request)};
	else if(type==14)
		http_request.onreadystatechange = function(){show_button_value(http_request)};
	else if(type==15)
		http_request.onreadystatechange = function(){show_schedule_list(http_request)};
	else if(type==16)	
	{	
			/* 
			   1../formatHD/riad_wizard4.asp 2nd_raid_wizard4.asp 2nd_rebuild4.asp 2nd_wizard4.asp s_wizard4.asp 
			   2../maintenance/upload_oled_state.asp
			*/
			http_request.onreadystatechange = function(){chk_bar_status(http_request)};	
	}
	else if(type==17)
		http_request.onreadystatechange = function(){show_NFS_share(http_request,goto)};
	else if(type==18)
		http_request.onreadystatechange = function(){get_ads_user_list(http_request,1,1,list_type)};
	else if(type==19)
		http_request.onreadystatechange = function(){show_ads_user_list(http_request,type,goto,list_type)};
	else if(type==20)
		http_request.onreadystatechange = function(){show_SMB_share(http_request,goto)};
	else if(type==21)
	{
		http_request.onreadystatechange = function(){set_nfs_ok(http_request)};
	}
	else if(type==22)
		http_request.onreadystatechange = function(){del_nfs_ok(http_request)};
	else if(type==23)
		http_request.onreadystatechange = function(){del_smb_ok(http_request)};		
	else if(type==24)
		http_request.onreadystatechange = function(){set_smb_ok(http_request)};	
	else
    		http_request.onreadystatechange = function(){show_modify_message(http_request)};
    }	

    url = url + "&id=" + Random();
    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(send_value);
}

function Random()
{
	var Random_value=Math.floor(Math.random()*1000000000000000);
	return Random_value;
}

function ChkIPAddress(ipaddr)
{
	if(ipaddr=="127.0.0.1")	
	{
		return 1;
	}
	else
		return 0;

}

function getNum(str, num)
{
	i=1;
	if ( num != 1 )
	{
		while (i!=num && str.length!=0)
		{
			if ( str.charAt(0) == '.' )
			{
				i++;
			}
			str = str.substring(1);
		}
		if ( i!=num )
			return -1;
	}
	for (i=0; i<str.length; i++)
	{
		if ( str.charAt(i) == '.' )
		{
			str = str.substring(0, i);
			break;
		}
	}
	if ( str.length == 0)
		return -1;
	d = parseInt(str, 10);
	return d;
}

function checkIP(str, num, min, max)
{
	d = getNum(str,num);
	if ( d > max || d < min )
		return false;
	return true;
}
function validKey(str)
{
	for (var i=0; i<str.length; i++)
	{
		if ( (str.charAt(i) >= '0' && str.charAt(i) <= '9') ||(str.charAt(i) == '.' ) )
			continue;
		return 0;
	}
	return 1;
}

function Chk_Folder_Name(FolderName)
{
	//return 1:	not a valid name
	
	var re=/[\\/:*?"<>|]/;
	if(re.test(FolderName))
	{
 		return 1;
	}

	if(FolderName.charAt(0)=="." || FolderName.charAt(FolderName.length-1)==".")
		return 2;

	return 0;
}
function rsync_chk_name(name)
{
	//return 1:	not a valid name
	var re=/[^a-zA-Z0-9_-]/;
	if(re.test(name))
		return 1;
	else
		return 0;
}
function rsync_chk_pw(pw)
{
	//return 1:	not a valid pw
	
	var re=/["'\\?#`:&+\;]/;

	//var re=/^[\w]{0,16}$/;
	//if(!re.test(pw))
	if(re.test(pw))
	{
 		return 1;
	}
	return 0;
	
}
function str_break(str,num)
{
	var strContent="";
	
	if(str.length<=num)
		strContent=str;
	else
	{
		while(str.length>num)
		{
			strContent+=str.substr(0,num)+"<br>"; 
			str=str.substr(num,str.length); 
		}
		
		if(str.length<=num)
			strContent+=str.substr(0,num)+"<br>";
	}
	
	return strContent;
}
function GetNodeValue(obj,num)
{
	var str = obj.childNodes[num].firstChild.nodeValue;
	return str;
}

function ip_same_net(ip,netmask,gateway,dns1,dns2)
{
	var loopback_flag = ip_chechk_loopback(ip)						//check loopback address,ex:127.x.x.x
	var broadcast_flag = ip_check_broadcast(ip,netmask,gateway);	// check broadcast address	
	var gw_flag = gw_chechk_loopback(gateway)
	var dns_flag1 = dns_chechk_loopback(dns1)
	var dns_flag2 = dns_chechk_loopback(dns2)
	var subnet_flag = subnet_chechk_loopback(netmask)
	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})$/);
	
	/*
		0 -> no error,
		1 -> IP address error
		2 -> broadcast address	error 
		3 -> loopback address error
		4 -> gw address error
	*/
	
	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])) &&
		( loopback_flag == 0 )  &&
		( broadcast_flag == 0 ) && (gw_flag == 0) && (dns_flag1 == 0) && (dns_flag2 == 0) && 
		( subnet_flag==0) )
		return 0
	else if (broadcast_flag == 1)
		return 2
	else if (loopback_flag == 1)
		return 3
	else if(gw_flag == 1)
		return 4;
	else if(dns_flag1==1)
		return 5;
	else if(dns_flag2==1)
		return 6;
	else if(subnet_flag==1)
		return 7;
}

function ip_check_broadcast(ip,netmask,gateway)
{
	var n = parseInt(255,10);
	
	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})$/);
	
	var ip_broadcast=((n-mt2[1])|mt1[1])+"."+((n-mt2[2])|mt1[2])+"."+((n-mt2[3])|mt1[3])+"."+((n-mt2[4])|mt1[4]);
	
	
//	var netmask_not=(n-mt2[1])+"."+(n-mt2[2])+"."+(n-mt2[3])+"."+(n-mt2[4]);
//	var mesg = "IP:"+mt1[1]+"."+mt1[2]+"."+mt1[3]+"."+mt1[4]+"\n";
//		mesg += "NetMask:"+mt2[1]+"."+mt2[2]+"."+mt2[3]+"."+mt2[4]+"\n";
//		mesg += "Gateway:"+mt3[1]+"."+mt3[2]+"."+mt3[3]+"."+mt3[4]+"\n";
//		mesg += "------------------------------------------\n"
//		mesg += "NetMask(Not):"+netmask_not+"\n";
//		mesg += "Broadcast:"+ip_broadcast+"\n";
//	alert(mesg);	
 		
		if ( (ip == ip_broadcast) || (netmask == ip_broadcast) || (gateway == ip_broadcast))
		{
			return 1;
		}
		else	
			return 0;
}

function ip_chechk_loopback(ip)
{
	var mt1 = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	
	if ( 127 == parseInt(mt1[1]))
	{
			return 1;
	}else	
			return 0;
}
function subnet_chechk_loopback(subnet)
{
	var mt1 = subnet.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);

	if ( 127 == parseInt(mt1[1]))
	{
			return 1;
	}else	
			return 0;
}
function gw_chechk_loopback(gw)
{
	if(gw.length==0)
		return 0;
		
	var mt1 = gw.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	
	if ( 127 == parseInt(mt1[1]))
	{
			return 1;
	}else	
			return 0;
}
function dns_chechk_loopback(dns)
{
	if(dns.length==0)
		return 0;
		
	var mt1 = dns.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	
	if ( 127 == parseInt(mt1[1]))
	{
			return 1;
	}else	
			return 0;
}
function check_login(status)
{
	if (status==0)
	{
		location.href="../login.asp";
	}

}
function chk_host(host)
{
	//for nfs check host format
	
	if(host=="*")
		return 0;
		
	var value=host.split(".");
	if(value.length<=1)
	{
		if(host.indexOf("\\") != -1)
			return 6;
		else
		return 1;
	}
	
	if(value.length==4)
	{
		if(!isNaN(value[0]) && !isNaN(value[1]) && !isNaN(value[2]) && !isNaN(value[3]))
		{
			if ( !checkDigitRange(host, 1, 1, 254) )
			{
				return 2;
			}
			if ( !checkDigitRange(host, 2, 0, 254) )
			{
				return 3;
			}
			if ( !checkDigitRange(host, 3, 0, 254) )
			{
				return 4;
			}
			if ( !checkDigitRange(host, 4, 1, 254) )
			{
				return 5;
			}
		}

	}	
}

function checkDigitRange(str, num, min, max)
{
	d = getDigit(str,num);
	if ( d > max || d < min )
		return false;
	return true;
}

function getDigit(str, num)
{
	i=1;
	if ( num != 1 )
	{
		while (i!=num && str.length!=0)
		{
			if ( str.charAt(0) == '.' )
			{
				i++;
			}
			str = str.substring(1);
		}
		if ( i!=num )
			return -1;
	}
	for (i=0; i<str.length; i++)
	{
		if ( str.charAt(i) == '.' )
		{
			str = str.substring(0, i);
			break;
		}
	}
	if ( str.length == 0)
		return -1;
	d = parseInt(str, 10);
	return d;
}
function chk_eng(str)
{
	var flag;
	for(i=0;i<str.length;i++)
	{
		//if(str.charCodeAt(i) >=49 && str.charCodeAt(i) <=57)
		if(str.charCodeAt(i) >=48 && str.charCodeAt(i) <=57)
		{
			//0-9
			flag=0;
		}
		else if( (str.charCodeAt(i) >=65 && str.charCodeAt(i) <=90) || (str.charCodeAt(i) >=97 && str.charCodeAt(i) <=122))
		{
			//a-zA-z
			flag=0;
		}
		else
		{
			if(str.charCodeAt(i)==45 || str.charCodeAt(i)==95)
				flag=0;
			else
			{
				flag=1;	//not english
				break;
			}
		}
	}
	return flag;
}
function chk_first_char(str)
{
	if( (str.charCodeAt(0) >=65 && str.charCodeAt(0) <=90) || (str.charCodeAt(0) >=97 && str.charCodeAt(0) <=122) )
		return 0;
	else
		return 1;
}
function Check_DHCP_range(device_ip,device_mask,s_ip)
{
	var v1 = device_ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	var v2 = device_mask.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	var v3 = s_ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
	
	if( ((v1[1]&v2[1])==(v3[1]&v2[1])) && ((v1[2]&v2[2])==(v3[2]&v2[2])) && ((v1[3]&v2[3])==(v3[3]&v2[3])))
		return 1;
	else
		return 0;
}
