var NFS_TOTAL_NUM;
var SMB_TOTAL_NUM;

var	LANG_REAL_PATH;
var	LANG_YES,LANG_NO;

var NFS_MAX_NUM = 64;

function S_makeRequest(url,content,type,row_num,goto,send_value,list_type) 
{	 
	var 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;
    }

	if (content == true)
    { 

		if(type==7)
		{
			//advanced/session.asp:nfs
			http_request.onreadystatechange = function(){Get_XML_List(http_request,1,goto,list_type)};
		}
		else if(type==9)
		{
			http_request.onreadystatechange = function(){show_SMB_share(http_request,goto)};
		}
		else if(type==10)
		{
			http_request.onreadystatechange = function(){show_NFS_share(http_request,goto)};
		}
		else if( type == 11 )
		{
			// session.asp
			http_request.onreadystatechange = function(){set_nfs_ok(http_request)};
		}
		else if( type == 12 )
		{
			// session.asp
			http_request.onreadystatechange = function(){del_nfs_ok(http_request)};
		}
		else if( type == 13 )
		{
			// session.asp
			http_request.onreadystatechange = function(){del_smb_ok(http_request)};
		}
		else if( type == 14 )
		{
			// session.asp
			http_request.onreadystatechange = function(){set_smb_ok(http_request)};
		}
	}
		
	url = url + "&" + Random2();
	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 Random2()
{
	Random_value=Math.floor(Math.random()*1000000000000000);
	return Random_value;
} 

function ads_enable()
{
	if(document.form1.f_nfs_status[0].checked)
	{
		//enable
		document.nfs_enable_form.f_enable.value=document.form1.f_nfs_status[0].value;
	}
	else
	{
		//disable
		document.nfs_enable_form.f_enable.value=document.form1.f_nfs_status[1].value;
	}
	document.nfs_enable_form.submit();
}
function show_item(type)
{	
	//type:0	check witch one be selected
	//type:1	smb
	//type:2	nfs
	
	if(type==0)
	{
		if(document.form1.f_type[0].checked)
			type=1;
		else
			type=2;
	}
	
	if(type==1)
	{
		document.getElementById("smb_list").style.display = "";
		document.getElementById("nfs_list").style.display = "none";
		document.getElementById("smb_tab").style.display = "";
		document.getElementById("nfs_tab").style.display = "none";
		Get_List_Info();
	}
	
	if(type==2)
	{
		document.getElementById("smb_list").style.display = "none";
		document.getElementById("nfs_list").style.display = "";
		document.getElementById("smb_tab").style.display = "none";
		document.getElementById("nfs_tab").style.display = "";
		Get_List_Info();
	}
}

function show_NFS_share(http_request,goto)
{
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
			var xmlDOM = http_request.responseXML.documentElement;
			var list_num=xmlDOM.getElementsByTagName("Item").length
			var obj;
			var NFS_Data = new Array(5);
			
			Show_Page_List(NFS_TOTAL_NUM,goto,"nfs");
			
			if(NFS_TOTAL_NUM<=PAGE_ITEM)
			{
				document.getElementById("nfs_page_div").style.display = "none";
			}
			else
			{
				document.getElementById("nfs_page_div").style.display = "";
				Show_Page_Div(goto,NFS_TOTAL_NUM,"nfs",2);
			}
						
			del_tb("nfs_tb");
			var k=0;
			for(i=0;i<list_num;i++)
			{
				obj=xmlDOM.getElementsByTagName("Item")[i];
				for(var j=0;j<5;j++)
				{
					NFS_Data[j]=GetNodeValue(obj,j);
				}

				k++;
				var line_num;
				if(goto!=1)
				{
					line_num=(goto-1)*PAGE_ITEM+k;
				}
				else
					line_num=k;
				
				write_nfs_tb(NFS_Data,line_num);
			}
		}
	}
}
function write_nfs_tb(data,line_num)
{
	var	td_num=6;
	var	src=document.getElementById("nfs_tb");	
	var	newrow   =   src.insertRow(src.rows.length);
	var	chg_icon = "<img border=\"0\" src=\"../images/edit.jpg\">";
	var	rw_icon = "<img border=\"0\" src=\"../images/edit2.jpg\">";
	var	ro_icon = "<img border=\"0\" src=\"../images/edit1.jpg\">";
	var	del_icon = "<img border=\"0\" src=\"../images/release.gif\">";
	
	newrow.onmouseover=function(){this.bgColor="#DFDFDF";}
	newrow.onmouseout=function(){this.bgColor="";}
	
	for(var i=0;i< td_num ;i++)
	{
		var newcell   =   newrow.insertCell(i);
		var	str,str2;
		
		switch(i)
		{
			case 0: newcell.innerHTML=data[2];	//host
					break;
			case 1: 
					str=str_break(data[1],40);
					str=str.replace(/\s/g,'&nbsp;');
					
					str2=str_break(data[0],40);
					str2=str2.replace(/\s/g,'&nbsp;');
					
					newcell.innerHTML=str + "<br>" + LANG_REAL_PATH + ": " +str2 + " ";	//share path
					break;
			case 2:
				newcell.align="center";
				if(data[4]==1)
					newcell.innerHTML=LANG_NO;	//no root squash
				else
					newcell.innerHTML=LANG_YES;	//root squash
					
				break;
			case 3:
				newcell.align="center";
				if(data[3]==1)
					newcell.innerHTML=rw_icon;	//rw
				else
					newcell.innerHTML=ro_icon;	//ro
				break;

			case 4:
				//show modify icon
				//data[2]:host	data[1]:path  data[4]:root squash   data[3]:rw
				str=data[2].replace(/'/g,"\\'");
				str2=data[1].replace(/'/g,"\\'");
				newcell.innerHTML="<a href=\"javascript:ModifyNFS('" + str + "','" + str2 + "','" + data[4] + "','" + data[3] + "');\">" + chg_icon + "</a>";
				break;
			case 5:
				//show del icon
				str=data[1].replace(/'/g,"\\'");
				str2=data[2].replace(/'/g,"\\'");
				newcell.innerHTML="<a href=\"javascript:DelNFS('" + str + "','" + str2 + "');\">" + del_icon + "</a>";
				break;
		}
	}
}

function show_SMB_share(http_request,goto)
{
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
			var xmlDOM = http_request.responseXML.documentElement;
			var list_num=xmlDOM.getElementsByTagName("Item").length
			var obj;
			var SMB_Data = new Array(7);

			Show_Page_List(SMB_TOTAL_NUM,goto,"smb");
			
			if(SMB_TOTAL_NUM<=PAGE_ITEM)
			{
				document.getElementById("smb_page_div").style.display = "none";
			}
			else
			{
				document.getElementById("smb_page_div").style.display = "";
				Show_Page_Div(goto,SMB_TOTAL_NUM,"smb",1);
			}
						
			del_tb("smb_tb");
			var k=0;
			for(i=0;i<list_num;i++)
			{
				obj=xmlDOM.getElementsByTagName("Item")[i];
				for(var j=0;j<7;j++)
				{
					SMB_Data[j]=GetNodeValue(obj,j);
				}
				
				k++;
				var line_num;
				if(goto!=1)
				{
					line_num=(goto-1)*PAGE_ITEM+k;
				}
				else
					line_num=k;
				
				write_smb_tb(SMB_Data,line_num);
			}
		}
	}
}
function write_smb_tb(data,line_num)
{
	var	td_num=9;
	var	src=document.getElementById("smb_tb");
	var	newrow   =   src.insertRow(src.rows.length);
	var	chg_icon = "<img border=\"0\" src=\"../images/edit.jpg\">";
	var	rw_icon = "<img border=\"0\" src=\"../images/edit2.jpg\">";
	var	ro_icon = "<img border=\"0\" src=\"../images/edit1.jpg\">";
	var	del_icon = "<img border=\"0\" src=\"../images/release.gif\">";
	
	newrow.onmouseover=function(){this.bgColor="#DFDFDF";}
	newrow.onmouseout=function(){this.bgColor="";}
	
	for(var i=0;i< td_num ;i++)
	{
		var newcell   =   newrow.insertCell(i);
		var ser;
		
		switch(i)
		{
			case 0: 
				str=str_break(data[0],10);
				str=str.replace(/\s/g,'&nbsp;');
				newcell.innerHTML=str;	//share name
				break;
			case 1: 
				str=str_break(data[1],20);
				str=str.replace(/\s/g,'&nbsp;');
				newcell.innerHTML=str;	//share path
				break;
			case 2:
				if(data[2].indexOf("@")!=-1)
				{
					var str=data[2].split("@");	//format: @username:groupname
					str=str[1].split(":");
					newcell.innerHTML=str[1];	// group name
					newcell.style.fontWeight="bold";
					
					newcell.setAttribute("title","User : "+ str[0]);	//user name
				}
				else
					newcell.innerHTML=data[2];	// user/group
				break;
			case 3:
				//comment
				newcell.style.padding="0px 0px 0px 5px";
				str=str_break(data[3],20);
				str=str.replace(/\s/g,'&nbsp;');
				if(data[3]=="[null]")
					newcell.innerHTML="&nbsp;";
				else
					newcell.innerHTML=str;
				break;
			case 4:
				//oplocks
				newcell.style.padding="0px 0px 0px 5px";
				newcell.align="center";
				if(data[4]=="no")
					newcell.innerHTML=LANG_NO;	
				if(data[4]=="yes")
					newcell.innerHTML=LANG_YES;	
				break;
			case 5:
				//map archive
				newcell.style.padding="0px 0px 0px 5px";
				newcell.align="center";
				if(data[5]=="no")
					newcell.innerHTML=LANG_NO;
				if(data[5]=="yes")
					newcell.innerHTML=LANG_YES;	
				break;
			case 6:
				//rw
				newcell.style.padding="0px 0px 0px 5px";
				newcell.align="center";
				if(data[6]=='no')
					newcell.innerHTML=rw_icon;	//rw
				else
					newcell.innerHTML=ro_icon;	//ro				
				break;
			case 7:
				//var new1=data[0].replace(/\s/g,'&nbsp;');
				var new1=data[0];
				new1=new1.replace(/'/g,"\\'");
				
				
				//var new2=data[1].replace(/\s/g,'&nbsp;');
				var new2=data[1];
				new2=new2.replace(/'/g,"\\'");
								
				//if(data[0]=="BT")
				if(data[0]=="BT" && (data[1]=="Volume_1/BT" || data[1]=="Volume_2/BT") )
					newcell.innerHTML="";
				else
				{
					//show modify icon
					newcell.innerHTML="<a href=\"javascript:ModifySession('" + new1 + "','" + data[2] + "','" + new2 + "','" + data[6] + "','" + data[4] + "','" + data[5] + "','" + data[3] + "');\">" + chg_icon + "</a>";
				}
				break;
			case 8:
			
				//var new1=data[0].replace(/\s/g,'&nbsp;');
				var new1=data[0];
				new1=new1.replace(/'/g,"\\'");
				
				//if(data[0]=="BT")
				if(data[0]=="BT" && (data[1]=="Volume_1/BT" || data[1]=="Volume_2/BT") )
					newcell.innerHTML="";
				else
				{			
					//show del icon
					newcell.innerHTML="<a href=\"javascript:DeleteSession('" + new1 + "');\">" + del_icon + "</a>";
				}
				break;
		}
	}
}
function Get_List_Info()
{
	Get_List(1,1);
	Get_List(2,1);
}

function Get_List(list_type,type)
{
	var request_url,goto;
	
	if(type==1)
		goto=1;
	else
		goto=0;	//end page flag

	if(list_type==1)
	{
		/*-----SMB share------*/
		
		//get smb share
		request_url="/goform/Get_SMB_List?" ;
		
		document.getElementById("smb_page_div").style.display = "none";

	}
	else
	{
		/*-----NFS share------*/
		
		//get nfs share
		request_url="/goform/Get_NFS_List?" ;
		
		document.getElementById("nfs_page_div").style.display = "none";

	}
	
	S_makeRequest( request_url,true,7,"",goto,"",list_type);	//return to :Get_XML_List()
	
	
}

function Get_XML_List(http_request,type,goto,list_type)
{
	/*
	type:1 from Get_List()
	type:2 from user input page num
	list_type:1 smb share
	list_type:2 nfs share
	*/
	var request_url,send_value;
	
	if(type==1)
	{
		if (http_request.readyState == 4) 
		{
			if (http_request.status == 200) 
			{
				
				if(goto==0)
				{
					goto=PAGE_NUM;	//show end page
				}
				
				send_value="goto=" + goto + "&item=" + PAGE_ITEM;
					
				if(list_type==1)
				{
					//get smb share
					
					SMB_TOTAL_NUM=http_request.responseText;

					SMB_PAGE_NUM=Math.ceil(SMB_TOTAL_NUM/PAGE_ITEM);
					Show_Page_List(SMB_TOTAL_NUM,goto,"smb");
					request_url="/goform/XML_Create_SMB_List?" ;
					S_makeRequest( request_url,true,9,"",goto,send_value,list_type);	//return to :show_SMB_share()		
				}
				else
				{
					//get nfs share
					NFS_TOTAL_NUM=http_request.responseText;

					NFS_PAGE_NUM=Math.ceil(NFS_TOTAL_NUM/PAGE_ITEM);
					Show_Page_List(NFS_TOTAL_NUM,goto,"nfs");
					request_url="/goform/XML_Create_NFS_List?" ;
					S_makeRequest( request_url,true,10,"",goto,send_value,list_type);	//return to :show_NFS_share()			
				}
			}
		}
	}
	else
	{
		send_value="goto=" + goto + "&item=" + PAGE_ITEM;;
		
		if(list_type==1)
		{
			//get smb share
			request_url="/goform/XML_Create_SMB_List?" ;
			S_makeRequest( request_url,true,9,"",goto,send_value,list_type);	//return to :show_SMB_share()		
		}
		else
		{
			//get nfs share
			request_url="/goform/XML_Create_NFS_List?" ;
			S_makeRequest( request_url,true,10,"",goto,send_value,list_type);	//return to :show_NFS_share()			
		}
	}
}

function del_tb(obj_name)
{
	var	src=document.getElementById(obj_name);
	var	row_num=src.rows.length;

	if(row_num==1)
		return;
	else
	{
		while(row_num>1)
		{
			src.deleteRow(row_num-1);
			row_num=src.rows.length;
		}
	}
}
function Show_Page_Div(page,list_num,obj,list_type)
{
	
	var obj0=document.getElementById(obj+"_first_page");
	var	obj1=document.getElementById(obj+"_go_prev");	
	var	obj2=document.getElementById(obj+"_page_num");
	var	obj3=document.getElementById(obj+"_go_next");
	var obj4=document.getElementById(obj+"_last_page");
	var	next_page=parseInt(page,10) +1;
	var	prev_page=parseInt(page,10) -1;
	var	function_name;
	var	PAGE_NUM;
	
	
	function_name="Get_XML_List";
	
	//select the page list
	if(obj=="smb")
	{
		document.form1.f_page1.value=page;
		PAGE_NUM=SMB_PAGE_NUM;
	}
	else
	{
		document.form1.f_page2.value=page;
		PAGE_NUM=NFS_PAGE_NUM;
	}
	
	if(page==1)
	{
		obj0.innerHTML=FIRST_ICON0;
		obj1.innerHTML=PREV_ICON0;
		
		if(list_num<=PAGE_ITEM)
		{
			
			obj4.innerHTML=END_ICON0;
			obj3.innerHTML=NEXT_ICON0 ;	
		}
		else
		{	
			obj3.innerHTML="<a href=javascript:" + function_name + "("  + "null" + ",2," + next_page + "," + list_type +");>" + NEXT_ICON +"</a>" ;
			obj4.innerHTML="<a href=javascript:" + function_name + "("  + "null" + ",2," + PAGE_NUM + "," + list_type + ");>" + END_ICON + "</a>" ;
		}
	}
	else
	{
		if(page==PAGE_NUM)
		{
			obj0.innerHTML="<a href=javascript:" + function_name + "("  + "null" + ",2," + 1 + "," + list_type + ");>" + FIRST_ICON+"</a>" ;
			obj1.innerHTML="<a href=javascript:" + function_name + "("  +  "null" + ",2," + prev_page + "," + list_type + ");>" + PREV_ICON + "</a>" ;
			obj4.innerHTML=END_ICON0;
			obj3.innerHTML=NEXT_ICON0 ;
		}
		else
		{
			obj0.innerHTML="<a href=javascript:" + function_name + "("  +  "null" + ",2," + 1 + "," + list_type + ");>" + FIRST_ICON +  "</a>" ;
			obj1.innerHTML="<a href=javascript:" + function_name + "("  +  "null" + ",2," + prev_page + "," + list_type + ");>" + PREV_ICON + "</a>" ;
			obj3.innerHTML="<a href=javascript:" + function_name + "("  +  "null" + ",2," + next_page + "," + list_type + ");>" + NEXT_ICON + "</a>" ;
			obj4.innerHTML="<a href=javascript:" + function_name + "("  +  "null" + ",2," + PAGE_NUM + "," + list_type + ");>" + END_ICON + "</a>" ;			
		}
	}
}
function goto_page(event,list_type)
{
	var goto;
	var PAGE_NUM;
	
	if(list_type==1)
	{
		 goto=parseInt(document.form1.f_page1.value);
		 PAGE_NUM=SMB_PAGE_NUM;
	}
	else
	{
		goto=parseInt(document.form1.f_page2.value);
		PAGE_NUM=NFS_PAGE_NUM;
	}
		
	if(isNaN(goto))
		goto=1;
	else if(goto>PAGE_NUM)
		goto=PAGE_NUM;
	else if(goto<1)
		goto=1;
		
	if(event.keyCode=='13')
	{
		Get_XML_List(null,2,goto,list_type);
	}
}
