//**********************************************************************
//  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))

// One object tracks the current modal dialog opened from this window.
var dialogWin = new Object()

// Generate a modal dialog.
// Parameters:
//    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
//    returnFunc -- 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 openWinDialog(url, width, height, returnFunc, args) {
	if (!dialogWin.win || (dialogWin.win && dialogWin.win.closed)) {
		// Initialize properties of the modal dialog object.
		dialogWin.returnFunc = returnFunc
		dialogWin.returnedValue = ""
		dialogWin.args = args
		dialogWin.url = url
		dialogWin.width = (width) ? width : "700";
		dialogWin.height = (height) ? height : "500";
		
		// 15/jul/2011 cesararnodl: acrescendo opcao de fullScreen
		if ( args == 'type=fullscreen' )
		{
			width=screen.availWidth
			height=screen.availHeight
			dialogWin.width = width
			dialogWin.height = height
		}

		// Keep name unique so Navigator doesn't overwrite an existing dialog.
		dialogWin.name = (new Date()).getSeconds().toString()
		// Assemble window attributes and try to center the dialog.
		if (Nav4) {
			// Center on the main window.
			dialogWin.left = window.screenX + 
			   ((window.outerWidth - dialogWin.width) / 2)
			dialogWin.top = window.screenY + 
			   ((window.outerHeight - dialogWin.height) / 2)
			var attr = "screenX=" + dialogWin.left + 
			   ",screenY=" + dialogWin.top + ",width=" + 
			   dialogWin.width + ",height=" + dialogWin.height
		} else {
			// The best we can do is center in screen.
			dialogWin.left = (screen.width - dialogWin.width) / 2
			dialogWin.top = (screen.height - dialogWin.height) / 2
			var attr = "left=" + dialogWin.left + ",top=" + 
			   dialogWin.top + ",width=" + dialogWin.width + 
			   ",height=" + dialogWin.height
		}

		//Seta mais alguns atributos da janela de Dialogo
		// 15/jul/2011 cesararnodl: acrescendo location=no
		attr += ", resizable=1, status=1, scrollbars=1, dependent=1, location=0, toolbar=0"
		/*
		status 		The status bar at the bottom of the window.
		toolbar 	The standard browser toolbar, with buttons such as Back and Forward.
		location 	The Location entry field where you enter the URL.
		menubar 	The menu bar of the window
		directories 	The standard browser directory buttons, such as What's New and What's Cool
		resizable 	Allow/Disallow the user to resize the window.
		scrollbars 	Enable the scrollbars if the document is bigger than the window
		height 		Specifies the height of the window in pixels. (example: height='350')
		width 		Specifies the width of the window in pixels.
		*/
		
		// Generate the dialog and make sure it has focus.
		dialogWin.win=window.open(dialogWin.url, dialogWin.name, attr)
		dialogWin.win.focus()
	} else {
		dialogWin.win.focus()
	}
}

// Event handler to inhibit Navigator form element 
// and IE link activity when dialog window is active.
function deadend() {
	if (dialogWin.win && !dialogWin.win.closed) {
		dialogWin.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
		}
		frames[h].window.onfocus = checkModal
    	frames[h].document.onclick = checkModal
	}
}

// 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 (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 (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() {
	setTimeout("finishChecking()", 50)
	return true
}

function finishChecking() {
	if (dialogWin.win && !dialogWin.win.closed) {
		dialogWin.win.focus() 
	}
}
//**************************
//  END MODAL DIALOG CODE
//**************************/

// Function to run upon closing the dialog with "OK".
function setPrefs() {
	// We're just displaying the returned value in a text box.
	var cod;
	var nome;
	var c = 0;
	while (dialogWin.returnedValue.charAt(c) != '|' && c < dialogWin.returnedValue.length) {c++;}
	cod = dialogWin.returnedValue.substring(0,c);
	nome = dialogWin.returnedValue.substring(c+1, dialogWin.returnedValue.length);
	document.retorno.identificadorPessoaDoador.value = cod;
	document.retorno.lbl_PessoaDoador.value = nome;
}

// Called by "Apply Settings" button to reload the page with
// search string data appended to URL.
function applySettings() {
	if (document.returned.searchURL.value) {
		location.href = "main.html" + document.returned.searchURL.value
	}
}

//*****************************************
//      BEGIN CUSTOM PAGE STYLE CODE
//******************************************/
// Convert location.search into an array of values
// indexed by name.
function getSearchAsArray() {
	var results = new Array()
	var input = unescape(location.search.substr(1))
	if (input) {
		var srchArray = input.split("&")
		var tempArray = new Array()
		for (i = 0; i < srchArray.length; i++) {
			tempArray = srchArray[i].split("=")
			results[tempArray[0]] = tempArray[1]
		}
	}
	return results
}
var srchData = getSearchAsArray()


// Close the dialog
function closeme() {
	parent.window.close()
}

// Handle click of OK button
function handleOK() {
	if (parent.opener && !parent.opener.closed) {
		transferData()
		parent.opener.dialogWin.returnFunc()
	} else {
		alert("You have closed the main window.\n\nNo action will be taken on the choices in this dialog box.")
	}
	closeme()
	return false
}

// Handle click of Cancel button
function handleCancel() {
	closeme()
	return false
}

// Generic function converts form element name-value pairs
// into a URL search string.
function getFormData(form) {
	var searchString = ""
	var onePair
	// Harvest values for each type of form element
	for (var i = 0; i < form.elements.length; i++) {
		/*if (form.elements[i].type == "text") {
			onePair = escape(form.elements[i].name) + "="
			onePair += escape(form.elements[i].value)
		} else if (form.elements[i].type.indexOf("select") != -1) {
			onePair = escape(form.elements[i].name) + "="
			onePair += escape(form.elements[i].options[form.elements[i].selectedIndex].value)
		} else if (form.elements[i].type == "radio") {
			onePair = escape(form.elements[i].name) + "="
			onePair += escape(form.elements[i].value)
		} else if (form.elements[i].type == "checkbox") {
			onePair = escape(form.elements[i].name) + "="
			onePair += escape(form.elements[i].value)
		} else if (form.elements[i].type == "hidden") {
			onePair = escape(form.elements[i].name) + "="
			onePair += escape(form.elements[i].value)
		} else continue
		searchString += onePair + "|"
		*/
		searchString = form.fdCod.value + "|" + form.fdNome.value;
	}
	return searchString
}

// Send gathered data to the dialog window object's returnedValue property
function transferData() {
	if (top.opener && !top.opener.closed) {
		top.opener.dialogWin.returnedValue = getFormData(document.frmRet);
	}
}

//============================================
// Funcoes Hemolife:
//============================================

function modal_seleciona(cod, nome) {
	parent.frames[1].document.frmRet.fdCod.value = cod;
	parent.frames[1].document.frmRet.fdNome.value = nome;
}
