/**********************************************************************
  BEGIN MODAL DIALOG CODE (can also be loaded as external .js file)
***********************************************************************/
// Global for brower version branching.
var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 4))
//var IE5  = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 5))

// One object tracks the current modal dialog opened from this window.
var dlg = new Object()
var dlgModal = new Object()
var dlgNoModal = new Object()

// Se inicializa el tipo de dialogo en No Modal
dlg.bModal = false;

// Generate a window that simulate a dialog.
// Parameters:
// 		bModal -- indicate if the dialog will be modal 
//    url -- URL of the page/frameset to be loaded into dialog
//    width -- pixel width of the dialog window
//    height -- pixel height of the dialog window
//    resizable -- [optional] resizable property 
//    returnFunc -- [optional] reference to the function (on this page)
//                  that is to act on the data returned from the dialog
//    args -- [optional] any data you need to pass to the dialog
function openDlg(bModal,url, width, height, resizable, scrollbars, returnFunc, args) {
	
	if (bModal==true)	
		dlg = dlgModal;
	else
		dlg = dlgNoModal;
		
	if (!dlg.win || (dlg.win && dlg.win.closed)) {
		// Initialize properties of the modal dialog object.
		dlg.returnFunc = returnFunc
		dlg.initValue = ""
		dlg.returnedValue = ""
		dlg.args = args
		dlg.bModal = bModal
		dlg.url = url
		dlg.width = width
		dlg.height = height
		dlg.resizable = resizable
		dlg.scrollbars = scrollbars
		// Keep name unique so Navigator doesn't overwrite an existing dialog.
		dlg.name = (new Date()).getSeconds().toString()
		// Assemble window attributes and try to center the dialog.
		if (Nav4) {
			// Center on the main window.
			var attr = "";
			dlg.left = window.screenX + 
			   ((window.outerWidth - dlg.width) / 2)
			dlg.top = window.screenY + 
			   ((window.outerHeight - dlg.height) / 2)
			if (Nav4) {
				attr += "screenX=" + dlg.left + 
			   	",screenY=" + dlg.top + ",width=" + 
			   	dlg.width + ",height=" + dlg.height;
			}
			else {
				attr += "left=" + dlg.left + ",top=" + 
			   	dlg.top + ",width=" + dlg.width + 
			   	",height=" + dlg.height;
			}
			if (dlg.resizable!=true)
					attr += ",resizable=no"
			else
					attr += ",resizable=yes"
			if (dlg.scrollbars!=true)
					attr += ",scrollbars=no"
			else
					attr += ",scrollbars=yes"
		} else {
			// The best we can do is center in screen.
			var attr = "";
			if (dlg.width>0 && dlg.height>0) {
				dlg.left = (screen.width - dlg.width) / 2;
				dlg.top = (screen.height - dlg.height) / 2;
			 	attr += "left=" + dlg.left + ",top=" + dlg.top;
			   attr += ",width=" + dlg.width + ",height=" + dlg.height;
				if (dlg.resizable!=true)
					attr += ",resizable=no"
				else
					attr += ",resizable=yes"
				if (dlg.scrollbars!=true)
					attr += ",scrollbars=no"
				else
					attr += ",scrollbars=yes"
			}
		}
		
		// Generate the dialog and make sure it has focus.
		// alert('attributos:'+attr)
		attr += ",dependent=yes"
		dlg.win=window.open(dlg.url, dlg.name, attr)
	} 
	dlg.win.focus()
}

function openDlgModal(url, width, height, resizable, scrollbars, returnFunc, args) {
	//Se sustitituye el valor de la variable pasada como filtro
	var partesUrl;
	var Variables;
	var partesVariables;
	var partesSubFuncion;
	var varFiltro;
	var valorFiltro;
		
	partesUrl = url.split("?");
	if (partesUrl && partesUrl[1]) {
		partesVariables = partesUrl[1].split("&");
		
		if (partesVariables && partesVariables[4])
			partesSubFuncion = partesVariables[4].split("|");
	}
	
	if (partesSubFuncion && partesSubFuncion[1]) {
		var urlAux;
		varFiltro = partesSubFuncion[1];
		valorFiltro = document.getElementById(varFiltro).value;
		urlAux = url;
		url = urlAux.replace(varFiltro,varFiltro+'='+valorFiltro);
	}
	
	openDlg(true,url, width, height, resizable, scrollbars, returnFunc, args)
}

function openDlgNoModal(url, width, height, resizable, scrollbars, returnFunc, args) {
	openDlg(false,url, width, height, resizable, scrollbars, returnFunc, args)
}





// Event handler to inhibit Navigator form element 
// and IE link activity when dialog window is active.
function deadend() {
	if (dlg.win && !dlg.win.closed) {
		dlg.win.focus()
		return false
	}
}

// Since links in IE4 cannot be disabled, preserve 
// IE link onclick event handlers while they're "disabled."
// Restore when re-enabling the main window.
var IELinkClicks

// Disable form elements and links in all frames for IE.
function disableForms() {
	IELinkClicks = new Array()
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = true
			}
		}
		IELinkClicks[h] = new Array()
		for (i = 0; i < frames[h].document.links.length; i++) {
			IELinkClicks[h][i] = frames[h].document.links[i].onclick
			frames[h].document.links[i].onclick = deadend
		}
	}
}

// Restore IE form elements and links to normal behavior.
function enableForms() {
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = false
			}
		}
		for (i = 0; i < frames[h].document.links.length; i++) {
			frames[h].document.links[i].onclick = IELinkClicks[h][i]
		}
	}
}

// Grab all Navigator events that might get through to form
// elements while dialog is open. For IE, disable form elements.
function blockEvents() {
	if (dlg.bModal==true) {
		if (Nav4) {
			window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
			window.onclick = deadend
		} else {
			disableForms()
		}
		window.onfocus = checkModal
	}
}
// As dialog closes, restore the main window's original
// event mechanisms.
function unblockEvents() {
	if (dlg.bModal==true) {
		if (Nav4) {
			window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
			window.onclick = null
			window.onfocus = null
		} else {
			enableForms()
		}
	}
}

// Invoked by onFocus event handler of EVERY frame,
// return focus to dialog window if it's open.
function checkModal() {

	if (dlg.win && !dlg.win.closed) {
		dlg.win.focus()	
	}

}

/**************************
  END MODAL DIALOG CODE
**************************/
