var TOTAL_INSTALL_MODULE="";
var M_SETUP = 1;
var M_ADVANCED = 2;
var M_TOOLS = 3;
var M_STATUS = 4;
var M_APPLICATION = 5;

function GetModuleTab(module_name)
{

	//Ūmodule table¡A¬ݸˤF¨º¨ƭodule->¨�dule list,module_total_num

	var url="/goform/Module_Get_Info?";
	var send_value="";
	//module_makeRequest(url,true,2,send_value,module_name);	//return to : Write_Module_Icon();
	module_makeRequest(url,true,2,send_value,module_name,M_APPLICATION);	//return to : Write_Module_Menu();
}
function Write_Module_Menu(http_request,module_name,tab)
{
	if(http_request.readyState == 4)
	{
		if (http_request.status == 200 || http_request.status == 0)
		{
			
			var ModuleMenu_jsonstr=http_request.responseText;
			
			var obj=document.getElementById("topnav_container2");
			
			var json= eval("("+ ModuleMenu_jsonstr + ")");
			
			var Menu_num=json.Menu.length;
			
			var str;
			
			var newcell=obj.insertCell(0);
			str="<IMG src=\"../../web/images/short_modnum.jpg\">" ;
			newcell.innerHTML=str;
						
			var MAX_MODULE_MUN=7;
			var i,k;
			var menu_list = new Array(MAX_MODULE_MUN);
			var menu_url = new Array(MAX_MODULE_MUN);
			k=0;
			
			for(i=0;i<MAX_MODULE_MUN;i++)
			{		
				menu_list[k]="null";
				menu_url[k]="null";
				if(i<Menu_num)
				{
					if(json.Menu[i].ModuleStatus==0 || json.Menu[i].ModuleName=="LANGUAGE" || json.Menu[i].ModuleName=="ONLINE-BACKUP" ||json.Menu[i].URL=="NULL")
					{
						continue;
					}
					menu_list[k]=json.Menu[i].ModuleName;
					menu_url[k]=json.Menu[i].URL;
				k++;
				}
			}
			
			for(i=0;i<5;i++)
			{	
				newcell=obj.insertCell(i+1);
				
				if(module_name==menu_list[i])
					newcell.setAttribute("id","topnavon");
				else
					newcell.setAttribute("id","topnavoff");
				
//				if(menu_list[i]=="null")
//				{
//					continue;
//					//str="";
//				}
//				else
				if(i>k-1)
					str=""
				else
				{
					var dir="";
					if(menu_url[i]=="web/user/download_scheduling.asp" || menu_url[i]=="web/user/bt_list.asp")
						dir=menu_url[i];
					else
					{
						dir="imodule/" + menu_list[i] + "/" + menu_url[i];
					}
					str="<a href=\"/goform/CheckUserTimeout?url=" + dir + "\">" + menu_list[i] + "</a>";
				}
				newcell.innerHTML=str;
			}
		}
	}
}
function Write_Module_Icon(http_request)
{
	if(http_request.readyState == 4)
	{
		if (http_request.status == 200 || http_request.status == 0)
		{
			
			var ModuleMenu_jsonstr=http_request.responseText;
						
			var json= eval("("+ ModuleMenu_jsonstr + ")");
			
			var Menu_num=json.Menu.length;

			var obj=document.getElementById("module_list");
	
			var i,j,k,newrow,newcell;
			
			var menu_list = new Array(Menu_num);
			var menu_status = new Array(Menu_num);
			var menu_url = new Array(Menu_num);
			
			for(i=0;i<Menu_num;i++)
			{
				menu_list[i]=json.Menu[i].ModuleName;
				menu_status[i]=json.Menu[i].ModuleStatus;
				menu_url[i]=json.Menu[i].URL;
			}
			
			var total_row=Math.ceil(Menu_num)/3;
			k=0;
			for(i=0;i<total_row;i++)
			{
				newrow = obj.insertRow(obj.rows.length);
				for(j=0;j<3;j++)
				{
					newcell = newrow.insertCell(j);
					newcell.align="center";
					
					if(k<menu_list.length)
					{
						var icon;
						
						if(menu_status[k]==0)
							icon="<img border=\"0\" src=\"../../imodule/" +  menu_list[k] + "_off.png\"><br>" + menu_list[k];
						else
							icon="<a href=\"/goform/CheckUserTimeout?url=../../imodule/" + menu_url[k] +"\"><img border=\"0\" src=\"../../imodule/" +  menu_list[k] + "_on.png\"></a><br>" + menu_list[k];
						newcell.innerHTML=icon;
					}
					else
						newcell.innerHTML="&nbsp;";
						
					k++;
				}
			}
		}
	}		
}
function module_makeRequest(url,content,return_to,send_value,module_name,tab_name) 
{	 			
    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(return_to==1)
			http_request.onreadystatechange = function(){show_module_info(http_request,module_name)};
		else if(return_to==2)
			http_request.onreadystatechange = function(){Write_Module_Menu(http_request,module_name,tab_name)};
		else if(return_to==3)
			http_request.onreadystatechange = function(){Show_Module_Menu(http_request,module_name,tab_name)};
		else if(return_to==4)
			http_request.onreadystatechange = function(){Show_Module_Help(http_request)};
			
	}
		
	url = url + "&r=" + 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()
{
	Random_value=Math.floor(Math.random()*1000000000000000);
	return Random_value;
}

function chg_image(obj_name,file_path)
{
	document.getElementById(obj_name).src=file_path;
}

function show_online_backup_menu(url)
{	
	var str='<a href="' + url + '">' + 'ONLINE-BACKUP' + '</a>' ;
	
	if(url=="")
		document.getElementById("ctera").style.display="none"
	else
	{
		document.getElementById("ctera").innerHTML = str;
	}
}
function Get_Install_module(tab,tab_name)
{
	var url="/goform/Module_Get_Menu?";
	var send_value="";
	module_makeRequest(url,true,3,send_value,tab,tab_name);	//return to : Show_Module_Menu();
}

function Show_Module_Menu(http_request,tab,tab_name)
{
	if(http_request.readyState == 4)
	{
		if (http_request.status == 200 || http_request.status == 0)
		{
			
			var ModuleMenu_jsonstr=http_request.responseText;
			
			var json= eval("("+ ModuleMenu_jsonstr + ")");
			
			var Menu_num=json.Menu.length;
			
			var str = "";
			var i;
			
			str= "<UL>\n"
			for(i=0;i<MENU_ARRAY.length-1;i++)
			{
				var class_name = " id=sidenavoff" ;
				var font_color = "#404343";
				if(MENU_ARRAY[i].indexOf("href")!=-1)
				{
					class_name = "";
					font_color = "#ffffff";
				}
				str=str+ "<LI>\n<DIV" + class_name + "><font color=\"" + font_color + "\">" + MENU_ARRAY[i] +"</font></DIV>\n"
			}
			for(i=0;i<Menu_num;i++)
			{
				if(i<Menu_num)
				{
					var u = json.Menu[i].URL;
					var id=get_url_value(u,"id");
					if(id!="null" && id == tab)
					{
						if(json.Menu[i].ModuleStatus==0 || json.Menu[i].ModuleName=="LANGUAGE" || json.Menu[i].URL=="NULL")
						{
							continue;
						}
						
						if(tab_name == json.Menu[i].ShowName)
							str=str + '<LI>\n<DIV id=sidenavoff>' + json.Menu[i].ShowName + '</DIV>\n' ;
						else
							str=str + '<LI>\n<DIV><a href="/imodule/' +  json.Menu[i].ModuleName + "/" +json.Menu[i].URL + '">' + json.Menu[i].ShowName + '</a></DIV>\n' ;
					}
				}
			}
			
			str=str+ "<LI>\n<DIV>" + MENU_ARRAY[MENU_ARRAY.length-1] +"</DIV>\n"
			str=str+"</UL>" ;
			
			document.getElementById("sidenav").innerHTML = str;

		}
	}
}

function get_url_value(url,name)
{
    if(url.indexOf("?")!=-1)//url裡有"?"符號
    {
        var ary=url.split("?")[1].split("&");
        var i=0;
        //for(i in ary)
        for(i=0;i<ary.length;i++)
        {
         	if(ary[i].split("=")[0]==name)
         	{
         		return ary[i].split("=")[1];
         	}
         		
            //str+=ary[i].split("=")[0]+":"; //變數名稱
            //str+=ary[i].split("=")[1]+"\n"; //變數值
        }
    }
    else
    { 	//url裡沒有"?"符號
        //str+="沒有get變數\n";
        return "null"
    }
    //window.alert(str);//code/blockquote
	
}

function Get_Install_module2()
{
	var url="/goform/Module_Get_Menu?";
	var send_value="";
	module_makeRequest(url,true,4,send_value);	//return to : Show_Module_Help();
}
function Show_Module_Help(http_request)
{
	if(http_request.readyState == 4)
	{
		if (http_request.status == 200 || http_request.status == 0)
		{
			
			var ModuleMenu_jsonstr=http_request.responseText;
			
			var json= eval("("+ ModuleMenu_jsonstr + ")");
			
			var Menu_num=json.Menu.length;

			var help_1="",help_2="",help_3="";
			
			for(i=0;i<Menu_num;i++)
			{
				var u = json.Menu[i].URL;
				var id=get_url_value(u,"id");
				
				if(json.Menu[i].ModuleStatus==0 || json.Menu[i].ModuleName=="LANGUAGE" || json.Menu[i].URL=="NULL")
					continue;
				
				switch(parseInt(id,10))
				{
					case 1:
						var tmp = '<li><font face="Arial"><a target="_blank" href="help_home.asp">' + json.Menu[i].ShowName + '</a></font></li>'
						help_1= help_1 + tmp;
						break;
					case 2:
						var tmp = '<li><font face="Arial"><a target="_blank" href="help_advanced.asp">' + json.Menu[i].ShowName + '</a></font></li>'
						help_2= help_2 + tmp;
						break;
					case 3:
						var tmp = '<li><font face="Arial"><a target="_blank" href="help_tools.asp">' + json.Menu[i].ShowName + '</a></font></li>'
						help_3= help_3 + tmp;
						break;
					default:
						break;
				}
			}

			document.getElementById("help_1").innerHTML = help_1;
			document.getElementById("help_2").innerHTML = help_2;	
			document.getElementById("help_3").innerHTML = help_3;
		}
	}	
}
