/**
 * Contains general JavaScript functions that are used by the DTS web application
 *
 * Copyright (c) 2002 Comprendium, Inc. All Rights Reserved.
 *
 * This file is part of the DTS project.
 */

function mOver(cell)
{
    if (! cell.contains(event.fromElement)) {
        cell.style.cursor = 'hand';
        cell.bgColor = '#c3ccd0';
    }
}

function mOut(cell)
{
    if (! cell.contains(event.toElement)) {
        cell.style.cursor = 'default';
        cell.bgColor = '#ffffff';
    }
}

function mClick(src) {
    if (event.srcElement.tagName=='TD') {
        src.children.tags('A')[0].click();
    }
}

/**
 * Pops up a confirmation window
 */
function showEnginePage(url) {
    var  engine = prompt('<bean:message key="admin.install.engine"/>', "????Engine");

    if (engine != null) {
        url = url + "compType=" + engine;	/* & has already been appended !!! */
        return showPage(url, "EngineConfig");
    }
}

/**
 * Opens a new browser window displaying the page specified by "path".
 */
function showPage(path, name) {

    var features='tool-bar=no,location=no,directories=no,menubar=yes,status=yes,scrollbars=yes,resizable=yes,copyhistory=no,alwayRaised=1,hotkeys=0';

    if ((navigator.appVersion.substring(0,1) > 3) && (screen.height > 700)) {
      features+= ',width=800';
      features+= ',height=600';
    }
    else {
      features+= ',fullscreen=no';
    }

    return window.open(path, name, features);
}

/**
 * Pops up a confirmation window
 */
function askConfirmation(message) {

    if (confirm(message)) {
        return true;
    }
    else {
        return false;
    }
}


/**
 * Pops up a confirmation window
 */
function askConfirmationHostRestartShutdown(message,url,whattodo) {
   if(confirm(message)) {
        goDoAminHostAction(whattodo,url);
      return true;
    }
    else {
        return false;
    }
}

/**
* Alric
 * Pops up a confirmation window
 */
function askConfirmationUninstall(message,url) {
  if (confirm(message)) {
        window.location.href=url;
        return true;
    }
    else {
        return false;
    }
}

/**
*  Alric
 * do host restart or shutdown
 */

function goDoAminHostAction(whattodo,url) {

    if(whattodo=="RESTART") {
      window.location.href=url;
    }

    if(whattodo=="SHUTDOWN") {
       window.location.href=url;
    }

}

/**
*   Alric
 *  stops a Component
 */
function doStopComponent(stopurl)
{
  window.location.href=stopurl;
}

/**
*   Alric
 *  start  a Component
 */
function doStartComponent(starturl)
{
  window.location.href=starturl;
}

/**
*   Alric
 *  calls showpage or showEnginePage based on
 *  the number of parameters called
 */
function getSelectInfo(hostName)
{
  //alert("Hostname: "+ hostName);
  //alert("Num of forms with name: "+ document.getElementsByName(hostName).length);
  //alert("Formname: "+document.getElementsByName(hostName)[0].name);
  //alert("Installmenu.value: "+ document.getElementsByName(hostName)[0].installmenu.value);
  var length=document.getElementsByName(hostName)[0].installmenu.length;
  //alert("Installmenu.length: "+ length);
  //alert("Installmenu.options[lenght-1].value: "+ document.getElementsByName(hostName)[0].installmenu.options[length-1].value);
  
  var indx=document.getElementsByName(hostName)[0].installmenu.selectedIndex;
  //alert("Installmenu.selectedIndex: "+ indx);
  var selectedinstalloption = document.getElementsByName(hostName)[0].installmenu.options[indx].value;
  //alert("Value of options[selectedIndex: "+ selectedinstalloption);
  
  
  showpageARG = selectedinstalloption.split(",");
  if(showpageARG.length>1) {
    showPage(showpageARG[0],showpageARG[1]);
  }
  else {
   showEnginePage(showpageARG[0]);
  }
  return false;
}

/**
 * Shows the instances that should be displayed.
 *
 * @param   type   the type of components that should be listed
 */
function showDTSInstances(request, type) {
    var  url = request + "/admin/hosts.do?selectedCompType=" + type;

    window.open(url, "_self");
}

/**
 * Generates HTML code that displays the translation
 *
 * @param	translation	the translation
 */
function writeTranslation(translation) {
	var txtarea = document.getElementsByName("text")[0];
	
    if ((frames.RESULT == null) || (frames.RESULT.document == null)) {
        alert("Error: Could not find result frame");
    }
    else {
        frames.RESULT.document.open();
        //frames.RESULT.width = 50; //txtarea.clientWidth;
        //frames.RESULT.height = 50; //txtarea.clientHeight;
        
        frames.RESULT.document.writeln("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.gencat.cat/web/gencat.dtd\">");
        frames.RESULT.document.writeln("<HTML xmlns=\"http://www.w3.org/1999/xhtml\">");
        frames.RESULT.document.writeln("<HEAD>");
        frames.RESULT.document.writeln("<link rel='stylesheet' type='text/css' href='styles/tradgen.css'>");
        frames.RESULT.document.writeln("<BODY TOPMARGIN=0 LEFTMARGIN=0 class=\"control\">");
        frames.RESULT.document.write("<FONT FACE='Verdana' SIZE='2em'>");
        frames.RESULT.document.write(translation);
        frames.RESULT.document.writeln("</FONT>");
        frames.RESULT.document.writeln("</BODY>");
        frames.RESULT.document.writeln("</HEAD>");
        frames.RESULT.document.writeln("</HTML>");
        frames.RESULT.document.close();
    }
}

/* From gencat.cat/js/funcions.js */

function ValidaParaulaCerca(){
	
		if(!document.getElementById) return;
		if(!document.getElementById("cercador")) return;
		
		var objFORM = document.getElementById("cercador");

		if(objFORM.nodeName.toLowerCase()!="form"){
			objFORM = objFORM.getElementsByTagName("form");
			
			if(objFORM.length>0){
				objFORM = objFORM[0];
			}else{
				return;
			}
		
		}
		
		objFORM.onsubmit = function(){

			var ca = "Si us plau, entreu les paraules a cercar.";
			var es = "Por favor, introduzca las palabras a buscar.";
			var en = "Type your search please.";
			var continuar = true;
			var idioma = "";
			
			if(document.documentElement.getAttribute && (document.documentElement.getAttribute("lang")!="" || document.documentElement.getAttribute("xml:lang")!="" )){
				idioma = (document.documentElement.getAttribute("lang")?document.documentElement.getAttribute("lang"):(document.documentElement.getAttribute("xml:lang")?document.documentElement.getAttribute("xml:lang"):""));
			}

			switch(document.getElementById("cerca").value){
				
				case "":
//					alert(idioma!=""?eval(idioma):ca);
					continuar = false;
					break;
				case "cercar":
					alert(idioma!=""?eval(idioma):ca);
					continuar = false;
					break;
				case "buscar":
					alert(idioma!=""?eval(idioma):es);
					continuar = false;
					break;
				case "search":
					alert(idioma!=""?eval(idioma):en);
					continuar = false;
					break;
			}
		
			if(continuar)
				return true;
			else{
				document.getElementById("cerca").focus();
				return false;
			}
			
		}
		
}

// addEvent(window, 'load', ValidaParaulaCerca);
window.onload = function() { ValidaParaulaCerca(); }
