var GEO_LANG_ID= "EN";
var XMLParser;
var str_Submit;
var str_Cancel;
var str_PLAY;
var str_PAUSE;
var str_STOP;

main();    //Create XML Paser
function main()
{  
  try 
  {
     GetUsrLanguage();		
     XMLParser = LoadDOM("WebTable.xml");
     //alert("doc: "+XMLParser.xml);   
     str_Submit = GetXML("IDS_WEB_SUBMIT"); 
     str_Cancel = GetXML("IDS_WEB_CANCEL");
     str_PLAY = GetXML("IDS_WEB_PLAY"); 
     str_PAUSE = GetXML("IDS_WEB_PAUSE");  
     str_STOP = GetXML("IDS_WEB_STOP"); 
     
              
  }
  catch (e)
  {
     str_Submit = "Submit";
     str_Cancel = "Cancel";
     str_PLAY = "Play"; 
     str_PAUSE = "Pause";  
     str_STOP = "Stop"; 
     
    //alert("tttt");
    //alert(e.description);
  }
}


function LoadDOM(file)
{
   var dom;
   try {
     dom = MakeDOM(null);
     dom.load(file);
    // if(dom ==null)
     //	alert("Error");
   }
   catch (e) {
    // alert(e.description);
    	return null;
   }
   return dom;
}

function MakeDOM(progID)
{
  if (progID == null) {
    progID = "msxml.DOMDocument";
  }

  var dom;
  try {
    dom = new ActiveXObject(progID);
    dom.async = false;
    dom.validateOnParse = false;
    dom.resolveExternals = false;
  }
  catch (e) {
    //alert(e.description);
    return null;
  }
  return dom;
}

function GetUsrLanguage()
{  
   var CY,Temp;
   //取得國家語系
   CY=navigator.userLanguage;  
   Temp = CY.substring(0,2);   
   	
   if(CY == "zh-tw" || CY == "zh-hk" )  
   {
   	GEO_LANG_ID = "TC";  // 繁體中文
   }
   else if(CY == "zh-ch" || CY == "zh-cn"  || CY == "zh-sg" )
   {
   	GEO_LANG_ID = "SC";  // 簡體中文
   }
   else if(CY == "ja")
   {
   	GEO_LANG_ID = "JPN";  //日文
   }
    else if(CY == "fr-be"||CY == "fr-ch" ||CY == "fr")
   {
   	GEO_LANG_ID = "FRA";  //法文
   }
    else if(Temp == "de")
   {
   	GEO_LANG_ID = "GEN";  //德文
   }
   else if(CY == "pl")
   {
   	GEO_LANG_ID = "POL";  //波蘭文
   }
   else if(CY == "pt-br"||CY == "pt")
   {
   	GEO_LANG_ID = "PTU";  //葡萄牙
   }
    else if(Temp == "es")
   {
   	GEO_LANG_ID = "SPN";  //西班牙
   }
   else if(CY == "ru")
   {   	
      GEO_LANG_ID = "RUS";  //俄文
   }
   else if(CY == "ar-iq" || CY == "ar-ly" || CY == "ar-ma" || CY == "ar-om" || CY == "ar-sy" ||  CY == "ar-lb"|| CY == "ar-ae"|| CY == "ar-qa"
   || CY == "ar-sa" || CY == "ar-eg" || CY == "ar-dz" || CY == "ar-tn" || CY == "ar-ye" || CY == "ar-jo" || CY == "ar-kw"|| CY == "ar-bn")
   {   	
      GEO_LANG_ID = "ARB";  //阿拉伯
   }
     else if(CY == "he")
   {   	
      GEO_LANG_ID = "HEB";  //希伯來文
   }
    else if(CY == "it")
   {   	
      GEO_LANG_ID = "ITA";  //義大利文
   }
   else if(CY == "cs")
   {
     GEO_LANG_ID = "CZH";  //捷克文
   }
   else if(CY == "da")
   {
     GEO_LANG_ID = "DAN";  //丹麥文
   }
   else if(CY == "th")
   {
     GEO_LANG_ID = "TAI";  //泰文
   }
   else if(CY == "nl")
   {
     GEO_LANG_ID = "NLD";  //荷蘭文
   }
   else if(CY == "sk" || CY=="sl" || CY=="sr")
   {
     GEO_LANG_ID = "SRL";  //南斯拉夫文
   }
   else
   {
     GEO_LANG_ID = "EN";  //英文
   }
   
}

function GetXML(ID)
{ 
  var  pLangResult;
  var  pElement;
  
  try   
  {   
    
     if(XMLParser  != null)
     {
     	 //尋找字串
     	try{
     	 pElement=XMLParser.documentElement.selectSingleNode(ID);
     	 }
  	 catch (e)
  	 {
  	 	//Server 關閉
  	 	//alert("Can't find Stable.xml");
  	 	return  DefaultString(ID);  	 	 
  	  }		   
  
     	 
   	 if(pElement == null) return ID;
   	 
   	
   
   	 //尋找該語系字串
         pLangResult = pElement.selectSingleNode(GEO_LANG_ID);
         if(pLangResult == null)
   	 {             
	   //尋找英文字串
      	   var pEN = pElement.selectSingleNode("EN");    
      	   if(pEN == null)
		  return ID;
	   else	 
		  return pEN.text;
	  
    	}
    	else
   	{  
	   //若該字串為空白,以英文取代
	   var Temp =  pLangResult.text;
	   if(Temp  == "")
	   {
              //尋找英文字串
		var pEN = pElement.selectSingleNode("EN");    
		if(pEN == null)
		   return ID;
		else	 
		   return  pEN.text;

	   }
	   else
		return   pLangResult.text;
   	}
   	 
   	
     } 
     else //XMLParser  == null
     {     	
     	//return ID;
     	switch(ID)
     	{
     	   
     	   case "IDS_WEB_CODEC_SELECT":
     	           return "Compression Selection:";
     	           break;  
     	           
     	   case "IDS_WEB_CODEC_MPEG4":
     	           return "Mpeg4 Encoder Viewer";
     	           break;
     	   
     	   case "IDS_WEB_CODEC_JPEG":
     	           return "JPEG Image Viewer";
     	           break;
     	           
     	   case "IDS_WEB_CODEC_RPB":
     	           return "Remote Play Back";
     	           break;
     	           
     	   case "IDS_WEB_CODEC_SVRINFO":
     	           return "Server Information";
     	           break;
     	          
     	   case "IDS_WEB_CODEC_REMOTECONTROL":
     	           return "Remote Control";
     	           break;
     	   
     	   case "IDS_WEB_SUBMIT":
     	           return "Submit";
     	           break;
     	           
     	   case "IDS_WEB_CANCEL":
     	           return "Cancel";
     	           break;
     	   
     	   case "IDS_WEB_INTERNET_CONNECT":
     	           return "Select Internet Connection:";
     	           break; 
     	                	           
     	   case "IDS_WEB_INTERNET_MODEM":
     	           return "MODEM";
     	           break; 
     	           
     	   case "IDS_WEB_INTERNET_LAN":
     	           return "DSL/CABLE/T-1";
     	           break;
     	           
     	   case "IDS_WEB_VIEWCHANNEL":
     	           return "Select Multi Window:";
     	           break;
     	           
     	   case "IDS_WEB_CHANNEL1":
     	           return "1 Window";
     	           break;
     	           
     	   case "IDS_WEB_CHANNEL2":
     	           return "2 Windows";
     	           break; 
     	           
     	   case "IDS_WEB_MULTI_CHANNEL":
     	           return "Multi View";
     	           break; 
     	           
     	   case "IDS_WEB_EVENTLIST_QUERY":
     	           return "Event List Query:";
     	           break;
     	           
     	   case "IDS_WEB_EVENT_TYPE":
     	           return "Event Type";
     	           break; 
     	           
     	   case "IDS_WEB_DATE":
     	           return "Date";
     	           break;
     	           
     	   case "IDS_WEB_START_TIME":
     	           return "Start Time";
     	           break; 
     	           
     	   case "IDS_WEB_END_TIME":
     	           return "End Time";
     	           break; 
     	           
     	   case "IDS_REMOTE_MESSAGE1":
     	           return "Type the name of the remote computer you want to use, select the screen size for your connection, and then click Connect.";
     	           break; 
     	           
     	   case "IDS_REMOTE_TITLE":
     	           return "Remote Desktop Web Connection";
     	           break;
     	           
     	   case "IDS_REMOTE_MESSAGE2":
     	           return "When the connection page opens, you can add it to your Favorites for easy connection to the same computer.";
     	           break;
     	           
     	   case "IDS_REMOTE_SERVER":
     	           return "Server";
     	           break;
     	           
     	   case "IDS_REMOTE_RESOULATION":
     	           return "Size";
     	           break; 
     	           
     	   case "IDS_REMOTE_LOGIN_INFO":
     	           return "Send logon information for this connection";
     	           break;
     	     
     	    case "IDS_WEB_ID" :
     	           return "ID";
     	           break;
     	         
     	     case "IDS_WEB_PASSWORD" :
     	           return "Password";
     	           break;
     	           
     	     case "IDS_WEB_WEBCAM_LOGIN" :
     	           return "Webcam login";
     	           break;
     	     
     	     case "IDS_WEB_GUEST_LOGIN" :
     	           return "Guest login";
     	           break;
     	           
     	     case "IDS_WEB_REMEMBER_ID_PWD" :
     	           return "Remember my id and password";
     	           break;       
     	     
     	     case "IDS_WEB_LOGIN" :
     	           return "Login";
     	           break; 
     	          
     	     case "IDS_WEB_CHANGE_PWD" :
     	           return "Change password";
     	           break; 
     	      
     	     case "IDS_WEB_FORGET_PWD" :
     	           return "Forget password";
     	           break; 
     	      
     	     case "IDS_WEB_EVENT_QUERY" :
     	           return "Event List Query";
     	           break;                 
     	       
     	     case "IDS_WEB_DOWNLOAD" :
     	           return "Download";
     	           break;
     	           
     	     case "IDS_WEB_LIVEVIEW" :
     	           return "Live view";
     	           break; 
     	           
     	     case "IDS_WEB_HOME" :
     	           return "Home";
     	           break;
     	           
     	      case "IDS_WEB_OLD_PWD":
     	           return "Old password";
     	           break; 
     	    
     	      case "IDS_WEB_NEW_PWD":
     	           return "New password";
     	           break;   	    
     	           
     	       case "IDS_WEB_HINT":
     	           return "Hint";
     	           break;
     	           
     	       case "IDS_WEB_ID_PWD_ERROR":
     	           return "ID or password error";
     	           break;
     	           
     	       case "IDS_WEB_OK":
     	           return "OK";
     	           break;        
     	      
     	          
     		
     	}
     	return ID;     	
     }
         
     	
  }
  catch (e)
  {
    alert(e.description);
  }		   
  return ID;
  
}

function  DefaultString(ID) 
{
	
	//return ID;
     	switch(ID)
     	{
     	   
     	   case "IDS_WEB_CODEC_SELECT":
     	           return "Compression Selection:";
     	           break;  
     	           
     	   case "IDS_WEB_CODEC_MPEG4":
     	           return "Mpeg4 Encoder Viewer";
     	           break;
     	   
     	   case "IDS_WEB_CODEC_JPEG":
     	           return "JPEG Image Viewer";
     	           break;
     	           
     	   case "IDS_WEB_CODEC_RPB":
     	           return "Remote Play Back";
     	           break;
     	           
     	   case "IDS_WEB_CODEC_SVRINFO":
     	           return "Server Information";
     	           break;
     	          
     	   case "IDS_WEB_CODEC_REMOTECONTROL":
     	           return "Remote Control";
     	           break;
     	   
     	   case "IDS_WEB_SUBMIT":
     	           return "Submit";
     	           break;
     	           
     	   case "IDS_WEB_CANCEL":
     	           return "Cancel";
     	           break;
     	   
     	   case "IDS_WEB_INTERNET_CONNECT":
     	           return "Select Internet Connection:";
     	           break; 
     	                	           
     	   case "IDS_WEB_INTERNET_MODEM":
     	           return "MODEM";
     	           break; 
     	           
     	   case "IDS_WEB_INTERNET_LAN":
     	           return "DSL/CABLE/T-1";
     	           break;
     	           
     	   case "IDS_WEB_VIEWCHANNEL":
     	           return "Select Multi Window:";
     	           break;
     	           
     	   case "IDS_WEB_CHANNEL1":
     	           return "1 Window";
     	           break;
     	           
     	   case "IDS_WEB_CHANNEL2":
     	           return "2 Windows";
     	           break; 
     	           
     	   case "IDS_WEB_MULTI_CHANNEL":
     	           return "Multi View";
     	           break; 
     	           
     	   case "IDS_WEB_EVENTLIST_QUERY":
     	           return "Event List Query:";
     	           break;
     	           
     	   case "IDS_WEB_EVENT_TYPE":
     	           return "Event Type";
     	           break; 
     	           
     	   case "IDS_WEB_DATE":
     	           return "Date";
     	           break;
     	           
     	   case "IDS_WEB_START_TIME":
     	           return "Start Time";
     	           break; 
     	           
     	   case "IDS_WEB_END_TIME":
     	           return "End Time";
     	           break; 
     	           
     	   case "IDS_REMOTE_MESSAGE1":
     	           return "Type the name of the remote computer you want to use, select the screen size for your connection, and then click Connect.";
     	           break; 
     	           
     	   case "IDS_REMOTE_TITLE":
     	           return "Remote Desktop Web Connection";
     	           break;
     	           
     	   case "IDS_REMOTE_MESSAGE2":
     	           return "When the connection page opens, you can add it to your Favorites for easy connection to the same computer.";
     	           break;
     	           
     	   case "IDS_REMOTE_SERVER":
     	           return "Server";
     	           break;
     	           
     	   case "IDS_REMOTE_RESOULATION":
     	           return "Size";
     	           break; 
     	           
     	   case "IDS_REMOTE_LOGIN_INFO":
     	           return "Send logon information for this connection";
     	           break; 
     	           
     	   case "IDS_WEB_EMAP":
     	           return "Emap";
     	           break;              
     	           
     	   case "IDS_WEB_ID_PWD_ERROR":
     	           return "ID or password error";
     	           break;
     	           
     	    case "IDS_WEB_OK":
     	           return "OK";
     	           break;     
     	           
     	        		
     	}
     	return ID;   

}


function  LoadString(ID) 
{
  document.write(GetXML(ID));
}

function  LoadGetString(ID) 
{
  return GetXML(ID);
}


function PlayFile(cam,sDate,sTime)
{
   //Dynamic Create MyWin HTML 
  MyWin=window.open("","Window","width=385,height=380,Resizeable=0,Top =250,Left=250,Menubar=no,toolbar=0,status=0");
  MyWin.document.open();
  
  //Write HTML Title
  MyWin.document.writeln("<html><head>"); 
  MyWin.document.writeln("<title>");  
  MyWin.document.writeln("Remote Play Search");
  MyWin.document.writeln("</title>");
  
  //Dynamic Create Java Script
  MyWin.document.writeln("<script language='JavaScript'>");
 
  //PlayFile Function
  MyWin.document.writeln("function PlayFile()");
  MyWin.document.writeln("{");
  MyWin.document.writeln("try {");
  MyWin.document.writeln("RPBX1.PlayFile("+cam+",'"+sDate+"','"+sTime+"'"+");");   
  MyWin.document.writeln("}");
  MyWin.document.writeln("catch(e){");
  //MyWin.document.writeln("alert('Please Download  WebCamX before');");
  MyWin.document.writeln("}");
  MyWin.document.writeln("}");
  
  //CreateX Function
  MyWin.document.writeln("function CreateX()");
  MyWin.document.writeln("{");
  MyWin.document.writeln("try {"); 
  MyWin.document.writeln("RPBX1.CreateX();");  
  MyWin.document.writeln("document.RPBX1.IpAddress= document.location;"); 
  //MyWin.document.writeln("document.RPBX1.IpAddress="+"'192.168.0.46'"+";");
  MyWin.document.writeln("document.RPBX1.CommandPort ="+ CommandPort+";");
  MyWin.document.writeln("document.RPBX1.DataPort ="+ DataPort+";");
  //MyWin.document.writeln("document.RPBX1.ShowTracker(false);");   	   
  MyWin.document.writeln("}");
  MyWin.document.writeln("catch(e){");
 // MyWin.document.writeln("alert('Please CrateX Error');");
  MyWin.document.writeln("}");
  MyWin.document.writeln("}");
  
  //DestroyX Function
  MyWin.document.writeln("function DestroyX()");
  MyWin.document.writeln("{");
  MyWin.document.writeln("try {");
  MyWin.document.writeln("RPBX1.DestroyX();");      
  MyWin.document.writeln("}");
  MyWin.document.writeln("catch(e){");
  //MyWin.document.writeln("alert('Please Download OCX Before');");
  MyWin.document.writeln("}");
  MyWin.document.writeln("}"); 
  
  //StopX Function
  MyWin.document.writeln("function StopX()");
  MyWin.document.writeln("{");
  MyWin.document.writeln("try {");
  MyWin.document.writeln("RPBX1.StopX();");      
  MyWin.document.writeln("}");
  MyWin.document.writeln("catch(e){");
  //MyWin.document.writeln("alert('Please Download OCX Before');");
  MyWin.document.writeln("}");
  MyWin.document.writeln("}"); 
  
  //SnapShotX Function
  MyWin.document.writeln("function SnapShotX()");
  MyWin.document.writeln("{");
  MyWin.document.writeln("try {");
  MyWin.document.writeln("RPBX1.SnapShotX();");      
  MyWin.document.writeln("}");
  MyWin.document.writeln("catch(e){");
  //MyWin.document.writeln("alert('Please Download OCX Before');");
  MyWin.document.writeln("}");
  MyWin.document.writeln("}"); 
  
  //PauseX Function
  MyWin.document.writeln("function PauseX()");
  MyWin.document.writeln("{");
  MyWin.document.writeln("try {");
  MyWin.document.writeln("RPBX1.PauseX();");      
  MyWin.document.writeln("}");
  MyWin.document.writeln("catch(e){");
  //MyWin.document.writeln("alert('Please Download OCX Before');");
  MyWin.document.writeln("}");
  MyWin.document.writeln("}"); 
    
  MyWin.document.writeln("</script>");   
  MyWin.document.writeln("</head>");
  
  
  MyWin.document.writeln("<body onBlur='self.focus()' OnLoad='CreateX()' OnUnload='DestroyX()'>");  
  //Form
  MyWin.document.writeln("<form name='ThisForm'>");
  MyWin.document.writeln("<p><input type='button' value='"+ str_PLAY+ "' name='Play' OnClick='PlayFile()'><input type='button' value='"+str_PAUSE+"' name='Pause' OnClick='PauseX()'><input type='button' value='"+str_STOP +"' name='Stop' OnClick='StopX()'></p>");
  MyWin.document.writeln("</form>");
  
  //RPBX Object
  MyWin.document.writeln("<p>");
  MyWin.document.writeln("<object classid='clsid:3C0B51F0-A0FD-415D-B16F-DFE587B87D71' id='RPBX1' width='355' height='315' codebase='cab/RPB.cab#version=6,1,0,0'>");         		
  MyWin.document.writeln("</object>"); 
  MyWin.document.writeln("</p>");
  MyWin.document.writeln("</body>"); 
  MyWin.document.writeln("</html>");
  MyWin.document.close(); 
}