/* Mensaje v1.0; Sugerir Detalles v1.0 */
/* ©2008 */
/* www.housetic.com */
/* Software licenced */

var cajMsg01 = new Image();	//Activo
cajMsg01.src = "/images/CajMsg_01.png";
var cajMsg02 = new Image();	//Activo
cajMsg02.src = "/images/CajMsg_02.png";
var cajMsg03 = new Image();	//Activo
cajMsg03.src = "/images/CajMsg_03.png";
var cajMsg04 = new Image();	//Activo
cajMsg04.src = "/images/CajMsg_04.png";
var cajMsg05 = new Image();	//Activo
cajMsg05.src = "/images/CajMsg_05.png";
var cajMsg06 = new Image();	//Activo
cajMsg06.src = "/images/CajMsg_06.png";
var cajMsg07 = new Image();	//Activo
cajMsg07.src = "/images/CajMsg_07.png";
var cajMsg08 = new Image();	//Activo
cajMsg08.src = "/images/CajMsg_08.png";
var cajMsg09 = new Image();	//Activo
cajMsg09.src = "/images/CajMsg_09.png";
var cajMsg10 = new Image();	//Activo
cajMsg10.src = "/images/CajMsg_10.png";
var cajMsg11 = new Image();	//Activo
cajMsg11.src = "/images/CajMsg_11.png";
var cajMsg12 = new Image();	//Activo
cajMsg12.src = "/images/CajMsg_12.png";
var cajMsg13 = new Image();	//Activo
cajMsg13.src = "/images/CajMsg_13.png";
var cajMsg14 = new Image();	//Activo
cajMsg14.src = "/images/CajMsg_14.png";

var icoMsg01 = new Image();	//Activo
icoMsg01.src = "/images/alert.png";
var icoMsg02 = new Image();	//Activo
icoMsg02.src = "/images/question.png";
var icoMsg03 = new Image();	//Activo
icoMsg03.src = "/images/ok.png";
var icoMsg04 = new Image();	//Activo
icoMsg04.src = "/images/loading.gif";

//Clase mensaje
function message(ico, buttons, title, message) {
    this.resetData = function() {
	//Variables
	this.ico = ico;				//Nombre de icono ej: ok.png
	this.buttons = buttons;		//1 Mostrar aceptar 0 no mostrar
	this.title = title;			//Título
	this.message = message;		//Mensaje
	//--------------------------------------------------
	this.visible = 0;		//Estado de la capa 0 oculta 1 visible
	this.icoCancel = '<img src="/images/CajMsg_07.png" style="cursor:pointer;" onclick="">';
	//Aceptar SE HA DE PONER CON EL IDIOMA
	this.ok ='<a href="#" onclick=""><b>Aceptar</b></a>';
	this.box = null;
	this.boxContent = '<table border="0" cellpadding="0" cellspacing="0">'
	+'<tr><td class="CajMsg_01"></td><td colspan="3" class="CajMsg_02"></td><td class="CajMsg_03"></td></tr>'
	+'<tr><td class="CajMsg_04"></td><td class="CajMsg_06" onMouseDown="dragStart(event, \'divMsg\')"><img src="/images/CajMsg_05.png"></td>'
	+'<td align="center" valign="top" class="CajMsg_06" onMouseDown="dragStart(event, \'divMsg\')">'
	+'<div id="divTitle" class="CajMsg_ti">Alta de usuarios</div>'
	+'</td><td  id="tdIcoCancel" align="right" class="CajMsg_06"><img src="/images/CajMsg_07.png" style="cursor:pointer;"></td>'
	+'<td class="CajMsg_08"></td></tr>'
	+'<tr><td class="CajMsg_09"></td><td colspan="3" class="CajMsg_10">'
	+'<table><tr><td id="tdIco"><img id="imgIco"></td><td>'
	+'<div id="divMessage" class="CajMsg_ms">El usuario se ha dado de alta<br>'
	+'Para acceder realice el login</div>'
	+'</td></tr><tr><td></td><td id="tdOk" align="right"><a href="#"><b>Aceptar</b></a></td></tr></table>'
	+'</td><td class="CajMsg_11"></td>'
	+'</tr><tr><td class="CajMsg_12"></td><td colspan="3" class="CajMsg_13"></td><td class="CajMsg_14"></td>'
	+'</tr></table></div>';
        
	//Icono por defecto
	if(this.ico == null) this.ico = "ok.png";
	if(this.buttons == null) this.buttons = 0;
	this.op = 0;
	this.funOk = "";
        this.funCancel = "";
	this.name = "msg";
    };

    //Mostrar mensaje o recargar contenido
    this.showMessage = function(){
	var box = document.getElementById("divMsg");
	if(box == null){
		//montar caja
		this.box = document.createElement("div");
		this.box.className = "CajMsg";
		this.box.innerHTML = this.boxContent;
		this.box.id = "divMsg";
		//Añadir la capa a la capa de la foto
		var body = document.getElementsByTagName("body").item(0);  //document.getElementsByTagName("body")[0]
		body.appendChild(this.box);
		this.setOp("divMsg", 0);	//Ocultar tabla

                var objFondoMsg = document.createElement("div");
		objFondoMsg.setAttribute('id','CajMsg_fondo');
		objFondoMsg.style.display = 'none';
		//objFondoMsg.onclick = function() { this.funCancel; }
		body.appendChild(objFondoMsg);
                
		//cargar elementos
		this.mountContent();
		this.center();		//Centrar
		this.showIn("divMsg");	//Mostrar

	}else{
		//cargar elementos
                this.box = box;
		this.mountContent();
		this.center();		//Centrar
		if(this.visible==0){
			this.showIn("divMsg");	//Mostrar
		}
	}
        	
    };

    //Centro de la pantalla
    this.center = function(){
	var cen = new clientInfo();
	this.box.style.left = cen.cX - 100 + "px"; 	//100 ancho/2 aprox
	this.box.style.top = cen.cY - 80 + "px";	//100 ancho/2 aprox
    };

    //Mostrar elementos con transparencias
    this.showIn = function(id){
	var obj = document.getElementById(id);
        if(obj!=null){
          obj.style.display = 'block';
          //this.visible = 1;
          if(this.op == 0){
            var objFondo = document.getElementById("CajMsg_fondo");
            if (objFondo!=null){
              hideSelectBoxes();
	      hideFlash();
              var arrayPageSize = getPageSize();
	      objFondo.style.height = arrayPageSize[1] + "px";
              objFondo.style.display = 'block';
            }
          }
          //this.op ++;
          var num;
          if (browser.version >= 7){
            num = 10;
            if (this.op <= num) this.op++; 
          }else{
            num = 9;
            if (this.op <= num) this.op++; 
          }
	  if(this.op <= num){
	    this.setOp(id, this.op);
	    setTimeout (this.name + ".showIn('" + id + "')", 50);
	  }else{
            this.visible = 1;
          }
	}	
    };

    //Ocultar elemento con transpatencia
    this.showOut = function(id){
	var obj = document.getElementById(id);
	if(obj!=null){
		this.op --;
		if(this.op >= 0){
			this.setOp(id, this.op);
			setTimeout (this.name + ".showOut('" + id + "')", 50);
		}else{
                  this.visible = 0;
                  obj.style.display="none";
                  var objFondo = document.getElementById("CajMsg_fondo");
                  if (objFondo!=null){
                    objFondo.style.display = 'none';
                    showSelectBoxes();
		    showFlash();
                  }
                }
	}	
    };

    //Establecer la opacidad de un elemento
    this.setOp = function(id, value) {
	var obj = document.getElementById(id);
	if(id!=null){
		if(value == 10){
                   if (browser.isIE) {
                     obj.style.filter = "";
                   }else{
                     obj.style.opacity = 1;
                   }
		}
		else{
                   if (browser.isIE) {
		     obj.style.filter = "alpha ( opacity = " + value * 10 + ")";
                   }else{	
                     obj.style.opacity = value / 10;
		   }	
		}	
	}
    };

    //Lanzar una función msgEv'x' cuando se hace clic en aceptar
    //this.runEvent = function(e){
//	if(ev!=0){setTimeout("msgEv" + ev + "()",50);}
//	if(okHide!=0){this.hideMessage();}
//	//Para que no ejecute el link
//  	if(e && e.preventDefault){
//    	  e.preventDefault(); // DOM style
//	}
//	return false; // IE style
//    };

    //Montar el contenido del mensaje
    this.mountContent = function(){
	//Título
	var title = document.getElementById("divTitle");
	title.removeChild(title.lastChild);
	title.innerHTML = this.title;
	//Mensaje
	var message = document.getElementById("divMessage");
	message.removeChild(message.lastChild);
	message.innerHTML = this.message;
	//Icono cancelar
	var icoCancel = document.getElementById("tdIcoCancel");
	if (icoCancel.childNodes.length > 0) icoCancel.removeChild(icoCancel.lastChild);
	//Botón aceptar
	var ok = document.getElementById("tdOk");
	if (ok.childNodes.length > 0) ok.removeChild(ok.lastChild);
	//Eventos
	if(this.buttons == 1){
		icoCancel.innerHTML = (this.icoCancel).replace('onclick=""','onclick="' + this.funCancel + ';return false;"');
		ok.innerHTML = (this.ok).replace('onclick=""','onclick="' + this.funOk + ';return false;"');
		//Activar los eventos
	//	if (icoCancel.addEventListener) {
	//		//Para el FireFox
    	//	icoCancel.addEventListener('click', this.hideMessage, false);
    	//	ok.addEventListener('click', this.runEvent, false);
    	//} else if (icoCancel.attachEvent) {
	//		//Para el IExplorer
	//		icoCancel.attachEvent('onclick', this.hideMessage);
	//		ok.attachEvent('onclick', this.runEvent);
	//	}		
	}
	var ico = document.getElementById("imgIco");
	ico.src = "/images/" + this.ico;
    };

    //Ocultar la ventans de mensaje
    this.hideMessage = function(){
        if (this.visible != 1){
          setTimeout(this.name + ".hideMessage()",50);
        }else{
	  this.showOut("divMsg");
        }

	//Para destruir la caja
	//var box = document.getElementById("divMsg");
	//var body = document.getElementsByTagName("body")[0];
	//body.removeChild(box);	
    };

    this.resetData();
}

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// ---------------------------------------------------

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

// ---------------------------------------------------

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}


// ---------------------------------------------------

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Help from Ran Bar-On [ran2103@gmail.com]
//

function pause(ms){
	var date = new Date();
	curDate = null;
	do{var curDate = new Date();}
	while( curDate - date < ms);
}
/*
function pause(numberMillis) {
	var curently = new Date().getTime() + sender;
	while (new Date().getTime();	
}
*/
// ---------------------------------------------------


//-------------------------------------------------------------------------
// 								MENSAJES
//-------------------------------------------------------------------------
var msgEdit = new message();
var ajaxSolicitud = new myAjax();
var ajaxMessage = new myAjax();
var msgCodigo = "";
var msgTitle = "";

//Sugerir Detalles
function sugerirDet(campo){
	msgTitle = campo.title;
	var msg = 'Escriba el codigo de verificacion<br><div id="imagenCodigo"></div><br><input id="msgCodigoDet" name="codigo" type="text" size="26">';
        msgEdit.title = "Detalles";
	msgEdit.message = msg;
	msgEdit.buttons = 1;
	msgEdit.ico = "lamp.png";
	msgEdit.name = "msgEdit";
        msgEdit.funCancel = "msgEdit.hideMessage()";
        msgEdit.funOk = "msgEv1()";
	msgEdit.showMessage();
        fnActualizarCodigo();
        
}
function msgEv1(){
	//SUBSTITUIR POR AJAX
	msgCodigo = document.getElementById("msgCodigoDet").value;
	 
        var msg = msgTitle.replace('-', '<br>');
	msg += '<br><input id="msgDet" name="valor" type="text" size="26"><input id="msgCampoDet" name="campo" value="' + msgTitle + '" type="hidden" size="26">';
	
        msgEdit.message = msg;
        msgEdit.funCancel = "msgEdit.hideMessage()";
        msgEdit.funOk = "msgEv1Enviar()";
	msgEdit.showMessage();
	
}
function msgEv1Enviar(){
	//SUBSTITUIR POR AJAX
	var campo = document.getElementById("msgDet").value;
	var valor = document.getElementById("msgCampoDet").value;
	var codigo = msgCodigo;
        ajaxSolicitud = new myAjax();
	ajaxSolicitud.setVar("campo", campo); // recomended method of setting data to be parsed.
	ajaxSolicitud.setVar("valor", valor); // recomended method of setting data to be parsed.
	ajaxSolicitud.setVar("codigo", codigo); // recomended method of setting data to be parsed.
        ajaxSolicitud.requestFile = "/manage/ope/OpeSolicitarDetalle.jsp";
        ajaxSolicitud.method = "GET";
        ajaxSolicitud.onLoading = msgEv1Cargando;
        ajaxSolicitud.onCompletion = msgEv1Cargada;
        ajaxSolicitud.onError = fnSugerenciasErrorAjax;
        ajaxSolicitud.runAJAX();
}
function msgEv1Cargando(){
  msgEdit.message = "Procesando solicitud ...";
  msgEdit.buttons = 1;
  msgEdit.ico = "loading.gif";
  msgEdit.name = "msgEdit";
  msgEdit.funOk = "msgEdit.hideMessage()";
  msgEdit.funCancel = "msgEdit.hideMessage()";
  msgEdit.showMessage();
}
function msgEv1Cargada(){
  if (ajaxSolicitud.responseStatus){
    var string = eval(ajaxSolicitud.response);
    msgEdit.title=string[0];
    msgEdit.message = string[1];
    msgEdit.ico=string[2];
    msgEdit.buttons = string[3];
    msgEdit.name = "msgEdit";
    msgEdit.funOk = "msgEdit.hideMessage()";
    msgEdit.funCancel = "msgEdit.hideMessage()";
    msgEdit.showMessage();
    setTimeout ("msgEdit.hideMessage();", 3000);
  }else{
    fnSugerenciasErrorAjax();  
  }
}
//-----------------------------------------------------

//Sugerir Observaciones
function sugerirObs(campo){
	msgTitle = campo.title;
	var msg = 'Escriba el codigo de verificacion<br><div id="imagenCodigo"></div><br><input id="msgCodigoObs" name="codigo" type="text" size="26">';
        msgEdit.title = "Observaciones";
	msgEdit.message = msg;
	msgEdit.buttons = 1;
	msgEdit.ico = "lamp.png";
	msgEdit.name = "msgEdit";
        msgEdit.funCancel = "msgEdit.hideMessage()";
        msgEdit.funOk = "msgEv2()";
	msgEdit.showMessage();
        fnActualizarCodigo();
        
}
function msgEv2(){
	//SUBSTITUIR POR AJAX
	msgCodigo = document.getElementById("msgCodigoObs").value;
	 
        var msg = msgTitle.replace('-', '<br>');
	msg += '<br><input id="msgObs" name="valor" type="text" size="26"><input id="msgCampoObs" name="campo" value="' + msgTitle + '" type="hidden" size="26">';
	
        msgEdit.message = msg;
        msgEdit.funCancel = "msgEdit.hideMessage()";
        msgEdit.funOk = "msgEv2Enviar()";
	msgEdit.showMessage();
	
}
function msgEv2Enviar(){
	//SUBSTITUIR POR AJAX
	var campo = document.getElementById("msgObs").value;
	var valor = document.getElementById("msgCampoObs").value;
	var codigo = msgCodigo;
        ajaxSolicitud = new myAjax();
	ajaxSolicitud.setVar("campo", campo); // recomended method of setting data to be parsed.
	ajaxSolicitud.setVar("valor", valor); // recomended method of setting data to be parsed.
	ajaxSolicitud.setVar("codigo", codigo); // recomended method of setting data to be parsed.
        ajaxSolicitud.requestFile = "/manage/ope/OpeSolicitarDetalle.jsp";
        ajaxSolicitud.method = "GET";
        ajaxSolicitud.onLoading = msgEv2Cargando;
        ajaxSolicitud.onCompletion = msgEv2Cargada;
        ajaxSolicitud.onError = fnSugerenciasErrorAjax;
        ajaxSolicitud.runAJAX();
}
function msgEv2Cargando(){
  msgEdit.message = "Procesando solicitud ...";
  msgEdit.buttons = 1;
  msgEdit.ico = "loading.gif";
  msgEdit.name = "msgEdit";
  msgEdit.funOk = "msgEdit.hideMessage()";
  msgEdit.funCancel = "msgEdit.hideMessage()";
  msgEdit.showMessage();
}
function msgEv2Cargada(){
  if (ajaxSolicitud.responseStatus){
    var string = eval(ajaxSolicitud.response);
    msgEdit.title=string[0];
    msgEdit.message = string[1];
    msgEdit.ico=string[2];
    msgEdit.buttons = string[3];
    msgEdit.name = "msgEdit";
    msgEdit.funOk = "msgEdit.hideMessage()";
    msgEdit.funCancel = "msgEdit.hideMessage()";
    msgEdit.showMessage();
    setTimeout ("msgEdit.hideMessage();", 3000);
  }else{
    fnSugerenciasErrorAjax();  
  }
}
//-----------------------------------------------------

//Actualizar codigo
function fnActualizarCodigo(){
  ajaxMessage = new myAjax();
  ajaxMessage.requestFile = "/mod/ModImagenClave.jsp";
  ajaxMessage.method = "GET";
  ajaxMessage.onLoading = fnActualizarCodigoCargando;
  ajaxMessage.onCompletion = fnActualizarCodigoCargada;
  ajaxMessage.onError = fnSugerenciasErrorAjax;
  ajaxMessage.runAJAX();
}
function fnActualizarCodigoCargando(){
  var img = document.getElementById("imagenCodigo");
  img.innerHTML = "<img src=\"/images/loading.gif\" height=\"70\">";
}
function fnActualizarCodigoCargada(){
  if (ajaxMessage.responseStatus){
    var string = ajaxMessage.response;
    var img = document.getElementById("imagenCodigo");
    img.innerHTML = string;
    //eval("document." + formularioAlta + ".codigo").value = "";
  }else{
    fnSugerenciasErrorAjax();  
  }
}
function fnSugerenciasErrorAjax(){
  msgEdit.title="Información";
  msgEdit.message = "No se ha podido realizar la operación.</br>Vuelva intentarlo.";
  msgEdit.buttons = 1;
  msgEdit.ico = "alert.png";
  msgEdit.name = "msgEdit";
  msgEdit.funOk = "msgEdit.hideMessage()";
  msgEdit.funCancel = "msgEdit.hideMessage()";
  msgEdit.showMessage();
}
//---------------------------------------------------
