var N=navigator.appName;
var V=navigator.appVersion;
var version="?";
var nom=N;
var os="ALL";
var osDetected="?";
var selectedLanguage="?";
var navigatorLanguage="?";
var url;
var paramUrl = new Array();

//if (window!=top) {top.location=window.location;} //pour chargement dans la page complete et pas dans une frame

function initDefault()
{
  paramUrl = getUrlParam();
  if(paramUrl["os"] == null)
  {
    detectParamNav();
  }
  else
  {
    os = paramUrl["os"];
  }
  if(paramUrl["lang"] == null)
  {
    changeLanguage("navigator");
  }
  else
  {
    document.getElementById(paramUrl["lang"]).selected=true;
  }
}

function initleg()
{
  paramUrl = getUrlParam();
  if(paramUrl["os"] == null)
  {
    detectParamNav();
  }
  else
  {
    os = paramUrl["os"];
  }
}

function initTop()
{
  if(detectNet())
  {
    window.document.getElementById("CD").style.display="none";
	window.document.getElementById("TITLE_CD").style.display="none";
    window.document.getElementById("WEB").style.display="block";
	window.document.getElementById("TITLE_WEB").style.display="block";
  }
  else
  {
    window.document.getElementById("CD").style.display="block";
	window.document.getElementById("TITLE_CD").style.display="block";
    window.document.getElementById("WEB").style.display="none";
	window.document.getElementById("TITLE_WEB").style.display="none";
  }
}

function initArray()
{
  if(detectNet())
  {
    window.document.getElementById("update").style.display="none";
  }
}

function langSetting(temp,set)
{
//alert(set);
	var lien;
	var langue;
    langue=temp;
    langue = langue.substring(0,2);
	if(langue=="fr")
    {
		if(set=="setting"){lien = "settings_fr.htm";}
		else if(set=="firmware") {lien = "firmware_fr.htm";}
		window.location.replace(lien);
		document.getElementById("fr").selected=true;
	}else if(langue=="en")
	{
		if(set=="setting"){lien = "settings_en.htm";}
		else if(set=="firmware") {lien = "firmware_en.htm";}
		window.location.replace(lien);
		document.getElementById("en").selected=true;
	}
}
	

function changeLanguage(lang)
  {
    var lien;
    var search;
    var language;
    if(lang=="navigator")
    {
      language = navigatorLanguage;
    }
    else
    {
      language=lang;
    }
    language = language.substring(0,2);
    selectedLanguage = language;
    if(language=="fr")
    {
      if(url != "default_fr.htm" && url != "DEFAULT_FR.HTM")
      {
        lien = "explore/fra/default_fr.htm";
        search = setUrlParam(window.location.search,"lang",language);
        lien += setUrlParam(search,"os",os);
        window.location.replace(lien);
      }
      else
      {
        document.getElementById("fr").selected=true;
      }
    }
    if(language=="en")
    {
      if(url != "default.htm" && url != "DEFAULT.HTM")
      {
        lien = "../../default.htm";
        search = setUrlParam(window.location.search,"lang",language);
        lien += setUrlParam(search,"os",os);
        window.location.replace(lien);
      }
      else
      {
        document.getElementById("en").selected=true;
      }
    }
  }


function detectParamNav()
{
  if (N=="Microsoft Internet Explorer") 
	{
		navigatorLanguage=navigator.systemLanguage;
		version=V.substring(V.indexOf("MSIE",0)+5,V.indexOf(";",V.indexOf("MSIE",0)));
		if (V.indexOf("Win",0)>0) 
		{
			if ( V.indexOf(";",V.indexOf("Win",0)) > 0 ) 
			{
				osDetected=V.substring(V.indexOf("Win",0),V.indexOf(";",V.indexOf("Win",0)));
			} else 
			{
				osDetected=V.substring(V.indexOf("Win",0),V.indexOf(")",V.indexOf("Win",0)));
			}
		}
		if (V.indexOf("Mac",0)>0) 
		{
			osDetected="Macintosh";
			version=V.substring(V.indexOf("MSIE",0)+5,V.indexOf("?",V.indexOf("MSIE",0)));
		}
	}
	if (N=="Opera") 
	{
		navigatorLanguage=navigator.language;
		version=V.substring(0,V.indexOf("(",0));
		osDetected=V.substring(V.indexOf("(",0)+1,V.indexOf(";",0));	
	}
	if (N=="Netscape") 
	{
		navigatorLanguage=navigator.language;
		if (navigator.vendor=="")  // Mozilla
		{
			version=(V.substring(0,V.indexOf("(",0)));
			nom="Mozilla";
			if (V.indexOf("Mac",0)>0) 
			{
				osDetected="Macintosh";
			}
			if (V.indexOf("Linux",0)>0) 
			{
				osDetected="Linux";
			}
			if (V.indexOf("Win",0)>0) 
			{
				osDetected=V.substring(V.indexOf("Win",0),V.indexOf(";",V.indexOf("Win",0)));
			}
			if (version==5) 
			{
				version="1";
			}
			if (navigator.oscpu) 
			{
				osDetected=navigator.oscpu;
			}
		}else 	// NS 4 ou 6
		{
      version=(V.substring(0,V.indexOf("(",0)));
			if (V.indexOf("Mac",0)>0) 
			{
				osDetected="Macintosh";
			}
			if (V.indexOf("Linux",0)>0) 
			{
				osDetected="Linux";
			}
			if (V.indexOf("Win",0)>0) 
			{
				osDetected=V.substring(V.indexOf("Win",0),V.indexOf(";",V.indexOf("Win",0)));
			}
			if (version==5) 
			{
				version="6.0";
				if (navigator.vendorSub!="")
        {
          version=navigator.vendorSub;
        }
			}
			if (navigator.oscpu) 
			{
				osDetected=navigator.oscpu;
			}
		}
	}
	V=V.toLowerCase();
  if(V.match("konqueror"))
	{
    var expression=new RegExp("linux");
    var exp=new RegExp("fr");
    if(V.match(expression))
    {
      osDetected="Linux";
    }
    if(V.match(exp))
    {
      navigatorLanguage = "fr";
    }
  }
  os = traitement(osDetected);
}

function getUrlParam()
{
  url = parent.location.href;
  var exp=new RegExp("[&?]+","g");
	var exp2=new RegExp("[=]+","g");
	var exp3=new RegExp("[/]+","g");
	var tabNom=url.split(exp);
  url = tabNom[0];
  url = url.split(exp3);
  url = url[url.length-1];
  var	tabParam=new Array();
	if (tabNom!=null)
  {
		for(var i=1;i<tabNom.length;i++)
   	{
      var tabTemp=tabNom[i].split(exp2);
			tabParam[tabTemp[0]]=tabTemp[1];
		}
	}
	return tabParam;
}

	

function setUrlParam(search, nameParam, valueParam)
{
  paramUrl=getUrlParam();
  if(search=="")
	{
    var param = "?";
    param += nameParam;
    param += "=";
    param += valueParam;
  }
  else
  {
    if(paramUrl[nameParam]== null)
    {
      var param = search;
      param += "&";
      param += nameParam;
      param += "=";
      param += valueParam;
    }
    else
    {
      if(paramUrl[nameParam]!= valueParam)
      {
        var param;
      	var exp=new RegExp(nameParam +"=","g");
      	var exp2=new RegExp("[&]+","g");
        var urlP = search;
        var tempParam;
        var tabParam = urlP.split(exp);
        tempParam=tabParam[1].split(exp2);
        var oldChaine = nameParam + "=" + tempParam[0];
        var newChaine = nameParam + "=" + valueParam;
        param=urlP.replace(oldChaine,newChaine);
      }
      if(paramUrl[nameParam]== valueParam)
      {
        var param = search;
      }
    }
  }
  return param;
}

function frameOut( link, target)
{
  link += parent.location.search;
  parent.open(link , target);
}

function openLink( link, target)
{
  link += setUrlParam(window.location.search,"os",os);
  window.open(link , target);
}

/**
 * Detect if the browser is the MGE UPS SYSTEMS browser
 */	
function fnIsOurCustomBrowser()
{
	//Check to see if this is our custom
	//browser. We just see if the function 
	//IsOurCustomBrowser is available. By leaving
	//the brackets off of our function it is treated
	//as a property and thus allows us to check if
	//our method exists in the external window. If it
	//returns null then obviously this is not our custom
	//browser, so we return false.
	detectParamNav();
  if(osDetected.substring(0,3)=="Win" && nom == "Microsoft Internet Explorer")
  {
    if(window.external.mgeIsOurCustomBrowser!=null)
		{  
      return true;
	  }
    else 
		{
      return false;
    }
	}
	else 
  {
    return false;
  }
}	
	
//setup a variable to let us know if this is our
//custom browser.

function fnCallFunction()
{
	//Call c++ function that we made
	//in our custom browser
	if(fnIsOurCustomBrowser())
		parent.external.mgeCustomFunction();
}	

function fnCallFunctionWithParams(strString, nNumber)
{
	//Call c++ function that accepts parameters 
	//that we made in our custom browser
	if(fnIsOurCustomBrowser())
		parent.external.mgeCustomFunctionWithParams(strString, nNumber);
}

/**
 * Open a window of MGE UPS SYSTEMS browser
 * @param strURL URL to open
 * @param nLeft width in pixels between the left of screen and the window 
 * @param nTop height in pixels between the top of screen and the window
 * @param nWidth width in pixels of window
 * @param nHeight height in pixels of window
 * @param nResizable window resizable or not
 */
	
function fnOpenWindow(strURL, nLeft, nTop, nWidth, nHeight, nResizable)
{
	if(fnIsOurCustomBrowser())
		parent.external.mgeOpenWindow(strURL, nLeft, nTop, nWidth, nHeight, nResizable);
}
  
/**
 * Open a modal dialog window
 * @param strURL URL to open
 * @param nLeft width in pixels between the left of screen and the window 
 * @param nTop height in pixels between the top of screen and the window
 * @param nWidth width in pixels of window
 * @param nHeight height in pixels of window
 */
	
function fnShowModalDialog(strURL, nLeft, nTop, nWidth, nHeight)
{
	if(fnIsOurCustomBrowser())
		parent.external.mgeShowModalDialog(strURL, nLeft, nTop, nWidth, nHeight);
}

/**
 * Open a modeless dialog window
 * @param strURL URL to open
 * @param nLeft width in pixels between the left of screen and the window 
 * @param nTop height in pixels between the top of screen and the window
 * @param nWidth width in pixels of window
 * @param nHeight height in pixels of window
 */
	
function fnShowModelessDialog(strURL, nLeft, nTop, nWidth, nHeight)
{
	if(fnIsOurCustomBrowser())
	parent.external.mgeShowModelessDialog(strURL, nLeft, nTop, nWidth, nHeight);
}
	
/**
 * to execute a file
 * @param strString Name of file to execute 
 * @param target target of window (_blank , _self ...)
 */	
	
function execute(strString , target)
{
	//Call c++ function that accepts parameters 
	//that we made in our custom browser
	var paramExe="height=600,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,titlebar=no,dependent=yes";
  var exp=new RegExp("explore|EXPLORE","g");
	var racineUrl = parent.location.href;
  racineUrl = racineUrl.split(exp);
  var link = racineUrl[0] + strString;
  //link = racineUrl[0] + "default.htm";
  //alert(strString + "\n" + link );
  if( link.match(".exe") != null && fnIsOurCustomBrowser() )
  {
    parent.external.mgeExec(link);
  }
  else
	{
		if ( fnIsOurCustomBrowser() )
    {
      fnOpenWindow( link, 0, 0, 800, 600, 1);
    }
    else
    {
      parent.open(link, target);//, paramExe);
    }
	}
}

function PopupImage(img)
{
  titre="Screen Shot"; // onclick='window.close()' onblur='window.close()'
  w=open("",'image','width=400,height=400,toolbar=no,scrollbars=no,resizable=yes'); 
  w.document.write("<HTML><HEAD><TITLE>"+titre+"</TITLE></HEAD>"); 
  w.document.write("<SCRIPT language=javascript>function checksize() { if (document.images[0].complete) { window.resizeTo(document.images[0].width+35,document.images[0].height+85); window.focus();} else { setTimeout('checksize()',250) } }</"+"SCRIPT>"); 
  w.document.write("<BODY onload='checksize()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0>");
  w.document.write("<TABLE width='100%' border='0' cellspacing='0' cellpadding='0' height='100%'><TR>");
  w.document.write("<TD valign='middle' align='center'><IMG src='"+img+"' border='0' onclick='window.close()'>"); 
  w.document.write("</TD></TR></TABLE>");
  w.document.write("</BODY></HTML>"); 
  w.document.close();
}

function show(file,txt)
{
  document.mainImage.src = file;
}

//Les parametre vont par 2 (lien vers l'image ; text du lien) 
function PopupImageMulti()
{
  if(PopupImage.arguments.length > 1)
  {
    titre="Screen Shot";
    w=open("",'image','width=400,height=400,toolbar=no,scrollbars=no,resizable=yes'); 
    w.document.write("<HTML><HEAD><TITLE>"+titre+"</TITLE>");
    w.document.write("<link rel='stylesheet' href='../../mgepage.css' type='text/css'>"); 
    w.document.write("</HEAD>"); 
    w.document.write("<SCRIPT language=javascript>function checksize() { if (document.mainImage.complete) { window.resizeTo(document.mainImage.width+30,document.mainImage.height+70); window.focus();} else { setTimeout('checksize()',250) } }</"+"SCRIPT>"); 
    w.document.write("<BODY onload='checksize()' onblur='window.close()' onclick='window.close()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0>");
    w.document.write("<TABLE width='100%' border='0' cellspacing='0' cellpadding='0' height='100%'>");
    w.document.write("<TR><TD align='center'>");
    for(var i = 0; i < PopupImage.arguments.length ; i = i + 2 )
    {
      w.document.write("<img src='../../images/newhome/bulred.gif' width='8' height='10' align='absmiddle' border='0'><a href=\"javascript:show('" + PopupImage.arguments[i] + "');\">" + PopupImage.arguments[i+1] + "</a>"); 
    }
    w.document.write("</TD></TD>");
    w.document.write("<TR>");
    w.document.write("<TD valign='middle' align='center'><IMG src='"+PopupImage.arguments[0]+"' border=0  id='mainImage' name='mainImage'>"); 
    w.document.write("</TD>");
    w.document.write("</TR>");
    w.document.write("</TABLE>");
    w.document.write("</BODY></HTML>"); 
    w.document.close(); 
  }
}
/**
 * to show a screen shot
 * @param strString Name of picture file to open 
 * @param target target of window (_blank , _self ...)
 * @param width width in pixels of window
 * @param height height in pixels of window
 */

function showScreenShot(strString , target, width , height)
{
	var paramSh="height=600,width=800,resizable=yes,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,titlebar=no,dependent=yes";
  /*if(fnIsOurCustomBrowser())
	{
		/*var exp=new RegExp("images|IMAGES","g");
    temp = strString.split(exp);
    var link = "images" + temp[1];*/
    //fnShowModelessDialog(strString,0,0,width,height);
	//}else{
		show_screenShot(strString);
    //parent.open(strString,target,paramSh);
	//}
}
	
/**
 * to show a popup
 * @param strString Name of file to open 
 * @param target target of window (_blank , _self ...)
 */

function showPopUp(strString , target)
{
	var paramPopUp="height=210,width=300,resizable=yes,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,titlebar=no,dependent=yes";
  if(fnIsOurCustomBrowser())
	{
		fnShowModelessDialog(strString,200,250,300,210);
	}else{
		parent.open(strString,target,paramPopUp);
	}
}

function showNSM(strString , target)
{
	var paramPopUp="height=302,width=502,resizable=no,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,titlebar=no,dependent=yes";
	parent.open(strString,target,paramPopUp);
}

function showDemo(strString , target)
{
	var paramPopUp="height=560,width=850,resizable=yes,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,titlebar=no,dependent=yes";
  parent.open(strString,target,paramPopUp);
}

	
/**
 * to show licence
 * @param strString Name of licence file to open 
 * @param target target of window (_blank , _self ...)
 */
	
function showLicence(strString , target, file, OS)
{
	var param="width=800,height=600,status=no,toolbar=no,menubar=no,resizable=yes,location=no,scrollbars=yes,titlebar=no,dependent=yes";
	search=parent.location.search;
  search = setUrlParam(search,"file",file);
  search = setUrlParam(search,"os",traitement(osDetected));
  strString += setUrlParam(search,"typeOs",OS);
  if(fnIsOurCustomBrowser())
	{
    fnShowModelessDialog(strString,0,0,800,600);
		//fnOpenWindow(reconstructionLien(strString),0,0,800,600);
	}
  else
  {
    parent.open(strString,target,param);
	}
}

/**
 * to show licence
 * @param strString Name of licence file to open 
 * @param target target of window (_blank , _self ...)
 */
	
function showUnix(strString , target, file)
{
	var param="height=600,width=800,status=no,toolbar=no,menubar=no,resizable=yes,location=no,scrollbars=yes,titlebar=no,dependent=yes";
	search=parent.location.search;
  strString += setUrlParam("","file",file);
  if(fnIsOurCustomBrowser())
	{
    //fnShowModelessDialog(strString,0,0,800,600);
    window.open(strString,target,param);
	}
  else
  {
		window.open(strString,target,param);
	}
}
function showCompat(strString , target)
{
	var paramPopUp="height=600,width=740,resizable=yes,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,titlebar=no,dependent=yes";
  if(fnIsOurCustomBrowser())
	{
		fnShowModelessDialog(strString,200,250,740,600);
	}else{
		parent.open(strString,target,paramPopUp);
	}
}


function showPre(strString , target)
{
	var paramPopUp="height=400,width=500,resizable=yes,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,titlebar=no,dependent=yes";
  if(fnIsOurCustomBrowser())
	{
		fnShowModelessDialog(strString,200,250,740,400);
	}else{
		parent.open(strString,target,paramPopUp);
	}
}

function showEmb(strString , target)
{
	var paramPopUp="height=600,width=800,resizable=yes,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,titlebar=no,dependent=yes";
  if(fnIsOurCustomBrowser())
	{
		fnShowModelessDialog(strString,100,100,800,600);
	}else{
		parent.open(strString,target,paramPopUp);
	}
}

function showNMP(strString , target)
{
	var paramPopUp="height=600,width=850,resizable=yes,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,titlebar=no,dependent=yes";
  if(fnIsOurCustomBrowser())
	{
		fnShowModelessDialog(strString,200,250,740,600);
	}else{
		parent.open(strString,target,paramPopUp);
	}
}
	
/**
 * to show a documentation
 * @param strString Name of documentation file to open 
 * @param target target of window (_blank , _self ...)
 */
	 
function showDoc (strString , target)
{
  var param="height=600,width=800,status=no,toolbar=no,menubar=yes,resizable=yes,location=no,scrollbars=yes,titlebar=no,dependent=yes";
  if(fnIsOurCustomBrowser())
	{
    fnOpenWindow(strString,10,10,800,600);
	}else{
		parent.open(strString,target,param);
	}
}
	
/**
 * open a dependent window
 * @param strString Name of file to open 
 * @param target target of window (_blank , _self ...)
 */
 
/**
 * Ajout de la boucle afin de pouvoir ouvrir correctement
 * les openWindows dans le CustomBrowser
 */
function openWindow(strString , target)
{
	var param="height=600,width=800,resizable=yes,status=no,menubar=no,location=no,toolbar=no,scrollbars=yes,dependent=yes,titlebar=no";  
  if(fnIsOurCustomBrowser())
	{
    fnOpenWindow(strString,10,10,800,600);
	}else{
		parent.open(strString,target,param);
	}
}

function openWindowTxt(strString , target)
{
	var param="height=600,width=800,resizable=yes,status=no,menubar=yes,location=no,toolbar=no,scrollbars=yes,dependent=yes,titlebar=no";  
  parent.open(strString,target,param); 
}
	
/**
 * Close a dependent window
 */
	 
function closeWindow()
{
	if(fnIsOurCustomBrowser())
	{
		parent.external.mgeClose();
	}
  else
  {
		parent.close();
	}
}
function detectNet()
{
  if(parent.location.href.match(".com") || parent.location.href.match("http://"))
  {
    return true;
  }
  else
  {
    return false;
  }
}

function traitement(osSelected)
{
  osSelected = osSelected.toLowerCase();
  if(osSelected.substring(0,3)=="win")
  {
    osSelected="WIN";
  }
  if(osSelected.substring(0,3)=="lin")
  {
    osSelected="LINUX";
  }
  if(osSelected.substring(0,3)=="mac" || osSelected.substring(0,3)=="PPC")
  {
    osSelected="MAC";
  }
  if(osSelected.substring(0,2)=="hp")
  {
    osSelected="HPUX";
  }
  return osSelected;
}

function built_menu()
{
	if(nom.toLowerCase()=='netscape' && navigator.appVersion.substring(0,1) <= '4')
		{
		aff = "<table><tr><td>";
		aff += "<A class='leftmenu' href=javascript:changeLanguage('en')>";
		aff += "<IMG border='0' height='10' src='explore/images/bulred.gif' width='8'>";
		aff += "English</A>";
		aff += "</td></tr><tr><td>";
		aff += "<A class='leftmenu' href=javascript:changeLanguage('fr')>";
		aff += "<IMG border='0' height='10' src='explore/images/bulred.gif' width='8'>";
		aff += "Fran&ccedil;ais</A>";
		aff += "</td></tr></table>";
		document.write(aff);
		}
	else
		{
		aff = "<SELECT class='leftmenu' id='selector' size='1' onChange='javascript:changeLanguage(this.value)'>";
		aff += "<OPTION id='en' value='en'>English </OPTION>";
		aff += "<OPTION id='fr' value='fr'>Français </OPTION>";
		aff += "</SELECT>";
		document.write(aff);
		}
}
	
